diff options
author | David Howells <dhowells@redhat.com> | 2014-07-01 11:40:19 -0400 |
---|---|---|
committer | David Howells <dhowells@redhat.com> | 2014-07-08 08:49:56 -0400 |
commit | 2e3fadbf730fd0d13c891d5e555af3e7f39ca3f4 (patch) | |
tree | 8618dea8bfd135a9e81650adc3080883410fd30e /crypto | |
parent | ace0107a3936774e9da2c30cbdc99ad13c103c10 (diff) |
PKCS#7: Implement a parser [RFC 2315]
Implement a parser for a PKCS#7 signed-data message as described in part of
RFC 2315.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/asymmetric_keys/Kconfig | 9 | ||||
-rw-r--r-- | crypto/asymmetric_keys/Makefile | 13 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7.asn1 | 127 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7_parser.c | 396 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7_parser.h | 61 |
5 files changed, 606 insertions, 0 deletions
diff --git a/crypto/asymmetric_keys/Kconfig b/crypto/asymmetric_keys/Kconfig index 03a6eb95ab50..a7cec9dd6154 100644 --- a/crypto/asymmetric_keys/Kconfig +++ b/crypto/asymmetric_keys/Kconfig | |||
@@ -37,4 +37,13 @@ config X509_CERTIFICATE_PARSER | |||
37 | data and provides the ability to instantiate a crypto key from a | 37 | data and provides the ability to instantiate a crypto key from a |
38 | public key packet found inside the certificate. | 38 | public key packet found inside the certificate. |
39 | 39 | ||
40 | config PKCS7_MESSAGE_PARSER | ||
41 | tristate "PKCS#7 message parser" | ||
42 | depends on X509_CERTIFICATE_PARSER | ||
43 | select ASN1 | ||
44 | select OID_REGISTRY | ||
45 | help | ||
46 | This option provides support for parsing PKCS#7 format messages for | ||
47 | signature data and provides the ability to verify the signature. | ||
48 | |||
40 | endif # ASYMMETRIC_KEY_TYPE | 49 | endif # ASYMMETRIC_KEY_TYPE |
diff --git a/crypto/asymmetric_keys/Makefile b/crypto/asymmetric_keys/Makefile index 0727204aab68..59d8cade5cfe 100644 --- a/crypto/asymmetric_keys/Makefile +++ b/crypto/asymmetric_keys/Makefile | |||
@@ -25,3 +25,16 @@ $(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h | |||
25 | 25 | ||
26 | clean-files += x509-asn1.c x509-asn1.h | 26 | clean-files += x509-asn1.c x509-asn1.h |
27 | clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h | 27 | clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h |
28 | |||
29 | # | ||
30 | # PKCS#7 message handling | ||
31 | # | ||
32 | obj-$(CONFIG_PKCS7_MESSAGE_PARSER) += pkcs7_message.o | ||
33 | pkcs7_message-y := \ | ||
34 | pkcs7-asn1.o \ | ||
35 | pkcs7_parser.o | ||
36 | |||
37 | $(obj)/pkcs7_parser.o: $(obj)/pkcs7-asn1.h | ||
38 | $(obj)/pkcs7-asn1.o: $(obj)/pkcs7-asn1.c $(obj)/pkcs7-asn1.h | ||
39 | |||
40 | clean-files += pkcs7-asn1.c pkcs7-asn1.h | ||
diff --git a/crypto/asymmetric_keys/pkcs7.asn1 b/crypto/asymmetric_keys/pkcs7.asn1 new file mode 100644 index 000000000000..a5a14ef28c86 --- /dev/null +++ b/crypto/asymmetric_keys/pkcs7.asn1 | |||
@@ -0,0 +1,127 @@ | |||
1 | PKCS7ContentInfo ::= SEQUENCE { | ||
2 | contentType ContentType, | ||
3 | content [0] EXPLICIT SignedData OPTIONAL | ||
4 | } | ||
5 | |||
6 | ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID }) | ||
7 | |||
8 | SignedData ::= SEQUENCE { | ||
9 | version INTEGER, | ||
10 | digestAlgorithms DigestAlgorithmIdentifiers, | ||
11 | contentInfo ContentInfo, | ||
12 | certificates CHOICE { | ||
13 | certSet [0] IMPLICIT ExtendedCertificatesAndCertificates, | ||
14 | certSequence [2] IMPLICIT Certificates | ||
15 | } OPTIONAL ({ pkcs7_note_certificate_list }), | ||
16 | crls CHOICE { | ||
17 | crlSet [1] IMPLICIT CertificateRevocationLists, | ||
18 | crlSequence [3] IMPLICIT CRLSequence | ||
19 | } OPTIONAL, | ||
20 | signerInfos SignerInfos | ||
21 | } | ||
22 | |||
23 | ContentInfo ::= SEQUENCE { | ||
24 | contentType ContentType, | ||
25 | content [0] EXPLICIT Data OPTIONAL | ||
26 | } | ||
27 | |||
28 | Data ::= ANY ({ pkcs7_note_data }) | ||
29 | |||
30 | DigestAlgorithmIdentifiers ::= CHOICE { | ||
31 | daSet SET OF DigestAlgorithmIdentifier, | ||
32 | daSequence SEQUENCE OF DigestAlgorithmIdentifier | ||
33 | } | ||
34 | |||
35 | DigestAlgorithmIdentifier ::= SEQUENCE { | ||
36 | algorithm OBJECT IDENTIFIER ({ pkcs7_note_OID }), | ||
37 | parameters ANY OPTIONAL | ||
38 | } | ||
39 | |||
40 | -- | ||
41 | -- Certificates and certificate lists | ||
42 | -- | ||
43 | ExtendedCertificatesAndCertificates ::= SET OF ExtendedCertificateOrCertificate | ||
44 | |||
45 | ExtendedCertificateOrCertificate ::= CHOICE { | ||
46 | certificate Certificate, -- X.509 | ||
47 | extendedCertificate [0] IMPLICIT ExtendedCertificate -- PKCS#6 | ||
48 | } | ||
49 | |||
50 | ExtendedCertificate ::= Certificate -- cheating | ||
51 | |||
52 | Certificates ::= SEQUENCE OF Certificate | ||
53 | |||
54 | CertificateRevocationLists ::= SET OF CertificateList | ||
55 | |||
56 | CertificateList ::= SEQUENCE OF Certificate -- This may be defined incorrectly | ||
57 | |||
58 | CRLSequence ::= SEQUENCE OF CertificateList | ||
59 | |||
60 | Certificate ::= ANY ({ pkcs7_extract_cert }) -- X.509 | ||
61 | |||
62 | -- | ||
63 | -- Signer information | ||
64 | -- | ||
65 | SignerInfos ::= CHOICE { | ||
66 | siSet SET OF SignerInfo, | ||
67 | siSequence SEQUENCE OF SignerInfo | ||
68 | } | ||
69 | |||
70 | SignerInfo ::= SEQUENCE { | ||
71 | version INTEGER, | ||
72 | issuerAndSerialNumber IssuerAndSerialNumber, | ||
73 | digestAlgorithm DigestAlgorithmIdentifier ({ pkcs7_sig_note_digest_algo }), | ||
74 | authenticatedAttributes CHOICE { | ||
75 | aaSet [0] IMPLICIT SetOfAuthenticatedAttribute | ||
76 | ({ pkcs7_sig_note_set_of_authattrs }), | ||
77 | aaSequence [2] EXPLICIT SEQUENCE OF AuthenticatedAttribute | ||
78 | -- Explicit because easier to compute digest on | ||
79 | -- sequence of attributes and then reuse encoded | ||
80 | -- sequence in aaSequence. | ||
81 | } OPTIONAL, | ||
82 | digestEncryptionAlgorithm | ||
83 | DigestEncryptionAlgorithmIdentifier ({ pkcs7_sig_note_pkey_algo }), | ||
84 | encryptedDigest EncryptedDigest, | ||
85 | unauthenticatedAttributes CHOICE { | ||
86 | uaSet [1] IMPLICIT SET OF UnauthenticatedAttribute, | ||
87 | uaSequence [3] IMPLICIT SEQUENCE OF UnauthenticatedAttribute | ||
88 | } OPTIONAL | ||
89 | } ({ pkcs7_note_signed_info }) | ||
90 | |||
91 | IssuerAndSerialNumber ::= SEQUENCE { | ||
92 | issuer Name ({ pkcs7_sig_note_issuer }), | ||
93 | serialNumber CertificateSerialNumber ({ pkcs7_sig_note_serial }) | ||
94 | } | ||
95 | |||
96 | CertificateSerialNumber ::= INTEGER | ||
97 | |||
98 | SetOfAuthenticatedAttribute ::= SET OF AuthenticatedAttribute | ||
99 | |||
100 | AuthenticatedAttribute ::= SEQUENCE { | ||
101 | type OBJECT IDENTIFIER ({ pkcs7_note_OID }), | ||
102 | values SET OF ANY ({ pkcs7_sig_note_authenticated_attr }) | ||
103 | } | ||
104 | |||
105 | UnauthenticatedAttribute ::= SEQUENCE { | ||
106 | type OBJECT IDENTIFIER ({ pkcs7_note_OID }), | ||
107 | values SET OF ANY | ||
108 | } | ||
109 | |||
110 | DigestEncryptionAlgorithmIdentifier ::= SEQUENCE { | ||
111 | algorithm OBJECT IDENTIFIER ({ pkcs7_note_OID }), | ||
112 | parameters ANY OPTIONAL | ||
113 | } | ||
114 | |||
115 | EncryptedDigest ::= OCTET STRING ({ pkcs7_sig_note_signature }) | ||
116 | |||
117 | --- | ||
118 | --- X.500 Name | ||
119 | --- | ||
120 | Name ::= SEQUENCE OF RelativeDistinguishedName | ||
121 | |||
122 | RelativeDistinguishedName ::= SET OF AttributeValueAssertion | ||
123 | |||
124 | AttributeValueAssertion ::= SEQUENCE { | ||
125 | attributeType OBJECT IDENTIFIER ({ pkcs7_note_OID }), | ||
126 | attributeValue ANY | ||
127 | } | ||
diff --git a/crypto/asymmetric_keys/pkcs7_parser.c b/crypto/asymmetric_keys/pkcs7_parser.c new file mode 100644 index 000000000000..42e56aa7d277 --- /dev/null +++ b/crypto/asymmetric_keys/pkcs7_parser.c | |||
@@ -0,0 +1,396 @@ | |||
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 | }; | ||
33 | |||
34 | /** | ||
35 | * pkcs7_free_message - Free a PKCS#7 message | ||
36 | * @pkcs7: The PKCS#7 message to free | ||
37 | */ | ||
38 | void pkcs7_free_message(struct pkcs7_message *pkcs7) | ||
39 | { | ||
40 | struct x509_certificate *cert; | ||
41 | struct pkcs7_signed_info *sinfo; | ||
42 | |||
43 | if (pkcs7) { | ||
44 | while (pkcs7->certs) { | ||
45 | cert = pkcs7->certs; | ||
46 | pkcs7->certs = cert->next; | ||
47 | x509_free_certificate(cert); | ||
48 | } | ||
49 | while (pkcs7->crl) { | ||
50 | cert = pkcs7->crl; | ||
51 | pkcs7->crl = cert->next; | ||
52 | x509_free_certificate(cert); | ||
53 | } | ||
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); | ||
59 | kfree(sinfo); | ||
60 | } | ||
61 | kfree(pkcs7); | ||
62 | } | ||
63 | } | ||
64 | EXPORT_SYMBOL_GPL(pkcs7_free_message); | ||
65 | |||
66 | /** | ||
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 | ||
70 | */ | ||
71 | struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen) | ||
72 | { | ||
73 | struct pkcs7_parse_context *ctx; | ||
74 | struct pkcs7_message *msg; | ||
75 | long ret; | ||
76 | |||
77 | ret = -ENOMEM; | ||
78 | msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL); | ||
79 | if (!msg) | ||
80 | goto error_no_sig; | ||
81 | ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL); | ||
82 | if (!ctx) | ||
83 | goto error_no_ctx; | ||
84 | ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL); | ||
85 | if (!ctx->sinfo) | ||
86 | goto error_no_sinfo; | ||
87 | |||
88 | ctx->msg = msg; | ||
89 | ctx->data = (unsigned long)data; | ||
90 | ctx->ppcerts = &ctx->certs; | ||
91 | ctx->ppsinfo = &ctx->msg->signed_infos; | ||
92 | |||
93 | /* Attempt to decode the signature */ | ||
94 | ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen); | ||
95 | if (ret < 0) | ||
96 | goto error_decode; | ||
97 | |||
98 | while (ctx->certs) { | ||
99 | struct x509_certificate *cert = ctx->certs; | ||
100 | ctx->certs = cert->next; | ||
101 | x509_free_certificate(cert); | ||
102 | } | ||
103 | mpi_free(ctx->sinfo->sig.mpi[0]); | ||
104 | kfree(ctx->sinfo->sig.digest); | ||
105 | kfree(ctx->sinfo); | ||
106 | kfree(ctx); | ||
107 | return msg; | ||
108 | |||
109 | error_decode: | ||
110 | mpi_free(ctx->sinfo->sig.mpi[0]); | ||
111 | kfree(ctx->sinfo->sig.digest); | ||
112 | kfree(ctx->sinfo); | ||
113 | error_no_sinfo: | ||
114 | kfree(ctx); | ||
115 | error_no_ctx: | ||
116 | pkcs7_free_message(msg); | ||
117 | error_no_sig: | ||
118 | return ERR_PTR(ret); | ||
119 | } | ||
120 | EXPORT_SYMBOL_GPL(pkcs7_parse_message); | ||
121 | |||
122 | /** | ||
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 | ||
128 | * | ||
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. | ||
132 | */ | ||
133 | int pkcs7_get_content_data(const struct pkcs7_message *pkcs7, | ||
134 | const void **_data, size_t *_data_len, | ||
135 | bool want_wrapper) | ||
136 | { | ||
137 | size_t wrapper; | ||
138 | |||
139 | if (!pkcs7->data) | ||
140 | return -ENODATA; | ||
141 | |||
142 | wrapper = want_wrapper ? pkcs7->data_hdrlen : 0; | ||
143 | *_data = pkcs7->data - wrapper; | ||
144 | *_data_len = pkcs7->data_len + wrapper; | ||
145 | return 0; | ||
146 | } | ||
147 | EXPORT_SYMBOL_GPL(pkcs7_get_content_data); | ||
148 | |||
149 | /* | ||
150 | * Note an OID when we find one for later processing when we know how | ||
151 | * to interpret it. | ||
152 | */ | ||
153 | int pkcs7_note_OID(void *context, size_t hdrlen, | ||
154 | unsigned char tag, | ||
155 | const void *value, size_t vlen) | ||
156 | { | ||
157 | struct pkcs7_parse_context *ctx = context; | ||
158 | |||
159 | ctx->last_oid = look_up_OID(value, vlen); | ||
160 | if (ctx->last_oid == OID__NR) { | ||
161 | char buffer[50]; | ||
162 | sprint_oid(value, vlen, buffer, sizeof(buffer)); | ||
163 | printk("PKCS7: Unknown OID: [%lu] %s\n", | ||
164 | (unsigned long)value - ctx->data, buffer); | ||
165 | } | ||
166 | return 0; | ||
167 | } | ||
168 | |||
169 | /* | ||
170 | * Note the digest algorithm for the signature. | ||
171 | */ | ||
172 | int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen, | ||
173 | unsigned char tag, | ||
174 | const void *value, size_t vlen) | ||
175 | { | ||
176 | struct pkcs7_parse_context *ctx = context; | ||
177 | |||
178 | switch (ctx->last_oid) { | ||
179 | case OID_md4: | ||
180 | ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4; | ||
181 | break; | ||
182 | case OID_md5: | ||
183 | ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5; | ||
184 | break; | ||
185 | case OID_sha1: | ||
186 | ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1; | ||
187 | break; | ||
188 | case OID_sha256: | ||
189 | ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256; | ||
190 | break; | ||
191 | default: | ||
192 | printk("Unsupported digest algo: %u\n", ctx->last_oid); | ||
193 | return -ENOPKG; | ||
194 | } | ||
195 | return 0; | ||
196 | } | ||
197 | |||
198 | /* | ||
199 | * Note the public key algorithm for the signature. | ||
200 | */ | ||
201 | int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen, | ||
202 | unsigned char tag, | ||
203 | const void *value, size_t vlen) | ||
204 | { | ||
205 | struct pkcs7_parse_context *ctx = context; | ||
206 | |||
207 | switch (ctx->last_oid) { | ||
208 | case OID_rsaEncryption: | ||
209 | ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA; | ||
210 | break; | ||
211 | default: | ||
212 | printk("Unsupported pkey algo: %u\n", ctx->last_oid); | ||
213 | return -ENOPKG; | ||
214 | } | ||
215 | return 0; | ||
216 | } | ||
217 | |||
218 | /* | ||
219 | * Extract a certificate and store it in the context. | ||
220 | */ | ||
221 | int pkcs7_extract_cert(void *context, size_t hdrlen, | ||
222 | unsigned char tag, | ||
223 | const void *value, size_t vlen) | ||
224 | { | ||
225 | struct pkcs7_parse_context *ctx = context; | ||
226 | struct x509_certificate *x509; | ||
227 | |||
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); | ||
231 | return -EBADMSG; | ||
232 | } | ||
233 | |||
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 | ||
237 | * stipulates BER). | ||
238 | */ | ||
239 | value -= hdrlen; | ||
240 | vlen += hdrlen; | ||
241 | |||
242 | if (((u8*)value)[1] == 0x80) | ||
243 | vlen += 2; /* Indefinite length - there should be an EOC */ | ||
244 | |||
245 | x509 = x509_cert_parse(value, vlen); | ||
246 | if (IS_ERR(x509)) | ||
247 | return PTR_ERR(x509); | ||
248 | |||
249 | pr_debug("Got cert for %s\n", x509->subject); | ||
250 | pr_debug("- fingerprint %s\n", x509->fingerprint); | ||
251 | |||
252 | x509->index = ++ctx->x509_index; | ||
253 | *ctx->ppcerts = x509; | ||
254 | ctx->ppcerts = &x509->next; | ||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | /* | ||
259 | * Save the certificate list | ||
260 | */ | ||
261 | int pkcs7_note_certificate_list(void *context, size_t hdrlen, | ||
262 | unsigned char tag, | ||
263 | const void *value, size_t vlen) | ||
264 | { | ||
265 | struct pkcs7_parse_context *ctx = context; | ||
266 | |||
267 | pr_devel("Got cert list (%02x)\n", tag); | ||
268 | |||
269 | *ctx->ppcerts = ctx->msg->certs; | ||
270 | ctx->msg->certs = ctx->certs; | ||
271 | ctx->certs = NULL; | ||
272 | ctx->ppcerts = &ctx->certs; | ||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | /* | ||
277 | * Extract the data from the message and store that and its content type OID in | ||
278 | * the context. | ||
279 | */ | ||
280 | int pkcs7_note_data(void *context, size_t hdrlen, | ||
281 | unsigned char tag, | ||
282 | const void *value, size_t vlen) | ||
283 | { | ||
284 | struct pkcs7_parse_context *ctx = context; | ||
285 | |||
286 | pr_debug("Got data\n"); | ||
287 | |||
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; | ||
292 | return 0; | ||
293 | } | ||
294 | |||
295 | /* | ||
296 | * Parse authenticated attributes | ||
297 | */ | ||
298 | int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen, | ||
299 | unsigned char tag, | ||
300 | const void *value, size_t vlen) | ||
301 | { | ||
302 | struct pkcs7_parse_context *ctx = context; | ||
303 | |||
304 | pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value); | ||
305 | |||
306 | switch (ctx->last_oid) { | ||
307 | case OID_messageDigest: | ||
308 | if (tag != ASN1_OTS) | ||
309 | return -EBADMSG; | ||
310 | ctx->sinfo->msgdigest = value; | ||
311 | ctx->sinfo->msgdigest_len = vlen; | ||
312 | return 0; | ||
313 | default: | ||
314 | return 0; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | /* | ||
319 | * Note the set of auth attributes for digestion purposes [RFC2315 9.3] | ||
320 | */ | ||
321 | int pkcs7_sig_note_set_of_authattrs(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 | |||
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); | ||
330 | return 0; | ||
331 | } | ||
332 | |||
333 | /* | ||
334 | * Note the issuing certificate serial number | ||
335 | */ | ||
336 | int pkcs7_sig_note_serial(void *context, size_t hdrlen, | ||
337 | unsigned char tag, | ||
338 | const void *value, size_t vlen) | ||
339 | { | ||
340 | struct pkcs7_parse_context *ctx = context; | ||
341 | ctx->sinfo->raw_serial = value; | ||
342 | ctx->sinfo->raw_serial_size = vlen; | ||
343 | return 0; | ||
344 | } | ||
345 | |||
346 | /* | ||
347 | * Note the issuer's name | ||
348 | */ | ||
349 | int pkcs7_sig_note_issuer(void *context, size_t hdrlen, | ||
350 | unsigned char tag, | ||
351 | const void *value, size_t vlen) | ||
352 | { | ||
353 | struct pkcs7_parse_context *ctx = context; | ||
354 | ctx->sinfo->raw_issuer = value; | ||
355 | ctx->sinfo->raw_issuer_size = vlen; | ||
356 | return 0; | ||
357 | } | ||
358 | |||
359 | /* | ||
360 | * Note the signature data | ||
361 | */ | ||
362 | int pkcs7_sig_note_signature(void *context, size_t hdrlen, | ||
363 | unsigned char tag, | ||
364 | const void *value, size_t vlen) | ||
365 | { | ||
366 | struct pkcs7_parse_context *ctx = context; | ||
367 | MPI mpi; | ||
368 | |||
369 | BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA); | ||
370 | |||
371 | mpi = mpi_read_raw_data(value, vlen); | ||
372 | if (!mpi) | ||
373 | return -ENOMEM; | ||
374 | |||
375 | ctx->sinfo->sig.mpi[0] = mpi; | ||
376 | ctx->sinfo->sig.nr_mpi = 1; | ||
377 | return 0; | ||
378 | } | ||
379 | |||
380 | /* | ||
381 | * Note a signature information block | ||
382 | */ | ||
383 | int pkcs7_note_signed_info(void *context, size_t hdrlen, | ||
384 | unsigned char tag, | ||
385 | const void *value, size_t vlen) | ||
386 | { | ||
387 | struct pkcs7_parse_context *ctx = context; | ||
388 | |||
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); | ||
393 | if (!ctx->sinfo) | ||
394 | return -ENOMEM; | ||
395 | return 0; | ||
396 | } | ||
diff --git a/crypto/asymmetric_keys/pkcs7_parser.h b/crypto/asymmetric_keys/pkcs7_parser.h new file mode 100644 index 000000000000..d25f4d15370f --- /dev/null +++ b/crypto/asymmetric_keys/pkcs7_parser.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* PKCS#7 crypto data parser internal definitions | ||
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 | #include <linux/oid_registry.h> | ||
13 | #include <crypto/pkcs7.h> | ||
14 | #include "x509_parser.h" | ||
15 | |||
16 | #define kenter(FMT, ...) \ | ||
17 | pr_devel("==> %s("FMT")\n", __func__, ##__VA_ARGS__) | ||
18 | #define kleave(FMT, ...) \ | ||
19 | pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__) | ||
20 | |||
21 | struct pkcs7_signed_info { | ||
22 | struct pkcs7_signed_info *next; | ||
23 | struct x509_certificate *signer; /* Signing certificate (in msg->certs) */ | ||
24 | unsigned index; | ||
25 | bool trusted; | ||
26 | |||
27 | /* Message digest - the digest of the Content Data (or NULL) */ | ||
28 | const void *msgdigest; | ||
29 | unsigned msgdigest_len; | ||
30 | |||
31 | /* Authenticated Attribute data (or NULL) */ | ||
32 | unsigned authattrs_len; | ||
33 | const void *authattrs; | ||
34 | |||
35 | /* Issuing cert serial number and issuer's name */ | ||
36 | const void *raw_serial; | ||
37 | unsigned raw_serial_size; | ||
38 | unsigned raw_issuer_size; | ||
39 | const void *raw_issuer; | ||
40 | |||
41 | /* Message signature. | ||
42 | * | ||
43 | * This contains the generated digest of _either_ the Content Data or | ||
44 | * the Authenticated Attributes [RFC2315 9.3]. If the latter, one of | ||
45 | * the attributes contains the digest of the the Content Data within | ||
46 | * it. | ||
47 | */ | ||
48 | struct public_key_signature sig; | ||
49 | }; | ||
50 | |||
51 | struct pkcs7_message { | ||
52 | struct x509_certificate *certs; /* Certificate list */ | ||
53 | struct x509_certificate *crl; /* Revocation list */ | ||
54 | struct pkcs7_signed_info *signed_infos; | ||
55 | |||
56 | /* Content Data (or NULL) */ | ||
57 | enum OID data_type; /* Type of Data */ | ||
58 | size_t data_len; /* Length of Data */ | ||
59 | size_t data_hdrlen; /* Length of Data ASN.1 header */ | ||
60 | const void *data; /* Content Data (or 0) */ | ||
61 | }; | ||