x509_cert_parser.c 13 KB

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