diff options
| author | cvs2svn <admin@example.com> | 2015-03-08 16:48:48 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2015-03-08 16:48:48 +0000 |
| commit | da1a9ad3a4a867ba6569c05e6fca66d7f296c553 (patch) | |
| tree | 44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/pkcs7/pk7_doit.c | |
| parent | 973703db67a8e73d70e63afa8f2cde19da09144d (diff) | |
| download | openbsd-OPENBSD_5_7_BASE.tar.gz openbsd-OPENBSD_5_7_BASE.tar.bz2 openbsd-OPENBSD_5_7_BASE.zip | |
This commit was manufactured by cvs2git to create tag 'OPENBSD_5_7_BASE'.OPENBSD_5_7_BASE
Diffstat (limited to 'src/lib/libcrypto/pkcs7/pk7_doit.c')
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_doit.c | 1217 |
1 files changed, 0 insertions, 1217 deletions
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c deleted file mode 100644 index 252fab04d7..0000000000 --- a/src/lib/libcrypto/pkcs7/pk7_doit.c +++ /dev/null | |||
| @@ -1,1217 +0,0 @@ | |||
| 1 | /* $OpenBSD: pk7_doit.c,v 1.31 2015/02/07 13:19:15 doug Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <stdlib.h> | ||
| 61 | #include <string.h> | ||
| 62 | |||
| 63 | #include <openssl/err.h> | ||
| 64 | #include <openssl/objects.h> | ||
| 65 | #include <openssl/x509.h> | ||
| 66 | #include <openssl/x509v3.h> | ||
| 67 | |||
| 68 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 69 | void *value); | ||
| 70 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid); | ||
| 71 | |||
| 72 | static int | ||
| 73 | PKCS7_type_is_other(PKCS7* p7) | ||
| 74 | { | ||
| 75 | int isOther = 1; | ||
| 76 | |||
| 77 | int nid = OBJ_obj2nid(p7->type); | ||
| 78 | |||
| 79 | switch (nid ) { | ||
| 80 | case NID_pkcs7_data: | ||
| 81 | case NID_pkcs7_signed: | ||
| 82 | case NID_pkcs7_enveloped: | ||
| 83 | case NID_pkcs7_signedAndEnveloped: | ||
| 84 | case NID_pkcs7_digest: | ||
| 85 | case NID_pkcs7_encrypted: | ||
| 86 | isOther = 0; | ||
| 87 | break; | ||
| 88 | default: | ||
| 89 | isOther = 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | return isOther; | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | static ASN1_OCTET_STRING * | ||
| 97 | PKCS7_get_octet_string(PKCS7 *p7) | ||
| 98 | { | ||
| 99 | if (PKCS7_type_is_data(p7)) | ||
| 100 | return p7->d.data; | ||
| 101 | if (PKCS7_type_is_other(p7) && p7->d.other && | ||
| 102 | (p7->d.other->type == V_ASN1_OCTET_STRING)) | ||
| 103 | return p7->d.other->value.octet_string; | ||
| 104 | return NULL; | ||
| 105 | } | ||
| 106 | |||
| 107 | static int | ||
| 108 | PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg) | ||
| 109 | { | ||
| 110 | BIO *btmp; | ||
| 111 | const EVP_MD *md; | ||
| 112 | if ((btmp = BIO_new(BIO_f_md())) == NULL) { | ||
| 113 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB); | ||
| 114 | goto err; | ||
| 115 | } | ||
| 116 | |||
| 117 | md = EVP_get_digestbyobj(alg->algorithm); | ||
| 118 | if (md == NULL) { | ||
| 119 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, | ||
| 120 | PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 121 | goto err; | ||
| 122 | } | ||
| 123 | |||
| 124 | BIO_set_md(btmp, md); | ||
| 125 | if (*pbio == NULL) | ||
| 126 | *pbio = btmp; | ||
| 127 | else if (!BIO_push(*pbio, btmp)) { | ||
| 128 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB); | ||
| 129 | goto err; | ||
| 130 | } | ||
| 131 | btmp = NULL; | ||
| 132 | |||
| 133 | return 1; | ||
| 134 | |||
| 135 | err: | ||
| 136 | BIO_free(btmp); | ||
| 137 | return 0; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | static int | ||
| 142 | pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, unsigned char *key, int keylen) | ||
| 143 | { | ||
| 144 | EVP_PKEY_CTX *pctx = NULL; | ||
| 145 | EVP_PKEY *pkey = NULL; | ||
| 146 | unsigned char *ek = NULL; | ||
| 147 | int ret = 0; | ||
| 148 | size_t eklen; | ||
| 149 | |||
| 150 | pkey = X509_get_pubkey(ri->cert); | ||
| 151 | if (!pkey) | ||
| 152 | return 0; | ||
| 153 | |||
| 154 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
| 155 | if (!pctx) | ||
| 156 | return 0; | ||
| 157 | |||
| 158 | if (EVP_PKEY_encrypt_init(pctx) <= 0) | ||
| 159 | goto err; | ||
| 160 | |||
| 161 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT, | ||
| 162 | EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) { | ||
| 163 | PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR); | ||
| 164 | goto err; | ||
| 165 | } | ||
| 166 | |||
| 167 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0) | ||
| 168 | goto err; | ||
| 169 | |||
| 170 | ek = malloc(eklen); | ||
| 171 | |||
| 172 | if (ek == NULL) { | ||
| 173 | PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE); | ||
| 174 | goto err; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0) | ||
| 178 | goto err; | ||
| 179 | |||
| 180 | ASN1_STRING_set0(ri->enc_key, ek, eklen); | ||
| 181 | ek = NULL; | ||
| 182 | |||
| 183 | ret = 1; | ||
| 184 | |||
| 185 | err: | ||
| 186 | EVP_PKEY_free(pkey); | ||
| 187 | EVP_PKEY_CTX_free(pctx); | ||
| 188 | free(ek); | ||
| 189 | return ret; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | static int | ||
| 194 | pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen, PKCS7_RECIP_INFO *ri, | ||
| 195 | EVP_PKEY *pkey) | ||
| 196 | { | ||
| 197 | EVP_PKEY_CTX *pctx = NULL; | ||
| 198 | unsigned char *ek = NULL; | ||
| 199 | size_t eklen; | ||
| 200 | |||
| 201 | int ret = -1; | ||
| 202 | |||
| 203 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
| 204 | if (!pctx) | ||
| 205 | return -1; | ||
| 206 | |||
| 207 | if (EVP_PKEY_decrypt_init(pctx) <= 0) | ||
| 208 | goto err; | ||
| 209 | |||
| 210 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT, | ||
| 211 | EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) { | ||
| 212 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR); | ||
| 213 | goto err; | ||
| 214 | } | ||
| 215 | |||
| 216 | if (EVP_PKEY_decrypt(pctx, NULL, &eklen, | ||
| 217 | ri->enc_key->data, ri->enc_key->length) <= 0) | ||
| 218 | goto err; | ||
| 219 | |||
| 220 | ek = malloc(eklen); | ||
| 221 | if (ek == NULL) { | ||
| 222 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE); | ||
| 223 | goto err; | ||
| 224 | } | ||
| 225 | |||
| 226 | if (EVP_PKEY_decrypt(pctx, ek, &eklen, | ||
| 227 | ri->enc_key->data, ri->enc_key->length) <= 0) { | ||
| 228 | ret = 0; | ||
| 229 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB); | ||
| 230 | goto err; | ||
| 231 | } | ||
| 232 | |||
| 233 | ret = 1; | ||
| 234 | |||
| 235 | if (*pek) { | ||
| 236 | OPENSSL_cleanse(*pek, *peklen); | ||
| 237 | free(*pek); | ||
| 238 | } | ||
| 239 | |||
| 240 | *pek = ek; | ||
| 241 | *peklen = eklen; | ||
| 242 | |||
| 243 | err: | ||
| 244 | EVP_PKEY_CTX_free(pctx); | ||
| 245 | if (!ret && ek) | ||
| 246 | free(ek); | ||
| 247 | |||
| 248 | return ret; | ||
| 249 | } | ||
| 250 | |||
| 251 | BIO * | ||
| 252 | PKCS7_dataInit(PKCS7 *p7, BIO *bio) | ||
| 253 | { | ||
| 254 | int i; | ||
| 255 | BIO *out = NULL, *btmp = NULL; | ||
| 256 | X509_ALGOR *xa = NULL; | ||
| 257 | const EVP_CIPHER *evp_cipher = NULL; | ||
| 258 | STACK_OF(X509_ALGOR) *md_sk = NULL; | ||
| 259 | STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; | ||
| 260 | X509_ALGOR *xalg = NULL; | ||
| 261 | PKCS7_RECIP_INFO *ri = NULL; | ||
| 262 | ASN1_OCTET_STRING *os = NULL; | ||
| 263 | |||
| 264 | i = OBJ_obj2nid(p7->type); | ||
| 265 | p7->state = PKCS7_S_HEADER; | ||
| 266 | |||
| 267 | switch (i) { | ||
| 268 | case NID_pkcs7_signed: | ||
| 269 | md_sk = p7->d.sign->md_algs; | ||
| 270 | os = PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 271 | break; | ||
| 272 | case NID_pkcs7_signedAndEnveloped: | ||
| 273 | rsk = p7->d.signed_and_enveloped->recipientinfo; | ||
| 274 | md_sk = p7->d.signed_and_enveloped->md_algs; | ||
| 275 | xalg = p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 276 | evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher; | ||
| 277 | if (evp_cipher == NULL) { | ||
| 278 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 279 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 280 | goto err; | ||
| 281 | } | ||
| 282 | break; | ||
| 283 | case NID_pkcs7_enveloped: | ||
| 284 | rsk = p7->d.enveloped->recipientinfo; | ||
| 285 | xalg = p7->d.enveloped->enc_data->algorithm; | ||
| 286 | evp_cipher = p7->d.enveloped->enc_data->cipher; | ||
| 287 | if (evp_cipher == NULL) { | ||
| 288 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 289 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 290 | goto err; | ||
| 291 | } | ||
| 292 | break; | ||
| 293 | case NID_pkcs7_digest: | ||
| 294 | xa = p7->d.digest->md; | ||
| 295 | os = PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 296 | break; | ||
| 297 | case NID_pkcs7_data: | ||
| 298 | break; | ||
| 299 | default: | ||
| 300 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 301 | PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 302 | goto err; | ||
| 303 | } | ||
| 304 | |||
| 305 | for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) | ||
| 306 | if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) | ||
| 307 | goto err; | ||
| 308 | |||
| 309 | if (xa && !PKCS7_bio_add_digest(&out, xa)) | ||
| 310 | goto err; | ||
| 311 | |||
| 312 | if (evp_cipher != NULL) { | ||
| 313 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 314 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 315 | int keylen, ivlen; | ||
| 316 | EVP_CIPHER_CTX *ctx; | ||
| 317 | |||
| 318 | if ((btmp = BIO_new(BIO_f_cipher())) == NULL) { | ||
| 319 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB); | ||
| 320 | goto err; | ||
| 321 | } | ||
| 322 | BIO_get_cipher_ctx(btmp, &ctx); | ||
| 323 | keylen = EVP_CIPHER_key_length(evp_cipher); | ||
| 324 | ivlen = EVP_CIPHER_iv_length(evp_cipher); | ||
| 325 | xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); | ||
| 326 | if (ivlen > 0) | ||
| 327 | arc4random_buf(iv, ivlen); | ||
| 328 | if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, | ||
| 329 | NULL, 1) <= 0) | ||
| 330 | goto err; | ||
| 331 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) | ||
| 332 | goto err; | ||
| 333 | if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0) | ||
| 334 | goto err; | ||
| 335 | |||
| 336 | if (ivlen > 0) { | ||
| 337 | if (xalg->parameter == NULL) { | ||
| 338 | xalg->parameter = ASN1_TYPE_new(); | ||
| 339 | if (xalg->parameter == NULL) | ||
| 340 | goto err; | ||
| 341 | } | ||
| 342 | if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) | ||
| 343 | goto err; | ||
| 344 | } | ||
| 345 | |||
| 346 | /* Lets do the pub key stuff :-) */ | ||
| 347 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { | ||
| 348 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); | ||
| 349 | if (pkcs7_encode_rinfo(ri, key, keylen) <= 0) | ||
| 350 | goto err; | ||
| 351 | } | ||
| 352 | OPENSSL_cleanse(key, keylen); | ||
| 353 | |||
| 354 | if (out == NULL) | ||
| 355 | out = btmp; | ||
| 356 | else | ||
| 357 | BIO_push(out, btmp); | ||
| 358 | btmp = NULL; | ||
| 359 | } | ||
| 360 | |||
| 361 | if (bio == NULL) { | ||
| 362 | if (PKCS7_is_detached(p7)) | ||
| 363 | bio = BIO_new(BIO_s_null()); | ||
| 364 | else if (os && os->length > 0) | ||
| 365 | bio = BIO_new_mem_buf(os->data, os->length); | ||
| 366 | if (bio == NULL) { | ||
| 367 | bio = BIO_new(BIO_s_mem()); | ||
| 368 | if (bio == NULL) | ||
| 369 | goto err; | ||
| 370 | BIO_set_mem_eof_return(bio, 0); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | if (out) | ||
| 374 | BIO_push(out, bio); | ||
| 375 | else | ||
| 376 | out = bio; | ||
| 377 | bio = NULL; | ||
| 378 | if (0) { | ||
| 379 | err: | ||
| 380 | if (out != NULL) | ||
| 381 | BIO_free_all(out); | ||
| 382 | if (btmp != NULL) | ||
| 383 | BIO_free_all(btmp); | ||
| 384 | out = NULL; | ||
| 385 | } | ||
| 386 | return (out); | ||
| 387 | } | ||
| 388 | |||
| 389 | static int | ||
| 390 | pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert) | ||
| 391 | { | ||
| 392 | int ret; | ||
| 393 | |||
| 394 | ret = X509_NAME_cmp(ri->issuer_and_serial->issuer, | ||
| 395 | pcert->cert_info->issuer); | ||
| 396 | if (ret) | ||
| 397 | return ret; | ||
| 398 | return M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber, | ||
| 399 | ri->issuer_and_serial->serial); | ||
| 400 | } | ||
| 401 | |||
| 402 | /* int */ | ||
| 403 | BIO * | ||
| 404 | PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) | ||
| 405 | { | ||
| 406 | int i, j; | ||
| 407 | BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL; | ||
| 408 | X509_ALGOR *xa; | ||
| 409 | ASN1_OCTET_STRING *data_body = NULL; | ||
| 410 | const EVP_MD *evp_md; | ||
| 411 | const EVP_CIPHER *evp_cipher = NULL; | ||
| 412 | EVP_CIPHER_CTX *evp_ctx = NULL; | ||
| 413 | X509_ALGOR *enc_alg = NULL; | ||
| 414 | STACK_OF(X509_ALGOR) *md_sk = NULL; | ||
| 415 | STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; | ||
| 416 | PKCS7_RECIP_INFO *ri = NULL; | ||
| 417 | unsigned char *ek = NULL, *tkey = NULL; | ||
| 418 | int eklen = 0, tkeylen = 0; | ||
| 419 | |||
| 420 | i = OBJ_obj2nid(p7->type); | ||
| 421 | p7->state = PKCS7_S_HEADER; | ||
| 422 | |||
| 423 | switch (i) { | ||
| 424 | case NID_pkcs7_signed: | ||
| 425 | data_body = PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 426 | md_sk = p7->d.sign->md_algs; | ||
| 427 | break; | ||
| 428 | case NID_pkcs7_signedAndEnveloped: | ||
| 429 | rsk = p7->d.signed_and_enveloped->recipientinfo; | ||
| 430 | md_sk = p7->d.signed_and_enveloped->md_algs; | ||
| 431 | data_body = p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 432 | enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 433 | evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 434 | if (evp_cipher == NULL) { | ||
| 435 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 436 | PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 437 | goto err; | ||
| 438 | } | ||
| 439 | break; | ||
| 440 | case NID_pkcs7_enveloped: | ||
| 441 | rsk = p7->d.enveloped->recipientinfo; | ||
| 442 | enc_alg = p7->d.enveloped->enc_data->algorithm; | ||
| 443 | data_body = p7->d.enveloped->enc_data->enc_data; | ||
| 444 | evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 445 | if (evp_cipher == NULL) { | ||
| 446 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 447 | PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 448 | goto err; | ||
| 449 | } | ||
| 450 | break; | ||
| 451 | default: | ||
| 452 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 453 | PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 454 | goto err; | ||
| 455 | } | ||
| 456 | |||
| 457 | /* We will be checking the signature */ | ||
| 458 | if (md_sk != NULL) { | ||
| 459 | for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { | ||
| 460 | xa = sk_X509_ALGOR_value(md_sk, i); | ||
| 461 | if ((btmp = BIO_new(BIO_f_md())) == NULL) { | ||
| 462 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 463 | ERR_R_BIO_LIB); | ||
| 464 | goto err; | ||
| 465 | } | ||
| 466 | |||
| 467 | j = OBJ_obj2nid(xa->algorithm); | ||
| 468 | evp_md = EVP_get_digestbynid(j); | ||
| 469 | if (evp_md == NULL) { | ||
| 470 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 471 | PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 472 | goto err; | ||
| 473 | } | ||
| 474 | |||
| 475 | BIO_set_md(btmp, evp_md); | ||
| 476 | if (out == NULL) | ||
| 477 | out = btmp; | ||
| 478 | else | ||
| 479 | BIO_push(out, btmp); | ||
| 480 | btmp = NULL; | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | if (evp_cipher != NULL) { | ||
| 485 | if ((etmp = BIO_new(BIO_f_cipher())) == NULL) { | ||
| 486 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB); | ||
| 487 | goto err; | ||
| 488 | } | ||
| 489 | |||
| 490 | /* It was encrypted, we need to decrypt the secret key | ||
| 491 | * with the private key */ | ||
| 492 | |||
| 493 | /* Find the recipientInfo which matches the passed certificate | ||
| 494 | * (if any) | ||
| 495 | */ | ||
| 496 | if (pcert) { | ||
| 497 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { | ||
| 498 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); | ||
| 499 | if (!pkcs7_cmp_ri(ri, pcert)) | ||
| 500 | break; | ||
| 501 | ri = NULL; | ||
| 502 | } | ||
| 503 | if (ri == NULL) { | ||
| 504 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 505 | PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); | ||
| 506 | goto err; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | /* If we haven't got a certificate try each ri in turn */ | ||
| 511 | if (pcert == NULL) { | ||
| 512 | /* Always attempt to decrypt all rinfo even | ||
| 513 | * after sucess as a defence against MMA timing | ||
| 514 | * attacks. | ||
| 515 | */ | ||
| 516 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { | ||
| 517 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); | ||
| 518 | |||
| 519 | if (pkcs7_decrypt_rinfo(&ek, &eklen, | ||
| 520 | ri, pkey) < 0) | ||
| 521 | goto err; | ||
| 522 | ERR_clear_error(); | ||
| 523 | } | ||
| 524 | } else { | ||
| 525 | /* Only exit on fatal errors, not decrypt failure */ | ||
| 526 | if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0) | ||
| 527 | goto err; | ||
| 528 | ERR_clear_error(); | ||
| 529 | } | ||
| 530 | |||
| 531 | evp_ctx = NULL; | ||
| 532 | BIO_get_cipher_ctx(etmp, &evp_ctx); | ||
| 533 | if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, | ||
| 534 | NULL, 0) <= 0) | ||
| 535 | goto err; | ||
| 536 | if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0) | ||
| 537 | goto err; | ||
| 538 | /* Generate random key as MMA defence */ | ||
| 539 | tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx); | ||
| 540 | tkey = malloc(tkeylen); | ||
| 541 | if (!tkey) | ||
| 542 | goto err; | ||
| 543 | if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0) | ||
| 544 | goto err; | ||
| 545 | if (ek == NULL) { | ||
| 546 | ek = tkey; | ||
| 547 | eklen = tkeylen; | ||
| 548 | tkey = NULL; | ||
| 549 | } | ||
| 550 | |||
| 551 | if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) { | ||
| 552 | /* Some S/MIME clients don't use the same key | ||
| 553 | * and effective key length. The key length is | ||
| 554 | * determined by the size of the decrypted RSA key. | ||
| 555 | */ | ||
| 556 | if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) { | ||
| 557 | /* Use random key as MMA defence */ | ||
| 558 | OPENSSL_cleanse(ek, eklen); | ||
| 559 | free(ek); | ||
| 560 | ek = tkey; | ||
| 561 | eklen = tkeylen; | ||
| 562 | tkey = NULL; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | /* Clear errors so we don't leak information useful in MMA */ | ||
| 566 | ERR_clear_error(); | ||
| 567 | if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0) | ||
| 568 | goto err; | ||
| 569 | |||
| 570 | if (ek) { | ||
| 571 | OPENSSL_cleanse(ek, eklen); | ||
| 572 | free(ek); | ||
| 573 | ek = NULL; | ||
| 574 | } | ||
| 575 | if (tkey) { | ||
| 576 | OPENSSL_cleanse(tkey, tkeylen); | ||
| 577 | free(tkey); | ||
| 578 | tkey = NULL; | ||
| 579 | } | ||
| 580 | |||
| 581 | if (out == NULL) | ||
| 582 | out = etmp; | ||
| 583 | else | ||
| 584 | BIO_push(out, etmp); | ||
| 585 | etmp = NULL; | ||
| 586 | } | ||
| 587 | |||
| 588 | if (PKCS7_is_detached(p7) || (in_bio != NULL)) { | ||
| 589 | bio = in_bio; | ||
| 590 | } else { | ||
| 591 | if (data_body != NULL && data_body->length > 0) | ||
| 592 | bio = BIO_new_mem_buf(data_body->data, data_body->length); | ||
| 593 | else { | ||
| 594 | bio = BIO_new(BIO_s_mem()); | ||
| 595 | BIO_set_mem_eof_return(bio, 0); | ||
| 596 | } | ||
| 597 | if (bio == NULL) | ||
| 598 | goto err; | ||
| 599 | } | ||
| 600 | BIO_push(out, bio); | ||
| 601 | bio = NULL; | ||
| 602 | |||
| 603 | if (0) { | ||
| 604 | err: | ||
| 605 | if (ek) { | ||
| 606 | OPENSSL_cleanse(ek, eklen); | ||
| 607 | free(ek); | ||
| 608 | } | ||
| 609 | if (tkey) { | ||
| 610 | OPENSSL_cleanse(tkey, tkeylen); | ||
| 611 | free(tkey); | ||
| 612 | } | ||
| 613 | if (out != NULL) | ||
| 614 | BIO_free_all(out); | ||
| 615 | if (btmp != NULL) | ||
| 616 | BIO_free_all(btmp); | ||
| 617 | if (etmp != NULL) | ||
| 618 | BIO_free_all(etmp); | ||
| 619 | if (bio != NULL) | ||
| 620 | BIO_free_all(bio); | ||
| 621 | out = NULL; | ||
| 622 | } | ||
| 623 | return (out); | ||
| 624 | } | ||
| 625 | |||
| 626 | static BIO * | ||
| 627 | PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) | ||
| 628 | { | ||
| 629 | for (;;) { | ||
| 630 | bio = BIO_find_type(bio, BIO_TYPE_MD); | ||
| 631 | if (bio == NULL) { | ||
| 632 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, | ||
| 633 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 634 | return NULL; | ||
| 635 | } | ||
| 636 | BIO_get_md_ctx(bio, pmd); | ||
| 637 | if (*pmd == NULL) { | ||
| 638 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, | ||
| 639 | ERR_R_INTERNAL_ERROR); | ||
| 640 | return NULL; | ||
| 641 | } | ||
| 642 | if (EVP_MD_CTX_type(*pmd) == nid) | ||
| 643 | return bio; | ||
| 644 | bio = BIO_next(bio); | ||
| 645 | } | ||
| 646 | return NULL; | ||
| 647 | } | ||
| 648 | |||
| 649 | static int | ||
| 650 | do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx) | ||
| 651 | { | ||
| 652 | unsigned char md_data[EVP_MAX_MD_SIZE]; | ||
| 653 | unsigned int md_len; | ||
| 654 | |||
| 655 | /* Add signing time if not already present */ | ||
| 656 | if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) { | ||
| 657 | if (!PKCS7_add0_attrib_signing_time(si, NULL)) { | ||
| 658 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, | ||
| 659 | ERR_R_MALLOC_FAILURE); | ||
| 660 | return 0; | ||
| 661 | } | ||
| 662 | } | ||
| 663 | |||
| 664 | /* Add digest */ | ||
| 665 | if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) { | ||
| 666 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB); | ||
| 667 | return 0; | ||
| 668 | } | ||
| 669 | if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) { | ||
| 670 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE); | ||
| 671 | return 0; | ||
| 672 | } | ||
| 673 | |||
| 674 | /* Now sign the attributes */ | ||
| 675 | if (!PKCS7_SIGNER_INFO_sign(si)) | ||
| 676 | return 0; | ||
| 677 | |||
| 678 | return 1; | ||
| 679 | } | ||
| 680 | |||
| 681 | |||
| 682 | int | ||
| 683 | PKCS7_dataFinal(PKCS7 *p7, BIO *bio) | ||
| 684 | { | ||
| 685 | int ret = 0; | ||
| 686 | int i, j; | ||
| 687 | BIO *btmp; | ||
| 688 | PKCS7_SIGNER_INFO *si; | ||
| 689 | EVP_MD_CTX *mdc, ctx_tmp; | ||
| 690 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 691 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; | ||
| 692 | ASN1_OCTET_STRING *os = NULL; | ||
| 693 | |||
| 694 | EVP_MD_CTX_init(&ctx_tmp); | ||
| 695 | i = OBJ_obj2nid(p7->type); | ||
| 696 | p7->state = PKCS7_S_HEADER; | ||
| 697 | |||
| 698 | switch (i) { | ||
| 699 | case NID_pkcs7_data: | ||
| 700 | os = p7->d.data; | ||
| 701 | break; | ||
| 702 | case NID_pkcs7_signedAndEnveloped: | ||
| 703 | /* XXX */ | ||
| 704 | si_sk = p7->d.signed_and_enveloped->signer_info; | ||
| 705 | os = p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 706 | if (!os) { | ||
| 707 | os = M_ASN1_OCTET_STRING_new(); | ||
| 708 | if (!os) { | ||
| 709 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 710 | ERR_R_MALLOC_FAILURE); | ||
| 711 | goto err; | ||
| 712 | } | ||
| 713 | p7->d.signed_and_enveloped->enc_data->enc_data = os; | ||
| 714 | } | ||
| 715 | break; | ||
| 716 | case NID_pkcs7_enveloped: | ||
| 717 | /* XXX */ | ||
| 718 | os = p7->d.enveloped->enc_data->enc_data; | ||
| 719 | if (!os) { | ||
| 720 | os = M_ASN1_OCTET_STRING_new(); | ||
| 721 | if (!os) { | ||
| 722 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 723 | ERR_R_MALLOC_FAILURE); | ||
| 724 | goto err; | ||
| 725 | } | ||
| 726 | p7->d.enveloped->enc_data->enc_data = os; | ||
| 727 | } | ||
| 728 | break; | ||
| 729 | case NID_pkcs7_signed: | ||
| 730 | si_sk = p7->d.sign->signer_info; | ||
| 731 | os = PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 732 | if (!PKCS7_is_detached(p7) && os == NULL) { | ||
| 733 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_DECODE_ERROR); | ||
| 734 | goto err; | ||
| 735 | } | ||
| 736 | /* If detached data then the content is excluded */ | ||
| 737 | if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { | ||
| 738 | M_ASN1_OCTET_STRING_free(os); | ||
| 739 | p7->d.sign->contents->d.data = NULL; | ||
| 740 | } | ||
| 741 | break; | ||
| 742 | |||
| 743 | case NID_pkcs7_digest: | ||
| 744 | os = PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 745 | if (os == NULL) { | ||
| 746 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_DECODE_ERROR); | ||
| 747 | goto err; | ||
| 748 | } | ||
| 749 | /* If detached data then the content is excluded */ | ||
| 750 | if (PKCS7_type_is_data(p7->d.digest->contents) && | ||
| 751 | p7->detached) { | ||
| 752 | M_ASN1_OCTET_STRING_free(os); | ||
| 753 | p7->d.digest->contents->d.data = NULL; | ||
| 754 | } | ||
| 755 | break; | ||
| 756 | |||
| 757 | default: | ||
| 758 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 759 | PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 760 | goto err; | ||
| 761 | } | ||
| 762 | |||
| 763 | if (si_sk != NULL) { | ||
| 764 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) { | ||
| 765 | si = sk_PKCS7_SIGNER_INFO_value(si_sk, i); | ||
| 766 | if (si->pkey == NULL) | ||
| 767 | continue; | ||
| 768 | |||
| 769 | j = OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 770 | |||
| 771 | btmp = bio; | ||
| 772 | |||
| 773 | btmp = PKCS7_find_digest(&mdc, btmp, j); | ||
| 774 | |||
| 775 | if (btmp == NULL) | ||
| 776 | goto err; | ||
| 777 | |||
| 778 | /* We now have the EVP_MD_CTX, lets do the | ||
| 779 | * signing. */ | ||
| 780 | if (!EVP_MD_CTX_copy_ex(&ctx_tmp, mdc)) | ||
| 781 | goto err; | ||
| 782 | |||
| 783 | sk = si->auth_attr; | ||
| 784 | |||
| 785 | /* If there are attributes, we add the digest | ||
| 786 | * attribute and only sign the attributes */ | ||
| 787 | if (sk_X509_ATTRIBUTE_num(sk) > 0) { | ||
| 788 | if (!do_pkcs7_signed_attrib(si, &ctx_tmp)) | ||
| 789 | goto err; | ||
| 790 | } else { | ||
| 791 | unsigned char *abuf = NULL; | ||
| 792 | unsigned int abuflen; | ||
| 793 | abuflen = EVP_PKEY_size(si->pkey); | ||
| 794 | abuf = malloc(abuflen); | ||
| 795 | if (!abuf) | ||
| 796 | goto err; | ||
| 797 | |||
| 798 | if (!EVP_SignFinal(&ctx_tmp, abuf, &abuflen, | ||
| 799 | si->pkey)) { | ||
| 800 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 801 | ERR_R_EVP_LIB); | ||
| 802 | goto err; | ||
| 803 | } | ||
| 804 | ASN1_STRING_set0(si->enc_digest, abuf, abuflen); | ||
| 805 | } | ||
| 806 | } | ||
| 807 | } else if (i == NID_pkcs7_digest) { | ||
| 808 | unsigned char md_data[EVP_MAX_MD_SIZE]; | ||
| 809 | unsigned int md_len; | ||
| 810 | if (!PKCS7_find_digest(&mdc, bio, | ||
| 811 | OBJ_obj2nid(p7->d.digest->md->algorithm))) | ||
| 812 | goto err; | ||
| 813 | if (!EVP_DigestFinal_ex(mdc, md_data, &md_len)) | ||
| 814 | goto err; | ||
| 815 | M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len); | ||
| 816 | } | ||
| 817 | |||
| 818 | if (!PKCS7_is_detached(p7) && !(os->flags & ASN1_STRING_FLAG_NDEF)) { | ||
| 819 | char *cont; | ||
| 820 | long contlen; | ||
| 821 | btmp = BIO_find_type(bio, BIO_TYPE_MEM); | ||
| 822 | if (btmp == NULL) { | ||
| 823 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 824 | PKCS7_R_UNABLE_TO_FIND_MEM_BIO); | ||
| 825 | goto err; | ||
| 826 | } | ||
| 827 | contlen = BIO_get_mem_data(btmp, &cont); | ||
| 828 | /* Mark the BIO read only then we can use its copy of the data | ||
| 829 | * instead of making an extra copy. | ||
| 830 | */ | ||
| 831 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); | ||
| 832 | BIO_set_mem_eof_return(btmp, 0); | ||
| 833 | ASN1_STRING_set0(os, (unsigned char *)cont, contlen); | ||
| 834 | } | ||
| 835 | ret = 1; | ||
| 836 | err: | ||
| 837 | EVP_MD_CTX_cleanup(&ctx_tmp); | ||
| 838 | return (ret); | ||
| 839 | } | ||
| 840 | |||
| 841 | int | ||
| 842 | PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si) | ||
| 843 | { | ||
| 844 | EVP_MD_CTX mctx; | ||
| 845 | EVP_PKEY_CTX *pctx; | ||
| 846 | unsigned char *abuf = NULL; | ||
| 847 | int alen; | ||
| 848 | size_t siglen; | ||
| 849 | const EVP_MD *md = NULL; | ||
| 850 | |||
| 851 | md = EVP_get_digestbyobj(si->digest_alg->algorithm); | ||
| 852 | if (md == NULL) | ||
| 853 | return 0; | ||
| 854 | |||
| 855 | EVP_MD_CTX_init(&mctx); | ||
| 856 | if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0) | ||
| 857 | goto err; | ||
| 858 | |||
| 859 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
| 860 | EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) { | ||
| 861 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | ||
| 862 | goto err; | ||
| 863 | } | ||
| 864 | |||
| 865 | alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf, | ||
| 866 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); | ||
| 867 | if (!abuf) | ||
| 868 | goto err; | ||
| 869 | if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0) | ||
| 870 | goto err; | ||
| 871 | free(abuf); | ||
| 872 | abuf = NULL; | ||
| 873 | if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0) | ||
| 874 | goto err; | ||
| 875 | abuf = malloc(siglen); | ||
| 876 | if (!abuf) | ||
| 877 | goto err; | ||
| 878 | if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0) | ||
| 879 | goto err; | ||
| 880 | |||
| 881 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
| 882 | EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) { | ||
| 883 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | ||
| 884 | goto err; | ||
| 885 | } | ||
| 886 | |||
| 887 | EVP_MD_CTX_cleanup(&mctx); | ||
| 888 | |||
| 889 | ASN1_STRING_set0(si->enc_digest, abuf, siglen); | ||
| 890 | |||
| 891 | return 1; | ||
| 892 | |||
| 893 | err: | ||
| 894 | free(abuf); | ||
| 895 | EVP_MD_CTX_cleanup(&mctx); | ||
| 896 | return 0; | ||
| 897 | } | ||
| 898 | |||
| 899 | int | ||
| 900 | PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, | ||
| 901 | PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 902 | { | ||
| 903 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 904 | int ret = 0, i; | ||
| 905 | STACK_OF(X509) *cert; | ||
| 906 | X509 *x509; | ||
| 907 | |||
| 908 | if (PKCS7_type_is_signed(p7)) { | ||
| 909 | cert = p7->d.sign->cert; | ||
| 910 | } else if (PKCS7_type_is_signedAndEnveloped(p7)) { | ||
| 911 | cert = p7->d.signed_and_enveloped->cert; | ||
| 912 | } else { | ||
| 913 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 914 | goto err; | ||
| 915 | } | ||
| 916 | /* XXXX */ | ||
| 917 | ias = si->issuer_and_serial; | ||
| 918 | |||
| 919 | x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial); | ||
| 920 | |||
| 921 | /* were we able to find the cert in passed to us */ | ||
| 922 | if (x509 == NULL) { | ||
| 923 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, | ||
| 924 | PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); | ||
| 925 | goto err; | ||
| 926 | } | ||
| 927 | |||
| 928 | /* Lets verify */ | ||
| 929 | if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) { | ||
| 930 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB); | ||
| 931 | goto err; | ||
| 932 | } | ||
| 933 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); | ||
| 934 | i = X509_verify_cert(ctx); | ||
| 935 | if (i <= 0) { | ||
| 936 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB); | ||
| 937 | X509_STORE_CTX_cleanup(ctx); | ||
| 938 | goto err; | ||
| 939 | } | ||
| 940 | X509_STORE_CTX_cleanup(ctx); | ||
| 941 | |||
| 942 | return PKCS7_signatureVerify(bio, p7, si, x509); | ||
| 943 | err: | ||
| 944 | return ret; | ||
| 945 | } | ||
| 946 | |||
| 947 | int | ||
| 948 | PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, X509 *x509) | ||
| 949 | { | ||
| 950 | ASN1_OCTET_STRING *os; | ||
| 951 | EVP_MD_CTX mdc_tmp, *mdc; | ||
| 952 | int ret = 0, i; | ||
| 953 | int md_type; | ||
| 954 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 955 | BIO *btmp; | ||
| 956 | EVP_PKEY *pkey; | ||
| 957 | |||
| 958 | EVP_MD_CTX_init(&mdc_tmp); | ||
| 959 | |||
| 960 | if (!PKCS7_type_is_signed(p7) && | ||
| 961 | !PKCS7_type_is_signedAndEnveloped(p7)) { | ||
| 962 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 963 | PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 964 | goto err; | ||
| 965 | } | ||
| 966 | |||
| 967 | md_type = OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 968 | |||
| 969 | btmp = bio; | ||
| 970 | for (;;) { | ||
| 971 | if ((btmp == NULL) || | ||
| 972 | ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) { | ||
| 973 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 974 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 975 | goto err; | ||
| 976 | } | ||
| 977 | BIO_get_md_ctx(btmp, &mdc); | ||
| 978 | if (mdc == NULL) { | ||
| 979 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 980 | ERR_R_INTERNAL_ERROR); | ||
| 981 | goto err; | ||
| 982 | } | ||
| 983 | if (EVP_MD_CTX_type(mdc) == md_type) | ||
| 984 | break; | ||
| 985 | /* Workaround for some broken clients that put the signature | ||
| 986 | * OID instead of the digest OID in digest_alg->algorithm | ||
| 987 | */ | ||
| 988 | if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) | ||
| 989 | break; | ||
| 990 | btmp = BIO_next(btmp); | ||
| 991 | } | ||
| 992 | |||
| 993 | /* mdc is the digest ctx that we want, unless there are attributes, | ||
| 994 | * in which case the digest is the signed attributes */ | ||
| 995 | if (!EVP_MD_CTX_copy_ex(&mdc_tmp, mdc)) | ||
| 996 | goto err; | ||
| 997 | |||
| 998 | sk = si->auth_attr; | ||
| 999 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) { | ||
| 1000 | unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; | ||
| 1001 | unsigned int md_len; | ||
| 1002 | int alen; | ||
| 1003 | ASN1_OCTET_STRING *message_digest; | ||
| 1004 | |||
| 1005 | if (!EVP_DigestFinal_ex(&mdc_tmp, md_dat, &md_len)) | ||
| 1006 | goto err; | ||
| 1007 | message_digest = PKCS7_digest_from_attributes(sk); | ||
| 1008 | if (!message_digest) { | ||
| 1009 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1010 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 1011 | goto err; | ||
| 1012 | } | ||
| 1013 | if ((message_digest->length != (int)md_len) || | ||
| 1014 | (memcmp(message_digest->data, md_dat, md_len))) { | ||
| 1015 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1016 | PKCS7_R_DIGEST_FAILURE); | ||
| 1017 | ret = -1; | ||
| 1018 | goto err; | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | if (!EVP_VerifyInit_ex(&mdc_tmp, EVP_get_digestbynid(md_type), | ||
| 1022 | NULL)) | ||
| 1023 | goto err; | ||
| 1024 | |||
| 1025 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, | ||
| 1026 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); | ||
| 1027 | if (alen <= 0) { | ||
| 1028 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB); | ||
| 1029 | ret = -1; | ||
| 1030 | goto err; | ||
| 1031 | } | ||
| 1032 | if (!EVP_VerifyUpdate(&mdc_tmp, abuf, alen)) | ||
| 1033 | goto err; | ||
| 1034 | |||
| 1035 | free(abuf); | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | os = si->enc_digest; | ||
| 1039 | pkey = X509_get_pubkey(x509); | ||
| 1040 | if (!pkey) { | ||
| 1041 | ret = -1; | ||
| 1042 | goto err; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | i = EVP_VerifyFinal(&mdc_tmp, os->data, os->length, pkey); | ||
| 1046 | EVP_PKEY_free(pkey); | ||
| 1047 | if (i <= 0) { | ||
| 1048 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1049 | PKCS7_R_SIGNATURE_FAILURE); | ||
| 1050 | ret = -1; | ||
| 1051 | goto err; | ||
| 1052 | } else | ||
| 1053 | ret = 1; | ||
| 1054 | err: | ||
| 1055 | EVP_MD_CTX_cleanup(&mdc_tmp); | ||
| 1056 | return (ret); | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | PKCS7_ISSUER_AND_SERIAL * | ||
| 1060 | PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) | ||
| 1061 | { | ||
| 1062 | STACK_OF(PKCS7_RECIP_INFO) *rsk; | ||
| 1063 | PKCS7_RECIP_INFO *ri; | ||
| 1064 | int i; | ||
| 1065 | |||
| 1066 | i = OBJ_obj2nid(p7->type); | ||
| 1067 | if (i != NID_pkcs7_signedAndEnveloped) | ||
| 1068 | return NULL; | ||
| 1069 | if (p7->d.signed_and_enveloped == NULL) | ||
| 1070 | return NULL; | ||
| 1071 | rsk = p7->d.signed_and_enveloped->recipientinfo; | ||
| 1072 | if (rsk == NULL) | ||
| 1073 | return NULL; | ||
| 1074 | ri = sk_PKCS7_RECIP_INFO_value(rsk, 0); | ||
| 1075 | if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) | ||
| 1076 | return (NULL); | ||
| 1077 | ri = sk_PKCS7_RECIP_INFO_value(rsk, idx); | ||
| 1078 | return (ri->issuer_and_serial); | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | ASN1_TYPE * | ||
| 1082 | PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1083 | { | ||
| 1084 | return (get_attribute(si->auth_attr, nid)); | ||
| 1085 | } | ||
| 1086 | |||
| 1087 | ASN1_TYPE * | ||
| 1088 | PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1089 | { | ||
| 1090 | return (get_attribute(si->unauth_attr, nid)); | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | static ASN1_TYPE * | ||
| 1094 | get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) | ||
| 1095 | { | ||
| 1096 | int i; | ||
| 1097 | X509_ATTRIBUTE *xa; | ||
| 1098 | ASN1_OBJECT *o; | ||
| 1099 | |||
| 1100 | o = OBJ_nid2obj(nid); | ||
| 1101 | if (!o || !sk) | ||
| 1102 | return (NULL); | ||
| 1103 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { | ||
| 1104 | xa = sk_X509_ATTRIBUTE_value(sk, i); | ||
| 1105 | if (OBJ_cmp(xa->object, o) == 0) { | ||
| 1106 | if (!xa->single && sk_ASN1_TYPE_num(xa->value.set)) | ||
| 1107 | return (sk_ASN1_TYPE_value(xa->value.set, 0)); | ||
| 1108 | else | ||
| 1109 | return (NULL); | ||
| 1110 | } | ||
| 1111 | } | ||
| 1112 | return (NULL); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | ASN1_OCTET_STRING * | ||
| 1116 | PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1117 | { | ||
| 1118 | ASN1_TYPE *astype; | ||
| 1119 | |||
| 1120 | if (!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) | ||
| 1121 | return NULL; | ||
| 1122 | return astype->value.octet_string; | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | int | ||
| 1126 | PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 1127 | STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1128 | { | ||
| 1129 | int i; | ||
| 1130 | |||
| 1131 | if (p7si->auth_attr != NULL) | ||
| 1132 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, | ||
| 1133 | X509_ATTRIBUTE_free); | ||
| 1134 | p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk); | ||
| 1135 | if (p7si->auth_attr == NULL) | ||
| 1136 | return 0; | ||
| 1137 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { | ||
| 1138 | if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i, | ||
| 1139 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk, i)))) | ||
| 1140 | == NULL) | ||
| 1141 | return (0); | ||
| 1142 | } | ||
| 1143 | return (1); | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | int | ||
| 1147 | PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1148 | { | ||
| 1149 | int i; | ||
| 1150 | |||
| 1151 | if (p7si->unauth_attr != NULL) | ||
| 1152 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, | ||
| 1153 | X509_ATTRIBUTE_free); | ||
| 1154 | p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk); | ||
| 1155 | if (p7si->unauth_attr == NULL) | ||
| 1156 | return 0; | ||
| 1157 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { | ||
| 1158 | if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i, | ||
| 1159 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk, i)))) | ||
| 1160 | == NULL) | ||
| 1161 | return (0); | ||
| 1162 | } | ||
| 1163 | return (1); | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | int | ||
| 1167 | PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 1168 | void *value) | ||
| 1169 | { | ||
| 1170 | return (add_attribute(&(p7si->auth_attr), nid, atrtype, value)); | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | int | ||
| 1174 | PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, void *value) | ||
| 1175 | { | ||
| 1176 | return (add_attribute(&(p7si->unauth_attr), nid, atrtype, value)); | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | static int | ||
| 1180 | add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, void *value) | ||
| 1181 | { | ||
| 1182 | X509_ATTRIBUTE *attr = NULL; | ||
| 1183 | |||
| 1184 | if (*sk == NULL) { | ||
| 1185 | *sk = sk_X509_ATTRIBUTE_new_null(); | ||
| 1186 | if (*sk == NULL) | ||
| 1187 | return 0; | ||
| 1188 | new_attrib: | ||
| 1189 | if (!(attr = X509_ATTRIBUTE_create(nid, atrtype, value))) | ||
| 1190 | return 0; | ||
| 1191 | if (!sk_X509_ATTRIBUTE_push(*sk, attr)) { | ||
| 1192 | X509_ATTRIBUTE_free(attr); | ||
| 1193 | return 0; | ||
| 1194 | } | ||
| 1195 | } else { | ||
| 1196 | int i; | ||
| 1197 | |||
| 1198 | for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) { | ||
| 1199 | attr = sk_X509_ATTRIBUTE_value(*sk, i); | ||
| 1200 | if (OBJ_obj2nid(attr->object) == nid) { | ||
| 1201 | X509_ATTRIBUTE_free(attr); | ||
| 1202 | attr = X509_ATTRIBUTE_create(nid, atrtype, | ||
| 1203 | value); | ||
| 1204 | if (attr == NULL) | ||
| 1205 | return 0; | ||
| 1206 | if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) { | ||
| 1207 | X509_ATTRIBUTE_free(attr); | ||
| 1208 | return 0; | ||
| 1209 | } | ||
| 1210 | goto end; | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | goto new_attrib; | ||
| 1214 | } | ||
| 1215 | end: | ||
| 1216 | return (1); | ||
| 1217 | } | ||
