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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
29 #include <ipxe/dhcp.h>
30 #include <ipxe/settings.h>
31 #include <ipxe/x509.h>
32 #include <ipxe/privkey.h>
38 * Life would in theory be easier if we could use a single file to
39 * hold both the certificate and corresponding private key.
40 * Unfortunately, the only common format which supports this is
41 * PKCS#12 (aka PFX), which is too ugly to be allowed anywhere near my
42 * codebase. See, for reference and amusement:
44 * http://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html
47 /* Allow private key to be overridden if not explicitly specified */
49 #define ALLOW_KEY_OVERRIDE 0
51 #define ALLOW_KEY_OVERRIDE 1
54 /* Raw private key data */
55 extern char private_key_data
[];
56 extern char private_key_len
[];
57 __asm__ ( ".section \".rodata\", \"a\", " PROGBITS
"\n\t"
58 "\nprivate_key_data:\n\t"
60 ".incbin \"" PRIVATE_KEY
"\"\n\t"
61 #endif /* PRIVATE_KEY */
62 ".size private_key_data, ( . - private_key_data )\n\t"
63 ".equ private_key_len, ( . - private_key_data )\n\t"
67 struct private_key private_key
= {
68 .refcnt
= REF_INIT ( ref_no_free
),
70 .data
= private_key_data
,
71 .len
= ( ( size_t ) private_key_len
),
75 /** Default private key */
76 static struct asn1_cursor default_private_key
= {
77 .data
= private_key_data
,
78 .len
= ( ( size_t ) private_key_len
),
81 /** Private key setting */
82 static struct setting privkey_setting
__setting ( SETTING_CRYPTO
, privkey
) = {
84 .description
= "Private key",
86 .type
= &setting_type_hex
,
92 * @v refcnt Reference counter
94 void privkey_free ( struct refcnt
*refcnt
) {
95 struct private_key
*key
=
96 container_of ( refcnt
, struct private_key
, refcnt
);
98 free ( key
->builder
.data
);
103 * Apply private key configuration settings
105 * @ret rc Return status code
107 static int privkey_apply_settings ( void ) {
108 static void *key_data
= NULL
;
111 /* Allow private key to be overridden only if not explicitly
112 * specified at build time.
114 if ( ALLOW_KEY_OVERRIDE
) {
116 /* Restore default private key */
117 memcpy ( &private_key
.builder
, &default_private_key
,
118 sizeof ( private_key
.builder
) );
120 /* Fetch new private key, if any */
122 if ( ( len
= fetch_raw_setting_copy ( NULL
, &privkey_setting
,
123 &key_data
) ) >= 0 ) {
124 private_key
.builder
.data
= key_data
;
125 private_key
.builder
.len
= len
;
130 if ( private_key
.builder
.len
) {
131 DBGC ( &private_key
, "PRIVKEY using %s private key:\n",
132 ( key_data ?
"external" : "built-in" ) );
133 DBGC_HDA ( &private_key
, 0, private_key
.builder
.data
,
134 private_key
.builder
.len
);
136 DBGC ( &private_key
, "PRIVKEY has no private key\n" );
142 /** Private key settings applicator */
143 struct settings_applicator privkey_applicator __settings_applicator
= {
144 .apply
= privkey_apply_settings
,