pkcs7_parser.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* PKCS#7 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) "PKCS7: "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 "pkcs7_parser.h"
  19. #include "pkcs7-asn1.h"
  20. struct pkcs7_parse_context {
  21. struct pkcs7_message *msg; /* Message being constructed */
  22. struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
  23. struct pkcs7_signed_info **ppsinfo;
  24. struct x509_certificate *certs; /* Certificate cache */
  25. struct x509_certificate **ppcerts;
  26. unsigned long data; /* Start of data */
  27. enum OID last_oid; /* Last OID encountered */
  28. unsigned x509_index;
  29. unsigned sinfo_index;
  30. const void *raw_serial;
  31. unsigned raw_serial_size;
  32. unsigned raw_issuer_size;
  33. const void *raw_issuer;
  34. };
  35. /*
  36. * Free a signed information block.
  37. */
  38. static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
  39. {
  40. if (sinfo) {
  41. mpi_free(sinfo->sig.mpi[0]);
  42. kfree(sinfo->sig.digest);
  43. kfree(sinfo->signing_cert_id);
  44. kfree(sinfo);
  45. }
  46. }
  47. /**
  48. * pkcs7_free_message - Free a PKCS#7 message
  49. * @pkcs7: The PKCS#7 message to free
  50. */
  51. void pkcs7_free_message(struct pkcs7_message *pkcs7)
  52. {
  53. struct x509_certificate *cert;
  54. struct pkcs7_signed_info *sinfo;
  55. if (pkcs7) {
  56. while (pkcs7->certs) {
  57. cert = pkcs7->certs;
  58. pkcs7->certs = cert->next;
  59. x509_free_certificate(cert);
  60. }
  61. while (pkcs7->crl) {
  62. cert = pkcs7->crl;
  63. pkcs7->crl = cert->next;
  64. x509_free_certificate(cert);
  65. }
  66. while (pkcs7->signed_infos) {
  67. sinfo = pkcs7->signed_infos;
  68. pkcs7->signed_infos = sinfo->next;
  69. pkcs7_free_signed_info(sinfo);
  70. }
  71. kfree(pkcs7);
  72. }
  73. }
  74. EXPORT_SYMBOL_GPL(pkcs7_free_message);
  75. /**
  76. * pkcs7_parse_message - Parse a PKCS#7 message
  77. * @data: The raw binary ASN.1 encoded message to be parsed
  78. * @datalen: The size of the encoded message
  79. */
  80. struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
  81. {
  82. struct pkcs7_parse_context *ctx;
  83. struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
  84. int ret;
  85. ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
  86. if (!ctx)
  87. goto out_no_ctx;
  88. ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
  89. if (!ctx->msg)
  90. goto out_no_msg;
  91. ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  92. if (!ctx->sinfo)
  93. goto out_no_sinfo;
  94. ctx->data = (unsigned long)data;
  95. ctx->ppcerts = &ctx->certs;
  96. ctx->ppsinfo = &ctx->msg->signed_infos;
  97. /* Attempt to decode the signature */
  98. ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
  99. if (ret < 0) {
  100. msg = ERR_PTR(ret);
  101. goto out;
  102. }
  103. msg = ctx->msg;
  104. ctx->msg = NULL;
  105. out:
  106. while (ctx->certs) {
  107. struct x509_certificate *cert = ctx->certs;
  108. ctx->certs = cert->next;
  109. x509_free_certificate(cert);
  110. }
  111. pkcs7_free_signed_info(ctx->sinfo);
  112. out_no_sinfo:
  113. pkcs7_free_message(ctx->msg);
  114. out_no_msg:
  115. kfree(ctx);
  116. out_no_ctx:
  117. return msg;
  118. }
  119. EXPORT_SYMBOL_GPL(pkcs7_parse_message);
  120. /**
  121. * pkcs7_get_content_data - Get access to the PKCS#7 content
  122. * @pkcs7: The preparsed PKCS#7 message to access
  123. * @_data: Place to return a pointer to the data
  124. * @_data_len: Place to return the data length
  125. * @want_wrapper: True if the ASN.1 object header should be included in the data
  126. *
  127. * Get access to the data content of the PKCS#7 message, including, optionally,
  128. * the header of the ASN.1 object that contains it. Returns -ENODATA if the
  129. * data object was missing from the message.
  130. */
  131. int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
  132. const void **_data, size_t *_data_len,
  133. bool want_wrapper)
  134. {
  135. size_t wrapper;
  136. if (!pkcs7->data)
  137. return -ENODATA;
  138. wrapper = want_wrapper ? pkcs7->data_hdrlen : 0;
  139. *_data = pkcs7->data - wrapper;
  140. *_data_len = pkcs7->data_len + wrapper;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
  144. /*
  145. * Note an OID when we find one for later processing when we know how
  146. * to interpret it.
  147. */
  148. int pkcs7_note_OID(void *context, size_t hdrlen,
  149. unsigned char tag,
  150. const void *value, size_t vlen)
  151. {
  152. struct pkcs7_parse_context *ctx = context;
  153. ctx->last_oid = look_up_OID(value, vlen);
  154. if (ctx->last_oid == OID__NR) {
  155. char buffer[50];
  156. sprint_oid(value, vlen, buffer, sizeof(buffer));
  157. printk("PKCS7: Unknown OID: [%lu] %s\n",
  158. (unsigned long)value - ctx->data, buffer);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Note the digest algorithm for the signature.
  164. */
  165. int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
  166. unsigned char tag,
  167. const void *value, size_t vlen)
  168. {
  169. struct pkcs7_parse_context *ctx = context;
  170. switch (ctx->last_oid) {
  171. case OID_md4:
  172. ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4;
  173. break;
  174. case OID_md5:
  175. ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5;
  176. break;
  177. case OID_sha1:
  178. ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1;
  179. break;
  180. case OID_sha256:
  181. ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256;
  182. break;
  183. default:
  184. printk("Unsupported digest algo: %u\n", ctx->last_oid);
  185. return -ENOPKG;
  186. }
  187. return 0;
  188. }
  189. /*
  190. * Note the public key algorithm for the signature.
  191. */
  192. int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
  193. unsigned char tag,
  194. const void *value, size_t vlen)
  195. {
  196. struct pkcs7_parse_context *ctx = context;
  197. switch (ctx->last_oid) {
  198. case OID_rsaEncryption:
  199. ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA;
  200. break;
  201. default:
  202. printk("Unsupported pkey algo: %u\n", ctx->last_oid);
  203. return -ENOPKG;
  204. }
  205. return 0;
  206. }
  207. /*
  208. * Extract a certificate and store it in the context.
  209. */
  210. int pkcs7_extract_cert(void *context, size_t hdrlen,
  211. unsigned char tag,
  212. const void *value, size_t vlen)
  213. {
  214. struct pkcs7_parse_context *ctx = context;
  215. struct x509_certificate *x509;
  216. if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
  217. pr_debug("Cert began with tag %02x at %lu\n",
  218. tag, (unsigned long)ctx - ctx->data);
  219. return -EBADMSG;
  220. }
  221. /* We have to correct for the header so that the X.509 parser can start
  222. * from the beginning. Note that since X.509 stipulates DER, there
  223. * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
  224. * stipulates BER).
  225. */
  226. value -= hdrlen;
  227. vlen += hdrlen;
  228. if (((u8*)value)[1] == 0x80)
  229. vlen += 2; /* Indefinite length - there should be an EOC */
  230. x509 = x509_cert_parse(value, vlen);
  231. if (IS_ERR(x509))
  232. return PTR_ERR(x509);
  233. x509->index = ++ctx->x509_index;
  234. pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
  235. pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
  236. *ctx->ppcerts = x509;
  237. ctx->ppcerts = &x509->next;
  238. return 0;
  239. }
  240. /*
  241. * Save the certificate list
  242. */
  243. int pkcs7_note_certificate_list(void *context, size_t hdrlen,
  244. unsigned char tag,
  245. const void *value, size_t vlen)
  246. {
  247. struct pkcs7_parse_context *ctx = context;
  248. pr_devel("Got cert list (%02x)\n", tag);
  249. *ctx->ppcerts = ctx->msg->certs;
  250. ctx->msg->certs = ctx->certs;
  251. ctx->certs = NULL;
  252. ctx->ppcerts = &ctx->certs;
  253. return 0;
  254. }
  255. /*
  256. * Extract the data from the message and store that and its content type OID in
  257. * the context.
  258. */
  259. int pkcs7_note_data(void *context, size_t hdrlen,
  260. unsigned char tag,
  261. const void *value, size_t vlen)
  262. {
  263. struct pkcs7_parse_context *ctx = context;
  264. pr_debug("Got data\n");
  265. ctx->msg->data = value;
  266. ctx->msg->data_len = vlen;
  267. ctx->msg->data_hdrlen = hdrlen;
  268. ctx->msg->data_type = ctx->last_oid;
  269. return 0;
  270. }
  271. /*
  272. * Parse authenticated attributes
  273. */
  274. int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
  275. unsigned char tag,
  276. const void *value, size_t vlen)
  277. {
  278. struct pkcs7_parse_context *ctx = context;
  279. pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
  280. switch (ctx->last_oid) {
  281. case OID_messageDigest:
  282. if (tag != ASN1_OTS)
  283. return -EBADMSG;
  284. ctx->sinfo->msgdigest = value;
  285. ctx->sinfo->msgdigest_len = vlen;
  286. return 0;
  287. default:
  288. return 0;
  289. }
  290. }
  291. /*
  292. * Note the set of auth attributes for digestion purposes [RFC2315 9.3]
  293. */
  294. int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
  295. unsigned char tag,
  296. const void *value, size_t vlen)
  297. {
  298. struct pkcs7_parse_context *ctx = context;
  299. /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
  300. ctx->sinfo->authattrs = value - (hdrlen - 1);
  301. ctx->sinfo->authattrs_len = vlen + (hdrlen - 1);
  302. return 0;
  303. }
  304. /*
  305. * Note the issuing certificate serial number
  306. */
  307. int pkcs7_sig_note_serial(void *context, size_t hdrlen,
  308. unsigned char tag,
  309. const void *value, size_t vlen)
  310. {
  311. struct pkcs7_parse_context *ctx = context;
  312. ctx->raw_serial = value;
  313. ctx->raw_serial_size = vlen;
  314. return 0;
  315. }
  316. /*
  317. * Note the issuer's name
  318. */
  319. int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
  320. unsigned char tag,
  321. const void *value, size_t vlen)
  322. {
  323. struct pkcs7_parse_context *ctx = context;
  324. ctx->raw_issuer = value;
  325. ctx->raw_issuer_size = vlen;
  326. return 0;
  327. }
  328. /*
  329. * Note the signature data
  330. */
  331. int pkcs7_sig_note_signature(void *context, size_t hdrlen,
  332. unsigned char tag,
  333. const void *value, size_t vlen)
  334. {
  335. struct pkcs7_parse_context *ctx = context;
  336. MPI mpi;
  337. BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA);
  338. mpi = mpi_read_raw_data(value, vlen);
  339. if (!mpi)
  340. return -ENOMEM;
  341. ctx->sinfo->sig.mpi[0] = mpi;
  342. ctx->sinfo->sig.nr_mpi = 1;
  343. return 0;
  344. }
  345. /*
  346. * Note a signature information block
  347. */
  348. int pkcs7_note_signed_info(void *context, size_t hdrlen,
  349. unsigned char tag,
  350. const void *value, size_t vlen)
  351. {
  352. struct pkcs7_parse_context *ctx = context;
  353. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  354. struct asymmetric_key_id *kid;
  355. /* Generate cert issuer + serial number key ID */
  356. kid = asymmetric_key_generate_id(ctx->raw_serial,
  357. ctx->raw_serial_size,
  358. ctx->raw_issuer,
  359. ctx->raw_issuer_size);
  360. if (IS_ERR(kid))
  361. return PTR_ERR(kid);
  362. sinfo->signing_cert_id = kid;
  363. sinfo->index = ++ctx->sinfo_index;
  364. *ctx->ppsinfo = sinfo;
  365. ctx->ppsinfo = &sinfo->next;
  366. ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  367. if (!ctx->sinfo)
  368. return -ENOMEM;
  369. return 0;
  370. }