Various fixes...
[openssl.git] / crypto / asn1 / p7_lib.c
1 /* crypto/asn1/p7_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/asn1_mac.h>
62 #include <openssl/pkcs7.h>
63 #include <openssl/objects.h>
64
65 #ifdef PKCS7_INDEFINITE_ENCODING
66
67 int i2d_PKCS7(PKCS7 *a, unsigned char **pp)
68         {
69         M_ASN1_I2D_vars(a);
70
71         if (a->asn1 != NULL)
72                 {
73                 if (pp == NULL)
74                         return((int)a->length);
75                 memcpy(*pp,a->asn1,(int)a->length);
76                 *pp+=a->length;
77                 return((int)a->length);
78                 }
79
80         ret+=4; /* sequence, BER header plus '0 0' end padding */
81         M_ASN1_I2D_len(a->type,i2d_ASN1_OBJECT);
82         if (a->d.ptr != NULL)
83                 {
84                 ret+=4; /* explicit tag [ 0 ] BER plus '0 0' */
85                 switch (OBJ_obj2nid(a->type))
86                         {
87                 case NID_pkcs7_data:
88                         M_ASN1_I2D_len(a->d.data,i2d_ASN1_OCTET_STRING);
89                         break;
90                 case NID_pkcs7_signed:
91                         M_ASN1_I2D_len(a->d.sign,i2d_PKCS7_SIGNED);
92                         break;
93                 case NID_pkcs7_enveloped:
94                         M_ASN1_I2D_len(a->d.enveloped,i2d_PKCS7_ENVELOPE);
95                         break;
96                 case NID_pkcs7_signedAndEnveloped:
97                         M_ASN1_I2D_len(a->d.signed_and_enveloped,
98                                 i2d_PKCS7_SIGN_ENVELOPE);
99                         break;
100                 case NID_pkcs7_digest:
101                         M_ASN1_I2D_len(a->d.digest,i2d_PKCS7_DIGEST);
102                         break;
103                 case NID_pkcs7_encrypted:
104                         M_ASN1_I2D_len(a->d.encrypted,i2d_PKCS7_ENCRYPT);
105                         break;
106                 default:
107                         break;
108                         }
109                 }
110         r=ret;
111         if (pp == NULL) return(r);
112         p= *pp;
113         M_ASN1_I2D_INF_seq_start(V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
114         M_ASN1_I2D_put(a->type,i2d_ASN1_OBJECT);
115
116         if (a->d.ptr != NULL)
117                 {
118                 M_ASN1_I2D_INF_seq_start(0,V_ASN1_CONTEXT_SPECIFIC);
119                 switch (OBJ_obj2nid(a->type))
120                         {
121                 case NID_pkcs7_data:
122                         M_ASN1_I2D_put(a->d.data,i2d_ASN1_OCTET_STRING);
123                         break;
124                 case NID_pkcs7_signed:
125                         M_ASN1_I2D_put(a->d.sign,i2d_PKCS7_SIGNED);
126                         break;
127                 case NID_pkcs7_enveloped:
128                         M_ASN1_I2D_put(a->d.enveloped,i2d_PKCS7_ENVELOPE);
129                         break;
130                 case NID_pkcs7_signedAndEnveloped:
131                         M_ASN1_I2D_put(a->d.signed_and_enveloped,
132                                 i2d_PKCS7_SIGN_ENVELOPE);
133                         break;
134                 case NID_pkcs7_digest:
135                         M_ASN1_I2D_put(a->d.digest,i2d_PKCS7_DIGEST);
136                         break;
137                 case NID_pkcs7_encrypted:
138                         M_ASN1_I2D_put(a->d.encrypted,i2d_PKCS7_ENCRYPT);
139                         break;
140                 default:
141                         break;
142                         }
143                 M_ASN1_I2D_INF_seq_end();
144                 }
145         M_ASN1_I2D_INF_seq_end();
146         M_ASN1_I2D_finish();
147         }
148
149 #else
150
151 int i2d_PKCS7(PKCS7 *a, unsigned char **pp)
152         {
153         int explen = 0;
154         M_ASN1_I2D_vars(a);
155
156         if (a->asn1 != NULL)
157                 {
158                 if (pp == NULL)
159                         return((int)a->length);
160                 memcpy(*pp,a->asn1,(int)a->length);
161                 *pp+=a->length;
162                 return((int)a->length);
163                 }
164
165         M_ASN1_I2D_len(a->type,i2d_ASN1_OBJECT);
166         if (a->d.ptr != NULL)
167                 {
168                 /* Save current length */
169                 r = ret;
170                 switch (OBJ_obj2nid(a->type))
171                         {
172                 case NID_pkcs7_data:
173                         M_ASN1_I2D_len(a->d.data,i2d_ASN1_OCTET_STRING);
174                         break;
175                 case NID_pkcs7_signed:
176                         M_ASN1_I2D_len(a->d.sign,i2d_PKCS7_SIGNED);
177                         break;
178                 case NID_pkcs7_enveloped:
179                         M_ASN1_I2D_len(a->d.enveloped,i2d_PKCS7_ENVELOPE);
180                         break;
181                 case NID_pkcs7_signedAndEnveloped:
182                         M_ASN1_I2D_len(a->d.signed_and_enveloped,
183                                 i2d_PKCS7_SIGN_ENVELOPE);
184                         break;
185                 case NID_pkcs7_digest:
186                         M_ASN1_I2D_len(a->d.digest,i2d_PKCS7_DIGEST);
187                         break;
188                 case NID_pkcs7_encrypted:
189                         M_ASN1_I2D_len(a->d.encrypted,i2d_PKCS7_ENCRYPT);
190                         break;
191                 default:
192                         break;
193                         }
194                 /* Work out explicit tag content size */
195                 explen = ret - r;
196                 /* Work out explicit tag size: Note: ASN1_object_size
197                  * includes the content length.
198                  */
199                 ret =  r + ASN1_object_size(1, explen, 0);
200                 }
201
202         M_ASN1_I2D_seq_total();
203
204         M_ASN1_I2D_put(a->type,i2d_ASN1_OBJECT);
205
206         if (a->d.ptr != NULL)
207                 {
208                 ASN1_put_object(&p, 1, explen, 0, V_ASN1_CONTEXT_SPECIFIC);
209                 switch (OBJ_obj2nid(a->type))
210                         {
211                 case NID_pkcs7_data:
212                         M_ASN1_I2D_put(a->d.data,i2d_ASN1_OCTET_STRING);
213                         break;
214                 case NID_pkcs7_signed:
215                         M_ASN1_I2D_put(a->d.sign,i2d_PKCS7_SIGNED);
216                         break;
217                 case NID_pkcs7_enveloped:
218                         M_ASN1_I2D_put(a->d.enveloped,i2d_PKCS7_ENVELOPE);
219                         break;
220                 case NID_pkcs7_signedAndEnveloped:
221                         M_ASN1_I2D_put(a->d.signed_and_enveloped,
222                                 i2d_PKCS7_SIGN_ENVELOPE);
223                         break;
224                 case NID_pkcs7_digest:
225                         M_ASN1_I2D_put(a->d.digest,i2d_PKCS7_DIGEST);
226                         break;
227                 case NID_pkcs7_encrypted:
228                         M_ASN1_I2D_put(a->d.encrypted,i2d_PKCS7_ENCRYPT);
229                         break;
230                 default:
231                         break;
232                         }
233                 }
234         M_ASN1_I2D_finish();
235         }
236
237 #endif
238
239 PKCS7 *d2i_PKCS7(PKCS7 **a, unsigned char **pp, long length)
240         {
241         M_ASN1_D2I_vars(a,PKCS7 *,PKCS7_new);
242
243         if ((a != NULL) && ((*a) != NULL))
244                 {
245                 if ((*a)->asn1 != NULL)
246                         {
247                         OPENSSL_free((*a)->asn1);
248                         (*a)->asn1=NULL;
249                         }
250                 (*a)->length=0;
251                 }
252
253         M_ASN1_D2I_Init();
254         M_ASN1_D2I_start_sequence();
255         M_ASN1_D2I_get(ret->type,d2i_ASN1_OBJECT);
256         if (!M_ASN1_D2I_end_sequence())
257                 {
258                 int Tinf,Ttag,Tclass;
259                 long Tlen;
260
261                 if (M_ASN1_next != (V_ASN1_CONSTRUCTED|
262                         V_ASN1_CONTEXT_SPECIFIC|0))
263                         {
264                         c.error=ASN1_R_BAD_PKCS7_CONTENT;
265                         c.line=__LINE__;
266                         goto err;
267                         }
268
269                 ret->detached=0;
270
271                 c.q=c.p;
272                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,
273                         (c.inf & 1)?(length+ *pp-c.q):c.slen);
274                 if (Tinf & 0x80) { c.line=__LINE__; goto err; }
275                 c.slen-=(c.p-c.q);
276
277                 switch (OBJ_obj2nid(ret->type))
278                         {
279                 case NID_pkcs7_data:
280                         M_ASN1_D2I_get(ret->d.data,d2i_ASN1_OCTET_STRING);
281                         break;
282                 case NID_pkcs7_signed:
283                         M_ASN1_D2I_get(ret->d.sign,d2i_PKCS7_SIGNED);
284                         if (ret->d.sign->contents->d.ptr == NULL)
285                                 ret->detached=1;
286                         break;
287                 case NID_pkcs7_enveloped:
288                         M_ASN1_D2I_get(ret->d.enveloped,d2i_PKCS7_ENVELOPE);
289                         break;
290                 case NID_pkcs7_signedAndEnveloped:
291                         M_ASN1_D2I_get(ret->d.signed_and_enveloped,
292                                 d2i_PKCS7_SIGN_ENVELOPE);
293                         break;
294                 case NID_pkcs7_digest:
295                         M_ASN1_D2I_get(ret->d.digest,d2i_PKCS7_DIGEST);
296                         break;
297                 case NID_pkcs7_encrypted:
298                         M_ASN1_D2I_get(ret->d.encrypted,d2i_PKCS7_ENCRYPT);
299                         break;
300                 default:
301                         c.error=ASN1_R_BAD_PKCS7_TYPE;
302                         c.line=__LINE__;
303                         goto err;
304                         /* break; */
305                         }
306                 if (Tinf == (1|V_ASN1_CONSTRUCTED))
307                         {
308                         if (!ASN1_check_infinite_end(&c.p,c.slen))
309                                 {
310                                 c.error=ERR_R_MISSING_ASN1_EOS;
311                                 c.line=__LINE__;
312                                 goto err;
313                                 }
314                         }
315                 }
316         else
317                 ret->detached=1;
318                 
319         M_ASN1_D2I_Finish(a,PKCS7_free,ASN1_F_D2I_PKCS7);
320         }
321
322 PKCS7 *PKCS7_new(void)
323         {
324         PKCS7 *ret=NULL;
325         ASN1_CTX c;
326
327         M_ASN1_New_Malloc(ret,PKCS7);
328         ret->type=OBJ_nid2obj(NID_undef);
329         ret->asn1=NULL;
330         ret->length=0;
331         ret->detached=0;
332         ret->d.ptr=NULL;
333         return(ret);
334         M_ASN1_New_Error(ASN1_F_PKCS7_NEW);
335         }
336
337 void PKCS7_free(PKCS7 *a)
338         {
339         if (a == NULL) return;
340
341         PKCS7_content_free(a);
342         if (a->type != NULL)
343                 {
344                 ASN1_OBJECT_free(a->type);
345                 }
346         OPENSSL_free(a);
347         }
348
349 void PKCS7_content_free(PKCS7 *a)
350         {
351         if(a == NULL)
352             return;
353
354         if (a->asn1 != NULL) OPENSSL_free(a->asn1);
355
356         if (a->d.ptr != NULL)
357                 {
358                 if (a->type == NULL) return;
359
360                 switch (OBJ_obj2nid(a->type))
361                         {
362                 case NID_pkcs7_data:
363                         M_ASN1_OCTET_STRING_free(a->d.data);
364                         break;
365                 case NID_pkcs7_signed:
366                         PKCS7_SIGNED_free(a->d.sign);
367                         break;
368                 case NID_pkcs7_enveloped:
369                         PKCS7_ENVELOPE_free(a->d.enveloped);
370                         break;
371                 case NID_pkcs7_signedAndEnveloped:
372                         PKCS7_SIGN_ENVELOPE_free(a->d.signed_and_enveloped);
373                         break;
374                 case NID_pkcs7_digest:
375                         PKCS7_DIGEST_free(a->d.digest);
376                         break;
377                 case NID_pkcs7_encrypted:
378                         PKCS7_ENCRYPT_free(a->d.encrypted);
379                         break;
380                 default:
381                         /* MEMORY LEAK */
382                         break;
383                         }
384                 }
385         a->d.ptr=NULL;
386         }
387
388 IMPLEMENT_STACK_OF(PKCS7)
389 IMPLEMENT_ASN1_SET_OF(PKCS7)