Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / privkey.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ipxe/dhcp.h>
26 #include <ipxe/settings.h>
27 #include <ipxe/x509.h>
28 #include <ipxe/privkey.h>
29
30 /** @file
31  *
32  * Private key
33  *
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:
39  *
40  *    http://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html
41  */
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
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"
55 #ifdef PRIVATE_KEY
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"
60           ".previous\n\t" );
61
62 /** Private key */
63 struct asn1_cursor private_key = {
64         .data = private_key_data,
65         .len = ( ( size_t ) private_key_len ),
66 };
67
68 /** Private key setting */
69 static struct setting privkey_setting __setting ( SETTING_CRYPTO, privkey ) = {
70         .name = "privkey",
71         .description = "Private key",
72         .tag = DHCP_EB_KEY,
73         .type = &setting_type_hex,
74 };
75
76 /**
77  * Apply private key configuration settings
78  *
79  * @ret rc              Return status code
80  */
81 static int privkey_apply_settings ( void ) {
82         static void *key_data = NULL;
83         int len;
84
85         /* Allow private key to be overridden only if not explicitly
86          * specified at build time.
87          */
88         if ( ALLOW_KEY_OVERRIDE ) {
89
90                 /* Restore default private key */
91                 private_key.data = private_key_data;
92                 private_key.len = ( ( size_t ) private_key_len );
93
94                 /* Fetch new private key, if any */
95                 free ( key_data );
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;
100                 }
101         }
102
103         /* Debug */
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 );
108         } else {
109                 DBGC ( &private_key, "PRIVKEY has no private key\n" );
110         }
111
112         return 0;
113 }
114
115 /** Private key settings applicator */
116 struct settings_applicator privkey_applicator __settings_applicator = {
117         .apply = privkey_apply_settings,
118 };