Blame view

crypto/asymmetric_keys/pkcs7_parser.c 16.3 KB
b4d0d230c   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
2e3fadbf7   David Howells   PKCS#7: Implement...
2
3
4
5
  /* PKCS#7 parser
   *
   * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
2e3fadbf7   David Howells   PKCS#7: Implement...
6
7
8
9
   */
  
  #define pr_fmt(fmt) "PKCS7: "fmt
  #include <linux/kernel.h>
1e684d382   David Howells   pkcs7: Set the mo...
10
  #include <linux/module.h>
2e3fadbf7   David Howells   PKCS#7: Implement...
11
12
13
14
  #include <linux/export.h>
  #include <linux/slab.h>
  #include <linux/err.h>
  #include <linux/oid_registry.h>
db6c43bd2   Tadeusz Struk   crypto: KEYS: con...
15
  #include <crypto/public_key.h>
2e3fadbf7   David Howells   PKCS#7: Implement...
16
  #include "pkcs7_parser.h"
4fa8bc949   Masahiro Yamada   kbuild: rename *-...
17
  #include "pkcs7.asn1.h"
2e3fadbf7   David Howells   PKCS#7: Implement...
18

1e684d382   David Howells   pkcs7: Set the mo...
19
20
21
  MODULE_DESCRIPTION("PKCS#7 parser");
  MODULE_AUTHOR("Red Hat, Inc.");
  MODULE_LICENSE("GPL");
2e3fadbf7   David Howells   PKCS#7: Implement...
22
23
24
25
26
27
28
29
30
31
  struct pkcs7_parse_context {
  	struct pkcs7_message	*msg;		/* Message being constructed */
  	struct pkcs7_signed_info *sinfo;	/* SignedInfo being constructed */
  	struct pkcs7_signed_info **ppsinfo;
  	struct x509_certificate *certs;		/* Certificate cache */
  	struct x509_certificate **ppcerts;
  	unsigned long	data;			/* Start of data */
  	enum OID	last_oid;		/* Last OID encountered */
  	unsigned	x509_index;
  	unsigned	sinfo_index;
46963b774   David Howells   KEYS: Overhaul ke...
32
33
34
35
  	const void	*raw_serial;
  	unsigned	raw_serial_size;
  	unsigned	raw_issuer_size;
  	const void	*raw_issuer;
60d65cacd   David Howells   PKCS#7: Support C...
36
37
38
  	const void	*raw_skid;
  	unsigned	raw_skid_size;
  	bool		expect_skid;
2e3fadbf7   David Howells   PKCS#7: Implement...
39
  };
3cd0920cd   David Howells   PKCS#7: Provide a...
40
41
42
43
44
45
  /*
   * Free a signed information block.
   */
  static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
  {
  	if (sinfo) {
566a117a8   David Howells   PKCS#7: Make the ...
46
  		public_key_signature_free(sinfo->sig);
3cd0920cd   David Howells   PKCS#7: Provide a...
47
48
49
  		kfree(sinfo);
  	}
  }
2e3fadbf7   David Howells   PKCS#7: Implement...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /**
   * pkcs7_free_message - Free a PKCS#7 message
   * @pkcs7: The PKCS#7 message to free
   */
  void pkcs7_free_message(struct pkcs7_message *pkcs7)
  {
  	struct x509_certificate *cert;
  	struct pkcs7_signed_info *sinfo;
  
  	if (pkcs7) {
  		while (pkcs7->certs) {
  			cert = pkcs7->certs;
  			pkcs7->certs = cert->next;
  			x509_free_certificate(cert);
  		}
  		while (pkcs7->crl) {
  			cert = pkcs7->crl;
  			pkcs7->crl = cert->next;
  			x509_free_certificate(cert);
  		}
  		while (pkcs7->signed_infos) {
  			sinfo = pkcs7->signed_infos;
  			pkcs7->signed_infos = sinfo->next;
3cd0920cd   David Howells   PKCS#7: Provide a...
73
  			pkcs7_free_signed_info(sinfo);
2e3fadbf7   David Howells   PKCS#7: Implement...
74
75
76
77
78
  		}
  		kfree(pkcs7);
  	}
  }
  EXPORT_SYMBOL_GPL(pkcs7_free_message);
