loader.c 15 KB

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