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