2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/dhcp.h>
26 #include <ipxe/settings.h>
27 #include <ipxe/x509.h>
28 #include <ipxe/privkey.h>
34 * Life would in theory be easier if we could use a single file to
35 * hold both the certificate and corresponding private key.
36 * Unfortunately, the only common format which supports this is
37 * PKCS#12 (aka PFX), which is too ugly to be allowed anywhere near my
38 * codebase. See, for reference and amusement:
40 * http://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html
43 /* Allow private key to be overridden if not explicitly specified */
45 #define ALLOW_KEY_OVERRIDE 0
47 #define ALLOW_KEY_OVERRIDE 1
50 /* Raw private key data */
51 extern char private_key_data[];
52 extern char private_key_len[];
53 __asm__ ( ".section \".rodata\", \"a\", @progbits\n\t"
54 "\nprivate_key_data:\n\t"
56 ".incbin \"" PRIVATE_KEY "\"\n\t"
57 #endif /* PRIVATE_KEY */
58 ".size private_key_data, ( . - private_key_data )\n\t"
59 ".equ private_key_len, ( . - private_key_data )\n\t"
63 struct asn1_cursor private_key = {
64 .data = private_key_data,
65 .len = ( ( size_t ) private_key_len ),
68 /** Private key setting */
69 static struct setting privkey_setting __setting ( SETTING_CRYPTO, privkey ) = {
71 .description = "Private key",
73 .type = &setting_type_hex,
77 * Apply private key configuration settings
79 * @ret rc Return status code
81 static int privkey_apply_settings ( void ) {
82 static void *key_data = NULL;
85 /* Allow private key to be overridden only if not explicitly
86 * specified at build time.
88 if ( ALLOW_KEY_OVERRIDE ) {
90 /* Restore default private key */
91 private_key.data = private_key_data;
92 private_key.len = ( ( size_t ) private_key_len );
94 /* Fetch new private key, if any */
96 if ( ( len = fetch_raw_setting_copy ( NULL, &privkey_setting,
97 &key_data ) ) >= 0 ) {
98 private_key.data = key_data;
99 private_key.len = len;
104 if ( private_key.len ) {
105 DBGC ( &private_key, "PRIVKEY using %s private key:\n",
106 ( key_data ? "external" : "built-in" ) );
107 DBGC_HDA ( &private_key, 0, private_key.data, private_key.len );
109 DBGC ( &private_key, "PRIVKEY has no private key\n" );
115 /** Private key settings applicator */
116 struct settings_applicator privkey_applicator __settings_applicator = {
117 .apply = privkey_apply_settings,