99db44350   David Howells   PKCS#7: Appropria...
79
80
81
82
83
84
  /*
   * Check authenticatedAttributes are provided or not provided consistently.
   */
  static int pkcs7_check_authattrs(struct pkcs7_message *msg)
  {
  	struct pkcs7_signed_info *sinfo;
06aae5924   Colin Ian King   PKCS#7: fix uniti...
85
  	bool want = false;
99db44350   David Howells   PKCS#7: Appropria...
86
87
  
  	sinfo = msg->signed_infos;
68a1fdbbf   Eric Sesterhenn   pkcs7: Prevent NU...
88
89
  	if (!sinfo)
  		goto inconsistent;
99db44350   David Howells   PKCS#7: Appropria...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  	if (sinfo->authattrs) {
  		want = true;
  		msg->have_authattrs = true;
  	}
  
  	for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
  		if (!!sinfo->authattrs != want)
  			goto inconsistent;
  	return 0;
  
  inconsistent:
  	pr_warn("Inconsistently supplied authAttrs
  ");
  	return -EINVAL;
  }
2e3fadbf7   David Howells   PKCS#7: Implement...
105
106
107
108
109
110
111
112
  /**
   * pkcs7_parse_message - Parse a PKCS#7 message
   * @data: The raw binary ASN.1 encoded message to be parsed
   * @datalen: The size of the encoded message
   */
  struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
  {
  	struct pkcs7_parse_context *ctx;
cecf5d2e1   David Howells   PKCS#7: Fix the p...
113
114
  	struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
  	int ret;
2e3fadbf7   David Howells   PKCS#7: Implement...
115

2e3fadbf7   David Howells   PKCS#7: Implement...
116
117
  	ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
  	if (!ctx)
cecf5d2e1   David Howells   PKCS#7: Fix the p...
118
119
120
121
  		goto out_no_ctx;
  	ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
  	if (!ctx->msg)
  		goto out_no_msg;
2e3fadbf7   David Howells   PKCS#7: Implement...
122
123
  	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  	if (!ctx->sinfo)
cecf5d2e1   David Howells   PKCS#7: Fix the p...
124
  		goto out_no_sinfo;
566a117a8   David Howells   PKCS#7: Make the ...
125
126
127
128
  	ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
  				  GFP_KERNEL);
  	if (!ctx->sinfo->sig)
  		goto out_no_sig;
2e3fadbf7   David Howells   PKCS#7: Implement...
129

2e3fadbf7   David Howells   PKCS#7: Implement...
130
131
132
133
134
135
  	ctx->data = (unsigned long)data;
  	ctx->ppcerts = &ctx->certs;
  	ctx->ppsinfo = &ctx->msg->signed_infos;
  
  	/* Attempt to decode the signature */
  	ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
cecf5d2e1   David Howells   PKCS#7: Fix the p...
136
137
138
139
  	if (ret < 0) {
  		msg = ERR_PTR(ret);
  		goto out;
  	}
99db44350   David Howells   PKCS#7: Appropria...
140
  	ret = pkcs7_check_authattrs(ctx->msg);
8ecb506d3   Eric Biggers   pkcs7: return cor...
141
142
  	if (ret < 0) {
  		msg = ERR_PTR(ret);
99db44350   David Howells   PKCS#7: Appropria...
143
  		goto out;
8ecb506d3   Eric Biggers   pkcs7: return cor...
144
  	}
99db44350   David Howells   PKCS#7: Appropria...
145

cecf5d2e1   David Howells   PKCS#7: Fix the p...
146
147
  	msg = ctx->msg;
  	ctx->msg = NULL;
2e3fadbf7   David Howells   PKCS#7: Implement...
148

cecf5d2e1   David Howells   PKCS#7: Fix the p...
149
  out:
2e3fadbf7   David Howells   PKCS#7: Implement...
150
151
152
153
154
  	while (ctx->certs) {
  		struct x509_certificate *cert = ctx->certs;
  		ctx->certs = cert->next;
  		x509_free_certificate(cert);
  	}
566a117a8   David Howells   PKCS#7: Make the ...
155
  out_no_sig:
3cd0920cd   David Howells   PKCS#7: Provide a...
156
  	pkcs7_free_signed_info(ctx->sinfo);
