loader.c 14 KB

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