asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * Some primitive asn methods for extraction ASN.1 data.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include "os_port.h"
  38. #include "crypto.h"
  39. #include "crypto_misc.h"
  40. #include "config.h"
  41. #define SIG_OID_PREFIX_SIZE 8
  42. #define SIG_IIS6_OID_SIZE 5
  43. #define SIG_SUBJECT_ALT_NAME_SIZE 3
  44. /* Must be an RSA algorithm with either SHA1 or MD5 for verifying to work */
  45. static const uint8_t sig_oid_prefix[SIG_OID_PREFIX_SIZE] =
  46. {
  47. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01
  48. };
  49. static const uint8_t sig_sha1WithRSAEncrypt[SIG_IIS6_OID_SIZE] =
  50. {
  51. 0x2b, 0x0e, 0x03, 0x02, 0x1d
  52. };
  53. static const uint8_t sig_subject_alt_name[SIG_SUBJECT_ALT_NAME_SIZE] =
  54. {
  55. 0x55, 0x1d, 0x11
  56. };
  57. /* CN, O, OU */
  58. static const uint8_t g_dn_types[] = { 3, 10, 11 };
  59. int get_asn1_length(const uint8_t *buf, int *offset)
  60. {
  61. int len, i;
  62. if (!(buf[*offset] & 0x80)) /* short form */
  63. {
  64. len = buf[(*offset)++];
  65. }
  66. else /* long form */
  67. {
  68. int length_bytes = buf[(*offset)++]&0x7f;
  69. len = 0;
  70. for (i = 0; i < length_bytes; i++)
  71. {
  72. len <<= 8;
  73. len += buf[(*offset)++];
  74. }
  75. }
  76. return len;
  77. }
  78. /**
  79. * Skip the ASN1.1 object type and its length. Get ready to read the object's
  80. * data.
  81. */
  82. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type)
  83. {
  84. if (buf[*offset] != obj_type)
  85. return X509_NOT_OK;
  86. (*offset)++;
  87. int tmp = get_asn1_length(buf, offset);
  88. return tmp;
  89. }
  90. /**
  91. * Skip over an ASN.1 object type completely. Get ready to read the next
  92. * object.
  93. */
  94. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type)
  95. {
  96. int len;
  97. if (buf[*offset] != obj_type)
  98. return X509_NOT_OK;
  99. (*offset)++;
  100. len = get_asn1_length(buf, offset);
  101. *offset += len;
  102. return 0;
  103. }
  104. /**
  105. * Read an integer value for ASN.1 data
  106. * Note: This function allocates memory which must be freed by the user.
  107. */
  108. int asn1_get_int(const uint8_t *buf, int *offset, uint8_t **object)
  109. {
  110. int len;
  111. if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0)
  112. goto end_int_array;
  113. if (len > 1 && buf[*offset] == 0x00) /* ignore the negative byte */
  114. {
  115. len--;
  116. (*offset)++;
  117. }
  118. *object = (uint8_t *)malloc(len);
  119. memcpy(*object, &buf[*offset], len);
  120. *offset += len;
  121. end_int_array:
  122. return len;
  123. }
  124. /**
  125. * Get all the RSA private key specifics from an ASN.1 encoded file
  126. */
  127. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
  128. {
  129. int offset = 7;
  130. uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL;
  131. int mod_len, priv_len, pub_len;
  132. #ifdef CONFIG_BIGINT_CRT
  133. uint8_t *p = NULL, *q = NULL, *dP = NULL, *dQ = NULL, *qInv = NULL;
  134. int p_len, q_len, dP_len, dQ_len, qInv_len;
  135. #endif
  136. /* not in der format */
  137. if (buf[0] != ASN1_SEQUENCE) /* basic sanity check */
  138. {
  139. #ifdef CONFIG_SSL_FULL_MODE
  140. printf("Error: This is not a valid ASN.1 file\n");
  141. #endif
  142. return X509_INVALID_PRIV_KEY;
  143. }
  144. /* Use the private key to mix up the RNG if possible. */
  145. RNG_custom_init(buf, len);
  146. mod_len = asn1_get_int(buf, &offset, &modulus);
  147. pub_len = asn1_get_int(buf, &offset, &pub_exp);
  148. priv_len = asn1_get_int(buf, &offset, &priv_exp);
  149. if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0)
  150. return X509_INVALID_PRIV_KEY;
  151. #ifdef CONFIG_BIGINT_CRT
  152. p_len = asn1_get_int(buf, &offset, &p);
  153. q_len = asn1_get_int(buf, &offset, &q);
  154. dP_len = asn1_get_int(buf, &offset, &dP);
  155. dQ_len = asn1_get_int(buf, &offset, &dQ);
  156. qInv_len = asn1_get_int(buf, &offset, &qInv);
  157. if (p_len <= 0 || q_len <= 0 || dP_len <= 0 || dQ_len <= 0 || qInv_len <= 0)
  158. return X509_INVALID_PRIV_KEY;
  159. RSA_priv_key_new(rsa_ctx,
  160. modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len,
  161. p, p_len, q, p_len, dP, dP_len, dQ, dQ_len, qInv, qInv_len);
  162. free(p);
  163. free(q);
  164. free(dP);
  165. free(dQ);
  166. free(qInv);
  167. #else
  168. RSA_priv_key_new(rsa_ctx,
  169. modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len);
  170. #endif
  171. free(modulus);
  172. free(priv_exp);
  173. free(pub_exp);
  174. return X509_OK;
  175. }
  176. /**
  177. * Get the time of a certificate. Ignore hours/minutes/seconds.
  178. */
  179. static int asn1_get_utc_time(const uint8_t *buf, int *offset, time_t *t)
  180. {
  181. int ret = X509_NOT_OK, len, t_offset;
  182. struct tm tm;
  183. if (buf[(*offset)++] != ASN1_UTC_TIME)
  184. goto end_utc_time;
  185. len = get_asn1_length(buf, offset);
  186. t_offset = *offset;
  187. memset(&tm, 0, sizeof(struct tm));
  188. tm.tm_year = (buf[t_offset] - '0')*10 + (buf[t_offset+1] - '0');
  189. if (tm.tm_year <= 50) /* 1951-2050 thing */
  190. {
  191. tm.tm_year += 100;
  192. }
  193. tm.tm_mon = (buf[t_offset+2] - '0')*10 + (buf[t_offset+3] - '0') - 1;
  194. tm.tm_mday = (buf[t_offset+4] - '0')*10 + (buf[t_offset+5] - '0');
  195. *t = mktime(&tm);
  196. *offset += len;
  197. ret = X509_OK;
  198. end_utc_time:
  199. return ret;
  200. }
  201. /**
  202. * Get the version type of a certificate (which we don't actually care about)
  203. */
  204. int asn1_version(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  205. {
  206. int ret = X509_NOT_OK;
  207. (*offset) += 2; /* get past explicit tag */
  208. if (asn1_skip_obj(cert, offset, ASN1_INTEGER))
  209. goto end_version;
  210. ret = X509_OK;
  211. end_version:
  212. return ret;
  213. }
  214. /**
  215. * Retrieve the notbefore and notafter certificate times.
  216. */
  217. int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  218. {
  219. return (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  220. asn1_get_utc_time(cert, offset, &x509_ctx->not_before) ||
  221. asn1_get_utc_time(cert, offset, &x509_ctx->not_after));
  222. }
  223. /**
  224. * Get the components of a distinguished name
  225. */
  226. static int asn1_get_oid_x520(const uint8_t *buf, int *offset)
  227. {
  228. int dn_type = 0;
  229. int len;
  230. if ((len = asn1_next_obj(buf, offset, ASN1_OID)) < 0)
  231. goto end_oid;
  232. /* expect a sequence of 2.5.4.[x] where x is a one of distinguished name
  233. components we are interested in. */
  234. if (len == 3 && buf[(*offset)++] == 0x55 && buf[(*offset)++] == 0x04)
  235. dn_type = buf[(*offset)++];
  236. else
  237. {
  238. *offset += len; /* skip over it */
  239. }
  240. end_oid:
  241. return dn_type;
  242. }
  243. /**
  244. * Obtain an ASN.1 printable string type.
  245. */
  246. static int asn1_get_printable_str(const uint8_t *buf, int *offset, char **str)
  247. {
  248. int len = X509_NOT_OK;
  249. int asn1_type = buf[*offset];
  250. /* some certs have this awful crud in them for some reason */
  251. if (asn1_type != ASN1_PRINTABLE_STR &&
  252. asn1_type != ASN1_PRINTABLE_STR2 &&
  253. asn1_type != ASN1_TELETEX_STR &&
  254. asn1_type != ASN1_IA5_STR &&
  255. asn1_type != ASN1_UNICODE_STR)
  256. goto end_pnt_str;
  257. (*offset)++;
  258. len = get_asn1_length(buf, offset);
  259. if (asn1_type == ASN1_UNICODE_STR)
  260. {
  261. int i;
  262. *str = (char *)malloc(len/2+1); /* allow for null */
  263. for (i = 0; i < len; i += 2)
  264. (*str)[i/2] = buf[*offset + i + 1];
  265. (*str)[len/2] = 0; /* null terminate */
  266. }
  267. else
  268. {
  269. *str = (char *)malloc(len+1); /* allow for null */
  270. memcpy(*str, &buf[*offset], len);
  271. (*str)[len] = 0; /* null terminate */
  272. }
  273. *offset += len;
  274. end_pnt_str:
  275. return len;
  276. }
  277. /**
  278. * Get the subject name (or the issuer) of a certificate.
  279. */
  280. int asn1_name(const uint8_t *cert, int *offset, char *dn[])
  281. {
  282. int ret = X509_NOT_OK;
  283. int dn_type;
  284. char *tmp;
  285. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
  286. goto end_name;
  287. while (asn1_next_obj(cert, offset, ASN1_SET) >= 0)
  288. {
  289. int i, found = 0;
  290. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  291. (dn_type = asn1_get_oid_x520(cert, offset)) < 0)
  292. goto end_name;
  293. tmp = NULL;
  294. if (asn1_get_printable_str(cert, offset, &tmp) < 0)
  295. {
  296. free(tmp);
  297. goto end_name;
  298. }
  299. /* find the distinguished named type */
  300. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  301. {
  302. if (dn_type == g_dn_types[i])
  303. {
  304. if (dn[i] == NULL)
  305. {
  306. dn[i] = tmp;
  307. found = 1;
  308. break;
  309. }
  310. }
  311. }
  312. if (found == 0) /* not found so get rid of it */
  313. {
  314. free(tmp);
  315. }
  316. }
  317. ret = X509_OK;
  318. end_name:
  319. return ret;
  320. }
  321. /**
  322. * Read the modulus and public exponent of a certificate.
  323. */
  324. int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  325. {
  326. int ret = X509_NOT_OK, mod_len, pub_len;
  327. uint8_t *modulus = NULL, *pub_exp = NULL;
  328. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  329. asn1_skip_obj(cert, offset, ASN1_SEQUENCE) ||
  330. asn1_next_obj(cert, offset, ASN1_BIT_STRING) < 0)
  331. goto end_pub_key;
  332. (*offset)++; /* ignore the padding bit field */
  333. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
  334. goto end_pub_key;
  335. mod_len = asn1_get_int(cert, offset, &modulus);
  336. pub_len = asn1_get_int(cert, offset, &pub_exp);
  337. RSA_pub_key_new(&x509_ctx->rsa_ctx, modulus, mod_len, pub_exp, pub_len);
  338. free(modulus);
  339. free(pub_exp);
  340. ret = X509_OK;
  341. end_pub_key:
  342. return ret;
  343. }
  344. #ifdef CONFIG_SSL_CERT_VERIFICATION
  345. /**
  346. * Read the signature of the certificate.
  347. */
  348. int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  349. {
  350. int ret = X509_NOT_OK;
  351. if (cert[(*offset)++] != ASN1_BIT_STRING)
  352. goto end_sig;
  353. x509_ctx->sig_len = get_asn1_length(cert, offset)-1;
  354. (*offset)++; /* ignore bit string padding bits */
  355. x509_ctx->signature = (uint8_t *)malloc(x509_ctx->sig_len);
  356. memcpy(x509_ctx->signature, &cert[*offset], x509_ctx->sig_len);
  357. *offset += x509_ctx->sig_len;
  358. ret = X509_OK;
  359. end_sig:
  360. return ret;
  361. }
  362. /*
  363. * Compare 2 distinguished name components for equality
  364. * @return 0 if a match
  365. */
  366. static int asn1_compare_dn_comp(const char *dn1, const char *dn2)
  367. {
  368. int ret;
  369. if (dn1 == NULL && dn2 == NULL)
  370. ret = 0;
  371. else
  372. ret = (dn1 && dn2) ? strcmp(dn1, dn2) : 1;
  373. return ret;
  374. }
  375. /**
  376. * Clean up all of the CA certificates.
  377. */
  378. void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx)
  379. {
  380. int i = 0;
  381. if (ca_cert_ctx == NULL)
  382. return;
  383. while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
  384. {
  385. x509_free(ca_cert_ctx->cert[i]);
  386. ca_cert_ctx->cert[i++] = NULL;
  387. }
  388. free(ca_cert_ctx);
  389. }
  390. /*
  391. * Compare 2 distinguished names for equality
  392. * @return 0 if a match
  393. */
  394. int asn1_compare_dn(char * const dn1[], char * const dn2[])
  395. {
  396. int i;
  397. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  398. {
  399. if (asn1_compare_dn_comp(dn1[i], dn2[i]))
  400. return 1;
  401. }
  402. return 0; /* all good */
  403. }
  404. int asn1_find_oid(const uint8_t* cert, int* offset,
  405. const uint8_t* oid, int oid_length)
  406. {
  407. int seqlen;
  408. if ((seqlen = asn1_next_obj(cert, offset, ASN1_SEQUENCE))> 0)
  409. {
  410. int end = *offset + seqlen;
  411. while (*offset < end)
  412. {
  413. int type = cert[(*offset)++];
  414. int length = get_asn1_length(cert, offset);
  415. int noffset = *offset + length;
  416. if (type == ASN1_SEQUENCE)
  417. {
  418. type = cert[(*offset)++];
  419. length = get_asn1_length(cert, offset);
  420. if (type == ASN1_OID && length == oid_length &&
  421. memcmp(cert + *offset, oid, oid_length) == 0)
  422. {
  423. *offset += oid_length;
  424. return 1;
  425. }
  426. }
  427. *offset = noffset;
  428. }
  429. }
  430. return 0;
  431. }
  432. int asn1_find_subjectaltname(const uint8_t* cert, int offset)
  433. {
  434. if (asn1_find_oid(cert, &offset, sig_subject_alt_name,
  435. SIG_SUBJECT_ALT_NAME_SIZE))
  436. {
  437. return offset;
  438. }
  439. return 0;
  440. }
  441. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  442. /**
  443. * Read the signature type of the certificate. We only support RSA-MD5 and
  444. * RSA-SHA1 signature types.
  445. */
  446. int asn1_signature_type(const uint8_t *cert,
  447. int *offset, X509_CTX *x509_ctx)
  448. {
  449. int ret = X509_NOT_OK, len;
  450. if (cert[(*offset)++] != ASN1_OID)
  451. goto end_check_sig;
  452. len = get_asn1_length(cert, offset);
  453. if (len == 5 && memcmp(sig_sha1WithRSAEncrypt, &cert[*offset],
  454. SIG_IIS6_OID_SIZE) == 0)
  455. {
  456. x509_ctx->sig_type = SIG_TYPE_SHA1;
  457. }
  458. else
  459. {
  460. if (memcmp(sig_oid_prefix, &cert[*offset], SIG_OID_PREFIX_SIZE))
  461. goto end_check_sig; /* unrecognised cert type */
  462. x509_ctx->sig_type = cert[*offset + SIG_OID_PREFIX_SIZE];
  463. }
  464. *offset += len;
  465. asn1_skip_obj(cert, offset, ASN1_NULL); /* if it's there */
  466. ret = X509_OK;
  467. end_check_sig:
  468. return ret;
  469. }