cecf5d2e1   David Howells   PKCS#7: Fix the p...
157
158
159
  out_no_sinfo:
  	pkcs7_free_message(ctx->msg);
  out_no_msg:
2e3fadbf7   David Howells   PKCS#7: Implement...
160
  	kfree(ctx);
cecf5d2e1   David Howells   PKCS#7: Fix the p...
161
  out_no_ctx:
2e3fadbf7   David Howells   PKCS#7: Implement...
162
  	return msg;
2e3fadbf7   David Howells   PKCS#7: Implement...
163
164
165
166
167
168
169
170
  }
  EXPORT_SYMBOL_GPL(pkcs7_parse_message);
  
  /**
   * pkcs7_get_content_data - Get access to the PKCS#7 content
   * @pkcs7: The preparsed PKCS#7 message to access
   * @_data: Place to return a pointer to the data
   * @_data_len: Place to return the data length
e68503bd6   David Howells   KEYS: Generalise ...
171
   * @_headerlen: Size of ASN.1 header not included in _data
2e3fadbf7   David Howells   PKCS#7: Implement...
172
   *
e68503bd6   David Howells   KEYS: Generalise ...
173
174
175
176
177
   * Get access to the data content of the PKCS#7 message.  The size of the
   * header of the ASN.1 object that contains it is also provided and can be used
   * to adjust *_data and *_data_len to get the entire object.
   *
   * Returns -ENODATA if the data object was missing from the message.
2e3fadbf7   David Howells   PKCS#7: Implement...
178
179
180
   */
  int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
  			   const void **_data, size_t *_data_len,
e68503bd6   David Howells   KEYS: Generalise ...
181
  			   size_t *_headerlen)
