summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pkcs7/pk7_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/pkcs7/pk7_lib.c')
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_lib.c656
1 files changed, 327 insertions, 329 deletions
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c
index 5d7d0b18a0..9eed9fc74a 100644
--- a/src/lib/libcrypto/pkcs7/pk7_lib.c
+++ b/src/lib/libcrypto/pkcs7/pk7_lib.c
@@ -1,25 +1,25 @@
1/* $OpenBSD: pk7_lib.c,v 1.10 2014/06/12 15:49:30 deraadt Exp $ */ 1/* $OpenBSD: pk7_lib.c,v 1.11 2014/06/29 17:05:36 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -62,599 +62,597 @@
62#include <openssl/x509.h> 62#include <openssl/x509.h>
63#include "asn1_locl.h" 63#include "asn1_locl.h"
64 64
65long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg) 65long
66 { 66PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
67{
67 int nid; 68 int nid;
68 long ret; 69 long ret;
69 70
70 nid=OBJ_obj2nid(p7->type); 71 nid = OBJ_obj2nid(p7->type);
71 72
72 switch (cmd) 73 switch (cmd) {
73 {
74 case PKCS7_OP_SET_DETACHED_SIGNATURE: 74 case PKCS7_OP_SET_DETACHED_SIGNATURE:
75 if (nid == NID_pkcs7_signed) 75 if (nid == NID_pkcs7_signed) {
76 { 76 ret = p7->detached = (int)larg;
77 ret=p7->detached=(int)larg; 77 if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
78 if (ret && PKCS7_type_is_data(p7->d.sign->contents)) 78 ASN1_OCTET_STRING *os;
79 { 79 os = p7->d.sign->contents->d.data;
80 ASN1_OCTET_STRING *os; 80 ASN1_OCTET_STRING_free(os);
81 os=p7->d.sign->contents->d.data; 81 p7->d.sign->contents->d.data = NULL;
82 ASN1_OCTET_STRING_free(os);
83 p7->d.sign->contents->d.data = NULL;
84 }
85 }
86 else
87 {
88 PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
89 ret=0;
90 } 82 }
83 } else {
84 PKCS7err(PKCS7_F_PKCS7_CTRL,
85 PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
86 ret = 0;
87 }
91 break; 88 break;
92 case PKCS7_OP_GET_DETACHED_SIGNATURE: 89 case PKCS7_OP_GET_DETACHED_SIGNATURE:
93 if (nid == NID_pkcs7_signed) 90 if (nid == NID_pkcs7_signed) {
94 { 91 if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
95 if(!p7->d.sign || !p7->d.sign->contents->d.ptr)
96 ret = 1; 92 ret = 1;
97 else ret = 0; 93 else
98 94 ret = 0;
95
99 p7->detached = ret; 96 p7->detached = ret;
100 } 97 } else {
101 else 98 PKCS7err(PKCS7_F_PKCS7_CTRL,
102 { 99 PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
103 PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); 100 ret = 0;
104 ret=0; 101 }
105 } 102
106
107 break; 103 break;
108 default: 104 default:
109 PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION); 105 PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
110 ret=0; 106 ret = 0;
111 }
112 return(ret);
113 } 107 }
108 return (ret);
109}
114 110
115int PKCS7_content_new(PKCS7 *p7, int type) 111int
116 { 112PKCS7_content_new(PKCS7 *p7, int type)
117 PKCS7 *ret=NULL; 113{
114 PKCS7 *ret = NULL;
118 115
119 if ((ret=PKCS7_new()) == NULL) goto err; 116 if ((ret = PKCS7_new()) == NULL)
120 if (!PKCS7_set_type(ret,type)) goto err; 117 goto err;
121 if (!PKCS7_set_content(p7,ret)) goto err; 118 if (!PKCS7_set_type(ret, type))
119 goto err;
120 if (!PKCS7_set_content(p7, ret))
121 goto err;
122 122
123 return(1); 123 return (1);
124err: 124err:
125 if (ret != NULL) PKCS7_free(ret); 125 if (ret != NULL)
126 return(0); 126 PKCS7_free(ret);
127 } 127 return (0);
128 128}
129int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) 129
130 { 130int
131PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
132{
131 int i; 133 int i;
132 134
133 i=OBJ_obj2nid(p7->type); 135 i = OBJ_obj2nid(p7->type);
134 switch (i) 136 switch (i) {
135 {
136 case NID_pkcs7_signed: 137 case NID_pkcs7_signed:
137 if (p7->d.sign->contents != NULL) 138 if (p7->d.sign->contents != NULL)
138 PKCS7_free(p7->d.sign->contents); 139 PKCS7_free(p7->d.sign->contents);
139 p7->d.sign->contents=p7_data; 140 p7->d.sign->contents = p7_data;
140 break; 141 break;
141 case NID_pkcs7_digest: 142 case NID_pkcs7_digest:
142 if (p7->d.digest->contents != NULL) 143 if (p7->d.digest->contents != NULL)
143 PKCS7_free(p7->d.digest->contents); 144 PKCS7_free(p7->d.digest->contents);
144 p7->d.digest->contents=p7_data; 145 p7->d.digest->contents = p7_data;
145 break; 146 break;
146 case NID_pkcs7_data: 147 case NID_pkcs7_data:
147 case NID_pkcs7_enveloped: 148 case NID_pkcs7_enveloped:
148 case NID_pkcs7_signedAndEnveloped: 149 case NID_pkcs7_signedAndEnveloped:
149 case NID_pkcs7_encrypted: 150 case NID_pkcs7_encrypted:
150 default: 151 default:
151 PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 152 PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,
153 PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
152 goto err; 154 goto err;
153 }
154 return(1);
155err:
156 return(0);
157 } 155 }
156 return (1);
157err:
158 return (0);
159}
158 160
159int PKCS7_set_type(PKCS7 *p7, int type) 161int
160 { 162PKCS7_set_type(PKCS7 *p7, int type)
163{
161 ASN1_OBJECT *obj; 164 ASN1_OBJECT *obj;
162 165
163 /*PKCS7_content_free(p7);*/ 166 /*PKCS7_content_free(p7);*/
164 obj=OBJ_nid2obj(type); /* will not fail */ 167 obj=OBJ_nid2obj(type); /* will not fail */
165 168
166 switch (type) 169 switch (type) {
167 {
168 case NID_pkcs7_signed: 170 case NID_pkcs7_signed:
169 p7->type=obj; 171 p7->type = obj;
170 if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL) 172 if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
171 goto err; 173 goto err;
172 if (!ASN1_INTEGER_set(p7->d.sign->version,1)) 174 if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
173 {
174 PKCS7_SIGNED_free(p7->d.sign); 175 PKCS7_SIGNED_free(p7->d.sign);
175 p7->d.sign=NULL; 176 p7->d.sign = NULL;
176 goto err; 177 goto err;
177 } 178 }
178 break; 179 break;
179 case NID_pkcs7_data: 180 case NID_pkcs7_data:
180 p7->type=obj; 181 p7->type = obj;
181 if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL) 182 if ((p7->d.data = M_ASN1_OCTET_STRING_new()) == NULL)
182 goto err; 183 goto err;
183 break; 184 break;
184 case NID_pkcs7_signedAndEnveloped: 185 case NID_pkcs7_signedAndEnveloped:
185 p7->type=obj; 186 p7->type = obj;
186 if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new()) 187 if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
187 == NULL) goto err; 188 == NULL) goto err;
188 ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1); 189 ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1);
189 if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1)) 190 if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
190 goto err; 191 goto err;
191 p7->d.signed_and_enveloped->enc_data->content_type 192 p7->d.signed_and_enveloped->enc_data->content_type
192 = OBJ_nid2obj(NID_pkcs7_data); 193 = OBJ_nid2obj(NID_pkcs7_data);
193 break; 194 break;
194 case NID_pkcs7_enveloped: 195 case NID_pkcs7_enveloped:
195 p7->type=obj; 196 p7->type = obj;
196 if ((p7->d.enveloped=PKCS7_ENVELOPE_new()) 197 if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
197 == NULL) goto err; 198 == NULL) goto err;
198 if (!ASN1_INTEGER_set(p7->d.enveloped->version,0)) 199 if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
199 goto err; 200 goto err;
200 p7->d.enveloped->enc_data->content_type 201 p7->d.enveloped->enc_data->content_type
201 = OBJ_nid2obj(NID_pkcs7_data); 202 = OBJ_nid2obj(NID_pkcs7_data);
202 break; 203 break;
203 case NID_pkcs7_encrypted: 204 case NID_pkcs7_encrypted:
204 p7->type=obj; 205 p7->type = obj;
205 if ((p7->d.encrypted=PKCS7_ENCRYPT_new()) 206 if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
206 == NULL) goto err; 207 == NULL) goto err;
207 if (!ASN1_INTEGER_set(p7->d.encrypted->version,0)) 208 if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
208 goto err; 209 goto err;
209 p7->d.encrypted->enc_data->content_type 210 p7->d.encrypted->enc_data->content_type
210 = OBJ_nid2obj(NID_pkcs7_data); 211 = OBJ_nid2obj(NID_pkcs7_data);
211 break; 212 break;
212 213
213 case NID_pkcs7_digest: 214 case NID_pkcs7_digest:
214 p7->type=obj; 215 p7->type = obj;
215 if ((p7->d.digest=PKCS7_DIGEST_new()) 216 if ((p7->d.digest = PKCS7_DIGEST_new())
216 == NULL) goto err; 217 == NULL) goto err;
217 if (!ASN1_INTEGER_set(p7->d.digest->version,0)) 218 if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
218 goto err; 219 goto err;
219 break; 220 break;
220 default: 221 default:
221 PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 222 PKCS7err(PKCS7_F_PKCS7_SET_TYPE,
223 PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
222 goto err; 224 goto err;
223 }
224 return(1);
225err:
226 return(0);
227 } 225 }
226 return (1);
227err:
228 return (0);
229}
228 230
229int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other) 231int
230 { 232PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
233{
231 p7->type = OBJ_nid2obj(type); 234 p7->type = OBJ_nid2obj(type);
232 p7->d.other = other; 235 p7->d.other = other;
233 return 1; 236 return 1;
234 } 237}
235 238
236int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) 239int
237 { 240PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
238 int i,j,nid; 241{
242 int i, j, nid;
239 X509_ALGOR *alg; 243 X509_ALGOR *alg;
240 STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; 244 STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
241 STACK_OF(X509_ALGOR) *md_sk; 245 STACK_OF(X509_ALGOR) *md_sk;
242 246
243 i=OBJ_obj2nid(p7->type); 247 i = OBJ_obj2nid(p7->type);
244 switch (i) 248 switch (i) {
245 {
246 case NID_pkcs7_signed: 249 case NID_pkcs7_signed:
247 signer_sk= p7->d.sign->signer_info; 250 signer_sk = p7->d.sign->signer_info;
248 md_sk= p7->d.sign->md_algs; 251 md_sk = p7->d.sign->md_algs;
249 break; 252 break;
250 case NID_pkcs7_signedAndEnveloped: 253 case NID_pkcs7_signedAndEnveloped:
251 signer_sk= p7->d.signed_and_enveloped->signer_info; 254 signer_sk = p7->d.signed_and_enveloped->signer_info;
252 md_sk= p7->d.signed_and_enveloped->md_algs; 255 md_sk = p7->d.signed_and_enveloped->md_algs;
253 break; 256 break;
254 default: 257 default:
255 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE); 258 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
256 return(0); 259 return (0);
257 } 260 }
258 261
259 nid=OBJ_obj2nid(psi->digest_alg->algorithm); 262 nid = OBJ_obj2nid(psi->digest_alg->algorithm);
260 263
261 /* If the digest is not currently listed, add it */ 264 /* If the digest is not currently listed, add it */
262 j=0; 265 j = 0;
263 for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) 266 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
264 { 267 alg = sk_X509_ALGOR_value(md_sk, i);
265 alg=sk_X509_ALGOR_value(md_sk,i); 268 if (OBJ_obj2nid(alg->algorithm) == nid) {
266 if (OBJ_obj2nid(alg->algorithm) == nid) 269 j = 1;
267 {
268 j=1;
269 break; 270 break;
270 }
271 } 271 }
272 }
272 if (!j) /* we need to add another algorithm */ 273 if (!j) /* we need to add another algorithm */
273 { 274 {
274 if(!(alg=X509_ALGOR_new()) 275 if (!(alg = X509_ALGOR_new()) ||
275 || !(alg->parameter = ASN1_TYPE_new())) 276 !(alg->parameter = ASN1_TYPE_new())) {
276 {
277 X509_ALGOR_free(alg); 277 X509_ALGOR_free(alg);
278 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE); 278 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,
279 return(0); 279 ERR_R_MALLOC_FAILURE);
280 } 280 return (0);
281 alg->algorithm=OBJ_nid2obj(nid); 281 }
282 alg->algorithm = OBJ_nid2obj(nid);
282 alg->parameter->type = V_ASN1_NULL; 283 alg->parameter->type = V_ASN1_NULL;
283 if (!sk_X509_ALGOR_push(md_sk,alg)) 284 if (!sk_X509_ALGOR_push(md_sk, alg)) {
284 {
285 X509_ALGOR_free(alg); 285 X509_ALGOR_free(alg);
286 return 0; 286 return 0;
287 }
288 } 287 }
288 }
289 289
290 if (!sk_PKCS7_SIGNER_INFO_push(signer_sk,psi)) 290 if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
291 return 0; 291 return 0;
292 return(1); 292 return (1);
293 } 293}
294 294
295int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) 295int
296 { 296PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
297{
297 int i; 298 int i;
298 STACK_OF(X509) **sk; 299 STACK_OF(X509) **sk;
299 300
300 i=OBJ_obj2nid(p7->type); 301 i = OBJ_obj2nid(p7->type);
301 switch (i) 302 switch (i) {
302 {
303 case NID_pkcs7_signed: 303 case NID_pkcs7_signed:
304 sk= &(p7->d.sign->cert); 304 sk = &(p7->d.sign->cert);
305 break; 305 break;
306 case NID_pkcs7_signedAndEnveloped: 306 case NID_pkcs7_signedAndEnveloped:
307 sk= &(p7->d.signed_and_enveloped->cert); 307 sk = &(p7->d.signed_and_enveloped->cert);
308 break; 308 break;
309 default: 309 default:
310 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE); 310 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,
311 return(0); 311 PKCS7_R_WRONG_CONTENT_TYPE);
312 } 312 return (0);
313 }
313 314
314 if (*sk == NULL) 315 if (*sk == NULL)
315 *sk=sk_X509_new_null(); 316 *sk = sk_X509_new_null();
316 if (*sk == NULL) 317 if (*sk == NULL) {
317 {
318 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE); 318 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
319 return 0; 319 return 0;
320 } 320 }
321 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); 321 CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
322 if (!sk_X509_push(*sk,x509)) 322 if (!sk_X509_push(*sk, x509)) {
323 {
324 X509_free(x509); 323 X509_free(x509);
325 return 0; 324 return 0;
326 }
327 return(1);
328 } 325 }
326 return (1);
327}
329 328
330int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) 329int
331 { 330PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
331{
332 int i; 332 int i;
333 STACK_OF(X509_CRL) **sk; 333 STACK_OF(X509_CRL) **sk;
334 334
335 i=OBJ_obj2nid(p7->type); 335 i = OBJ_obj2nid(p7->type);
336 switch (i) 336 switch (i) {
337 {
338 case NID_pkcs7_signed: 337 case NID_pkcs7_signed:
339 sk= &(p7->d.sign->crl); 338 sk = &(p7->d.sign->crl);
340 break; 339 break;
341 case NID_pkcs7_signedAndEnveloped: 340 case NID_pkcs7_signedAndEnveloped:
342 sk= &(p7->d.signed_and_enveloped->crl); 341 sk = &(p7->d.signed_and_enveloped->crl);
343 break; 342 break;
344 default: 343 default:
345 PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE); 344 PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
346 return(0); 345 return (0);
347 } 346 }
348 347
349 if (*sk == NULL) 348 if (*sk == NULL)
350 *sk=sk_X509_CRL_new_null(); 349 *sk = sk_X509_CRL_new_null();
351 if (*sk == NULL) 350 if (*sk == NULL) {
352 { 351 PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
353 PKCS7err(PKCS7_F_PKCS7_ADD_CRL,ERR_R_MALLOC_FAILURE);
354 return 0; 352 return 0;
355 } 353 }
356 354
357 CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL); 355 CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
358 if (!sk_X509_CRL_push(*sk,crl)) 356 if (!sk_X509_CRL_push(*sk, crl)) {
359 {
360 X509_CRL_free(crl); 357 X509_CRL_free(crl);
361 return 0; 358 return 0;
362 }
363 return(1);
364 } 359 }
360 return (1);
361}
365 362
366int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, 363int
367 const EVP_MD *dgst) 364PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
368 { 365 const EVP_MD *dgst)
366{
369 int ret; 367 int ret;
370 368
371 /* We now need to add another PKCS7_SIGNER_INFO entry */ 369 /* We now need to add another PKCS7_SIGNER_INFO entry */
372 if (!ASN1_INTEGER_set(p7i->version,1)) 370 if (!ASN1_INTEGER_set(p7i->version, 1))
373 goto err; 371 goto err;
374 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, 372 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
375 X509_get_issuer_name(x509))) 373 X509_get_issuer_name(x509)))
376 goto err; 374 goto err;
377 375
378 /* because ASN1_INTEGER_set is used to set a 'long' we will do 376 /* because ASN1_INTEGER_set is used to set a 'long' we will do
379 * things the ugly way. */ 377 * things the ugly way. */
380 M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); 378 M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
381 if (!(p7i->issuer_and_serial->serial= 379 if (!(p7i->issuer_and_serial->serial =
382 M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) 380 M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
383 goto err; 381 goto err;
384 382
385 /* lets keep the pkey around for a while */ 383 /* lets keep the pkey around for a while */
386 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); 384 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
387 p7i->pkey=pkey; 385 p7i->pkey = pkey;
388 386
389 /* Set the algorithms */ 387 /* Set the algorithms */
390 388
391 X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)), 389 X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
392 V_ASN1_NULL, NULL); 390 V_ASN1_NULL, NULL);
393 391
394 if (pkey->ameth && pkey->ameth->pkey_ctrl) 392 if (pkey->ameth && pkey->ameth->pkey_ctrl) {
395 {
396 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 393 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN,
397 0, p7i); 394 0, p7i);
398 if (ret > 0) 395 if (ret > 0)
399 return 1; 396 return 1;
400 if (ret != -2) 397 if (ret != -2) {
401 {
402 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, 398 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
403 PKCS7_R_SIGNING_CTRL_FAILURE); 399 PKCS7_R_SIGNING_CTRL_FAILURE);
404 return 0; 400 return 0;
405 }
406 } 401 }
402 }
407 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, 403 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
408 PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 404 PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
409err: 405err:
410 return 0; 406 return 0;
411 } 407}
412 408
413PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, 409PKCS7_SIGNER_INFO *
414 const EVP_MD *dgst) 410PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, const EVP_MD *dgst)
415 { 411{
416 PKCS7_SIGNER_INFO *si = NULL; 412 PKCS7_SIGNER_INFO *si = NULL;
417 413
418 if (dgst == NULL) 414 if (dgst == NULL) {
419 {
420 int def_nid; 415 int def_nid;
421 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) 416 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
422 goto err; 417 goto err;
423 dgst = EVP_get_digestbynid(def_nid); 418 dgst = EVP_get_digestbynid(def_nid);
424 if (dgst == NULL) 419 if (dgst == NULL) {
425 {
426 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, 420 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE,
427 PKCS7_R_NO_DEFAULT_DIGEST); 421 PKCS7_R_NO_DEFAULT_DIGEST);
428 goto err; 422 goto err;
429 }
430 } 423 }
424 }
431 425
432 if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err; 426 if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
433 if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err; 427 goto err;
434 if (!PKCS7_add_signer(p7,si)) goto err; 428 if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
435 return(si); 429 goto err;
430 if (!PKCS7_add_signer(p7, si))
431 goto err;
432 return (si);
436err: 433err:
437 if (si) 434 if (si)
438 PKCS7_SIGNER_INFO_free(si); 435 PKCS7_SIGNER_INFO_free(si);
439 return(NULL); 436 return (NULL);
440 } 437}
441 438
442int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) 439int
443 { 440PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
444 if (PKCS7_type_is_digest(p7)) 441{
445 { 442 if (PKCS7_type_is_digest(p7)) {
446 if(!(p7->d.digest->md->parameter = ASN1_TYPE_new())) 443 if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
447 { 444 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,
448 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,ERR_R_MALLOC_FAILURE); 445 ERR_R_MALLOC_FAILURE);
449 return 0; 446 return 0;
450 } 447 }
451 p7->d.digest->md->parameter->type = V_ASN1_NULL; 448 p7->d.digest->md->parameter->type = V_ASN1_NULL;
452 p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md)); 449 p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
453 return 1; 450 return 1;
454 }
455
456 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,PKCS7_R_WRONG_CONTENT_TYPE);
457 return 1;
458 } 451 }
459 452
460STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) 453 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
461 { 454 return 1;
462 if (PKCS7_type_is_signed(p7)) 455}
463 {
464 return(p7->d.sign->signer_info);
465 }
466 else if (PKCS7_type_is_signedAndEnveloped(p7))
467 {
468 return(p7->d.signed_and_enveloped->signer_info);
469 }
470 else
471 return(NULL);
472 }
473 456
474void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, 457STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
475 X509_ALGOR **pdig, X509_ALGOR **psig) 458{
476 { 459 if (PKCS7_type_is_signed(p7)) {
460 return (p7->d.sign->signer_info);
461 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
462 return (p7->d.signed_and_enveloped->signer_info);
463 } else
464 return (NULL);
465}
466
467void
468PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
469 X509_ALGOR **pdig, X509_ALGOR **psig)
470{
477 if (pk) 471 if (pk)
478 *pk = si->pkey; 472 *pk = si->pkey;
479 if (pdig) 473 if (pdig)
480 *pdig = si->digest_alg; 474 *pdig = si->digest_alg;
481 if (psig) 475 if (psig)
482 *psig = si->digest_enc_alg; 476 *psig = si->digest_enc_alg;
483 } 477}
484 478
485void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc) 479void
486 { 480PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
481{
487 if (penc) 482 if (penc)
488 *penc = ri->key_enc_algor; 483 *penc = ri->key_enc_algor;
489 } 484}
490 485
491PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) 486PKCS7_RECIP_INFO *
492 { 487PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
488{
493 PKCS7_RECIP_INFO *ri; 489 PKCS7_RECIP_INFO *ri;
494 490
495 if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err; 491 if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
496 if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err; 492 goto err;
497 if (!PKCS7_add_recipient_info(p7,ri)) goto err; 493 if (!PKCS7_RECIP_INFO_set(ri, x509))
494 goto err;
495 if (!PKCS7_add_recipient_info(p7, ri))
496 goto err;
498 return ri; 497 return ri;
499err: 498err:
500 if (ri) 499 if (ri)
501 PKCS7_RECIP_INFO_free(ri); 500 PKCS7_RECIP_INFO_free(ri);
502 return NULL; 501 return NULL;
503 } 502}
504 503
505int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) 504int
506 { 505PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
506{
507 int i; 507 int i;
508 STACK_OF(PKCS7_RECIP_INFO) *sk; 508 STACK_OF(PKCS7_RECIP_INFO) *sk;
509 509
510 i=OBJ_obj2nid(p7->type); 510 i = OBJ_obj2nid(p7->type);
511 switch (i) 511 switch (i) {
512 {
513 case NID_pkcs7_signedAndEnveloped: 512 case NID_pkcs7_signedAndEnveloped:
514 sk= p7->d.signed_and_enveloped->recipientinfo; 513 sk = p7->d.signed_and_enveloped->recipientinfo;
515 break; 514 break;
516 case NID_pkcs7_enveloped: 515 case NID_pkcs7_enveloped:
517 sk= p7->d.enveloped->recipientinfo; 516 sk = p7->d.enveloped->recipientinfo;
518 break; 517 break;
519 default: 518 default:
520 PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE); 519 PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
521 return(0); 520 PKCS7_R_WRONG_CONTENT_TYPE);
522 } 521 return (0);
522 }
523 523
524 if (!sk_PKCS7_RECIP_INFO_push(sk,ri)) 524 if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
525 return 0; 525 return 0;
526 return(1); 526 return (1);
527 } 527}
528 528
529int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) 529int
530 { 530PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
531{
531 int ret; 532 int ret;
532 EVP_PKEY *pkey = NULL; 533 EVP_PKEY *pkey = NULL;
533 if (!ASN1_INTEGER_set(p7i->version,0)) 534 if (!ASN1_INTEGER_set(p7i->version, 0))
534 return 0; 535 return 0;
535 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, 536 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
536 X509_get_issuer_name(x509))) 537 X509_get_issuer_name(x509)))
537 return 0; 538 return 0;
538 539
539 M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); 540 M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
540 if (!(p7i->issuer_and_serial->serial= 541 if (!(p7i->issuer_and_serial->serial =
541 M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) 542 M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
542 return 0; 543 return 0;
543 544
544 pkey = X509_get_pubkey(x509); 545 pkey = X509_get_pubkey(x509);
545 546
546 if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) 547 if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
547 {
548 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 548 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
549 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 549 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
550 goto err; 550 goto err;
551 } 551 }
552 552
553 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 553 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT,
554 0, p7i); 554 0, p7i);
555 if (ret == -2) 555 if (ret == -2) {
556 {
557 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 556 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
558 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 557 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
559 goto err; 558 goto err;
560 } 559 }
561 if (ret <= 0) 560 if (ret <= 0) {
562 {
563 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 561 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
564 PKCS7_R_ENCRYPTION_CTRL_FAILURE); 562 PKCS7_R_ENCRYPTION_CTRL_FAILURE);
565 goto err; 563 goto err;
566 } 564 }
567 565
568 EVP_PKEY_free(pkey); 566 EVP_PKEY_free(pkey);
569 567
570 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); 568 CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
571 p7i->cert=x509; 569 p7i->cert = x509;
572 570
573 return 1; 571 return 1;
574 572
575 err: 573err:
576 if (pkey) 574 if (pkey)
577 EVP_PKEY_free(pkey); 575 EVP_PKEY_free(pkey);
578 return 0; 576 return 0;
579 } 577}
580 578
581X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si) 579X509 *
582 { 580PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
581{
583 if (PKCS7_type_is_signed(p7)) 582 if (PKCS7_type_is_signed(p7))
584 return(X509_find_by_issuer_and_serial(p7->d.sign->cert, 583 return(X509_find_by_issuer_and_serial(p7->d.sign->cert,
585 si->issuer_and_serial->issuer, 584 si->issuer_and_serial->issuer,
586 si->issuer_and_serial->serial)); 585 si->issuer_and_serial->serial));
587 else 586 else
588 return(NULL); 587 return (NULL);
589 } 588}
590 589
591int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) 590int
592 { 591PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
592{
593 int i; 593 int i;
594 PKCS7_ENC_CONTENT *ec; 594 PKCS7_ENC_CONTENT *ec;
595 595
596 i=OBJ_obj2nid(p7->type); 596 i = OBJ_obj2nid(p7->type);
597 switch (i) 597 switch (i) {
598 {
599 case NID_pkcs7_signedAndEnveloped: 598 case NID_pkcs7_signedAndEnveloped:
600 ec=p7->d.signed_and_enveloped->enc_data; 599 ec = p7->d.signed_and_enveloped->enc_data;
601 break; 600 break;
602 case NID_pkcs7_enveloped: 601 case NID_pkcs7_enveloped:
603 ec=p7->d.enveloped->enc_data; 602 ec = p7->d.enveloped->enc_data;
604 break; 603 break;
605 default: 604 default:
606 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE); 605 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
607 return(0); 606 return (0);
608 } 607 }
609 608
610 /* Check cipher OID exists and has data in it*/ 609 /* Check cipher OID exists and has data in it*/
611 i = EVP_CIPHER_type(cipher); 610 i = EVP_CIPHER_type(cipher);
612 if(i == NID_undef) { 611 if (i == NID_undef) {
613 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); 612 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
614 return(0); 613 PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
614 return (0);
615 } 615 }
616 616
617 ec->cipher = cipher; 617 ec->cipher = cipher;
618 return 1; 618 return 1;
619 } 619}
620 620
621int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7) 621int
622 { 622PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
623{
623 ASN1_OCTET_STRING *os = NULL; 624 ASN1_OCTET_STRING *os = NULL;
624 625
625 switch (OBJ_obj2nid(p7->type)) 626 switch (OBJ_obj2nid(p7->type)) {
626 { 627 case NID_pkcs7_data:
627 case NID_pkcs7_data:
628 os = p7->d.data; 628 os = p7->d.data;
629 break; 629 break;
630 630
631 case NID_pkcs7_signedAndEnveloped: 631 case NID_pkcs7_signedAndEnveloped:
632 os = p7->d.signed_and_enveloped->enc_data->enc_data; 632 os = p7->d.signed_and_enveloped->enc_data->enc_data;
633 if (os == NULL) 633 if (os == NULL) {
634 { 634 os = M_ASN1_OCTET_STRING_new();
635 os=M_ASN1_OCTET_STRING_new(); 635 p7->d.signed_and_enveloped->enc_data->enc_data = os;
636 p7->d.signed_and_enveloped->enc_data->enc_data=os; 636 }
637 }
638 break; 637 break;
639 638
640 case NID_pkcs7_enveloped: 639 case NID_pkcs7_enveloped:
641 os = p7->d.enveloped->enc_data->enc_data; 640 os = p7->d.enveloped->enc_data->enc_data;
642 if (os == NULL) 641 if (os == NULL) {
643 { 642 os = M_ASN1_OCTET_STRING_new();
644 os=M_ASN1_OCTET_STRING_new(); 643 p7->d.enveloped->enc_data->enc_data = os;
645 p7->d.enveloped->enc_data->enc_data=os; 644 }
646 }
647 break; 645 break;
648 646
649 case NID_pkcs7_signed: 647 case NID_pkcs7_signed:
650 os=p7->d.sign->contents->d.data; 648 os = p7->d.sign->contents->d.data;
651 break; 649 break;
652 650
653 default: 651 default:
654 os = NULL; 652 os = NULL;
655 break; 653 break;
656 } 654 }
657 655
658 if (os == NULL) 656 if (os == NULL)
659 return 0; 657 return 0;
660 658
@@ -662,4 +660,4 @@ int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
662 *boundary = &os->data; 660 *boundary = &os->data;
663 661
664 return 1; 662 return 1;
665 } 663}