x509_cert_parser.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* X.509 certificate parser
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define pr_fmt(fmt) "X.509: "fmt
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/oid_registry.h>
  13. #include <crypto/public_key.h>
  14. #include "x509_parser.h"
  15. #include "x509.asn1.h"
  16. #include "x509_akid.asn1.h"
  17. struct x509_parse_context {
  18. struct x509_certificate *cert; /* Certificate being constructed */
  19. unsigned long data; /* Start of data */
  20. const void *key; /* Key data */
  21. size_t key_size; /* Size of key data */
  22. const void *params; /* Key parameters */
  23. size_t params_size; /* Size of key parameters */
  24. enum OID key_algo; /* Algorithm used by the cert's key */
  25. enum OID last_oid; /* Last OID encountered */
  26. enum OID sig_algo; /* Algorithm used to sign the cert */
  27. u8 o_size; /* Size of organizationName (O) */
  28. u8 cn_size; /* Size of commonName (CN) */
  29. u8 email_size; /* Size of emailAddress */
  30. u16 o_offset; /* Offset of organizationName (O) */
  31. u16 cn_offset; /* Offset of commonName (CN) */
  32. u16 email_offset; /* Offset of emailAddress */
  33. unsigned raw_akid_size;
  34. const void *raw_akid; /* Raw authorityKeyId in ASN.1 */
  35. const void *akid_raw_issuer; /* Raw directoryName in authorityKeyId */
  36. unsigned akid_raw_issuer_size;
  37. };
  38. /*
  39. * Free an X.509 certificate
  40. */
  41. void x509_free_certificate(struct x509_certificate *cert)
  42. {
  43. if (cert) {
  44. public_key_free(cert->pub);
  45. public_key_signature_free(cert->sig);
  46. kfree(cert->issuer);
  47. kfree(cert->subject);
  48. kfree(cert->id);
  49. kfree(cert->skid);
  50. kfree(cert);
  51. }
  52. }
  53. EXPORT_SYMBOL_GPL(x509_free_certificate);
  54. /*
  55. * Parse an X.509 certificate
  56. */
  57. struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
  58. {
  59. struct x509_certificate *cert;
  60. struct x509_parse_context *ctx;
  61. struct asymmetric_key_id *kid;
  62. long ret;
  63. ret = -ENOMEM;
  64. cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
  65. if (!cert)
  66. goto error_no_cert;
  67. cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
  68. if (!cert->pub)
  69. goto error_no_ctx;
  70. cert->sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL);
  71. if (!cert->sig)
  72. goto error_no_ctx;
  73. ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);
  74. if (!ctx)
  75. goto error_no_ctx;
  76. ctx->cert = cert;
  77. ctx->data = (unsigned long)data;
  78. /* Attempt to decode the certificate */
  79. ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen);
  80. if (ret < 0)
  81. goto error_decode;
  82. /* Decode the AuthorityKeyIdentifier */
  83. if (ctx->raw_akid) {
  84. pr_devel("AKID: %u %*phN\n",
  85. ctx->raw_akid_size, ctx->raw_akid_size, ctx->raw_akid);
  86. ret = asn1_ber_decoder(&x509_akid_decoder, ctx,
  87. ctx->raw_akid, ctx->raw_akid_size);
  88. if (ret < 0) {
  89. pr_warn("Couldn't decode AuthKeyIdentifier\n");
  90. goto error_decode;
  91. }
  92. }
  93. ret = -ENOMEM;
  94. cert->pub->key = kmemdup(ctx->key, ctx->key_size, GFP_KERNEL);
  95. if (!cert->pub->key)
  96. goto error_decode;
  97. cert->pub->keylen = ctx->key_size;
  98. cert->pub->params = kmemdup(ctx->params, ctx->params_size, GFP_KERNEL);
  99. if (!cert->pub->params)
  100. goto error_decode;
  101. cert->pub->paramlen = ctx->params_size;
  102. cert->pub->algo = ctx->key_algo;
  103. /* Grab the signature bits */
  104. ret = x509_get_sig_params(cert);
  105. if (ret < 0)
  106. goto error_decode;
  107. /* Generate cert issuer + serial number key ID */
  108. kid = asymmetric_key_generate_id(cert->raw_serial,
  109. cert->raw_serial_size,
  110. cert->raw_issuer,
  111. cert->raw_issuer_size);
  112. if (IS_ERR(kid)) {
  113. ret = PTR_ERR(kid);
  114. goto error_decode;
  115. }
  116. cert->id = kid;
  117. /* Detect self-signed certificates */
  118. ret = x509_check_for_self_signed(cert);
  119. if (ret < 0)
  120. goto error_decode;
  121. kfree(ctx);
  122. return cert;
  123. error_decode:
  124. kfree(ctx);
  125. error_no_ctx:
  126. x509_free_certificate(cert);
  127. error_no_cert:
  128. return ERR_PTR(ret);
  129. }
  130. EXPORT_SYMBOL_GPL(x509_cert_parse);
  131. /*
  132. * Note an OID when we find one for later processing when we know how
  133. * to interpret it.
  134. */
  135. int x509_note_OID(void *context, size_t hdrlen,
  136. unsigned char tag,
  137. const void *value, size_t vlen)
  138. {
  139. struct x509_parse_context *ctx = context;
  140. ctx->last_oid = look_up_OID(value, vlen);
  141. if (ctx->last_oid == OID__NR) {
  142. char buffer[50];
  143. sprint_oid(value, vlen, buffer, sizeof(buffer));
  144. pr_debug("Unknown OID: [%lu] %s\n",
  145. (unsigned long)value - ctx->data, buffer);
  146. }
  147. return 0;
  148. }
  149. /*
  150. * Save the position of the TBS data so that we can check the signature over it
  151. * later.
  152. */
  153. int x509_note_tbs_certificate(void *context, size_t hdrlen,
  154. unsigned char tag,
  155. const void *value, size_t vlen)
  156. {
  157. struct x509_parse_context *ctx = context;
  158. pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",
  159. hdrlen, tag, (unsigned long)value - ctx->data, vlen);
  160. ctx->cert->tbs = value - hdrlen;
  161. ctx->cert->tbs_size = vlen + hdrlen;
  162. return 0;
  163. }
  164. /*
  165. * Record the algorithm that was used to sign this certificate.
  166. */
  167. int x509_note_sig_algo(void *context, size_t hdrlen, unsigned char tag,
  168. const void *value, size_t vlen)
  169. {
  170. struct x509_parse_context *ctx = context;
  171. pr_debug("PubKey Algo: %u\n", ctx->last_oid);
  172. switch (ctx->last_oid) {
  173. case OID_md2WithRSAEncryption:
  174. case OID_md3WithRSAEncryption:
  175. default:
  176. return -ENOPKG; /* Unsupported combination */
  177. case OID_md4WithRSAEncryption:
  178. ctx->cert->sig->hash_algo = "md4";
  179. goto rsa_pkcs1;
  180. case OID_sha1WithRSAEncryption:
  181. ctx->cert->sig->hash_algo = "sha1";
  182. goto rsa_pkcs1;
  183. case OID_sha256WithRSAEncryption:
  184. ctx->cert->sig->hash_algo = "sha256";
  185. goto rsa_pkcs1;
  186. case OID_sha384WithRSAEncryption:
  187. ctx->cert->sig->hash_algo = "sha384";
  188. goto rsa_pkcs1;
  189. case OID_sha512WithRSAEncryption:
  190. ctx->cert->sig->hash_algo = "sha512";
  191. goto rsa_pkcs1;
  192. case OID_sha224WithRSAEncryption:
  193. ctx->cert->sig->hash_algo = "sha224";
  194. goto rsa_pkcs1;
  195. case OID_id_ecdsa_with_sha1:
  196. ctx->cert->sig->hash_algo = "sha1";
  197. goto ecdsa;
  198. case OID_id_ecdsa_with_sha224:
  199. ctx->cert->sig->hash_algo = "sha224";
  200. goto ecdsa;
  201. case OID_id_ecdsa_with_sha256:
  202. ctx->cert->sig->hash_algo = "sha256";
  203. goto ecdsa;
  204. case OID_id_ecdsa_with_sha384:
  205. ctx->cert->sig->hash_algo = "sha384";
  206. goto ecdsa;
  207. case OID_id_ecdsa_with_sha512:
  208. ctx->cert->sig->hash_algo = "sha512";
  209. goto ecdsa;
  210. case OID_gost2012Signature256:
  211. ctx->cert->sig->hash_algo = "streebog256";
  212. goto ecrdsa;
  213. case OID_gost2012Signature512:
  214. ctx->cert->sig->hash_algo = "streebog512";
  215. goto ecrdsa;
  216. case OID_SM2_with_SM3:
  217. ctx->cert->sig->hash_algo = "sm3";
  218. goto sm2;
  219. }
  220. rsa_pkcs1:
  221. ctx->cert->sig->pkey_algo = "rsa";
  222. ctx->cert->sig->encoding = "pkcs1";
  223. ctx->sig_algo = ctx->last_oid;
  224. return 0;
  225. ecrdsa:
  226. ctx->cert->sig->pkey_algo = "ecrdsa";
  227. ctx->cert->sig->encoding = "raw";
  228. ctx->sig_algo = ctx->last_oid;
  229. return 0;
  230. sm2:
  231. ctx->cert->sig->pkey_algo = "sm2";
  232. ctx->cert->sig->encoding = "raw";
  233. ctx->sig_algo = ctx->last_oid;
  234. return 0;
  235. ecdsa:
  236. ctx->cert->sig->pkey_algo = "ecdsa";
  237. ctx->cert->sig->encoding = "x962";
  238. ctx->sig_algo = ctx->last_oid;
  239. return 0;
  240. }
  241. /*
  242. * Note the whereabouts and type of the signature.
  243. */
  244. int x509_note_signature(void *context, size_t hdrlen,
  245. unsigned char tag,
  246. const void *value, size_t vlen)
  247. {
  248. struct x509_parse_context *ctx = context;
  249. pr_debug("Signature: alg=%u, size=%zu\n", ctx->last_oid, vlen);
  250. /*
  251. * In X.509 certificates, the signature's algorithm is stored in two
  252. * places: inside the TBSCertificate (the data that is signed), and
  253. * alongside the signature. These *must* match.
  254. */
  255. if (ctx->last_oid != ctx->sig_algo) {
  256. pr_warn("signatureAlgorithm (%u) differs from tbsCertificate.signature (%u)\n",
  257. ctx->last_oid, ctx->sig_algo);
  258. return -EINVAL;
  259. }
  260. if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 ||
  261. strcmp(ctx->cert->sig->pkey_algo, "ecrdsa") == 0 ||
  262. strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0 ||
  263. strcmp(ctx->cert->sig->pkey_algo, "ecdsa") == 0) {
  264. /* Discard the BIT STRING metadata */
  265. if (vlen < 1 || *(const u8 *)value != 0)
  266. return -EBADMSG;
  267. value++;
  268. vlen--;
  269. }
  270. ctx->cert->raw_sig = value;
  271. ctx->cert->raw_sig_size = vlen;
  272. return 0;
  273. }
  274. /*
  275. * Note the certificate serial number
  276. */
  277. int x509_note_serial(void *context, size_t hdrlen,
  278. unsigned char tag,
  279. const void *value, size_t vlen)
  280. {
  281. struct x509_parse_context *ctx = context;
  282. ctx->cert->raw_serial = value;
  283. ctx->cert->raw_serial_size = vlen;
  284. return 0;
  285. }
  286. /*
  287. * Note some of the name segments from which we'll fabricate a name.
  288. */
  289. int x509_extract_name_segment(void *context, size_t hdrlen,
  290. unsigned char tag,
  291. const void *value, size_t vlen)
  292. {
  293. struct x509_parse_context *ctx = context;
  294. switch (ctx->last_oid) {
  295. case OID_commonName:
  296. ctx->cn_size = vlen;
  297. ctx->cn_offset = (unsigned long)value - ctx->data;
  298. break;
  299. case OID_organizationName:
  300. ctx->o_size = vlen;
  301. ctx->o_offset = (unsigned long)value - ctx->data;
  302. break;
  303. case OID_email_address:
  304. ctx->email_size = vlen;
  305. ctx->email_offset = (unsigned long)value - ctx->data;
  306. break;
  307. default:
  308. break;
  309. }
  310. return 0;
  311. }
  312. /*
  313. * Fabricate and save the issuer and subject names
  314. */
  315. static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
  316. unsigned char tag,
  317. char **_name, size_t vlen)
  318. {
  319. const void *name, *data = (const void *)ctx->data;
  320. size_t namesize;
  321. char *buffer;
  322. if (*_name)
  323. return -EINVAL;
  324. /* Empty name string if no material */
  325. if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {
  326. buffer = kmalloc(1, GFP_KERNEL);
  327. if (!buffer)
  328. return -ENOMEM;
  329. buffer[0] = 0;
  330. goto done;
  331. }
  332. if (ctx->cn_size && ctx->o_size) {
  333. /* Consider combining O and CN, but use only the CN if it is
  334. * prefixed by the O, or a significant portion thereof.
  335. */
  336. namesize = ctx->cn_size;
  337. name = data + ctx->cn_offset;
  338. if (ctx->cn_size >= ctx->o_size &&
  339. memcmp(data + ctx->cn_offset, data + ctx->o_offset,
  340. ctx->o_size) == 0)
  341. goto single_component;
  342. if (ctx->cn_size >= 7 &&
  343. ctx->o_size >= 7 &&
  344. memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)
  345. goto single_component;
  346. buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,
  347. GFP_KERNEL);
  348. if (!buffer)
  349. return -ENOMEM;
  350. memcpy(buffer,
  351. data + ctx->o_offset, ctx->o_size);
  352. buffer[ctx->o_size + 0] = ':';
  353. buffer[ctx->o_size + 1] = ' ';
  354. memcpy(buffer + ctx->o_size + 2,
  355. data + ctx->cn_offset, ctx->cn_size);
  356. buffer[ctx->o_size + 2 + ctx->cn_size] = 0;
  357. goto done;
  358. } else if (ctx->cn_size) {
  359. namesize = ctx->cn_size;
  360. name = data + ctx->cn_offset;
  361. } else if (ctx->o_size) {
  362. namesize = ctx->o_size;
  363. name = data + ctx->o_offset;
  364. } else {
  365. namesize = ctx->email_size;
  366. name = data + ctx->email_offset;
  367. }
  368. single_component:
  369. buffer = kmalloc(namesize + 1, GFP_KERNEL);
  370. if (!buffer)
  371. return -ENOMEM;
  372. memcpy(buffer, name, namesize);
  373. buffer[namesize] = 0;
  374. done:
  375. *_name = buffer;
  376. ctx->cn_size = 0;
  377. ctx->o_size = 0;
  378. ctx->email_size = 0;
  379. return 0;
  380. }
  381. int x509_note_issuer(void *context, size_t hdrlen,
  382. unsigned char tag,
  383. const void *value, size_t vlen)
  384. {
  385. struct x509_parse_context *ctx = context;
  386. struct asymmetric_key_id *kid;
  387. ctx->cert->raw_issuer = value;
  388. ctx->cert->raw_issuer_size = vlen;
  389. if (!ctx->cert->sig->auth_ids[2]) {
  390. kid = asymmetric_key_generate_id(value, vlen, "", 0);
  391. if (IS_ERR(kid))
  392. return PTR_ERR(kid);
  393. ctx->cert->sig->auth_ids[2] = kid;
  394. }
  395. return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);
  396. }
  397. int x509_note_subject(void *context, size_t hdrlen,
  398. unsigned char tag,
  399. const void *value, size_t vlen)
  400. {
  401. struct x509_parse_context *ctx = context;
  402. ctx->cert->raw_subject = value;
  403. ctx->cert->raw_subject_size = vlen;
  404. return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);
  405. }
  406. /*
  407. * Extract the parameters for the public key
  408. */
  409. int x509_note_params(void *context, size_t hdrlen,
  410. unsigned char tag,
  411. const void *value, size_t vlen)
  412. {
  413. struct x509_parse_context *ctx = context;
  414. /*
  415. * AlgorithmIdentifier is used three times in the x509, we should skip
  416. * first and ignore third, using second one which is after subject and
  417. * before subjectPublicKey.
  418. */
  419. if (!ctx->cert->raw_subject || ctx->key)
  420. return 0;
  421. ctx->params = value - hdrlen;
  422. ctx->params_size = vlen + hdrlen;
  423. return 0;
  424. }
  425. /*
  426. * Extract the data for the public key algorithm
  427. */
  428. int x509_extract_key_data(void *context, size_t hdrlen,
  429. unsigned char tag,
  430. const void *value, size_t vlen)
  431. {
  432. struct x509_parse_context *ctx = context;
  433. enum OID oid;
  434. ctx->key_algo = ctx->last_oid;
  435. switch (ctx->last_oid) {
  436. case OID_rsaEncryption:
  437. ctx->cert->pub->pkey_algo = "rsa";
  438. break;
  439. case OID_gost2012PKey256:
  440. case OID_gost2012PKey512:
  441. ctx->cert->pub->pkey_algo = "ecrdsa";
  442. break;
  443. case OID_sm2:
  444. ctx->cert->pub->pkey_algo = "sm2";
  445. break;
  446. case OID_id_ecPublicKey:
  447. if (parse_OID(ctx->params, ctx->params_size, &oid) != 0)
  448. return -EBADMSG;
  449. switch (oid) {
  450. case OID_sm2:
  451. ctx->cert->pub->pkey_algo = "sm2";
  452. break;
  453. case OID_id_prime192v1:
  454. ctx->cert->pub->pkey_algo = "ecdsa-nist-p192";
  455. break;
  456. case OID_id_prime256v1:
  457. ctx->cert->pub->pkey_algo = "ecdsa-nist-p256";
  458. break;
  459. case OID_id_ansip384r1:
  460. ctx->cert->pub->pkey_algo = "ecdsa-nist-p384";
  461. break;
  462. default:
  463. return -ENOPKG;
  464. }
  465. break;
  466. default:
  467. return -ENOPKG;
  468. }
  469. /* Discard the BIT STRING metadata */
  470. if (vlen < 1 || *(const u8 *)value != 0)
  471. return -EBADMSG;
  472. ctx->key = value + 1;
  473. ctx->key_size = vlen - 1;
  474. return 0;
  475. }
  476. /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
  477. #define SEQ_TAG_KEYID (ASN1_CONT << 6)
  478. /*
  479. * Process certificate extensions that are used to qualify the certificate.
  480. */
  481. int x509_process_extension(void *context, size_t hdrlen,
  482. unsigned char tag,
  483. const void *value, size_t vlen)
  484. {
  485. struct x509_parse_context *ctx = context;
  486. struct asymmetric_key_id *kid;
  487. const unsigned char *v = value;
  488. pr_debug("Extension: %u\n", ctx->last_oid);
  489. if (ctx->last_oid == OID_subjectKeyIdentifier) {
  490. /* Get hold of the key fingerprint */
  491. if (ctx->cert->skid || vlen < 3)
  492. return -EBADMSG;
  493. if (v[0] != ASN1_OTS || v[1] != vlen - 2)
  494. return -EBADMSG;
  495. v += 2;
  496. vlen -= 2;
  497. ctx->cert->raw_skid_size = vlen;
  498. ctx->cert->raw_skid = v;
  499. kid = asymmetric_key_generate_id(v, vlen, "", 0);
  500. if (IS_ERR(kid))
  501. return PTR_ERR(kid);
  502. ctx->cert->skid = kid;
  503. pr_debug("subjkeyid %*phN\n", kid->len, kid->data);
  504. return 0;
  505. }
  506. if (ctx->last_oid == OID_authorityKeyIdentifier) {
  507. /* Get hold of the CA key fingerprint */
  508. ctx->raw_akid = v;
  509. ctx->raw_akid_size = vlen;
  510. return 0;
  511. }
  512. return 0;
  513. }
  514. /**
  515. * x509_decode_time - Decode an X.509 time ASN.1 object
  516. * @_t: The time to fill in
  517. * @hdrlen: The length of the object header
  518. * @tag: The object tag
  519. * @value: The object value
  520. * @vlen: The size of the object value
  521. *
  522. * Decode an ASN.1 universal time or generalised time field into a struct the
  523. * kernel can handle and check it for validity. The time is decoded thus:
  524. *
  525. * [RFC5280 §4.1.2.5]
  526. * CAs conforming to this profile MUST always encode certificate validity
  527. * dates through the year 2049 as UTCTime; certificate validity dates in
  528. * 2050 or later MUST be encoded as GeneralizedTime. Conforming
  529. * applications MUST be able to process validity dates that are encoded in
  530. * either UTCTime or GeneralizedTime.
  531. */
  532. int x509_decode_time(time64_t *_t, size_t hdrlen,
  533. unsigned char tag,
  534. const unsigned char *value, size_t vlen)
  535. {
  536. static const unsigned char month_lengths[] = { 31, 28, 31, 30, 31, 30,
  537. 31, 31, 30, 31, 30, 31 };
  538. const unsigned char *p = value;
  539. unsigned year, mon, day, hour, min, sec, mon_len;
  540. #define dec2bin(X) ({ unsigned char x = (X) - '0'; if (x > 9) goto invalid_time; x; })
  541. #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
  542. if (tag == ASN1_UNITIM) {
  543. /* UTCTime: YYMMDDHHMMSSZ */
  544. if (vlen != 13)
  545. goto unsupported_time;
  546. year = DD2bin(p);
  547. if (year >= 50)
  548. year += 1900;
  549. else
  550. year += 2000;
  551. } else if (tag == ASN1_GENTIM) {
  552. /* GenTime: YYYYMMDDHHMMSSZ */
  553. if (vlen != 15)
  554. goto unsupported_time;
  555. year = DD2bin(p) * 100 + DD2bin(p);
  556. if (year >= 1950 && year <= 2049)
  557. goto invalid_time;
  558. } else {
  559. goto unsupported_time;
  560. }
  561. mon = DD2bin(p);
  562. day = DD2bin(p);
  563. hour = DD2bin(p);
  564. min = DD2bin(p);
  565. sec = DD2bin(p);
  566. if (*p != 'Z')
  567. goto unsupported_time;
  568. if (year < 1970 ||
  569. mon < 1 || mon > 12)
  570. goto invalid_time;
  571. mon_len = month_lengths[mon - 1];
  572. if (mon == 2) {
  573. if (year % 4 == 0) {
  574. mon_len = 29;
  575. if (year % 100 == 0) {
  576. mon_len = 28;
  577. if (year % 400 == 0)
  578. mon_len = 29;
  579. }
  580. }
  581. }
  582. if (day < 1 || day > mon_len ||
  583. hour > 24 || /* ISO 8601 permits 24:00:00 as midnight tomorrow */
  584. min > 59 ||
  585. sec > 60) /* ISO 8601 permits leap seconds [X.680 46.3] */
  586. goto invalid_time;
  587. *_t = mktime64(year, mon, day, hour, min, sec);
  588. return 0;
  589. unsupported_time:
  590. pr_debug("Got unsupported time [tag %02x]: '%*phN'\n",
  591. tag, (int)vlen, value);
  592. return -EBADMSG;
  593. invalid_time:
  594. pr_debug("Got invalid time [tag %02x]: '%*phN'\n",
  595. tag, (int)vlen, value);
  596. return -EBADMSG;
  597. }
  598. EXPORT_SYMBOL_GPL(x509_decode_time);
  599. int x509_note_not_before(void *context, size_t hdrlen,
  600. unsigned char tag,
  601. const void *value, size_t vlen)
  602. {
  603. struct x509_parse_context *ctx = context;
  604. return x509_decode_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);
  605. }
  606. int x509_note_not_after(void *context, size_t hdrlen,
  607. unsigned char tag,
  608. const void *value, size_t vlen)
  609. {
  610. struct x509_parse_context *ctx = context;
  611. return x509_decode_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);
  612. }
  613. /*
  614. * Note a key identifier-based AuthorityKeyIdentifier
  615. */
  616. int x509_akid_note_kid(void *context, size_t hdrlen,
  617. unsigned char tag,
  618. const void *value, size_t vlen)
  619. {
  620. struct x509_parse_context *ctx = context;
  621. struct asymmetric_key_id *kid;
  622. pr_debug("AKID: keyid: %*phN\n", (int)vlen, value);
  623. if (ctx->cert->sig->auth_ids[1])
  624. return 0;
  625. kid = asymmetric_key_generate_id(value, vlen, "", 0);
  626. if (IS_ERR(kid))
  627. return PTR_ERR(kid);
  628. pr_debug("authkeyid %*phN\n", kid->len, kid->data);
  629. ctx->cert->sig->auth_ids[1] = kid;
  630. return 0;
  631. }
  632. /*
  633. * Note a directoryName in an AuthorityKeyIdentifier
  634. */
  635. int x509_akid_note_name(void *context, size_t hdrlen,
  636. unsigned char tag,
  637. const void *value, size_t vlen)
  638. {
  639. struct x509_parse_context *ctx = context;
  640. pr_debug("AKID: name: %*phN\n", (int)vlen, value);
  641. ctx->akid_raw_issuer = value;
  642. ctx->akid_raw_issuer_size = vlen;
  643. return 0;
  644. }
  645. /*
  646. * Note a serial number in an AuthorityKeyIdentifier
  647. */
  648. int x509_akid_note_serial(void *context, size_t hdrlen,
  649. unsigned char tag,
  650. const void *value, size_t vlen)
  651. {
  652. struct x509_parse_context *ctx = context;
  653. struct asymmetric_key_id *kid;
  654. pr_debug("AKID: serial: %*phN\n", (int)vlen, value);
  655. if (!ctx->akid_raw_issuer || ctx->cert->sig->auth_ids[0])
  656. return 0;
  657. kid = asymmetric_key_generate_id(value,
  658. vlen,
  659. ctx->akid_raw_issuer,
  660. ctx->akid_raw_issuer_size);
  661. if (IS_ERR(kid))
  662. return PTR_ERR(kid);
  663. pr_debug("authkeyid %*phN\n", kid->len, kid->data);
  664. ctx->cert->sig->auth_ids[0] = kid;
  665. return 0;
  666. }