]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs7/pk7_doit.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / pkcs7 / pk7_doit.c
1 /* crypto/pkcs7/pk7_doit.c */
2 /* Copyright (C) 1995-1997 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 "cryptlib.h"
61 #include "objects.h"
62 #include "x509.h"
63
64 BIO *PKCS7_dataInit(p7,bio)
65 PKCS7 *p7;
66 BIO *bio;
67 {
68 int i,j;
69 BIO *out=NULL,*btmp;
70 X509_ALGOR *xa;
71 EVP_MD *evp_md;
72
73 i=OBJ_obj2nid(p7->type);
74 p7->state=PKCS7_S_HEADER;
75
76 switch (i)
77 {
78 case NID_pkcs7_signed:
79 for (i=0; i<sk_num(p7->d.sign->md_algs); i++)
80 {
81 xa=(X509_ALGOR *)sk_value(p7->d.sign->md_algs,i);
82 if ((btmp=BIO_new(BIO_f_md())) == NULL) goto err;
83
84 j=OBJ_obj2nid(xa->algorithm);
85 evp_md=EVP_get_digestbyname(OBJ_nid2sn(j));
86 if (evp_md == NULL)
87 {
88 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE);
89 goto err;
90 }
91
92 BIO_set_md(btmp,evp_md);
93 if (out == NULL)
94 out=btmp;
95 else
96 BIO_push(out,btmp);
97 }
98 break;
99 default:
100 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
101 goto err;
102 }
103 if (bio == NULL)
104 {
105 if (p7->detached)
106 bio=BIO_new(BIO_s_null());
107 else
108 {
109 bio=BIO_new(BIO_s_mem());
110 if (PKCS7_type_is_signed(p7) &&
111 PKCS7_type_is_data(p7->d.sign->contents))
112 {
113 ASN1_OCTET_STRING *os;
114
115 os=p7->d.sign->contents->d.data;
116 if (os->length > 0)
117 BIO_write(bio,os->data,os->length);
118 }
119 }
120 }
121 BIO_push(out,bio);
122 return(out);
123 err:
124 return(NULL);
125 }
126
127 int PKCS7_dataSign(p7,bio)
128 PKCS7 *p7;
129 BIO *bio;
130 {
131 int ret=0;
132 int i,j;
133 BIO *btmp;
134 BUF_MEM *buf_mem=NULL;
135 BUF_MEM *buf=NULL;
136 PKCS7_SIGNER_INFO *si;
137 EVP_MD_CTX *mdc,ctx_tmp;
138 STACK *sk;
139 unsigned char *p,*pp=NULL;
140 int x;
141
142 i=OBJ_obj2nid(p7->type);
143 p7->state=PKCS7_S_HEADER;
144
145 switch (i)
146 {
147 case NID_pkcs7_signed:
148
149 if ((buf=BUF_MEM_new()) == NULL) goto err;
150 for (i=0; i<sk_num(p7->d.sign->signer_info); i++)
151 {
152 si=(PKCS7_SIGNER_INFO *)
153 sk_value(p7->d.sign->signer_info,i);
154 if (si->pkey == NULL)
155 continue;
156 j=OBJ_obj2nid(si->digest_enc_alg->algorithm);
157
158 btmp=bio;
159 for (;;)
160 {
161 if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD))
162 == NULL)
163 {
164 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
165 goto err;
166 }
167 BIO_get_md_ctx(btmp,&mdc);
168 if (mdc == NULL)
169 {
170 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_INTERNAL_ERROR);
171 goto err;
172 }
173 if (EVP_MD_pkey_type(EVP_MD_CTX_type(mdc)) == j)
174 break;
175 else
176 btmp=btmp->next_bio;
177 }
178
179 /* We now have the EVP_MD_CTX, lets do the
180 * signing. */
181 memcpy(&ctx_tmp,mdc,sizeof(ctx_tmp));
182 if (!BUF_MEM_grow(buf,EVP_PKEY_size(si->pkey)))
183 goto err;
184
185 sk=si->auth_attr;
186 if ((sk != NULL) && (sk_num(sk) != 0))
187 {
188 x=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
189 V_ASN1_SET,V_ASN1_UNIVERSAL);
190 pp=(unsigned char *)Malloc(i);
191 p=pp;
192 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
193 V_ASN1_SET,V_ASN1_UNIVERSAL);
194 EVP_SignUpdate(&ctx_tmp,pp,x);
195 Free(pp);
196 }
197
198 if (!EVP_SignFinal(&ctx_tmp,buf->data,
199 (unsigned int *)&buf->length,si->pkey))
200 goto err;
201 if (!ASN1_STRING_set(si->enc_digest,
202 (unsigned char *)buf->data,buf->length))
203 goto err;
204
205 }
206 if (p7->detached)
207 PKCS7_content_free(p7->d.sign->contents);
208 else
209 {
210 btmp=BIO_find_type(bio,BIO_TYPE_MEM);
211 if (btmp == NULL)
212 {
213 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
214 goto err;
215 }
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);
219 }
220 if (pp != NULL) Free(pp);
221 pp=NULL;
222 break;
223 default:
224 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
225 goto err;
226 }
227
228 if (p7->detached)
229 {
230
231 }
232 ret=1;
233 err:
234 if (buf != NULL) BUF_MEM_free(buf);
235 return(ret);
236 }
237
238 int PKCS7_dataVerify(cert_store,ctx,bio,p7,si)
239 X509_STORE *cert_store;
240 X509_STORE_CTX *ctx;
241 BIO *bio;
242 PKCS7 *p7;
243 PKCS7_SIGNER_INFO *si;
244 {
245 PKCS7_SIGNED *s;
246 ASN1_OCTET_STRING *os;
247 EVP_MD_CTX mdc_tmp,*mdc;
248 unsigned char *pp,*p;
249 PKCS7_ISSUER_AND_SERIAL *ias;
250 int ret=0,md_type,i;
251 STACK *sk;
252 BIO *btmp;
253 X509 *x509;
254
255 if (!PKCS7_type_is_signed(p7)) abort();
256 ias=si->issuer_and_serial;
257 s=p7->d.sign;
258
259 x509=X509_find_by_issuer_and_serial(s->cert,ias->issuer,ias->serial);
260
261 /* were we able to find the cert in passed to us */
262 if (x509 == NULL)
263 {
264 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
265 goto err;
266 }
267
268 /* Lets verify */
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);
273
274 /* So we like 'x509', lets check the signature. */
275 md_type=OBJ_obj2nid(si->digest_alg->algorithm);
276
277 btmp=bio;
278 for (;;)
279 {
280 if ((btmp == NULL) ||
281 ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL))
282 {
283 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
284 goto err;
285 }
286 BIO_get_md_ctx(btmp,&mdc);
287 if (mdc == NULL)
288 {
289 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_INTERNAL_ERROR);
290 goto err;
291 }
292 if (EVP_MD_type(EVP_MD_CTX_type(mdc)) == md_type)
293 break;
294 btmp=btmp->next_bio;
295 }
296
297 /* mdc is the digest ctx that we want */
298 memcpy(&mdc_tmp,mdc,sizeof(mdc_tmp));
299
300 sk=si->auth_attr;
301 if ((sk != NULL) && (sk_num(sk) != 0))
302 {
303 i=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
304 V_ASN1_SET,V_ASN1_UNIVERSAL);
305 pp=(unsigned char *)malloc(i);
306 p=pp;
307 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
308 V_ASN1_SET,V_ASN1_UNIVERSAL);
309 EVP_VerifyUpdate(&mdc_tmp,pp,i);
310 free(pp);
311 }
312
313 os=si->enc_digest;
314 i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length,
315 X509_get_pubkey(x509));
316 if (i <= 0)
317 {
318 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_SIGNATURE_FAILURE);
319 ret= -1;
320 goto err;
321 }
322 else
323 ret=1;
324 err:
325 return(ret);
326 }
327