Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* PKCS#7 parser
  3 *
  4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5 * Written by David Howells (dhowells@redhat.com)
 
 
 
 
 
  6 */
  7
  8#define pr_fmt(fmt) "PKCS7: "fmt
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/export.h>
 12#include <linux/slab.h>
 13#include <linux/err.h>
 14#include <linux/oid_registry.h>
 15#include <crypto/public_key.h>
 16#include "pkcs7_parser.h"
 17#include "pkcs7.asn1.h"
 18
 19MODULE_DESCRIPTION("PKCS#7 parser");
 20MODULE_AUTHOR("Red Hat, Inc.");
 21MODULE_LICENSE("GPL");
 22
 23struct pkcs7_parse_context {
 24	struct pkcs7_message	*msg;		/* Message being constructed */
 25	struct pkcs7_signed_info *sinfo;	/* SignedInfo being constructed */
 26	struct pkcs7_signed_info **ppsinfo;
 27	struct x509_certificate *certs;		/* Certificate cache */
 28	struct x509_certificate **ppcerts;
 29	unsigned long	data;			/* Start of data */
 30	enum OID	last_oid;		/* Last OID encountered */
 31	unsigned	x509_index;
 32	unsigned	sinfo_index;
 33	const void	*raw_serial;
 34	unsigned	raw_serial_size;
 35	unsigned	raw_issuer_size;
 36	const void	*raw_issuer;
 37	const void	*raw_skid;
 38	unsigned	raw_skid_size;
 39	bool		expect_skid;
 40};
 41
 42/*
 43 * Free a signed information block.
 44 */
 45static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
 46{
 47	if (sinfo) {
 48		public_key_signature_free(sinfo->sig);
 
 
 49		kfree(sinfo);
 50	}
 51}
 52
 53/**
 54 * pkcs7_free_message - Free a PKCS#7 message
 55 * @pkcs7: The PKCS#7 message to free
 56 */
 57void pkcs7_free_message(struct pkcs7_message *pkcs7)
 58{
 59	struct x509_certificate *cert;
 60	struct pkcs7_signed_info *sinfo;
 61
 62	if (pkcs7) {
 63		while (pkcs7->certs) {
 64			cert = pkcs7->certs;
 65			pkcs7->certs = cert->next;
 66			x509_free_certificate(cert);
 67		}
 68		while (pkcs7->crl) {
 69			cert = pkcs7->crl;
 70			pkcs7->crl = cert->next;
 71			x509_free_certificate(cert);
 72		}
 73		while (pkcs7->signed_infos) {
 74			sinfo = pkcs7->signed_infos;
 75			pkcs7->signed_infos = sinfo->next;
 76			pkcs7_free_signed_info(sinfo);
 77		}
 78		kfree(pkcs7);
 79	}
 80}
 81EXPORT_SYMBOL_GPL(pkcs7_free_message);
 82
 83/*
 84 * Check authenticatedAttributes are provided or not provided consistently.
 85 */
 86static int pkcs7_check_authattrs(struct pkcs7_message *msg)
 87{
 88	struct pkcs7_signed_info *sinfo;
 89	bool want = false;
 90
 91	sinfo = msg->signed_infos;
 92	if (!sinfo)
 93		goto inconsistent;
 94
 95	if (sinfo->authattrs) {
 96		want = true;
 97		msg->have_authattrs = true;
 98	}
 99
100	for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
101		if (!!sinfo->authattrs != want)
102			goto inconsistent;
103	return 0;
104
105inconsistent:
106	pr_warn("Inconsistently supplied authAttrs\n");
107	return -EINVAL;
108}
109
110/**
111 * pkcs7_parse_message - Parse a PKCS#7 message
112 * @data: The raw binary ASN.1 encoded message to be parsed
113 * @datalen: The size of the encoded message
114 */
115struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
116{
117	struct pkcs7_parse_context *ctx;
118	struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
119	int ret;
120
121	ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
122	if (!ctx)
123		goto out_no_ctx;
124	ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
125	if (!ctx->msg)
126		goto out_no_msg;
127	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
128	if (!ctx->sinfo)
129		goto out_no_sinfo;
130	ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
131				  GFP_KERNEL);
132	if (!ctx->sinfo->sig)
133		goto out_no_sig;
134
135	ctx->data = (unsigned long)data;
136	ctx->ppcerts = &ctx->certs;
137	ctx->ppsinfo = &ctx->msg->signed_infos;
138
139	/* Attempt to decode the signature */
140	ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
141	if (ret < 0) {
142		msg = ERR_PTR(ret);
143		goto out;
144	}
145
146	ret = pkcs7_check_authattrs(ctx->msg);
147	if (ret < 0) {
148		msg = ERR_PTR(ret);
149		goto out;
150	}
151
152	msg = ctx->msg;
153	ctx->msg = NULL;
154
155out:
156	while (ctx->certs) {
157		struct x509_certificate *cert = ctx->certs;
158		ctx->certs = cert->next;
159		x509_free_certificate(cert);
160	}
161out_no_sig:
162	pkcs7_free_signed_info(ctx->sinfo);
163out_no_sinfo:
164	pkcs7_free_message(ctx->msg);
165out_no_msg:
166	kfree(ctx);
167out_no_ctx:
168	return msg;
169}
170EXPORT_SYMBOL_GPL(pkcs7_parse_message);
171
172/**
173 * pkcs7_get_content_data - Get access to the PKCS#7 content
174 * @pkcs7: The preparsed PKCS#7 message to access
175 * @_data: Place to return a pointer to the data
176 * @_data_len: Place to return the data length
177 * @_headerlen: Size of ASN.1 header not included in _data
178 *
179 * Get access to the data content of the PKCS#7 message.  The size of the
180 * header of the ASN.1 object that contains it is also provided and can be used
181 * to adjust *_data and *_data_len to get the entire object.
182 *
183 * Returns -ENODATA if the data object was missing from the message.
 
 
184 */
185int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
186			   const void **_data, size_t *_data_len,
187			   size_t *_headerlen)
188{
 
 
189	if (!pkcs7->data)
190		return -ENODATA;
191
192	*_data = pkcs7->data;
193	*_data_len = pkcs7->data_len;
194	if (_headerlen)
195		*_headerlen = pkcs7->data_hdrlen;
196	return 0;
197}
198EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
199
200/*
201 * Note an OID when we find one for later processing when we know how
202 * to interpret it.
203 */
204int pkcs7_note_OID(void *context, size_t hdrlen,
205		   unsigned char tag,
206		   const void *value, size_t vlen)
207{
208	struct pkcs7_parse_context *ctx = context;
209
210	ctx->last_oid = look_up_OID(value, vlen);
211	if (ctx->last_oid == OID__NR) {
212		char buffer[50];
213		sprint_oid(value, vlen, buffer, sizeof(buffer));
214		printk("PKCS7: Unknown OID: [%lu] %s\n",
215		       (unsigned long)value - ctx->data, buffer);
216	}
217	return 0;
218}
219
220/*
221 * Note the digest algorithm for the signature.
222 */
223int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
224			       unsigned char tag,
225			       const void *value, size_t vlen)
226{
227	struct pkcs7_parse_context *ctx = context;
228
229	switch (ctx->last_oid) {
230	case OID_md4:
231		ctx->sinfo->sig->hash_algo = "md4";
232		break;
233	case OID_md5:
234		ctx->sinfo->sig->hash_algo = "md5";
235		break;
236	case OID_sha1:
237		ctx->sinfo->sig->hash_algo = "sha1";
238		break;
239	case OID_sha256:
240		ctx->sinfo->sig->hash_algo = "sha256";
241		break;
242	case OID_sha384:
243		ctx->sinfo->sig->hash_algo = "sha384";
244		break;
245	case OID_sha512:
246		ctx->sinfo->sig->hash_algo = "sha512";
247		break;
248	case OID_sha224:
249		ctx->sinfo->sig->hash_algo = "sha224";
250		break;
251	case OID_sm3:
252		ctx->sinfo->sig->hash_algo = "sm3";
253		break;
254	case OID_gost2012Digest256:
255		ctx->sinfo->sig->hash_algo = "streebog256";
256		break;
257	case OID_gost2012Digest512:
258		ctx->sinfo->sig->hash_algo = "streebog512";
259		break;
260	default:
261		printk("Unsupported digest algo: %u\n", ctx->last_oid);
262		return -ENOPKG;
263	}
264	return 0;
265}
266
267/*
268 * Note the public key algorithm for the signature.
269 */
270int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
271			     unsigned char tag,
272			     const void *value, size_t vlen)
273{
274	struct pkcs7_parse_context *ctx = context;
275
276	switch (ctx->last_oid) {
277	case OID_rsaEncryption:
278		ctx->sinfo->sig->pkey_algo = "rsa";
279		ctx->sinfo->sig->encoding = "pkcs1";
280		break;
281	case OID_id_ecdsa_with_sha1:
282	case OID_id_ecdsa_with_sha224:
283	case OID_id_ecdsa_with_sha256:
284	case OID_id_ecdsa_with_sha384:
285	case OID_id_ecdsa_with_sha512:
286		ctx->sinfo->sig->pkey_algo = "ecdsa";
287		ctx->sinfo->sig->encoding = "x962";
288		break;
289	case OID_SM2_with_SM3:
290		ctx->sinfo->sig->pkey_algo = "sm2";
291		ctx->sinfo->sig->encoding = "raw";
292		break;
293	case OID_gost2012PKey256:
294	case OID_gost2012PKey512:
295		ctx->sinfo->sig->pkey_algo = "ecrdsa";
296		ctx->sinfo->sig->encoding = "raw";
297		break;
298	default:
299		printk("Unsupported pkey algo: %u\n", ctx->last_oid);
300		return -ENOPKG;
301	}
302	return 0;
303}
304
305/*
306 * We only support signed data [RFC2315 sec 9].
307 */
308int pkcs7_check_content_type(void *context, size_t hdrlen,
309			     unsigned char tag,
310			     const void *value, size_t vlen)
311{
312	struct pkcs7_parse_context *ctx = context;
313
314	if (ctx->last_oid != OID_signed_data) {
315		pr_warn("Only support pkcs7_signedData type\n");
316		return -EINVAL;
317	}
318
319	return 0;
320}
321
322/*
323 * Note the SignedData version
324 */
325int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
326				  unsigned char tag,
327				  const void *value, size_t vlen)
328{
329	struct pkcs7_parse_context *ctx = context;
330	unsigned version;
331
332	if (vlen != 1)
333		goto unsupported;
334
335	ctx->msg->version = version = *(const u8 *)value;
336	switch (version) {
337	case 1:
338		/* PKCS#7 SignedData [RFC2315 sec 9.1]
339		 * CMS ver 1 SignedData [RFC5652 sec 5.1]
340		 */
341		break;
342	case 3:
343		/* CMS ver 3 SignedData [RFC2315 sec 5.1] */
344		break;
345	default:
346		goto unsupported;
347	}
348
349	return 0;
350
351unsupported:
352	pr_warn("Unsupported SignedData version\n");
353	return -EINVAL;
354}
355
356/*
357 * Note the SignerInfo version
358 */
359int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
360				  unsigned char tag,
361				  const void *value, size_t vlen)
362{
363	struct pkcs7_parse_context *ctx = context;
364	unsigned version;
365
366	if (vlen != 1)
367		goto unsupported;
368
369	version = *(const u8 *)value;
370	switch (version) {
371	case 1:
372		/* PKCS#7 SignerInfo [RFC2315 sec 9.2]
373		 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
374		 */
375		if (ctx->msg->version != 1)
376			goto version_mismatch;
377		ctx->expect_skid = false;
378		break;
379	case 3:
380		/* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
381		if (ctx->msg->version == 1)
382			goto version_mismatch;
383		ctx->expect_skid = true;
384		break;
385	default:
386		goto unsupported;
387	}
388
389	return 0;
390
391unsupported:
392	pr_warn("Unsupported SignerInfo version\n");
393	return -EINVAL;
394version_mismatch:
395	pr_warn("SignedData-SignerInfo version mismatch\n");
396	return -EBADMSG;
397}
398
399/*
400 * Extract a certificate and store it in the context.
401 */
402int pkcs7_extract_cert(void *context, size_t hdrlen,
403		       unsigned char tag,
404		       const void *value, size_t vlen)
405{
406	struct pkcs7_parse_context *ctx = context;
407	struct x509_certificate *x509;
408
409	if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
410		pr_debug("Cert began with tag %02x at %lu\n",
411			 tag, (unsigned long)ctx - ctx->data);
412		return -EBADMSG;
413	}
414
415	/* We have to correct for the header so that the X.509 parser can start
416	 * from the beginning.  Note that since X.509 stipulates DER, there
417	 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
418	 * stipulates BER).
419	 */
420	value -= hdrlen;
421	vlen += hdrlen;
422
423	if (((u8*)value)[1] == 0x80)
424		vlen += 2; /* Indefinite length - there should be an EOC */
425
426	x509 = x509_cert_parse(value, vlen);
427	if (IS_ERR(x509))
428		return PTR_ERR(x509);
429
430	x509->index = ++ctx->x509_index;
431	pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
432	pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
433
434	*ctx->ppcerts = x509;
435	ctx->ppcerts = &x509->next;
436	return 0;
437}
438
439/*
440 * Save the certificate list
441 */
442int pkcs7_note_certificate_list(void *context, size_t hdrlen,
443				unsigned char tag,
444				const void *value, size_t vlen)
445{
446	struct pkcs7_parse_context *ctx = context;
447
448	pr_devel("Got cert list (%02x)\n", tag);
449
450	*ctx->ppcerts = ctx->msg->certs;
451	ctx->msg->certs = ctx->certs;
452	ctx->certs = NULL;
453	ctx->ppcerts = &ctx->certs;
454	return 0;
455}
456
457/*
458 * Note the content type.
459 */
460int pkcs7_note_content(void *context, size_t hdrlen,
461		       unsigned char tag,
462		       const void *value, size_t vlen)
463{
464	struct pkcs7_parse_context *ctx = context;
465
466	if (ctx->last_oid != OID_data &&
467	    ctx->last_oid != OID_msIndirectData) {
468		pr_warn("Unsupported data type %d\n", ctx->last_oid);
469		return -EINVAL;
470	}
471
472	ctx->msg->data_type = ctx->last_oid;
473	return 0;
474}
475
476/*
477 * Extract the data from the message and store that and its content type OID in
478 * the context.
479 */
480int pkcs7_note_data(void *context, size_t hdrlen,
481		    unsigned char tag,
482		    const void *value, size_t vlen)
483{
484	struct pkcs7_parse_context *ctx = context;
485
486	pr_debug("Got data\n");
487
488	ctx->msg->data = value;
489	ctx->msg->data_len = vlen;
490	ctx->msg->data_hdrlen = hdrlen;
491	return 0;
492}
493
494/*
495 * Parse authenticated attributes.
496 */
497int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
498				      unsigned char tag,
499				      const void *value, size_t vlen)
500{
501	struct pkcs7_parse_context *ctx = context;
502	struct pkcs7_signed_info *sinfo = ctx->sinfo;
503	enum OID content_type;
504
505	pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
506
507	switch (ctx->last_oid) {
508	case OID_contentType:
509		if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
510			goto repeated;
511		content_type = look_up_OID(value, vlen);
512		if (content_type != ctx->msg->data_type) {
513			pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
514				ctx->msg->data_type, sinfo->index,
515				content_type);
516			return -EBADMSG;
517		}
518		return 0;
519
520	case OID_signingTime:
521		if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
522			goto repeated;
523		/* Should we check that the signing time is consistent
524		 * with the signer's X.509 cert?
525		 */
526		return x509_decode_time(&sinfo->signing_time,
527					hdrlen, tag, value, vlen);
528
529	case OID_messageDigest:
530		if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
531			goto repeated;
532		if (tag != ASN1_OTS)
533			return -EBADMSG;
534		sinfo->msgdigest = value;
535		sinfo->msgdigest_len = vlen;
536		return 0;
537
538	case OID_smimeCapabilites:
539		if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
540			goto repeated;
541		if (ctx->msg->data_type != OID_msIndirectData) {
542			pr_warn("S/MIME Caps only allowed with Authenticode\n");
543			return -EKEYREJECTED;
544		}
545		return 0;
546
547		/* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
548		 * char URLs and cont[1] 8-bit char URLs.
549		 *
550		 * Microsoft StatementType seems to contain a list of OIDs that
551		 * are also used as extendedKeyUsage types in X.509 certs.
552		 */
553	case OID_msSpOpusInfo:
554		if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
555			goto repeated;
556		goto authenticode_check;
557	case OID_msStatementType:
558		if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
559			goto repeated;
560	authenticode_check:
561		if (ctx->msg->data_type != OID_msIndirectData) {
562			pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
563			return -EKEYREJECTED;
564		}
565		/* I'm not sure how to validate these */
566		return 0;
567	default:
568		return 0;
569	}
570
571repeated:
572	/* We permit max one item per AuthenticatedAttribute and no repeats */
573	pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
574	return -EKEYREJECTED;
575}
576
577/*
578 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
579 */
580int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
581				    unsigned char tag,
582				    const void *value, size_t vlen)
583{
584	struct pkcs7_parse_context *ctx = context;
585	struct pkcs7_signed_info *sinfo = ctx->sinfo;
586
587	if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
588	    !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
589		pr_warn("Missing required AuthAttr\n");
590		return -EBADMSG;
591	}
592
593	if (ctx->msg->data_type != OID_msIndirectData &&
594	    test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
595		pr_warn("Unexpected Authenticode AuthAttr\n");
596		return -EBADMSG;
597	}
598
599	/* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
600	sinfo->authattrs = value - (hdrlen - 1);
601	sinfo->authattrs_len = vlen + (hdrlen - 1);
602	return 0;
603}
604
605/*
606 * Note the issuing certificate serial number
607 */
608int pkcs7_sig_note_serial(void *context, size_t hdrlen,
609			  unsigned char tag,
610			  const void *value, size_t vlen)
611{
612	struct pkcs7_parse_context *ctx = context;
613	ctx->raw_serial = value;
614	ctx->raw_serial_size = vlen;
615	return 0;
616}
617
618/*
619 * Note the issuer's name
620 */
621int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
622			  unsigned char tag,
623			  const void *value, size_t vlen)
624{
625	struct pkcs7_parse_context *ctx = context;
626	ctx->raw_issuer = value;
627	ctx->raw_issuer_size = vlen;
628	return 0;
629}
630
631/*
632 * Note the issuing cert's subjectKeyIdentifier
633 */
634int pkcs7_sig_note_skid(void *context, size_t hdrlen,
635			unsigned char tag,
636			const void *value, size_t vlen)
637{
638	struct pkcs7_parse_context *ctx = context;
639
640	pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
641
642	ctx->raw_skid = value;
643	ctx->raw_skid_size = vlen;
644	return 0;
645}
646
647/*
648 * Note the signature data
649 */
650int pkcs7_sig_note_signature(void *context, size_t hdrlen,
651			     unsigned char tag,
652			     const void *value, size_t vlen)
653{
654	struct pkcs7_parse_context *ctx = context;
655
656	ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
657	if (!ctx->sinfo->sig->s)
658		return -ENOMEM;
659
660	ctx->sinfo->sig->s_size = vlen;
661	return 0;
662}
663
664/*
665 * Note a signature information block
666 */
667int pkcs7_note_signed_info(void *context, size_t hdrlen,
668			   unsigned char tag,
669			   const void *value, size_t vlen)
670{
671	struct pkcs7_parse_context *ctx = context;
672	struct pkcs7_signed_info *sinfo = ctx->sinfo;
673	struct asymmetric_key_id *kid;
674
675	if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
676		pr_warn("Authenticode requires AuthAttrs\n");
677		return -EBADMSG;
678	}
679
680	/* Generate cert issuer + serial number key ID */
681	if (!ctx->expect_skid) {
682		kid = asymmetric_key_generate_id(ctx->raw_serial,
683						 ctx->raw_serial_size,
684						 ctx->raw_issuer,
685						 ctx->raw_issuer_size);
686	} else {
687		kid = asymmetric_key_generate_id(ctx->raw_skid,
688						 ctx->raw_skid_size,
689						 "", 0);
690	}
691	if (IS_ERR(kid))
692		return PTR_ERR(kid);
693
694	pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
695
696	sinfo->sig->auth_ids[0] = kid;
697	sinfo->index = ++ctx->sinfo_index;
698	*ctx->ppsinfo = sinfo;
699	ctx->ppsinfo = &sinfo->next;
700	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
701	if (!ctx->sinfo)
702		return -ENOMEM;
703	ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
704				  GFP_KERNEL);
705	if (!ctx->sinfo->sig)
706		return -ENOMEM;
707	return 0;
708}
v4.6
 
  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
 12#define pr_fmt(fmt) "PKCS7: "fmt
 13#include <linux/kernel.h>
 
 14#include <linux/export.h>
 15#include <linux/slab.h>
 16#include <linux/err.h>
 17#include <linux/oid_registry.h>
 18#include <crypto/public_key.h>
 19#include "pkcs7_parser.h"
 20#include "pkcs7-asn1.h"
 
 
 
 
 21
 22struct pkcs7_parse_context {
 23	struct pkcs7_message	*msg;		/* Message being constructed */
 24	struct pkcs7_signed_info *sinfo;	/* SignedInfo being constructed */
 25	struct pkcs7_signed_info **ppsinfo;
 26	struct x509_certificate *certs;		/* Certificate cache */
 27	struct x509_certificate **ppcerts;
 28	unsigned long	data;			/* Start of data */
 29	enum OID	last_oid;		/* Last OID encountered */
 30	unsigned	x509_index;
 31	unsigned	sinfo_index;
 32	const void	*raw_serial;
 33	unsigned	raw_serial_size;
 34	unsigned	raw_issuer_size;
 35	const void	*raw_issuer;
 36	const void	*raw_skid;
 37	unsigned	raw_skid_size;
 38	bool		expect_skid;
 39};
 40
 41/*
 42 * Free a signed information block.
 43 */
 44static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
 45{
 46	if (sinfo) {
 47		kfree(sinfo->sig.s);
 48		kfree(sinfo->sig.digest);
 49		kfree(sinfo->signing_cert_id);
 50		kfree(sinfo);
 51	}
 52}
 53
 54/**
 55 * pkcs7_free_message - Free a PKCS#7 message
 56 * @pkcs7: The PKCS#7 message to free
 57 */
 58void pkcs7_free_message(struct pkcs7_message *pkcs7)
 59{
 60	struct x509_certificate *cert;
 61	struct pkcs7_signed_info *sinfo;
 62
 63	if (pkcs7) {
 64		while (pkcs7->certs) {
 65			cert = pkcs7->certs;
 66			pkcs7->certs = cert->next;
 67			x509_free_certificate(cert);
 68		}
 69		while (pkcs7->crl) {
 70			cert = pkcs7->crl;
 71			pkcs7->crl = cert->next;
 72			x509_free_certificate(cert);
 73		}
 74		while (pkcs7->signed_infos) {
 75			sinfo = pkcs7->signed_infos;
 76			pkcs7->signed_infos = sinfo->next;
 77			pkcs7_free_signed_info(sinfo);
 78		}
 79		kfree(pkcs7);
 80	}
 81}
 82EXPORT_SYMBOL_GPL(pkcs7_free_message);
 83
 84/*
 85 * Check authenticatedAttributes are provided or not provided consistently.
 86 */
 87static int pkcs7_check_authattrs(struct pkcs7_message *msg)
 88{
 89	struct pkcs7_signed_info *sinfo;
 90	bool want = false;
 91
 92	sinfo = msg->signed_infos;
 
 
 
 93	if (sinfo->authattrs) {
 94		want = true;
 95		msg->have_authattrs = true;
 96	}
 97
 98	for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
 99		if (!!sinfo->authattrs != want)
100			goto inconsistent;
101	return 0;
102
103inconsistent:
104	pr_warn("Inconsistently supplied authAttrs\n");
105	return -EINVAL;
106}
107
108/**
109 * pkcs7_parse_message - Parse a PKCS#7 message
110 * @data: The raw binary ASN.1 encoded message to be parsed
111 * @datalen: The size of the encoded message
112 */
113struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
114{
115	struct pkcs7_parse_context *ctx;
116	struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
117	int ret;
118
119	ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
120	if (!ctx)
121		goto out_no_ctx;
122	ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
123	if (!ctx->msg)
124		goto out_no_msg;
125	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
126	if (!ctx->sinfo)
127		goto out_no_sinfo;
 
 
 
 
128
129	ctx->data = (unsigned long)data;
130	ctx->ppcerts = &ctx->certs;
131	ctx->ppsinfo = &ctx->msg->signed_infos;
132
133	/* Attempt to decode the signature */
134	ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
135	if (ret < 0) {
136		msg = ERR_PTR(ret);
137		goto out;
138	}
139
140	ret = pkcs7_check_authattrs(ctx->msg);
141	if (ret < 0)
 
142		goto out;
 
143
144	msg = ctx->msg;
145	ctx->msg = NULL;
146
147out:
148	while (ctx->certs) {
149		struct x509_certificate *cert = ctx->certs;
150		ctx->certs = cert->next;
151		x509_free_certificate(cert);
152	}
 
153	pkcs7_free_signed_info(ctx->sinfo);
154out_no_sinfo:
155	pkcs7_free_message(ctx->msg);
156out_no_msg:
157	kfree(ctx);
158out_no_ctx:
159	return msg;
160}
161EXPORT_SYMBOL_GPL(pkcs7_parse_message);
162
163/**
164 * pkcs7_get_content_data - Get access to the PKCS#7 content
165 * @pkcs7: The preparsed PKCS#7 message to access
166 * @_data: Place to return a pointer to the data
167 * @_data_len: Place to return the data length
168 * @want_wrapper: True if the ASN.1 object header should be included in the data
 
 
 
 
169 *
170 * Get access to the data content of the PKCS#7 message, including, optionally,
171 * the header of the ASN.1 object that contains it.  Returns -ENODATA if the
172 * data object was missing from the message.
173 */
174int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
175			   const void **_data, size_t *_data_len,
176			   bool want_wrapper)
177{
178	size_t wrapper;
179
180	if (!pkcs7->data)
181		return -ENODATA;
182
183	wrapper = want_wrapper ? pkcs7->data_hdrlen : 0;
184	*_data = pkcs7->data - wrapper;
185	*_data_len = pkcs7->data_len + wrapper;
 
186	return 0;
187}
188EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
189
190/*
191 * Note an OID when we find one for later processing when we know how
192 * to interpret it.
193 */
194int pkcs7_note_OID(void *context, size_t hdrlen,
195		   unsigned char tag,
196		   const void *value, size_t vlen)
197{
198	struct pkcs7_parse_context *ctx = context;
199
200	ctx->last_oid = look_up_OID(value, vlen);
201	if (ctx->last_oid == OID__NR) {
202		char buffer[50];
203		sprint_oid(value, vlen, buffer, sizeof(buffer));
204		printk("PKCS7: Unknown OID: [%lu] %s\n",
205		       (unsigned long)value - ctx->data, buffer);
206	}
207	return 0;
208}
209
210/*
211 * Note the digest algorithm for the signature.
212 */
213int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
214			       unsigned char tag,
215			       const void *value, size_t vlen)
216{
217	struct pkcs7_parse_context *ctx = context;
218
219	switch (ctx->last_oid) {
220	case OID_md4:
221		ctx->sinfo->sig.hash_algo = "md4";
222		break;
223	case OID_md5:
224		ctx->sinfo->sig.hash_algo = "md5";
225		break;
226	case OID_sha1:
227		ctx->sinfo->sig.hash_algo = "sha1";
228		break;
229	case OID_sha256:
230		ctx->sinfo->sig.hash_algo = "sha256";
231		break;
232	case OID_sha384:
233		ctx->sinfo->sig.hash_algo = "sha384";
234		break;
235	case OID_sha512:
236		ctx->sinfo->sig.hash_algo = "sha512";
237		break;
238	case OID_sha224:
239		ctx->sinfo->sig.hash_algo = "sha224";
 
 
 
 
 
 
 
 
 
 
240	default:
241		printk("Unsupported digest algo: %u\n", ctx->last_oid);
242		return -ENOPKG;
243	}
244	return 0;
245}
246
247/*
248 * Note the public key algorithm for the signature.
249 */
250int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
251			     unsigned char tag,
252			     const void *value, size_t vlen)
253{
254	struct pkcs7_parse_context *ctx = context;
255
256	switch (ctx->last_oid) {
257	case OID_rsaEncryption:
258		ctx->sinfo->sig.pkey_algo = "rsa";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259		break;
260	default:
261		printk("Unsupported pkey algo: %u\n", ctx->last_oid);
262		return -ENOPKG;
263	}
264	return 0;
265}
266
267/*
268 * We only support signed data [RFC2315 sec 9].
269 */
270int pkcs7_check_content_type(void *context, size_t hdrlen,
271			     unsigned char tag,
272			     const void *value, size_t vlen)
273{
274	struct pkcs7_parse_context *ctx = context;
275
276	if (ctx->last_oid != OID_signed_data) {
277		pr_warn("Only support pkcs7_signedData type\n");
278		return -EINVAL;
279	}
280
281	return 0;
282}
283
284/*
285 * Note the SignedData version
286 */
287int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
288				  unsigned char tag,
289				  const void *value, size_t vlen)
290{
291	struct pkcs7_parse_context *ctx = context;
292	unsigned version;
293
294	if (vlen != 1)
295		goto unsupported;
296
297	ctx->msg->version = version = *(const u8 *)value;
298	switch (version) {
299	case 1:
300		/* PKCS#7 SignedData [RFC2315 sec 9.1]
301		 * CMS ver 1 SignedData [RFC5652 sec 5.1]
302		 */
303		break;
304	case 3:
305		/* CMS ver 3 SignedData [RFC2315 sec 5.1] */
306		break;
307	default:
308		goto unsupported;
309	}
310
311	return 0;
312
313unsupported:
314	pr_warn("Unsupported SignedData version\n");
315	return -EINVAL;
316}
317
318/*
319 * Note the SignerInfo version
320 */
321int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
322				  unsigned char tag,
323				  const void *value, size_t vlen)
324{
325	struct pkcs7_parse_context *ctx = context;
326	unsigned version;
327
328	if (vlen != 1)
329		goto unsupported;
330
331	version = *(const u8 *)value;
332	switch (version) {
333	case 1:
334		/* PKCS#7 SignerInfo [RFC2315 sec 9.2]
335		 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
336		 */
337		if (ctx->msg->version != 1)
338			goto version_mismatch;
339		ctx->expect_skid = false;
340		break;
341	case 3:
342		/* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
343		if (ctx->msg->version == 1)
344			goto version_mismatch;
345		ctx->expect_skid = true;
346		break;
347	default:
348		goto unsupported;
349	}
350
351	return 0;
352
353unsupported:
354	pr_warn("Unsupported SignerInfo version\n");
355	return -EINVAL;
356version_mismatch:
357	pr_warn("SignedData-SignerInfo version mismatch\n");
358	return -EBADMSG;
359}
360
361/*
362 * Extract a certificate and store it in the context.
363 */
364int pkcs7_extract_cert(void *context, size_t hdrlen,
365		       unsigned char tag,
366		       const void *value, size_t vlen)
367{
368	struct pkcs7_parse_context *ctx = context;
369	struct x509_certificate *x509;
370
371	if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
372		pr_debug("Cert began with tag %02x at %lu\n",
373			 tag, (unsigned long)ctx - ctx->data);
374		return -EBADMSG;
375	}
376
377	/* We have to correct for the header so that the X.509 parser can start
378	 * from the beginning.  Note that since X.509 stipulates DER, there
379	 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
380	 * stipulates BER).
381	 */
382	value -= hdrlen;
383	vlen += hdrlen;
384
385	if (((u8*)value)[1] == 0x80)
386		vlen += 2; /* Indefinite length - there should be an EOC */
387
388	x509 = x509_cert_parse(value, vlen);
389	if (IS_ERR(x509))
390		return PTR_ERR(x509);
391
392	x509->index = ++ctx->x509_index;
393	pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
394	pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
395
396	*ctx->ppcerts = x509;
397	ctx->ppcerts = &x509->next;
398	return 0;
399}
400
401/*
402 * Save the certificate list
403 */
404int pkcs7_note_certificate_list(void *context, size_t hdrlen,
405				unsigned char tag,
406				const void *value, size_t vlen)
407{
408	struct pkcs7_parse_context *ctx = context;
409
410	pr_devel("Got cert list (%02x)\n", tag);
411
412	*ctx->ppcerts = ctx->msg->certs;
413	ctx->msg->certs = ctx->certs;
414	ctx->certs = NULL;
415	ctx->ppcerts = &ctx->certs;
416	return 0;
417}
418
419/*
420 * Note the content type.
421 */
422int pkcs7_note_content(void *context, size_t hdrlen,
423		       unsigned char tag,
424		       const void *value, size_t vlen)
425{
426	struct pkcs7_parse_context *ctx = context;
427
428	if (ctx->last_oid != OID_data &&
429	    ctx->last_oid != OID_msIndirectData) {
430		pr_warn("Unsupported data type %d\n", ctx->last_oid);
431		return -EINVAL;
432	}
433
434	ctx->msg->data_type = ctx->last_oid;
435	return 0;
436}
437
438/*
439 * Extract the data from the message and store that and its content type OID in
440 * the context.
441 */
442int pkcs7_note_data(void *context, size_t hdrlen,
443		    unsigned char tag,
444		    const void *value, size_t vlen)
445{
446	struct pkcs7_parse_context *ctx = context;
447
448	pr_debug("Got data\n");
449
450	ctx->msg->data = value;
451	ctx->msg->data_len = vlen;
452	ctx->msg->data_hdrlen = hdrlen;
453	return 0;
454}
455
456/*
457 * Parse authenticated attributes.
458 */
459int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
460				      unsigned char tag,
461				      const void *value, size_t vlen)
462{
463	struct pkcs7_parse_context *ctx = context;
464	struct pkcs7_signed_info *sinfo = ctx->sinfo;
465	enum OID content_type;
466
467	pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
468
469	switch (ctx->last_oid) {
470	case OID_contentType:
471		if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
472			goto repeated;
473		content_type = look_up_OID(value, vlen);
474		if (content_type != ctx->msg->data_type) {
475			pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
476				ctx->msg->data_type, sinfo->index,
477				content_type);
478			return -EBADMSG;
479		}
480		return 0;
481
482	case OID_signingTime:
483		if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
484			goto repeated;
485		/* Should we check that the signing time is consistent
486		 * with the signer's X.509 cert?
487		 */
488		return x509_decode_time(&sinfo->signing_time,
489					hdrlen, tag, value, vlen);
490
491	case OID_messageDigest:
492		if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
493			goto repeated;
494		if (tag != ASN1_OTS)
495			return -EBADMSG;
496		sinfo->msgdigest = value;
497		sinfo->msgdigest_len = vlen;
498		return 0;
499
500	case OID_smimeCapabilites:
501		if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
502			goto repeated;
503		if (ctx->msg->data_type != OID_msIndirectData) {
504			pr_warn("S/MIME Caps only allowed with Authenticode\n");
505			return -EKEYREJECTED;
506		}
507		return 0;
508
509		/* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
510		 * char URLs and cont[1] 8-bit char URLs.
511		 *
512		 * Microsoft StatementType seems to contain a list of OIDs that
513		 * are also used as extendedKeyUsage types in X.509 certs.
514		 */
515	case OID_msSpOpusInfo:
516		if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
517			goto repeated;
518		goto authenticode_check;
519	case OID_msStatementType:
520		if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
521			goto repeated;
522	authenticode_check:
523		if (ctx->msg->data_type != OID_msIndirectData) {
524			pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
525			return -EKEYREJECTED;
526		}
527		/* I'm not sure how to validate these */
528		return 0;
529	default:
530		return 0;
531	}
532
533repeated:
534	/* We permit max one item per AuthenticatedAttribute and no repeats */
535	pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
536	return -EKEYREJECTED;
537}
538
539/*
540 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
541 */
542int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
543				    unsigned char tag,
544				    const void *value, size_t vlen)
545{
546	struct pkcs7_parse_context *ctx = context;
547	struct pkcs7_signed_info *sinfo = ctx->sinfo;
548
549	if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
550	    !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
551		pr_warn("Missing required AuthAttr\n");
552		return -EBADMSG;
553	}
554
555	if (ctx->msg->data_type != OID_msIndirectData &&
556	    test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
557		pr_warn("Unexpected Authenticode AuthAttr\n");
558		return -EBADMSG;
559	}
560
561	/* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
562	sinfo->authattrs = value - (hdrlen - 1);
563	sinfo->authattrs_len = vlen + (hdrlen - 1);
564	return 0;
565}
566
567/*
568 * Note the issuing certificate serial number
569 */
570int pkcs7_sig_note_serial(void *context, size_t hdrlen,
571			  unsigned char tag,
572			  const void *value, size_t vlen)
573{
574	struct pkcs7_parse_context *ctx = context;
575	ctx->raw_serial = value;
576	ctx->raw_serial_size = vlen;
577	return 0;
578}
579
580/*
581 * Note the issuer's name
582 */
583int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
584			  unsigned char tag,
585			  const void *value, size_t vlen)
586{
587	struct pkcs7_parse_context *ctx = context;
588	ctx->raw_issuer = value;
589	ctx->raw_issuer_size = vlen;
590	return 0;
591}
592
593/*
594 * Note the issuing cert's subjectKeyIdentifier
595 */
596int pkcs7_sig_note_skid(void *context, size_t hdrlen,
597			unsigned char tag,
598			const void *value, size_t vlen)
599{
600	struct pkcs7_parse_context *ctx = context;
601
602	pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
603
604	ctx->raw_skid = value;
605	ctx->raw_skid_size = vlen;
606	return 0;
607}
608
609/*
610 * Note the signature data
611 */
612int pkcs7_sig_note_signature(void *context, size_t hdrlen,
613			     unsigned char tag,
614			     const void *value, size_t vlen)
615{
616	struct pkcs7_parse_context *ctx = context;
617
618	ctx->sinfo->sig.s = kmemdup(value, vlen, GFP_KERNEL);
619	if (!ctx->sinfo->sig.s)
620		return -ENOMEM;
621
622	ctx->sinfo->sig.s_size = vlen;
623	return 0;
624}
625
626/*
627 * Note a signature information block
628 */
629int pkcs7_note_signed_info(void *context, size_t hdrlen,
630			   unsigned char tag,
631			   const void *value, size_t vlen)
632{
633	struct pkcs7_parse_context *ctx = context;
634	struct pkcs7_signed_info *sinfo = ctx->sinfo;
635	struct asymmetric_key_id *kid;
636
637	if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
638		pr_warn("Authenticode requires AuthAttrs\n");
639		return -EBADMSG;
640	}
641
642	/* Generate cert issuer + serial number key ID */
643	if (!ctx->expect_skid) {
644		kid = asymmetric_key_generate_id(ctx->raw_serial,
645						 ctx->raw_serial_size,
646						 ctx->raw_issuer,
647						 ctx->raw_issuer_size);
648	} else {
649		kid = asymmetric_key_generate_id(ctx->raw_skid,
650						 ctx->raw_skid_size,
651						 "", 0);
652	}
653	if (IS_ERR(kid))
654		return PTR_ERR(kid);
655
656	pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
657
658	sinfo->signing_cert_id = kid;
659	sinfo->index = ++ctx->sinfo_index;
660	*ctx->ppsinfo = sinfo;
661	ctx->ppsinfo = &sinfo->next;
662	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
663	if (!ctx->sinfo)
 
 
 
 
664		return -ENOMEM;
665	return 0;
666}