x509_cert_parser.c 21 KB

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