1 /* crypto/pkcs7/pk7_doit.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
64 BIO *PKCS7_dataInit(p7,bio)
73 i=OBJ_obj2nid(p7->type);
74 p7->state=PKCS7_S_HEADER;
78 case NID_pkcs7_signed:
79 for (i=0; i<sk_num(p7->d.sign->md_algs); i++)
81 xa=(X509_ALGOR *)sk_value(p7->d.sign->md_algs,i);
82 if ((btmp=BIO_new(BIO_f_md())) == NULL) goto err;
84 j=OBJ_obj2nid(xa->algorithm);
85 evp_md=EVP_get_digestbyname(OBJ_nid2sn(j));
88 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE);
92 BIO_set_md(btmp,evp_md);
100 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
106 bio=BIO_new(BIO_s_null());
109 bio=BIO_new(BIO_s_mem());
110 if (PKCS7_type_is_signed(p7) &&
111 PKCS7_type_is_data(p7->d.sign->contents))
113 ASN1_OCTET_STRING *os;
115 os=p7->d.sign->contents->d.data;
117 BIO_write(bio,os->data,os->length);
127 int PKCS7_dataSign(p7,bio)
134 BUF_MEM *buf_mem=NULL;
136 PKCS7_SIGNER_INFO *si;
137 EVP_MD_CTX *mdc,ctx_tmp;
139 unsigned char *p,*pp=NULL;
142 i=OBJ_obj2nid(p7->type);
143 p7->state=PKCS7_S_HEADER;
147 case NID_pkcs7_signed:
149 if ((buf=BUF_MEM_new()) == NULL) goto err;
150 for (i=0; i<sk_num(p7->d.sign->signer_info); i++)
152 si=(PKCS7_SIGNER_INFO *)
153 sk_value(p7->d.sign->signer_info,i);
154 if (si->pkey == NULL)
156 j=OBJ_obj2nid(si->digest_enc_alg->algorithm);
161 if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD))
164 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
167 BIO_get_md_ctx(btmp,&mdc);
170 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_INTERNAL_ERROR);
173 if (EVP_MD_pkey_type(EVP_MD_CTX_type(mdc)) == j)
179 /* We now have the EVP_MD_CTX, lets do the
181 memcpy(&ctx_tmp,mdc,sizeof(ctx_tmp));
182 if (!BUF_MEM_grow(buf,EVP_PKEY_size(si->pkey)))
186 if ((sk != NULL) && (sk_num(sk) != 0))
188 x=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
189 V_ASN1_SET,V_ASN1_UNIVERSAL);
190 pp=(unsigned char *)Malloc(i);
192 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
193 V_ASN1_SET,V_ASN1_UNIVERSAL);
194 EVP_SignUpdate(&ctx_tmp,pp,x);
198 if (!EVP_SignFinal(&ctx_tmp,buf->data,
199 (unsigned int *)&buf->length,si->pkey))
201 if (!ASN1_STRING_set(si->enc_digest,
202 (unsigned char *)buf->data,buf->length))
207 PKCS7_content_free(p7->d.sign->contents);
210 btmp=BIO_find_type(bio,BIO_TYPE_MEM);
213 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
216 BIO_get_mem_ptr(btmp,&buf_mem);
217 ASN1_OCTET_STRING_set(p7->d.sign->contents->d.data,
218 (unsigned char *)buf_mem->data,buf_mem->length);
220 if (pp != NULL) Free(pp);
224 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
234 if (buf != NULL) BUF_MEM_free(buf);
238 int PKCS7_dataVerify(cert_store,ctx,bio,p7,si)
239 X509_STORE *cert_store;
243 PKCS7_SIGNER_INFO *si;
246 ASN1_OCTET_STRING *os;
247 EVP_MD_CTX mdc_tmp,*mdc;
248 unsigned char *pp,*p;
249 PKCS7_ISSUER_AND_SERIAL *ias;
255 if (!PKCS7_type_is_signed(p7)) abort();
256 ias=si->issuer_and_serial;
259 x509=X509_find_by_issuer_and_serial(s->cert,ias->issuer,ias->serial);
261 /* were we able to find the cert in passed to us */
264 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
269 X509_STORE_CTX_init(ctx,cert_store,x509,s->cert);
270 i=X509_verify_cert(ctx);
271 if (i <= 0) goto err;
272 X509_STORE_CTX_cleanup(ctx);
274 /* So we like 'x509', lets check the signature. */
275 md_type=OBJ_obj2nid(si->digest_alg->algorithm);
280 if ((btmp == NULL) ||
281 ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL))
283 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
286 BIO_get_md_ctx(btmp,&mdc);
289 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_INTERNAL_ERROR);
292 if (EVP_MD_type(EVP_MD_CTX_type(mdc)) == md_type)
297 /* mdc is the digest ctx that we want */
298 memcpy(&mdc_tmp,mdc,sizeof(mdc_tmp));
301 if ((sk != NULL) && (sk_num(sk) != 0))
303 i=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
304 V_ASN1_SET,V_ASN1_UNIVERSAL);
305 pp=(unsigned char *)malloc(i);
307 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
308 V_ASN1_SET,V_ASN1_UNIVERSAL);
309 EVP_VerifyUpdate(&mdc_tmp,pp,i);
314 i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length,
315 X509_get_pubkey(x509));
318 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_SIGNATURE_FAILURE);