2 * Copyright (c) 2007, Cameron Rich
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Load certificates/keys into memory. These can be in many different formats.
33 * PEM support and other formats can be processed here.
35 * The PEM private keys may be optionally encrypted with AES128 or AES256.
36 * The encrypted PEM keys were generated with something like:
38 * openssl genrsa -aes128 -passout pass:abcd -out axTLS.key_aes128.pem 512
48 static int do_obj(SSL_CTX
*ssl_ctx
, int obj_type
,
49 SSLObjLoader
*ssl_obj
, const char *password
);
50 #ifdef CONFIG_SSL_HAS_PEM
51 static int ssl_obj_PEM_load(SSL_CTX
*ssl_ctx
, int obj_type
,
52 SSLObjLoader
*ssl_obj
, const char *password
);
56 * Load a file into memory that is in binary DER (or ascii PEM) format.
58 EXP_FUNC
int STDCALL
ssl_obj_load(SSL_CTX
*ssl_ctx
, int obj_type
,
59 const char *filename
, const char *password
)
61 #ifndef CONFIG_SSL_SKELETON_MODE
62 static const char * const begin
= "-----BEGIN";
64 SSLObjLoader
*ssl_obj
= NULL
;
68 ret
= SSL_ERROR_INVALID_KEY
;
72 ssl_obj
= (SSLObjLoader
*)calloc(1, sizeof(SSLObjLoader
));
73 ssl_obj
->len
= get_file(filename
, &ssl_obj
->buf
);
74 if (ssl_obj
->len
<= 0)
76 ret
= SSL_ERROR_INVALID_KEY
;
80 /* is the file a PEM file? */
81 if (strstr((char *)ssl_obj
->buf
, begin
) != NULL
)
83 #ifdef CONFIG_SSL_HAS_PEM
84 ret
= ssl_obj_PEM_load(ssl_ctx
, obj_type
, ssl_obj
, password
);
86 printf(unsupported_str
);
87 ret
= SSL_ERROR_NOT_SUPPORTED
;
91 ret
= do_obj(ssl_ctx
, obj_type
, ssl_obj
, password
);
94 ssl_obj_free(ssl_obj
);
97 printf(unsupported_str
);
98 return SSL_ERROR_NOT_SUPPORTED
;
99 #endif /* CONFIG_SSL_SKELETON_MODE */
103 * Transfer binary data into the object loader.
105 EXP_FUNC
int STDCALL
ssl_obj_memory_load(SSL_CTX
*ssl_ctx
, int mem_type
,
106 const uint8_t *data
, int len
, const char *password
)
109 SSLObjLoader ssl_obj
;
112 ret
= do_obj(ssl_ctx
, mem_type
, &ssl_obj
, password
);
118 * Actually work out what we are doing
120 static int do_obj(SSL_CTX
*ssl_ctx
, int obj_type
,
121 SSLObjLoader
*ssl_obj
, const char *password
)
127 case SSL_OBJ_RSA_KEY
:
128 ret
= add_private_key(ssl_ctx
, ssl_obj
);
131 case SSL_OBJ_X509_CERT
:
132 ret
= add_cert(ssl_ctx
, ssl_obj
->buf
, ssl_obj
->len
);
135 #ifdef CONFIG_SSL_CERT_VERIFICATION
136 case SSL_OBJ_X509_CACERT
:
137 add_cert_auth(ssl_ctx
, ssl_obj
->buf
, ssl_obj
->len
);
141 #ifdef CONFIG_SSL_USE_PKCS12
143 ret
= pkcs8_decode(ssl_ctx
, ssl_obj
, password
);
147 ret
= pkcs12_decode(ssl_ctx
, ssl_obj
, password
);
151 printf(unsupported_str
);
152 ret
= SSL_ERROR_NOT_SUPPORTED
;
162 void ssl_obj_free(SSLObjLoader
*ssl_obj
)
172 * Support for PEM encoded keys/certificates.
174 #ifdef CONFIG_SSL_HAS_PEM
176 #define NUM_PEM_TYPES 4
178 #define IS_RSA_PRIVATE_KEY 0
179 #define IS_ENCRYPTED_PRIVATE_KEY 1
180 #define IS_PRIVATE_KEY 2
181 #define IS_CERTIFICATE 3
183 static const char * const begins
[NUM_PEM_TYPES
] =
185 "-----BEGIN RSA PRIVATE KEY-----",
186 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
187 "-----BEGIN PRIVATE KEY-----",
188 "-----BEGIN CERTIFICATE-----",
191 static const char * const ends
[NUM_PEM_TYPES
] =
193 "-----END RSA PRIVATE KEY-----",
194 "-----END ENCRYPTED PRIVATE KEY-----",
195 "-----END PRIVATE KEY-----",
196 "-----END CERTIFICATE-----",
199 static const char * const aes_str
[2] =
201 "DEK-Info: AES-128-CBC,",
202 "DEK-Info: AES-256-CBC,"
206 * Take a base64 blob of data and decrypt it (using AES) into its
209 static int pem_decrypt(const char *where
, const char *end
,
210 const char *password
, SSLObjLoader
*ssl_obj
)
219 uint8_t key
[32]; /* AES256 size */
221 if (password
== NULL
|| strlen(password
) == 0)
223 #ifdef CONFIG_SSL_FULL_MODE
224 printf("Error: Need a password for this PEM file\n"); TTY_FLUSH();
229 if ((start
= strstr((const char *)where
, aes_str
[0]))) /* AES128? */
231 start
+= strlen(aes_str
[0]);
233 else if ((start
= strstr((const char *)where
, aes_str
[1]))) /* AES256? */
236 start
+= strlen(aes_str
[1]);
240 #ifdef CONFIG_SSL_FULL_MODE
241 printf("Error: Unsupported password cipher\n"); TTY_FLUSH();
246 /* convert from hex to binary - assumes uppercase hex */
247 for (i
= 0; i
< IV_SIZE
; i
++)
249 char c
= *start
++ - '0';
250 iv
[i
] = (c
> 9 ? c
+ '0' - 'A' + 10 : c
) << 4;
252 iv
[i
] += (c
> 9 ? c
+ '0' - 'A' + 10 : c
);
255 while (*start
== '\r' || *start
== '\n')
258 /* turn base64 into binary */
259 pem_size
= (int)(end
-start
);
260 if (base64_decode(start
, pem_size
, ssl_obj
->buf
, &ssl_obj
->len
) != 0)
263 /* work out the key */
265 MD5_Update(&md5_ctx
, (const uint8_t *)password
, strlen(password
));
266 MD5_Update(&md5_ctx
, iv
, SALT_SIZE
);
267 MD5_Final(key
, &md5_ctx
);
272 MD5_Update(&md5_ctx
, key
, MD5_SIZE
);
273 MD5_Update(&md5_ctx
, (const uint8_t *)password
, strlen(password
));
274 MD5_Update(&md5_ctx
, iv
, SALT_SIZE
);
275 MD5_Final(&key
[MD5_SIZE
], &md5_ctx
);
278 /* decrypt using the key/iv */
279 AES_set_key(&aes_ctx
, key
, iv
, is_aes_256
? AES_MODE_256
: AES_MODE_128
);
280 AES_convert_key(&aes_ctx
);
281 AES_cbc_decrypt(&aes_ctx
, ssl_obj
->buf
, ssl_obj
->buf
, ssl_obj
->len
);
289 * Take a base64 blob of data and turn it into its proper ASN.1 form.
291 static int new_pem_obj(SSL_CTX
*ssl_ctx
, int is_cacert
, char *where
,
292 int remain
, const char *password
)
294 int ret
= SSL_ERROR_BAD_CERTIFICATE
;
295 SSLObjLoader
*ssl_obj
= NULL
;
299 int i
, pem_size
, obj_type
;
300 char *start
= NULL
, *end
= NULL
;
302 for (i
= 0; i
< NUM_PEM_TYPES
; i
++)
304 if ((start
= strstr(where
, begins
[i
])) &&
305 (end
= strstr(where
, ends
[i
])))
307 remain
-= (int)(end
-where
);
308 start
+= strlen(begins
[i
]);
309 pem_size
= (int)(end
-start
);
311 ssl_obj
= (SSLObjLoader
*)calloc(1, sizeof(SSLObjLoader
));
313 /* 4/3 bigger than what we need but so what */
314 ssl_obj
->buf
= (uint8_t *)calloc(1, pem_size
);
315 ssl_obj
->len
= pem_size
;
317 if (i
== IS_RSA_PRIVATE_KEY
&&
318 strstr(start
, "Proc-Type:") &&
319 strstr(start
, "4,ENCRYPTED"))
321 /* check for encrypted PEM file */
322 if (pem_decrypt(start
, end
, password
, ssl_obj
) < 0)
324 ret
= SSL_ERROR_BAD_CERTIFICATE
;
330 ssl_obj
->len
= pem_size
;
331 if (base64_decode(start
, pem_size
,
332 ssl_obj
->buf
, &ssl_obj
->len
) != 0)
334 ret
= SSL_ERROR_BAD_CERTIFICATE
;
341 case IS_RSA_PRIVATE_KEY
:
342 obj_type
= SSL_OBJ_RSA_KEY
;
345 case IS_ENCRYPTED_PRIVATE_KEY
:
347 obj_type
= SSL_OBJ_PKCS8
;
351 obj_type
= is_cacert
?
352 SSL_OBJ_X509_CACERT
: SSL_OBJ_X509_CERT
;
356 ret
= SSL_ERROR_BAD_CERTIFICATE
;
360 /* In a format we can now understand - so process it */
361 if ((ret
= do_obj(ssl_ctx
, obj_type
, ssl_obj
, password
)))
364 end
+= strlen(ends
[i
]);
365 remain
-= strlen(ends
[i
]);
366 while (remain
> 0 && (*end
== '\r' || *end
== '\n'))
377 ssl_obj_free(ssl_obj
);
383 ssl_obj_free(ssl_obj
);
388 * Load a file into memory that is in ASCII PEM format.
390 static int ssl_obj_PEM_load(SSL_CTX
*ssl_ctx
, int obj_type
,
391 SSLObjLoader
*ssl_obj
, const char *password
)
395 /* add a null terminator */
397 ssl_obj
->buf
= (uint8_t *)realloc(ssl_obj
->buf
, ssl_obj
->len
);
398 ssl_obj
->buf
[ssl_obj
->len
-1] = 0;
399 start
= (char *)ssl_obj
->buf
;
400 return new_pem_obj(ssl_ctx
, obj_type
== SSL_OBJ_X509_CACERT
,
401 start
, ssl_obj
->len
, password
);
403 #endif /* CONFIG_SSL_HAS_PEM */
406 * Load the key/certificates in memory depending on compile-time and user
409 int load_key_certs(SSL_CTX
*ssl_ctx
)
412 uint32_t options
= ssl_ctx
->options
;
413 #ifdef CONFIG_SSL_GENERATE_X509_CERT
414 uint8_t *cert_data
= NULL
;
416 static const char *dn
[] =
418 CONFIG_SSL_X509_COMMON_NAME
,
419 CONFIG_SSL_X509_ORGANIZATION_NAME
,
420 CONFIG_SSL_X509_ORGANIZATION_UNIT_NAME
424 /* do the private key first */
425 if (strlen(CONFIG_SSL_PRIVATE_KEY_LOCATION
) > 0)
427 if ((ret
= ssl_obj_load(ssl_ctx
, SSL_OBJ_RSA_KEY
,
428 CONFIG_SSL_PRIVATE_KEY_LOCATION
,
429 CONFIG_SSL_PRIVATE_KEY_PASSWORD
)) < 0)
432 else if (!(options
& SSL_NO_DEFAULT_KEY
))
434 #if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
435 // static const /* saves a few more bytes */
436 //#include "private_key.h"
437 // ssl_obj_memory_load(ssl_ctx, SSL_OBJ_RSA_KEY, default_private_key,
438 // default_private_key_len, NULL);
442 /* now load the certificate */
443 #ifdef CONFIG_SSL_GENERATE_X509_CERT
444 if ((cert_size
= ssl_x509_create(ssl_ctx
, 0, dn
, &cert_data
)) < 0)
450 ssl_obj_memory_load(ssl_ctx
, SSL_OBJ_X509_CERT
, cert_data
, cert_size
, NULL
);
453 if (strlen(CONFIG_SSL_X509_CERT_LOCATION
))
455 if ((ret
= ssl_obj_load(ssl_ctx
, SSL_OBJ_X509_CERT
,
456 CONFIG_SSL_X509_CERT_LOCATION
, NULL
)) < 0)
459 else if (!(options
& SSL_NO_DEFAULT_KEY
))
461 #if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
462 static const /* saves a few bytes and RAM */
464 ssl_obj_memory_load(ssl_ctx
, SSL_OBJ_X509_CERT
,
465 default_certificate
, default_certificate_len
, NULL
);
471 #ifdef CONFIG_SSL_FULL_MODE
474 printf("Error: Certificate or key not loaded\n"); TTY_FLUSH();