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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** Private key setting */
  66. static struct setting privkey_setting __setting ( SETTING_CRYPTO, privkey ) = {
  67. .name = "privkey",
  68. .description = "Private key",
  69. .tag = DHCP_EB_KEY,
  70. .type = &setting_type_hex,
  71. };
  72. /**
  73. * Apply private key configuration settings
  74. *
  75. * @ret rc Return status code
  76. */
  77. static int privkey_apply_settings ( void ) {
  78. static void *key_data = NULL;
  79. int len;
  80. /* Allow private key to be overridden only if not explicitly
  81. * specified at build time.
  82. */
  83. if ( ALLOW_KEY_OVERRIDE ) {
  84. /* Restore default private key */
  85. private_key.data = private_key_data;
  86. private_key.len = ( ( size_t ) private_key_len );
  87. /* Fetch new private key, if any */
  88. free ( key_data );
  89. if ( ( len = fetch_raw_setting_copy ( NULL, &privkey_setting,
  90. &key_data ) ) >= 0 ) {
  91. private_key.data = key_data;
  92. private_key.len = len;
  93. }
  94. }
  95. /* Debug */
  96. if ( private_key.len ) {
  97. DBGC ( &private_key, "PRIVKEY using %s private key:\n",
  98. ( key_data ? "external" : "built-in" ) );
  99. DBGC_HDA ( &private_key, 0, private_key.data, private_key.len );
  100. } else {
  101. DBGC ( &private_key, "PRIVKEY has no private key\n" );
  102. }
  103. return 0;
  104. }
  105. /** Private key settings applicator */
  106. struct settings_applicator privkey_applicator __settings_applicator = {
  107. .apply = privkey_apply_settings,
  108. };