asn1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright (c) 2007-2016, 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. /* 1.2.840.113549.1.1 OID prefix - handle the following */
  41. /* md5WithRSAEncryption(4) */
  42. /* sha1WithRSAEncryption(5) */
  43. /* sha256WithRSAEncryption (11) */
  44. /* sha384WithRSAEncryption (12) */
  45. /* sha512WithRSAEncryption (13) */
  46. static const uint8_t sig_oid_prefix[] =
  47. {
  48. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01
  49. };
  50. /* 1.3.14.3.2.29 SHA1 with RSA signature */
  51. static const uint8_t sig_sha1WithRSAEncrypt[] =
  52. {
  53. 0x2b, 0x0e, 0x03, 0x02, 0x1d
  54. };
  55. /* 2.16.840.1.101.3.4.2.1 SHA-256 */
  56. static const uint8_t sig_sha256[] =
  57. {
  58. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
  59. };
  60. /* 2.16.840.1.101.3.4.2.2 SHA-384 */
  61. static const uint8_t sig_sha384[] =
  62. {
  63. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
  64. };
  65. /* 2.16.840.1.101.3.4.2.3 SHA-512 */
  66. static const uint8_t sig_sha512[] =
  67. {
  68. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
  69. };
  70. static const uint8_t sig_subject_alt_name[] =
  71. {
  72. 0x55, 0x1d, 0x11
  73. };
  74. static const uint8_t sig_basic_constraints[] =
  75. {
  76. 0x55, 0x1d, 0x13
  77. };
  78. static const uint8_t sig_key_usage[] =
  79. {
  80. 0x55, 0x1d, 0x0f
  81. };
  82. /* CN, O, OU, L, C, ST */
  83. static const uint8_t g_dn_types[] = { 3, 10, 11, 7, 6, 8 };
  84. uint32_t get_asn1_length(const uint8_t *buf, int *offset)
  85. {
  86. int i;
  87. uint32_t len;
  88. if (!(buf[*offset] & 0x80)) /* short form */
  89. {
  90. len = buf[(*offset)++];
  91. }
  92. else /* long form */
  93. {
  94. int length_bytes = buf[(*offset)++]&0x7f;
  95. if (length_bytes > 4) /* limit number of bytes */
  96. return 0;
  97. len = 0;
  98. for (i = 0; i < length_bytes; i++)
  99. {
  100. len <<= 8;
  101. len += buf[(*offset)++];
  102. }
  103. }
  104. return len;
  105. }
  106. /**
  107. * Skip the ASN1.1 object type and its length. Get ready to read the object's
  108. * data.
  109. */
  110. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type)
  111. {
  112. if (buf[*offset] != obj_type)
  113. return X509_NOT_OK;
  114. (*offset)++;
  115. return get_asn1_length(buf, offset);
  116. }
  117. /**
  118. * Skip over an ASN.1 object type completely. Get ready to read the next
  119. * object.
  120. */
  121. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type)
  122. {
  123. int len;
  124. if (buf[*offset] != obj_type)
  125. return X509_NOT_OK;
  126. (*offset)++;
  127. len = get_asn1_length(buf, offset);
  128. *offset += len;
  129. return 0;
  130. }
  131. /**
  132. * Read an integer value for ASN.1 data
  133. * Note: This function allocates memory which must be freed by the user.
  134. */
  135. int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object)
  136. {
  137. int len;
  138. if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0)
  139. goto end_big_int;
  140. if (len > 1 && buf[*offset] == 0x00) /* ignore the negative byte */
  141. {
  142. len--;
  143. (*offset)++;
  144. }
  145. *object = (uint8_t *)malloc(len);
  146. memcpy(*object, &buf[*offset], len);
  147. *offset += len;
  148. end_big_int:
  149. return len;
  150. }
  151. /**
  152. * Read an integer value for ASN.1 data
  153. */
  154. int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val)
  155. {
  156. int res = X509_OK;
  157. int len;
  158. int i;
  159. if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0 ||
  160. len > sizeof(int32_t))
  161. {
  162. res = X509_NOT_OK;
  163. goto end_int;
  164. }
  165. *val = 0;
  166. for (i = 0; i < len; i++)
  167. {
  168. *val <<= 8;
  169. *val |= buf[(*offset)++];
  170. }
  171. end_int:
  172. return res;
  173. }
  174. /**
  175. * Read an boolean value for ASN.1 data
  176. */
  177. int asn1_get_bool(const uint8_t *buf, int *offset, bool *val)
  178. {
  179. int res = X509_OK;
  180. if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) != 1)
  181. {
  182. res = X509_NOT_OK;
  183. goto end_bool;
  184. }
  185. /* DER demands that "If the encoding represents the boolean value TRUE,
  186. its single contents octet shall have all eight bits set to one."
  187. Thus only 0 and 255 are valid encoded values. */
  188. *val = buf[(*offset)++] == 0xFF;
  189. end_bool:
  190. return res;
  191. }
  192. /**
  193. * Convert an ASN.1 bit string into a 32 bit integer. Used for key usage
  194. */
  195. int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val)
  196. {
  197. int res = X509_OK;
  198. int len, i;
  199. if ((len = asn1_next_obj(buf, offset, ASN1_BIT_STRING)) < 0 || len > 5)
  200. {
  201. res = X509_NOT_OK;
  202. goto end_bit_string_as_int;
  203. }
  204. /* number of bits left unused in the final byte of content */
  205. (*offset)++;
  206. len--;
  207. *val = 0;
  208. /* not sure why key usage doesn't used proper DER spec version */
  209. for (i = len-1; i >= 0; --i)
  210. {
  211. *val <<= 8;
  212. *val |= buf[(*offset) + i];
  213. }
  214. *offset += len;
  215. end_bit_string_as_int:
  216. return res;
  217. }
  218. /**
  219. * Get all the RSA private key specifics from an ASN.1 encoded file
  220. */
  221. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
  222. {
  223. int offset = 7;
  224. uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL;
  225. int mod_len, priv_len, pub_len;
  226. #ifdef CONFIG_BIGINT_CRT
  227. uint8_t *p = NULL, *q = NULL, *dP = NULL, *dQ = NULL, *qInv = NULL;
  228. int p_len, q_len, dP_len, dQ_len, qInv_len;
  229. #endif
  230. /* not in der format */
  231. if (buf[0] != ASN1_SEQUENCE) /* basic sanity check */
  232. {
  233. #ifdef CONFIG_SSL_FULL_MODE
  234. printf("Error: This is not a valid ASN.1 file\n");
  235. #endif
  236. return X509_INVALID_PRIV_KEY;
  237. }
  238. /* Use the private key to mix up the RNG if possible. */
  239. RNG_custom_init(buf, len);
  240. mod_len = asn1_get_big_int(buf, &offset, &modulus);
  241. pub_len = asn1_get_big_int(buf, &offset, &pub_exp);
  242. priv_len = asn1_get_big_int(buf, &offset, &priv_exp);
  243. if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0)
  244. return X509_INVALID_PRIV_KEY;
  245. #ifdef CONFIG_BIGINT_CRT
  246. p_len = asn1_get_big_int(buf, &offset, &p);
  247. q_len = asn1_get_big_int(buf, &offset, &q);
  248. dP_len = asn1_get_big_int(buf, &offset, &dP);
  249. dQ_len = asn1_get_big_int(buf, &offset, &dQ);
  250. qInv_len = asn1_get_big_int(buf, &offset, &qInv);
  251. if (p_len <= 0 || q_len <= 0 || dP_len <= 0 || dQ_len <= 0 || qInv_len <= 0)
  252. return X509_INVALID_PRIV_KEY;
  253. RSA_priv_key_new(rsa_ctx,
  254. modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len,
  255. p, p_len, q, p_len, dP, dP_len, dQ, dQ_len, qInv, qInv_len);
  256. free(p);
  257. free(q);
  258. free(dP);
  259. free(dQ);
  260. free(qInv);
  261. #else
  262. RSA_priv_key_new(rsa_ctx,
  263. modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len);
  264. #endif
  265. free(modulus);
  266. free(priv_exp);
  267. free(pub_exp);
  268. return X509_OK;
  269. }
  270. /**
  271. * Get the time of a certificate. Ignore hours/minutes/seconds.
  272. */
  273. static int asn1_get_utc_time(const uint8_t *buf, int *offset, time_t *t)
  274. {
  275. int ret = X509_NOT_OK, len, t_offset, abs_year;
  276. struct tm tm;
  277. /* see http://tools.ietf.org/html/rfc5280#section-4.1.2.5 */
  278. if (buf[*offset] == ASN1_UTC_TIME)
  279. {
  280. (*offset)++;
  281. len = get_asn1_length(buf, offset);
  282. t_offset = *offset;
  283. memset(&tm, 0, sizeof(struct tm));
  284. tm.tm_year = (buf[t_offset] - '0')*10 + (buf[t_offset+1] - '0');
  285. if (tm.tm_year < 50) /* 1951-2050 thing */
  286. {
  287. tm.tm_year += 100;
  288. }
  289. tm.tm_mon = (buf[t_offset+2] - '0')*10 + (buf[t_offset+3] - '0') - 1;
  290. tm.tm_mday = (buf[t_offset+4] - '0')*10 + (buf[t_offset+5] - '0');
  291. tm.tm_hour = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0');
  292. tm.tm_min = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0');
  293. tm.tm_sec = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0');
  294. *t = mktime(&tm);
  295. *offset += len;
  296. ret = X509_OK;
  297. }
  298. else if (buf[*offset] == ASN1_GENERALIZED_TIME)
  299. {
  300. (*offset)++;
  301. len = get_asn1_length(buf, offset);
  302. t_offset = *offset;
  303. memset(&tm, 0, sizeof(struct tm));
  304. abs_year = ((buf[t_offset] - '0')*1000 +
  305. (buf[t_offset+1] - '0')*100 + (buf[t_offset+2] - '0')*10 +
  306. (buf[t_offset+3] - '0'));
  307. if (abs_year <= 1901)
  308. {
  309. tm.tm_year = 1;
  310. tm.tm_mon = 0;
  311. tm.tm_mday = 1;
  312. }
  313. else
  314. {
  315. tm.tm_year = abs_year - 1900;
  316. tm.tm_mon = (buf[t_offset+4] - '0')*10 + (buf[t_offset+5] - '0') - 1;
  317. tm.tm_mday = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0');
  318. tm.tm_hour = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0');
  319. tm.tm_min = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0');
  320. tm.tm_sec = (buf[t_offset+12] - '0')*10 + (buf[t_offset+13] - '0');
  321. *t = mktime(&tm);
  322. }
  323. *offset += len;
  324. ret = X509_OK;
  325. }
  326. return ret;
  327. }
  328. /**
  329. * Get the version type of a certificate
  330. */
  331. int asn1_version(const uint8_t *cert, int *offset, int *val)
  332. {
  333. (*offset) += 2; /* get past explicit tag */
  334. return asn1_get_int(cert, offset, val);
  335. }
  336. /**
  337. * Retrieve the notbefore and notafter certificate times.
  338. */
  339. int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  340. {
  341. return (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  342. asn1_get_utc_time(cert, offset, &x509_ctx->not_before) ||
  343. asn1_get_utc_time(cert, offset, &x509_ctx->not_after));
  344. }
  345. /**
  346. * Get the components of a distinguished name
  347. */
  348. static int asn1_get_oid_x520(const uint8_t *buf, int *offset)
  349. {
  350. int dn_type = 0;
  351. int len;
  352. if ((len = asn1_next_obj(buf, offset, ASN1_OID)) < 0)
  353. goto end_oid;
  354. /* expect a sequence of 2.5.4.[x] where x is a one of distinguished name
  355. components we are interested in. */
  356. if (len == 3 && buf[(*offset)++] == 0x55 && buf[(*offset)++] == 0x04)
  357. dn_type = buf[(*offset)++];
  358. else
  359. {
  360. *offset += len; /* skip over it */
  361. }
  362. end_oid:
  363. return dn_type;
  364. }
  365. /**
  366. * Obtain an ASN.1 printable string type.
  367. */
  368. static int asn1_get_printable_str(const uint8_t *buf, int *offset, char **str)
  369. {
  370. int len = X509_NOT_OK;
  371. int asn1_type = buf[*offset];
  372. /* some certs have this awful crud in them for some reason */
  373. if (asn1_type != ASN1_PRINTABLE_STR &&
  374. asn1_type != ASN1_PRINTABLE_STR2 &&
  375. asn1_type != ASN1_TELETEX_STR &&
  376. asn1_type != ASN1_IA5_STR &&
  377. asn1_type != ASN1_UNICODE_STR)
  378. goto end_pnt_str;
  379. (*offset)++;
  380. len = get_asn1_length(buf, offset);
  381. if (asn1_type == ASN1_UNICODE_STR)
  382. {
  383. int i;
  384. *str = (char *)malloc(len/2+1); /* allow for null */
  385. for (i = 0; i < len; i += 2)
  386. (*str)[i/2] = buf[*offset + i + 1];
  387. (*str)[len/2] = 0; /* null terminate */
  388. }
  389. else
  390. {
  391. *str = (char *)malloc(len+1); /* allow for null */
  392. memcpy(*str, &buf[*offset], len);
  393. (*str)[len] = 0; /* null terminate */
  394. }
  395. *offset += len;
  396. end_pnt_str:
  397. return len;
  398. }
  399. /**
  400. * Get the subject name (or the issuer) of a certificate.
  401. */
  402. int asn1_name(const uint8_t *cert, int *offset, char *dn[])
  403. {
  404. int ret = X509_NOT_OK;
  405. int dn_type;
  406. char *tmp = NULL;
  407. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
  408. goto end_name;
  409. while (asn1_next_obj(cert, offset, ASN1_SET) >= 0)
  410. {
  411. int i, found = 0;
  412. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  413. (dn_type = asn1_get_oid_x520(cert, offset)) < 0)
  414. goto end_name;
  415. tmp = NULL;
  416. if (asn1_get_printable_str(cert, offset, &tmp) < 0)
  417. {
  418. free(tmp);
  419. goto end_name;
  420. }
  421. /* find the distinguished named type */
  422. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  423. {
  424. if (dn_type == g_dn_types[i])
  425. {
  426. if (dn[i] == NULL)
  427. {
  428. dn[i] = tmp;
  429. found = 1;
  430. break;
  431. }
  432. }
  433. }
  434. if (found == 0) /* not found so get rid of it */
  435. {
  436. free(tmp);
  437. }
  438. }
  439. ret = X509_OK;
  440. end_name:
  441. return ret;
  442. }
  443. /**
  444. * Read the modulus and public exponent of a certificate.
  445. */
  446. int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  447. {
  448. int ret = X509_NOT_OK, mod_len, pub_len;
  449. uint8_t *modulus = NULL, *pub_exp = NULL;
  450. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
  451. asn1_skip_obj(cert, offset, ASN1_SEQUENCE) ||
  452. asn1_next_obj(cert, offset, ASN1_BIT_STRING) < 0)
  453. goto end_pub_key;
  454. (*offset)++; /* ignore the padding bit field */
  455. if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
  456. goto end_pub_key;
  457. mod_len = asn1_get_big_int(cert, offset, &modulus);
  458. pub_len = asn1_get_big_int(cert, offset, &pub_exp);
  459. RSA_pub_key_new(&x509_ctx->rsa_ctx, modulus, mod_len, pub_exp, pub_len);
  460. free(modulus);
  461. free(pub_exp);
  462. ret = X509_OK;
  463. end_pub_key:
  464. return ret;
  465. }
  466. #ifdef CONFIG_SSL_CERT_VERIFICATION
  467. /**
  468. * Read the signature of the certificate.
  469. */
  470. int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
  471. {
  472. int ret = X509_NOT_OK;
  473. if (cert[(*offset)++] != ASN1_BIT_STRING)
  474. goto end_sig;
  475. x509_ctx->sig_len = get_asn1_length(cert, offset)-1;
  476. (*offset)++; /* ignore bit string padding bits */
  477. x509_ctx->signature = (uint8_t *)malloc(x509_ctx->sig_len);
  478. memcpy(x509_ctx->signature, &cert[*offset], x509_ctx->sig_len);
  479. *offset += x509_ctx->sig_len;
  480. ret = X509_OK;
  481. end_sig:
  482. return ret;
  483. }
  484. /*
  485. * Compare 2 distinguished name components for equality
  486. * @return 0 if a match
  487. */
  488. static int asn1_compare_dn_comp(const char *dn1, const char *dn2)
  489. {
  490. int ret;
  491. if (dn1 == NULL && dn2 == NULL)
  492. ret = 0;
  493. else
  494. ret = (dn1 && dn2) ? strcmp(dn1, dn2) : 1;
  495. return ret;
  496. }
  497. /**
  498. * Clean up all of the CA certificates.
  499. */
  500. void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx)
  501. {
  502. int i = 0;
  503. if (ca_cert_ctx == NULL)
  504. return;
  505. while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
  506. {
  507. x509_free(ca_cert_ctx->cert[i]);
  508. ca_cert_ctx->cert[i++] = NULL;
  509. }
  510. free(ca_cert_ctx);
  511. }
  512. /*
  513. * Compare 2 distinguished names for equality
  514. * @return 0 if a match
  515. */
  516. int asn1_compare_dn(char * const dn1[], char * const dn2[])
  517. {
  518. int i;
  519. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  520. {
  521. if (asn1_compare_dn_comp(dn1[i], dn2[i]))
  522. return 1;
  523. }
  524. return 0; /* all good */
  525. }
  526. int asn1_find_oid(const uint8_t* cert, int* offset,
  527. const uint8_t* oid, int oid_length)
  528. {
  529. int seqlen;
  530. if ((seqlen = asn1_next_obj(cert, offset, ASN1_SEQUENCE))> 0)
  531. {
  532. int end = *offset + seqlen;
  533. while (*offset < end)
  534. {
  535. int type = cert[(*offset)++];
  536. int length = get_asn1_length(cert, offset);
  537. int noffset = *offset + length;
  538. if (type == ASN1_SEQUENCE)
  539. {
  540. type = cert[(*offset)++];
  541. length = get_asn1_length(cert, offset);
  542. if (type == ASN1_OID && length == oid_length &&
  543. memcmp(cert + *offset, oid, oid_length) == 0)
  544. {
  545. *offset += oid_length;
  546. return 1;
  547. }
  548. }
  549. *offset = noffset;
  550. }
  551. }
  552. return 0;
  553. }
  554. int asn1_is_subject_alt_name(const uint8_t *cert, int offset)
  555. {
  556. if (asn1_find_oid(cert, &offset, sig_subject_alt_name,
  557. sizeof(sig_subject_alt_name)))
  558. {
  559. return offset;
  560. }
  561. return 0;
  562. }
  563. int asn1_is_basic_constraints(const uint8_t *cert, int offset)
  564. {
  565. if (asn1_find_oid(cert, &offset, sig_basic_constraints,
  566. sizeof(sig_basic_constraints)))
  567. {
  568. return offset;
  569. }
  570. return 0;
  571. }
  572. int asn1_is_key_usage(const uint8_t *cert, int offset)
  573. {
  574. if (asn1_find_oid(cert, &offset, sig_key_usage,
  575. sizeof(sig_key_usage)))
  576. {
  577. return offset;
  578. }
  579. return 0;
  580. }
  581. bool asn1_is_critical_ext(const uint8_t *buf, int *offset)
  582. {
  583. /* critical is optional */
  584. bool res = false;
  585. if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) == 1)
  586. res = buf[(*offset)++] == 0xFF;
  587. return res;
  588. }
  589. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  590. /**
  591. * Read the signature type of the certificate. We only support RSA-MD5 and
  592. * RSA-SHA1 signature types.
  593. */
  594. int asn1_signature_type(const uint8_t *cert,
  595. int *offset, X509_CTX *x509_ctx)
  596. {
  597. int ret = X509_NOT_OK, len;
  598. if (cert[(*offset)++] != ASN1_OID)
  599. goto end_check_sig;
  600. len = get_asn1_length(cert, offset);
  601. if (len == sizeof(sig_sha1WithRSAEncrypt) &&
  602. memcmp(sig_sha1WithRSAEncrypt, &cert[*offset],
  603. sizeof(sig_sha1WithRSAEncrypt)) == 0)
  604. {
  605. x509_ctx->sig_type = SIG_TYPE_SHA1;
  606. }
  607. else if (len == sizeof(sig_sha256) &&
  608. memcmp(sig_sha256, &cert[*offset],
  609. sizeof(sig_sha256)) == 0)
  610. {
  611. x509_ctx->sig_type = SIG_TYPE_SHA256;
  612. }
  613. else if (len == sizeof(sig_sha384) &&
  614. memcmp(sig_sha384, &cert[*offset],
  615. sizeof(sig_sha384)) == 0)
  616. {
  617. x509_ctx->sig_type = SIG_TYPE_SHA384;
  618. }
  619. else if (len == sizeof(sig_sha512) &&
  620. memcmp(sig_sha512, &cert[*offset],
  621. sizeof(sig_sha512)) == 0)
  622. {
  623. x509_ctx->sig_type = SIG_TYPE_SHA512;
  624. }
  625. else
  626. {
  627. if (memcmp(sig_oid_prefix, &cert[*offset], sizeof(sig_oid_prefix)))
  628. {
  629. #ifdef CONFIG_SSL_FULL_MODE
  630. int i;
  631. printf("invalid digest: ");
  632. for (i = 0; i < len; i++)
  633. printf("%02x ", cert[*offset + i]);
  634. printf("\n");
  635. #endif
  636. goto end_check_sig; /* unrecognised cert type */
  637. }
  638. x509_ctx->sig_type = cert[*offset + sizeof(sig_oid_prefix)];
  639. }
  640. *offset += len;
  641. asn1_skip_obj(cert, offset, ASN1_NULL); /* if it's there */
  642. ret = X509_OK;
  643. end_check_sig:
  644. return ret;
  645. }