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