1*0Sstevel@tonic-gate /* crypto/pkcs7/pk7_doit.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #include <stdio.h> 60*0Sstevel@tonic-gate #include "cryptlib.h" 61*0Sstevel@tonic-gate #include <openssl/rand.h> 62*0Sstevel@tonic-gate #include <openssl/objects.h> 63*0Sstevel@tonic-gate #include <openssl/x509.h> 64*0Sstevel@tonic-gate #include <openssl/x509v3.h> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, 67*0Sstevel@tonic-gate void *value); 68*0Sstevel@tonic-gate static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate static int PKCS7_type_is_other(PKCS7* p7) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate int isOther=1; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate int nid=OBJ_obj2nid(p7->type); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate switch( nid ) 77*0Sstevel@tonic-gate { 78*0Sstevel@tonic-gate case NID_pkcs7_data: 79*0Sstevel@tonic-gate case NID_pkcs7_signed: 80*0Sstevel@tonic-gate case NID_pkcs7_enveloped: 81*0Sstevel@tonic-gate case NID_pkcs7_signedAndEnveloped: 82*0Sstevel@tonic-gate case NID_pkcs7_digest: 83*0Sstevel@tonic-gate case NID_pkcs7_encrypted: 84*0Sstevel@tonic-gate isOther=0; 85*0Sstevel@tonic-gate break; 86*0Sstevel@tonic-gate default: 87*0Sstevel@tonic-gate isOther=1; 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate return isOther; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7) 95*0Sstevel@tonic-gate { 96*0Sstevel@tonic-gate if ( PKCS7_type_is_data(p7)) 97*0Sstevel@tonic-gate return p7->d.data; 98*0Sstevel@tonic-gate if ( PKCS7_type_is_other(p7) && p7->d.other 99*0Sstevel@tonic-gate && (p7->d.other->type == V_ASN1_OCTET_STRING)) 100*0Sstevel@tonic-gate return p7->d.other->value.octet_string; 101*0Sstevel@tonic-gate return NULL; 102*0Sstevel@tonic-gate } 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) 105*0Sstevel@tonic-gate { 106*0Sstevel@tonic-gate int i; 107*0Sstevel@tonic-gate BIO *out=NULL,*btmp=NULL; 108*0Sstevel@tonic-gate X509_ALGOR *xa; 109*0Sstevel@tonic-gate const EVP_MD *evp_md; 110*0Sstevel@tonic-gate const EVP_CIPHER *evp_cipher=NULL; 111*0Sstevel@tonic-gate STACK_OF(X509_ALGOR) *md_sk=NULL; 112*0Sstevel@tonic-gate STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; 113*0Sstevel@tonic-gate X509_ALGOR *xalg=NULL; 114*0Sstevel@tonic-gate PKCS7_RECIP_INFO *ri=NULL; 115*0Sstevel@tonic-gate EVP_PKEY *pkey; 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate i=OBJ_obj2nid(p7->type); 118*0Sstevel@tonic-gate p7->state=PKCS7_S_HEADER; 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate switch (i) 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate case NID_pkcs7_signed: 123*0Sstevel@tonic-gate md_sk=p7->d.sign->md_algs; 124*0Sstevel@tonic-gate break; 125*0Sstevel@tonic-gate case NID_pkcs7_signedAndEnveloped: 126*0Sstevel@tonic-gate rsk=p7->d.signed_and_enveloped->recipientinfo; 127*0Sstevel@tonic-gate md_sk=p7->d.signed_and_enveloped->md_algs; 128*0Sstevel@tonic-gate xalg=p7->d.signed_and_enveloped->enc_data->algorithm; 129*0Sstevel@tonic-gate evp_cipher=p7->d.signed_and_enveloped->enc_data->cipher; 130*0Sstevel@tonic-gate if (evp_cipher == NULL) 131*0Sstevel@tonic-gate { 132*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT, 133*0Sstevel@tonic-gate PKCS7_R_CIPHER_NOT_INITIALIZED); 134*0Sstevel@tonic-gate goto err; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate break; 137*0Sstevel@tonic-gate case NID_pkcs7_enveloped: 138*0Sstevel@tonic-gate rsk=p7->d.enveloped->recipientinfo; 139*0Sstevel@tonic-gate xalg=p7->d.enveloped->enc_data->algorithm; 140*0Sstevel@tonic-gate evp_cipher=p7->d.enveloped->enc_data->cipher; 141*0Sstevel@tonic-gate if (evp_cipher == NULL) 142*0Sstevel@tonic-gate { 143*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT, 144*0Sstevel@tonic-gate PKCS7_R_CIPHER_NOT_INITIALIZED); 145*0Sstevel@tonic-gate goto err; 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate break; 148*0Sstevel@tonic-gate default: 149*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 150*0Sstevel@tonic-gate goto err; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate if (md_sk != NULL) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate xa=sk_X509_ALGOR_value(md_sk,i); 158*0Sstevel@tonic-gate if ((btmp=BIO_new(BIO_f_md())) == NULL) 159*0Sstevel@tonic-gate { 160*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); 161*0Sstevel@tonic-gate goto err; 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate evp_md=EVP_get_digestbyobj(xa->algorithm); 165*0Sstevel@tonic-gate if (evp_md == NULL) 166*0Sstevel@tonic-gate { 167*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE); 168*0Sstevel@tonic-gate goto err; 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate BIO_set_md(btmp,evp_md); 172*0Sstevel@tonic-gate if (out == NULL) 173*0Sstevel@tonic-gate out=btmp; 174*0Sstevel@tonic-gate else 175*0Sstevel@tonic-gate BIO_push(out,btmp); 176*0Sstevel@tonic-gate btmp=NULL; 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate if (evp_cipher != NULL) 181*0Sstevel@tonic-gate { 182*0Sstevel@tonic-gate unsigned char key[EVP_MAX_KEY_LENGTH]; 183*0Sstevel@tonic-gate unsigned char iv[EVP_MAX_IV_LENGTH]; 184*0Sstevel@tonic-gate int keylen,ivlen; 185*0Sstevel@tonic-gate int jj,max; 186*0Sstevel@tonic-gate unsigned char *tmp; 187*0Sstevel@tonic-gate EVP_CIPHER_CTX *ctx; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate if ((btmp=BIO_new(BIO_f_cipher())) == NULL) 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); 192*0Sstevel@tonic-gate goto err; 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate BIO_get_cipher_ctx(btmp, &ctx); 195*0Sstevel@tonic-gate keylen=EVP_CIPHER_key_length(evp_cipher); 196*0Sstevel@tonic-gate ivlen=EVP_CIPHER_iv_length(evp_cipher); 197*0Sstevel@tonic-gate if (RAND_bytes(key,keylen) <= 0) 198*0Sstevel@tonic-gate goto err; 199*0Sstevel@tonic-gate xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); 200*0Sstevel@tonic-gate if (ivlen > 0) RAND_pseudo_bytes(iv,ivlen); 201*0Sstevel@tonic-gate EVP_CipherInit_ex(ctx, evp_cipher, NULL, key, iv, 1); 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate if (ivlen > 0) { 204*0Sstevel@tonic-gate if (xalg->parameter == NULL) 205*0Sstevel@tonic-gate xalg->parameter=ASN1_TYPE_new(); 206*0Sstevel@tonic-gate if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) 207*0Sstevel@tonic-gate goto err; 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate /* Lets do the pub key stuff :-) */ 211*0Sstevel@tonic-gate max=0; 212*0Sstevel@tonic-gate for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) 213*0Sstevel@tonic-gate { 214*0Sstevel@tonic-gate ri=sk_PKCS7_RECIP_INFO_value(rsk,i); 215*0Sstevel@tonic-gate if (ri->cert == NULL) 216*0Sstevel@tonic-gate { 217*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_MISSING_CERIPEND_INFO); 218*0Sstevel@tonic-gate goto err; 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate pkey=X509_get_pubkey(ri->cert); 221*0Sstevel@tonic-gate jj=EVP_PKEY_size(pkey); 222*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 223*0Sstevel@tonic-gate if (max < jj) max=jj; 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate if ((tmp=(unsigned char *)OPENSSL_malloc(max)) == NULL) 226*0Sstevel@tonic-gate { 227*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_MALLOC_FAILURE); 228*0Sstevel@tonic-gate goto err; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) 231*0Sstevel@tonic-gate { 232*0Sstevel@tonic-gate ri=sk_PKCS7_RECIP_INFO_value(rsk,i); 233*0Sstevel@tonic-gate pkey=X509_get_pubkey(ri->cert); 234*0Sstevel@tonic-gate jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey); 235*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 236*0Sstevel@tonic-gate if (jj <= 0) 237*0Sstevel@tonic-gate { 238*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_EVP_LIB); 239*0Sstevel@tonic-gate OPENSSL_free(tmp); 240*0Sstevel@tonic-gate goto err; 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate M_ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj); 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate OPENSSL_free(tmp); 245*0Sstevel@tonic-gate OPENSSL_cleanse(key, keylen); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate if (out == NULL) 248*0Sstevel@tonic-gate out=btmp; 249*0Sstevel@tonic-gate else 250*0Sstevel@tonic-gate BIO_push(out,btmp); 251*0Sstevel@tonic-gate btmp=NULL; 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate if (bio == NULL) 255*0Sstevel@tonic-gate { 256*0Sstevel@tonic-gate if (PKCS7_is_detached(p7)) 257*0Sstevel@tonic-gate bio=BIO_new(BIO_s_null()); 258*0Sstevel@tonic-gate else 259*0Sstevel@tonic-gate { 260*0Sstevel@tonic-gate ASN1_OCTET_STRING *os; 261*0Sstevel@tonic-gate os = PKCS7_get_octet_string(p7->d.sign->contents); 262*0Sstevel@tonic-gate if (os && os->length > 0) 263*0Sstevel@tonic-gate bio = BIO_new_mem_buf(os->data, os->length); 264*0Sstevel@tonic-gate if(bio == NULL) 265*0Sstevel@tonic-gate { 266*0Sstevel@tonic-gate bio=BIO_new(BIO_s_mem()); 267*0Sstevel@tonic-gate BIO_set_mem_eof_return(bio,0); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate BIO_push(out,bio); 272*0Sstevel@tonic-gate bio=NULL; 273*0Sstevel@tonic-gate if (0) 274*0Sstevel@tonic-gate { 275*0Sstevel@tonic-gate err: 276*0Sstevel@tonic-gate if (out != NULL) 277*0Sstevel@tonic-gate BIO_free_all(out); 278*0Sstevel@tonic-gate if (btmp != NULL) 279*0Sstevel@tonic-gate BIO_free_all(btmp); 280*0Sstevel@tonic-gate out=NULL; 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate return(out); 283*0Sstevel@tonic-gate } 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate /* int */ 286*0Sstevel@tonic-gate BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) 287*0Sstevel@tonic-gate { 288*0Sstevel@tonic-gate int i,j; 289*0Sstevel@tonic-gate BIO *out=NULL,*btmp=NULL,*etmp=NULL,*bio=NULL; 290*0Sstevel@tonic-gate unsigned char *tmp=NULL; 291*0Sstevel@tonic-gate X509_ALGOR *xa; 292*0Sstevel@tonic-gate ASN1_OCTET_STRING *data_body=NULL; 293*0Sstevel@tonic-gate const EVP_MD *evp_md; 294*0Sstevel@tonic-gate const EVP_CIPHER *evp_cipher=NULL; 295*0Sstevel@tonic-gate EVP_CIPHER_CTX *evp_ctx=NULL; 296*0Sstevel@tonic-gate X509_ALGOR *enc_alg=NULL; 297*0Sstevel@tonic-gate STACK_OF(X509_ALGOR) *md_sk=NULL; 298*0Sstevel@tonic-gate STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; 299*0Sstevel@tonic-gate X509_ALGOR *xalg=NULL; 300*0Sstevel@tonic-gate PKCS7_RECIP_INFO *ri=NULL; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate i=OBJ_obj2nid(p7->type); 303*0Sstevel@tonic-gate p7->state=PKCS7_S_HEADER; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate switch (i) 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate case NID_pkcs7_signed: 308*0Sstevel@tonic-gate data_body=PKCS7_get_octet_string(p7->d.sign->contents); 309*0Sstevel@tonic-gate md_sk=p7->d.sign->md_algs; 310*0Sstevel@tonic-gate break; 311*0Sstevel@tonic-gate case NID_pkcs7_signedAndEnveloped: 312*0Sstevel@tonic-gate rsk=p7->d.signed_and_enveloped->recipientinfo; 313*0Sstevel@tonic-gate md_sk=p7->d.signed_and_enveloped->md_algs; 314*0Sstevel@tonic-gate data_body=p7->d.signed_and_enveloped->enc_data->enc_data; 315*0Sstevel@tonic-gate enc_alg=p7->d.signed_and_enveloped->enc_data->algorithm; 316*0Sstevel@tonic-gate evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); 317*0Sstevel@tonic-gate if (evp_cipher == NULL) 318*0Sstevel@tonic-gate { 319*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); 320*0Sstevel@tonic-gate goto err; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate xalg=p7->d.signed_and_enveloped->enc_data->algorithm; 323*0Sstevel@tonic-gate break; 324*0Sstevel@tonic-gate case NID_pkcs7_enveloped: 325*0Sstevel@tonic-gate rsk=p7->d.enveloped->recipientinfo; 326*0Sstevel@tonic-gate enc_alg=p7->d.enveloped->enc_data->algorithm; 327*0Sstevel@tonic-gate data_body=p7->d.enveloped->enc_data->enc_data; 328*0Sstevel@tonic-gate evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); 329*0Sstevel@tonic-gate if (evp_cipher == NULL) 330*0Sstevel@tonic-gate { 331*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); 332*0Sstevel@tonic-gate goto err; 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate xalg=p7->d.enveloped->enc_data->algorithm; 335*0Sstevel@tonic-gate break; 336*0Sstevel@tonic-gate default: 337*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 338*0Sstevel@tonic-gate goto err; 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate /* We will be checking the signature */ 342*0Sstevel@tonic-gate if (md_sk != NULL) 343*0Sstevel@tonic-gate { 344*0Sstevel@tonic-gate for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) 345*0Sstevel@tonic-gate { 346*0Sstevel@tonic-gate xa=sk_X509_ALGOR_value(md_sk,i); 347*0Sstevel@tonic-gate if ((btmp=BIO_new(BIO_f_md())) == NULL) 348*0Sstevel@tonic-gate { 349*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); 350*0Sstevel@tonic-gate goto err; 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate j=OBJ_obj2nid(xa->algorithm); 354*0Sstevel@tonic-gate evp_md=EVP_get_digestbynid(j); 355*0Sstevel@tonic-gate if (evp_md == NULL) 356*0Sstevel@tonic-gate { 357*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNKNOWN_DIGEST_TYPE); 358*0Sstevel@tonic-gate goto err; 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate BIO_set_md(btmp,evp_md); 362*0Sstevel@tonic-gate if (out == NULL) 363*0Sstevel@tonic-gate out=btmp; 364*0Sstevel@tonic-gate else 365*0Sstevel@tonic-gate BIO_push(out,btmp); 366*0Sstevel@tonic-gate btmp=NULL; 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate if (evp_cipher != NULL) 371*0Sstevel@tonic-gate { 372*0Sstevel@tonic-gate #if 0 373*0Sstevel@tonic-gate unsigned char key[EVP_MAX_KEY_LENGTH]; 374*0Sstevel@tonic-gate unsigned char iv[EVP_MAX_IV_LENGTH]; 375*0Sstevel@tonic-gate unsigned char *p; 376*0Sstevel@tonic-gate int keylen,ivlen; 377*0Sstevel@tonic-gate int max; 378*0Sstevel@tonic-gate X509_OBJECT ret; 379*0Sstevel@tonic-gate #endif 380*0Sstevel@tonic-gate int jj; 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate if ((etmp=BIO_new(BIO_f_cipher())) == NULL) 383*0Sstevel@tonic-gate { 384*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); 385*0Sstevel@tonic-gate goto err; 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate /* It was encrypted, we need to decrypt the secret key 389*0Sstevel@tonic-gate * with the private key */ 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate /* Find the recipientInfo which matches the passed certificate 392*0Sstevel@tonic-gate * (if any) 393*0Sstevel@tonic-gate */ 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) { 396*0Sstevel@tonic-gate ri=sk_PKCS7_RECIP_INFO_value(rsk,i); 397*0Sstevel@tonic-gate if(!X509_NAME_cmp(ri->issuer_and_serial->issuer, 398*0Sstevel@tonic-gate pcert->cert_info->issuer) && 399*0Sstevel@tonic-gate !M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber, 400*0Sstevel@tonic-gate ri->issuer_and_serial->serial)) break; 401*0Sstevel@tonic-gate ri=NULL; 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate if (ri == NULL) { 404*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE, 405*0Sstevel@tonic-gate PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); 406*0Sstevel@tonic-gate goto err; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate jj=EVP_PKEY_size(pkey); 410*0Sstevel@tonic-gate tmp=(unsigned char *)OPENSSL_malloc(jj+10); 411*0Sstevel@tonic-gate if (tmp == NULL) 412*0Sstevel@tonic-gate { 413*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_MALLOC_FAILURE); 414*0Sstevel@tonic-gate goto err; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate jj=EVP_PKEY_decrypt(tmp, M_ASN1_STRING_data(ri->enc_key), 418*0Sstevel@tonic-gate M_ASN1_STRING_length(ri->enc_key), pkey); 419*0Sstevel@tonic-gate if (jj <= 0) 420*0Sstevel@tonic-gate { 421*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_EVP_LIB); 422*0Sstevel@tonic-gate goto err; 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate evp_ctx=NULL; 426*0Sstevel@tonic-gate BIO_get_cipher_ctx(etmp,&evp_ctx); 427*0Sstevel@tonic-gate EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0); 428*0Sstevel@tonic-gate if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0) 429*0Sstevel@tonic-gate goto err; 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate if (jj != EVP_CIPHER_CTX_key_length(evp_ctx)) { 432*0Sstevel@tonic-gate /* Some S/MIME clients don't use the same key 433*0Sstevel@tonic-gate * and effective key length. The key length is 434*0Sstevel@tonic-gate * determined by the size of the decrypted RSA key. 435*0Sstevel@tonic-gate */ 436*0Sstevel@tonic-gate if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, jj)) 437*0Sstevel@tonic-gate { 438*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATADECODE, 439*0Sstevel@tonic-gate PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH); 440*0Sstevel@tonic-gate goto err; 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate EVP_CipherInit_ex(evp_ctx,NULL,NULL,tmp,NULL,0); 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate OPENSSL_cleanse(tmp,jj); 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate if (out == NULL) 448*0Sstevel@tonic-gate out=etmp; 449*0Sstevel@tonic-gate else 450*0Sstevel@tonic-gate BIO_push(out,etmp); 451*0Sstevel@tonic-gate etmp=NULL; 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate #if 1 455*0Sstevel@tonic-gate if (PKCS7_is_detached(p7) || (in_bio != NULL)) 456*0Sstevel@tonic-gate { 457*0Sstevel@tonic-gate bio=in_bio; 458*0Sstevel@tonic-gate } 459*0Sstevel@tonic-gate else 460*0Sstevel@tonic-gate { 461*0Sstevel@tonic-gate #if 0 462*0Sstevel@tonic-gate bio=BIO_new(BIO_s_mem()); 463*0Sstevel@tonic-gate /* We need to set this so that when we have read all 464*0Sstevel@tonic-gate * the data, the encrypt BIO, if present, will read 465*0Sstevel@tonic-gate * EOF and encode the last few bytes */ 466*0Sstevel@tonic-gate BIO_set_mem_eof_return(bio,0); 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate if (data_body->length > 0) 469*0Sstevel@tonic-gate BIO_write(bio,(char *)data_body->data,data_body->length); 470*0Sstevel@tonic-gate #else 471*0Sstevel@tonic-gate if (data_body->length > 0) 472*0Sstevel@tonic-gate bio = BIO_new_mem_buf(data_body->data,data_body->length); 473*0Sstevel@tonic-gate else { 474*0Sstevel@tonic-gate bio=BIO_new(BIO_s_mem()); 475*0Sstevel@tonic-gate BIO_set_mem_eof_return(bio,0); 476*0Sstevel@tonic-gate } 477*0Sstevel@tonic-gate #endif 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate BIO_push(out,bio); 480*0Sstevel@tonic-gate bio=NULL; 481*0Sstevel@tonic-gate #endif 482*0Sstevel@tonic-gate if (0) 483*0Sstevel@tonic-gate { 484*0Sstevel@tonic-gate err: 485*0Sstevel@tonic-gate if (out != NULL) BIO_free_all(out); 486*0Sstevel@tonic-gate if (btmp != NULL) BIO_free_all(btmp); 487*0Sstevel@tonic-gate if (etmp != NULL) BIO_free_all(etmp); 488*0Sstevel@tonic-gate if (bio != NULL) BIO_free_all(bio); 489*0Sstevel@tonic-gate out=NULL; 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate if (tmp != NULL) 492*0Sstevel@tonic-gate OPENSSL_free(tmp); 493*0Sstevel@tonic-gate return(out); 494*0Sstevel@tonic-gate } 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) 497*0Sstevel@tonic-gate { 498*0Sstevel@tonic-gate int ret=0; 499*0Sstevel@tonic-gate int i,j; 500*0Sstevel@tonic-gate BIO *btmp; 501*0Sstevel@tonic-gate BUF_MEM *buf_mem=NULL; 502*0Sstevel@tonic-gate BUF_MEM *buf=NULL; 503*0Sstevel@tonic-gate PKCS7_SIGNER_INFO *si; 504*0Sstevel@tonic-gate EVP_MD_CTX *mdc,ctx_tmp; 505*0Sstevel@tonic-gate STACK_OF(X509_ATTRIBUTE) *sk; 506*0Sstevel@tonic-gate STACK_OF(PKCS7_SIGNER_INFO) *si_sk=NULL; 507*0Sstevel@tonic-gate ASN1_OCTET_STRING *os=NULL; 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gate EVP_MD_CTX_init(&ctx_tmp); 510*0Sstevel@tonic-gate i=OBJ_obj2nid(p7->type); 511*0Sstevel@tonic-gate p7->state=PKCS7_S_HEADER; 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate switch (i) 514*0Sstevel@tonic-gate { 515*0Sstevel@tonic-gate case NID_pkcs7_signedAndEnveloped: 516*0Sstevel@tonic-gate /* XXXXXXXXXXXXXXXX */ 517*0Sstevel@tonic-gate si_sk=p7->d.signed_and_enveloped->signer_info; 518*0Sstevel@tonic-gate os=M_ASN1_OCTET_STRING_new(); 519*0Sstevel@tonic-gate p7->d.signed_and_enveloped->enc_data->enc_data=os; 520*0Sstevel@tonic-gate break; 521*0Sstevel@tonic-gate case NID_pkcs7_enveloped: 522*0Sstevel@tonic-gate /* XXXXXXXXXXXXXXXX */ 523*0Sstevel@tonic-gate os=M_ASN1_OCTET_STRING_new(); 524*0Sstevel@tonic-gate p7->d.enveloped->enc_data->enc_data=os; 525*0Sstevel@tonic-gate break; 526*0Sstevel@tonic-gate case NID_pkcs7_signed: 527*0Sstevel@tonic-gate si_sk=p7->d.sign->signer_info; 528*0Sstevel@tonic-gate os=PKCS7_get_octet_string(p7->d.sign->contents); 529*0Sstevel@tonic-gate /* If detached data then the content is excluded */ 530*0Sstevel@tonic-gate if(PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { 531*0Sstevel@tonic-gate M_ASN1_OCTET_STRING_free(os); 532*0Sstevel@tonic-gate p7->d.sign->contents->d.data = NULL; 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate break; 535*0Sstevel@tonic-gate } 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate if (si_sk != NULL) 538*0Sstevel@tonic-gate { 539*0Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) 540*0Sstevel@tonic-gate { 541*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB); 542*0Sstevel@tonic-gate goto err; 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++) 545*0Sstevel@tonic-gate { 546*0Sstevel@tonic-gate si=sk_PKCS7_SIGNER_INFO_value(si_sk,i); 547*0Sstevel@tonic-gate if (si->pkey == NULL) continue; 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gate j=OBJ_obj2nid(si->digest_alg->algorithm); 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate btmp=bio; 552*0Sstevel@tonic-gate for (;;) 553*0Sstevel@tonic-gate { 554*0Sstevel@tonic-gate if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) 555*0Sstevel@tonic-gate == NULL) 556*0Sstevel@tonic-gate { 557*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); 558*0Sstevel@tonic-gate goto err; 559*0Sstevel@tonic-gate } 560*0Sstevel@tonic-gate BIO_get_md_ctx(btmp,&mdc); 561*0Sstevel@tonic-gate if (mdc == NULL) 562*0Sstevel@tonic-gate { 563*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_INTERNAL_ERROR); 564*0Sstevel@tonic-gate goto err; 565*0Sstevel@tonic-gate } 566*0Sstevel@tonic-gate if (EVP_MD_CTX_type(mdc) == j) 567*0Sstevel@tonic-gate break; 568*0Sstevel@tonic-gate else 569*0Sstevel@tonic-gate btmp=BIO_next(btmp); 570*0Sstevel@tonic-gate } 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate /* We now have the EVP_MD_CTX, lets do the 573*0Sstevel@tonic-gate * signing. */ 574*0Sstevel@tonic-gate EVP_MD_CTX_copy_ex(&ctx_tmp,mdc); 575*0Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,EVP_PKEY_size(si->pkey))) 576*0Sstevel@tonic-gate { 577*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB); 578*0Sstevel@tonic-gate goto err; 579*0Sstevel@tonic-gate } 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate sk=si->auth_attr; 582*0Sstevel@tonic-gate 583*0Sstevel@tonic-gate /* If there are attributes, we add the digest 584*0Sstevel@tonic-gate * attribute and only sign the attributes */ 585*0Sstevel@tonic-gate if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) 586*0Sstevel@tonic-gate { 587*0Sstevel@tonic-gate unsigned char md_data[EVP_MAX_MD_SIZE], *abuf=NULL; 588*0Sstevel@tonic-gate unsigned int md_len, alen; 589*0Sstevel@tonic-gate ASN1_OCTET_STRING *digest; 590*0Sstevel@tonic-gate ASN1_UTCTIME *sign_time; 591*0Sstevel@tonic-gate const EVP_MD *md_tmp; 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate /* Add signing time if not already present */ 594*0Sstevel@tonic-gate if (!PKCS7_get_signed_attribute(si, 595*0Sstevel@tonic-gate NID_pkcs9_signingTime)) 596*0Sstevel@tonic-gate { 597*0Sstevel@tonic-gate sign_time=X509_gmtime_adj(NULL,0); 598*0Sstevel@tonic-gate PKCS7_add_signed_attribute(si, 599*0Sstevel@tonic-gate NID_pkcs9_signingTime, 600*0Sstevel@tonic-gate V_ASN1_UTCTIME,sign_time); 601*0Sstevel@tonic-gate } 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate /* Add digest */ 604*0Sstevel@tonic-gate md_tmp=EVP_MD_CTX_md(&ctx_tmp); 605*0Sstevel@tonic-gate EVP_DigestFinal_ex(&ctx_tmp,md_data,&md_len); 606*0Sstevel@tonic-gate digest=M_ASN1_OCTET_STRING_new(); 607*0Sstevel@tonic-gate M_ASN1_OCTET_STRING_set(digest,md_data,md_len); 608*0Sstevel@tonic-gate PKCS7_add_signed_attribute(si, 609*0Sstevel@tonic-gate NID_pkcs9_messageDigest, 610*0Sstevel@tonic-gate V_ASN1_OCTET_STRING,digest); 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate /* Now sign the attributes */ 613*0Sstevel@tonic-gate EVP_SignInit_ex(&ctx_tmp,md_tmp,NULL); 614*0Sstevel@tonic-gate alen = ASN1_item_i2d((ASN1_VALUE *)sk,&abuf, 615*0Sstevel@tonic-gate ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); 616*0Sstevel@tonic-gate if(!abuf) goto err; 617*0Sstevel@tonic-gate EVP_SignUpdate(&ctx_tmp,abuf,alen); 618*0Sstevel@tonic-gate OPENSSL_free(abuf); 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 622*0Sstevel@tonic-gate if (si->pkey->type == EVP_PKEY_DSA) 623*0Sstevel@tonic-gate ctx_tmp.digest=EVP_dss1(); 624*0Sstevel@tonic-gate #endif 625*0Sstevel@tonic-gate 626*0Sstevel@tonic-gate if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data, 627*0Sstevel@tonic-gate (unsigned int *)&buf->length,si->pkey)) 628*0Sstevel@tonic-gate { 629*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_EVP_LIB); 630*0Sstevel@tonic-gate goto err; 631*0Sstevel@tonic-gate } 632*0Sstevel@tonic-gate if (!ASN1_STRING_set(si->enc_digest, 633*0Sstevel@tonic-gate (unsigned char *)buf->data,buf->length)) 634*0Sstevel@tonic-gate { 635*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_ASN1_LIB); 636*0Sstevel@tonic-gate goto err; 637*0Sstevel@tonic-gate } 638*0Sstevel@tonic-gate } 639*0Sstevel@tonic-gate } 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate if (!PKCS7_is_detached(p7)) 642*0Sstevel@tonic-gate { 643*0Sstevel@tonic-gate btmp=BIO_find_type(bio,BIO_TYPE_MEM); 644*0Sstevel@tonic-gate if (btmp == NULL) 645*0Sstevel@tonic-gate { 646*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MEM_BIO); 647*0Sstevel@tonic-gate goto err; 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate BIO_get_mem_ptr(btmp,&buf_mem); 650*0Sstevel@tonic-gate /* Mark the BIO read only then we can use its copy of the data 651*0Sstevel@tonic-gate * instead of making an extra copy. 652*0Sstevel@tonic-gate */ 653*0Sstevel@tonic-gate BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); 654*0Sstevel@tonic-gate BIO_set_mem_eof_return(btmp, 0); 655*0Sstevel@tonic-gate os->data = (unsigned char *)buf_mem->data; 656*0Sstevel@tonic-gate os->length = buf_mem->length; 657*0Sstevel@tonic-gate #if 0 658*0Sstevel@tonic-gate M_ASN1_OCTET_STRING_set(os, 659*0Sstevel@tonic-gate (unsigned char *)buf_mem->data,buf_mem->length); 660*0Sstevel@tonic-gate #endif 661*0Sstevel@tonic-gate } 662*0Sstevel@tonic-gate ret=1; 663*0Sstevel@tonic-gate err: 664*0Sstevel@tonic-gate EVP_MD_CTX_cleanup(&ctx_tmp); 665*0Sstevel@tonic-gate if (buf != NULL) BUF_MEM_free(buf); 666*0Sstevel@tonic-gate return(ret); 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gate int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, 670*0Sstevel@tonic-gate PKCS7 *p7, PKCS7_SIGNER_INFO *si) 671*0Sstevel@tonic-gate { 672*0Sstevel@tonic-gate PKCS7_ISSUER_AND_SERIAL *ias; 673*0Sstevel@tonic-gate int ret=0,i; 674*0Sstevel@tonic-gate STACK_OF(X509) *cert; 675*0Sstevel@tonic-gate X509 *x509; 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gate if (PKCS7_type_is_signed(p7)) 678*0Sstevel@tonic-gate { 679*0Sstevel@tonic-gate cert=p7->d.sign->cert; 680*0Sstevel@tonic-gate } 681*0Sstevel@tonic-gate else if (PKCS7_type_is_signedAndEnveloped(p7)) 682*0Sstevel@tonic-gate { 683*0Sstevel@tonic-gate cert=p7->d.signed_and_enveloped->cert; 684*0Sstevel@tonic-gate } 685*0Sstevel@tonic-gate else 686*0Sstevel@tonic-gate { 687*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_WRONG_PKCS7_TYPE); 688*0Sstevel@tonic-gate goto err; 689*0Sstevel@tonic-gate } 690*0Sstevel@tonic-gate /* XXXXXXXXXXXXXXXXXXXXXXX */ 691*0Sstevel@tonic-gate ias=si->issuer_and_serial; 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate x509=X509_find_by_issuer_and_serial(cert,ias->issuer,ias->serial); 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate /* were we able to find the cert in passed to us */ 696*0Sstevel@tonic-gate if (x509 == NULL) 697*0Sstevel@tonic-gate { 698*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); 699*0Sstevel@tonic-gate goto err; 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gate /* Lets verify */ 703*0Sstevel@tonic-gate if(!X509_STORE_CTX_init(ctx,cert_store,x509,cert)) 704*0Sstevel@tonic-gate { 705*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); 706*0Sstevel@tonic-gate goto err; 707*0Sstevel@tonic-gate } 708*0Sstevel@tonic-gate X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); 709*0Sstevel@tonic-gate i=X509_verify_cert(ctx); 710*0Sstevel@tonic-gate if (i <= 0) 711*0Sstevel@tonic-gate { 712*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); 713*0Sstevel@tonic-gate X509_STORE_CTX_cleanup(ctx); 714*0Sstevel@tonic-gate goto err; 715*0Sstevel@tonic-gate } 716*0Sstevel@tonic-gate X509_STORE_CTX_cleanup(ctx); 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate return PKCS7_signatureVerify(bio, p7, si, x509); 719*0Sstevel@tonic-gate err: 720*0Sstevel@tonic-gate return ret; 721*0Sstevel@tonic-gate } 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gate int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, 724*0Sstevel@tonic-gate X509 *x509) 725*0Sstevel@tonic-gate { 726*0Sstevel@tonic-gate ASN1_OCTET_STRING *os; 727*0Sstevel@tonic-gate EVP_MD_CTX mdc_tmp,*mdc; 728*0Sstevel@tonic-gate int ret=0,i; 729*0Sstevel@tonic-gate int md_type; 730*0Sstevel@tonic-gate STACK_OF(X509_ATTRIBUTE) *sk; 731*0Sstevel@tonic-gate BIO *btmp; 732*0Sstevel@tonic-gate EVP_PKEY *pkey; 733*0Sstevel@tonic-gate 734*0Sstevel@tonic-gate EVP_MD_CTX_init(&mdc_tmp); 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate if (!PKCS7_type_is_signed(p7) && 737*0Sstevel@tonic-gate !PKCS7_type_is_signedAndEnveloped(p7)) { 738*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 739*0Sstevel@tonic-gate PKCS7_R_WRONG_PKCS7_TYPE); 740*0Sstevel@tonic-gate goto err; 741*0Sstevel@tonic-gate } 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate md_type=OBJ_obj2nid(si->digest_alg->algorithm); 744*0Sstevel@tonic-gate 745*0Sstevel@tonic-gate btmp=bio; 746*0Sstevel@tonic-gate for (;;) 747*0Sstevel@tonic-gate { 748*0Sstevel@tonic-gate if ((btmp == NULL) || 749*0Sstevel@tonic-gate ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL)) 750*0Sstevel@tonic-gate { 751*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 752*0Sstevel@tonic-gate PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); 753*0Sstevel@tonic-gate goto err; 754*0Sstevel@tonic-gate } 755*0Sstevel@tonic-gate BIO_get_md_ctx(btmp,&mdc); 756*0Sstevel@tonic-gate if (mdc == NULL) 757*0Sstevel@tonic-gate { 758*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 759*0Sstevel@tonic-gate ERR_R_INTERNAL_ERROR); 760*0Sstevel@tonic-gate goto err; 761*0Sstevel@tonic-gate } 762*0Sstevel@tonic-gate if (EVP_MD_CTX_type(mdc) == md_type) 763*0Sstevel@tonic-gate break; 764*0Sstevel@tonic-gate /* Workaround for some broken clients that put the signature 765*0Sstevel@tonic-gate * OID instead of the digest OID in digest_alg->algorithm 766*0Sstevel@tonic-gate */ 767*0Sstevel@tonic-gate if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) 768*0Sstevel@tonic-gate break; 769*0Sstevel@tonic-gate btmp=BIO_next(btmp); 770*0Sstevel@tonic-gate } 771*0Sstevel@tonic-gate 772*0Sstevel@tonic-gate /* mdc is the digest ctx that we want, unless there are attributes, 773*0Sstevel@tonic-gate * in which case the digest is the signed attributes */ 774*0Sstevel@tonic-gate EVP_MD_CTX_copy_ex(&mdc_tmp,mdc); 775*0Sstevel@tonic-gate 776*0Sstevel@tonic-gate sk=si->auth_attr; 777*0Sstevel@tonic-gate if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) 778*0Sstevel@tonic-gate { 779*0Sstevel@tonic-gate unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; 780*0Sstevel@tonic-gate unsigned int md_len, alen; 781*0Sstevel@tonic-gate ASN1_OCTET_STRING *message_digest; 782*0Sstevel@tonic-gate 783*0Sstevel@tonic-gate EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len); 784*0Sstevel@tonic-gate message_digest=PKCS7_digest_from_attributes(sk); 785*0Sstevel@tonic-gate if (!message_digest) 786*0Sstevel@tonic-gate { 787*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 788*0Sstevel@tonic-gate PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); 789*0Sstevel@tonic-gate goto err; 790*0Sstevel@tonic-gate } 791*0Sstevel@tonic-gate if ((message_digest->length != (int)md_len) || 792*0Sstevel@tonic-gate (memcmp(message_digest->data,md_dat,md_len))) 793*0Sstevel@tonic-gate { 794*0Sstevel@tonic-gate #if 0 795*0Sstevel@tonic-gate { 796*0Sstevel@tonic-gate int ii; 797*0Sstevel@tonic-gate for (ii=0; ii<message_digest->length; ii++) 798*0Sstevel@tonic-gate printf("%02X",message_digest->data[ii]); printf(" sent\n"); 799*0Sstevel@tonic-gate for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n"); 800*0Sstevel@tonic-gate } 801*0Sstevel@tonic-gate #endif 802*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 803*0Sstevel@tonic-gate PKCS7_R_DIGEST_FAILURE); 804*0Sstevel@tonic-gate ret= -1; 805*0Sstevel@tonic-gate goto err; 806*0Sstevel@tonic-gate } 807*0Sstevel@tonic-gate 808*0Sstevel@tonic-gate EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL); 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gate alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, 811*0Sstevel@tonic-gate ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); 812*0Sstevel@tonic-gate EVP_VerifyUpdate(&mdc_tmp, abuf, alen); 813*0Sstevel@tonic-gate 814*0Sstevel@tonic-gate OPENSSL_free(abuf); 815*0Sstevel@tonic-gate } 816*0Sstevel@tonic-gate 817*0Sstevel@tonic-gate os=si->enc_digest; 818*0Sstevel@tonic-gate pkey = X509_get_pubkey(x509); 819*0Sstevel@tonic-gate if (!pkey) 820*0Sstevel@tonic-gate { 821*0Sstevel@tonic-gate ret = -1; 822*0Sstevel@tonic-gate goto err; 823*0Sstevel@tonic-gate } 824*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 825*0Sstevel@tonic-gate if(pkey->type == EVP_PKEY_DSA) mdc_tmp.digest=EVP_dss1(); 826*0Sstevel@tonic-gate #endif 827*0Sstevel@tonic-gate 828*0Sstevel@tonic-gate i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length, pkey); 829*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 830*0Sstevel@tonic-gate if (i <= 0) 831*0Sstevel@tonic-gate { 832*0Sstevel@tonic-gate PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, 833*0Sstevel@tonic-gate PKCS7_R_SIGNATURE_FAILURE); 834*0Sstevel@tonic-gate ret= -1; 835*0Sstevel@tonic-gate goto err; 836*0Sstevel@tonic-gate } 837*0Sstevel@tonic-gate else 838*0Sstevel@tonic-gate ret=1; 839*0Sstevel@tonic-gate err: 840*0Sstevel@tonic-gate EVP_MD_CTX_cleanup(&mdc_tmp); 841*0Sstevel@tonic-gate return(ret); 842*0Sstevel@tonic-gate } 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) 845*0Sstevel@tonic-gate { 846*0Sstevel@tonic-gate STACK_OF(PKCS7_RECIP_INFO) *rsk; 847*0Sstevel@tonic-gate PKCS7_RECIP_INFO *ri; 848*0Sstevel@tonic-gate int i; 849*0Sstevel@tonic-gate 850*0Sstevel@tonic-gate i=OBJ_obj2nid(p7->type); 851*0Sstevel@tonic-gate if (i != NID_pkcs7_signedAndEnveloped) return(NULL); 852*0Sstevel@tonic-gate rsk=p7->d.signed_and_enveloped->recipientinfo; 853*0Sstevel@tonic-gate ri=sk_PKCS7_RECIP_INFO_value(rsk,0); 854*0Sstevel@tonic-gate if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); 855*0Sstevel@tonic-gate ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); 856*0Sstevel@tonic-gate return(ri->issuer_and_serial); 857*0Sstevel@tonic-gate } 858*0Sstevel@tonic-gate 859*0Sstevel@tonic-gate ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) 860*0Sstevel@tonic-gate { 861*0Sstevel@tonic-gate return(get_attribute(si->auth_attr,nid)); 862*0Sstevel@tonic-gate } 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gate ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) 865*0Sstevel@tonic-gate { 866*0Sstevel@tonic-gate return(get_attribute(si->unauth_attr,nid)); 867*0Sstevel@tonic-gate } 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) 870*0Sstevel@tonic-gate { 871*0Sstevel@tonic-gate int i; 872*0Sstevel@tonic-gate X509_ATTRIBUTE *xa; 873*0Sstevel@tonic-gate ASN1_OBJECT *o; 874*0Sstevel@tonic-gate 875*0Sstevel@tonic-gate o=OBJ_nid2obj(nid); 876*0Sstevel@tonic-gate if (!o || !sk) return(NULL); 877*0Sstevel@tonic-gate for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) 878*0Sstevel@tonic-gate { 879*0Sstevel@tonic-gate xa=sk_X509_ATTRIBUTE_value(sk,i); 880*0Sstevel@tonic-gate if (OBJ_cmp(xa->object,o) == 0) 881*0Sstevel@tonic-gate { 882*0Sstevel@tonic-gate if (!xa->single && sk_ASN1_TYPE_num(xa->value.set)) 883*0Sstevel@tonic-gate return(sk_ASN1_TYPE_value(xa->value.set,0)); 884*0Sstevel@tonic-gate else 885*0Sstevel@tonic-gate return(NULL); 886*0Sstevel@tonic-gate } 887*0Sstevel@tonic-gate } 888*0Sstevel@tonic-gate return(NULL); 889*0Sstevel@tonic-gate } 890*0Sstevel@tonic-gate 891*0Sstevel@tonic-gate ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) 892*0Sstevel@tonic-gate { 893*0Sstevel@tonic-gate ASN1_TYPE *astype; 894*0Sstevel@tonic-gate if(!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) return NULL; 895*0Sstevel@tonic-gate return astype->value.octet_string; 896*0Sstevel@tonic-gate } 897*0Sstevel@tonic-gate 898*0Sstevel@tonic-gate int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, 899*0Sstevel@tonic-gate STACK_OF(X509_ATTRIBUTE) *sk) 900*0Sstevel@tonic-gate { 901*0Sstevel@tonic-gate int i; 902*0Sstevel@tonic-gate 903*0Sstevel@tonic-gate if (p7si->auth_attr != NULL) 904*0Sstevel@tonic-gate sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free); 905*0Sstevel@tonic-gate p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk); 906*0Sstevel@tonic-gate for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) 907*0Sstevel@tonic-gate { 908*0Sstevel@tonic-gate if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i, 909*0Sstevel@tonic-gate X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) 910*0Sstevel@tonic-gate == NULL) 911*0Sstevel@tonic-gate return(0); 912*0Sstevel@tonic-gate } 913*0Sstevel@tonic-gate return(1); 914*0Sstevel@tonic-gate } 915*0Sstevel@tonic-gate 916*0Sstevel@tonic-gate int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) 917*0Sstevel@tonic-gate { 918*0Sstevel@tonic-gate int i; 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate if (p7si->unauth_attr != NULL) 921*0Sstevel@tonic-gate sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, 922*0Sstevel@tonic-gate X509_ATTRIBUTE_free); 923*0Sstevel@tonic-gate p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk); 924*0Sstevel@tonic-gate for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) 925*0Sstevel@tonic-gate { 926*0Sstevel@tonic-gate if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i, 927*0Sstevel@tonic-gate X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) 928*0Sstevel@tonic-gate == NULL) 929*0Sstevel@tonic-gate return(0); 930*0Sstevel@tonic-gate } 931*0Sstevel@tonic-gate return(1); 932*0Sstevel@tonic-gate } 933*0Sstevel@tonic-gate 934*0Sstevel@tonic-gate int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, 935*0Sstevel@tonic-gate void *value) 936*0Sstevel@tonic-gate { 937*0Sstevel@tonic-gate return(add_attribute(&(p7si->auth_attr),nid,atrtype,value)); 938*0Sstevel@tonic-gate } 939*0Sstevel@tonic-gate 940*0Sstevel@tonic-gate int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, 941*0Sstevel@tonic-gate void *value) 942*0Sstevel@tonic-gate { 943*0Sstevel@tonic-gate return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value)); 944*0Sstevel@tonic-gate } 945*0Sstevel@tonic-gate 946*0Sstevel@tonic-gate static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, 947*0Sstevel@tonic-gate void *value) 948*0Sstevel@tonic-gate { 949*0Sstevel@tonic-gate X509_ATTRIBUTE *attr=NULL; 950*0Sstevel@tonic-gate 951*0Sstevel@tonic-gate if (*sk == NULL) 952*0Sstevel@tonic-gate { 953*0Sstevel@tonic-gate *sk = sk_X509_ATTRIBUTE_new_null(); 954*0Sstevel@tonic-gate new_attrib: 955*0Sstevel@tonic-gate attr=X509_ATTRIBUTE_create(nid,atrtype,value); 956*0Sstevel@tonic-gate sk_X509_ATTRIBUTE_push(*sk,attr); 957*0Sstevel@tonic-gate } 958*0Sstevel@tonic-gate else 959*0Sstevel@tonic-gate { 960*0Sstevel@tonic-gate int i; 961*0Sstevel@tonic-gate 962*0Sstevel@tonic-gate for (i=0; i<sk_X509_ATTRIBUTE_num(*sk); i++) 963*0Sstevel@tonic-gate { 964*0Sstevel@tonic-gate attr=sk_X509_ATTRIBUTE_value(*sk,i); 965*0Sstevel@tonic-gate if (OBJ_obj2nid(attr->object) == nid) 966*0Sstevel@tonic-gate { 967*0Sstevel@tonic-gate X509_ATTRIBUTE_free(attr); 968*0Sstevel@tonic-gate attr=X509_ATTRIBUTE_create(nid,atrtype,value); 969*0Sstevel@tonic-gate sk_X509_ATTRIBUTE_set(*sk,i,attr); 970*0Sstevel@tonic-gate goto end; 971*0Sstevel@tonic-gate } 972*0Sstevel@tonic-gate } 973*0Sstevel@tonic-gate goto new_attrib; 974*0Sstevel@tonic-gate } 975*0Sstevel@tonic-gate end: 976*0Sstevel@tonic-gate return(1); 977*0Sstevel@tonic-gate } 978*0Sstevel@tonic-gate 979