PKCS#7: Check content type and versions
[cascardo/linux.git] / crypto / asymmetric_keys / pkcs7_parser.c
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 "public_key.h"
19 #include "pkcs7_parser.h"
20 #include "pkcs7-asn1.h"
21
22 struct 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 };
37
38 /*
39  * Free a signed information block.
40  */
41 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
42 {
43         if (sinfo) {
44                 mpi_free(sinfo->sig.mpi[0]);
45                 kfree(sinfo->sig.digest);
46                 kfree(sinfo->signing_cert_id);
47                 kfree(sinfo);
48         }
49 }
50
51 /**
52  * pkcs7_free_message - Free a PKCS#7 message
53  * @pkcs7: The PKCS#7 message to free
54  */
55 void pkcs7_free_message(struct pkcs7_message *pkcs7)
56 {
57         struct x509_certificate *cert;
58         struct pkcs7_signed_info *sinfo;
59
60         if (pkcs7) {
61                 while (pkcs7->certs) {
62                         cert = pkcs7->certs;
63                         pkcs7->certs = cert->next;
64                         x509_free_certificate(cert);
65                 }
66                 while (pkcs7->crl) {
67                         cert = pkcs7->crl;
68                         pkcs7->crl = cert->next;
69                         x509_free_certificate(cert);
70                 }
71                 while (pkcs7->signed_infos) {
72                         sinfo = pkcs7->signed_infos;
73                         pkcs7->signed_infos = sinfo->next;
74                         pkcs7_free_signed_info(sinfo);
75                 }
76                 kfree(pkcs7);
77         }
78 }
79 EXPORT_SYMBOL_GPL(pkcs7_free_message);
80
81 /**
82  * pkcs7_parse_message - Parse a PKCS#7 message
83  * @data: The raw binary ASN.1 encoded message to be parsed
84  * @datalen: The size of the encoded message
85  */
86 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
87 {
88         struct pkcs7_parse_context *ctx;
89         struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
90         int ret;
91
92         ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
93         if (!ctx)
94                 goto out_no_ctx;
95         ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
96         if (!ctx->msg)
97                 goto out_no_msg;
98         ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
99         if (!ctx->sinfo)
100                 goto out_no_sinfo;
101
102         ctx->data = (unsigned long)data;
103         ctx->ppcerts = &ctx->certs;
104         ctx->ppsinfo = &ctx->msg->signed_infos;
105
106         /* Attempt to decode the signature */
107         ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
108         if (ret < 0) {
109                 msg = ERR_PTR(ret);
110                 goto out;
111         }
112
113         msg = ctx->msg;
114         ctx->msg = NULL;
115
116 out:
117         while (ctx->certs) {
118                 struct x509_certificate *cert = ctx->certs;
119                 ctx->certs = cert->next;
120                 x509_free_certificate(cert);
121         }
122         pkcs7_free_signed_info(ctx->sinfo);
123 out_no_sinfo:
124         pkcs7_free_message(ctx->msg);
125 out_no_msg:
126         kfree(ctx);
127 out_no_ctx:
128         return msg;
129 }
130 EXPORT_SYMBOL_GPL(pkcs7_parse_message);
131
132 /**
133  * pkcs7_get_content_data - Get access to the PKCS#7 content
134  * @pkcs7: The preparsed PKCS#7 message to access
135  * @_data: Place to return a pointer to the data
136  * @_data_len: Place to return the data length
137  * @want_wrapper: True if the ASN.1 object header should be included in the data
138  *
139  * Get access to the data content of the PKCS#7 message, including, optionally,
140  * the header of the ASN.1 object that contains it.  Returns -ENODATA if the
141  * data object was missing from the message.
142  */
143 int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
144                            const void **_data, size_t *_data_len,
145                            bool want_wrapper)
146 {
147         size_t wrapper;
148
149         if (!pkcs7->data)
150                 return -ENODATA;
151
152         wrapper = want_wrapper ? pkcs7->data_hdrlen : 0;
153         *_data = pkcs7->data - wrapper;
154         *_data_len = pkcs7->data_len + wrapper;
155         return 0;
156 }
157 EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
158
159 /*
160  * Note an OID when we find one for later processing when we know how
161  * to interpret it.
162  */
163 int pkcs7_note_OID(void *context, size_t hdrlen,
164                    unsigned char tag,
165                    const void *value, size_t vlen)
166 {
167         struct pkcs7_parse_context *ctx = context;
168
169         ctx->last_oid = look_up_OID(value, vlen);
170         if (ctx->last_oid == OID__NR) {
171                 char buffer[50];
172                 sprint_oid(value, vlen, buffer, sizeof(buffer));
173                 printk("PKCS7: Unknown OID: [%lu] %s\n",
174                        (unsigned long)value - ctx->data, buffer);
175         }
176         return 0;
177 }
178
179 /*
180  * Note the digest algorithm for the signature.
181  */
182 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
183                                unsigned char tag,
184                                const void *value, size_t vlen)
185 {
186         struct pkcs7_parse_context *ctx = context;
187
188         switch (ctx->last_oid) {
189         case OID_md4:
190                 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4;
191                 break;
192         case OID_md5:
193                 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5;
194                 break;
195         case OID_sha1:
196                 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1;
197                 break;
198         case OID_sha256:
199                 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256;
200                 break;
201         default:
202                 printk("Unsupported digest algo: %u\n", ctx->last_oid);
203                 return -ENOPKG;
204         }
205         return 0;
206 }
207
208 /*
209  * Note the public key algorithm for the signature.
210  */
211 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
212                              unsigned char tag,
213                              const void *value, size_t vlen)
214 {
215         struct pkcs7_parse_context *ctx = context;
216
217         switch (ctx->last_oid) {
218         case OID_rsaEncryption:
219                 ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA;
220                 break;
221         default:
222                 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
223                 return -ENOPKG;
224         }
225         return 0;
226 }
227
228 /*
229  * We only support signed data [RFC2315 sec 9].
230  */
231 int pkcs7_check_content_type(void *context, size_t hdrlen,
232                              unsigned char tag,
233                              const void *value, size_t vlen)
234 {
235         struct pkcs7_parse_context *ctx = context;
236
237         if (ctx->last_oid != OID_signed_data) {
238                 pr_warn("Only support pkcs7_signedData type\n");
239                 return -EINVAL;
240         }
241
242         return 0;
243 }
244
245 /*
246  * Note the SignedData version
247  */
248 int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
249                                   unsigned char tag,
250                                   const void *value, size_t vlen)
251 {
252         unsigned version;
253
254         if (vlen != 1)
255                 goto unsupported;
256
257         version = *(const u8 *)value;
258         switch (version) {
259         case 1:
260                 /* PKCS#7 SignedData [RFC2315 sec 9.1] */
261                 break;
262         default:
263                 goto unsupported;
264         }
265
266         return 0;
267
268 unsupported:
269         pr_warn("Unsupported SignedData version\n");
270         return -EINVAL;
271 }
272
273 /*
274  * Note the SignerInfo version
275  */
276 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
277                                   unsigned char tag,
278                                   const void *value, size_t vlen)
279 {
280         unsigned version;
281
282         if (vlen != 1)
283                 goto unsupported;
284
285         version = *(const u8 *)value;
286         switch (version) {
287         case 1:
288                 /* PKCS#7 SignerInfo [RFC2315 sec 9.2] */
289                 break;
290         default:
291                 goto unsupported;
292         }
293
294         return 0;
295
296 unsupported:
297         pr_warn("Unsupported SignerInfo version\n");
298         return -EINVAL;
299 }
300
301 /*
302  * Extract a certificate and store it in the context.
303  */
304 int pkcs7_extract_cert(void *context, size_t hdrlen,
305                        unsigned char tag,
306                        const void *value, size_t vlen)
307 {
308         struct pkcs7_parse_context *ctx = context;
309         struct x509_certificate *x509;
310
311         if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
312                 pr_debug("Cert began with tag %02x at %lu\n",
313                          tag, (unsigned long)ctx - ctx->data);
314                 return -EBADMSG;
315         }
316
317         /* We have to correct for the header so that the X.509 parser can start
318          * from the beginning.  Note that since X.509 stipulates DER, there
319          * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
320          * stipulates BER).
321          */
322         value -= hdrlen;
323         vlen += hdrlen;
324
325         if (((u8*)value)[1] == 0x80)
326                 vlen += 2; /* Indefinite length - there should be an EOC */
327
328         x509 = x509_cert_parse(value, vlen);
329         if (IS_ERR(x509))
330                 return PTR_ERR(x509);
331
332         x509->index = ++ctx->x509_index;
333         pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
334         pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
335
336         *ctx->ppcerts = x509;
337         ctx->ppcerts = &x509->next;
338         return 0;
339 }
340
341 /*
342  * Save the certificate list
343  */
344 int pkcs7_note_certificate_list(void *context, size_t hdrlen,
345                                 unsigned char tag,
346                                 const void *value, size_t vlen)
347 {
348         struct pkcs7_parse_context *ctx = context;
349
350         pr_devel("Got cert list (%02x)\n", tag);
351
352         *ctx->ppcerts = ctx->msg->certs;
353         ctx->msg->certs = ctx->certs;
354         ctx->certs = NULL;
355         ctx->ppcerts = &ctx->certs;
356         return 0;
357 }
358
359 /*
360  * Extract the data from the message and store that and its content type OID in
361  * the context.
362  */
363 int pkcs7_note_data(void *context, size_t hdrlen,
364                     unsigned char tag,
365                     const void *value, size_t vlen)
366 {
367         struct pkcs7_parse_context *ctx = context;
368
369         pr_debug("Got data\n");
370
371         ctx->msg->data = value;
372         ctx->msg->data_len = vlen;
373         ctx->msg->data_hdrlen = hdrlen;
374         ctx->msg->data_type = ctx->last_oid;
375         return 0;
376 }
377
378 /*
379  * Parse authenticated attributes
380  */
381 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
382                                       unsigned char tag,
383                                       const void *value, size_t vlen)
384 {
385         struct pkcs7_parse_context *ctx = context;
386
387         pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
388
389         switch (ctx->last_oid) {
390         case OID_messageDigest:
391                 if (tag != ASN1_OTS)
392                         return -EBADMSG;
393                 ctx->sinfo->msgdigest = value;
394                 ctx->sinfo->msgdigest_len = vlen;
395                 return 0;
396         default:
397                 return 0;
398         }
399 }
400
401 /*
402  * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
403  */
404 int pkcs7_sig_note_set_of_authattrs(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         /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
411         ctx->sinfo->authattrs = value - (hdrlen - 1);
412         ctx->sinfo->authattrs_len = vlen + (hdrlen - 1);
413         return 0;
414 }
415
416 /*
417  * Note the issuing certificate serial number
418  */
419 int pkcs7_sig_note_serial(void *context, size_t hdrlen,
420                           unsigned char tag,
421                           const void *value, size_t vlen)
422 {
423         struct pkcs7_parse_context *ctx = context;
424         ctx->raw_serial = value;
425         ctx->raw_serial_size = vlen;
426         return 0;
427 }
428
429 /*
430  * Note the issuer's name
431  */
432 int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
433                           unsigned char tag,
434                           const void *value, size_t vlen)
435 {
436         struct pkcs7_parse_context *ctx = context;
437         ctx->raw_issuer = value;
438         ctx->raw_issuer_size = vlen;
439         return 0;
440 }
441
442 /*
443  * Note the signature data
444  */
445 int pkcs7_sig_note_signature(void *context, size_t hdrlen,
446                              unsigned char tag,
447                              const void *value, size_t vlen)
448 {
449         struct pkcs7_parse_context *ctx = context;
450         MPI mpi;
451
452         BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA);
453
454         mpi = mpi_read_raw_data(value, vlen);
455         if (!mpi)
456                 return -ENOMEM;
457
458         ctx->sinfo->sig.mpi[0] = mpi;
459         ctx->sinfo->sig.nr_mpi = 1;
460         return 0;
461 }
462
463 /*
464  * Note a signature information block
465  */
466 int pkcs7_note_signed_info(void *context, size_t hdrlen,
467                            unsigned char tag,
468                            const void *value, size_t vlen)
469 {
470         struct pkcs7_parse_context *ctx = context;
471         struct pkcs7_signed_info *sinfo = ctx->sinfo;
472         struct asymmetric_key_id *kid;
473
474         /* Generate cert issuer + serial number key ID */
475         kid = asymmetric_key_generate_id(ctx->raw_serial,
476                                          ctx->raw_serial_size,
477                                          ctx->raw_issuer,
478                                          ctx->raw_issuer_size);
479         if (IS_ERR(kid))
480                 return PTR_ERR(kid);
481
482         sinfo->signing_cert_id = kid;
483         sinfo->index = ++ctx->sinfo_index;
484         *ctx->ppsinfo = sinfo;
485         ctx->ppsinfo = &sinfo->next;
486         ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
487         if (!ctx->sinfo)
488                 return -ENOMEM;
489         return 0;
490 }