loader.c 15 KB

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