2e3fadbf7   David Howells   PKCS#7: Implement...
182
  {
2e3fadbf7   David Howells   PKCS#7: Implement...
183
184
  	if (!pkcs7->data)
  		return -ENODATA;
e68503bd6   David Howells   KEYS: Generalise ...
185
186
187
188
  	*_data = pkcs7->data;
  	*_data_len = pkcs7->data_len;
  	if (_headerlen)
  		*_headerlen = pkcs7->data_hdrlen;
2e3fadbf7   David Howells   PKCS#7: Implement...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  	return 0;
  }
  EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
  
  /*
   * Note an OID when we find one for later processing when we know how
   * to interpret it.
   */
  int pkcs7_note_OID(void *context, size_t hdrlen,
  		   unsigned char tag,
  		   const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	ctx->last_oid = look_up_OID(value, vlen);
  	if (ctx->last_oid == OID__NR) {
  		char buffer[50];
  		sprint_oid(value, vlen, buffer, sizeof(buffer));
  		printk("PKCS7: Unknown OID: [%lu] %s
  ",
  		       (unsigned long)value - ctx->data, buffer);
  	}
  	return 0;
  }
  
  /*
   * Note the digest algorithm for the signature.
   */
  int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
  			       unsigned char tag,
  			       const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	switch (ctx->last_oid) {
  	case OID_md4:
566a117a8   David Howells   PKCS#7: Make the ...
225
  		ctx->sinfo->sig->hash_algo = "md4";
2e3fadbf7   David Howells   PKCS#7: Implement...
226
227
  		break;
  	case OID_md5:
566a117a8   David Howells   PKCS#7: Make the ...
228
  		ctx->sinfo->sig->hash_algo = "md5";
2e3fadbf7   David Howells   PKCS#7: Implement...
229
230
  		break;
  	case OID_sha1:
566a117a8   David Howells   PKCS#7: Make the ...
231
  		ctx->sinfo->sig->hash_algo = "sha1";
2e3fadbf7   David Howells   PKCS#7: Implement...
232
233
  		break;
  	case OID_sha256:
566a117a8   David Howells   PKCS#7: Make the ...
234
  		ctx->sinfo->sig->hash_algo = "sha256";
2e3fadbf7   David Howells   PKCS#7: Implement...
235
  		break;
07f081fb5   David Howells   PKCS#7: Add OIDs ...
236
  	case OID_sha384:
566a117a8   David Howells   PKCS#7: Make the ...
237
  		ctx->sinfo->sig->hash_algo = "sha384";
07f081fb5   David Howells   PKCS#7: Add OIDs ...
238
239
  		break;
  	case OID_sha512:
566a117a8   David Howells   PKCS#7: Make the ...
240
  		ctx->sinfo->sig->hash_algo = "sha512";
07f081fb5   David Howells   PKCS#7: Add OIDs ...
241
242
  		break;
  	case OID_sha224:
566a117a8   David Howells   PKCS#7: Make the ...
243
244
  		ctx->sinfo->sig->hash_algo = "sha224";
  		break;
2e3fadbf7   David Howells   PKCS#7: Implement...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  	default:
  		printk("Unsupported digest algo: %u
  ", ctx->last_oid);
  		return -ENOPKG;
  	}
  	return 0;
  }
  
  /*
   * Note the public key algorithm for the signature.
   */
  int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
  			     unsigned char tag,
  			     const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	switch (ctx->last_oid) {
  	case OID_rsaEncryption:
566a117a8   David Howells   PKCS#7: Make the ...
264
  		ctx->sinfo->sig->pkey_algo = "rsa";
039884907   David Howells   KEYS: Make the X....
265
  		ctx->sinfo->sig->encoding = "pkcs1";
2e3fadbf7   David Howells   PKCS#7: Implement...
266
267
268
269
270
271
272
273
274
275
  		break;
  	default:
  		printk("Unsupported pkey algo: %u
  ", ctx->last_oid);
  		return -ENOPKG;
  	}
  	return 0;
  }
  
  /*
2c7fd3675   David Howells   PKCS#7: Check con...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
   * We only support signed data [RFC2315 sec 9].
   */
  int pkcs7_check_content_type(void *context, size_t hdrlen,
  			     unsigned char tag,
  			     const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	if (ctx->last_oid != OID_signed_data) {
  		pr_warn("Only support pkcs7_signedData type
  ");
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  /*
   * Note the SignedData version
   */
  int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
  				  unsigned char tag,
  				  const void *value, size_t vlen)
  {
60d65cacd   David Howells   PKCS#7: Support C...
300
  	struct pkcs7_parse_context *ctx = context;
2c7fd3675   David Howells   PKCS#7: Check con...
301
302
303
304
  	unsigned version;
  
  	if (vlen != 1)
  		goto unsupported;
60d65cacd   David Howells   PKCS#7: Support C...
305
  	ctx->msg->version = version = *(const u8 *)value;
2c7fd3675   David Howells   PKCS#7: Check con...
306
307
  	switch (version) {
  	case 1:
60d65cacd   David Howells   PKCS#7: Support C...
308
309
310
311
312
313
  		/* PKCS#7 SignedData [RFC2315 sec 9.1]
  		 * CMS ver 1 SignedData [RFC5652 sec 5.1]
  		 */
  		break;
  	case 3:
  		/* CMS ver 3 SignedData [RFC2315 sec 5.1] */
2c7fd3675   David Howells   PKCS#7: Check con...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  		break;
  	default:
  		goto unsupported;
  	}
  
  	return 0;
  
  unsupported:
  	pr_warn("Unsupported SignedData version
  ");
  	return -EINVAL;
  }
  
  /*
   * Note the SignerInfo version
   */
  int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
  				  unsigned char tag,
  				  const void *value, size_t vlen)
  {
60d65cacd   David Howells   PKCS#7: Support C...
334
  	struct pkcs7_parse_context *ctx = context;
2c7fd3675   David Howells   PKCS#7: Check con...
335
336
337
338
339
340
341
342
  	unsigned version;
  
  	if (vlen != 1)
  		goto unsupported;
  
  	version = *(const u8 *)value;
  	switch (version) {
  	case 1:
60d65cacd   David Howells   PKCS#7: Support C...
343
344
345
346
347
348
349
350
351
352
353
354
  		/* PKCS#7 SignerInfo [RFC2315 sec 9.2]
  		 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
  		 */
  		if (ctx->msg->version != 1)
  			goto version_mismatch;
  		ctx->expect_skid = false;
  		break;
  	case 3:
  		/* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
  		if (ctx->msg->version == 1)
  			goto version_mismatch;
  		ctx->expect_skid = true;
2c7fd3675   David Howells   PKCS#7: Check con...
355
356
357
358
359
360
361
362
363
364
365
  		break;
  	default:
  		goto unsupported;
  	}
  
  	return 0;
  
  unsupported:
  	pr_warn("Unsupported SignerInfo version
  ");
  	return -EINVAL;
60d65cacd   David Howells   PKCS#7: Support C...
366
367
368
369
  version_mismatch:
  	pr_warn("SignedData-SignerInfo version mismatch
  ");
  	return -EBADMSG;
2c7fd3675   David Howells   PKCS#7: Check con...
370
371
372
  }
  
  /*
2e3fadbf7   David Howells   PKCS#7: Implement...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
   * Extract a certificate and store it in the context.
   */
  int pkcs7_extract_cert(void *context, size_t hdrlen,
  		       unsigned char tag,
  		       const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  	struct x509_certificate *x509;
  
  	if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
  		pr_debug("Cert began with tag %02x at %lu
  ",
  			 tag, (unsigned long)ctx - ctx->data);
  		return -EBADMSG;
  	}
  
  	/* We have to correct for the header so that the X.509 parser can start
  	 * from the beginning.  Note that since X.509 stipulates DER, there
  	 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
  	 * stipulates BER).
  	 */
  	value -= hdrlen;
  	vlen += hdrlen;
  
  	if (((u8*)value)[1] == 0x80)
  		vlen += 2; /* Indefinite length - there should be an EOC */
  
  	x509 = x509_cert_parse(value, vlen);
  	if (IS_ERR(x509))
  		return PTR_ERR(x509);
2e3fadbf7   David Howells   PKCS#7: Implement...
403
  	x509->index = ++ctx->x509_index;
46963b774   David Howells   KEYS: Overhaul ke...
404
405
406
407
  	pr_debug("Got cert %u for %s
  ", x509->index, x509->subject);
  	pr_debug("- fingerprint %*phN
  ", x509->id->len, x509->id->data);
2e3fadbf7   David Howells   PKCS#7: Implement...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  	*ctx->ppcerts = x509;
  	ctx->ppcerts = &x509->next;
  	return 0;
  }
  
  /*
   * Save the certificate list
   */
  int pkcs7_note_certificate_list(void *context, size_t hdrlen,
  				unsigned char tag,
  				const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	pr_devel("Got cert list (%02x)
  ", tag);
  
  	*ctx->ppcerts = ctx->msg->certs;
  	ctx->msg->certs = ctx->certs;
  	ctx->certs = NULL;
  	ctx->ppcerts = &ctx->certs;
  	return 0;
  }
  
  /*
99db44350   David Howells   PKCS#7: Appropria...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
   * Note the content type.
   */
  int pkcs7_note_content(void *context, size_t hdrlen,
  		       unsigned char tag,
  		       const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	if (ctx->last_oid != OID_data &&
  	    ctx->last_oid != OID_msIndirectData) {
  		pr_warn("Unsupported data type %d
  ", ctx->last_oid);
  		return -EINVAL;
  	}
  
  	ctx->msg->data_type = ctx->last_oid;
  	return 0;
  }
  
  /*
2e3fadbf7   David Howells   PKCS#7: Implement...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
   * Extract the data from the message and store that and its content type OID in
   * the context.
   */
  int pkcs7_note_data(void *context, size_t hdrlen,
  		    unsigned char tag,
  		    const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	pr_debug("Got data
  ");
  
  	ctx->msg->data = value;
  	ctx->msg->data_len = vlen;
  	ctx->msg->data_hdrlen = hdrlen;
2e3fadbf7   David Howells   PKCS#7: Implement...
468
469
470
471
  	return 0;
  }
  
  /*
99db44350   David Howells   PKCS#7: Appropria...
472
   * Parse authenticated attributes.
2e3fadbf7   David Howells   PKCS#7: Implement...
473
474
475
476
477
478
   */
  int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
  				      unsigned char tag,
  				      const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
99db44350   David Howells   PKCS#7: Appropria...
479
480
  	struct pkcs7_signed_info *sinfo = ctx->sinfo;
  	enum OID content_type;
2e3fadbf7   David Howells   PKCS#7: Implement...
481
482
483
484
485
  
  	pr_devel("AuthAttr: %02x %zu [%*ph]
  ", tag, vlen, (unsigned)vlen, value);
  
  	switch (ctx->last_oid) {
99db44350   David Howells   PKCS#7: Appropria...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  	case OID_contentType:
  		if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
  			goto repeated;
  		content_type = look_up_OID(value, vlen);
  		if (content_type != ctx->msg->data_type) {
  			pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)
  ",
  				ctx->msg->data_type, sinfo->index,
  				content_type);
  			return -EBADMSG;
  		}
  		return 0;
  
  	case OID_signingTime:
  		if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
  			goto repeated;
  		/* Should we check that the signing time is consistent
  		 * with the signer's X.509 cert?
  		 */
  		return x509_decode_time(&sinfo->signing_time,
  					hdrlen, tag, value, vlen);
2e3fadbf7   David Howells   PKCS#7: Implement...
507
  	case OID_messageDigest:
99db44350   David Howells   PKCS#7: Appropria...
508
509
  		if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
  			goto repeated;
2e3fadbf7   David Howells   PKCS#7: Implement...
510
511
  		if (tag != ASN1_OTS)
  			return -EBADMSG;
99db44350   David Howells   PKCS#7: Appropria...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  		sinfo->msgdigest = value;
  		sinfo->msgdigest_len = vlen;
  		return 0;
  
  	case OID_smimeCapabilites:
  		if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
  			goto repeated;
  		if (ctx->msg->data_type != OID_msIndirectData) {
  			pr_warn("S/MIME Caps only allowed with Authenticode
  ");
  			return -EKEYREJECTED;
  		}
  		return 0;
  
  		/* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
  		 * char URLs and cont[1] 8-bit char URLs.
  		 *
  		 * Microsoft StatementType seems to contain a list of OIDs that
  		 * are also used as extendedKeyUsage types in X.509 certs.
  		 */
  	case OID_msSpOpusInfo:
  		if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
  			goto repeated;
  		goto authenticode_check;
  	case OID_msStatementType:
  		if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
  			goto repeated;
  	authenticode_check:
  		if (ctx->msg->data_type != OID_msIndirectData) {
  			pr_warn("Authenticode AuthAttrs only allowed with Authenticode
  ");
  			return -EKEYREJECTED;
  		}
  		/* I'm not sure how to validate these */
2e3fadbf7   David Howells   PKCS#7: Implement...
546
547
548
549
  		return 0;
  	default:
  		return 0;
  	}
99db44350   David Howells   PKCS#7: Appropria...
550
551
552
553
554
555
  
  repeated:
  	/* We permit max one item per AuthenticatedAttribute and no repeats */
  	pr_warn("Repeated/multivalue AuthAttrs not permitted
  ");
  	return -EKEYREJECTED;
2e3fadbf7   David Howells   PKCS#7: Implement...
556
557
558
  }
  
  /*
2c7fd3675   David Howells   PKCS#7: Check con...
559
   * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
2e3fadbf7   David Howells   PKCS#7: Implement...
560
561
562
563
564
565
   */
  int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
  				    unsigned char tag,
  				    const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
99db44350   David Howells   PKCS#7: Appropria...
566
567
568
  	struct pkcs7_signed_info *sinfo = ctx->sinfo;
  
  	if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
7ee7014d0   Peter Jones   PKCS#7: Don't req...
569
  	    !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
99db44350   David Howells   PKCS#7: Appropria...
570
571
572
573
574
575
576
577
578
579
580
  		pr_warn("Missing required AuthAttr
  ");
  		return -EBADMSG;
  	}
  
  	if (ctx->msg->data_type != OID_msIndirectData &&
  	    test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
  		pr_warn("Unexpected Authenticode AuthAttr
  ");
  		return -EBADMSG;
  	}
2e3fadbf7   David Howells   PKCS#7: Implement...
581
582
  
  	/* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
99db44350   David Howells   PKCS#7: Appropria...
583
584
  	sinfo->authattrs = value - (hdrlen - 1);
  	sinfo->authattrs_len = vlen + (hdrlen - 1);
2e3fadbf7   David Howells   PKCS#7: Implement...
585
586
587
588
589
590
591
592
593
594
595
  	return 0;
  }
  
  /*
   * Note the issuing certificate serial number
   */
  int pkcs7_sig_note_serial(void *context, size_t hdrlen,
  			  unsigned char tag,
  			  const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
46963b774   David Howells   KEYS: Overhaul ke...
596
597
  	ctx->raw_serial = value;
  	ctx->raw_serial_size = vlen;
2e3fadbf7   David Howells   PKCS#7: Implement...
598
599
600
601
602
603
604
605
606
607
608
  	return 0;
  }
  
  /*
   * Note the issuer's name
   */
  int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
  			  unsigned char tag,
  			  const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
46963b774   David Howells   KEYS: Overhaul ke...
609
610
  	ctx->raw_issuer = value;
  	ctx->raw_issuer_size = vlen;
2e3fadbf7   David Howells   PKCS#7: Implement...
611
612
613
614
  	return 0;
  }
  
  /*
60d65cacd   David Howells   PKCS#7: Support C...
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
   * Note the issuing cert's subjectKeyIdentifier
   */
  int pkcs7_sig_note_skid(void *context, size_t hdrlen,
  			unsigned char tag,
  			const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
  
  	pr_devel("SKID: %02x %zu [%*ph]
  ", tag, vlen, (unsigned)vlen, value);
  
  	ctx->raw_skid = value;
  	ctx->raw_skid_size = vlen;
  	return 0;
  }
  
  /*
2e3fadbf7   David Howells   PKCS#7: Implement...
632
633
634
635
636
637
638
   * Note the signature data
   */
  int pkcs7_sig_note_signature(void *context, size_t hdrlen,
  			     unsigned char tag,
  			     const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
2e3fadbf7   David Howells   PKCS#7: Implement...
639

566a117a8   David Howells   PKCS#7: Make the ...
640
641
  	ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
  	if (!ctx->sinfo->sig->s)
2e3fadbf7   David Howells   PKCS#7: Implement...
642
  		return -ENOMEM;
566a117a8   David Howells   PKCS#7: Make the ...
643
  	ctx->sinfo->sig->s_size = vlen;
2e3fadbf7   David Howells   PKCS#7: Implement...
644
645
646
647
648
649
650
651
652
653
654
  	return 0;
  }
  
  /*
   * Note a signature information block
   */
  int pkcs7_note_signed_info(void *context, size_t hdrlen,
  			   unsigned char tag,
  			   const void *value, size_t vlen)
  {
  	struct pkcs7_parse_context *ctx = context;
46963b774   David Howells   KEYS: Overhaul ke...
655
656
  	struct pkcs7_signed_info *sinfo = ctx->sinfo;
  	struct asymmetric_key_id *kid;
99db44350   David Howells   PKCS#7: Appropria...
657
658
659
660
661
  	if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
  		pr_warn("Authenticode requires AuthAttrs
  ");
  		return -EBADMSG;
  	}
46963b774   David Howells   KEYS: Overhaul ke...
662
  	/* Generate cert issuer + serial number key ID */
60d65cacd   David Howells   PKCS#7: Support C...
663
664
665
666
667
668
669
670
671
672
  	if (!ctx->expect_skid) {
  		kid = asymmetric_key_generate_id(ctx->raw_serial,
  						 ctx->raw_serial_size,
  						 ctx->raw_issuer,
  						 ctx->raw_issuer_size);
  	} else {
  		kid = asymmetric_key_generate_id(ctx->raw_skid,
  						 ctx->raw_skid_size,
  						 "", 0);
  	}
46963b774   David Howells   KEYS: Overhaul ke...
673
674
  	if (IS_ERR(kid))
  		return PTR_ERR(kid);
60d65cacd   David Howells   PKCS#7: Support C...
675
676
  	pr_devel("SINFO KID: %u [%*phN]
  ", kid->len, kid->len, kid->data);
566a117a8   David Howells   PKCS#7: Make the ...
677
  	sinfo->sig->auth_ids[0] = kid;
46963b774   David Howells   KEYS: Overhaul ke...
678
679
680
  	sinfo->index = ++ctx->sinfo_index;
  	*ctx->ppsinfo = sinfo;
  	ctx->ppsinfo = &sinfo->next;
2e3fadbf7   David Howells   PKCS#7: Implement...
681
682
683
  	ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  	if (!ctx->sinfo)
  		return -ENOMEM;
566a117a8   David Howells   PKCS#7: Make the ...
684
685
686
687
  	ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
  				  GFP_KERNEL);
  	if (!ctx->sinfo->sig)
  		return -ENOMEM;
2e3fadbf7   David Howells   PKCS#7: Implement...
688
689
  	return 0;
  }