Debian Codegraph @

ccast/axTLS/loader.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2007-2014, Cameron Rich
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
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.
17 *
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.
29 */
30
31 /**
32 * Load certificates/keys into memory. These can be in many different formats.
33 * PEM support and other formats can be processed here.
34 *
35 * The PEM private keys may be optionally encrypted with AES128 or AES256.
36 * The encrypted PEM keys were generated with something like:
37 *
38 * openssl genrsa -aes128 -passout pass:abcd -out axTLS.key_aes128.pem 512
39 */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include "os_port.h"
45 #include "ssl.h"
46
47 static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
48 SSLObjLoader *ssl_obj, const char *password);
49 #ifdef CONFIG_SSL_HAS_PEM
50 static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type,
51 SSLObjLoader *ssl_obj, const char *password);
52 #endif
53
54 /*
55 * Load a file into memory that is in binary DER (or ascii PEM) format.
56 */
57 EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
58 const char *filename, const char *password)
59 {
60 #ifndef CONFIG_SSL_SKELETON_MODE
61 static const char * const begin = "-----BEGIN";
62 int ret = SSL_OK;
63 SSLObjLoader *ssl_obj = NULL;
64
65 if (filename == NULL)
66 {
67 ret = SSL_ERROR_INVALID_KEY;
68 goto error;
69 }
70
71 ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
72 ssl_obj->len = get_file(filename, &ssl_obj->buf);
73 if (ssl_obj->len <= 0)
74 {
75 ret = SSL_ERROR_INVALID_KEY;
76 goto error;
77 }
78
79 /* is the file a PEM file? */
80 if (strstr((char *)ssl_obj->buf, begin) != NULL)
81 {
82 #ifdef CONFIG_SSL_HAS_PEM
83 ret = ssl_obj_PEM_load(ssl_ctx, obj_type, ssl_obj, password);
84 #else
85 #ifdef CONFIG_DEBUG
86 printf("%s", unsupported_str);
87 #endif
88 ret = SSL_ERROR_NOT_SUPPORTED;
89 #endif
90 }
91 else
92 ret = do_obj(ssl_ctx, obj_type, ssl_obj, password);
93
94 error:
95 ssl_obj_free(ssl_obj);
96 return ret;
97 #else
98 #ifdef CONFIG_DEBUG
99 printf("%s", unsupported_str);
100 #endif
101 return SSL_ERROR_NOT_SUPPORTED;
102 #endif /* CONFIG_SSL_SKELETON_MODE */
103 }
104
105 /*
106 * Transfer binary data into the object loader.
107 */
108 EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int mem_type,
109 const uint8_t *data, int len, const char *password)
110 {
111 int ret;
112 SSLObjLoader *ssl_obj;
113
114 ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
115 ssl_obj->buf = (uint8_t *)malloc(len);
116 memcpy(ssl_obj->buf, data, len);
117 ssl_obj->len = len;
118 ret = do_obj(ssl_ctx, mem_type, ssl_obj, password);
119 ssl_obj_free(ssl_obj);
120 return ret;
121 }
122
123 /*
124 * Actually work out what we are doing
125 */
126 static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
127 SSLObjLoader *ssl_obj, const char *password)
128 {
129 int ret = SSL_OK;
130
131 switch (obj_type)
132 {
133 case SSL_OBJ_RSA_KEY:
134 ret = add_private_key(ssl_ctx, ssl_obj);
135 break;
136
137 case SSL_OBJ_X509_CERT:
138 ret = add_cert(ssl_ctx, ssl_obj->buf, ssl_obj->len);
139 break;
140
141 #ifdef CONFIG_SSL_CERT_VERIFICATION
142 case SSL_OBJ_X509_CACERT:
143 add_cert_auth(ssl_ctx, ssl_obj->buf, ssl_obj->len);
144 break;
145 #endif
146
147 #ifdef CONFIG_SSL_USE_PKCS12
148 case SSL_OBJ_PKCS8:
149 ret = pkcs8_decode(ssl_ctx, ssl_obj, password);
150 break;
151
152 case SSL_OBJ_PKCS12:
153 ret = pkcs12_decode(ssl_ctx, ssl_obj, password);
154 break;
155 #endif
156 default:
157 #ifdef CONFIG_DEBUG
158 printf("%s", unsupported_str);
159 #endif
160 ret = SSL_ERROR_NOT_SUPPORTED;
161 break;
162 }
163
164 return ret;
165 }
166
167 /*
168 * Clean up our mess.
169 */
170 void ssl_obj_free(SSLObjLoader *ssl_obj)
171 {
172 if (ssl_obj)
173 {
174 free(ssl_obj->buf);
175 free(ssl_obj);
176 }
177 }
178
179 /*
180 * Support for PEM encoded keys/certificates.
181 */
182 #ifdef CONFIG_SSL_HAS_PEM
183
184 #define NUM_PEM_TYPES 4
185 #define IV_SIZE 16
186 #define IS_RSA_PRIVATE_KEY 0
187 #define IS_ENCRYPTED_PRIVATE_KEY 1
188 #define IS_PRIVATE_KEY 2
189 #define IS_CERTIFICATE 3
190
191 static const char * const begins[NUM_PEM_TYPES] =
192 {
193 "-----BEGIN RSA PRIVATE KEY-----",
194 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
195 "-----BEGIN PRIVATE KEY-----",
196 "-----BEGIN CERTIFICATE-----",
197 };
198
199 static const char * const ends[NUM_PEM_TYPES] =
200 {
201 "-----END RSA PRIVATE KEY-----",
202 "-----END ENCRYPTED PRIVATE KEY-----",
203 "-----END PRIVATE KEY-----",
204 "-----END CERTIFICATE-----",
205 };
206
207 static const char * const aes_str[2] =
208 {
209 "DEK-Info: AES-128-CBC,",
210 "DEK-Info: AES-256-CBC,"
211 };
212
213 /**
214 * Take a base64 blob of data and decrypt it (using AES) into its
215 * proper ASN.1 form.
216 */
217 static int pem_decrypt(const char *where, const char *end,
218 const char *password, SSLObjLoader *ssl_obj)
219 {
220 int ret = -1;
221 int is_aes_256 = 0;
222 char *start = NULL;
223 uint8_t iv[IV_SIZE];
224 int i, pem_size;
225 MD5_CTX md5_ctx;
226 AES_CTX aes_ctx;
227 uint8_t key[32]; /* AES256 size */
228
229 if (password == NULL || strlen(password) == 0)
230 {
231 #ifdef CONFIG_DEBUG
232 printf("Error: Need a password for this PEM file\n");
233 #endif
234 goto error;
235 }
236
237 if ((start = strstr((const char *)where, aes_str[0]))) /* AES128? */
238 {
239 start += strlen(aes_str[0]);
240 }
241 else if ((start = strstr((const char *)where, aes_str[1]))) /* AES256? */
242 {
243 is_aes_256 = 1;
244 start += strlen(aes_str[1]);
245 }
246 else
247 {
248 #ifdef CONFIG_DEBUG
249 printf("Error: Unsupported password cipher\n");
250 #endif
251 goto error;
252 }
253
254 /* convert from hex to binary - assumes uppercase hex */
255 for (i = 0; i < IV_SIZE; i++)
256 {
257 char c = *start++ - '0';
258 iv[i] = (c > 9 ? c + '0' - 'A' + 10 : c) << 4;
259 c = *start++ - '0';
260 iv[i] += (c > 9 ? c + '0' - 'A' + 10 : c);
261 }
262
263 while (*start == '\r' || *start == '\n')
264 start++;
265
266 /* turn base64 into binary */
267 pem_size = (int)(end-start);
268 if (base64_decode(start, pem_size, ssl_obj->buf, &ssl_obj->len) != 0)
269 goto error;
270
271 /* work out the key */
272 MD5_Init(&md5_ctx);
273 MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password));
274 MD5_Update(&md5_ctx, iv, SALT_SIZE);
275 MD5_Final(key, &md5_ctx);
276
277 if (is_aes_256)
278 {
279 MD5_Init(&md5_ctx);
280 MD5_Update(&md5_ctx, key, MD5_SIZE);
281 MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password));
282 MD5_Update(&md5_ctx, iv, SALT_SIZE);
283 MD5_Final(&key[MD5_SIZE], &md5_ctx);
284 }
285
286 /* decrypt using the key/iv */
287 AES_set_key(&aes_ctx, key, iv, is_aes_256 ? AES_MODE_256 : AES_MODE_128);
288 AES_convert_key(&aes_ctx);
289 AES_cbc_decrypt(&aes_ctx, ssl_obj->buf, ssl_obj->buf, ssl_obj->len);
290 ret = 0;
291
292 error:
293 return ret;
294 }
295
296 /**
297 * Take a base64 blob of data and turn it into its proper ASN.1 form.
298 */
299 static int new_pem_obj(SSL_CTX *ssl_ctx, int is_cacert, char *where,
300 int remain, const char *password)
301 {
302 int ret = SSL_ERROR_BAD_CERTIFICATE;
303 SSLObjLoader *ssl_obj = NULL;
304
305 while (remain > 0)
306 {
307 int i, pem_size, obj_type;
308 char *start = NULL, *end = NULL;
309
310 for (i = 0; i < NUM_PEM_TYPES; i++)
311 {
312 if ((start = strstr(where, begins[i])) &&
313 (end = strstr(where, ends[i])))
314 {
315 remain -= (int)(end-where);
316 start += strlen(begins[i]);
317 pem_size = (int)(end-start);
318
319 ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
320
321 /* 4/3 bigger than what we need but so what */
322 ssl_obj->buf = (uint8_t *)calloc(1, pem_size);
323 ssl_obj->len = pem_size;
324
325 if (i == IS_RSA_PRIVATE_KEY &&
326 strstr(start, "Proc-Type:") &&
327 strstr(start, "4,ENCRYPTED"))
328 {
329 /* check for encrypted PEM file */
330 if (pem_decrypt(start, end, password, ssl_obj) < 0)
331 {
332 ret = SSL_ERROR_BAD_CERTIFICATE;
333 goto error;
334 }
335 }
336 else
337 {
338 ssl_obj->len = pem_size;
339 if (base64_decode(start, pem_size,
340 ssl_obj->buf, &ssl_obj->len) != 0)
341 {
342 ret = SSL_ERROR_BAD_CERTIFICATE;
343 goto error;
344 }
345 }
346
347 switch (i)
348 {
349 case IS_RSA_PRIVATE_KEY:
350 obj_type = SSL_OBJ_RSA_KEY;
351 break;
352
353 case IS_ENCRYPTED_PRIVATE_KEY:
354 case IS_PRIVATE_KEY:
355 obj_type = SSL_OBJ_PKCS8;
356 break;
357
358 case IS_CERTIFICATE:
359 obj_type = is_cacert ?
360 SSL_OBJ_X509_CACERT : SSL_OBJ_X509_CERT;
361 break;
362
363 default:
364 ret = SSL_ERROR_BAD_CERTIFICATE;
365 goto error;
366 }
367
368 /* In a format we can now understand - so process it */
369 if ((ret = do_obj(ssl_ctx, obj_type, ssl_obj, password)))
370 goto error;
371
372 end += strlen(ends[i]);
373 remain -= strlen(ends[i]);
374 while (remain > 0 && (*end == '\r' || *end == '\n'))
375 {
376 end++;
377 remain--;
378 }
379
380 where = end;
381 break;
382 }
383 }
384
385 ssl_obj_free(ssl_obj);
386 ssl_obj = NULL;
387 if (start == NULL)
388 break;
389 }
390 error:
391 ssl_obj_free(ssl_obj);
392 return ret;
393 }
394
395 /*
396 * Load a file into memory that is in ASCII PEM format.
397 */
398 static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type,
399 SSLObjLoader *ssl_obj, const char *password)
400 {
401 char *start;
402
403 /* add a null terminator */
404 ssl_obj->len++;
405 ssl_obj->buf = (uint8_t *)realloc(ssl_obj->buf, ssl_obj->len);
406 ssl_obj->buf[ssl_obj->len-1] = 0;
407 start = (char *)ssl_obj->buf;
408 return new_pem_obj(ssl_ctx, obj_type == SSL_OBJ_X509_CACERT,
409 start, ssl_obj->len, password);
410 }
411 #endif /* CONFIG_SSL_HAS_PEM */
412
413 /**
414 * Load the key/certificates in memory depending on compile-time and user
415 * options.
416 */
417 int load_key_certs(SSL_CTX *ssl_ctx)
418 {
419 int ret = SSL_OK;
420 uint32_t options = ssl_ctx->options;
421 #ifdef CONFIG_SSL_GENERATE_X509_CERT
422 uint8_t *cert_data = NULL;
423 int cert_size;
424 static const char *dn[] =
425 {
426 CONFIG_SSL_X509_COMMON_NAME,
427 CONFIG_SSL_X509_ORGANIZATION_NAME,
428 CONFIG_SSL_X509_ORGANIZATION_UNIT_NAME
429 };
430 #endif
431
432 /* do the private key first */
433 if (strlen(CONFIG_SSL_PRIVATE_KEY_LOCATION) > 0)
434 {
435 if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY,
436 CONFIG_SSL_PRIVATE_KEY_LOCATION,
437 CONFIG_SSL_PRIVATE_KEY_PASSWORD)) < 0)
438 goto error;
439 }
440 else if (!(options & SSL_NO_DEFAULT_KEY))
441 {
442 #if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
443 static const /* saves a few more bytes */
444 #include "private_key.h"
445
446 ssl_obj_memory_load(ssl_ctx, SSL_OBJ_RSA_KEY, default_private_key,
447 default_private_key_len, NULL);
448 #endif
449 }
450
451 /* now load the certificate */
452 #ifdef CONFIG_SSL_GENERATE_X509_CERT
453 if ((cert_size = ssl_x509_create(ssl_ctx, 0, dn, &cert_data)) < 0)
454 {
455 ret = cert_size;
456 goto error;
457 }
458
459 ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT, cert_data, cert_size, NULL);
460 free(cert_data);
461 #else
462 if (strlen(CONFIG_SSL_X509_CERT_LOCATION))
463 {
464 if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT,
465 CONFIG_SSL_X509_CERT_LOCATION, NULL)) < 0)
466 goto error;
467 }
468 else if (!(options & SSL_NO_DEFAULT_KEY))
469 {
470 #if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
471 #include "cert.h"
472 ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT,
473 default_certificate, default_certificate_len, NULL);
474 #endif
475 }
476 #endif
477
478 error:
479 #ifdef CONFIG_DEBUG
480 if (ret)
481 {
482 printf("Error: Certificate or key not loaded\n");
483 }
484 #endif
485
486 return ret;
487
488 }