3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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"
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 */
35 * pkcs7_free_message - Free a PKCS#7 message
36 * @pkcs7: The PKCS#7 message to free
38 void pkcs7_free_message(struct pkcs7_message
*pkcs7
)
40 struct x509_certificate
*cert
;
41 struct pkcs7_signed_info
*sinfo
;
44 while (pkcs7
->certs
) {
46 pkcs7
->certs
= cert
->next
;
47 x509_free_certificate(cert
);
51 pkcs7
->crl
= cert
->next
;
52 x509_free_certificate(cert
);
54 while (pkcs7
->signed_infos
) {
55 sinfo
= pkcs7
->signed_infos
;
56 pkcs7
->signed_infos
= sinfo
->next
;
57 mpi_free(sinfo
->sig
.mpi
[0]);
58 kfree(sinfo
->sig
.digest
);
64 EXPORT_SYMBOL_GPL(pkcs7_free_message
);
67 * pkcs7_parse_message - Parse a PKCS#7 message
68 * @data: The raw binary ASN.1 encoded message to be parsed
69 * @datalen: The size of the encoded message
71 struct pkcs7_message
*pkcs7_parse_message(const void *data
, size_t datalen
)
73 struct pkcs7_parse_context
*ctx
;
74 struct pkcs7_message
*msg
;
78 msg
= kzalloc(sizeof(struct pkcs7_message
), GFP_KERNEL
);
81 ctx
= kzalloc(sizeof(struct pkcs7_parse_context
), GFP_KERNEL
);
84 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
89 ctx
->data
= (unsigned long)data
;
90 ctx
->ppcerts
= &ctx
->certs
;
91 ctx
->ppsinfo
= &ctx
->msg
->signed_infos
;
93 /* Attempt to decode the signature */
94 ret
= asn1_ber_decoder(&pkcs7_decoder
, ctx
, data
, datalen
);
99 struct x509_certificate
*cert
= ctx
->certs
;
100 ctx
->certs
= cert
->next
;
101 x509_free_certificate(cert
);
103 mpi_free(ctx
->sinfo
->sig
.mpi
[0]);
104 kfree(ctx
->sinfo
->sig
.digest
);
110 mpi_free(ctx
->sinfo
->sig
.mpi
[0]);
111 kfree(ctx
->sinfo
->sig
.digest
);
116 pkcs7_free_message(msg
);
120 EXPORT_SYMBOL_GPL(pkcs7_parse_message
);
123 * pkcs7_get_content_data - Get access to the PKCS#7 content
124 * @pkcs7: The preparsed PKCS#7 message to access
125 * @_data: Place to return a pointer to the data
126 * @_data_len: Place to return the data length
127 * @want_wrapper: True if the ASN.1 object header should be included in the data
129 * Get access to the data content of the PKCS#7 message, including, optionally,
130 * the header of the ASN.1 object that contains it. Returns -ENODATA if the
131 * data object was missing from the message.
133 int pkcs7_get_content_data(const struct pkcs7_message
*pkcs7
,
134 const void **_data
, size_t *_data_len
,
142 wrapper
= want_wrapper
? pkcs7
->data_hdrlen
: 0;
143 *_data
= pkcs7
->data
- wrapper
;
144 *_data_len
= pkcs7
->data_len
+ wrapper
;
147 EXPORT_SYMBOL_GPL(pkcs7_get_content_data
);
150 * Note an OID when we find one for later processing when we know how
153 int pkcs7_note_OID(void *context
, size_t hdrlen
,
155 const void *value
, size_t vlen
)
157 struct pkcs7_parse_context
*ctx
= context
;
159 ctx
->last_oid
= look_up_OID(value
, vlen
);
160 if (ctx
->last_oid
== OID__NR
) {
162 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
163 printk("PKCS7: Unknown OID: [%lu] %s\n",
164 (unsigned long)value
- ctx
->data
, buffer
);
170 * Note the digest algorithm for the signature.
172 int pkcs7_sig_note_digest_algo(void *context
, size_t hdrlen
,
174 const void *value
, size_t vlen
)
176 struct pkcs7_parse_context
*ctx
= context
;
178 switch (ctx
->last_oid
) {
180 ctx
->sinfo
->sig
.pkey_hash_algo
= HASH_ALGO_MD4
;
183 ctx
->sinfo
->sig
.pkey_hash_algo
= HASH_ALGO_MD5
;
186 ctx
->sinfo
->sig
.pkey_hash_algo
= HASH_ALGO_SHA1
;
189 ctx
->sinfo
->sig
.pkey_hash_algo
= HASH_ALGO_SHA256
;
192 printk("Unsupported digest algo: %u\n", ctx
->last_oid
);
199 * Note the public key algorithm for the signature.
201 int pkcs7_sig_note_pkey_algo(void *context
, size_t hdrlen
,
203 const void *value
, size_t vlen
)
205 struct pkcs7_parse_context
*ctx
= context
;
207 switch (ctx
->last_oid
) {
208 case OID_rsaEncryption
:
209 ctx
->sinfo
->sig
.pkey_algo
= PKEY_ALGO_RSA
;
212 printk("Unsupported pkey algo: %u\n", ctx
->last_oid
);
219 * Extract a certificate and store it in the context.
221 int pkcs7_extract_cert(void *context
, size_t hdrlen
,
223 const void *value
, size_t vlen
)
225 struct pkcs7_parse_context
*ctx
= context
;
226 struct x509_certificate
*x509
;
228 if (tag
!= ((ASN1_UNIV
<< 6) | ASN1_CONS_BIT
| ASN1_SEQ
)) {
229 pr_debug("Cert began with tag %02x at %lu\n",
230 tag
, (unsigned long)ctx
- ctx
->data
);
234 /* We have to correct for the header so that the X.509 parser can start
235 * from the beginning. Note that since X.509 stipulates DER, there
236 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
242 if (((u8
*)value
)[1] == 0x80)
243 vlen
+= 2; /* Indefinite length - there should be an EOC */
245 x509
= x509_cert_parse(value
, vlen
);
247 return PTR_ERR(x509
);
249 pr_debug("Got cert for %s\n", x509
->subject
);
250 pr_debug("- fingerprint %s\n", x509
->fingerprint
);
252 x509
->index
= ++ctx
->x509_index
;
253 *ctx
->ppcerts
= x509
;
254 ctx
->ppcerts
= &x509
->next
;
259 * Save the certificate list
261 int pkcs7_note_certificate_list(void *context
, size_t hdrlen
,
263 const void *value
, size_t vlen
)
265 struct pkcs7_parse_context
*ctx
= context
;
267 pr_devel("Got cert list (%02x)\n", tag
);
269 *ctx
->ppcerts
= ctx
->msg
->certs
;
270 ctx
->msg
->certs
= ctx
->certs
;
272 ctx
->ppcerts
= &ctx
->certs
;
277 * Extract the data from the message and store that and its content type OID in
280 int pkcs7_note_data(void *context
, size_t hdrlen
,
282 const void *value
, size_t vlen
)
284 struct pkcs7_parse_context
*ctx
= context
;
286 pr_debug("Got data\n");
288 ctx
->msg
->data
= value
;
289 ctx
->msg
->data_len
= vlen
;
290 ctx
->msg
->data_hdrlen
= hdrlen
;
291 ctx
->msg
->data_type
= ctx
->last_oid
;
296 * Parse authenticated attributes
298 int pkcs7_sig_note_authenticated_attr(void *context
, size_t hdrlen
,
300 const void *value
, size_t vlen
)
302 struct pkcs7_parse_context
*ctx
= context
;
304 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
306 switch (ctx
->last_oid
) {
307 case OID_messageDigest
:
310 ctx
->sinfo
->msgdigest
= value
;
311 ctx
->sinfo
->msgdigest_len
= vlen
;
319 * Note the set of auth attributes for digestion purposes [RFC2315 9.3]
321 int pkcs7_sig_note_set_of_authattrs(void *context
, size_t hdrlen
,
323 const void *value
, size_t vlen
)
325 struct pkcs7_parse_context
*ctx
= context
;
327 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
328 ctx
->sinfo
->authattrs
= value
- (hdrlen
- 1);
329 ctx
->sinfo
->authattrs_len
= vlen
+ (hdrlen
- 1);
334 * Note the issuing certificate serial number
336 int pkcs7_sig_note_serial(void *context
, size_t hdrlen
,
338 const void *value
, size_t vlen
)
340 struct pkcs7_parse_context
*ctx
= context
;
341 ctx
->sinfo
->raw_serial
= value
;
342 ctx
->sinfo
->raw_serial_size
= vlen
;
347 * Note the issuer's name
349 int pkcs7_sig_note_issuer(void *context
, size_t hdrlen
,
351 const void *value
, size_t vlen
)
353 struct pkcs7_parse_context
*ctx
= context
;
354 ctx
->sinfo
->raw_issuer
= value
;
355 ctx
->sinfo
->raw_issuer_size
= vlen
;
360 * Note the signature data
362 int pkcs7_sig_note_signature(void *context
, size_t hdrlen
,
364 const void *value
, size_t vlen
)
366 struct pkcs7_parse_context
*ctx
= context
;
369 BUG_ON(ctx
->sinfo
->sig
.pkey_algo
!= PKEY_ALGO_RSA
);
371 mpi
= mpi_read_raw_data(value
, vlen
);
375 ctx
->sinfo
->sig
.mpi
[0] = mpi
;
376 ctx
->sinfo
->sig
.nr_mpi
= 1;
381 * Note a signature information block
383 int pkcs7_note_signed_info(void *context
, size_t hdrlen
,
385 const void *value
, size_t vlen
)
387 struct pkcs7_parse_context
*ctx
= context
;
389 ctx
->sinfo
->index
= ++ctx
->sinfo_index
;
390 *ctx
->ppsinfo
= ctx
->sinfo
;
391 ctx
->ppsinfo
= &ctx
->sinfo
->next
;
392 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
This page took 0.041073 seconds and 5 git commands to generate.