x509_cert_parser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* X.509 certificate parser
  2. *
  3. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "X.509: "fmt
  12. #include <linux/kernel.h>
  13. #include <linux/export.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/oid_registry.h>
  17. #include "public_key.h"
  18. #include "x509_parser.h"
  19. #include "x509-asn1.h"
  20. #include "x509_rsakey-asn1.h"
  21. struct x509_parse_context {
  22. struct x509_certificate *cert; /* Certificate being constructed */
  23. unsigned long data; /* Start of data */
  24. const void *cert_start; /* Start of cert content */
  25. const void *key; /* Key data */
  26. size_t key_size; /* Size of key data */
  27. enum OID last_oid; /* Last OID encountered */
  28. enum OID algo_oid; /* Algorithm OID */
  29. unsigned char nr_mpi; /* Number of MPIs stored */
  30. u8 o_size; /* Size of organizationName (O) */
  31. u8 cn_size; /* Size of commonName (CN) */
  32. u8 email_size; /* Size of emailAddress */
  33. u16 o_offset; /* Offset of organizationName (O) */
  34. u16 cn_offset; /* Offset of commonName (CN) */
  35. u16 email_offset; /* Offset of emailAddress */
  36. };
  37. /*
  38. * Free an X.509 certificate
  39. */
  40. void x509_free_certificate(struct x509_certificate *cert)
  41. {
  42. if (cert) {
  43. public_key_destroy(cert->pub);
  44. kfree(cert->issuer);
  45. kfree(cert->subject);
  46. kfree(cert->id);
  47. kfree(cert->skid);
  48. kfree(cert->authority);
  49. kfree(cert->sig.digest);
  50. mpi_free(cert->sig.rsa.s);
  51. kfree(cert);
  52. }
  53. }
  54. EXPORT_SYMBOL_GPL(x509_free_certificate);
  55. /*
  56. * Parse an X.509 certificate
  57. */
  58. struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
  59. {
  60. struct x509_certificate *cert;
  61. struct x509_parse_context *ctx;
  62. struct asymmetric_key_id *kid;
  63. long ret;
  64. ret = -ENOMEM;
  65. cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
  66. if (!cert)
  67. goto error_no_cert;
  68. cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
  69. if (!cert->pub)
  70. goto error_no_ctx;
  71. ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);
  72. if (!ctx)
  73. goto error_no_ctx;
  74. ctx->cert = cert;
  75. ctx->data = (unsigned long)data;
  76. /* Attempt to decode the certificate */
  77. ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen);
  78. if (ret < 0)
  79. goto error_decode;
  80. /* Decode the public key */
  81. ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx,
  82. ctx->key, ctx->key_size);
  83. if (ret < 0)
  84. goto error_decode;
  85. /* Generate cert issuer + serial number key ID */
  86. kid = asymmetric_key_generate_id(cert->raw_serial,
  87. cert->raw_serial_size,
  88. cert->raw_issuer,
  89. cert->raw_issuer_size);
  90. if (IS_ERR(kid)) {
  91. ret = PTR_ERR(kid);
  92. goto error_decode;
  93. }
  94. cert->id = kid;
  95. kfree(ctx);
  96. return cert;
  97. error_decode:
  98. kfree(ctx);
  99. error_no_ctx:
  100. x509_free_certificate(cert);
  101. error_no_cert:
  102. return ERR_PTR(ret);
  103. }
  104. EXPORT_SYMBOL_GPL(x509_cert_parse);
  105. /*
  106. * Note an OID when we find one for later processing when we know how
  107. * to interpret it.
  108. */
  109. int x509_note_OID(void *context, size_t hdrlen,
  110. unsigned char tag,
  111. const void *value, size_t vlen)
  112. {
  113. struct x509_parse_context *ctx = context;
  114. ctx->last_oid = look_up_OID(value, vlen);
  115. if (ctx->last_oid == OID__NR) {
  116. char buffer[50];
  117. sprint_oid(value, vlen, buffer, sizeof(buffer));
  118. pr_debug("Unknown OID: [%lu] %s\n",
  119. (unsigned long)value - ctx->data, buffer);
  120. }
  121. return 0;
  122. }
  123. /*
  124. * Save the position of the TBS data so that we can check the signature over it
  125. * later.
  126. */
  127. int x509_note_tbs_certificate(void *context, size_t hdrlen,
  128. unsigned char tag,
  129. const void *value, size_t vlen)
  130. {
  131. struct x509_parse_context *ctx = context;
  132. pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",
  133. hdrlen, tag, (unsigned long)value - ctx->data, vlen);
  134. ctx->cert->tbs = value - hdrlen;
  135. ctx->cert->tbs_size = vlen + hdrlen;
  136. return 0;
  137. }
  138. /*
  139. * Record the public key algorithm
  140. */
  141. int x509_note_pkey_algo(void *context, size_t hdrlen,
  142. unsigned char tag,
  143. const void *value, size_t vlen)
  144. {
  145. struct x509_parse_context *ctx = context;
  146. pr_debug("PubKey Algo: %u\n", ctx->last_oid);
  147. switch (ctx->last_oid) {
  148. case OID_md2WithRSAEncryption:
  149. case OID_md3WithRSAEncryption:
  150. default:
  151. return -ENOPKG; /* Unsupported combination */
  152. case OID_md4WithRSAEncryption:
  153. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_MD5;
  154. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  155. break;
  156. case OID_sha1WithRSAEncryption:
  157. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA1;
  158. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  159. break;
  160. case OID_sha256WithRSAEncryption:
  161. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA256;
  162. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  163. break;
  164. case OID_sha384WithRSAEncryption:
  165. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA384;
  166. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  167. break;
  168. case OID_sha512WithRSAEncryption:
  169. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA512;
  170. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  171. break;
  172. case OID_sha224WithRSAEncryption:
  173. ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA224;
  174. ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
  175. break;
  176. }
  177. ctx->algo_oid = ctx->last_oid;
  178. return 0;
  179. }
  180. /*
  181. * Note the whereabouts and type of the signature.
  182. */
  183. int x509_note_signature(void *context, size_t hdrlen,
  184. unsigned char tag,
  185. const void *value, size_t vlen)
  186. {
  187. struct x509_parse_context *ctx = context;
  188. pr_debug("Signature type: %u size %zu\n", ctx->last_oid, vlen);
  189. if (ctx->last_oid != ctx->algo_oid) {
  190. pr_warn("Got cert with pkey (%u) and sig (%u) algorithm OIDs\n",
  191. ctx->algo_oid, ctx->last_oid);
  192. return -EINVAL;
  193. }
  194. ctx->cert->raw_sig = value;
  195. ctx->cert->raw_sig_size = vlen;
  196. return 0;
  197. }
  198. /*
  199. * Note the certificate serial number
  200. */
  201. int x509_note_serial(void *context, size_t hdrlen,
  202. unsigned char tag,
  203. const void *value, size_t vlen)
  204. {
  205. struct x509_parse_context *ctx = context;
  206. ctx->cert->raw_serial = value;
  207. ctx->cert->raw_serial_size = vlen;
  208. return 0;
  209. }
  210. /*
  211. * Note some of the name segments from which we'll fabricate a name.
  212. */
  213. int x509_extract_name_segment(void *context, size_t hdrlen,
  214. unsigned char tag,
  215. const void *value, size_t vlen)
  216. {
  217. struct x509_parse_context *ctx = context;
  218. switch (ctx->last_oid) {
  219. case OID_commonName:
  220. ctx->cn_size = vlen;
  221. ctx->cn_offset = (unsigned long)value - ctx->data;
  222. break;
  223. case OID_organizationName:
  224. ctx->o_size = vlen;
  225. ctx->o_offset = (unsigned long)value - ctx->data;
  226. break;
  227. case OID_email_address:
  228. ctx->email_size = vlen;
  229. ctx->email_offset = (unsigned long)value - ctx->data;
  230. break;
  231. default:
  232. break;
  233. }
  234. return 0;
  235. }
  236. /*
  237. * Fabricate and save the issuer and subject names
  238. */
  239. static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
  240. unsigned char tag,
  241. char **_name, size_t vlen)
  242. {
  243. const void *name, *data = (const void *)ctx->data;
  244. size_t namesize;
  245. char *buffer;
  246. if (*_name)
  247. return -EINVAL;
  248. /* Empty name string if no material */
  249. if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {
  250. buffer = kmalloc(1, GFP_KERNEL);
  251. if (!buffer)
  252. return -ENOMEM;
  253. buffer[0] = 0;
  254. goto done;
  255. }
  256. if (ctx->cn_size && ctx->o_size) {
  257. /* Consider combining O and CN, but use only the CN if it is
  258. * prefixed by the O, or a significant portion thereof.
  259. */
  260. namesize = ctx->cn_size;
  261. name = data + ctx->cn_offset;
  262. if (ctx->cn_size >= ctx->o_size &&
  263. memcmp(data + ctx->cn_offset, data + ctx->o_offset,
  264. ctx->o_size) == 0)
  265. goto single_component;
  266. if (ctx->cn_size >= 7 &&
  267. ctx->o_size >= 7 &&
  268. memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)
  269. goto single_component;
  270. buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,
  271. GFP_KERNEL);
  272. if (!buffer)
  273. return -ENOMEM;
  274. memcpy(buffer,
  275. data + ctx->o_offset, ctx->o_size);
  276. buffer[ctx->o_size + 0] = ':';
  277. buffer[ctx->o_size + 1] = ' ';
  278. memcpy(buffer + ctx->o_size + 2,
  279. data + ctx->cn_offset, ctx->cn_size);
  280. buffer[ctx->o_size + 2 + ctx->cn_size] = 0;
  281. goto done;
  282. } else if (ctx->cn_size) {
  283. namesize = ctx->cn_size;
  284. name = data + ctx->cn_offset;
  285. } else if (ctx->o_size) {
  286. namesize = ctx->o_size;
  287. name = data + ctx->o_offset;
  288. } else {
  289. namesize = ctx->email_size;
  290. name = data + ctx->email_offset;
  291. }
  292. single_component:
  293. buffer = kmalloc(namesize + 1, GFP_KERNEL);
  294. if (!buffer)
  295. return -ENOMEM;
  296. memcpy(buffer, name, namesize);
  297. buffer[namesize] = 0;
  298. done:
  299. *_name = buffer;
  300. ctx->cn_size = 0;
  301. ctx->o_size = 0;
  302. ctx->email_size = 0;
  303. return 0;
  304. }
  305. int x509_note_issuer(void *context, size_t hdrlen,
  306. unsigned char tag,
  307. const void *value, size_t vlen)
  308. {
  309. struct x509_parse_context *ctx = context;
  310. ctx->cert->raw_issuer = value;
  311. ctx->cert->raw_issuer_size = vlen;
  312. return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);
  313. }
  314. int x509_note_subject(void *context, size_t hdrlen,
  315. unsigned char tag,
  316. const void *value, size_t vlen)
  317. {
  318. struct x509_parse_context *ctx = context;
  319. ctx->cert->raw_subject = value;
  320. ctx->cert->raw_subject_size = vlen;
  321. return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);
  322. }
  323. /*
  324. * Extract the data for the public key algorithm
  325. */
  326. int x509_extract_key_data(void *context, size_t hdrlen,
  327. unsigned char tag,
  328. const void *value, size_t vlen)
  329. {
  330. struct x509_parse_context *ctx = context;
  331. if (ctx->last_oid != OID_rsaEncryption)
  332. return -ENOPKG;
  333. ctx->cert->pub->pkey_algo = PKEY_ALGO_RSA;
  334. /* Discard the BIT STRING metadata */
  335. ctx->key = value + 1;
  336. ctx->key_size = vlen - 1;
  337. return 0;
  338. }
  339. /*
  340. * Extract a RSA public key value
  341. */
  342. int rsa_extract_mpi(void *context, size_t hdrlen,
  343. unsigned char tag,
  344. const void *value, size_t vlen)
  345. {
  346. struct x509_parse_context *ctx = context;
  347. MPI mpi;
  348. if (ctx->nr_mpi >= ARRAY_SIZE(ctx->cert->pub->mpi)) {
  349. pr_err("Too many public key MPIs in certificate\n");
  350. return -EBADMSG;
  351. }
  352. mpi = mpi_read_raw_data(value, vlen);
  353. if (!mpi)
  354. return -ENOMEM;
  355. ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi;
  356. return 0;
  357. }
  358. /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
  359. #define SEQ_TAG_KEYID (ASN1_CONT << 6)
  360. /*
  361. * Process certificate extensions that are used to qualify the certificate.
  362. */
  363. int x509_process_extension(void *context, size_t hdrlen,
  364. unsigned char tag,
  365. const void *value, size_t vlen)
  366. {
  367. struct x509_parse_context *ctx = context;
  368. struct asymmetric_key_id *kid;
  369. const unsigned char *v = value;
  370. int i;
  371. pr_debug("Extension: %u\n", ctx->last_oid);
  372. if (ctx->last_oid == OID_subjectKeyIdentifier) {
  373. /* Get hold of the key fingerprint */
  374. if (ctx->cert->skid || vlen < 3)
  375. return -EBADMSG;
  376. if (v[0] != ASN1_OTS || v[1] != vlen - 2)
  377. return -EBADMSG;
  378. v += 2;
  379. vlen -= 2;
  380. ctx->cert->raw_skid_size = vlen;
  381. ctx->cert->raw_skid = v;
  382. kid = asymmetric_key_generate_id(ctx->cert->raw_subject,
  383. ctx->cert->raw_subject_size,
  384. v, vlen);
  385. if (IS_ERR(kid))
  386. return PTR_ERR(kid);
  387. ctx->cert->skid = kid;
  388. pr_debug("subjkeyid %*phN\n", kid->len, kid->data);
  389. return 0;
  390. }
  391. if (ctx->last_oid == OID_authorityKeyIdentifier) {
  392. /* Get hold of the CA key fingerprint */
  393. if (ctx->cert->authority || vlen < 5)
  394. return -EBADMSG;
  395. /* Authority Key Identifier must be a Constructed SEQUENCE */
  396. if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
  397. return -EBADMSG;
  398. /* Authority Key Identifier is not indefinite length */
  399. if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
  400. return -EBADMSG;
  401. if (vlen < ASN1_INDEFINITE_LENGTH) {
  402. /* Short Form length */
  403. if (v[1] != vlen - 2 ||
  404. v[2] != SEQ_TAG_KEYID ||
  405. v[3] > vlen - 4)
  406. return -EBADMSG;
  407. vlen = v[3];
  408. v += 4;
  409. } else {
  410. /* Long Form length */
  411. size_t seq_len = 0;
  412. size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
  413. if (sub > 2)
  414. return -EBADMSG;
  415. /* calculate the length from subsequent octets */
  416. v += 2;
  417. for (i = 0; i < sub; i++) {
  418. seq_len <<= 8;
  419. seq_len |= v[i];
  420. }
  421. if (seq_len != vlen - 2 - sub ||
  422. v[sub] != SEQ_TAG_KEYID ||
  423. v[sub + 1] > vlen - 4 - sub)
  424. return -EBADMSG;
  425. vlen = v[sub + 1];
  426. v += (sub + 2);
  427. }
  428. kid = asymmetric_key_generate_id(ctx->cert->raw_issuer,
  429. ctx->cert->raw_issuer_size,
  430. v, vlen);
  431. if (IS_ERR(kid))
  432. return PTR_ERR(kid);
  433. pr_debug("authkeyid %*phN\n", kid->len, kid->data);
  434. ctx->cert->authority = kid;
  435. return 0;
  436. }
  437. return 0;
  438. }
  439. /*
  440. * Record a certificate time.
  441. */
  442. static int x509_note_time(struct tm *tm, size_t hdrlen,
  443. unsigned char tag,
  444. const unsigned char *value, size_t vlen)
  445. {
  446. const unsigned char *p = value;
  447. #define dec2bin(X) ((X) - '0')
  448. #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
  449. if (tag == ASN1_UNITIM) {
  450. /* UTCTime: YYMMDDHHMMSSZ */
  451. if (vlen != 13)
  452. goto unsupported_time;
  453. tm->tm_year = DD2bin(p);
  454. if (tm->tm_year >= 50)
  455. tm->tm_year += 1900;
  456. else
  457. tm->tm_year += 2000;
  458. } else if (tag == ASN1_GENTIM) {
  459. /* GenTime: YYYYMMDDHHMMSSZ */
  460. if (vlen != 15)
  461. goto unsupported_time;
  462. tm->tm_year = DD2bin(p) * 100 + DD2bin(p);
  463. } else {
  464. goto unsupported_time;
  465. }
  466. tm->tm_year -= 1900;
  467. tm->tm_mon = DD2bin(p) - 1;
  468. tm->tm_mday = DD2bin(p);
  469. tm->tm_hour = DD2bin(p);
  470. tm->tm_min = DD2bin(p);
  471. tm->tm_sec = DD2bin(p);
  472. if (*p != 'Z')
  473. goto unsupported_time;
  474. return 0;
  475. unsupported_time:
  476. pr_debug("Got unsupported time [tag %02x]: '%*.*s'\n",
  477. tag, (int)vlen, (int)vlen, value);
  478. return -EBADMSG;
  479. }
  480. int x509_note_not_before(void *context, size_t hdrlen,
  481. unsigned char tag,
  482. const void *value, size_t vlen)
  483. {
  484. struct x509_parse_context *ctx = context;
  485. return x509_note_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);
  486. }
  487. int x509_note_not_after(void *context, size_t hdrlen,
  488. unsigned char tag,
  489. const void *value, size_t vlen)
  490. {
  491. struct x509_parse_context *ctx = context;
  492. return x509_note_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);
  493. }