You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

privkey.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ipxe/dhcp.h>
  28. #include <ipxe/settings.h>
  29. #include <ipxe/x509.h>
  30. #include <ipxe/privkey.h>
  31. /** @file
  32. *
  33. * Private key
  34. *
  35. * Life would in theory be easier if we could use a single file to
  36. * hold both the certificate and corresponding private key.
  37. * Unfortunately, the only common format which supports this is
  38. * PKCS#12 (aka PFX), which is too ugly to be allowed anywhere near my
  39. * codebase. See, for reference and amusement:
  40. *
  41. * http://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html
  42. */
  43. /* Allow private key to be overridden if not explicitly specified */
  44. #ifdef PRIVATE_KEY
  45. #define ALLOW_KEY_OVERRIDE 0
  46. #else
  47. #define ALLOW_KEY_OVERRIDE 1
  48. #endif
  49. /* Raw private key data */
  50. extern char private_key_data[];
  51. extern char private_key_len[];
  52. __asm__ ( ".section \".rodata\", \"a\", " PROGBITS "\n\t"
  53. "\nprivate_key_data:\n\t"
  54. #ifdef PRIVATE_KEY
  55. ".incbin \"" PRIVATE_KEY "\"\n\t"
  56. #endif /* PRIVATE_KEY */
  57. ".size private_key_data, ( . - private_key_data )\n\t"
  58. ".equ private_key_len, ( . - private_key_data )\n\t"
  59. ".previous\n\t" );
  60. /** Private key */
  61. struct asn1_cursor private_key = {
  62. .data = private_key_data,
  63. .len = ( ( size_t ) private_key_len ),
  64. };
  65. /** Default private key */
  66. static struct asn1_cursor default_private_key = {
  67. .data = private_key_data,
  68. .len = ( ( size_t ) private_key_len ),
  69. };
  70. /** Private key setting */
  71. static struct setting privkey_setting __setting ( SETTING_CRYPTO, privkey ) = {
  72. .name = "privkey",
  73. .description = "Private key",
  74. .tag = DHCP_EB_KEY,
  75. .type = &setting_type_hex,
  76. };
  77. /**
  78. * Apply private key configuration settings
  79. *
  80. * @ret rc Return status code
  81. */
  82. static int privkey_apply_settings ( void ) {
  83. static void *key_data = NULL;
  84. int len;
  85. /* Allow private key to be overridden only if not explicitly
  86. * specified at build time.
  87. */
  88. if ( ALLOW_KEY_OVERRIDE ) {
  89. /* Restore default private key */
  90. memcpy ( &private_key, &default_private_key,
  91. sizeof ( private_key ) );
  92. /* Fetch new private key, if any */
  93. free ( key_data );
  94. if ( ( len = fetch_raw_setting_copy ( NULL, &privkey_setting,
  95. &key_data ) ) >= 0 ) {
  96. private_key.data = key_data;
  97. private_key.len = len;
  98. }
  99. }
  100. /* Debug */
  101. if ( private_key.len ) {
  102. DBGC ( &private_key, "PRIVKEY using %s private key:\n",
  103. ( key_data ? "external" : "built-in" ) );
  104. DBGC_HDA ( &private_key, 0, private_key.data, private_key.len );
  105. } else {
  106. DBGC ( &private_key, "PRIVKEY has no private key\n" );
  107. }
  108. return 0;
  109. }
  110. /** Private key settings applicator */
  111. struct settings_applicator privkey_applicator __settings_applicator = {
  112. .apply = privkey_apply_settings,
  113. };