Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

privkey.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ipxe/dhcp.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/x509.h>
  26. #include <ipxe/privkey.h>
  27. /** @file
  28. *
  29. * Private key
  30. *
  31. * Life would in theory be easier if we could use a single file to
  32. * hold both the certificate and corresponding private key.
  33. * Unfortunately, the only common format which supports this is
  34. * PKCS#12 (aka PFX), which is too ugly to be allowed anywhere near my
  35. * codebase. See, for reference and amusement:
  36. *
  37. * http://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html
  38. */
  39. /* Allow private key to be overridden if not explicitly specified */
  40. #ifdef PRIVATE_KEY
  41. #define ALLOW_KEY_OVERRIDE 0
  42. #else
  43. #define ALLOW_KEY_OVERRIDE 1
  44. #endif
  45. /* Raw private key data */
  46. extern char private_key_data[];
  47. extern char private_key_len[];
  48. __asm__ ( ".section \".rodata\", \"a\", @progbits\n\t"
  49. "\nprivate_key_data:\n\t"
  50. #ifdef PRIVATE_KEY
  51. ".incbin \"" PRIVATE_KEY "\"\n\t"
  52. #endif /* PRIVATE_KEY */
  53. ".size private_key_data, ( . - private_key_data )\n\t"
  54. ".equ private_key_len, ( . - private_key_data )\n\t"
  55. ".previous\n\t" );
  56. /** Private key */
  57. struct asn1_cursor private_key = {
  58. .data = private_key_data,
  59. .len = ( ( size_t ) private_key_len ),
  60. };
  61. /** Private key setting */
  62. static struct setting privkey_setting __setting ( SETTING_CRYPTO, privkey ) = {
  63. .name = "privkey",
  64. .description = "Private key",
  65. .tag = DHCP_EB_KEY,
  66. .type = &setting_type_hex,
  67. };
  68. /**
  69. * Apply private key configuration settings
  70. *
  71. * @ret rc Return status code
  72. */
  73. static int privkey_apply_settings ( void ) {
  74. static void *key_data = NULL;
  75. int len;
  76. /* Allow private key to be overridden only if not explicitly
  77. * specified at build time.
  78. */
  79. if ( ALLOW_KEY_OVERRIDE ) {
  80. /* Restore default private key */
  81. private_key.data = private_key_data;
  82. private_key.len = ( ( size_t ) private_key_len );
  83. /* Fetch new private key, if any */
  84. free ( key_data );
  85. if ( ( len = fetch_raw_setting_copy ( NULL, &privkey_setting,
  86. &key_data ) ) >= 0 ) {
  87. private_key.data = key_data;
  88. private_key.len = len;
  89. }
  90. }
  91. /* Debug */
  92. if ( private_key.len ) {
  93. DBGC ( &private_key, "PRIVKEY using %s private key:\n",
  94. ( key_data ? "external" : "built-in" ) );
  95. DBGC_HDA ( &private_key, 0, private_key.data, private_key.len );
  96. } else {
  97. DBGC ( &private_key, "PRIVKEY has no private key\n" );
  98. }
  99. return 0;
  100. }
  101. /** Private key settings applicator */
  102. struct settings_applicator privkey_applicator __settings_applicator = {
  103. .apply = privkey_apply_settings,
  104. };