summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1
diff options
context:
space:
mode:
authorryker <>1998-10-05 20:13:14 +0000
committerryker <>1998-10-05 20:13:14 +0000
commitaeeae06a79815dc190061534d47236cec09f9e32 (patch)
tree851692b9c2f9c04f077666855641900f19fdb217 /src/lib/libcrypto/asn1
parenta4f79641824cbf9f60ca9d1168d1fcc46717a82a (diff)
downloadopenbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.gz
openbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.bz2
openbsd-aeeae06a79815dc190061534d47236cec09f9e32.zip
Import of SSLeay-0.9.0b with RSA and IDEA stubbed + OpenBSD build
functionality for shared libs. Note that routines such as sslv2_init and friends that use RSA will not work due to lack of RSA in this library. Needs documentation and help from ports for easy upgrade to full functionality where legally possible.
Diffstat (limited to 'src/lib/libcrypto/asn1')
-rw-r--r--src/lib/libcrypto/asn1/a_bitstr.c204
-rw-r--r--src/lib/libcrypto/asn1/a_bool.c121
-rw-r--r--src/lib/libcrypto/asn1/a_bytes.c346
-rw-r--r--src/lib/libcrypto/asn1/a_d2i_fp.c201
-rw-r--r--src/lib/libcrypto/asn1/a_digest.c91
-rw-r--r--src/lib/libcrypto/asn1/a_dup.c86
-rw-r--r--src/lib/libcrypto/asn1/a_i2d_fp.c119
-rw-r--r--src/lib/libcrypto/asn1/a_int.c305
-rw-r--r--src/lib/libcrypto/asn1/a_object.c389
-rw-r--r--src/lib/libcrypto/asn1/a_octet.c90
-rw-r--r--src/lib/libcrypto/asn1/a_print.c161
-rw-r--r--src/lib/libcrypto/asn1/a_set.c149
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c147
-rw-r--r--src/lib/libcrypto/asn1/a_type.c325
-rw-r--r--src/lib/libcrypto/asn1/a_verify.c121
-rw-r--r--src/lib/libcrypto/asn1/asn1.h859
-rw-r--r--src/lib/libcrypto/asn1/asn1_err.c266
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c444
-rw-r--r--src/lib/libcrypto/asn1/asn1_mac.h321
-rw-r--r--src/lib/libcrypto/asn1/asn1_par.c393
-rw-r--r--src/lib/libcrypto/asn1/d2i_pr.c117
-rw-r--r--src/lib/libcrypto/asn1/d2i_pu.c117
-rw-r--r--src/lib/libcrypto/asn1/evp_asn1.c193
-rw-r--r--src/lib/libcrypto/asn1/f_int.c211
-rw-r--r--src/lib/libcrypto/asn1/f_string.c210
-rw-r--r--src/lib/libcrypto/asn1/i2d_pr.c86
-rw-r--r--src/lib/libcrypto/asn1/i2d_pu.c84
-rw-r--r--src/lib/libcrypto/asn1/n_pkey.c365
-rw-r--r--src/lib/libcrypto/asn1/t_pkey.c392
-rw-r--r--src/lib/libcrypto/asn1/t_req.c226
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c386
-rw-r--r--src/lib/libcrypto/asn1/x_algor.c126
-rw-r--r--src/lib/libcrypto/asn1/x_attrib.c152
-rw-r--r--src/lib/libcrypto/asn1/x_crl.c353
-rw-r--r--src/lib/libcrypto/asn1/x_exten.c156
-rw-r--r--src/lib/libcrypto/asn1/x_info.c111
-rw-r--r--src/lib/libcrypto/asn1/x_name.c295
-rw-r--r--src/lib/libcrypto/asn1/x_pkey.c156
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c256
-rw-r--r--src/lib/libcrypto/asn1/x_req.c247
-rw-r--r--src/lib/libcrypto/asn1/x_sig.c119
-rw-r--r--src/lib/libcrypto/asn1/x_spki.c181
-rw-r--r--src/lib/libcrypto/asn1/x_val.c118
-rw-r--r--src/lib/libcrypto/asn1/x_x509.c158
44 files changed, 9953 insertions, 0 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c
new file mode 100644
index 0000000000..2c10120651
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bitstr.c
@@ -0,0 +1,204 @@
1/* crypto/asn1/a_bitstr.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 "asn1.h"
62
63/* ASN1err(ASN1_F_ASN1_STRING_NEW,ASN1_R_STRING_TOO_SHORT);
64 * ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,ASN1_R_EXPECTING_A_BIT_STRING);
65 */
66
67int i2d_ASN1_BIT_STRING(a,pp)
68ASN1_BIT_STRING *a;
69unsigned char **pp;
70 {
71 int ret,j,r,bits;
72 unsigned char *p,*d;
73
74 if (a == NULL) return(0);
75
76 /* our bit strings are always a multiple of 8 :-) */
77 bits=0;
78 ret=1+a->length;
79 r=ASN1_object_size(0,ret,V_ASN1_BIT_STRING);
80 if (pp == NULL) return(r);
81 p= *pp;
82
83 ASN1_put_object(&p,0,ret,V_ASN1_BIT_STRING,V_ASN1_UNIVERSAL);
84 if (bits == 0)
85 j=0;
86 else j=8-bits;
87 *(p++)=(unsigned char)j;
88 d=a->data;
89 memcpy(p,d,a->length);
90 p+=a->length;
91 if (a->length > 0) p[-1]&=(0xff<<j);
92 *pp=p;
93 return(r);
94 }
95
96ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(a, pp, length)
97ASN1_BIT_STRING **a;
98unsigned char **pp;
99long length;
100 {
101 ASN1_BIT_STRING *ret=NULL;
102 unsigned char *p,*s;
103 long len;
104 int inf,tag,xclass;
105 int i;
106
107 if ((a == NULL) || ((*a) == NULL))
108 {
109 if ((ret=ASN1_BIT_STRING_new()) == NULL) return(NULL);
110 }
111 else
112 ret=(*a);
113
114 p= *pp;
115 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
116 if (inf & 0x80)
117 {
118 i=ASN1_R_BAD_OBJECT_HEADER;
119 goto err;
120 }
121
122 if (tag != V_ASN1_BIT_STRING)
123 {
124 i=ASN1_R_EXPECTING_A_BIT_STRING;
125 goto err;
126 }
127 if (len < 1) { i=ASN1_R_STRING_TOO_SHORT; goto err; }
128
129 i= *(p++);
130 if (len-- > 1) /* using one because of the bits left byte */
131 {
132 s=(unsigned char *)Malloc((int)len);
133 if (s == NULL)
134 {
135 i=ERR_R_MALLOC_FAILURE;
136 goto err;
137 }
138 memcpy(s,p,(int)len);
139 s[len-1]&=(0xff<<i);
140 p+=len;
141 }
142 else
143 s=NULL;
144
145 ret->length=(int)len;
146 if (ret->data != NULL) Free((char *)ret->data);
147 ret->data=s;
148 ret->type=V_ASN1_BIT_STRING;
149 if (a != NULL) (*a)=ret;
150 *pp=p;
151 return(ret);
152err:
153 ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,i);
154 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
155 ASN1_BIT_STRING_free(ret);
156 return(NULL);
157 }
158
159/* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
160 */
161int ASN1_BIT_STRING_set_bit(a,n,value)
162ASN1_BIT_STRING *a;
163int n;
164int value;
165 {
166 int w,v,iv;
167 unsigned char *c;
168
169 w=n/8;
170 v=1<<(7-(n&0x07));
171 iv= ~v;
172
173 if (a == NULL) return(0);
174 if ((a->length < (w+1)) || (a->data == NULL))
175 {
176 if (!value) return(1); /* Don't need to set */
177 if (a->data == NULL)
178 c=(unsigned char *)Malloc(w+1);
179 else
180 c=(unsigned char *)Realloc(a->data,w+1);
181 if (c == NULL) return(0);
182 a->data=c;
183 a->length=w+1;
184 c[w]=0;
185 }
186 a->data[w]=((a->data[w])&iv)|v;
187 while ((a->length > 0) && (a->data[a->length-1] == 0))
188 a->length--;
189 return(1);
190 }
191
192int ASN1_BIT_STRING_get_bit(a,n)
193ASN1_BIT_STRING *a;
194int n;
195 {
196 int w,v;
197
198 w=n/8;
199 v=1<<(7-(n&0x07));
200 if ((a == NULL) || (a->length < (w+1)) || (a->data == NULL))
201 return(0);
202 return((a->data[w]&v) != 0);
203 }
204
diff --git a/src/lib/libcrypto/asn1/a_bool.c b/src/lib/libcrypto/asn1/a_bool.c
new file mode 100644
index 0000000000..41a95aa278
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bool.c
@@ -0,0 +1,121 @@
1/* crypto/asn1/a_bool.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 "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_EXPECTING_A_BOOLEAN);
64 * ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
65 */
66
67int i2d_ASN1_BOOLEAN(a,pp)
68int a;
69unsigned char **pp;
70 {
71 int r;
72 unsigned char *p;
73
74 r=ASN1_object_size(0,1,V_ASN1_BOOLEAN);
75 if (pp == NULL) return(r);
76 p= *pp;
77
78 ASN1_put_object(&p,0,1,V_ASN1_BOOLEAN,V_ASN1_UNIVERSAL);
79 *(p++)= (unsigned char)a;
80 *pp=p;
81 return(r);
82 }
83
84int d2i_ASN1_BOOLEAN(a, pp, length)
85int *a;
86unsigned char **pp;
87long length;
88 {
89 int ret= -1;
90 unsigned char *p;
91 long len;
92 int inf,tag,xclass;
93 int i=0;
94
95 p= *pp;
96 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
97 if (inf & 0x80)
98 {
99 i=ASN1_R_BAD_OBJECT_HEADER;
100 goto err;
101 }
102
103 if (tag != V_ASN1_BOOLEAN)
104 {
105 i=ASN1_R_EXPECTING_A_BOOLEAN;
106 goto err;
107 }
108
109 if (len != 1)
110 {
111 i=ASN1_R_BOOLEAN_IS_WRONG_LENGTH;
112 goto err;
113 }
114 ret= (int)*(p++);
115 if (a != NULL) (*a)=ret;
116 *pp=p;
117 return(ret);
118err:
119 ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,i);
120 return(ret);
121 }
diff --git a/src/lib/libcrypto/asn1/a_bytes.c b/src/lib/libcrypto/asn1/a_bytes.c
new file mode 100644
index 0000000000..14168d61ad
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bytes.c
@@ -0,0 +1,346 @@
1/* crypto/asn1/a_bytes.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 "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK);
64 * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_ERROR_STACK);
65 * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_WRONG_TYPE);
66 * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
67 */
68
69static unsigned long tag2bit[32]={
700, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
71B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
72B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
73B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 12-15 */
740, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING,
75B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,0,
760,B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,
77B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN,
78 };
79
80#ifndef NOPROTO
81static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c);
82#else
83static int asn1_collate_primative();
84#endif
85
86/* type is a 'bitmap' of acceptable string types to be accepted.
87 */
88ASN1_STRING *d2i_ASN1_type_bytes(a, pp, length, type)
89ASN1_STRING **a;
90unsigned char **pp;
91long length;
92int type;
93 {
94 ASN1_STRING *ret=NULL;
95 unsigned char *p,*s;
96 long len;
97 int inf,tag,xclass;
98 int i=0;
99
100 p= *pp;
101 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
102 if (inf & 0x80) goto err;
103
104 if (tag >= 32)
105 {
106 i=ASN1_R_TAG_VALUE_TOO_HIGH;;
107 goto err;
108 }
109 if (!(tag2bit[tag] & type))
110 {
111 i=ASN1_R_WRONG_TYPE;
112 goto err;
113 }
114
115 /* If a bit-string, exit early */
116 if (tag == V_ASN1_BIT_STRING)
117 return(d2i_ASN1_BIT_STRING(a,pp,length));
118
119 if ((a == NULL) || ((*a) == NULL))
120 {
121 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
122 }
123 else
124 ret=(*a);
125
126 if (len != 0)
127 {
128 s=(unsigned char *)Malloc((int)len+1);
129 if (s == NULL)
130 {
131 i=ERR_R_MALLOC_FAILURE;
132 goto err;
133 }
134 memcpy(s,p,(int)len);
135 s[len]='\0';
136 p+=len;
137 }
138 else
139 s=NULL;
140
141 if (ret->data != NULL) Free((char *)ret->data);
142 ret->length=(int)len;
143 ret->data=s;
144 ret->type=tag;
145 if (a != NULL) (*a)=ret;
146 *pp=p;
147 return(ret);
148err:
149 ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i);
150 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
151 ASN1_STRING_free(ret);
152 return(NULL);
153 }
154
155int i2d_ASN1_bytes(a, pp, tag, xclass)
156ASN1_STRING *a;
157unsigned char **pp;
158int tag;
159int xclass;
160 {
161 int ret,r,constructed;
162 unsigned char *p;
163
164 if (a == NULL) return(0);
165
166 if (tag == V_ASN1_BIT_STRING)
167 return(i2d_ASN1_BIT_STRING(a,pp));
168
169 ret=a->length;
170 r=ASN1_object_size(0,ret,tag);
171 if (pp == NULL) return(r);
172 p= *pp;
173
174 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
175 constructed=1;
176 else
177 constructed=0;
178 ASN1_put_object(&p,constructed,ret,tag,xclass);
179 memcpy(p,a->data,a->length);
180 p+=a->length;
181 *pp= p;
182 return(r);
183 }
184
185ASN1_STRING *d2i_ASN1_bytes(a, pp, length, Ptag, Pclass)
186ASN1_STRING **a;
187unsigned char **pp;
188long length;
189int Ptag;
190int Pclass;
191 {
192 ASN1_STRING *ret=NULL;
193 unsigned char *p,*s;
194 long len;
195 int inf,tag,xclass;
196 int i=0;
197
198 if ((a == NULL) || ((*a) == NULL))
199 {
200 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
201 }
202 else
203 ret=(*a);
204
205 p= *pp;
206 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
207 if (inf & 0x80)
208 {
209 i=ASN1_R_BAD_OBJECT_HEADER;
210 goto err;
211 }
212
213 if (tag != Ptag)
214 {
215 i=ASN1_R_WRONG_TAG;
216 goto err;
217 }
218
219 if (inf & V_ASN1_CONSTRUCTED)
220 {
221 ASN1_CTX c;
222
223 c.pp=pp;
224 c.p=p;
225 c.inf=inf;
226 c.slen=len;
227 c.tag=Ptag;
228 c.xclass=Pclass;
229 c.max=(length == 0)?0:(p+length);
230 if (!asn1_collate_primative(ret,&c))
231 goto err;
232 else
233 {
234 p=c.p;
235 }
236 }
237 else
238 {
239 if (len != 0)
240 {
241 if ((ret->length < len) || (ret->data == NULL))
242 {
243 if (ret->data != NULL) Free((char *)ret->data);
244 s=(unsigned char *)Malloc((int)len);
245 if (s == NULL)
246 {
247 i=ERR_R_MALLOC_FAILURE;
248 goto err;
249 }
250 }
251 else
252 s=ret->data;
253 memcpy(s,p,(int)len);
254 p+=len;
255 }
256 else
257 {
258 s=NULL;
259 if (ret->data != NULL) Free((char *)ret->data);
260 }
261
262 ret->length=(int)len;
263 ret->data=s;
264 ret->type=Ptag;
265 }
266
267 if (a != NULL) (*a)=ret;
268 *pp=p;
269 return(ret);
270err:
271 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
272 ASN1_STRING_free(ret);
273 ASN1err(ASN1_F_D2I_ASN1_BYTES,i);
274 return(NULL);
275 }
276
277
278/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapes
279 * them into the one struture that is then returned */
280/* There have been a few bug fixes for this function from
281 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
282static int asn1_collate_primative(a,c)
283ASN1_STRING *a;
284ASN1_CTX *c;
285 {
286 ASN1_STRING *os=NULL;
287 BUF_MEM b;
288 int num;
289
290 b.length=0;
291 b.max=0;
292 b.data=NULL;
293
294 if (a == NULL)
295 {
296 c->error=ERR_R_PASSED_NULL_PARAMETER;
297 goto err;
298 }
299
300 num=0;
301 for (;;)
302 {
303 if (c->inf & 1)
304 {
305 c->eos=ASN1_check_infinite_end(&c->p,
306 (long)(c->max-c->p));
307 if (c->eos) break;
308 }
309 else
310 {
311 if (c->slen <= 0) break;
312 }
313
314 c->q=c->p;
315 if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
316 == NULL)
317 {
318 c->error=ERR_R_ASN1_LIB;
319 goto err;
320 }
321
322 if (!BUF_MEM_grow(&b,num+os->length))
323 {
324 c->error=ERR_R_BUF_LIB;
325 goto err;
326 }
327 memcpy(&(b.data[num]),os->data,os->length);
328 if (!(c->inf & 1))
329 c->slen-=(c->p-c->q);
330 num+=os->length;
331 }
332
333 if (!asn1_Finish(c)) goto err;
334
335 a->length=num;
336 if (a->data != NULL) Free(a->data);
337 a->data=(unsigned char *)b.data;
338 if (os != NULL) ASN1_STRING_free(os);
339 return(1);
340err:
341 ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,c->error);
342 if (os != NULL) ASN1_STRING_free(os);
343 if (b.data != NULL) Free(b.data);
344 return(0);
345 }
346
diff --git a/src/lib/libcrypto/asn1/a_d2i_fp.c b/src/lib/libcrypto/asn1/a_d2i_fp.c
new file mode 100644
index 0000000000..d952836a91
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_d2i_fp.c
@@ -0,0 +1,201 @@
1/* crypto/asn1/a_d2i_fp.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 "buffer.h"
62#include "asn1_mac.h"
63
64#define HEADER_SIZE 8
65
66#ifndef NO_FP_API
67char *ASN1_d2i_fp(xnew,d2i,in,x)
68char *(*xnew)();
69char *(*d2i)();
70FILE *in;
71unsigned char **x;
72 {
73 BIO *b;
74 char *ret;
75
76 if ((b=BIO_new(BIO_s_file())) == NULL)
77 {
78 ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB);
79 return(NULL);
80 }
81 BIO_set_fp(b,in,BIO_NOCLOSE);
82 ret=ASN1_d2i_bio(xnew,d2i,b,x);
83 BIO_free(b);
84 return(ret);
85 }
86#endif
87
88char *ASN1_d2i_bio(xnew,d2i,in,x)
89char *(*xnew)();
90char *(*d2i)();
91BIO *in;
92unsigned char **x;
93 {
94 BUF_MEM *b;
95 unsigned char *p;
96 int i;
97 char *ret=NULL;
98 ASN1_CTX c;
99 int want=HEADER_SIZE;
100 int eos=0;
101 int off=0;
102 int len=0;
103
104 b=BUF_MEM_new();
105 if (b == NULL)
106 {
107 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
108 return(NULL);
109 }
110
111 ERR_clear_error();
112 for (;;)
113 {
114 if (want >= (len-off))
115 {
116 want-=(len-off);
117
118 if (!BUF_MEM_grow(b,len+want))
119 {
120 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123 i=BIO_read(in,&(b->data[len]),want);
124 if ((i < 0) && ((len-off) == 0))
125 {
126 ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA);
127 goto err;
128 }
129 if (i > 0)
130 len+=i;
131 }
132 /* else data already loaded */
133
134 p=(unsigned char *)&(b->data[off]);
135 c.p=p;
136 c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
137 len-off);
138 if (c.inf & 0x80)
139 {
140 unsigned long e;
141
142 e=ERR_GET_REASON(ERR_peek_error());
143 if (e != ASN1_R_TOO_LONG)
144 goto err;
145 else
146 ERR_get_error(); /* clear error */
147 }
148 i=c.p-p;/* header length */
149 off+=i; /* end of data */
150
151 if (c.inf & 1)
152 {
153 /* no data body so go round again */
154 eos++;
155 want=HEADER_SIZE;
156 }
157 else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
158 {
159 /* eos value, so go back and read another header */
160 eos--;
161 if (eos <= 0)
162 break;
163 else
164 want=HEADER_SIZE;
165 }
166 else
167 {
168 /* suck in c.slen bytes of data */
169 want=(int)c.slen;
170 if (want > (len-off))
171 {
172 want-=(len-off);
173 if (!BUF_MEM_grow(b,len+want))
174 {
175 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
176 goto err;
177 }
178 i=BIO_read(in,&(b->data[len]),want);
179 if (i <= 0)
180 {
181 ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA);
182 goto err;
183 }
184 len+=i;
185 }
186 off+=(int)c.slen;
187 if (eos <= 0)
188 {
189 break;
190 }
191 else
192 want=HEADER_SIZE;
193 }
194 }
195
196 p=(unsigned char *)b->data;
197 ret=d2i(x,&p,off);
198err:
199 if (b != NULL) BUF_MEM_free(b);
200 return(ret);
201 }
diff --git a/src/lib/libcrypto/asn1/a_digest.c b/src/lib/libcrypto/asn1/a_digest.c
new file mode 100644
index 0000000000..8ddb65b0dc
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_digest.c
@@ -0,0 +1,91 @@
1/* crypto/asn1/a_digest.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 <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "evp.h"
66#include "x509.h"
67#include "buffer.h"
68
69int ASN1_digest(i2d,type,data,md,len)
70int (*i2d)();
71EVP_MD *type;
72char *data;
73unsigned char *md;
74unsigned int *len;
75 {
76 EVP_MD_CTX ctx;
77 int i;
78 unsigned char *str,*p;
79
80 i=i2d(data,NULL);
81 if ((str=(unsigned char *)Malloc(i)) == NULL) return(0);
82 p=str;
83 i2d(data,&p);
84
85 EVP_DigestInit(&ctx,type);
86 EVP_DigestUpdate(&ctx,str,i);
87 EVP_DigestFinal(&ctx,md,len);
88 Free(str);
89 return(1);
90 }
91
diff --git a/src/lib/libcrypto/asn1/a_dup.c b/src/lib/libcrypto/asn1/a_dup.c
new file mode 100644
index 0000000000..961b4cb069
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_dup.c
@@ -0,0 +1,86 @@
1/* crypto/asn1/a_dup.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 "asn1_mac.h"
62
63#define READ_CHUNK 2048
64
65char *ASN1_dup(i2d,d2i,x)
66int (*i2d)();
67char *(*d2i)();
68char *x;
69 {
70 unsigned char *b,*p;
71 long i;
72 char *ret;
73
74 if (x == NULL) return(NULL);
75
76 i=(long)i2d(x,NULL);
77 b=(unsigned char *)Malloc((unsigned int)i+10);
78 if (b == NULL)
79 { ASN1err(ASN1_F_ASN1_DUP,ERR_R_MALLOC_FAILURE); return(NULL); }
80 p= b;
81 i=i2d(x,&p);
82 p= b;
83 ret=d2i(NULL,&p,i);
84 Free((char *)b);
85 return(ret);
86 }
diff --git a/src/lib/libcrypto/asn1/a_i2d_fp.c b/src/lib/libcrypto/asn1/a_i2d_fp.c
new file mode 100644
index 0000000000..66c3df68d5
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_i2d_fp.c
@@ -0,0 +1,119 @@
1/* crypto/asn1/a_i2d_fp.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 "buffer.h"
62#include "asn1_mac.h"
63
64#ifndef NO_FP_API
65int ASN1_i2d_fp(i2d,out,x)
66int (*i2d)();
67FILE *out;
68unsigned char *x;
69 {
70 BIO *b;
71 int ret;
72
73 if ((b=BIO_new(BIO_s_file())) == NULL)
74 {
75 ASN1err(ASN1_F_ASN1_I2D_FP,ERR_R_BUF_LIB);
76 return(0);
77 }
78 BIO_set_fp(b,out,BIO_NOCLOSE);
79 ret=ASN1_i2d_bio(i2d,b,x);
80 BIO_free(b);
81 return(ret);
82 }
83#endif
84
85int ASN1_i2d_bio(i2d,out,x)
86int (*i2d)();
87BIO *out;
88unsigned char *x;
89 {
90 char *b;
91 unsigned char *p;
92 int i,j=0,n,ret=1;
93
94 n=i2d(x,NULL);
95 b=(char *)Malloc(n);
96 if (b == NULL)
97 {
98 ASN1err(ASN1_F_ASN1_I2D_BIO,ERR_R_MALLOC_FAILURE);
99 return(0);
100 }
101
102 p=(unsigned char *)b;
103 i2d(x,&p);
104
105 for (;;)
106 {
107 i=BIO_write(out,&(b[j]),n);
108 if (i == n) break;
109 if (i <= 0)
110 {
111 ret=0;
112 break;
113 }
114 j+=i;
115 n-=i;
116 }
117 Free((char *)b);
118 return(ret);
119 }
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c
new file mode 100644
index 0000000000..df79cf99bb
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_int.c
@@ -0,0 +1,305 @@
1/* crypto/asn1/a_int.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 "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_INTEGER,ASN1_R_EXPECTING_AN_INTEGER);
64 */
65
66int i2d_ASN1_INTEGER(a,pp)
67ASN1_INTEGER *a;
68unsigned char **pp;
69 {
70 int pad=0,ret,r,i,t;
71 unsigned char *p,*pt,*n,pb=0;
72
73 if ((a == NULL) || (a->data == NULL)) return(0);
74 t=a->type;
75 if (a->length == 0)
76 ret=1;
77 else
78 {
79 ret=a->length;
80 i=a->data[0];
81 if ((t == V_ASN1_INTEGER) && (i > 127))
82 {
83 pad=1;
84 pb=0;
85 }
86 else if ((t == V_ASN1_NEG_INTEGER) && (i>128))
87 {
88 pad=1;
89 pb=0xFF;
90 }
91 ret+=pad;
92 }
93 r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
94 if (pp == NULL) return(r);
95 p= *pp;
96
97 ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
98 if (pad) *(p++)=pb;
99 if (a->length == 0)
100 *(p++)=0;
101 else if (t == V_ASN1_INTEGER)
102 {
103 memcpy(p,a->data,(unsigned int)a->length);
104 p+=a->length;
105 }
106 else
107 {
108 n=a->data;
109 pt=p;
110 for (i=a->length; i>0; i--)
111 *(p++)= (*(n++)^0xFF)+1;
112 if (!pad) *pt|=0x80;
113 }
114
115 *pp=p;
116 return(r);
117 }
118
119ASN1_INTEGER *d2i_ASN1_INTEGER(a, pp, length)
120ASN1_INTEGER **a;
121unsigned char **pp;
122long length;
123 {
124 ASN1_INTEGER *ret=NULL;
125 unsigned char *p,*to,*s;
126 long len;
127 int inf,tag,xclass;
128 int i;
129
130 if ((a == NULL) || ((*a) == NULL))
131 {
132 if ((ret=ASN1_INTEGER_new()) == NULL) return(NULL);
133 ret->type=V_ASN1_INTEGER;
134 }
135 else
136 ret=(*a);
137
138 p= *pp;
139 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
140 if (inf & 0x80)
141 {
142 i=ASN1_R_BAD_OBJECT_HEADER;
143 goto err;
144 }
145
146 if (tag != V_ASN1_INTEGER)
147 {
148 i=ASN1_R_EXPECTING_AN_INTEGER;
149 goto err;
150 }
151
152 /* We must Malloc stuff, even for 0 bytes otherwise it
153 * signifies a missing NULL parameter. */
154 s=(unsigned char *)Malloc((int)len+1);
155 if (s == NULL)
156 {
157 i=ERR_R_MALLOC_FAILURE;
158 goto err;
159 }
160 to=s;
161 if (*p & 0x80) /* a negative number */
162 {
163 ret->type=V_ASN1_NEG_INTEGER;
164 if (*p == 0xff)
165 {
166 p++;
167 len--;
168 }
169 for (i=(int)len; i>0; i--)
170 *(to++)= (*(p++)^0xFF)+1;
171 }
172 else
173 {
174 ret->type=V_ASN1_INTEGER;
175 if ((*p == 0) && (len != 1))
176 {
177 p++;
178 len--;
179 }
180 memcpy(s,p,(int)len);
181 p+=len;
182 }
183
184 if (ret->data != NULL) Free((char *)ret->data);
185 ret->data=s;
186 ret->length=(int)len;
187 if (a != NULL) (*a)=ret;
188 *pp=p;
189 return(ret);
190err:
191 ASN1err(ASN1_F_D2I_ASN1_INTEGER,i);
192 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
193 ASN1_INTEGER_free(ret);
194 return(NULL);
195 }
196
197int ASN1_INTEGER_set(a,v)
198ASN1_INTEGER *a;
199long v;
200 {
201 int i,j,k;
202 unsigned char buf[sizeof(long)+1];
203 long d;
204
205 a->type=V_ASN1_INTEGER;
206 if (a->length < (sizeof(long)+1))
207 {
208 if (a->data != NULL)
209 Free((char *)a->data);
210 if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL)
211 memset((char *)a->data,0,sizeof(long)+1);
212 }
213 if (a->data == NULL)
214 {
215 ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
216 return(0);
217 }
218 d=v;
219 if (d < 0)
220 {
221 d= -d;
222 a->type=V_ASN1_NEG_INTEGER;
223 }
224
225 for (i=0; i<sizeof(long); i++)
226 {
227 if (d == 0) break;
228 buf[i]=(int)d&0xff;
229 d>>=8;
230 }
231 j=0;
232 if (v < 0) a->data[j++]=0;
233 for (k=i-1; k >=0; k--)
234 a->data[j++]=buf[k];
235 a->length=j;
236 return(1);
237 }
238
239long ASN1_INTEGER_get(a)
240ASN1_INTEGER *a;
241 {
242 int neg=0,i;
243 long r=0;
244
245 if (a == NULL) return(0L);
246 i=a->type;
247 if (i == V_ASN1_NEG_INTEGER)
248 neg=1;
249 else if (i != V_ASN1_INTEGER)
250 return(0);
251
252 if (a->length > sizeof(long))
253 {
254 /* hmm... a bit ugly */
255 return(0xffffffffL);
256 }
257 if (a->data == NULL)
258 return(0);
259
260 for (i=0; i<a->length; i++)
261 {
262 r<<=8;
263 r|=(unsigned char)a->data[i];
264 }
265 if (neg) r= -r;
266 return(r);
267 }
268
269ASN1_INTEGER *BN_to_ASN1_INTEGER(bn,ai)
270BIGNUM *bn;
271ASN1_INTEGER *ai;
272 {
273 ASN1_INTEGER *ret;
274 int len,j;
275
276 if (ai == NULL)
277 ret=ASN1_INTEGER_new();
278 else
279 ret=ai;
280 if (ret == NULL)
281 {
282 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ASN1_R_ERROR_STACK);
283 goto err;
284 }
285 ret->type=V_ASN1_INTEGER;
286 j=BN_num_bits(bn);
287 len=((j == 0)?0:((j/8)+1));
288 ret->data=(unsigned char *)Malloc(len+4);
289 ret->length=BN_bn2bin(bn,ret->data);
290 return(ret);
291err:
292 if (ret != ai) ASN1_INTEGER_free(ret);
293 return(NULL);
294 }
295
296BIGNUM *ASN1_INTEGER_to_BN(ai,bn)
297ASN1_INTEGER *ai;
298BIGNUM *bn;
299 {
300 BIGNUM *ret;
301
302 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
303 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
304 return(ret);
305 }
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
new file mode 100644
index 0000000000..5a7eeef8d8
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_object.c
@@ -0,0 +1,389 @@
1/* crypto/asn1/a_object.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 "buffer.h"
62#include "asn1.h"
63#include "objects.h"
64
65/* ASN1err(ASN1_F_ASN1_OBJECT_NEW,ASN1_R_EXPECTING_AN_OBJECT);
66 * ASN1err(ASN1_F_D2I_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER);
67 * ASN1err(ASN1_F_I2T_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER);
68 */
69
70int i2d_ASN1_OBJECT(a, pp)
71ASN1_OBJECT *a;
72unsigned char **pp;
73 {
74 unsigned char *p;
75
76 if ((a == NULL) || (a->data == NULL)) return(0);
77
78 if (pp == NULL)
79 return(ASN1_object_size(0,a->length,V_ASN1_OBJECT));
80
81 p= *pp;
82 ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
83 memcpy(p,a->data,a->length);
84 p+=a->length;
85
86 *pp=p;
87 return(a->length);
88 }
89
90int a2d_ASN1_OBJECT(out,olen,buf,num)
91unsigned char *out;
92int olen;
93char *buf;
94int num;
95 {
96 int i,first,len=0,c;
97 char tmp[24],*p;
98 unsigned long l;
99
100 if (num == 0)
101 return(0);
102 else if (num == -1)
103 num=strlen(buf);
104
105 p=buf;
106 c= *(p++);
107 num--;
108 if ((c >= '0') && (c <= '2'))
109 {
110 first=(c-'0')*40;
111 }
112 else
113 {
114 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
115 goto err;
116 }
117
118 if (num <= 0)
119 {
120 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
121 goto err;
122 }
123 c= *(p++);
124 num--;
125 for (;;)
126 {
127 if (num <= 0) break;
128 if ((c != '.') && (c != ' '))
129 {
130 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
131 goto err;
132 }
133 l=0;
134 for (;;)
135 {
136 if (num <= 0) break;
137 num--;
138 c= *(p++);
139 if ((c == ' ') || (c == '.'))
140 break;
141 if ((c < '0') || (c > '9'))
142 {
143 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
144 goto err;
145 }
146 l=l*10L+(long)(c-'0');
147 }
148 if (len == 0)
149 {
150 if ((first < 2) && (l >= 40))
151 {
152 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
153 goto err;
154 }
155 l+=(long)first;
156 }
157 i=0;
158 for (;;)
159 {
160 tmp[i++]=(unsigned char)l&0x7f;
161 l>>=7L;
162 if (l == 0L) break;
163 }
164 if (out != NULL)
165 {
166 if (len+i > olen)
167 {
168 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
169 goto err;
170 }
171 while (--i > 0)
172 out[len++]=tmp[i]|0x80;
173 out[len++]=tmp[0];
174 }
175 else
176 len+=i;
177 }
178 return(len);
179err:
180 return(0);
181 }
182
183int i2t_ASN1_OBJECT(buf,buf_len,a)
184char *buf;
185int buf_len;
186ASN1_OBJECT *a;
187 {
188 int i,idx=0,n=0,len,nid;
189 unsigned long l;
190 unsigned char *p;
191 char *s;
192 char tbuf[32];
193
194 if (buf_len <= 0) return(0);
195
196 if ((a == NULL) || (a->data == NULL))
197 {
198 buf[0]='\0';
199 return(0);
200 }
201
202 nid=OBJ_obj2nid(a);
203 if (nid == NID_undef)
204 {
205 len=a->length;
206 p=a->data;
207
208 idx=0;
209 l=0;
210 while (idx < a->length)
211 {
212 l|=(p[idx]&0x7f);
213 if (!(p[idx] & 0x80)) break;
214 l<<=7L;
215 idx++;
216 }
217 idx++;
218 i=(int)(l/40);
219 if (i > 2) i=2;
220 l-=(long)(i*40);
221
222 sprintf(tbuf,"%d.%ld",i,l);
223 i=strlen(tbuf);
224 strncpy(buf,tbuf,buf_len);
225 buf_len-=i;
226 buf+=i;
227 n+=i;
228
229 l=0;
230 for (; idx<len; idx++)
231 {
232 l|=p[idx]&0x7f;
233 if (!(p[idx] & 0x80))
234 {
235 sprintf(tbuf,".%ld",l);
236 i=strlen(tbuf);
237 if (buf_len > 0)
238 strncpy(buf,tbuf,buf_len);
239 buf_len-=i;
240 buf+=i;
241 n+=i;
242 l=0;
243 }
244 l<<=7L;
245 }
246 }
247 else
248 {
249 s=(char *)OBJ_nid2ln(nid);
250 if (s == NULL)
251 s=(char *)OBJ_nid2sn(nid);
252 strncpy(buf,s,buf_len);
253 n=strlen(s);
254 }
255 buf[buf_len-1]='\0';
256 return(n);
257 }
258
259int i2a_ASN1_OBJECT(bp,a)
260BIO *bp;
261ASN1_OBJECT *a;
262 {
263 char buf[80];
264 int i;
265
266 if ((a == NULL) || (a->data == NULL))
267 return(BIO_write(bp,"NULL",4));
268 i=i2t_ASN1_OBJECT(buf,80,a);
269 if (i > 80) i=80;
270 BIO_write(bp,buf,i);
271 return(i);
272 }
273
274ASN1_OBJECT *d2i_ASN1_OBJECT(a, pp, length)
275ASN1_OBJECT **a;
276unsigned char **pp;
277long length;
278 {
279 ASN1_OBJECT *ret=NULL;
280 unsigned char *p;
281 long len;
282 int tag,xclass;
283 int inf,i;
284
285 /* only the ASN1_OBJECTs from the 'table' will have values
286 * for ->sn or ->ln */
287 if ((a == NULL) || ((*a) == NULL) ||
288 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
289 {
290 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
291 }
292 else ret=(*a);
293
294 p= *pp;
295
296 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
297 if (inf & 0x80)
298 {
299 i=ASN1_R_BAD_OBJECT_HEADER;
300 goto err;
301 }
302
303 if (tag != V_ASN1_OBJECT)
304 {
305 i=ASN1_R_EXPECTING_AN_OBJECT;
306 goto err;
307 }
308 if ((ret->data == NULL) || (ret->length < len))
309 {
310 if (ret->data != NULL) Free((char *)ret->data);
311 ret->data=(unsigned char *)Malloc((int)len);
312 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
313 if (ret->data == NULL)
314 { i=ERR_R_MALLOC_FAILURE; goto err; }
315 }
316 memcpy(ret->data,p,(int)len);
317 ret->length=(int)len;
318 ret->sn=NULL;
319 ret->ln=NULL;
320 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
321 p+=len;
322
323 if (a != NULL) (*a)=ret;
324 *pp=p;
325 return(ret);
326err:
327 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
328 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
329 ASN1_OBJECT_free(ret);
330 return(NULL);
331 }
332
333ASN1_OBJECT *ASN1_OBJECT_new()
334 {
335 ASN1_OBJECT *ret;
336
337 ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
338 if (ret == NULL)
339 {
340 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
341 return(NULL);
342 }
343 ret->length=0;
344 ret->data=NULL;
345 ret->nid=0;
346 ret->sn=NULL;
347 ret->ln=NULL;
348 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
349 return(ret);
350 }
351
352void ASN1_OBJECT_free(a)
353ASN1_OBJECT *a;
354 {
355 if (a == NULL) return;
356 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
357 {
358 if (a->sn != NULL) Free(a->sn);
359 if (a->ln != NULL) Free(a->ln);
360 a->sn=a->ln=NULL;
361 }
362 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
363 {
364 if (a->data != NULL) Free(a->data);
365 a->data=NULL;
366 a->length=0;
367 }
368 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
369 Free((char *)a);
370 }
371
372ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln)
373int nid;
374unsigned char *data;
375int len;
376char *sn,*ln;
377 {
378 ASN1_OBJECT o;
379
380 o.sn=sn;
381 o.ln=ln;
382 o.data=data;
383 o.nid=nid;
384 o.length=len;
385 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|
386 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA;
387 return(OBJ_dup(&o));
388 }
389
diff --git a/src/lib/libcrypto/asn1/a_octet.c b/src/lib/libcrypto/asn1/a_octet.c
new file mode 100644
index 0000000000..be3f172a8c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_octet.c
@@ -0,0 +1,90 @@
1/* crypto/asn1/a_octet.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 "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_EXPECTING_AN_OCTET_STRING);
64 */
65
66int i2d_ASN1_OCTET_STRING(a, pp)
67ASN1_OCTET_STRING *a;
68unsigned char **pp;
69 {
70 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
71 V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL));
72 }
73
74ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(a, pp, length)
75ASN1_OCTET_STRING **a;
76unsigned char **pp;
77long length;
78 {
79 ASN1_OCTET_STRING *ret=NULL;
80
81 ret=(ASN1_OCTET_STRING *)d2i_ASN1_bytes((ASN1_STRING **)a,
82 pp,length,V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL);
83 if (ret == NULL)
84 {
85 ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_ERROR_STACK);
86 return(NULL);
87 }
88 return(ret);
89 }
90
diff --git a/src/lib/libcrypto/asn1/a_print.c b/src/lib/libcrypto/asn1/a_print.c
new file mode 100644
index 0000000000..3023361dee
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_print.c
@@ -0,0 +1,161 @@
1/* crypto/asn1/a_print.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 "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_WRONG_PRINTABLE_TYPE);
64 * ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_TAG_VALUE_TOO_HIGH);
65 */
66
67int i2d_ASN1_IA5STRING(a,pp)
68ASN1_IA5STRING *a;
69unsigned char **pp;
70 { return(M_i2d_ASN1_IA5STRING(a,pp)); }
71
72ASN1_IA5STRING *d2i_ASN1_IA5STRING(a,pp,l)
73ASN1_IA5STRING **a;
74unsigned char **pp;
75long l;
76 { return(M_d2i_ASN1_IA5STRING(a,pp,l)); }
77
78ASN1_T61STRING *d2i_ASN1_T61STRING(a,pp,l)
79ASN1_T61STRING **a;
80unsigned char **pp;
81long l;
82 { return(M_d2i_ASN1_T61STRING(a,pp,l)); }
83
84ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(a,pp,l)
85ASN1_PRINTABLESTRING **a;
86unsigned char **pp;
87long l;
88 { return(M_d2i_ASN1_PRINTABLESTRING(a,pp,l)); }
89
90int i2d_ASN1_PRINTABLE(a,pp)
91ASN1_STRING *a;
92unsigned char **pp;
93 { return(M_i2d_ASN1_PRINTABLE(a,pp)); }
94
95ASN1_STRING *d2i_ASN1_PRINTABLE(a,pp,l)
96ASN1_STRING **a;
97unsigned char **pp;
98long l;
99 { return(M_d2i_ASN1_PRINTABLE(a,pp,l)); }
100
101int ASN1_PRINTABLE_type(s,len)
102unsigned char *s;
103int len;
104 {
105 int c;
106 int ia5=0;
107 int t61=0;
108
109 if (len <= 0) len= -1;
110 if (s == NULL) return(V_ASN1_PRINTABLESTRING);
111
112 while ((*s) && (len-- != 0))
113 {
114 c= *(s++);
115 if (!( ((c >= 'a') && (c <= 'z')) ||
116 ((c >= 'A') && (c <= 'Z')) ||
117 (c == ' ') ||
118 ((c >= '0') && (c <= '9')) ||
119 (c == ' ') || (c == '\'') ||
120 (c == '(') || (c == ')') ||
121 (c == '+') || (c == ',') ||
122 (c == '-') || (c == '.') ||
123 (c == '/') || (c == ':') ||
124 (c == '=') || (c == '?')))
125 ia5=1;
126 if (c&0x80)
127 t61=1;
128 }
129 if (t61) return(V_ASN1_T61STRING);
130 if (ia5) return(V_ASN1_IA5STRING);
131 return(V_ASN1_PRINTABLESTRING);
132 }
133
134int ASN1_UNIVERSALSTRING_to_string(s)
135ASN1_UNIVERSALSTRING *s;
136 {
137 int i;
138 unsigned char *p;
139
140 if (s->type != V_ASN1_UNIVERSALSTRING) return(0);
141 if ((s->length%4) != 0) return(0);
142 p=s->data;
143 for (i=0; i<s->length; i+=4)
144 {
145 if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
146 break;
147 else
148 p+=4;
149 }
150 if (i < s->length) return(0);
151 p=s->data;
152 for (i=3; i<s->length; i+=4)
153 {
154 *(p++)=s->data[i];
155 }
156 *(p)='\0';
157 s->length/=4;
158 s->type=ASN1_PRINTABLE_type(s->data,s->length);
159 return(1);
160 }
161
diff --git a/src/lib/libcrypto/asn1/a_set.c b/src/lib/libcrypto/asn1/a_set.c
new file mode 100644
index 0000000000..17c49946cf
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_set.c
@@ -0,0 +1,149 @@
1/* crypto/asn1/a_set.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 "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ERR_R_MALLOC_FAILURE);
64 */
65
66int i2d_ASN1_SET(a,pp,func,ex_tag,ex_class)
67STACK *a;
68unsigned char **pp;
69int (*func)();
70int ex_tag;
71int ex_class;
72 {
73 int ret=0,r;
74 int i;
75 unsigned char *p;
76
77 if (a == NULL) return(0);
78 for (i=sk_num(a)-1; i>=0; i--)
79 ret+=func(sk_value(a,i),NULL);
80 r=ASN1_object_size(1,ret,ex_tag);
81 if (pp == NULL) return(r);
82
83 p= *pp;
84 ASN1_put_object(&p,1,ret,ex_tag,ex_class);
85 for (i=0; i<sk_num(a); i++)
86 func(sk_value(a,i),&p);
87
88 *pp=p;
89 return(r);
90 }
91
92STACK *d2i_ASN1_SET(a,pp,length,func,ex_tag,ex_class)
93STACK **a;
94unsigned char **pp;
95long length;
96char *(*func)();
97int ex_tag;
98int ex_class;
99 {
100 ASN1_CTX c;
101 STACK *ret=NULL;
102
103 if ((a == NULL) || ((*a) == NULL))
104 { if ((ret=sk_new(NULL)) == NULL) goto err; }
105 else
106 ret=(*a);
107
108 c.p= *pp;
109 c.max=(length == 0)?0:(c.p+length);
110
111 c.inf=ASN1_get_object(&c.p,&c.slen,&c.tag,&c.xclass,c.max-c.p);
112 if (c.inf & 0x80) goto err;
113 if (ex_class != c.xclass)
114 {
115 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_CLASS);
116 goto err;
117 }
118 if (ex_tag != c.tag)
119 {
120 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_TAG);
121 goto err;
122 }
123 if ((c.slen+c.p) > c.max)
124 {
125 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR);
126 goto err;
127 }
128 /* check for infinite constructed - it can be as long
129 * as the amount of data passed to us */
130 if (c.inf == (V_ASN1_CONSTRUCTED+1))
131 c.slen=length+ *pp-c.p;
132 c.max=c.p+c.slen;
133
134 while (c.p < c.max)
135 {
136 char *s;
137
138 if (M_ASN1_D2I_end_sequence()) break;
139 if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) goto err;
140 if (!sk_push(ret,s)) goto err;
141 }
142 if (a != NULL) (*a)=ret;
143 *pp=c.p;
144 return(ret);
145err:
146 if ((ret != NULL) && ((a == NULL) || (*a != ret))) sk_free(ret);
147 return(NULL);
148 }
149
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
new file mode 100644
index 0000000000..02188e68c4
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_sign.c
@@ -0,0 +1,147 @@
1/* crypto/asn1/a_sign.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 <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "bn.h"
66#include "evp.h"
67#include "x509.h"
68#include "objects.h"
69#include "buffer.h"
70#include "pem.h"
71
72int ASN1_sign(i2d,algor1,algor2,signature,data,pkey,type)
73int (*i2d)();
74X509_ALGOR *algor1;
75X509_ALGOR *algor2;
76ASN1_BIT_STRING *signature;
77char *data;
78EVP_PKEY *pkey;
79EVP_MD *type;
80 {
81 EVP_MD_CTX ctx;
82 unsigned char *p,*buf_in=NULL,*buf_out=NULL;
83 int i,inl=0,outl=0,outll=0;
84 X509_ALGOR *a;
85
86 for (i=0; i<2; i++)
87 {
88 if (i == 0)
89 a=algor1;
90 else
91 a=algor2;
92 if (a == NULL) continue;
93 if ( (a->parameter == NULL) ||
94 (a->parameter->type != V_ASN1_NULL))
95 {
96 ASN1_TYPE_free(a->parameter);
97 if ((a->parameter=ASN1_TYPE_new()) == NULL) goto err;
98 a->parameter->type=V_ASN1_NULL;
99 }
100 ASN1_OBJECT_free(a->algorithm);
101 a->algorithm=OBJ_nid2obj(type->pkey_type);
102 if (a->algorithm == NULL)
103 {
104 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_UNKNOWN_OBJECT_TYPE);
105 goto err;
106 }
107 if (a->algorithm->length == 0)
108 {
109 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
110 goto err;
111 }
112 }
113 inl=i2d(data,NULL);
114 buf_in=(unsigned char *)Malloc((unsigned int)inl);
115 outll=outl=EVP_PKEY_size(pkey);
116 buf_out=(unsigned char *)Malloc((unsigned int)outl);
117 if ((buf_in == NULL) || (buf_out == NULL))
118 {
119 outl=0;
120 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123 p=buf_in;
124
125 i2d(data,&p);
126 EVP_SignInit(&ctx,type);
127 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
128 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
129 (unsigned int *)&outl,pkey))
130 {
131 outl=0;
132 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_EVP_LIB);
133 goto err;
134 }
135 if (signature->data != NULL) Free((char *)signature->data);
136 signature->data=buf_out;
137 buf_out=NULL;
138 signature->length=outl;
139
140err:
141 memset(&ctx,0,sizeof(ctx));
142 if (buf_in != NULL)
143 { memset((char *)buf_in,0,(unsigned int)inl); Free((char *)buf_in); }
144 if (buf_out != NULL)
145 { memset((char *)buf_out,0,outll); Free((char *)buf_out); }
146 return(outl);
147 }
diff --git a/src/lib/libcrypto/asn1/a_type.c b/src/lib/libcrypto/asn1/a_type.c
new file mode 100644
index 0000000000..7c0004084c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_type.c
@@ -0,0 +1,325 @@
1/* crypto/asn1/a_type.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 "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK);
64 * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_ERROR_STACK);
65 * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_WRONG_TAG);
66 * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
67 */
68
69#ifndef NOPROTO
70static void ASN1_TYPE_component_free(ASN1_TYPE *a);
71#else
72static void ASN1_TYPE_component_free();
73#endif
74
75int i2d_ASN1_TYPE(a,pp)
76ASN1_TYPE *a;
77unsigned char **pp;
78 {
79 int r=0;
80
81 if (a == NULL) return(0);
82
83 switch (a->type)
84 {
85 case V_ASN1_NULL:
86 if (pp != NULL)
87 ASN1_put_object(pp,0,0,V_ASN1_NULL,V_ASN1_UNIVERSAL);
88 r=2;
89 break;
90 case V_ASN1_INTEGER:
91 case V_ASN1_NEG_INTEGER:
92 r=i2d_ASN1_INTEGER(a->value.integer,pp);
93 break;
94 case V_ASN1_BIT_STRING:
95 r=i2d_ASN1_BIT_STRING(a->value.bit_string,pp);
96 break;
97 case V_ASN1_OCTET_STRING:
98 r=i2d_ASN1_OCTET_STRING(a->value.octet_string,pp);
99 break;
100 case V_ASN1_OBJECT:
101 r=i2d_ASN1_OBJECT(a->value.object,pp);
102 break;
103 case V_ASN1_PRINTABLESTRING:
104 r=M_i2d_ASN1_PRINTABLESTRING(a->value.printablestring,pp);
105 break;
106 case V_ASN1_T61STRING:
107 r=M_i2d_ASN1_T61STRING(a->value.t61string,pp);
108 break;
109 case V_ASN1_IA5STRING:
110 r=M_i2d_ASN1_IA5STRING(a->value.ia5string,pp);
111 break;
112 case V_ASN1_GENERALSTRING:
113 r=M_i2d_ASN1_GENERALSTRING(a->value.generalstring,pp);
114 break;
115 case V_ASN1_UNIVERSALSTRING:
116 r=M_i2d_ASN1_UNIVERSALSTRING(a->value.universalstring,pp);
117 break;
118 case V_ASN1_BMPSTRING:
119 r=M_i2d_ASN1_BMPSTRING(a->value.bmpstring,pp);
120 break;
121 case V_ASN1_UTCTIME:
122 r=i2d_ASN1_UTCTIME(a->value.utctime,pp);
123 break;
124 case V_ASN1_SET:
125 case V_ASN1_SEQUENCE:
126 if (a->value.set == NULL)
127 r=0;
128 else
129 {
130 r=a->value.set->length;
131 if (pp != NULL)
132 {
133 memcpy(*pp,a->value.set->data,r);
134 *pp+=r;
135 }
136 }
137 break;
138 }
139 return(r);
140 }
141
142ASN1_TYPE *d2i_ASN1_TYPE(a,pp,length)
143ASN1_TYPE **a;
144unsigned char **pp;
145long length;
146 {
147 ASN1_TYPE *ret=NULL;
148 unsigned char *q,*p,*max;
149 int inf,tag,xclass;
150 long len;
151
152 if ((a == NULL) || ((*a) == NULL))
153 {
154 if ((ret=ASN1_TYPE_new()) == NULL) goto err;
155 }
156 else
157 ret=(*a);
158
159 p= *pp;
160 q=p;
161 max=(p+length);
162
163 inf=ASN1_get_object(&q,&len,&tag,&xclass,length);
164 if (inf & 0x80) goto err;
165
166 ASN1_TYPE_component_free(ret);
167
168 switch (tag)
169 {
170 case V_ASN1_NULL:
171 p=q;
172 ret->value.ptr=NULL;
173 break;
174 case V_ASN1_INTEGER:
175 if ((ret->value.integer=
176 d2i_ASN1_INTEGER(NULL,&p,max-p)) == NULL)
177 goto err;
178 break;
179 case V_ASN1_BIT_STRING:
180 if ((ret->value.bit_string=
181 d2i_ASN1_BIT_STRING(NULL,&p,max-p)) == NULL)
182 goto err;
183 break;
184 case V_ASN1_OCTET_STRING:
185 if ((ret->value.octet_string=
186 d2i_ASN1_OCTET_STRING(NULL,&p,max-p)) == NULL)
187 goto err;
188 break;
189 case V_ASN1_OBJECT:
190 if ((ret->value.object=
191 d2i_ASN1_OBJECT(NULL,&p,max-p)) == NULL)
192 goto err;
193 break;
194 case V_ASN1_PRINTABLESTRING:
195 if ((ret->value.printablestring=
196 d2i_ASN1_PRINTABLESTRING(NULL,&p,max-p)) == NULL)
197 goto err;
198 break;
199 case V_ASN1_T61STRING:
200 if ((ret->value.t61string=
201 M_d2i_ASN1_T61STRING(NULL,&p,max-p)) == NULL)
202 goto err;
203 break;
204 case V_ASN1_IA5STRING:
205 if ((ret->value.ia5string=
206 M_d2i_ASN1_IA5STRING(NULL,&p,max-p)) == NULL)
207 goto err;
208 break;
209 case V_ASN1_GENERALSTRING:
210 if ((ret->value.generalstring=
211 M_d2i_ASN1_GENERALSTRING(NULL,&p,max-p)) == NULL)
212 goto err;
213 break;
214 case V_ASN1_UNIVERSALSTRING:
215 if ((ret->value.universalstring=
216 M_d2i_ASN1_UNIVERSALSTRING(NULL,&p,max-p)) == NULL)
217 goto err;
218 break;
219 case V_ASN1_BMPSTRING:
220 if ((ret->value.bmpstring=
221 M_d2i_ASN1_BMPSTRING(NULL,&p,max-p)) == NULL)
222 goto err;
223 break;
224 case V_ASN1_UTCTIME:
225 if ((ret->value.utctime=
226 d2i_ASN1_UTCTIME(NULL,&p,max-p)) == NULL)
227 goto err;
228 break;
229 case V_ASN1_SET:
230 case V_ASN1_SEQUENCE:
231 /* Sets and sequences are left complete */
232 if ((ret->value.set=ASN1_STRING_new()) == NULL) goto err;
233 ret->value.set->type=tag;
234 len+=(q-p);
235 if (!ASN1_STRING_set(ret->value.set,p,(int)len)) goto err;
236 p+=len;
237 break;
238 default:
239 ASN1err(ASN1_F_D2I_ASN1_TYPE,ASN1_R_BAD_TYPE);
240 goto err;
241 }
242
243 ret->type=tag;
244 if (a != NULL) (*a)=ret;
245 *pp=p;
246 return(ret);
247err:
248 if ((ret != NULL) && ((a == NULL) || (*a != ret))) ASN1_TYPE_free(ret);
249 return(NULL);
250 }
251
252ASN1_TYPE *ASN1_TYPE_new()
253 {
254 ASN1_TYPE *ret=NULL;
255
256 M_ASN1_New_Malloc(ret,ASN1_TYPE);
257 ret->type= -1;
258 ret->value.ptr=NULL;
259 return(ret);
260 M_ASN1_New_Error(ASN1_F_ASN1_TYPE_NEW);
261 }
262
263void ASN1_TYPE_free(a)
264ASN1_TYPE *a;
265 {
266 if (a == NULL) return;
267 ASN1_TYPE_component_free(a);
268 Free((char *)(char *)a);
269 }
270
271int ASN1_TYPE_get(a)
272ASN1_TYPE *a;
273 {
274 if (a->value.ptr != NULL)
275 return(a->type);
276 else
277 return(0);
278 }
279
280void ASN1_TYPE_set(a,type,value)
281ASN1_TYPE *a;
282int type;
283char *value;
284 {
285 if (a->value.ptr != NULL)
286 ASN1_TYPE_component_free(a);
287 a->type=type;
288 a->value.ptr=value;
289 }
290
291static void ASN1_TYPE_component_free(a)
292ASN1_TYPE *a;
293 {
294 if (a == NULL) return;
295
296 if (a->value.ptr != NULL)
297 {
298 switch (a->type)
299 {
300 case V_ASN1_OBJECT:
301 ASN1_OBJECT_free(a->value.object);
302 break;
303 case V_ASN1_INTEGER:
304 case V_ASN1_NEG_INTEGER:
305 case V_ASN1_BIT_STRING:
306 case V_ASN1_OCTET_STRING:
307 case V_ASN1_PRINTABLESTRING:
308 case V_ASN1_T61STRING:
309 case V_ASN1_IA5STRING:
310 case V_ASN1_UNIVERSALSTRING:
311 case V_ASN1_GENERALSTRING:
312 case V_ASN1_UTCTIME:
313 case V_ASN1_SET:
314 case V_ASN1_SEQUENCE:
315 ASN1_STRING_free((ASN1_STRING *)a->value.ptr);
316 break;
317 default:
318 /* MEMORY LEAK */
319 break;
320 }
321 a->type=0;
322 a->value.ptr=NULL;
323 }
324 }
325
diff --git a/src/lib/libcrypto/asn1/a_verify.c b/src/lib/libcrypto/asn1/a_verify.c
new file mode 100644
index 0000000000..03fc63dbef
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_verify.c
@@ -0,0 +1,121 @@
1/* crypto/asn1/a_verify.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 <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "bn.h"
66#include "x509.h"
67#include "objects.h"
68#include "buffer.h"
69#include "evp.h"
70#include "pem.h"
71
72int ASN1_verify(i2d,a,signature,data,pkey)
73int (*i2d)();
74X509_ALGOR *a;
75ASN1_BIT_STRING *signature;
76char *data;
77EVP_PKEY *pkey;
78 {
79 EVP_MD_CTX ctx;
80 EVP_MD *type;
81 unsigned char *p,*buf_in=NULL;
82 int ret= -1,i,inl;
83
84 i=OBJ_obj2nid(a->algorithm);
85 type=EVP_get_digestbyname(OBJ_nid2sn(i));
86 if (type == NULL)
87 {
88 ASN1err(ASN1_F_ASN1_VERIFY,ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
89 goto err;
90 }
91
92 inl=i2d(data,NULL);
93 buf_in=(unsigned char *)Malloc((unsigned int)inl);
94 if (buf_in == NULL)
95 {
96 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_MALLOC_FAILURE);
97 goto err;
98 }
99 p=buf_in;
100
101 i2d(data,&p);
102 EVP_VerifyInit(&ctx,type);
103 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
104
105 memset(buf_in,0,(unsigned int)inl);
106 Free((char *)buf_in);
107
108 if (EVP_VerifyFinal(&ctx,(unsigned char *)signature->data,
109 (unsigned int)signature->length,pkey) <= 0)
110 {
111 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB);
112 ret=0;
113 goto err;
114 }
115 /* we don't need to zero the 'ctx' because we just checked
116 * public information */
117 /* memset(&ctx,0,sizeof(ctx)); */
118 ret=1;
119err:
120 return(ret);
121 }
diff --git a/src/lib/libcrypto/asn1/asn1.h b/src/lib/libcrypto/asn1/asn1.h
new file mode 100644
index 0000000000..9793db365d
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1.h
@@ -0,0 +1,859 @@
1/* crypto/asn1/asn1.h */
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#ifndef HEADER_ASN1_H
60#define HEADER_ASN1_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include <time.h>
67#include "bn.h"
68#include "stack.h"
69
70#define V_ASN1_UNIVERSAL 0x00
71#define V_ASN1_APPLICATION 0x40
72#define V_ASN1_CONTEXT_SPECIFIC 0x80
73#define V_ASN1_PRIVATE 0xc0
74
75#define V_ASN1_CONSTRUCTED 0x20
76#define V_ASN1_PRIMATIVE_TAG 0x1f
77
78#define V_ASN1_APP_CHOOSE -2 /* let the recipent choose */
79
80#define V_ASN1_UNDEF -1
81#define V_ASN1_EOC 0
82#define V_ASN1_BOOLEAN 1 /**/
83#define V_ASN1_INTEGER 2
84#define V_ASN1_NEG_INTEGER (2+0x100)
85#define V_ASN1_BIT_STRING 3
86#define V_ASN1_OCTET_STRING 4
87#define V_ASN1_NULL 5
88#define V_ASN1_OBJECT 6
89#define V_ASN1_OBJECT_DESCRIPTOR 7
90#define V_ASN1_EXTERNAL 8
91#define V_ASN1_REAL 9
92#define V_ASN1_ENUMERATED 10 /* microsoft weirdness */
93#define V_ASN1_SEQUENCE 16
94#define V_ASN1_SET 17
95#define V_ASN1_NUMERICSTRING 18 /**/
96#define V_ASN1_PRINTABLESTRING 19
97#define V_ASN1_T61STRING 20
98#define V_ASN1_TELETEXSTRING 20 /* alias */
99#define V_ASN1_VIDEOTEXSTRING 21 /**/
100#define V_ASN1_IA5STRING 22
101#define V_ASN1_UTCTIME 23
102#define V_ASN1_GENERALIZEDTIME 24 /**/
103#define V_ASN1_GRAPHICSTRING 25 /**/
104#define V_ASN1_ISO64STRING 26 /**/
105#define V_ASN1_VISIBLESTRING 26 /* alias */
106#define V_ASN1_GENERALSTRING 27 /**/
107#define V_ASN1_UNIVERSALSTRING 28 /**/
108#define V_ASN1_BMPSTRING 30
109
110/* For use with d2i_ASN1_type_bytes() */
111#define B_ASN1_NUMERICSTRING 0x0001
112#define B_ASN1_PRINTABLESTRING 0x0002
113#define B_ASN1_T61STRING 0x0004
114#define B_ASN1_VIDEOTEXSTRING 0x0008
115#define B_ASN1_IA5STRING 0x0010
116#define B_ASN1_GRAPHICSTRING 0x0020
117#define B_ASN1_ISO64STRING 0x0040
118#define B_ASN1_GENERALSTRING 0x0080
119#define B_ASN1_UNIVERSALSTRING 0x0100
120#define B_ASN1_OCTET_STRING 0x0200
121#define B_ASN1_BIT_STRING 0x0400
122#define B_ASN1_BMPSTRING 0x0800
123#define B_ASN1_UNKNOWN 0x1000
124
125#ifndef DEBUG
126
127#define ASN1_INTEGER ASN1_STRING
128#define ASN1_BIT_STRING ASN1_STRING
129#define ASN1_OCTET_STRING ASN1_STRING
130#define ASN1_PRINTABLESTRING ASN1_STRING
131#define ASN1_T61STRING ASN1_STRING
132#define ASN1_IA5STRING ASN1_STRING
133#define ASN1_UTCTIME ASN1_STRING
134#define ASN1_GENERALIZEDTIME ASN1_STRING
135#define ASN1_GENERALSTRING ASN1_STRING
136#define ASN1_UNIVERSALSTRING ASN1_STRING
137#define ASN1_BMPSTRING ASN1_STRING
138
139#else
140
141typedef struct asn1_integer_st
142 {
143 int length;
144 int type;
145 unsigned char *data;
146 } ASN1_INTEGER;
147
148typedef struct asn1_bit_string_st
149 {
150 int length;
151 int type;
152 unsigned char *data;
153 } ASN1_BIT_STRING;
154
155typedef struct asn1_octet_string_st
156 {
157 int length;
158 int type;
159 unsigned char *data;
160 } ASN1_OCTET_STRING;
161
162typedef struct asn1_printablestring_st
163 {
164 int length;
165 int type;
166 unsigned char *data;
167 } ASN1_PRINTABLESTRING;
168
169typedef struct asn1_t61string_st
170 {
171 int length;
172 int type;
173 unsigned char *data;
174 } ASN1_T61STRING;
175
176typedef struct asn1_ia5string_st
177 {
178 int length;
179 int type;
180 unsigned char *data;
181 } ASN1_IA5STRING;
182
183typedef struct asn1_generalstring_st
184 {
185 int length;
186 int type;
187 unsigned char *data;
188 } ASN1_GENERALSTRING;
189
190typedef struct asn1_universalstring_st
191 {
192 int length;
193 int type;
194 unsigned char *data;
195 } ASN1_UNIVERSALSTRING;
196
197typedef struct asn1_bmpstring_st
198 {
199 int length;
200 int type;
201 unsigned char *data;
202 } ASN1_BMPSTRING;
203
204typedef struct asn1_utctime_st
205 {
206 int length;
207 int type;
208 unsigned char *data;
209 } ASN1_UTCTIME;
210
211typedef struct asn1_generalizedtime_st
212 {
213 int length;
214 int type;
215 unsigned char *data;
216 } ASN1_GENERALIZEDTIME;
217
218#endif
219
220typedef struct asn1_ctx_st
221 {
222 unsigned char *p;/* work char pointer */
223 int eos; /* end of sequence read for indefinite encoding */
224 int error; /* error code to use when returning an error */
225 int inf; /* constructed if 0x20, indefinite is 0x21 */
226 int tag; /* tag from last 'get object' */
227 int xclass; /* class from last 'get object' */
228 long slen; /* length of last 'get object' */
229 unsigned char *max; /* largest value of p alowed */
230 unsigned char *q;/* temporary variable */
231 unsigned char **pp;/* variable */
232 } ASN1_CTX;
233
234/* These are used internally in the ASN1_OBJECT to keep track of
235 * whether the names and data need to be free()ed */
236#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
237#define ASN1_OBJECT_FLAG_CRITICAL 0x02 /* critical x509v3 object id */
238#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
239#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
240typedef struct asn1_object_st
241 {
242 char *sn,*ln;
243 int nid;
244 int length;
245 unsigned char *data;
246 int flags; /* Should we free this one */
247 } ASN1_OBJECT;
248
249/* This is the base type that holds just about everything :-) */
250typedef struct asn1_string_st
251 {
252 int length;
253 int type;
254 unsigned char *data;
255 } ASN1_STRING;
256
257typedef struct asn1_type_st
258 {
259 int type;
260 union {
261 char *ptr;
262 ASN1_STRING * asn1_string;
263 ASN1_OBJECT * object;
264 ASN1_INTEGER * integer;
265 ASN1_BIT_STRING * bit_string;
266 ASN1_OCTET_STRING * octet_string;
267 ASN1_PRINTABLESTRING * printablestring;
268 ASN1_T61STRING * t61string;
269 ASN1_IA5STRING * ia5string;
270 ASN1_GENERALSTRING * generalstring;
271 ASN1_BMPSTRING * bmpstring;
272 ASN1_UNIVERSALSTRING * universalstring;
273 ASN1_UTCTIME * utctime;
274 ASN1_GENERALIZEDTIME * generalizedtime;
275 /* set and sequence are left complete and still
276 * contain the set or sequence bytes */
277 ASN1_STRING * set;
278 ASN1_STRING * sequence;
279 } value;
280 } ASN1_TYPE;
281
282typedef struct asn1_method_st
283 {
284 int (*i2d)();
285 char *(*d2i)();
286 char *(*create)();
287 void (*destroy)();
288 } ASN1_METHOD;
289
290/* This is used when parsing some Netscape objects */
291typedef struct asn1_header_st
292 {
293 ASN1_OCTET_STRING *header;
294 char *data;
295 ASN1_METHOD *meth;
296 } ASN1_HEADER;
297
298#define ASN1_STRING_length(x) ((x)->length)
299#define ASN1_STRING_type(x) ((x)->type)
300#define ASN1_STRING_data(x) ((x)->data)
301
302/* Macros for string operations */
303#define ASN1_BIT_STRING_new() (ASN1_BIT_STRING *)\
304 ASN1_STRING_type_new(V_ASN1_BIT_STRING)
305#define ASN1_BIT_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
306#define ASN1_BIT_STRING_dup(a) (ASN1_BIT_STRING *)\
307 ASN1_STRING_dup((ASN1_STRING *)a)
308#define ASN1_BIT_STRING_cmp(a,b) ASN1_STRING_cmp(\
309 (ASN1_STRING *)a,(ASN1_STRING *)b)
310#define ASN1_BIT_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
311/* i2d_ASN1_BIT_STRING() is a function */
312/* d2i_ASN1_BIT_STRING() is a function */
313
314#define ASN1_INTEGER_new() (ASN1_INTEGER *)\
315 ASN1_STRING_type_new(V_ASN1_INTEGER)
316#define ASN1_INTEGER_free(a) ASN1_STRING_free((ASN1_STRING *)a)
317#define ASN1_INTEGER_dup(a) (ASN1_INTEGER *)ASN1_STRING_dup((ASN1_STRING *)a)
318#define ASN1_INTEGER_cmp(a,b) ASN1_STRING_cmp(\
319 (ASN1_STRING *)a,(ASN1_STRING *)b)
320/* ASN1_INTEGER_set() is a function, also see BN_to_ASN1_INTEGER() */
321/* ASN1_INTEGER_get() is a function, also see ASN1_INTEGER_to_BN() */
322/* i2d_ASN1_INTEGER() is a function */
323/* d2i_ASN1_INTEGER() is a function */
324
325#define ASN1_OCTET_STRING_new() (ASN1_OCTET_STRING *)\
326 ASN1_STRING_type_new(V_ASN1_OCTET_STRING)
327#define ASN1_OCTET_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
328#define ASN1_OCTET_STRING_dup(a) (ASN1_OCTET_STRING *)\
329 ASN1_STRING_dup((ASN1_STRING *)a)
330#define ASN1_OCTET_STRING_cmp(a,b) ASN1_STRING_cmp(\
331 (ASN1_STRING *)a,(ASN1_STRING *)b)
332#define ASN1_OCTET_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
333#define ASN1_OCTET_STRING_print(a,b) ASN1_STRING_print(a,(ASN1_STRING *)b)
334#define M_i2d_ASN1_OCTET_STRING(a,pp) \
335 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_OCTET_STRING,\
336 V_ASN1_OCTET_STRING)
337/* d2i_ASN1_OCTET_STRING() is a function */
338
339#define ASN1_PRINTABLE_new() ASN1_STRING_type_new(V_ASN1_T61STRING)
340#define ASN1_PRINTABLE_free(a) ASN1_STRING_free((ASN1_STRING *)a)
341#define M_i2d_ASN1_PRINTABLE(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\
342 pp,a->type,V_ASN1_UNIVERSAL)
343#define M_d2i_ASN1_PRINTABLE(a,pp,l) \
344 d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \
345 B_ASN1_PRINTABLESTRING| \
346 B_ASN1_T61STRING| \
347 B_ASN1_IA5STRING| \
348 B_ASN1_BIT_STRING| \
349 B_ASN1_UNIVERSALSTRING|\
350 B_ASN1_BMPSTRING|\
351 B_ASN1_UNKNOWN)
352
353#define ASN1_PRINTABLESTRING_new() (ASN1_PRINTABLESTRING_STRING *)\
354 ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING)
355#define ASN1_PRINTABLESTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
356#define M_i2d_ASN1_PRINTABLESTRING(a,pp) \
357 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_PRINTABLESTRING,\
358 V_ASN1_UNIVERSAL)
359#define M_d2i_ASN1_PRINTABLESTRING(a,pp,l) \
360 (ASN1_PRINTABLESTRING *)d2i_ASN1_type_bytes\
361 ((ASN1_STRING **)a,pp,l,B_ASN1_PRINTABLESTRING)
362
363#define ASN1_T61STRING_new() (ASN1_T61STRING_STRING *)\
364 ASN1_STRING_type_new(V_ASN1_T61STRING)
365#define ASN1_T61STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
366#define M_i2d_ASN1_T61STRING(a,pp) \
367 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_T61STRING,\
368 V_ASN1_UNIVERSAL)
369#define M_d2i_ASN1_T61STRING(a,pp,l) \
370 (ASN1_T61STRING *)d2i_ASN1_type_bytes\
371 ((ASN1_STRING **)a,pp,l,B_ASN1_T61STRING)
372
373#define ASN1_IA5STRING_new() (ASN1_IA5STRING *)\
374 ASN1_STRING_type_new(V_ASN1_IA5STRING)
375#define ASN1_IA5STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
376#define M_i2d_ASN1_IA5STRING(a,pp) \
377 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_IA5STRING,\
378 V_ASN1_UNIVERSAL)
379#define M_d2i_ASN1_IA5STRING(a,pp,l) \
380 (ASN1_IA5STRING *)d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l,\
381 B_ASN1_IA5STRING)
382
383#define ASN1_UTCTIME_new() (ASN1_UTCTIME *)\
384 ASN1_STRING_type_new(V_ASN1_UTCTIME)
385#define ASN1_UTCTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
386#define ASN1_UTCTIME_dup(a) (ASN1_UTCTIME *)ASN1_STRING_dup((ASN1_STRING *)a)
387/* i2d_ASN1_UTCTIME() is a function */
388/* d2i_ASN1_UTCTIME() is a function */
389/* ASN1_UTCTIME_set() is a function */
390/* ASN1_UTCTIME_check() is a function */
391
392#define ASN1_GENERALIZEDTIME_new() (ASN1_GENERALIZEDTIME *)\
393 ASN1_STRING_type_new(V_ASN1_GENERALIZEDTIME)
394#define ASN1_GENERALIZEDTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
395#define ASN1_GENERALIZEDTIME_dup(a) (ASN1_UTCTIME *)ASN1_STRING_dup(\
396 (ASN1_STRING *)a)
397/* DOES NOT EXIST YET i2d_ASN1_GENERALIZEDTIME() is a function */
398/* DOES NOT EXIST YET d2i_ASN1_GENERALIZEDTIME() is a function */
399/* DOES NOT EXIST YET ASN1_GENERALIZEDTIME_set() is a function */
400/* DOES NOT EXIST YET ASN1_GENERALIZEDTIME_check() is a function */
401
402#define ASN1_GENERALSTRING_new() (ASN1_GENERALSTRING *)\
403 ASN1_STRING_type_new(V_ASN1_GENERALSTRING)
404#define ASN1_GENERALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
405#define M_i2d_ASN1_GENERALSTRING(a,pp) \
406 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_GENERALSTRING,\
407 V_ASN1_UNIVERSAL)
408#define M_d2i_ASN1_GENERALSTRING(a,pp,l) \
409 (ASN1_GENERALSTRING *)d2i_ASN1_type_bytes\
410 ((ASN1_STRING **)a,pp,l,B_ASN1_GENERALSTRING)
411
412#define ASN1_UNIVERSALSTRING_new() (ASN1_UNIVERSALSTRING *)\
413 ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING)
414#define ASN1_UNIVERSALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
415#define M_i2d_ASN1_UNIVERSALSTRING(a,pp) \
416 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_UNIVERSALSTRING,\
417 V_ASN1_UNIVERSAL)
418#define M_d2i_ASN1_UNIVERSALSTRING(a,pp,l) \
419 (ASN1_UNIVERSALSTRING *)d2i_ASN1_type_bytes\
420 ((ASN1_STRING **)a,pp,l,B_ASN1_UNIVERSALSTRING)
421
422#define ASN1_BMPSTRING_new() (ASN1_BMPSTRING *)\
423 ASN1_STRING_type_new(V_ASN1_BMPSTRING)
424#define ASN1_BMPSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
425#define M_i2d_ASN1_BMPSTRING(a,pp) \
426 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_BMPSTRING,\
427 V_ASN1_UNIVERSAL)
428#define M_d2i_ASN1_BMPSTRING(a,pp,l) \
429 (ASN1_BMPSTRING *)d2i_ASN1_type_bytes\
430 ((ASN1_STRING **)a,pp,l,B_ASN1_BMPSTRING)
431
432#ifndef NOPROTO
433ASN1_TYPE * ASN1_TYPE_new(void );
434void ASN1_TYPE_free(ASN1_TYPE *a);
435int i2d_ASN1_TYPE(ASN1_TYPE *a,unsigned char **pp);
436ASN1_TYPE * d2i_ASN1_TYPE(ASN1_TYPE **a,unsigned char **pp,long length);
437int ASN1_TYPE_get(ASN1_TYPE *a);
438void ASN1_TYPE_set(ASN1_TYPE *a, int type, char *value);
439
440ASN1_OBJECT * ASN1_OBJECT_new(void );
441void ASN1_OBJECT_free(ASN1_OBJECT *a);
442int i2d_ASN1_OBJECT(ASN1_OBJECT *a,unsigned char **pp);
443ASN1_OBJECT * d2i_ASN1_OBJECT(ASN1_OBJECT **a,unsigned char **pp,
444 long length);
445
446ASN1_STRING * ASN1_STRING_new(void );
447void ASN1_STRING_free(ASN1_STRING *a);
448ASN1_STRING * ASN1_STRING_dup(ASN1_STRING *a);
449ASN1_STRING * ASN1_STRING_type_new(int type );
450int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b);
451int ASN1_STRING_set(ASN1_STRING *str,unsigned char *data, int len);
452
453int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a,unsigned char **pp);
454ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,unsigned char **pp,
455 long length);
456int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value);
457int ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n);
458
459
460int i2d_ASN1_BOOLEAN(int a,unsigned char **pp);
461int d2i_ASN1_BOOLEAN(int *a,unsigned char **pp,long length);
462
463int i2d_ASN1_INTEGER(ASN1_INTEGER *a,unsigned char **pp);
464ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a,unsigned char **pp,
465 long length);
466
467int ASN1_UTCTIME_check(ASN1_UTCTIME *a);
468ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s,time_t t);
469int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, char *str);
470
471int i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a,unsigned char **pp);
472ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **a,
473 unsigned char **pp,long length);
474
475int i2d_ASN1_PRINTABLE(ASN1_STRING *a,unsigned char **pp);
476ASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **a,
477 unsigned char **pp, long l);
478ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(ASN1_PRINTABLESTRING **a,
479 unsigned char **pp, long l);
480
481ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **a,
482 unsigned char **pp, long l);
483int i2d_ASN1_IA5STRING(ASN1_IA5STRING *a,unsigned char **pp);
484ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **a,
485 unsigned char **pp, long l);
486
487int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a,unsigned char **pp);
488ASN1_UTCTIME * d2i_ASN1_UTCTIME(ASN1_UTCTIME **a,unsigned char **pp,
489 long length);
490
491int i2d_ASN1_SET(STACK *a, unsigned char **pp,
492 int (*func)(), int ex_tag, int ex_class);
493STACK * d2i_ASN1_SET(STACK **a, unsigned char **pp, long length,
494 char *(*func)(), int ex_tag, int ex_class);
495
496#ifdef HEADER_BIO_H
497int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a);
498int a2i_ASN1_INTEGER(BIO *bp,ASN1_INTEGER *bs,char *buf,int size);
499int i2a_ASN1_OBJECT(BIO *bp,ASN1_OBJECT *a);
500int a2i_ASN1_STRING(BIO *bp,ASN1_STRING *bs,char *buf,int size);
501int i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type);
502#endif
503int i2t_ASN1_OBJECT(char *buf,int buf_len,ASN1_OBJECT *a);
504
505int a2d_ASN1_OBJECT(unsigned char *out,int olen, char *buf, int num);
506ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data,int len,
507 char *sn, char *ln);
508
509int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
510long ASN1_INTEGER_get(ASN1_INTEGER *a);
511ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai);
512BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai,BIGNUM *bn);
513
514/* General */
515/* given a string, return the correct type, max is the maximum length */
516int ASN1_PRINTABLE_type(unsigned char *s, int max);
517
518int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass);
519ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp,
520 long length, int Ptag, int Pclass);
521/* type is one or more of the B_ASN1_ values. */
522ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a,unsigned char **pp,
523 long length,int type);
524
525/* PARSING */
526int asn1_Finish(ASN1_CTX *c);
527
528/* SPECIALS */
529int ASN1_get_object(unsigned char **pp, long *plength, int *ptag,
530 int *pclass, long omax);
531int ASN1_check_infinite_end(unsigned char **p,long len);
532void ASN1_put_object(unsigned char **pp, int constructed, int length,
533 int tag, int xclass);
534int ASN1_object_size(int constructed, int length, int tag);
535
536/* Used to implement other functions */
537char *ASN1_dup(int (*i2d)(),char *(*d2i)(),char *x);
538
539#ifndef NO_FP_API
540char *ASN1_d2i_fp(char *(*xnew)(),char *(*d2i)(),FILE *fp,unsigned char **x);
541int ASN1_i2d_fp(int (*i2d)(),FILE *out,unsigned char *x);
542#endif
543
544#ifdef HEADER_BIO_H
545char *ASN1_d2i_bio(char *(*xnew)(),char *(*d2i)(),BIO *bp,unsigned char **x);
546int ASN1_i2d_bio(int (*i2d)(),BIO *out,unsigned char *x);
547int ASN1_UTCTIME_print(BIO *fp,ASN1_UTCTIME *a);
548int ASN1_STRING_print(BIO *bp,ASN1_STRING *v);
549int ASN1_parse(BIO *bp,unsigned char *pp,long len,int indent);
550#endif
551
552/* Used to load and write netscape format cert/key */
553int i2d_ASN1_HEADER(ASN1_HEADER *a,unsigned char **pp);
554ASN1_HEADER *d2i_ASN1_HEADER(ASN1_HEADER **a,unsigned char **pp, long length);
555ASN1_HEADER *ASN1_HEADER_new(void );
556void ASN1_HEADER_free(ASN1_HEADER *a);
557
558int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s);
559
560void ERR_load_ASN1_strings(void);
561
562/* Not used that much at this point, except for the first two */
563ASN1_METHOD *X509_asn1_meth(void);
564ASN1_METHOD *RSAPrivateKey_asn1_meth(void);
565ASN1_METHOD *ASN1_IA5STRING_asn1_meth(void);
566ASN1_METHOD *ASN1_BIT_STRING_asn1_meth(void);
567
568int ASN1_TYPE_set_octetstring(ASN1_TYPE *a,
569 unsigned char *data, int len);
570int ASN1_TYPE_get_octetstring(ASN1_TYPE *a,
571 unsigned char *data, int max_len);
572int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num,
573 unsigned char *data, int len);
574int ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a,long *num,
575 unsigned char *data, int max_len);
576
577#else
578
579ASN1_TYPE * ASN1_TYPE_new();
580void ASN1_TYPE_free();
581int i2d_ASN1_TYPE();
582ASN1_TYPE * d2i_ASN1_TYPE();
583int ASN1_TYPE_get();
584void ASN1_TYPE_set();
585
586ASN1_OBJECT * ASN1_OBJECT_new();
587void ASN1_OBJECT_free();
588int i2d_ASN1_OBJECT();
589ASN1_OBJECT * d2i_ASN1_OBJECT();
590ASN1_STRING * ASN1_STRING_new();
591void ASN1_STRING_free();
592ASN1_STRING * ASN1_STRING_dup();
593ASN1_STRING * ASN1_STRING_type_new();
594int ASN1_STRING_cmp();
595int ASN1_STRING_set();
596int i2d_ASN1_BIT_STRING();
597ASN1_BIT_STRING *d2i_ASN1_BIT_STRING();
598int ASN1_BIT_STRING_set_bit();
599int ASN1_BIT_STRING_get_bit();
600int i2d_ASN1_BOOLEAN();
601int d2i_ASN1_BOOLEAN();
602int i2d_ASN1_INTEGER();
603ASN1_INTEGER *d2i_ASN1_INTEGER();
604int ASN1_UTCTIME_check();
605ASN1_UTCTIME *ASN1_UTCTIME_set();
606int ASN1_UTCTIME_set_string();
607int i2d_ASN1_OCTET_STRING();
608ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING();
609int i2d_ASN1_PRINTABLE();
610ASN1_STRING *d2i_ASN1_PRINTABLE();
611ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING();
612ASN1_T61STRING *d2i_ASN1_T61STRING();
613int i2d_ASN1_IA5STRING();
614ASN1_IA5STRING *d2i_ASN1_IA5STRING();
615int i2d_ASN1_UTCTIME();
616ASN1_UTCTIME * d2i_ASN1_UTCTIME();
617int i2d_ASN1_SET();
618STACK * d2i_ASN1_SET();
619int a2d_ASN1_OBJECT();
620ASN1_OBJECT *ASN1_OBJECT_create();
621int ASN1_INTEGER_set();
622long ASN1_INTEGER_get();
623ASN1_INTEGER *BN_to_ASN1_INTEGER();
624BIGNUM *ASN1_INTEGER_to_BN();
625int ASN1_PRINTABLE_type();
626int i2d_ASN1_bytes();
627ASN1_STRING *d2i_ASN1_bytes();
628ASN1_STRING *d2i_ASN1_type_bytes();
629int asn1_Finish();
630int ASN1_get_object();
631int ASN1_check_infinite_end();
632void ASN1_put_object();
633int ASN1_object_size();
634char *ASN1_dup();
635#ifndef NO_FP_API
636char *ASN1_d2i_fp();
637int ASN1_i2d_fp();
638#endif
639
640char *ASN1_d2i_bio();
641int ASN1_i2d_bio();
642int ASN1_UTCTIME_print();
643int ASN1_STRING_print();
644int ASN1_parse();
645int i2a_ASN1_INTEGER();
646int a2i_ASN1_INTEGER();
647int i2a_ASN1_OBJECT();
648int i2t_ASN1_OBJECT();
649int a2i_ASN1_STRING();
650int i2a_ASN1_STRING();
651
652int i2d_ASN1_HEADER();
653ASN1_HEADER *d2i_ASN1_HEADER();
654ASN1_HEADER *ASN1_HEADER_new();
655void ASN1_HEADER_free();
656void ERR_load_ASN1_strings();
657ASN1_METHOD *X509_asn1_meth();
658ASN1_METHOD *RSAPrivateKey_asn1_meth();
659ASN1_METHOD *ASN1_IA5STRING_asn1_meth();
660ASN1_METHOD *ASN1_BIT_STRING_asn1_meth();
661
662int ASN1_UNIVERSALSTRING_to_string();
663
664int ASN1_TYPE_set_octetstring();
665int ASN1_TYPE_get_octetstring();
666int ASN1_TYPE_set_int_octetstring();
667int ASN1_TYPE_get_int_octetstring();
668
669#endif
670
671/* BEGIN ERROR CODES */
672/* Error codes for the ASN1 functions. */
673
674/* Function codes. */
675#define ASN1_F_A2D_ASN1_OBJECT 100
676#define ASN1_F_A2I_ASN1_INTEGER 101
677#define ASN1_F_A2I_ASN1_STRING 102
678#define ASN1_F_ASN1_COLLATE_PRIMATIVE 103
679#define ASN1_F_ASN1_D2I_BIO 104
680#define ASN1_F_ASN1_D2I_FP 105
681#define ASN1_F_ASN1_DUP 106
682#define ASN1_F_ASN1_GET_OBJECT 107
683#define ASN1_F_ASN1_HEADER_NEW 108
684#define ASN1_F_ASN1_I2D_BIO 109
685#define ASN1_F_ASN1_I2D_FP 110
686#define ASN1_F_ASN1_INTEGER_SET 111
687#define ASN1_F_ASN1_INTEGER_TO_BN 112
688#define ASN1_F_ASN1_OBJECT_NEW 113
689#define ASN1_F_ASN1_SIGN 114
690#define ASN1_F_ASN1_STRING_NEW 115
691#define ASN1_F_ASN1_STRING_TYPE_NEW 116
692#define ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING 117
693#define ASN1_F_ASN1_TYPE_GET_OCTETSTRING 118
694#define ASN1_F_ASN1_TYPE_NEW 119
695#define ASN1_F_ASN1_UTCTIME_NEW 120
696#define ASN1_F_ASN1_VERIFY 121
697#define ASN1_F_BN_TO_ASN1_INTEGER 122
698#define ASN1_F_D2I_ASN1_BIT_STRING 123
699#define ASN1_F_D2I_ASN1_BMPSTRING 124
700#define ASN1_F_D2I_ASN1_BOOLEAN 125
701#define ASN1_F_D2I_ASN1_BYTES 126
702#define ASN1_F_D2I_ASN1_HEADER 127
703#define ASN1_F_D2I_ASN1_INTEGER 128
704#define ASN1_F_D2I_ASN1_OBJECT 129
705#define ASN1_F_D2I_ASN1_OCTET_STRING 130
706#define ASN1_F_D2I_ASN1_PRINT_TYPE 131
707#define ASN1_F_D2I_ASN1_SET 132
708#define ASN1_F_D2I_ASN1_TYPE 133
709#define ASN1_F_D2I_ASN1_TYPE_BYTES 134
710#define ASN1_F_D2I_ASN1_UTCTIME 135
711#define ASN1_F_D2I_DHPARAMS 136
712#define ASN1_F_D2I_DSAPARAMS 137
713#define ASN1_F_D2I_DSAPRIVATEKEY 138
714#define ASN1_F_D2I_DSAPUBLICKEY 139
715#define ASN1_F_D2I_NETSCAPE_PKEY 140
716#define ASN1_F_D2I_NETSCAPE_RSA 141
717#define ASN1_F_D2I_NETSCAPE_RSA_2 142
718#define ASN1_F_D2I_NETSCAPE_SPKAC 143
719#define ASN1_F_D2I_NETSCAPE_SPKI 144
720#define ASN1_F_D2I_PKCS7 145
721#define ASN1_F_D2I_PKCS7_DIGEST 146
722#define ASN1_F_D2I_PKCS7_ENCRYPT 147
723#define ASN1_F_D2I_PKCS7_ENC_CONTENT 148
724#define ASN1_F_D2I_PKCS7_ENVELOPE 149
725#define ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL 150
726#define ASN1_F_D2I_PKCS7_RECIP_INFO 151
727#define ASN1_F_D2I_PKCS7_SIGNED 152
728#define ASN1_F_D2I_PKCS7_SIGNER_INFO 153
729#define ASN1_F_D2I_PKCS7_SIGN_ENVELOPE 154
730#define ASN1_F_D2I_PRIVATEKEY 155
731#define ASN1_F_D2I_PUBLICKEY 156
732#define ASN1_F_D2I_RSAPRIVATEKEY 157
733#define ASN1_F_D2I_RSAPUBLICKEY 158
734#define ASN1_F_D2I_X509 159
735#define ASN1_F_D2I_X509_ALGOR 160
736#define ASN1_F_D2I_X509_ATTRIBUTE 161
737#define ASN1_F_D2I_X509_CINF 162
738#define ASN1_F_D2I_X509_CRL 163
739#define ASN1_F_D2I_X509_CRL_INFO 164
740#define ASN1_F_D2I_X509_EXTENSION 165
741#define ASN1_F_D2I_X509_KEY 166
742#define ASN1_F_D2I_X509_NAME 167
743#define ASN1_F_D2I_X509_NAME_ENTRY 168
744#define ASN1_F_D2I_X509_PKEY 169
745#define ASN1_F_D2I_X509_PUBKEY 170
746#define ASN1_F_D2I_X509_REQ 171
747#define ASN1_F_D2I_X509_REQ_INFO 172
748#define ASN1_F_D2I_X509_REVOKED 173
749#define ASN1_F_D2I_X509_SIG 174
750#define ASN1_F_D2I_X509_VAL 175
751#define ASN1_F_I2D_ASN1_HEADER 176
752#define ASN1_F_I2D_DHPARAMS 177
753#define ASN1_F_I2D_DSAPARAMS 178
754#define ASN1_F_I2D_DSAPRIVATEKEY 179
755#define ASN1_F_I2D_DSAPUBLICKEY 180
756#define ASN1_F_I2D_NETSCAPE_RSA 181
757#define ASN1_F_I2D_PKCS7 182
758#define ASN1_F_I2D_PRIVATEKEY 183
759#define ASN1_F_I2D_PUBLICKEY 184
760#define ASN1_F_I2D_RSAPRIVATEKEY 185
761#define ASN1_F_I2D_RSAPUBLICKEY 186
762#define ASN1_F_I2D_X509_ATTRIBUTE 187
763#define ASN1_F_I2T_ASN1_OBJECT 188
764#define ASN1_F_NETSCAPE_PKEY_NEW 189
765#define ASN1_F_NETSCAPE_SPKAC_NEW 190
766#define ASN1_F_NETSCAPE_SPKI_NEW 191
767#define ASN1_F_PKCS7_DIGEST_NEW 192
768#define ASN1_F_PKCS7_ENCRYPT_NEW 193
769#define ASN1_F_PKCS7_ENC_CONTENT_NEW 194
770#define ASN1_F_PKCS7_ENVELOPE_NEW 195
771#define ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW 196
772#define ASN1_F_PKCS7_NEW 197
773#define ASN1_F_PKCS7_RECIP_INFO_NEW 198
774#define ASN1_F_PKCS7_SIGNED_NEW 199
775#define ASN1_F_PKCS7_SIGNER_INFO_NEW 200
776#define ASN1_F_PKCS7_SIGN_ENVELOPE_NEW 201
777#define ASN1_F_X509_ALGOR_NEW 202
778#define ASN1_F_X509_ATTRIBUTE_NEW 203
779#define ASN1_F_X509_CINF_NEW 204
780#define ASN1_F_X509_CRL_INFO_NEW 205
781#define ASN1_F_X509_CRL_NEW 206
782#define ASN1_F_X509_DHPARAMS_NEW 207
783#define ASN1_F_X509_EXTENSION_NEW 208
784#define ASN1_F_X509_INFO_NEW 209
785#define ASN1_F_X509_KEY_NEW 210
786#define ASN1_F_X509_NAME_ENTRY_NEW 211
787#define ASN1_F_X509_NAME_NEW 212
788#define ASN1_F_X509_NEW 213
789#define ASN1_F_X509_PKEY_NEW 214
790#define ASN1_F_X509_PUBKEY_NEW 215
791#define ASN1_F_X509_REQ_INFO_NEW 216
792#define ASN1_F_X509_REQ_NEW 217
793#define ASN1_F_X509_REVOKED_NEW 218
794#define ASN1_F_X509_SIG_NEW 219
795#define ASN1_F_X509_VAL_FREE 220
796#define ASN1_F_X509_VAL_NEW 221
797
798/* Reason codes. */
799#define ASN1_R_BAD_CLASS 100
800#define ASN1_R_BAD_GET_OBJECT 101
801#define ASN1_R_BAD_OBJECT_HEADER 102
802#define ASN1_R_BAD_PASSWORD_READ 103
803#define ASN1_R_BAD_PKCS7_CONTENT 104
804#define ASN1_R_BAD_PKCS7_TYPE 105
805#define ASN1_R_BAD_TAG 106
806#define ASN1_R_BAD_TYPE 107
807#define ASN1_R_BN_LIB 108
808#define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 109
809#define ASN1_R_BUFFER_TOO_SMALL 110
810#define ASN1_R_DATA_IS_WRONG 111
811#define ASN1_R_DECODING_ERROR 112
812#define ASN1_R_ERROR_STACK 113
813#define ASN1_R_EXPECTING_AN_INTEGER 114
814#define ASN1_R_EXPECTING_AN_OBJECT 115
815#define ASN1_R_EXPECTING_AN_OCTET_STRING 116
816#define ASN1_R_EXPECTING_A_BIT_STRING 117
817#define ASN1_R_EXPECTING_A_BOOLEAN 118
818#define ASN1_R_EXPECTING_A_SEQUENCE 119
819#define ASN1_R_EXPECTING_A_UTCTIME 120
820#define ASN1_R_FIRST_NUM_TOO_LARGE 121
821#define ASN1_R_HEADER_TOO_LONG 122
822#define ASN1_R_INVALID_DIGIT 123
823#define ASN1_R_INVALID_SEPARATOR 124
824#define ASN1_R_INVALID_TIME_FORMAT 125
825#define ASN1_R_IV_TOO_LARGE 126
826#define ASN1_R_LENGTH_ERROR 127
827#define ASN1_R_LENGTH_MISMATCH 128
828#define ASN1_R_MISSING_EOS 129
829#define ASN1_R_MISSING_SECOND_NUMBER 130
830#define ASN1_R_NON_HEX_CHARACTERS 131
831#define ASN1_R_NOT_ENOUGH_DATA 132
832#define ASN1_R_ODD_NUMBER_OF_CHARS 133
833#define ASN1_R_PARSING 134
834#define ASN1_R_PRIVATE_KEY_HEADER_MISSING 135
835#define ASN1_R_SECOND_NUMBER_TOO_LARGE 136
836#define ASN1_R_SHORT_LINE 137
837#define ASN1_R_STRING_TOO_SHORT 138
838#define ASN1_R_TAG_VALUE_TOO_HIGH 139
839#define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 140
840#define ASN1_R_TOO_LONG 141
841#define ASN1_R_UNABLE_TO_DECODE_RSA_KEY 142
842#define ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY 143
843#define ASN1_R_UNKNOWN_ATTRIBUTE_TYPE 144
844#define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 145
845#define ASN1_R_UNKNOWN_OBJECT_TYPE 146
846#define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 147
847#define ASN1_R_UNSUPPORTED_CIPHER 148
848#define ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM 149
849#define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 150
850#define ASN1_R_UTCTIME_TOO_LONG 151
851#define ASN1_R_WRONG_PRINTABLE_TYPE 152
852#define ASN1_R_WRONG_TAG 153
853#define ASN1_R_WRONG_TYPE 154
854
855#ifdef __cplusplus
856}
857#endif
858#endif
859
diff --git a/src/lib/libcrypto/asn1/asn1_err.c b/src/lib/libcrypto/asn1/asn1_err.c
new file mode 100644
index 0000000000..03c2858e7d
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_err.c
@@ -0,0 +1,266 @@
1/* lib/asn1/asn1_err.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#include <stdio.h>
59#include "err.h"
60#include "asn1.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA ASN1_str_functs[]=
65 {
66{ERR_PACK(0,ASN1_F_A2D_ASN1_OBJECT,0), "a2d_ASN1_OBJECT"},
67{ERR_PACK(0,ASN1_F_A2I_ASN1_INTEGER,0), "a2i_ASN1_INTEGER"},
68{ERR_PACK(0,ASN1_F_A2I_ASN1_STRING,0), "a2i_ASN1_STRING"},
69{ERR_PACK(0,ASN1_F_ASN1_COLLATE_PRIMATIVE,0), "ASN1_COLLATE_PRIMATIVE"},
70{ERR_PACK(0,ASN1_F_ASN1_D2I_BIO,0), "ASN1_d2i_bio"},
71{ERR_PACK(0,ASN1_F_ASN1_D2I_FP,0), "ASN1_d2i_fp"},
72{ERR_PACK(0,ASN1_F_ASN1_DUP,0), "ASN1_dup"},
73{ERR_PACK(0,ASN1_F_ASN1_GET_OBJECT,0), "ASN1_get_object"},
74{ERR_PACK(0,ASN1_F_ASN1_HEADER_NEW,0), "ASN1_HEADER_new"},
75{ERR_PACK(0,ASN1_F_ASN1_I2D_BIO,0), "ASN1_i2d_bio"},
76{ERR_PACK(0,ASN1_F_ASN1_I2D_FP,0), "ASN1_i2d_fp"},
77{ERR_PACK(0,ASN1_F_ASN1_INTEGER_SET,0), "ASN1_INTEGER_set"},
78{ERR_PACK(0,ASN1_F_ASN1_INTEGER_TO_BN,0), "ASN1_INTEGER_to_BN"},
79{ERR_PACK(0,ASN1_F_ASN1_OBJECT_NEW,0), "ASN1_OBJECT_new"},
80{ERR_PACK(0,ASN1_F_ASN1_SIGN,0), "ASN1_SIGN"},
81{ERR_PACK(0,ASN1_F_ASN1_STRING_NEW,0), "ASN1_STRING_new"},
82{ERR_PACK(0,ASN1_F_ASN1_STRING_TYPE_NEW,0), "ASN1_STRING_type_new"},
83{ERR_PACK(0,ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,0), "ASN1_TYPE_get_int_octetstring"},
84{ERR_PACK(0,ASN1_F_ASN1_TYPE_GET_OCTETSTRING,0), "ASN1_TYPE_get_octetstring"},
85{ERR_PACK(0,ASN1_F_ASN1_TYPE_NEW,0), "ASN1_TYPE_new"},
86{ERR_PACK(0,ASN1_F_ASN1_UTCTIME_NEW,0), "ASN1_UTCTIME_NEW"},
87{ERR_PACK(0,ASN1_F_ASN1_VERIFY,0), "ASN1_VERIFY"},
88{ERR_PACK(0,ASN1_F_BN_TO_ASN1_INTEGER,0), "BN_to_ASN1_INTEGER"},
89{ERR_PACK(0,ASN1_F_D2I_ASN1_BIT_STRING,0), "d2i_ASN1_BIT_STRING"},
90{ERR_PACK(0,ASN1_F_D2I_ASN1_BMPSTRING,0), "D2I_ASN1_BMPSTRING"},
91{ERR_PACK(0,ASN1_F_D2I_ASN1_BOOLEAN,0), "d2i_ASN1_BOOLEAN"},
92{ERR_PACK(0,ASN1_F_D2I_ASN1_BYTES,0), "d2i_ASN1_bytes"},
93{ERR_PACK(0,ASN1_F_D2I_ASN1_HEADER,0), "d2i_ASN1_HEADER"},
94{ERR_PACK(0,ASN1_F_D2I_ASN1_INTEGER,0), "d2i_ASN1_INTEGER"},
95{ERR_PACK(0,ASN1_F_D2I_ASN1_OBJECT,0), "d2i_ASN1_OBJECT"},
96{ERR_PACK(0,ASN1_F_D2I_ASN1_OCTET_STRING,0), "d2i_ASN1_OCTET_STRING"},
97{ERR_PACK(0,ASN1_F_D2I_ASN1_PRINT_TYPE,0), "D2I_ASN1_PRINT_TYPE"},
98{ERR_PACK(0,ASN1_F_D2I_ASN1_SET,0), "d2i_ASN1_SET"},
99{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE,0), "d2i_ASN1_TYPE"},
100{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE_BYTES,0), "d2i_ASN1_type_bytes"},
101{ERR_PACK(0,ASN1_F_D2I_ASN1_UTCTIME,0), "d2i_ASN1_UTCTIME"},
102{ERR_PACK(0,ASN1_F_D2I_DHPARAMS,0), "D2I_DHPARAMS"},
103{ERR_PACK(0,ASN1_F_D2I_DSAPARAMS,0), "D2I_DSAPARAMS"},
104{ERR_PACK(0,ASN1_F_D2I_DSAPRIVATEKEY,0), "D2I_DSAPRIVATEKEY"},
105{ERR_PACK(0,ASN1_F_D2I_DSAPUBLICKEY,0), "D2I_DSAPUBLICKEY"},
106{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_PKEY,0), "D2I_NETSCAPE_PKEY"},
107{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA,0), "D2I_NETSCAPE_RSA"},
108{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA_2,0), "D2I_NETSCAPE_RSA_2"},
109{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKAC,0), "D2I_NETSCAPE_SPKAC"},
110{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKI,0), "D2I_NETSCAPE_SPKI"},
111{ERR_PACK(0,ASN1_F_D2I_PKCS7,0), "D2I_PKCS7"},
112{ERR_PACK(0,ASN1_F_D2I_PKCS7_DIGEST,0), "D2I_PKCS7_DIGEST"},
113{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENCRYPT,0), "D2I_PKCS7_ENCRYPT"},
114{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENC_CONTENT,0), "D2I_PKCS7_ENC_CONTENT"},
115{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENVELOPE,0), "D2I_PKCS7_ENVELOPE"},
116{ERR_PACK(0,ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL,0), "D2I_PKCS7_ISSUER_AND_SERIAL"},
117{ERR_PACK(0,ASN1_F_D2I_PKCS7_RECIP_INFO,0), "D2I_PKCS7_RECIP_INFO"},
118{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNED,0), "D2I_PKCS7_SIGNED"},
119{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNER_INFO,0), "D2I_PKCS7_SIGNER_INFO"},
120{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGN_ENVELOPE,0), "D2I_PKCS7_SIGN_ENVELOPE"},
121{ERR_PACK(0,ASN1_F_D2I_PRIVATEKEY,0), "D2I_PRIVATEKEY"},
122{ERR_PACK(0,ASN1_F_D2I_PUBLICKEY,0), "D2I_PUBLICKEY"},
123{ERR_PACK(0,ASN1_F_D2I_RSAPRIVATEKEY,0), "D2I_RSAPRIVATEKEY"},
124{ERR_PACK(0,ASN1_F_D2I_RSAPUBLICKEY,0), "D2I_RSAPUBLICKEY"},
125{ERR_PACK(0,ASN1_F_D2I_X509,0), "D2I_X509"},
126{ERR_PACK(0,ASN1_F_D2I_X509_ALGOR,0), "D2I_X509_ALGOR"},
127{ERR_PACK(0,ASN1_F_D2I_X509_ATTRIBUTE,0), "D2I_X509_ATTRIBUTE"},
128{ERR_PACK(0,ASN1_F_D2I_X509_CINF,0), "D2I_X509_CINF"},
129{ERR_PACK(0,ASN1_F_D2I_X509_CRL,0), "D2I_X509_CRL"},
130{ERR_PACK(0,ASN1_F_D2I_X509_CRL_INFO,0), "D2I_X509_CRL_INFO"},
131{ERR_PACK(0,ASN1_F_D2I_X509_EXTENSION,0), "D2I_X509_EXTENSION"},
132{ERR_PACK(0,ASN1_F_D2I_X509_KEY,0), "D2I_X509_KEY"},
133{ERR_PACK(0,ASN1_F_D2I_X509_NAME,0), "D2I_X509_NAME"},
134{ERR_PACK(0,ASN1_F_D2I_X509_NAME_ENTRY,0), "D2I_X509_NAME_ENTRY"},
135{ERR_PACK(0,ASN1_F_D2I_X509_PKEY,0), "D2I_X509_PKEY"},
136{ERR_PACK(0,ASN1_F_D2I_X509_PUBKEY,0), "D2I_X509_PUBKEY"},
137{ERR_PACK(0,ASN1_F_D2I_X509_REQ,0), "D2I_X509_REQ"},
138{ERR_PACK(0,ASN1_F_D2I_X509_REQ_INFO,0), "D2I_X509_REQ_INFO"},
139{ERR_PACK(0,ASN1_F_D2I_X509_REVOKED,0), "D2I_X509_REVOKED"},
140{ERR_PACK(0,ASN1_F_D2I_X509_SIG,0), "D2I_X509_SIG"},
141{ERR_PACK(0,ASN1_F_D2I_X509_VAL,0), "D2I_X509_VAL"},
142{ERR_PACK(0,ASN1_F_I2D_ASN1_HEADER,0), "i2d_ASN1_HEADER"},
143{ERR_PACK(0,ASN1_F_I2D_DHPARAMS,0), "I2D_DHPARAMS"},
144{ERR_PACK(0,ASN1_F_I2D_DSAPARAMS,0), "I2D_DSAPARAMS"},
145{ERR_PACK(0,ASN1_F_I2D_DSAPRIVATEKEY,0), "I2D_DSAPRIVATEKEY"},
146{ERR_PACK(0,ASN1_F_I2D_DSAPUBLICKEY,0), "I2D_DSAPUBLICKEY"},
147{ERR_PACK(0,ASN1_F_I2D_NETSCAPE_RSA,0), "I2D_NETSCAPE_RSA"},
148{ERR_PACK(0,ASN1_F_I2D_PKCS7,0), "I2D_PKCS7"},
149{ERR_PACK(0,ASN1_F_I2D_PRIVATEKEY,0), "I2D_PRIVATEKEY"},
150{ERR_PACK(0,ASN1_F_I2D_PUBLICKEY,0), "I2D_PUBLICKEY"},
151{ERR_PACK(0,ASN1_F_I2D_RSAPRIVATEKEY,0), "I2D_RSAPRIVATEKEY"},
152{ERR_PACK(0,ASN1_F_I2D_RSAPUBLICKEY,0), "I2D_RSAPUBLICKEY"},
153{ERR_PACK(0,ASN1_F_I2D_X509_ATTRIBUTE,0), "I2D_X509_ATTRIBUTE"},
154{ERR_PACK(0,ASN1_F_I2T_ASN1_OBJECT,0), "i2t_ASN1_OBJECT"},
155{ERR_PACK(0,ASN1_F_NETSCAPE_PKEY_NEW,0), "NETSCAPE_PKEY_NEW"},
156{ERR_PACK(0,ASN1_F_NETSCAPE_SPKAC_NEW,0), "NETSCAPE_SPKAC_NEW"},
157{ERR_PACK(0,ASN1_F_NETSCAPE_SPKI_NEW,0), "NETSCAPE_SPKI_NEW"},
158{ERR_PACK(0,ASN1_F_PKCS7_DIGEST_NEW,0), "PKCS7_DIGEST_NEW"},
159{ERR_PACK(0,ASN1_F_PKCS7_ENCRYPT_NEW,0), "PKCS7_ENCRYPT_NEW"},
160{ERR_PACK(0,ASN1_F_PKCS7_ENC_CONTENT_NEW,0), "PKCS7_ENC_CONTENT_NEW"},
161{ERR_PACK(0,ASN1_F_PKCS7_ENVELOPE_NEW,0), "PKCS7_ENVELOPE_NEW"},
162{ERR_PACK(0,ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW,0), "PKCS7_ISSUER_AND_SERIAL_NEW"},
163{ERR_PACK(0,ASN1_F_PKCS7_NEW,0), "PKCS7_NEW"},
164{ERR_PACK(0,ASN1_F_PKCS7_RECIP_INFO_NEW,0), "PKCS7_RECIP_INFO_NEW"},
165{ERR_PACK(0,ASN1_F_PKCS7_SIGNED_NEW,0), "PKCS7_SIGNED_NEW"},
166{ERR_PACK(0,ASN1_F_PKCS7_SIGNER_INFO_NEW,0), "PKCS7_SIGNER_INFO_NEW"},
167{ERR_PACK(0,ASN1_F_PKCS7_SIGN_ENVELOPE_NEW,0), "PKCS7_SIGN_ENVELOPE_NEW"},
168{ERR_PACK(0,ASN1_F_X509_ALGOR_NEW,0), "X509_ALGOR_NEW"},
169{ERR_PACK(0,ASN1_F_X509_ATTRIBUTE_NEW,0), "X509_ATTRIBUTE_NEW"},
170{ERR_PACK(0,ASN1_F_X509_CINF_NEW,0), "X509_CINF_NEW"},
171{ERR_PACK(0,ASN1_F_X509_CRL_INFO_NEW,0), "X509_CRL_INFO_NEW"},
172{ERR_PACK(0,ASN1_F_X509_CRL_NEW,0), "X509_CRL_NEW"},
173{ERR_PACK(0,ASN1_F_X509_DHPARAMS_NEW,0), "X509_DHPARAMS_NEW"},
174{ERR_PACK(0,ASN1_F_X509_EXTENSION_NEW,0), "X509_EXTENSION_NEW"},
175{ERR_PACK(0,ASN1_F_X509_INFO_NEW,0), "X509_INFO_NEW"},
176{ERR_PACK(0,ASN1_F_X509_KEY_NEW,0), "X509_KEY_NEW"},
177{ERR_PACK(0,ASN1_F_X509_NAME_ENTRY_NEW,0), "X509_NAME_ENTRY_NEW"},
178{ERR_PACK(0,ASN1_F_X509_NAME_NEW,0), "X509_NAME_NEW"},
179{ERR_PACK(0,ASN1_F_X509_NEW,0), "X509_NEW"},
180{ERR_PACK(0,ASN1_F_X509_PKEY_NEW,0), "X509_PKEY_NEW"},
181{ERR_PACK(0,ASN1_F_X509_PUBKEY_NEW,0), "X509_PUBKEY_NEW"},
182{ERR_PACK(0,ASN1_F_X509_REQ_INFO_NEW,0), "X509_REQ_INFO_NEW"},
183{ERR_PACK(0,ASN1_F_X509_REQ_NEW,0), "X509_REQ_NEW"},
184{ERR_PACK(0,ASN1_F_X509_REVOKED_NEW,0), "X509_REVOKED_NEW"},
185{ERR_PACK(0,ASN1_F_X509_SIG_NEW,0), "X509_SIG_NEW"},
186{ERR_PACK(0,ASN1_F_X509_VAL_FREE,0), "X509_VAL_FREE"},
187{ERR_PACK(0,ASN1_F_X509_VAL_NEW,0), "X509_VAL_NEW"},
188{0,NULL},
189 };
190
191static ERR_STRING_DATA ASN1_str_reasons[]=
192 {
193{ASN1_R_BAD_CLASS ,"bad class"},
194{ASN1_R_BAD_GET_OBJECT ,"bad get object"},
195{ASN1_R_BAD_OBJECT_HEADER ,"bad object header"},
196{ASN1_R_BAD_PASSWORD_READ ,"bad password read"},
197{ASN1_R_BAD_PKCS7_CONTENT ,"bad pkcs7 content"},
198{ASN1_R_BAD_PKCS7_TYPE ,"bad pkcs7 type"},
199{ASN1_R_BAD_TAG ,"bad tag"},
200{ASN1_R_BAD_TYPE ,"bad type"},
201{ASN1_R_BN_LIB ,"bn lib"},
202{ASN1_R_BOOLEAN_IS_WRONG_LENGTH ,"boolean is wrong length"},
203{ASN1_R_BUFFER_TOO_SMALL ,"buffer too small"},
204{ASN1_R_DATA_IS_WRONG ,"data is wrong"},
205{ASN1_R_DECODING_ERROR ,"decoding error"},
206{ASN1_R_ERROR_STACK ,"error stack"},
207{ASN1_R_EXPECTING_AN_INTEGER ,"expecting an integer"},
208{ASN1_R_EXPECTING_AN_OBJECT ,"expecting an object"},
209{ASN1_R_EXPECTING_AN_OCTET_STRING ,"expecting an octet string"},
210{ASN1_R_EXPECTING_A_BIT_STRING ,"expecting a bit string"},
211{ASN1_R_EXPECTING_A_BOOLEAN ,"expecting a boolean"},
212{ASN1_R_EXPECTING_A_SEQUENCE ,"expecting a sequence"},
213{ASN1_R_EXPECTING_A_UTCTIME ,"expecting a utctime"},
214{ASN1_R_FIRST_NUM_TOO_LARGE ,"first num too large"},
215{ASN1_R_HEADER_TOO_LONG ,"header too long"},
216{ASN1_R_INVALID_DIGIT ,"invalid digit"},
217{ASN1_R_INVALID_SEPARATOR ,"invalid separator"},
218{ASN1_R_INVALID_TIME_FORMAT ,"invalid time format"},
219{ASN1_R_IV_TOO_LARGE ,"iv too large"},
220{ASN1_R_LENGTH_ERROR ,"length error"},
221{ASN1_R_LENGTH_MISMATCH ,"length mismatch"},
222{ASN1_R_MISSING_EOS ,"missing eos"},
223{ASN1_R_MISSING_SECOND_NUMBER ,"missing second number"},
224{ASN1_R_NON_HEX_CHARACTERS ,"non hex characters"},
225{ASN1_R_NOT_ENOUGH_DATA ,"not enough data"},
226{ASN1_R_ODD_NUMBER_OF_CHARS ,"odd number of chars"},
227{ASN1_R_PARSING ,"parsing"},
228{ASN1_R_PRIVATE_KEY_HEADER_MISSING ,"private key header missing"},
229{ASN1_R_SECOND_NUMBER_TOO_LARGE ,"second number too large"},
230{ASN1_R_SHORT_LINE ,"short line"},
231{ASN1_R_STRING_TOO_SHORT ,"string too short"},
232{ASN1_R_TAG_VALUE_TOO_HIGH ,"tag value too high"},
233{ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"},
234{ASN1_R_TOO_LONG ,"too long"},
235{ASN1_R_UNABLE_TO_DECODE_RSA_KEY ,"unable to decode rsa key"},
236{ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY ,"unable to decode rsa private key"},
237{ASN1_R_UNKNOWN_ATTRIBUTE_TYPE ,"unknown attribute type"},
238{ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM ,"unknown message digest algorithm"},
239{ASN1_R_UNKNOWN_OBJECT_TYPE ,"unknown object type"},
240{ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE ,"unknown public key type"},
241{ASN1_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
242{ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM ,"unsupported encryption algorithm"},
243{ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE ,"unsupported public key type"},
244{ASN1_R_UTCTIME_TOO_LONG ,"utctime too long"},
245{ASN1_R_WRONG_PRINTABLE_TYPE ,"wrong printable type"},
246{ASN1_R_WRONG_TAG ,"wrong tag"},
247{ASN1_R_WRONG_TYPE ,"wrong type"},
248{0,NULL},
249 };
250
251#endif
252
253void ERR_load_ASN1_strings()
254 {
255 static int init=1;
256
257 if (init);
258 {;
259 init=0;
260#ifndef NO_ERR
261 ERR_load_strings(ERR_LIB_ASN1,ASN1_str_functs);
262 ERR_load_strings(ERR_LIB_ASN1,ASN1_str_reasons);
263#endif
264
265 }
266 }
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
new file mode 100644
index 0000000000..ff30b25836
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_lib.c
@@ -0,0 +1,444 @@
1/* crypto/asn1/asn1_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 "asn1.h"
62#include "asn1_mac.h"
63
64#ifndef NOPROTO
65static int asn1_get_length(unsigned char **pp,int *inf,long *rl,int max);
66static void asn1_put_length(unsigned char **pp, int length);
67#else
68static int asn1_get_length();
69static void asn1_put_length();
70#endif
71
72char *ASN1_version="ASN1 part of SSLeay 0.9.0b 29-Jun-1998";
73
74int ASN1_check_infinite_end(p,len)
75unsigned char **p;
76long len;
77 {
78 /* If there is 0 or 1 byte left, the length check should pick
79 * things up */
80 if (len <= 0)
81 return(1);
82 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0))
83 {
84 (*p)+=2;
85 return(1);
86 }
87 return(0);
88 }
89
90
91int ASN1_get_object(pp, plength, ptag, pclass, omax)
92unsigned char **pp;
93long *plength;
94int *ptag;
95int *pclass;
96long omax;
97 {
98 int i,ret;
99 long l;
100 unsigned char *p= *pp;
101 int tag,xclass,inf;
102 long max=omax;
103
104 if (!max) goto err;
105 ret=(*p&V_ASN1_CONSTRUCTED);
106 xclass=(*p&V_ASN1_PRIVATE);
107 i= *p&V_ASN1_PRIMATIVE_TAG;
108 if (i == V_ASN1_PRIMATIVE_TAG)
109 { /* high-tag */
110 p++;
111 if (--max == 0) goto err;
112 l=0;
113 while (*p&0x80)
114 {
115 l<<=7L;
116 l|= *(p++)&0x7f;
117 if (--max == 0) goto err;
118 }
119 l<<=7L;
120 l|= *(p++)&0x7f;
121 tag=(int)l;
122 }
123 else
124 {
125 tag=i;
126 p++;
127 if (--max == 0) goto err;
128 }
129 *ptag=tag;
130 *pclass=xclass;
131 if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
132
133#ifdef undef
134 fprintf(stderr,"p=%d + *plength=%d > omax=%d + *pp=%d (%d > %d)\n",
135 p,*plength,omax,*pp,(p+ *plength),omax+ *pp);
136
137#endif
138 if ((p+ *plength) > (omax+ *pp))
139 {
140 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);
141 /* Set this so that even if things are not long enough
142 * the values are set correctly */
143 ret|=0x80;
144 }
145 *pp=p;
146 return(ret+inf);
147err:
148 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG);
149 return(0x80);
150 }
151
152static int asn1_get_length(pp,inf,rl,max)
153unsigned char **pp;
154int *inf;
155long *rl;
156int max;
157 {
158 unsigned char *p= *pp;
159 long ret=0;
160 int i;
161
162 if (max-- < 1) return(0);
163 if (*p == 0x80)
164 {
165 *inf=1;
166 ret=0;
167 p++;
168 }
169 else
170 {
171 *inf=0;
172 i= *p&0x7f;
173 if (*(p++) & 0x80)
174 {
175 if (max-- == 0) return(0);
176 while (i-- > 0)
177 {
178 ret<<=8L;
179 ret|= *(p++);
180 if (max-- == 0) return(0);
181 }
182 }
183 else
184 ret=i;
185 }
186 *pp=p;
187 *rl=ret;
188 return(1);
189 }
190
191/* class 0 is constructed
192 * constructed == 2 for indefinitle length constructed */
193void ASN1_put_object(pp,constructed,length,tag,xclass)
194unsigned char **pp;
195int constructed;
196int length;
197int tag;
198int xclass;
199 {
200 unsigned char *p= *pp;
201 int i;
202
203 i=(constructed)?V_ASN1_CONSTRUCTED:0;
204 i|=(xclass&V_ASN1_PRIVATE);
205 if (tag < 31)
206 *(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);
207 else
208 {
209 *(p++)=i|V_ASN1_PRIMATIVE_TAG;
210 while (tag > 0x7f)
211 {
212 *(p++)=(tag&0x7f)|0x80;
213 tag>>=7;
214 }
215 *(p++)=(tag&0x7f);
216 }
217 if ((constructed == 2) && (length == 0))
218 *(p++)=0x80; /* der_put_length would output 0 instead */
219 else
220 asn1_put_length(&p,length);
221 *pp=p;
222 }
223
224static void asn1_put_length(pp, length)
225unsigned char **pp;
226int length;
227 {
228 unsigned char *p= *pp;
229 int i,l;
230 if (length <= 127)
231 *(p++)=(unsigned char)length;
232 else
233 {
234 l=length;
235 for (i=0; l > 0; i++)
236 l>>=8;
237 *(p++)=i|0x80;
238 l=i;
239 while (i-- > 0)
240 {
241 p[i]=length&0xff;
242 length>>=8;
243 }
244 p+=l;
245 }
246 *pp=p;
247 }
248
249int ASN1_object_size(constructed, length, tag)
250int constructed;
251int length;
252int tag;
253 {
254 int ret;
255
256 ret=length;
257 ret++;
258 if (tag >= 31)
259 {
260 while (tag > 0)
261 {
262 tag>>=7;
263 ret++;
264 }
265 }
266 if ((length == 0) && (constructed == 2))
267 ret+=2;
268 ret++;
269 if (length > 127)
270 {
271 while (length > 0)
272 {
273 length>>=8;
274 ret++;
275 }
276 }
277 return(ret);
278 }
279
280int asn1_Finish(c)
281ASN1_CTX *c;
282 {
283 if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos))
284 {
285 if (!ASN1_check_infinite_end(&c->p,c->slen))
286 {
287 c->error=ASN1_R_MISSING_EOS;
288 return(0);
289 }
290 }
291 if ( ((c->slen != 0) && !(c->inf & 1)) ||
292 ((c->slen < 0) && (c->inf & 1)))
293 {
294 c->error=ASN1_R_LENGTH_MISMATCH;
295 return(0);
296 }
297 return(1);
298 }
299
300int asn1_GetSequence(c,length)
301ASN1_CTX *c;
302long *length;
303 {
304 unsigned char *q;
305
306 q=c->p;
307 c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
308 *length);
309 if (c->inf & 0x80)
310 {
311 c->error=ASN1_R_BAD_GET_OBJECT;
312 return(0);
313 }
314 if (c->tag != V_ASN1_SEQUENCE)
315 {
316 c->error=ASN1_R_EXPECTING_A_SEQUENCE;
317 return(0);
318 }
319 (*length)-=(c->p-q);
320 if (c->max && (*length < 0))
321 {
322 c->error=ASN1_R_LENGTH_MISMATCH;
323 return(0);
324 }
325 if (c->inf == (1|V_ASN1_CONSTRUCTED))
326 c->slen= *length+ *(c->pp)-c->p;
327 c->eos=0;
328 return(1);
329 }
330
331ASN1_STRING *ASN1_STRING_dup(str)
332ASN1_STRING *str;
333 {
334 ASN1_STRING *ret;
335
336 if (str == NULL) return(NULL);
337 if ((ret=ASN1_STRING_type_new(str->type)) == NULL)
338 return(NULL);
339 if (!ASN1_STRING_set(ret,str->data,str->length))
340 {
341 ASN1_STRING_free(ret);
342 return(NULL);
343 }
344 return(ret);
345 }
346
347int ASN1_STRING_set(str,data,len)
348ASN1_STRING *str;
349unsigned char *data;
350int len;
351 {
352 char *c;
353
354 if (len < 0)
355 {
356 if (data == NULL)
357 return(0);
358 else
359 len=strlen((char *)data);
360 }
361 if ((str->length < len) || (str->data == NULL))
362 {
363 c=(char *)str->data;
364 if (c == NULL)
365 str->data=(unsigned char *)Malloc(len+1);
366 else
367 str->data=(unsigned char *)Realloc(c,len+1);
368
369 if (str->data == NULL)
370 {
371 str->data=(unsigned char *)c;
372 return(0);
373 }
374 }
375 str->length=len;
376 if (data != NULL)
377 {
378 memcpy(str->data,data,len);
379 /* an alowance for strings :-) */
380 str->data[len]='\0';
381 }
382 return(1);
383 }
384
385ASN1_STRING *ASN1_STRING_new()
386 {
387 return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
388 }
389
390
391ASN1_STRING *ASN1_STRING_type_new(type)
392int type;
393 {
394 ASN1_STRING *ret;
395
396 ret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));
397 if (ret == NULL)
398 {
399 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);
400 return(NULL);
401 }
402 ret->length=0;
403 ret->type=type;
404 ret->data=NULL;
405 return(ret);
406 }
407
408void ASN1_STRING_free(a)
409ASN1_STRING *a;
410 {
411 if (a == NULL) return;
412 if (a->data != NULL) Free((char *)a->data);
413 Free((char *)a);
414 }
415
416int ASN1_STRING_cmp(a,b)
417ASN1_STRING *a,*b;
418 {
419 int i;
420
421 i=(a->length-b->length);
422 if (i == 0)
423 {
424 i=memcmp(a->data,b->data,a->length);
425 if (i == 0)
426 return(a->type-b->type);
427 else
428 return(i);
429 }
430 else
431 return(i);
432 }
433
434void asn1_add_error(address,offset)
435unsigned char *address;
436int offset;
437 {
438 char buf1[16],buf2[16];
439
440 sprintf(buf1,"%lu",(unsigned long)address);
441 sprintf(buf2,"%d",offset);
442 ERR_add_error_data(4,"address=",buf1," offset=",buf2);
443 }
444
diff --git a/src/lib/libcrypto/asn1/asn1_mac.h b/src/lib/libcrypto/asn1/asn1_mac.h
new file mode 100644
index 0000000000..4fba70e4bb
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_mac.h
@@ -0,0 +1,321 @@
1/* crypto/asn1/asn1_mac.h */
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#ifndef HEADER_ASN1_MAC_H
60#define HEADER_ASN1_MAC_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "asn1.h"
67#include "x509.h"
68#include "pkcs7.h"
69
70#define M_ASN1_D2I_vars(a,type,func) \
71 ASN1_CTX c; \
72 type ret=NULL; \
73 \
74 c.pp=pp; \
75 c.error=ASN1_R_ERROR_STACK; \
76 if ((a == NULL) || ((*a) == NULL)) \
77 { if ((ret=(type)func()) == NULL) goto err; } \
78 else ret=(*a);
79
80#define M_ASN1_D2I_Init() \
81 c.p= *pp; \
82 c.max=(length == 0)?0:(c.p+length);
83
84#define M_ASN1_D2I_Finish_2(a) \
85 if (!asn1_Finish(&c)) goto err; \
86 *pp=c.p; \
87 if (a != NULL) (*a)=ret; \
88 return(ret);
89
90#define M_ASN1_D2I_Finish(a,func,e) \
91 M_ASN1_D2I_Finish_2(a); \
92err:\
93 ASN1err((e),c.error); \
94 asn1_add_error(*pp,(int)(c.q- *pp)); \
95 if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
96 return(NULL)
97
98#define M_ASN1_D2I_start_sequence() \
99 if (!asn1_GetSequence(&c,&length)) goto err;
100
101#define M_ASN1_D2I_end_sequence() \
102 (((c.inf&1) == 0)?(c.slen <= 0): \
103 (c.eos=ASN1_check_infinite_end(&c.p,c.slen)))
104
105#define M_ASN1_D2I_get(b,func) \
106 c.q=c.p; \
107 if (func(&(b),&c.p,c.slen) == NULL) goto err; \
108 c.slen-=(c.p-c.q);
109
110#define M_ASN1_D2I_get_opt(b,func,type) \
111 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
112 == (V_ASN1_UNIVERSAL|(type)))) \
113 { \
114 M_ASN1_D2I_get(b,func); \
115 }
116
117#define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
118 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
119 (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
120 { \
121 unsigned char tmp; \
122 tmp=M_ASN1_next; \
123 M_ASN1_next=(tmp& ~V_ASN1_PRIMATIVE_TAG)|type; \
124 M_ASN1_D2I_get(b,func); \
125 M_ASN1_next_prev=tmp; \
126 }
127
128#define M_ASN1_D2I_get_set(r,func) \
129 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SET,V_ASN1_UNIVERSAL);
130
131#define M_ASN1_D2I_get_IMP_set_opt(b,func,tag) \
132 if ((c.slen != 0) && \
133 (M_ASN1_next == \
134 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
135 { \
136 M_ASN1_D2I_get_imp_set(b,func,tag,V_ASN1_CONTEXT_SPECIFIC); \
137 }
138
139#define M_ASN1_D2I_get_seq(r,func) \
140 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
141
142#define M_ASN1_D2I_get_seq_opt(r,func) \
143 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
144 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
145 { M_ASN1_D2I_get_seq(r,func); }
146
147#define M_ASN1_D2I_get_IMP_set(r,func,x) \
148 M_ASN1_D2I_get_imp_set(r,func,x,V_ASN1_CONTEXT_SPECIFIC);
149
150#define M_ASN1_D2I_get_imp_set(r,func,a,b) \
151 c.q=c.p; \
152 if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,a,b) == NULL) \
153 goto err; \
154 c.slen-=(c.p-c.q);
155
156#define M_ASN1_D2I_get_set_strings(r,func,a,b) \
157 c.q=c.p; \
158 if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
159 goto err; \
160 c.slen-=(c.p-c.q);
161
162#define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
163 if ((c.slen != 0L) && (M_ASN1_next == \
164 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
165 { \
166 int Tinf,Ttag,Tclass; \
167 long Tlen; \
168 \
169 c.q=c.p; \
170 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
171 if (Tinf & 0x80) \
172 { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
173 if (func(&(r),&c.p,Tlen) == NULL) \
174 goto err; \
175 c.slen-=(c.p-c.q); \
176 }
177
178#define M_ASN1_D2I_get_EXP_set_opt(r,func,tag,b) \
179 if ((c.slen != 0) && (M_ASN1_next == \
180 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
181 { \
182 int Tinf,Ttag,Tclass; \
183 long Tlen; \
184 \
185 c.q=c.p; \
186 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
187 if (Tinf & 0x80) \
188 { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
189 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
190 b,V_ASN1_UNIVERSAL) == NULL) \
191 goto err; \
192 c.slen-=(c.p-c.q); \
193 }
194
195/* New macros */
196#define M_ASN1_New_Malloc(ret,type) \
197 if ((ret=(type *)Malloc(sizeof(type))) == NULL) goto err2;
198
199#define M_ASN1_New(arg,func) \
200 if (((arg)=func()) == NULL) return(NULL)
201
202#define M_ASN1_New_Error(a) \
203/* err: ASN1err((a),ASN1_R_ERROR_STACK); \
204 return(NULL);*/ \
205 err2: ASN1err((a),ERR_R_MALLOC_FAILURE); \
206 return(NULL)
207
208
209#define M_ASN1_next (*c.p)
210#define M_ASN1_next_prev (*c.q)
211
212/*************************************************/
213
214#define M_ASN1_I2D_vars(a) int r=0,ret=0; \
215 unsigned char *p; \
216 if (a == NULL) return(0)
217
218/* Length Macros */
219#define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
220#define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
221
222#define M_ASN1_I2D_len_SET(a,f) \
223 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL);
224
225#define M_ASN1_I2D_len_SEQ(a,f) \
226 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
227
228#define M_ASN1_I2D_len_SEQ_opt(a,f) \
229 if ((a != NULL) && (sk_num(a) != 0)) \
230 M_ASN1_I2D_len_SEQ(a,f);
231
232#define M_ASN1_I2D_len_IMP_set(a,f,x) \
233 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
234
235#define M_ASN1_I2D_len_IMP_set_opt(a,f,x) \
236 if ((a != NULL) && (sk_num(a) != 0)) \
237 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
238
239#define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
240 if (a != NULL)\
241 { \
242 v=f(a,NULL); \
243 ret+=ASN1_object_size(1,v,mtag); \
244 }
245
246#define M_ASN1_I2D_len_EXP_set_opt(a,f,mtag,tag,v) \
247 if ((a != NULL) && (sk_num(a) != 0))\
248 { \
249 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL); \
250 ret+=ASN1_object_size(1,v,mtag); \
251 }
252
253/* Put Macros */
254#define M_ASN1_I2D_put(a,f) f(a,&p)
255
256#define M_ASN1_I2D_put_IMP_opt(a,f,t) \
257 if (a != NULL) \
258 { \
259 unsigned char *q=p; \
260 f(a,&p); \
261 *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
262 }
263
264#define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
265 V_ASN1_UNIVERSAL)
266#define M_ASN1_I2D_put_IMP_set(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
267 V_ASN1_CONTEXT_SPECIFIC)
268
269#define M_ASN1_I2D_put_SEQ(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
270 V_ASN1_UNIVERSAL)
271
272#define M_ASN1_I2D_put_SEQ_opt(a,f) \
273 if ((a != NULL) && (sk_num(a) != 0)) \
274 M_ASN1_I2D_put_SEQ(a,f);
275
276#define M_ASN1_I2D_put_IMP_set_opt(a,f,x) \
277 if ((a != NULL) && (sk_num(a) != 0)) \
278 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC); }
279
280#define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
281 if (a != NULL) \
282 { \
283 ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
284 f(a,&p); \
285 }
286
287#define M_ASN1_I2D_put_EXP_set_opt(a,f,mtag,tag,v) \
288 if ((a != NULL) && (sk_num(a) != 0)) \
289 { \
290 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
291 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL); \
292 }
293
294#define M_ASN1_I2D_seq_total() \
295 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
296 if (pp == NULL) return(r); \
297 p= *pp; \
298 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
299
300#define M_ASN1_I2D_INF_seq_start(tag,ctx) \
301 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
302 *(p++)=0x80
303
304#define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
305
306#define M_ASN1_I2D_finish() *pp=p; \
307 return(r);
308
309#ifndef NOPROTO
310int asn1_GetSequence(ASN1_CTX *c, long *length);
311void asn1_add_error(unsigned char *address,int offset);
312#else
313int asn1_GetSequence();
314void asn1_add_error();
315#endif
316
317#ifdef __cplusplus
318}
319#endif
320
321#endif
diff --git a/src/lib/libcrypto/asn1/asn1_par.c b/src/lib/libcrypto/asn1/asn1_par.c
new file mode 100644
index 0000000000..3906227d21
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_par.c
@@ -0,0 +1,393 @@
1/* crypto/asn1/asn1_par.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 "buffer.h"
62#include "objects.h"
63#include "x509.h"
64
65#ifndef NOPROTO
66static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
67 int indent);
68static int asn1_parse2(BIO *bp, unsigned char **pp, long length,
69 int offset, int depth, int indent);
70#else
71static int asn1_print_info();
72static int asn1_parse2();
73#endif
74
75static int asn1_print_info(bp, tag, xclass, constructed,indent)
76BIO *bp;
77int tag;
78int xclass;
79int constructed;
80int indent;
81 {
82 static char *fmt="%-18s";
83 static char *fmt2="%2d %-15s";
84 char *p,str[128],*p2=NULL;
85
86 if (constructed & V_ASN1_CONSTRUCTED)
87 p="cons: ";
88 else
89 p="prim: ";
90 if (BIO_write(bp,p,6) < 6) goto err;
91 if (indent)
92 {
93 if (indent > 128) indent=128;
94 memset(str,' ',indent);
95 if (BIO_write(bp,str,indent) < indent) goto err;
96 }
97
98 p=str;
99 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
100 sprintf(str,"priv [ %d ] ",tag);
101 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
102 sprintf(str,"cont [ %d ]",tag);
103 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
104 sprintf(str,"appl [ %d ]",tag);
105 else if ((tag == V_ASN1_EOC) /* && (xclass == V_ASN1_UNIVERSAL) */)
106 p="EOC";
107 else if (tag == V_ASN1_BOOLEAN)
108 p="BOOLEAN";
109 else if (tag == V_ASN1_INTEGER)
110 p="INTEGER";
111 else if (tag == V_ASN1_BIT_STRING)
112 p="BIT STRING";
113 else if (tag == V_ASN1_OCTET_STRING)
114 p="OCTET STRING";
115 else if (tag == V_ASN1_NULL)
116 p="NULL";
117 else if (tag == V_ASN1_OBJECT)
118 p="OBJECT";
119 else if (tag == V_ASN1_SEQUENCE)
120 p="SEQUENCE";
121 else if (tag == V_ASN1_SET)
122 p="SET";
123 else if (tag == V_ASN1_PRINTABLESTRING)
124 p="PRINTABLESTRING";
125 else if (tag == V_ASN1_T61STRING)
126 p="T61STRING";
127 else if (tag == V_ASN1_IA5STRING)
128 p="IA5STRING";
129 else if (tag == V_ASN1_UTCTIME)
130 p="UTCTIME";
131
132 /* extras */
133 else if (tag == V_ASN1_NUMERICSTRING)
134 p="NUMERICSTRING";
135 else if (tag == V_ASN1_VIDEOTEXSTRING)
136 p="VIDEOTEXSTRING";
137 else if (tag == V_ASN1_GENERALIZEDTIME)
138 p="GENERALIZEDTIME";
139 else if (tag == V_ASN1_GRAPHICSTRING)
140 p="GRAPHICSTRING";
141 else if (tag == V_ASN1_ISO64STRING)
142 p="ISO64STRING";
143 else if (tag == V_ASN1_GENERALSTRING)
144 p="GENERALSTRING";
145 else if (tag == V_ASN1_UNIVERSALSTRING)
146 p="UNIVERSALSTRING";
147 else if (tag == V_ASN1_BMPSTRING)
148 p="BMPSTRING";
149 else
150 p2="(unknown)";
151
152 if (p2 != NULL)
153 {
154 if (BIO_printf(bp,fmt2,tag,p2) <= 0) goto err;
155 }
156 else
157 {
158 if (BIO_printf(bp,fmt,p) <= 0) goto err;
159 }
160 return(1);
161err:
162 return(0);
163 }
164
165int ASN1_parse(bp, pp, len, indent)
166BIO *bp;
167unsigned char *pp;
168long len;
169int indent;
170 {
171 return(asn1_parse2(bp,&pp,len,0,0,indent));
172 }
173
174static int asn1_parse2(bp, pp, length, offset, depth, indent)
175BIO *bp;
176unsigned char **pp;
177long length;
178int offset;
179int depth;
180int indent;
181 {
182 unsigned char *p,*ep,*tot,*op,*opp;
183 long len;
184 int tag,xclass,ret=0;
185 int nl,hl,j,r;
186 ASN1_OBJECT *o=NULL;
187 ASN1_OCTET_STRING *os=NULL;
188 /* ASN1_BMPSTRING *bmp=NULL;*/
189
190 p= *pp;
191 tot=p+length;
192 op=p-1;
193 while ((p < tot) && (op < p))
194 {
195 op=p;
196 j=ASN1_get_object(&p,&len,&tag,&xclass,length);
197#ifdef LINT
198 j=j;
199#endif
200 if (j & 0x80)
201 {
202 if (BIO_write(bp,"Error in encoding\n",18) <= 0)
203 goto end;
204 ret=0;
205 goto end;
206 }
207 hl=(p-op);
208 length-=hl;
209 /* if j == 0x21 it is a constructed indefinite length object */
210 if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))
211 <= 0) goto end;
212
213 if (j != (V_ASN1_CONSTRUCTED | 1))
214 {
215 if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",
216 depth,(long)hl,len) <= 0)
217 goto end;
218 }
219 else
220 {
221 if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ",
222 depth,(long)hl) <= 0)
223 goto end;
224 }
225 if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))
226 goto end;
227 if (j & V_ASN1_CONSTRUCTED)
228 {
229 ep=p+len;
230 if (BIO_write(bp,"\n",1) <= 0) goto end;
231 if (len > length)
232 {
233 BIO_printf(bp,
234 "length is greater than %ld\n",length);
235 ret=0;
236 goto end;
237 }
238 if ((j == 0x21) && (len == 0))
239 {
240 for (;;)
241 {
242 r=asn1_parse2(bp,&p,(long)(tot-p),
243 offset+(p - *pp),depth+1,
244 indent);
245 if (r == 0) { ret=0; goto end; }
246 if ((r == 2) || (p >= tot)) break;
247 }
248 }
249 else
250 while (p < ep)
251 {
252 r=asn1_parse2(bp,&p,(long)len,
253 offset+(p - *pp),depth+1,
254 indent);
255 if (r == 0) { ret=0; goto end; }
256 }
257 }
258 else if (xclass != 0)
259 {
260 p+=len;
261 if (BIO_write(bp,"\n",1) <= 0) goto end;
262 }
263 else
264 {
265 nl=0;
266 if ( (tag == V_ASN1_PRINTABLESTRING) ||
267 (tag == V_ASN1_T61STRING) ||
268 (tag == V_ASN1_IA5STRING) ||
269 (tag == V_ASN1_UTCTIME))
270 {
271 if (BIO_write(bp,":",1) <= 0) goto end;
272 if ((len > 0) &&
273 BIO_write(bp,(char *)p,(int)len)
274 != (int)len)
275 goto end;
276 }
277 else if (tag == V_ASN1_OBJECT)
278 {
279 opp=op;
280 if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
281 {
282 if (BIO_write(bp,":",1) <= 0) goto end;
283 i2a_ASN1_OBJECT(bp,o);
284 }
285 else
286 {
287 if (BIO_write(bp,":BAD OBJECT",11) <= 0)
288 goto end;
289 }
290 }
291 else if (tag == V_ASN1_BOOLEAN)
292 {
293 int ii;
294
295 opp=op;
296 ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
297 if (ii < 0)
298 {
299 if (BIO_write(bp,"Bad boolean\n",12))
300 goto end;
301 }
302 BIO_printf(bp,":%d",ii);
303 }
304 else if (tag == V_ASN1_BMPSTRING)
305 {
306 /* do the BMP thang */
307 }
308 else if (tag == V_ASN1_OCTET_STRING)
309 {
310 int i,printable=1;
311
312 opp=op;
313 os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
314 if (os != NULL)
315 {
316 opp=os->data;
317 for (i=0; i<os->length; i++)
318 {
319 if (( (opp[i] < ' ') &&
320 (opp[i] != '\n') &&
321 (opp[i] != '\r') &&
322 (opp[i] != '\t')) ||
323 (opp[i] > '~'))
324 {
325 printable=0;
326 break;
327 }
328 }
329 if (printable && (os->length > 0))
330 {
331 if (BIO_write(bp,":",1) <= 0)
332 goto end;
333 if (BIO_write(bp,(char *)opp,
334 os->length) <= 0)
335 goto end;
336 }
337 ASN1_OCTET_STRING_free(os);
338 os=NULL;
339 }
340 }
341 else if (tag == V_ASN1_INTEGER)
342 {
343 ASN1_INTEGER *bs;
344 int i;
345
346 opp=op;
347 bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
348 if (bs != NULL)
349 {
350 if (BIO_write(bp,":",1) <= 0) goto end;
351 if (bs->type == V_ASN1_NEG_INTEGER)
352 if (BIO_write(bp,"-",1) <= 0)
353 goto end;
354 for (i=0; i<bs->length; i++)
355 {
356 if (BIO_printf(bp,"%02X",
357 bs->data[i]) <= 0)
358 goto end;
359 }
360 if (bs->length == 0)
361 {
362 if (BIO_write(bp,"00",2) <= 0)
363 goto end;
364 }
365 }
366 else
367 {
368 if (BIO_write(bp,"BAD INTEGER",11) <= 0)
369 goto end;
370 }
371 ASN1_INTEGER_free(bs);
372 }
373
374 if (!nl)
375 {
376 if (BIO_write(bp,"\n",1) <= 0) goto end;
377 }
378 p+=len;
379 if ((tag == V_ASN1_EOC) && (xclass == 0))
380 {
381 ret=2; /* End of sequence */
382 goto end;
383 }
384 }
385 length-=len;
386 }
387 ret=1;
388end:
389 if (o != NULL) ASN1_OBJECT_free(o);
390 if (os != NULL) ASN1_OCTET_STRING_free(os);
391 *pp=p;
392 return(ret);
393 }
diff --git a/src/lib/libcrypto/asn1/d2i_pr.c b/src/lib/libcrypto/asn1/d2i_pr.c
new file mode 100644
index 0000000000..b9eaa9629b
--- /dev/null
+++ b/src/lib/libcrypto/asn1/d2i_pr.c
@@ -0,0 +1,117 @@
1/* crypto/asn1/d2i_pr.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 "bn.h"
62#include "evp.h"
63#include "objects.h"
64#include "x509.h"
65
66EVP_PKEY *d2i_PrivateKey(type,a,pp,length)
67int type;
68EVP_PKEY **a;
69unsigned char **pp;
70long length;
71 {
72 EVP_PKEY *ret;
73
74 if ((a == NULL) || (*a == NULL))
75 {
76 if ((ret=EVP_PKEY_new()) == NULL)
77 {
78 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_EVP_LIB);
79 return(NULL);
80 }
81 }
82 else ret= *a;
83
84 ret->save_type=type;
85 ret->type=EVP_PKEY_type(type);
86 switch (ret->type)
87 {
88#ifndef NO_RSA
89 case EVP_PKEY_RSA:
90 if ((ret->pkey.rsa=d2i_RSAPrivateKey(NULL,pp,length)) == NULL)
91 {
92 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
93 goto err;
94 }
95 break;
96#endif
97#ifndef NO_DSA
98 case EVP_PKEY_DSA:
99 if ((ret->pkey.dsa=d2i_DSAPrivateKey(NULL,pp,length)) == NULL)
100 {
101 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
102 goto err;
103 }
104 break;
105#endif
106 default:
107 ASN1err(ASN1_F_D2I_PRIVATEKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
108 goto err;
109 break;
110 }
111 if (a != NULL) (*a)=ret;
112 return(ret);
113err:
114 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
115 return(NULL);
116 }
117
diff --git a/src/lib/libcrypto/asn1/d2i_pu.c b/src/lib/libcrypto/asn1/d2i_pu.c
new file mode 100644
index 0000000000..5d6192f1e5
--- /dev/null
+++ b/src/lib/libcrypto/asn1/d2i_pu.c
@@ -0,0 +1,117 @@
1/* crypto/asn1/d2i_pu.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 "bn.h"
62#include "evp.h"
63#include "objects.h"
64#include "x509.h"
65
66EVP_PKEY *d2i_PublicKey(type,a,pp,length)
67int type;
68EVP_PKEY **a;
69unsigned char **pp;
70long length;
71 {
72 EVP_PKEY *ret;
73
74 if ((a == NULL) || (*a == NULL))
75 {
76 if ((ret=EVP_PKEY_new()) == NULL)
77 {
78 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
79 return(NULL);
80 }
81 }
82 else ret= *a;
83
84 ret->save_type=type;
85 ret->type=EVP_PKEY_type(type);
86 switch (ret->type)
87 {
88#ifndef NO_RSA
89 case EVP_PKEY_RSA:
90 if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,pp,length)) == NULL)
91 {
92 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
93 goto err;
94 }
95 break;
96#endif
97#ifndef NO_DSA
98 case EVP_PKEY_DSA:
99 if ((ret->pkey.dsa=d2i_DSAPublicKey(NULL,pp,length)) == NULL)
100 {
101 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
102 goto err;
103 }
104 break;
105#endif
106 default:
107 ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
108 goto err;
109 break;
110 }
111 if (a != NULL) (*a)=ret;
112 return(ret);
113err:
114 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
115 return(NULL);
116 }
117
diff --git a/src/lib/libcrypto/asn1/evp_asn1.c b/src/lib/libcrypto/asn1/evp_asn1.c
new file mode 100644
index 0000000000..ebe34a3362
--- /dev/null
+++ b/src/lib/libcrypto/asn1/evp_asn1.c
@@ -0,0 +1,193 @@
1/* crypto/asn1/evp_asn1.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 "asn1.h"
62#include "asn1_mac.h"
63
64int ASN1_TYPE_set_octetstring(a,data,len)
65ASN1_TYPE *a;
66unsigned char *data;
67int len;
68 {
69 ASN1_STRING *os;
70
71 if ((os=ASN1_OCTET_STRING_new()) == NULL) return(0);
72 if (!ASN1_OCTET_STRING_set(os,data,len)) return(0);
73 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,(char *)os);
74 return(1);
75 }
76
77int ASN1_TYPE_get_octetstring(a,data,max_len)
78ASN1_TYPE *a;
79unsigned char *data;
80int max_len; /* for returned value */
81 {
82 int ret,num;
83 unsigned char *p;
84
85 if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL))
86 {
87 ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
88 return(-1);
89 }
90 p=ASN1_STRING_data(a->value.octet_string);
91 ret=ASN1_STRING_length(a->value.octet_string);
92 if (ret < max_len)
93 num=ret;
94 else
95 num=max_len;
96 memcpy(data,p,num);
97 return(ret);
98 }
99
100int ASN1_TYPE_set_int_octetstring(a,num,data,len)
101ASN1_TYPE *a;
102long num;
103unsigned char *data;
104int len;
105 {
106 int n,size;
107 ASN1_OCTET_STRING os,*osp;
108 ASN1_INTEGER in;
109 unsigned char *p;
110 unsigned char buf[32]; /* when they have 256bit longs,
111 * I'll be in trouble */
112 in.data=buf;
113 in.length=32;
114 os.data=data;
115 os.type=V_ASN1_OCTET_STRING;
116 os.length=len;
117 ASN1_INTEGER_set(&in,num);
118 n = i2d_ASN1_INTEGER(&in,NULL);
119 n+=M_i2d_ASN1_OCTET_STRING(&os,NULL);
120
121 size=ASN1_object_size(1,n,V_ASN1_SEQUENCE);
122
123 if ((osp=ASN1_STRING_new()) == NULL) return(0);
124 /* Grow the 'string' */
125 ASN1_STRING_set(osp,NULL,size);
126
127 ASN1_STRING_length(osp)=size;
128 p=ASN1_STRING_data(osp);
129
130 ASN1_put_object(&p,1,n,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
131 i2d_ASN1_INTEGER(&in,&p);
132 M_i2d_ASN1_OCTET_STRING(&os,&p);
133
134 ASN1_TYPE_set(a,V_ASN1_SEQUENCE,(char *)osp);
135 return(1);
136 }
137
138/* we return the actual length... */
139int ASN1_TYPE_get_int_octetstring(a,num,data,max_len)
140ASN1_TYPE *a;
141long *num;
142unsigned char *data;
143int max_len; /* for returned value */
144 {
145 int ret= -1,n;
146 ASN1_INTEGER *ai=NULL;
147 ASN1_OCTET_STRING *os=NULL;
148 unsigned char *p;
149 long length;
150 ASN1_CTX c;
151
152 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
153 {
154 goto err;
155 }
156 p=ASN1_STRING_data(a->value.sequence);
157 length=ASN1_STRING_length(a->value.sequence);
158
159 c.pp= &p;
160 c.p=p;
161 c.max=p+length;
162 c.error=ASN1_R_DATA_IS_WRONG;
163
164 M_ASN1_D2I_start_sequence();
165 c.q=c.p;
166 if ((ai=d2i_ASN1_INTEGER(NULL,&c.p,c.slen)) == NULL) goto err;
167 c.slen-=(c.p-c.q);
168 c.q=c.p;
169 if ((os=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) goto err;
170 c.slen-=(c.p-c.q);
171 if (!M_ASN1_D2I_end_sequence()) goto err;
172
173 if (num != NULL)
174 *num=ASN1_INTEGER_get(ai);
175
176 ret=ASN1_STRING_length(os);
177 if (max_len > ret)
178 n=ret;
179 else
180 n=max_len;
181
182 if (data != NULL)
183 memcpy(data,ASN1_STRING_data(os),n);
184 if (0)
185 {
186err:
187 ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
188 }
189 if (os != NULL) ASN1_OCTET_STRING_free(os);
190 if (ai != NULL) ASN1_INTEGER_free(ai);
191 return(ret);
192 }
193
diff --git a/src/lib/libcrypto/asn1/f_int.c b/src/lib/libcrypto/asn1/f_int.c
new file mode 100644
index 0000000000..4817c45cb7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/f_int.c
@@ -0,0 +1,211 @@
1/* crypto/asn1/f_int.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 "buffer.h"
62#include "x509.h"
63
64int i2a_ASN1_INTEGER(bp, a)
65BIO *bp;
66ASN1_INTEGER *a;
67 {
68 int i,n=0;
69 static char *h="0123456789ABCDEF";
70 char buf[2];
71
72 if (a == NULL) return(0);
73
74 if (a->length == 0)
75 {
76 if (BIO_write(bp,"00",2) != 2) goto err;
77 n=2;
78 }
79 else
80 {
81 for (i=0; i<a->length; i++)
82 {
83 if ((i != 0) && (i%35 == 0))
84 {
85 if (BIO_write(bp,"\\\n",2) != 2) goto err;
86 n+=2;
87 }
88 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
89 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
90 if (BIO_write(bp,buf,2) != 2) goto err;
91 n+=2;
92 }
93 }
94 return(n);
95err:
96 return(-1);
97 }
98
99int a2i_ASN1_INTEGER(bp,bs,buf,size)
100BIO *bp;
101ASN1_INTEGER *bs;
102char *buf;
103int size;
104 {
105 int ret=0;
106 int i,j,k,m,n,again,bufsize;
107 unsigned char *s=NULL,*sp;
108 unsigned char *bufp;
109 int num=0,slen=0,first=1;
110
111 bs->type=V_ASN1_INTEGER;
112
113 bufsize=BIO_gets(bp,buf,size);
114 for (;;)
115 {
116 if (bufsize < 1) goto err_sl;
117 i=bufsize;
118 if (buf[i-1] == '\n') buf[--i]='\0';
119 if (i == 0) goto err_sl;
120 if (buf[i-1] == '\r') buf[--i]='\0';
121 if (i == 0) goto err_sl;
122 again=(buf[i-1] == '\\');
123
124 for (j=0; j<i; j++)
125 {
126 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
127 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
128 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
129 {
130 i=j;
131 break;
132 }
133 }
134 buf[i]='\0';
135 /* We have now cleared all the crap off the end of the
136 * line */
137 if (i < 2) goto err_sl;
138
139 bufp=(unsigned char *)buf;
140 if (first)
141 {
142 first=0;
143 if ((bufp[0] == '0') && (buf[1] == '0'))
144 {
145 bufp+=2;
146 i-=2;
147 }
148 }
149 k=0;
150 i-=again;
151 if (i%2 != 0)
152 {
153 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS);
154 goto err;
155 }
156 i/=2;
157 if (num+i > slen)
158 {
159 if (s == NULL)
160 sp=(unsigned char *)Malloc(
161 (unsigned int)num+i*2);
162 else
163 sp=(unsigned char *)Realloc(s,
164 (unsigned int)num+i*2);
165 if (sp == NULL)
166 {
167 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
168 if (s != NULL) Free((char *)s);
169 goto err;
170 }
171 s=sp;
172 slen=num+i*2;
173 }
174 for (j=0; j<i; j++,k+=2)
175 {
176 for (n=0; n<2; n++)
177 {
178 m=bufp[k+n];
179 if ((m >= '0') && (m <= '9'))
180 m-='0';
181 else if ((m >= 'a') && (m <= 'f'))
182 m=m-'a'+10;
183 else if ((m >= 'A') && (m <= 'F'))
184 m=m-'A'+10;
185 else
186 {
187 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS);
188 goto err;
189 }
190 s[num+j]<<=4;
191 s[num+j]|=m;
192 }
193 }
194 num+=i;
195 if (again)
196 bufsize=BIO_gets(bp,buf,size);
197 else
198 break;
199 }
200 bs->length=num;
201 bs->data=s;
202 ret=1;
203err:
204 if (0)
205 {
206err_sl:
207 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE);
208 }
209 return(ret);
210 }
211
diff --git a/src/lib/libcrypto/asn1/f_string.c b/src/lib/libcrypto/asn1/f_string.c
new file mode 100644
index 0000000000..ab2837824e
--- /dev/null
+++ b/src/lib/libcrypto/asn1/f_string.c
@@ -0,0 +1,210 @@
1/* crypto/asn1/f_string.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 "buffer.h"
62#include "x509.h"
63
64int i2a_ASN1_STRING(bp, a, type)
65BIO *bp;
66ASN1_STRING *a;
67int type;
68 {
69 int i,n=0;
70 static char *h="0123456789ABCDEF";
71 char buf[2];
72
73 if (a == NULL) return(0);
74
75 if (a->length == 0)
76 {
77 if (BIO_write(bp,"0",1) != 1) goto err;
78 n=1;
79 }
80 else
81 {
82 for (i=0; i<a->length; i++)
83 {
84 if ((i != 0) && (i%35 == 0))
85 {
86 if (BIO_write(bp,"\\\n",2) != 2) goto err;
87 n+=2;
88 }
89 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
90 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
91 if (BIO_write(bp,buf,2) != 2) goto err;
92 n+=2;
93 }
94 }
95 return(n);
96err:
97 return(-1);
98 }
99
100int a2i_ASN1_STRING(bp,bs,buf,size)
101BIO *bp;
102ASN1_STRING *bs;
103char *buf;
104int size;
105 {
106 int ret=0;
107 int i,j,k,m,n,again,bufsize;
108 unsigned char *s=NULL,*sp;
109 unsigned char *bufp;
110 int num=0,slen=0,first=1;
111
112 bufsize=BIO_gets(bp,buf,size);
113 for (;;)
114 {
115 if (bufsize < 1)
116 {
117 if (first)
118 break;
119 else
120 goto err_sl;
121 }
122 first=0;
123
124 i=bufsize;
125 if (buf[i-1] == '\n') buf[--i]='\0';
126 if (i == 0) goto err_sl;
127 if (buf[i-1] == '\r') buf[--i]='\0';
128 if (i == 0) goto err_sl;
129 again=(buf[i-1] == '\\');
130
131 for (j=i-1; j>0; j--)
132 {
133 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
134 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
135 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
136 {
137 i=j;
138 break;
139 }
140 }
141 buf[i]='\0';
142 /* We have now cleared all the crap off the end of the
143 * line */
144 if (i < 2) goto err_sl;
145
146 bufp=(unsigned char *)buf;
147
148 k=0;
149 i-=again;
150 if (i%2 != 0)
151 {
152 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS);
153 goto err;
154 }
155 i/=2;
156 if (num+i > slen)
157 {
158 if (s == NULL)
159 sp=(unsigned char *)Malloc(
160 (unsigned int)num+i*2);
161 else
162 sp=(unsigned char *)Realloc(s,
163 (unsigned int)num+i*2);
164 if (sp == NULL)
165 {
166 ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE);
167 if (s != NULL) Free((char *)s);
168 goto err;
169 }
170 s=sp;
171 slen=num+i*2;
172 }
173 for (j=0; j<i; j++,k+=2)
174 {
175 for (n=0; n<2; n++)
176 {
177 m=bufp[k+n];
178 if ((m >= '0') && (m <= '9'))
179 m-='0';
180 else if ((m >= 'a') && (m <= 'f'))
181 m=m-'a'+10;
182 else if ((m >= 'A') && (m <= 'F'))
183 m=m-'A'+10;
184 else
185 {
186 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS);
187 goto err;
188 }
189 s[num+j]<<=4;
190 s[num+j]|=m;
191 }
192 }
193 num+=i;
194 if (again)
195 bufsize=BIO_gets(bp,buf,size);
196 else
197 break;
198 }
199 bs->length=num;
200 bs->data=s;
201 ret=1;
202err:
203 if (0)
204 {
205err_sl:
206 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE);
207 }
208 return(ret);
209 }
210
diff --git a/src/lib/libcrypto/asn1/i2d_pr.c b/src/lib/libcrypto/asn1/i2d_pr.c
new file mode 100644
index 0000000000..b6b821d73c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/i2d_pr.c
@@ -0,0 +1,86 @@
1/* crypto/asn1/i2d_pr.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 "bn.h"
62#include "evp.h"
63#include "objects.h"
64
65int i2d_PrivateKey(a,pp)
66EVP_PKEY *a;
67unsigned char **pp;
68 {
69#ifndef NO_RSA
70 if (a->type == EVP_PKEY_RSA)
71 {
72 return(i2d_RSAPrivateKey(a->pkey.rsa,pp));
73 }
74 else
75#endif
76#ifndef NO_DSA
77 if (a->type == EVP_PKEY_DSA)
78 {
79 return(i2d_DSAPrivateKey(a->pkey.dsa,pp));
80 }
81#endif
82
83 ASN1err(ASN1_F_I2D_PRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
84 return(-1);
85 }
86
diff --git a/src/lib/libcrypto/asn1/i2d_pu.c b/src/lib/libcrypto/asn1/i2d_pu.c
new file mode 100644
index 0000000000..1b854252b7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/i2d_pu.c
@@ -0,0 +1,84 @@
1/* crypto/asn1/i2d_pu.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 "bn.h"
62#include "evp.h"
63#include "objects.h"
64
65int i2d_PublicKey(a,pp)
66EVP_PKEY *a;
67unsigned char **pp;
68 {
69 switch (a->type)
70 {
71#ifndef NO_RSA
72 case EVP_PKEY_RSA:
73 return(i2d_RSAPublicKey(a->pkey.rsa,pp));
74#endif
75#ifndef NO_DSA
76 case EVP_PKEY_DSA:
77 return(i2d_DSAPublicKey(a->pkey.dsa,pp));
78#endif
79 default:
80 ASN1err(ASN1_F_I2D_PUBLICKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
81 return(-1);
82 }
83 }
84
diff --git a/src/lib/libcrypto/asn1/n_pkey.c b/src/lib/libcrypto/asn1/n_pkey.c
new file mode 100644
index 0000000000..5110c91bec
--- /dev/null
+++ b/src/lib/libcrypto/asn1/n_pkey.c
@@ -0,0 +1,365 @@
1/* crypto/asn1/n_pkey.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 "rsa.h"
62#include "objects.h"
63#include "asn1_mac.h"
64#include "evp.h"
65#include "x509.h"
66
67
68#ifndef NO_RC4
69
70typedef struct netscape_pkey_st
71 {
72 ASN1_INTEGER *version;
73 X509_ALGOR *algor;
74 ASN1_OCTET_STRING *private_key;
75 } NETSCAPE_PKEY;
76
77/*
78 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_LENGTH_MISMATCH);
79 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_DECODING_ERROR);
80 * ASN1err(ASN1_F_D2I_NETSCAPE_PKEY,ASN1_R_DECODING_ERROR);
81 * ASN1err(ASN1_F_NETSCAPE_PKEY_NEW,ASN1_R_DECODING_ERROR);
82 */
83#ifndef NOPROTO
84static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp);
85static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length);
86static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void);
87static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *);
88#else
89static int i2d_NETSCAPE_PKEY();
90static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY();
91static NETSCAPE_PKEY *NETSCAPE_PKEY_new();
92static void NETSCAPE_PKEY_free();
93#endif
94
95int i2d_Netscape_RSA(a,pp,cb)
96RSA *a;
97unsigned char **pp;
98int (*cb)();
99 {
100 int i,j,l[6];
101 NETSCAPE_PKEY *pkey;
102 unsigned char buf[256],*zz;
103 unsigned char key[EVP_MAX_KEY_LENGTH];
104 EVP_CIPHER_CTX ctx;
105 X509_ALGOR *alg=NULL;
106 ASN1_OCTET_STRING os,os2;
107 M_ASN1_I2D_vars(a);
108
109 if (a == NULL) return(0);
110
111#ifdef WIN32
112 r=r; /* shut the damn compiler up :-) */
113#endif
114
115 os.data=os2.data=NULL;
116 if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
117 if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
118
119 if (pkey->algor->algorithm != NULL)
120 ASN1_OBJECT_free(pkey->algor->algorithm);
121 pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
122 if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
123 pkey->algor->parameter->type=V_ASN1_NULL;
124
125 l[0]=i2d_RSAPrivateKey(a,NULL);
126 pkey->private_key->length=l[0];
127
128 os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
129 l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
130
131 if ((alg=X509_ALGOR_new()) == NULL) goto err;
132 if (alg->algorithm != NULL)
133 ASN1_OBJECT_free(alg->algorithm);
134 alg->algorithm=OBJ_nid2obj(NID_rc4);
135 if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
136 alg->parameter->type=V_ASN1_NULL;
137
138 l[2]=i2d_X509_ALGOR(alg,NULL);
139 l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
140
141 os.data=(unsigned char *)"private-key";
142 os.length=11;
143 l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
144
145 l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
146
147 if (pp == NULL)
148 {
149 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
150 if (alg != NULL) X509_ALGOR_free(alg);
151 return(l[5]);
152 }
153
154 if (pkey->private_key->data != NULL)
155 Free((char *)pkey->private_key->data);
156 if ((pkey->private_key->data=(unsigned char *)Malloc(l[0])) == NULL)
157 {
158 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
159 goto err;
160 }
161 zz=pkey->private_key->data;
162 i2d_RSAPrivateKey(a,&zz);
163
164 if ((os2.data=(unsigned char *)Malloc(os2.length)) == NULL)
165 {
166 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
167 goto err;
168 }
169 zz=os2.data;
170 i2d_NETSCAPE_PKEY(pkey,&zz);
171
172 if (cb == NULL)
173 cb=EVP_read_pw_string;
174 i=cb(buf,256,"Enter Private Key password:",1);
175 if (i != 0)
176 {
177 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
178 goto err;
179 }
180 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
181 strlen((char *)buf),1,key,NULL);
182 memset(buf,0,256);
183
184 EVP_CIPHER_CTX_init(&ctx);
185 EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
186 EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
187 EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
188 EVP_CIPHER_CTX_cleanup(&ctx);
189
190 p= *pp;
191 ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
192 i2d_ASN1_OCTET_STRING(&os,&p);
193 ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
194 i2d_X509_ALGOR(alg,&p);
195 i2d_ASN1_OCTET_STRING(&os2,&p);
196 ret=l[5];
197err:
198 if (os2.data != NULL) Free((char *)os2.data);
199 if (alg != NULL) X509_ALGOR_free(alg);
200 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
201 r=r;
202 return(ret);
203 }
204
205RSA *d2i_Netscape_RSA(a,pp,length,cb)
206RSA **a;
207unsigned char **pp;
208long length;
209int (*cb)();
210 {
211 RSA *ret=NULL;
212 ASN1_OCTET_STRING *os=NULL;
213 ASN1_CTX c;
214
215 c.pp=pp;
216 c.error=ASN1_R_DECODING_ERROR;
217
218 M_ASN1_D2I_Init();
219 M_ASN1_D2I_start_sequence();
220 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
221 if ((os->length != 11) || (strncmp("private-key",
222 (char *)os->data,os->length) != 0))
223 {
224 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
225 ASN1_BIT_STRING_free(os);
226 goto err;
227 }
228 ASN1_BIT_STRING_free(os);
229 c.q=c.p;
230 if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err;
231 c.slen-=(c.p-c.q);
232
233 M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
234 }
235
236RSA *d2i_Netscape_RSA_2(a,pp,length,cb)
237RSA **a;
238unsigned char **pp;
239long length;
240int (*cb)();
241 {
242 NETSCAPE_PKEY *pkey=NULL;
243 RSA *ret=NULL;
244 int i,j;
245 unsigned char buf[256],*zz;
246 unsigned char key[EVP_MAX_KEY_LENGTH];
247 EVP_CIPHER_CTX ctx;
248 X509_ALGOR *alg=NULL;
249 ASN1_OCTET_STRING *os=NULL;
250 ASN1_CTX c;
251
252 c.error=ASN1_R_ERROR_STACK;
253 c.pp=pp;
254
255 M_ASN1_D2I_Init();
256 M_ASN1_D2I_start_sequence();
257 M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
258 if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
259 {
260 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
261 goto err;
262 }
263 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
264 if (cb == NULL)
265 cb=EVP_read_pw_string;
266 i=cb(buf,256,"Enter Private Key password:",0);
267 if (i != 0)
268 {
269 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
270 goto err;
271 }
272
273 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
274 strlen((char *)buf),1,key,NULL);
275 memset(buf,0,256);
276
277 EVP_CIPHER_CTX_init(&ctx);
278 EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
279 EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
280 EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
281 EVP_CIPHER_CTX_cleanup(&ctx);
282 os->length=i+j;
283
284 zz=os->data;
285
286 if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
287 {
288 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
289 goto err;
290 }
291
292 zz=pkey->private_key->data;
293 if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
294 {
295 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
296 goto err;
297 }
298 if (!asn1_Finish(&c)) goto err;
299 *pp=c.p;
300err:
301 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
302 if (os != NULL) ASN1_BIT_STRING_free(os);
303 if (alg != NULL) X509_ALGOR_free(alg);
304 return(ret);
305 }
306
307static int i2d_NETSCAPE_PKEY(a,pp)
308NETSCAPE_PKEY *a;
309unsigned char **pp;
310 {
311 M_ASN1_I2D_vars(a);
312
313
314 M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER);
315 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
316 M_ASN1_I2D_len(a->private_key, i2d_ASN1_OCTET_STRING);
317
318 M_ASN1_I2D_seq_total();
319
320 M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER);
321 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
322 M_ASN1_I2D_put(a->private_key, i2d_ASN1_OCTET_STRING);
323
324 M_ASN1_I2D_finish();
325 }
326
327static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(a,pp,length)
328NETSCAPE_PKEY **a;
329unsigned char **pp;
330long length;
331 {
332 M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
333
334 M_ASN1_D2I_Init();
335 M_ASN1_D2I_start_sequence();
336 M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
337 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
338 M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
339 M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
340 }
341
342static NETSCAPE_PKEY *NETSCAPE_PKEY_new()
343 {
344 NETSCAPE_PKEY *ret=NULL;
345
346 M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
347 M_ASN1_New(ret->version,ASN1_INTEGER_new);
348 M_ASN1_New(ret->algor,X509_ALGOR_new);
349 M_ASN1_New(ret->private_key,ASN1_OCTET_STRING_new);
350 return(ret);
351 M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
352 }
353
354static void NETSCAPE_PKEY_free(a)
355NETSCAPE_PKEY *a;
356 {
357 if (a == NULL) return;
358 ASN1_INTEGER_free(a->version);
359 X509_ALGOR_free(a->algor);
360 ASN1_OCTET_STRING_free(a->private_key);
361 Free((char *)a);
362 }
363
364#endif /* NO_RC4 */
365
diff --git a/src/lib/libcrypto/asn1/t_pkey.c b/src/lib/libcrypto/asn1/t_pkey.c
new file mode 100644
index 0000000000..bc518d59a2
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_pkey.c
@@ -0,0 +1,392 @@
1/* crypto/asn1/t_pkey.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 "buffer.h"
62#include "bn.h"
63#ifndef NO_RSA
64#include "rsa.h"
65#endif
66#ifndef NO_DH
67#include "dh.h"
68#endif
69#ifndef NO_DSA
70#include "dsa.h"
71#endif
72
73/* DHerr(DH_F_DHPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
74 * DSAerr(DSA_F_DSAPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
75 */
76
77#ifndef NOPROTO
78static int print(BIO *fp,char *str,BIGNUM *num,
79 unsigned char *buf,int off);
80#else
81static int print();
82#endif
83
84#ifndef NO_RSA
85#ifndef NO_FP_API
86int RSA_print_fp(fp,x,off)
87FILE *fp;
88RSA *x;
89int off;
90 {
91 BIO *b;
92 int ret;
93
94 if ((b=BIO_new(BIO_s_file())) == NULL)
95 {
96 RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
97 return(0);
98 }
99 BIO_set_fp(b,fp,BIO_NOCLOSE);
100 ret=RSA_print(b,x,off);
101 BIO_free(b);
102 return(ret);
103 }
104#endif
105
106int RSA_print(bp,x,off)
107BIO *bp;
108RSA *x;
109int off;
110 {
111 char str[128],*s;
112 unsigned char *m=NULL;
113 int i,ret=0;
114
115 i=RSA_size(x);
116 m=(unsigned char *)Malloc((unsigned int)i+10);
117 if (m == NULL)
118 {
119 RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
120 goto err;
121 }
122
123 if (off)
124 {
125 if (off > 128) off=128;
126 memset(str,' ',off);
127 }
128 if (x->d != NULL)
129 {
130 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
131 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
132 <= 0) goto err;
133 }
134
135 if (x->d == NULL)
136 sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
137 else
138 strcpy(str,"modulus:");
139 if (!print(bp,str,x->n,m,off)) goto err;
140 s=(x->d == NULL)?"Exponent:":"publicExponent:";
141 if (!print(bp,s,x->e,m,off)) goto err;
142 if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
143 if (!print(bp,"prime1:",x->p,m,off)) goto err;
144 if (!print(bp,"prime2:",x->q,m,off)) goto err;
145 if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
146 if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
147 if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
148 ret=1;
149err:
150 if (m != NULL) Free((char *)m);
151 return(ret);
152 }
153#endif /* NO_RSA */
154
155#ifndef NO_DSA
156#ifndef NO_FP_API
157int DSA_print_fp(fp,x,off)
158FILE *fp;
159DSA *x;
160int off;
161 {
162 BIO *b;
163 int ret;
164
165 if ((b=BIO_new(BIO_s_file())) == NULL)
166 {
167 DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
168 return(0);
169 }
170 BIO_set_fp(b,fp,BIO_NOCLOSE);
171 ret=DSA_print(b,x,off);
172 BIO_free(b);
173 return(ret);
174 }
175#endif
176
177int DSA_print(bp,x,off)
178BIO *bp;
179DSA *x;
180int off;
181 {
182 char str[128];
183 unsigned char *m=NULL;
184 int i,ret=0;
185 BIGNUM *bn=NULL;
186
187 if (x->p != NULL)
188 bn=x->p;
189 else if (x->priv_key != NULL)
190 bn=x->priv_key;
191 else if (x->pub_key != NULL)
192 bn=x->pub_key;
193
194 /* larger than needed but what the hell :-) */
195 if (bn != NULL)
196 i=BN_num_bytes(bn)*2;
197 else
198 i=256;
199 m=(unsigned char *)Malloc((unsigned int)i+10);
200 if (m == NULL)
201 {
202 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
203 goto err;
204 }
205
206 if (off)
207 {
208 if (off > 128) off=128;
209 memset(str,' ',off);
210 }
211 if (x->priv_key != NULL)
212 {
213 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
214 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
215 <= 0) goto err;
216 }
217
218 if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
219 goto err;
220 if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
221 goto err;
222 if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
223 if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
224 if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
225 ret=1;
226err:
227 if (m != NULL) Free((char *)m);
228 return(ret);
229 }
230#endif /* !NO_DSA */
231
232static int print(bp,number,num,buf,off)
233BIO *bp;
234char *number;
235BIGNUM *num;
236unsigned char *buf;
237int off;
238 {
239 int n,i;
240 char str[128],*neg;
241
242 if (num == NULL) return(1);
243 neg=(num->neg)?"-":"";
244 if (off)
245 {
246 if (off > 128) off=128;
247 memset(str,' ',off);
248 if (BIO_write(bp,str,off) <= 0) return(0);
249 }
250
251 if (BN_num_bytes(num) <= BN_BYTES)
252 {
253 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
254 (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
255 <= 0) return(0);
256 }
257 else
258 {
259 buf[0]=0;
260 if (BIO_printf(bp,"%s%s",number,
261 (neg[0] == '-')?" (Negative)":"") <= 0)
262 return(0);
263 n=BN_bn2bin(num,&buf[1]);
264
265 if (buf[1] & 0x80)
266 n++;
267 else buf++;
268
269 for (i=0; i<n; i++)
270 {
271 if ((i%15) == 0)
272 {
273 str[0]='\n';
274 memset(&(str[1]),' ',off+4);
275 if (BIO_write(bp,str,off+1+4) <= 0) return(0);
276 }
277 if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
278 <= 0) return(0);
279 }
280 if (BIO_write(bp,"\n",1) <= 0) return(0);
281 }
282 return(1);
283 }
284
285#ifndef NO_DH
286#ifndef NO_FP_API
287int DHparams_print_fp(fp,x)
288FILE *fp;
289DH *x;
290 {
291 BIO *b;
292 int ret;
293
294 if ((b=BIO_new(BIO_s_file())) == NULL)
295 {
296 DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
297 return(0);
298 }
299 BIO_set_fp(b,fp,BIO_NOCLOSE);
300 ret=DHparams_print(b, x);
301 BIO_free(b);
302 return(ret);
303 }
304#endif
305
306int DHparams_print(bp,x)
307BIO *bp;
308DH *x;
309 {
310 unsigned char *m=NULL;
311 int reason=ERR_R_BUF_LIB,i,ret=0;
312
313 i=BN_num_bytes(x->p);
314 m=(unsigned char *)Malloc((unsigned int)i+10);
315 if (m == NULL)
316 {
317 reason=ERR_R_MALLOC_FAILURE;
318 goto err;
319 }
320
321 if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
322 BN_num_bits(x->p)) <= 0)
323 goto err;
324 if (!print(bp,"prime:",x->p,m,4)) goto err;
325 if (!print(bp,"generator:",x->g,m,4)) goto err;
326 if (x->length != 0)
327 {
328 if (BIO_printf(bp," recomented-private-length: %d bits\n",
329 (int)x->length) <= 0) goto err;
330 }
331 ret=1;
332 if (0)
333 {
334err:
335 DHerr(DH_F_DHPARAMS_PRINT,reason);
336 }
337 if (m != NULL) Free((char *)m);
338 return(ret);
339 }
340#endif
341
342#ifndef NO_DSA
343#ifndef NO_FP_API
344int DSAparams_print_fp(fp,x)
345FILE *fp;
346DSA *x;
347 {
348 BIO *b;
349 int ret;
350
351 if ((b=BIO_new(BIO_s_file())) == NULL)
352 {
353 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
354 return(0);
355 }
356 BIO_set_fp(b,fp,BIO_NOCLOSE);
357 ret=DSAparams_print(b, x);
358 BIO_free(b);
359 return(ret);
360 }
361#endif
362
363int DSAparams_print(bp,x)
364BIO *bp;
365DSA *x;
366 {
367 unsigned char *m=NULL;
368 int reason=ERR_R_BUF_LIB,i,ret=0;
369
370 i=BN_num_bytes(x->p);
371 m=(unsigned char *)Malloc((unsigned int)i+10);
372 if (m == NULL)
373 {
374 reason=ERR_R_MALLOC_FAILURE;
375 goto err;
376 }
377
378 if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
379 BN_num_bits(x->p)) <= 0)
380 goto err;
381 if (!print(bp,"p:",x->p,m,4)) goto err;
382 if (!print(bp,"q:",x->q,m,4)) goto err;
383 if (!print(bp,"g:",x->g,m,4)) goto err;
384 ret=1;
385err:
386 if (m != NULL) Free((char *)m);
387 DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
388 return(ret);
389 }
390
391#endif /* !NO_DSA */
392
diff --git a/src/lib/libcrypto/asn1/t_req.c b/src/lib/libcrypto/asn1/t_req.c
new file mode 100644
index 0000000000..7df749a48f
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_req.c
@@ -0,0 +1,226 @@
1/* crypto/asn1/t_req.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 "buffer.h"
62#include "bn.h"
63#include "objects.h"
64#include "x509.h"
65
66#ifndef NO_FP_API
67int X509_REQ_print_fp(fp,x)
68FILE *fp;
69X509_REQ *x;
70 {
71 BIO *b;
72 int ret;
73
74 if ((b=BIO_new(BIO_s_file())) == NULL)
75 {
76 X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
77 return(0);
78 }
79 BIO_set_fp(b,fp,BIO_NOCLOSE);
80 ret=X509_REQ_print(b, x);
81 BIO_free(b);
82 return(ret);
83 }
84#endif
85
86int X509_REQ_print(bp,x)
87BIO *bp;
88X509_REQ *x;
89 {
90 unsigned long l;
91 int i,n;
92 char *s,*neg;
93 X509_REQ_INFO *ri;
94 EVP_PKEY *pkey;
95 STACK *sk;
96 char str[128];
97
98 ri=x->req_info;
99 sprintf(str,"Certificate Request:\n");
100 if (BIO_puts(bp,str) <= 0) goto err;
101 sprintf(str,"%4sData:\n","");
102 if (BIO_puts(bp,str) <= 0) goto err;
103
104 neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
105 l=0;
106 for (i=0; i<ri->version->length; i++)
107 { l<<=8; l+=ri->version->data[i]; }
108 sprintf(str,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,l);
109 if (BIO_puts(bp,str) <= 0) goto err;
110 sprintf(str,"%8sSubject: ","");
111 if (BIO_puts(bp,str) <= 0) goto err;
112
113 X509_NAME_print(bp,ri->subject,16);
114 sprintf(str,"\n%8sSubject Public Key Info:\n","");
115 if (BIO_puts(bp,str) <= 0) goto err;
116 i=OBJ_obj2nid(ri->pubkey->algor->algorithm);
117 sprintf(str,"%12sPublic Key Algorithm: %s\n","",
118 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
119 if (BIO_puts(bp,str) <= 0) goto err;
120
121 pkey=X509_REQ_get_pubkey(x);
122#ifndef NO_RSA
123 if (pkey->type == EVP_PKEY_RSA)
124 {
125 BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
126 BN_num_bits(pkey->pkey.rsa->n));
127 RSA_print(bp,pkey->pkey.rsa,16);
128 }
129 else
130#endif
131#ifndef NO_DSA
132 if (pkey->type == EVP_PKEY_DSA)
133 {
134 BIO_printf(bp,"%12sDSA Public Key:\n","");
135 DSA_print(bp,pkey->pkey.dsa,16);
136 }
137 else
138#endif
139 BIO_printf(bp,"%12sUnknown Public Key:\n","");
140
141 /* may not be */
142 sprintf(str,"%8sAttributes:\n","");
143 if (BIO_puts(bp,str) <= 0) goto err;
144
145 sk=x->req_info->attributes;
146 if ((sk == NULL) || (sk_num(sk) == 0))
147 {
148 if (!x->req_info->req_kludge)
149 {
150 sprintf(str,"%12sa0:00\n","");
151 if (BIO_puts(bp,str) <= 0) goto err;
152 }
153 }
154 else
155 {
156 for (i=0; i<sk_num(sk); i++)
157 {
158 ASN1_TYPE *at;
159 X509_ATTRIBUTE *a;
160 ASN1_BIT_STRING *bs=NULL;
161 ASN1_TYPE *t;
162 int j,type=0,count=1,ii=0;
163
164 a=(X509_ATTRIBUTE *)sk_value(sk,i);
165 sprintf(str,"%12s","");
166 if (BIO_puts(bp,str) <= 0) goto err;
167 if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
168
169 if (a->set)
170 {
171 ii=0;
172 count=sk_num(a->value.set);
173get_next:
174 at=(ASN1_TYPE *)sk_value(a->value.set,ii);
175 type=at->type;
176 bs=at->value.asn1_string;
177 }
178 else
179 {
180 t=a->value.single;
181 type=t->type;
182 bs=t->value.bit_string;
183 }
184 for (j=25-j; j>0; j--)
185 if (BIO_write(bp," ",1) != 1) goto err;
186 if (BIO_puts(bp,":") <= 0) goto err;
187 if ( (type == V_ASN1_PRINTABLESTRING) ||
188 (type == V_ASN1_T61STRING) ||
189 (type == V_ASN1_IA5STRING))
190 {
191 if (BIO_write(bp,(char *)bs->data,bs->length)
192 != bs->length)
193 goto err;
194 BIO_puts(bp,"\n");
195 }
196 else
197 {
198 BIO_puts(bp,"unable to print attribute\n");
199 }
200 if (++ii < count) goto get_next;
201 }
202 }
203
204 i=OBJ_obj2nid(x->sig_alg->algorithm);
205 sprintf(str,"%4sSignature Algorithm: %s","",
206 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
207 if (BIO_puts(bp,str) <= 0) goto err;
208
209 n=x->signature->length;
210 s=(char *)x->signature->data;
211 for (i=0; i<n; i++)
212 {
213 if ((i%18) == 0)
214 {
215 sprintf(str,"\n%8s","");
216 if (BIO_puts(bp,str) <= 0) goto err;
217 }
218 sprintf(str,"%02x%s",(unsigned char)s[i],((i+1) == n)?"":":");
219 if (BIO_puts(bp,str) <= 0) goto err;
220 }
221 if (BIO_puts(bp,"\n") <= 0) goto err;
222 return(1);
223err:
224 X509err(X509_F_X509_REQ_PRINT,ERR_R_BUF_LIB);
225 return(0);
226 }
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
new file mode 100644
index 0000000000..b10fbbb992
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_x509.c
@@ -0,0 +1,386 @@
1/* crypto/asn1/t_x509.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 "buffer.h"
62#include "bn.h"
63#ifndef NO_RSA
64#include "rsa.h"
65#endif
66#ifndef NO_DSA
67#include "dsa.h"
68#endif
69#include "objects.h"
70#include "x509.h"
71
72#ifndef NO_FP_API
73int X509_print_fp(fp,x)
74FILE *fp;
75X509 *x;
76 {
77 BIO *b;
78 int ret;
79
80 if ((b=BIO_new(BIO_s_file())) == NULL)
81 {
82 X509err(X509_F_X509_PRINT_FP,ERR_R_BUF_LIB);
83 return(0);
84 }
85 BIO_set_fp(b,fp,BIO_NOCLOSE);
86 ret=X509_print(b, x);
87 BIO_free(b);
88 return(ret);
89 }
90#endif
91
92int X509_print(bp,x)
93BIO *bp;
94X509 *x;
95 {
96 long l;
97 int ret=0,i,j,n;
98 char *m=NULL,*s;
99 X509_CINF *ci;
100 ASN1_INTEGER *bs;
101 EVP_PKEY *pkey=NULL;
102 char *neg;
103 X509_EXTENSION *ex;
104 ASN1_STRING *str=NULL;
105
106 ci=x->cert_info;
107 if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err;
108 if (BIO_write(bp," Data:\n",10) <= 0) goto err;
109 l=X509_get_version(x);
110 if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err;
111 if (BIO_write(bp," Serial Number:",22) <= 0) goto err;
112
113 bs=X509_get_serialNumber(x);
114 if (bs->length <= 4)
115 {
116 l=ASN1_INTEGER_get(bs);
117 if (l < 0)
118 {
119 l= -l;
120 neg="-";
121 }
122 else
123 neg="";
124 if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
125 goto err;
126 }
127 else
128 {
129 neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
130 if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err;
131
132 for (i=0; i<bs->length; i++)
133 {
134 if (BIO_printf(bp,"%02x%c",bs->data[i],
135 ((i+1 == bs->length)?'\n':':')) <= 0)
136 goto err;
137 }
138 }
139
140 i=OBJ_obj2nid(ci->signature->algorithm);
141 if (BIO_printf(bp,"%8sSignature Algorithm: %s\n","",
142 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)
143 goto err;
144
145 if (BIO_write(bp," Issuer: ",16) <= 0) goto err;
146 if (!X509_NAME_print(bp,X509_get_issuer_name(x),16)) goto err;
147 if (BIO_write(bp,"\n Validity\n",18) <= 0) goto err;
148 if (BIO_write(bp," Not Before: ",24) <= 0) goto err;
149 if (!ASN1_UTCTIME_print(bp,X509_get_notBefore(x))) goto err;
150 if (BIO_write(bp,"\n Not After : ",25) <= 0) goto err;
151 if (!ASN1_UTCTIME_print(bp,X509_get_notAfter(x))) goto err;
152 if (BIO_write(bp,"\n Subject: ",18) <= 0) goto err;
153 if (!X509_NAME_print(bp,X509_get_subject_name(x),16)) goto err;
154 if (BIO_write(bp,"\n Subject Public Key Info:\n",34) <= 0)
155 goto err;
156 i=OBJ_obj2nid(ci->key->algor->algorithm);
157 if (BIO_printf(bp,"%12sPublic Key Algorithm: %s\n","",
158 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
159
160 pkey=X509_get_pubkey(x);
161#ifndef NO_RSA
162 if (pkey->type == EVP_PKEY_RSA)
163 {
164 BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
165 BN_num_bits(pkey->pkey.rsa->n));
166 RSA_print(bp,pkey->pkey.rsa,16);
167 }
168 else
169#endif
170#ifndef NO_DSA
171 if (pkey->type == EVP_PKEY_DSA)
172 {
173 BIO_printf(bp,"%12sDSA Public Key:\n","");
174 DSA_print(bp,pkey->pkey.dsa,16);
175 }
176 else
177#endif
178 BIO_printf(bp,"%12sDSA Public Key:\n","");
179
180 n=X509_get_ext_count(x);
181 if (n > 0)
182 {
183 BIO_printf(bp,"%8sX509v3 extensions:\n","");
184 for (i=0; i<n; i++)
185 {
186 int data_type,pack_type;
187 ASN1_OBJECT *obj;
188
189 ex=X509_get_ext(x,i);
190 if (BIO_printf(bp,"%12s","") <= 0) goto err;
191 obj=X509_EXTENSION_get_object(ex);
192 i2a_ASN1_OBJECT(bp,obj);
193 j=X509_EXTENSION_get_critical(ex);
194 if (BIO_printf(bp,": %s\n%16s",j?"critical":"","") <= 0)
195 goto err;
196
197 pack_type=X509v3_pack_type_by_OBJ(obj);
198 data_type=X509v3_data_type_by_OBJ(obj);
199
200 if (pack_type == X509_EXT_PACK_STRING)
201 {
202 if (X509v3_unpack_string(
203 &str,data_type,
204 X509_EXTENSION_get_data(ex)) == NULL)
205 {
206 /* hmm... */
207 goto err;
208 }
209 if ( (data_type == V_ASN1_IA5STRING) ||
210 (data_type == V_ASN1_PRINTABLESTRING) ||
211 (data_type == V_ASN1_T61STRING))
212 {
213 if (BIO_write(bp,(char *)str->data,
214 str->length) <= 0)
215 goto err;
216 }
217 else if (data_type == V_ASN1_BIT_STRING)
218 {
219 BIO_printf(bp,"0x");
220 for (j=0; j<str->length; j++)
221 {
222 BIO_printf(bp,"%02X",
223 str->data[j]);
224 }
225 }
226 }
227 else
228 {
229 ASN1_OCTET_STRING_print(bp,ex->value);
230 }
231 if (BIO_write(bp,"\n",1) <= 0) goto err;
232 }
233 }
234
235 i=OBJ_obj2nid(x->sig_alg->algorithm);
236 if (BIO_printf(bp,"%4sSignature Algorithm: %s","",
237 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
238
239 n=x->signature->length;
240 s=(char *)x->signature->data;
241 for (i=0; i<n; i++)
242 {
243 if ((i%18) == 0)
244 if (BIO_write(bp,"\n ",9) <= 0) goto err;
245 if (BIO_printf(bp,"%02x%s",(unsigned char)s[i],
246 ((i+1) == n)?"":":") <= 0) goto err;
247 }
248 if (BIO_write(bp,"\n",1) != 1) goto err;
249 ret=1;
250err:
251 if (str != NULL) ASN1_STRING_free(str);
252 if (m != NULL) Free((char *)m);
253 return(ret);
254 }
255
256int ASN1_STRING_print(bp,v)
257BIO *bp;
258ASN1_STRING *v;
259 {
260 int i,n;
261 char buf[80],*p;;
262
263 if (v == NULL) return(0);
264 n=0;
265 p=(char *)v->data;
266 for (i=0; i<v->length; i++)
267 {
268 if ((p[i] > '~') || ((p[i] < ' ') &&
269 (p[i] != '\n') && (p[i] != '\r')))
270 buf[n]='.';
271 else
272 buf[n]=p[i];
273 n++;
274 if (n >= 80)
275 {
276 if (BIO_write(bp,buf,n) <= 0)
277 return(0);
278 n=0;
279 }
280 }
281 if (n > 0)
282 if (BIO_write(bp,buf,n) <= 0)
283 return(0);
284 return(1);
285 }
286
287int ASN1_UTCTIME_print(bp,tm)
288BIO *bp;
289ASN1_UTCTIME *tm;
290 {
291 char *v;
292 int gmt=0;
293 static char *mon[12]={
294 "Jan","Feb","Mar","Apr","May","Jun",
295 "Jul","Aug","Sep","Oct","Nov","Dec"};
296 int i;
297 int y=0,M=0,d=0,h=0,m=0,s=0;
298
299 i=tm->length;
300 v=(char *)tm->data;
301
302 if (i < 10) goto err;
303 if (v[i-1] == 'Z') gmt=1;
304 for (i=0; i<10; i++)
305 if ((v[i] > '9') || (v[i] < '0')) goto err;
306 y= (v[0]-'0')*10+(v[1]-'0');
307 if (y < 50) y+=100;
308 M= (v[2]-'0')*10+(v[3]-'0');
309 if ((M > 12) || (M < 1)) goto err;
310 d= (v[4]-'0')*10+(v[5]-'0');
311 h= (v[6]-'0')*10+(v[7]-'0');
312 m= (v[8]-'0')*10+(v[9]-'0');
313 if ( (v[10] >= '0') && (v[10] <= '9') &&
314 (v[11] >= '0') && (v[11] <= '9'))
315 s= (v[10]-'0')*10+(v[11]-'0');
316
317 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
318 mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)
319 return(0);
320 else
321 return(1);
322err:
323 BIO_write(bp,"Bad time value",14);
324 return(0);
325 }
326
327int X509_NAME_print(bp,name,obase)
328BIO *bp;
329X509_NAME *name;
330int obase;
331 {
332 char *s,*c;
333 int ret=0,l,ll,i,first=1;
334 char buf[256];
335
336 ll=80-2-obase;
337
338 s=X509_NAME_oneline(name,buf,256);
339 s++; /* skip the first slash */
340
341 l=ll;
342 c=s;
343 for (;;)
344 {
345 if ( ((*s == '/') &&
346 ((s[1] >= 'A') && (s[1] <= 'Z') && (
347 (s[2] == '=') ||
348 ((s[2] >= 'A') && (s[2] <= 'Z') &&
349 (s[3] == '='))
350 ))) ||
351 (*s == '\0'))
352 {
353 if ((l <= 0) && !first)
354 {
355 first=0;
356 if (BIO_write(bp,"\n",1) != 1) goto err;
357 for (i=0; i<obase; i++)
358 {
359 if (BIO_write(bp," ",1) != 1) goto err;
360 }
361 l=ll;
362 }
363 i=s-c;
364 if (BIO_write(bp,c,i) != i) goto err;
365 c+=i;
366 c++;
367 if (*s != '\0')
368 {
369 if (BIO_write(bp,", ",2) != 2) goto err;
370 }
371 l--;
372 }
373 if (*s == '\0') break;
374 s++;
375 l--;
376 }
377
378 ret=1;
379 if (0)
380 {
381err:
382 X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
383 }
384 return(ret);
385 }
386
diff --git a/src/lib/libcrypto/asn1/x_algor.c b/src/lib/libcrypto/asn1/x_algor.c
new file mode 100644
index 0000000000..0ed2c87b64
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_algor.c
@@ -0,0 +1,126 @@
1/* crypto/asn1/x_algor.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 "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_ALGOR_NEW,ASN1_R_EXPECTING_A_SEQUENCE);
66 * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH);
67 */
68
69int i2d_X509_ALGOR(a,pp)
70X509_ALGOR *a;
71unsigned char **pp;
72 {
73 M_ASN1_I2D_vars(a);
74
75 M_ASN1_I2D_len(a->algorithm,i2d_ASN1_OBJECT);
76 if (a->parameter != NULL)
77 { M_ASN1_I2D_len(a->parameter,i2d_ASN1_TYPE); }
78
79 M_ASN1_I2D_seq_total();
80 M_ASN1_I2D_put(a->algorithm,i2d_ASN1_OBJECT);
81 if (a->parameter != NULL)
82 { M_ASN1_I2D_put(a->parameter,i2d_ASN1_TYPE); }
83
84 M_ASN1_I2D_finish();
85 }
86
87X509_ALGOR *d2i_X509_ALGOR(a,pp,length)
88X509_ALGOR **a;
89unsigned char **pp;
90long length;
91 {
92 M_ASN1_D2I_vars(a,X509_ALGOR *,X509_ALGOR_new);
93
94 M_ASN1_D2I_Init();
95 M_ASN1_D2I_start_sequence();
96 M_ASN1_D2I_get(ret->algorithm,d2i_ASN1_OBJECT);
97 if (!M_ASN1_D2I_end_sequence())
98 { M_ASN1_D2I_get(ret->parameter,d2i_ASN1_TYPE); }
99 else
100 {
101 ASN1_TYPE_free(ret->parameter);
102 ret->parameter=NULL;
103 }
104 M_ASN1_D2I_Finish(a,X509_ALGOR_free,ASN1_F_D2I_X509_ALGOR);
105 }
106
107X509_ALGOR *X509_ALGOR_new()
108 {
109 X509_ALGOR *ret=NULL;
110
111 M_ASN1_New_Malloc(ret,X509_ALGOR);
112 M_ASN1_New(ret->algorithm,ASN1_OBJECT_new);
113 ret->parameter=NULL;
114 return(ret);
115 M_ASN1_New_Error(ASN1_F_X509_ALGOR_NEW);
116 }
117
118void X509_ALGOR_free(a)
119X509_ALGOR *a;
120 {
121 if (a == NULL) return;
122 ASN1_OBJECT_free(a->algorithm);
123 ASN1_TYPE_free(a->parameter);
124 Free((char *)a);
125 }
126
diff --git a/src/lib/libcrypto/asn1/x_attrib.c b/src/lib/libcrypto/asn1/x_attrib.c
new file mode 100644
index 0000000000..e52ced8627
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_attrib.c
@@ -0,0 +1,152 @@
1/* crypto/asn1/x_attrib.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 "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_ATTRIBUTE,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_ATTRIBUTE_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
67 * ASN1err(ASN1_F_I2D_X509_ATTRIBUTE,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
68 */
69
70/* sequence */
71int i2d_X509_ATTRIBUTE(a,pp)
72X509_ATTRIBUTE *a;
73unsigned char **pp;
74 {
75 int k=0;
76 int r=0,ret=0;
77 unsigned char **p=NULL;
78
79 if (a == NULL) return(0);
80
81 p=NULL;
82 for (;;)
83 {
84 if (k)
85 {
86 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
87 if (pp == NULL) return(r);
88 p=pp;
89 ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE,
90 V_ASN1_UNIVERSAL);
91 }
92
93 ret+=i2d_ASN1_OBJECT(a->object,p);
94 if (a->set)
95 ret+=i2d_ASN1_SET(a->value.set,p,i2d_ASN1_TYPE,
96 V_ASN1_SET,V_ASN1_UNIVERSAL);
97 else
98 ret+=i2d_ASN1_TYPE(a->value.single,p);
99 if (k++) return(r);
100 }
101 }
102
103X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(a,pp,length)
104X509_ATTRIBUTE **a;
105unsigned char **pp;
106long length;
107 {
108 M_ASN1_D2I_vars(a,X509_ATTRIBUTE *,X509_ATTRIBUTE_new);
109
110 M_ASN1_D2I_Init();
111 M_ASN1_D2I_start_sequence();
112 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
113
114 if ((c.slen != 0) &&
115 (M_ASN1_next == (V_ASN1_CONSTRUCTED|V_ASN1_UNIVERSAL|V_ASN1_SET)))
116 {
117 ret->set=1;
118 M_ASN1_D2I_get_set(ret->value.set,d2i_ASN1_TYPE);
119 }
120 else
121 {
122 ret->set=0;
123 M_ASN1_D2I_get(ret->value.single,d2i_ASN1_TYPE);
124 }
125
126 M_ASN1_D2I_Finish(a,X509_ATTRIBUTE_free,ASN1_F_D2I_X509_ATTRIBUTE);
127 }
128
129X509_ATTRIBUTE *X509_ATTRIBUTE_new()
130 {
131 X509_ATTRIBUTE *ret=NULL;
132
133 M_ASN1_New_Malloc(ret,X509_ATTRIBUTE);
134 M_ASN1_New(ret->object,ASN1_OBJECT_new);
135 ret->set=0;
136 ret->value.ptr=NULL;
137 return(ret);
138 M_ASN1_New_Error(ASN1_F_X509_ATTRIBUTE_NEW);
139 }
140
141void X509_ATTRIBUTE_free(a)
142X509_ATTRIBUTE *a;
143 {
144 if (a == NULL) return;
145 ASN1_OBJECT_free(a->object);
146 if (a->set)
147 sk_pop_free(a->value.set,ASN1_TYPE_free);
148 else
149 ASN1_TYPE_free(a->value.single);
150 Free((char *)a);
151 }
152
diff --git a/src/lib/libcrypto/asn1/x_crl.c b/src/lib/libcrypto/asn1/x_crl.c
new file mode 100644
index 0000000000..13acdab427
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_crl.c
@@ -0,0 +1,353 @@
1/* crypto/asn1/x_crl.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 "asn1_mac.h"
62#include "x509.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_CRL,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_D2I_X509_CRL_INFO,ASN1_R_EXPECTING_A_SEQUENCE);
67 * ASN1err(ASN1_F_D2I_X509_REVOKED,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_CRL_NEW,ASN1_R_LENGTH_MISMATCH);
69 * ASN1err(ASN1_F_X509_CRL_INFO_NEW,ASN1_R_EXPECTING_A_SEQUENCE);
70 * ASN1err(ASN1_F_X509_REVOKED_NEW,ASN1_R_LENGTH_MISMATCH);
71 */
72
73#ifndef NOPROTO
74static int X509_REVOKED_cmp(X509_REVOKED **a,X509_REVOKED **b);
75static int X509_REVOKED_seq_cmp(X509_REVOKED **a,X509_REVOKED **b);
76#else
77static int X509_REVOKED_cmp();
78static int X509_REVOKED_seq_cmp();
79#endif
80
81int i2d_X509_REVOKED(a,pp)
82X509_REVOKED *a;
83unsigned char **pp;
84 {
85 M_ASN1_I2D_vars(a);
86
87 M_ASN1_I2D_len(a->serialNumber,i2d_ASN1_INTEGER);
88 M_ASN1_I2D_len(a->revocationDate,i2d_ASN1_UTCTIME);
89 M_ASN1_I2D_len_SEQ_opt(a->extensions,i2d_X509_EXTENSION);
90
91 M_ASN1_I2D_seq_total();
92
93 M_ASN1_I2D_put(a->serialNumber,i2d_ASN1_INTEGER);
94 M_ASN1_I2D_put(a->revocationDate,i2d_ASN1_UTCTIME);
95 M_ASN1_I2D_put_SEQ_opt(a->extensions,i2d_X509_EXTENSION);
96
97 M_ASN1_I2D_finish();
98 }
99
100X509_REVOKED *d2i_X509_REVOKED(a,pp,length)
101X509_REVOKED **a;
102unsigned char **pp;
103long length;
104 {
105 M_ASN1_D2I_vars(a,X509_REVOKED *,X509_REVOKED_new);
106
107 M_ASN1_D2I_Init();
108 M_ASN1_D2I_start_sequence();
109 M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER);
110 M_ASN1_D2I_get(ret->revocationDate,d2i_ASN1_UTCTIME);
111 M_ASN1_D2I_get_seq_opt(ret->extensions,d2i_X509_EXTENSION);
112 M_ASN1_D2I_Finish(a,X509_REVOKED_free,ASN1_F_D2I_X509_REVOKED);
113 }
114
115int i2d_X509_CRL_INFO(a,pp)
116X509_CRL_INFO *a;
117unsigned char **pp;
118 {
119 int v1=0;
120 long l=0;
121 M_ASN1_I2D_vars(a);
122
123 if (sk_num(a->revoked) != 0)
124 qsort((char *)a->revoked->data,sk_num(a->revoked),
125 sizeof(X509_REVOKED *),(int (*)(P_CC_CC))X509_REVOKED_seq_cmp);
126 if ((a->version != NULL) && ((l=ASN1_INTEGER_get(a->version)) != 0))
127 {
128 M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER);
129 }
130 M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR);
131 M_ASN1_I2D_len(a->issuer,i2d_X509_NAME);
132 M_ASN1_I2D_len(a->lastUpdate,i2d_ASN1_UTCTIME);
133 if (a->nextUpdate != NULL)
134 { M_ASN1_I2D_len(a->nextUpdate,i2d_ASN1_UTCTIME); }
135 M_ASN1_I2D_len_SEQ_opt(a->revoked,i2d_X509_REVOKED);
136 M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0,
137 V_ASN1_SEQUENCE,v1);
138
139 M_ASN1_I2D_seq_total();
140
141 if ((a->version != NULL) && (l != 0))
142 {
143 M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER);
144 }
145 M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR);
146 M_ASN1_I2D_put(a->issuer,i2d_X509_NAME);
147 M_ASN1_I2D_put(a->lastUpdate,i2d_ASN1_UTCTIME);
148 if (a->nextUpdate != NULL)
149 { M_ASN1_I2D_put(a->nextUpdate,i2d_ASN1_UTCTIME); }
150 M_ASN1_I2D_put_SEQ_opt(a->revoked,i2d_X509_REVOKED);
151 M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0,
152 V_ASN1_SEQUENCE,v1);
153
154 M_ASN1_I2D_finish();
155 }
156
157X509_CRL_INFO *d2i_X509_CRL_INFO(a,pp,length)
158X509_CRL_INFO **a;
159unsigned char **pp;
160long length;
161 {
162 int i,ver=0;
163 M_ASN1_D2I_vars(a,X509_CRL_INFO *,X509_CRL_INFO_new);
164
165
166 M_ASN1_D2I_Init();
167 M_ASN1_D2I_start_sequence();
168 M_ASN1_D2I_get_opt(ret->version,d2i_ASN1_INTEGER,V_ASN1_INTEGER);
169 if (ret->version != NULL)
170 ver=ret->version->data[0];
171
172 if ((ver == 0) && (ret->version != NULL))
173 {
174 ASN1_INTEGER_free(ret->version);
175 ret->version=NULL;
176 }
177 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
178 M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME);
179 M_ASN1_D2I_get(ret->lastUpdate,d2i_ASN1_UTCTIME);
180 M_ASN1_D2I_get_opt(ret->nextUpdate,d2i_ASN1_UTCTIME,V_ASN1_UTCTIME);
181 if (ret->revoked != NULL)
182 {
183 while (sk_num(ret->revoked))
184 X509_REVOKED_free((X509_REVOKED *)sk_pop(ret->revoked));
185 }
186 M_ASN1_D2I_get_seq_opt(ret->revoked,d2i_X509_REVOKED);
187
188 if (ret->revoked != NULL)
189 {
190 for (i=0; i<sk_num(ret->revoked); i++)
191 {
192 ((X509_REVOKED *)sk_value(ret->revoked,i))->sequence=i;
193 }
194 }
195
196 if (ver >= 1)
197 {
198 if (ret->extensions != NULL)
199 {
200 while (sk_num(ret->extensions))
201 X509_EXTENSION_free((X509_EXTENSION *)
202 sk_pop(ret->extensions));
203 }
204
205 M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION,
206 0,V_ASN1_SEQUENCE);
207 }
208
209 M_ASN1_D2I_Finish(a,X509_CRL_INFO_free,ASN1_F_D2I_X509_CRL_INFO);
210 }
211
212int i2d_X509_CRL(a,pp)
213X509_CRL *a;
214unsigned char **pp;
215 {
216 M_ASN1_I2D_vars(a);
217
218 M_ASN1_I2D_len(a->crl,i2d_X509_CRL_INFO);
219 M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR);
220 M_ASN1_I2D_len(a->signature,i2d_ASN1_BIT_STRING);
221
222 M_ASN1_I2D_seq_total();
223
224 M_ASN1_I2D_put(a->crl,i2d_X509_CRL_INFO);
225 M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR);
226 M_ASN1_I2D_put(a->signature,i2d_ASN1_BIT_STRING);
227
228 M_ASN1_I2D_finish();
229 }
230
231X509_CRL *d2i_X509_CRL(a,pp,length)
232X509_CRL **a;
233unsigned char **pp;
234long length;
235 {
236 M_ASN1_D2I_vars(a,X509_CRL *,X509_CRL_new);
237
238 M_ASN1_D2I_Init();
239 M_ASN1_D2I_start_sequence();
240 M_ASN1_D2I_get(ret->crl,d2i_X509_CRL_INFO);
241 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
242 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
243
244 M_ASN1_D2I_Finish(a,X509_CRL_free,ASN1_F_D2I_X509_CRL);
245 }
246
247
248X509_REVOKED *X509_REVOKED_new()
249 {
250 X509_REVOKED *ret=NULL;
251
252 M_ASN1_New_Malloc(ret,X509_REVOKED);
253 M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);
254 M_ASN1_New(ret->revocationDate,ASN1_UTCTIME_new);
255 ret->extensions=NULL;
256 return(ret);
257 M_ASN1_New_Error(ASN1_F_X509_REVOKED_NEW);
258 }
259
260X509_CRL_INFO *X509_CRL_INFO_new()
261 {
262 X509_CRL_INFO *ret=NULL;
263
264 M_ASN1_New_Malloc(ret,X509_CRL_INFO);
265 ret->version=NULL;
266 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
267 M_ASN1_New(ret->issuer,X509_NAME_new);
268 M_ASN1_New(ret->lastUpdate,ASN1_UTCTIME_new);
269 ret->nextUpdate=NULL;
270 M_ASN1_New(ret->revoked,sk_new_null);
271 M_ASN1_New(ret->extensions,sk_new_null);
272 ret->revoked->comp=(int (*)())X509_REVOKED_cmp;
273 return(ret);
274 M_ASN1_New_Error(ASN1_F_X509_CRL_INFO_NEW);
275 }
276
277X509_CRL *X509_CRL_new()
278 {
279 X509_CRL *ret=NULL;
280
281 M_ASN1_New_Malloc(ret,X509_CRL);
282 ret->references=1;
283 M_ASN1_New(ret->crl,X509_CRL_INFO_new);
284 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
285 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
286 return(ret);
287 M_ASN1_New_Error(ASN1_F_X509_CRL_NEW);
288 }
289
290void X509_REVOKED_free(a)
291X509_REVOKED *a;
292 {
293 if (a == NULL) return;
294 ASN1_INTEGER_free(a->serialNumber);
295 ASN1_UTCTIME_free(a->revocationDate);
296 sk_pop_free(a->extensions,X509_EXTENSION_free);
297 Free((char *)a);
298 }
299
300void X509_CRL_INFO_free(a)
301X509_CRL_INFO *a;
302 {
303 if (a == NULL) return;
304 ASN1_INTEGER_free(a->version);
305 X509_ALGOR_free(a->sig_alg);
306 X509_NAME_free(a->issuer);
307 ASN1_UTCTIME_free(a->lastUpdate);
308 if (a->nextUpdate)
309 ASN1_UTCTIME_free(a->nextUpdate);
310 sk_pop_free(a->revoked,X509_REVOKED_free);
311 sk_pop_free(a->extensions,X509_EXTENSION_free);
312 Free((char *)a);
313 }
314
315void X509_CRL_free(a)
316X509_CRL *a;
317 {
318 int i;
319
320 if (a == NULL) return;
321
322 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_CRL);
323#ifdef REF_PRINT
324 REF_PRINT("X509_CRL",a);
325#endif
326 if (i > 0) return;
327#ifdef REF_CHECK
328 if (i < 0)
329 {
330 fprintf(stderr,"X509_CRL_free, bad reference count\n");
331 abort();
332 }
333#endif
334
335 X509_CRL_INFO_free(a->crl);
336 X509_ALGOR_free(a->sig_alg);
337 ASN1_BIT_STRING_free(a->signature);
338 Free((char *)a);
339 }
340
341static int X509_REVOKED_cmp(a,b)
342X509_REVOKED **a,**b;
343 {
344 return(ASN1_STRING_cmp(
345 (ASN1_STRING *)(*a)->serialNumber,
346 (ASN1_STRING *)(*b)->serialNumber));
347 }
348
349static int X509_REVOKED_seq_cmp(a,b)
350X509_REVOKED **a,**b;
351 {
352 return((*a)->sequence-(*b)->sequence);
353 }
diff --git a/src/lib/libcrypto/asn1/x_exten.c b/src/lib/libcrypto/asn1/x_exten.c
new file mode 100644
index 0000000000..54ffe2f00b
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_exten.c
@@ -0,0 +1,156 @@
1/* crypto/asn1/x_exten.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 "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_EXTENSION,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_EXTENSION_NEW,ASN1_R_LENGTH_MISMATCH);
67 */
68
69int i2d_X509_EXTENSION(a,pp)
70X509_EXTENSION *a;
71unsigned char **pp;
72 {
73 int k=0;
74 int r=0,ret=0;
75 unsigned char **p=NULL;
76
77 if (a == NULL) return(0);
78
79 p=NULL;
80 for (;;)
81 {
82 if (k)
83 {
84 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
85 if (pp == NULL) return(r);
86 p=pp;
87 ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE,
88 V_ASN1_UNIVERSAL);
89 }
90
91 ret+=i2d_ASN1_OBJECT(a->object,p);
92 if ((a->critical) || a->netscape_hack)
93 ret+=i2d_ASN1_BOOLEAN(a->critical,p);
94 ret+=i2d_ASN1_OCTET_STRING(a->value,p);
95 if (k++) return(r);
96 }
97 }
98
99X509_EXTENSION *d2i_X509_EXTENSION(a,pp,length)
100X509_EXTENSION **a;
101unsigned char **pp;
102long length;
103 {
104 int i;
105 M_ASN1_D2I_vars(a,X509_EXTENSION *,X509_EXTENSION_new);
106
107 M_ASN1_D2I_Init();
108 M_ASN1_D2I_start_sequence();
109 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
110
111 if ((ret->argp != NULL) && (ret->ex_free != NULL))
112 ret->ex_free(ret);
113 ret->argl=0;
114 ret->argp=NULL;
115 ret->netscape_hack=0;
116 if ((c.slen != 0) &&
117 (M_ASN1_next == (V_ASN1_UNIVERSAL|V_ASN1_BOOLEAN)))
118 {
119 c.q=c.p;
120 if (d2i_ASN1_BOOLEAN(&i,&c.p,c.slen) < 0) goto err;
121 ret->critical=i;
122 c.slen-=(c.p-c.q);
123 if (ret->critical == 0) ret->netscape_hack=1;
124 }
125 M_ASN1_D2I_get(ret->value,d2i_ASN1_OCTET_STRING);
126
127 M_ASN1_D2I_Finish(a,X509_EXTENSION_free,ASN1_F_D2I_X509_EXTENSION);
128 }
129
130X509_EXTENSION *X509_EXTENSION_new()
131 {
132 X509_EXTENSION *ret=NULL;
133
134 M_ASN1_New_Malloc(ret,X509_EXTENSION);
135 M_ASN1_New(ret->object,ASN1_OBJECT_new);
136 M_ASN1_New(ret->value,ASN1_OCTET_STRING_new);
137 ret->critical=0;
138 ret->netscape_hack=0;
139 ret->argl=0L;
140 ret->argp=NULL;
141 ret->ex_free=NULL;
142 return(ret);
143 M_ASN1_New_Error(ASN1_F_X509_EXTENSION_NEW);
144 }
145
146void X509_EXTENSION_free(a)
147X509_EXTENSION *a;
148 {
149 if (a == NULL) return;
150 if ((a->argp != NULL) && (a->ex_free != NULL))
151 a->ex_free(a);
152 ASN1_OBJECT_free(a->object);
153 ASN1_OCTET_STRING_free(a->value);
154 Free((char *)a);
155 }
156
diff --git a/src/lib/libcrypto/asn1/x_info.c b/src/lib/libcrypto/asn1/x_info.c
new file mode 100644
index 0000000000..b55f0ce77a
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_info.c
@@ -0,0 +1,111 @@
1/* crypto/asn1/x_info.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 "evp.h"
62#include "asn1_mac.h"
63#include "x509.h"
64
65X509_INFO *X509_INFO_new()
66 {
67 X509_INFO *ret=NULL;
68
69 ret=(X509_INFO *)Malloc(sizeof(X509_INFO));
70 if (ret == NULL)
71 {
72 ASN1err(ASN1_F_X509_INFO_NEW,ERR_R_MALLOC_FAILURE);
73 return(NULL);
74 }
75
76 ret->enc_cipher.cipher=NULL;
77 ret->enc_len=0;
78 ret->enc_data=NULL;
79
80 ret->references=1;
81 ret->x509=NULL;
82 ret->crl=NULL;
83 ret->x_pkey=NULL;
84 return(ret);
85 }
86
87void X509_INFO_free(x)
88X509_INFO *x;
89 {
90 int i;
91
92 if (x == NULL) return;
93
94 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_INFO);
95#ifdef REF_PRINT
96 REF_PRINT("X509_INFO",x);
97#endif
98 if (i > 0) return;
99#ifdef REF_CHECK
100 if (i < 0)
101 {
102 fprintf(stderr,"X509_INFO_free, bad reference count\n");
103 abort();
104 }
105#endif
106
107 if (x->x509 != NULL) X509_free(x->x509);
108 if (x->crl != NULL) X509_CRL_free(x->crl);
109 if (x->x_pkey != NULL) X509_PKEY_free(x->x_pkey);
110 Free((char *)x);
111 }
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c
new file mode 100644
index 0000000000..28b9c34b58
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_name.c
@@ -0,0 +1,295 @@
1/* crypto/asn1/x_name.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 "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_NAME,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_NAME_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
67 * ASN1err(ASN1_F_D2I_X509_NAME_ENTRY,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_NAME_ENTRY_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
69 */
70
71#ifndef NOPROTO
72static int i2d_X509_NAME_entries(X509_NAME *a);
73#else
74static int i2d_X509_NAME_entries();
75#endif
76
77int i2d_X509_NAME_ENTRY(a,pp)
78X509_NAME_ENTRY *a;
79unsigned char **pp;
80 {
81 M_ASN1_I2D_vars(a);
82
83 M_ASN1_I2D_len(a->object,i2d_ASN1_OBJECT);
84 M_ASN1_I2D_len(a->value,i2d_ASN1_PRINTABLE);
85
86 M_ASN1_I2D_seq_total();
87
88 M_ASN1_I2D_put(a->object,i2d_ASN1_OBJECT);
89 M_ASN1_I2D_put(a->value,i2d_ASN1_PRINTABLE);
90
91 M_ASN1_I2D_finish();
92 }
93
94X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(a,pp,length)
95X509_NAME_ENTRY **a;
96unsigned char **pp;
97long length;
98 {
99 M_ASN1_D2I_vars(a,X509_NAME_ENTRY *,X509_NAME_ENTRY_new);
100
101 M_ASN1_D2I_Init();
102 M_ASN1_D2I_start_sequence();
103 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
104 M_ASN1_D2I_get(ret->value,d2i_ASN1_PRINTABLE);
105 ret->set=0;
106 M_ASN1_D2I_Finish(a,X509_NAME_ENTRY_free,ASN1_F_D2I_X509_NAME_ENTRY);
107 }
108
109int i2d_X509_NAME(a,pp)
110X509_NAME *a;
111unsigned char **pp;
112 {
113 int ret;
114
115 if (a == NULL) return(0);
116 if (a->modified)
117 {
118 ret=i2d_X509_NAME_entries(a);
119 if (ret < 0) return(ret);
120 }
121
122 ret=a->bytes->length;
123 if (pp != NULL)
124 {
125 memcpy(*pp,a->bytes->data,ret);
126 *pp+=ret;
127 }
128 return(ret);
129 }
130
131static int i2d_X509_NAME_entries(a)
132X509_NAME *a;
133 {
134 X509_NAME_ENTRY *ne,*fe=NULL;
135 STACK *sk;
136 BUF_MEM *buf=NULL;
137 int set=0,r,ret=0;
138 int i;
139 unsigned char *p;
140 int size=0;
141
142 sk=a->entries;
143 for (i=0; i<sk_num(sk); i++)
144 {
145 ne=(X509_NAME_ENTRY *)sk_value(sk,i);
146 if (fe == NULL)
147 {
148 fe=ne;
149 size=0;
150 }
151
152 if (ne->set != set)
153 {
154 ret+=ASN1_object_size(1,size,V_ASN1_SET);
155 fe->size=size;
156 fe=ne;
157 size=0;
158 set=ne->set;
159 }
160 size+=i2d_X509_NAME_ENTRY(ne,NULL);
161 }
162
163 ret+=ASN1_object_size(1,size,V_ASN1_SET);
164 if (fe != NULL)
165 fe->size=size;
166
167 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
168
169 buf=a->bytes;
170 if (!BUF_MEM_grow(buf,r)) goto err;
171 p=(unsigned char *)buf->data;
172
173 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
174
175 set= -1;
176 for (i=0; i<sk_num(sk); i++)
177 {
178 ne=(X509_NAME_ENTRY *)sk_value(sk,i);
179 if (set != ne->set)
180 {
181 set=ne->set;
182 ASN1_put_object(&p,1,ne->size,
183 V_ASN1_SET,V_ASN1_UNIVERSAL);
184 }
185 i2d_X509_NAME_ENTRY(ne,&p);
186 }
187 a->modified=0;
188 return(r);
189err:
190 return(-1);
191 }
192
193X509_NAME *d2i_X509_NAME(a,pp,length)
194X509_NAME **a;
195unsigned char **pp;
196long length;
197 {
198 int set=0,i;
199 int idx=0;
200 unsigned char *orig;
201 M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new);
202
203 orig= *pp;
204 if (sk_num(ret->entries) > 0)
205 {
206 while (sk_num(ret->entries) > 0)
207 X509_NAME_ENTRY_free((X509_NAME_ENTRY *)
208 sk_pop(ret->entries));
209 }
210
211 M_ASN1_D2I_Init();
212 M_ASN1_D2I_start_sequence();
213 for (;;)
214 {
215 if (M_ASN1_D2I_end_sequence()) break;
216 M_ASN1_D2I_get_set(ret->entries,d2i_X509_NAME_ENTRY);
217 for (; idx < sk_num(ret->entries); idx++)
218 {
219 ((X509_NAME_ENTRY *)sk_value(ret->entries,idx))->set=
220 set;
221 }
222 set++;
223 }
224
225 i=(int)(c.p-orig);
226 if (!BUF_MEM_grow(ret->bytes,i)) goto err;
227 memcpy(ret->bytes->data,orig,i);
228 ret->bytes->length=i;
229 ret->modified=0;
230
231 M_ASN1_D2I_Finish(a,X509_NAME_free,ASN1_F_D2I_X509_NAME);
232 }
233
234X509_NAME *X509_NAME_new()
235 {
236 X509_NAME *ret=NULL;
237
238 M_ASN1_New_Malloc(ret,X509_NAME);
239 if ((ret->entries=sk_new(NULL)) == NULL) goto err2;
240 M_ASN1_New(ret->bytes,BUF_MEM_new);
241 ret->modified=1;
242 return(ret);
243 M_ASN1_New_Error(ASN1_F_X509_NAME_NEW);
244 }
245
246X509_NAME_ENTRY *X509_NAME_ENTRY_new()
247 {
248 X509_NAME_ENTRY *ret=NULL;
249
250 M_ASN1_New_Malloc(ret,X509_NAME_ENTRY);
251/* M_ASN1_New(ret->object,ASN1_OBJECT_new);*/
252 ret->object=NULL;
253 ret->set=0;
254 M_ASN1_New(ret->value,ASN1_STRING_new);
255 return(ret);
256 M_ASN1_New_Error(ASN1_F_X509_NAME_ENTRY_NEW);
257 }
258
259void X509_NAME_free(a)
260X509_NAME *a;
261 {
262 BUF_MEM_free(a->bytes);
263 sk_pop_free(a->entries,X509_NAME_ENTRY_free);
264 Free((char *)a);
265 }
266
267void X509_NAME_ENTRY_free(a)
268X509_NAME_ENTRY *a;
269 {
270 if (a == NULL) return;
271 ASN1_OBJECT_free(a->object);
272 ASN1_BIT_STRING_free(a->value);
273 Free((char *)a);
274 }
275
276int X509_NAME_set(xn,name)
277X509_NAME **xn;
278X509_NAME *name;
279 {
280 X509_NAME *in;
281
282 if (*xn == NULL) return(0);
283
284 if (*xn != name)
285 {
286 in=X509_NAME_dup(name);
287 if (in != NULL)
288 {
289 X509_NAME_free(*xn);
290 *xn=in;
291 }
292 }
293 return(*xn != NULL);
294 }
295
diff --git a/src/lib/libcrypto/asn1/x_pkey.c b/src/lib/libcrypto/asn1/x_pkey.c
new file mode 100644
index 0000000000..1d4d926129
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_pkey.c
@@ -0,0 +1,156 @@
1/* crypto/asn1/x_pkey.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 "evp.h"
62#include "objects.h"
63#include "asn1_mac.h"
64
65/* ASN1err(ASN1_F_D2I_X509_PKEY,ASN1_R_UNSUPPORTED_CIPHER); */
66/* ASN1err(ASN1_F_X509_PKEY_NEW,ASN1_R_IV_TOO_LARGE); */
67
68/* need to implement */
69int i2d_X509_PKEY(a,pp)
70X509_PKEY *a;
71unsigned char **pp;
72 {
73 return(0);
74 }
75
76X509_PKEY *d2i_X509_PKEY(a,pp,length)
77X509_PKEY **a;
78unsigned char **pp;
79long length;
80 {
81 int i;
82 M_ASN1_D2I_vars(a,X509_PKEY *,X509_PKEY_new);
83
84 M_ASN1_D2I_Init();
85 M_ASN1_D2I_start_sequence();
86 M_ASN1_D2I_get(ret->enc_algor,d2i_X509_ALGOR);
87 M_ASN1_D2I_get(ret->enc_pkey,d2i_ASN1_OCTET_STRING);
88
89 ret->cipher.cipher=EVP_get_cipherbyname(
90 OBJ_nid2ln(OBJ_obj2nid(ret->enc_algor->algorithm)));
91 if (ret->cipher.cipher == NULL)
92 {
93 c.error=ASN1_R_UNSUPPORTED_CIPHER;
94 goto err;
95 }
96 if (ret->enc_algor->parameter->type == V_ASN1_OCTET_STRING)
97 {
98 i=ret->enc_algor->parameter->value.octet_string->length;
99 if (i > EVP_MAX_IV_LENGTH)
100 {
101 c.error=ASN1_R_IV_TOO_LARGE;
102 goto err;
103 }
104 memcpy(ret->cipher.iv,
105 ret->enc_algor->parameter->value.octet_string->data,i);
106 }
107 else
108 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
109 M_ASN1_D2I_Finish(a,X509_PKEY_free,ASN1_F_D2I_X509_PKEY);
110 }
111
112X509_PKEY *X509_PKEY_new()
113 {
114 X509_PKEY *ret=NULL;
115
116 M_ASN1_New_Malloc(ret,X509_PKEY);
117 ret->version=0;
118 M_ASN1_New(ret->enc_algor,X509_ALGOR_new);
119 M_ASN1_New(ret->enc_pkey,ASN1_OCTET_STRING_new);
120 ret->dec_pkey=NULL;
121 ret->key_length=0;
122 ret->key_data=NULL;
123 ret->key_free=0;
124 ret->cipher.cipher=NULL;
125 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
126 ret->references=1;
127 return(ret);
128 M_ASN1_New_Error(ASN1_F_X509_PKEY_NEW);
129 }
130
131void X509_PKEY_free(x)
132X509_PKEY *x;
133 {
134 int i;
135
136 if (x == NULL) return;
137
138 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_PKEY);
139#ifdef REF_PRINT
140 REF_PRINT("X509_PKEY",x);
141#endif
142 if (i > 0) return;
143#ifdef REF_CHECK
144 if (i < 0)
145 {
146 fprintf(stderr,"X509_PKEY_free, bad reference count\n");
147 abort();
148 }
149#endif
150
151 if (x->enc_algor != NULL) X509_ALGOR_free(x->enc_algor);
152 if (x->enc_pkey != NULL) ASN1_OCTET_STRING_free(x->enc_pkey);
153 if (x->dec_pkey != NULL)EVP_PKEY_free(x->dec_pkey);
154 if ((x->key_data != NULL) && (x->key_free)) Free((char *)x->key_data);
155 Free((char *)(char *)x);
156 }
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
new file mode 100644
index 0000000000..a309cf74a7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -0,0 +1,256 @@
1/* crypto/asn1/x_pubkey.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 "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_PUBKEY,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_PUBKEY_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_PUBKEY(a,pp)
69X509_PUBKEY *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
75 M_ASN1_I2D_len(a->public_key, i2d_ASN1_BIT_STRING);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
80 M_ASN1_I2D_put(a->public_key, i2d_ASN1_BIT_STRING);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_PUBKEY *d2i_X509_PUBKEY(a,pp,length)
86X509_PUBKEY **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_PUBKEY *,X509_PUBKEY_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
95 M_ASN1_D2I_get(ret->public_key,d2i_ASN1_BIT_STRING);
96 if (ret->pkey != NULL)
97 {
98 EVP_PKEY_free(ret->pkey);
99 ret->pkey=NULL;
100 }
101 M_ASN1_D2I_Finish(a,X509_PUBKEY_free,ASN1_F_D2I_X509_PUBKEY);
102 }
103
104X509_PUBKEY *X509_PUBKEY_new()
105 {
106 X509_PUBKEY *ret=NULL;
107
108 M_ASN1_New_Malloc(ret,X509_PUBKEY);
109 M_ASN1_New(ret->algor,X509_ALGOR_new);
110 M_ASN1_New(ret->public_key,ASN1_BIT_STRING_new);
111 ret->pkey=NULL;
112 return(ret);
113 M_ASN1_New_Error(ASN1_F_X509_PUBKEY_NEW);
114 }
115
116void X509_PUBKEY_free(a)
117X509_PUBKEY *a;
118 {
119 if (a == NULL) return;
120 X509_ALGOR_free(a->algor);
121 ASN1_BIT_STRING_free(a->public_key);
122 if (a->pkey != NULL) EVP_PKEY_free(a->pkey);
123 Free((char *)a);
124 }
125
126int X509_PUBKEY_set(x,pkey)
127X509_PUBKEY **x;
128EVP_PKEY *pkey;
129 {
130 int ok=0;
131 X509_PUBKEY *pk;
132 X509_ALGOR *a;
133 ASN1_OBJECT *o;
134 unsigned char *s,*p;
135 int i;
136
137 if (x == NULL) return(0);
138
139 if ((pk=X509_PUBKEY_new()) == NULL) goto err;
140 a=pk->algor;
141
142 /* set the algorithm id */
143 if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
144 ASN1_OBJECT_free(a->algorithm);
145 a->algorithm=o;
146
147 /* Set the parameter list */
148 if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA))
149 {
150 if ((a->parameter == NULL) ||
151 (a->parameter->type != V_ASN1_NULL))
152 {
153 ASN1_TYPE_free(a->parameter);
154 a->parameter=ASN1_TYPE_new();
155 a->parameter->type=V_ASN1_NULL;
156 }
157 }
158 else
159#ifndef NO_DSA
160 if (pkey->type == EVP_PKEY_DSA)
161 {
162 unsigned char *pp;
163 DSA *dsa;
164
165 dsa=pkey->pkey.dsa;
166 dsa->write_params=0;
167 ASN1_TYPE_free(a->parameter);
168 i=i2d_DSAparams(dsa,NULL);
169 p=(unsigned char *)Malloc(i);
170 pp=p;
171 i2d_DSAparams(dsa,&pp);
172 a->parameter=ASN1_TYPE_new();
173 a->parameter->type=V_ASN1_SEQUENCE;
174 a->parameter->value.sequence=ASN1_STRING_new();
175 ASN1_STRING_set(a->parameter->value.sequence,p,i);
176 Free(p);
177 }
178 else
179#endif
180 {
181 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
182 goto err;
183 }
184
185 i=i2d_PublicKey(pkey,NULL);
186 if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err;
187 p=s;
188 i2d_PublicKey(pkey,&p);
189 if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
190 Free(s);
191
192 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
193 pk->pkey=pkey;
194
195 if (*x != NULL)
196 X509_PUBKEY_free(*x);
197
198 *x=pk;
199 pk=NULL;
200
201 ok=1;
202err:
203 if (pk != NULL) X509_PUBKEY_free(pk);
204 return(ok);
205 }
206
207EVP_PKEY *X509_PUBKEY_get(key)
208X509_PUBKEY *key;
209 {
210 EVP_PKEY *ret=NULL;
211 long j;
212 int type;
213 unsigned char *p;
214#ifndef NO_DSA
215 X509_ALGOR *a;
216#endif
217
218 if (key == NULL) goto err;
219
220 if (key->pkey != NULL) return(key->pkey);
221
222 if (key->public_key == NULL) goto err;
223
224 type=OBJ_obj2nid(key->algor->algorithm);
225 p=key->public_key->data;
226 j=key->public_key->length;
227 if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)
228 {
229 X509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);
230 goto err;
231 }
232 ret->save_parameters=0;
233
234#ifndef NO_DSA
235 a=key->algor;
236 if (ret->type == EVP_PKEY_DSA)
237 {
238 if (a->parameter->type == V_ASN1_SEQUENCE)
239 {
240 ret->pkey.dsa->write_params=0;
241 p=a->parameter->value.sequence->data;
242 j=a->parameter->value.sequence->length;
243 if (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j))
244 goto err;
245 }
246 ret->save_parameters=1;
247 }
248#endif
249 key->pkey=ret;
250 return(ret);
251err:
252 if (ret != NULL)
253 EVP_PKEY_free(ret);
254 return(NULL);
255 }
256
diff --git a/src/lib/libcrypto/asn1/x_req.c b/src/lib/libcrypto/asn1/x_req.c
new file mode 100644
index 0000000000..ff0be13d37
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_req.c
@@ -0,0 +1,247 @@
1/* crypto/asn1/x_req.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 "asn1_mac.h"
62#include "x509.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_REQ,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_D2I_X509_REQ_INFO,ASN1_R_LENGTH_MISMATCH);
67 * ASN1err(ASN1_F_X509_REQ_NEW,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_REQ_INFO_NEW,ASN1_R_LENGTH_MISMATCH);
69 */
70
71int i2d_X509_REQ_INFO(a,pp)
72X509_REQ_INFO *a;
73unsigned char **pp;
74 {
75 M_ASN1_I2D_vars(a);
76
77 M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER);
78 M_ASN1_I2D_len(a->subject, i2d_X509_NAME);
79 M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY);
80
81 /* this is a *nasty* hack reported to be required to
82 * allow some CA Software to accept the cert request.
83 * It is not following the PKCS standards ...
84 * PKCS#10 pg 5
85 * attributes [0] IMPLICIT Attibutes
86 * NOTE: no OPTIONAL ... so it *must* be there
87 */
88 if (a->req_kludge)
89 {
90 M_ASN1_I2D_len_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0);
91 }
92 else
93 {
94 M_ASN1_I2D_len_IMP_set(a->attributes, i2d_X509_ATTRIBUTE,0);
95 }
96
97 M_ASN1_I2D_seq_total();
98 M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER);
99 M_ASN1_I2D_put(a->subject, i2d_X509_NAME);
100 M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY);
101
102 /* this is a *nasty* hack reported to be required by some CA's.
103 * It is not following the PKCS standards ...
104 * PKCS#10 pg 5
105 * attributes [0] IMPLICIT Attibutes
106 * NOTE: no OPTIONAL ... so it *must* be there
107 */
108 if (a->req_kludge)
109 {
110 M_ASN1_I2D_put_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0);
111 }
112 else
113 {
114 M_ASN1_I2D_put_IMP_set(a->attributes,i2d_X509_ATTRIBUTE,0);
115 }
116
117 M_ASN1_I2D_finish();
118 }
119
120X509_REQ_INFO *d2i_X509_REQ_INFO(a,pp,length)
121X509_REQ_INFO **a;
122unsigned char **pp;
123long length;
124 {
125 M_ASN1_D2I_vars(a,X509_REQ_INFO *,X509_REQ_INFO_new);
126
127 M_ASN1_D2I_Init();
128 M_ASN1_D2I_start_sequence();
129 M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
130 M_ASN1_D2I_get(ret->subject,d2i_X509_NAME);
131 M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY);
132
133 /* this is a *nasty* hack to allow for some CA's that
134 * have been reported as requiring it.
135 * It is not following the PKCS standards ...
136 * PKCS#10 pg 5
137 * attributes [0] IMPLICIT Attibutes
138 * NOTE: no OPTIONAL ... so it *must* be there
139 */
140 if (asn1_Finish(&c))
141 ret->req_kludge=1;
142 else
143 {
144 M_ASN1_D2I_get_IMP_set(ret->attributes,d2i_X509_ATTRIBUTE,0);
145 }
146
147 M_ASN1_D2I_Finish(a,X509_REQ_INFO_free,ASN1_F_D2I_X509_REQ_INFO);
148 }
149
150X509_REQ_INFO *X509_REQ_INFO_new()
151 {
152 X509_REQ_INFO *ret=NULL;
153
154 M_ASN1_New_Malloc(ret,X509_REQ_INFO);
155 M_ASN1_New(ret->version,ASN1_INTEGER_new);
156 M_ASN1_New(ret->subject,X509_NAME_new);
157 M_ASN1_New(ret->pubkey,X509_PUBKEY_new);
158 M_ASN1_New(ret->attributes,sk_new_null);
159 ret->req_kludge=0;
160 return(ret);
161 M_ASN1_New_Error(ASN1_F_X509_REQ_INFO_NEW);
162 }
163
164void X509_REQ_INFO_free(a)
165X509_REQ_INFO *a;
166 {
167 if (a == NULL) return;
168 ASN1_INTEGER_free(a->version);
169 X509_NAME_free(a->subject);
170 X509_PUBKEY_free(a->pubkey);
171 sk_pop_free(a->attributes,X509_ATTRIBUTE_free);
172 Free((char *)a);
173 }
174
175int i2d_X509_REQ(a,pp)
176X509_REQ *a;
177unsigned char **pp;
178 {
179 M_ASN1_I2D_vars(a);
180 M_ASN1_I2D_len(a->req_info, i2d_X509_REQ_INFO);
181 M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR);
182 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
183
184 M_ASN1_I2D_seq_total();
185
186 M_ASN1_I2D_put(a->req_info, i2d_X509_REQ_INFO);
187 M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR);
188 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
189
190 M_ASN1_I2D_finish();
191 }
192
193X509_REQ *d2i_X509_REQ(a,pp,length)
194X509_REQ **a;
195unsigned char **pp;
196long length;
197 {
198 M_ASN1_D2I_vars(a,X509_REQ *,X509_REQ_new);
199
200 M_ASN1_D2I_Init();
201 M_ASN1_D2I_start_sequence();
202 M_ASN1_D2I_get(ret->req_info,d2i_X509_REQ_INFO);
203 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
204 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
205 M_ASN1_D2I_Finish(a,X509_REQ_free,ASN1_F_D2I_X509_REQ);
206 }
207
208X509_REQ *X509_REQ_new()
209 {
210 X509_REQ *ret=NULL;
211
212 M_ASN1_New_Malloc(ret,X509_REQ);
213 ret->references=1;
214 M_ASN1_New(ret->req_info,X509_REQ_INFO_new);
215 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
216 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
217 return(ret);
218 M_ASN1_New_Error(ASN1_F_X509_REQ_NEW);
219 }
220
221void X509_REQ_free(a)
222X509_REQ *a;
223 {
224 int i;
225
226 if (a == NULL) return;
227
228 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_REQ);
229#ifdef REF_PRINT
230 REF_PRINT("X509_REQ",a);
231#endif
232 if (i > 0) return;
233#ifdef REF_CHECK
234 if (i < 0)
235 {
236 fprintf(stderr,"X509_REQ_free, bad reference count\n");
237 abort();
238 }
239#endif
240
241 X509_REQ_INFO_free(a->req_info);
242 X509_ALGOR_free(a->sig_alg);
243 ASN1_BIT_STRING_free(a->signature);
244 Free((char *)a);
245 }
246
247
diff --git a/src/lib/libcrypto/asn1/x_sig.c b/src/lib/libcrypto/asn1/x_sig.c
new file mode 100644
index 0000000000..f0a2e4c27a
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_sig.c
@@ -0,0 +1,119 @@
1/* crypto/asn1/x_sig.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 "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_SIG,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_SIG_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_SIG(a,pp)
69X509_SIG *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
75 M_ASN1_I2D_len(a->digest, i2d_ASN1_OCTET_STRING);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
80 M_ASN1_I2D_put(a->digest, i2d_ASN1_OCTET_STRING);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_SIG *d2i_X509_SIG(a,pp,length)
86X509_SIG **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_SIG *,X509_SIG_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
95 M_ASN1_D2I_get(ret->digest,d2i_ASN1_OCTET_STRING);
96 M_ASN1_D2I_Finish(a,X509_SIG_free,ASN1_F_D2I_X509_SIG);
97 }
98
99X509_SIG *X509_SIG_new()
100 {
101 X509_SIG *ret=NULL;
102
103 M_ASN1_New_Malloc(ret,X509_SIG);
104 M_ASN1_New(ret->algor,X509_ALGOR_new);
105 M_ASN1_New(ret->digest,ASN1_OCTET_STRING_new);
106 return(ret);
107 M_ASN1_New_Error(ASN1_F_X509_SIG_NEW);
108 }
109
110void X509_SIG_free(a)
111X509_SIG *a;
112 {
113 if (a == NULL) return;
114 X509_ALGOR_free(a->algor);
115 ASN1_OCTET_STRING_free(a->digest);
116 Free((char *)a);
117 }
118
119
diff --git a/src/lib/libcrypto/asn1/x_spki.c b/src/lib/libcrypto/asn1/x_spki.c
new file mode 100644
index 0000000000..4a80df44b8
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_spki.c
@@ -0,0 +1,181 @@
1/* crypto/asn1/x_spki.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 /* This module was send to me my Pat Richards <patr@x509.com> who
60 * wrote it. It is under my Copyright with his permision
61 */
62
63#include <stdio.h>
64#include "cryptlib.h"
65#include "x509.h"
66#include "asn1_mac.h"
67
68/*
69 * ASN1err(ASN1_F_D2I_NETSCAPE_SPKAC,ASN1_R_LENGTH_MISMATCH);
70 * ASN1err(ASN1_F_NETSCAPE_SPKAC_NEW,ASN1_R_LENGTH_MISMATCH);
71 * ASN1err(ASN1_F_D2I_NETSCAPE_SPKI,ASN1_R_LENGTH_MISMATCH);
72 * ASN1err(ASN1_F_NETSCAPE_SPKI_NEW,ASN1_R_LENGTH_MISMATCH);
73 */
74
75int i2d_NETSCAPE_SPKAC(a,pp)
76NETSCAPE_SPKAC *a;
77unsigned char **pp;
78 {
79 M_ASN1_I2D_vars(a);
80
81 M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY);
82 M_ASN1_I2D_len(a->challenge, i2d_ASN1_IA5STRING);
83
84 M_ASN1_I2D_seq_total();
85
86 M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY);
87 M_ASN1_I2D_put(a->challenge, i2d_ASN1_IA5STRING);
88
89 M_ASN1_I2D_finish();
90 }
91
92NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(a,pp,length)
93NETSCAPE_SPKAC **a;
94unsigned char **pp;
95long length;
96 {
97 M_ASN1_D2I_vars(a,NETSCAPE_SPKAC *,NETSCAPE_SPKAC_new);
98
99 M_ASN1_D2I_Init();
100 M_ASN1_D2I_start_sequence();
101 M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY);
102 M_ASN1_D2I_get(ret->challenge,d2i_ASN1_IA5STRING);
103 M_ASN1_D2I_Finish(a,NETSCAPE_SPKAC_free,ASN1_F_D2I_NETSCAPE_SPKAC);
104 }
105
106NETSCAPE_SPKAC *NETSCAPE_SPKAC_new()
107 {
108 NETSCAPE_SPKAC *ret=NULL;
109
110 M_ASN1_New_Malloc(ret,NETSCAPE_SPKAC);
111 M_ASN1_New(ret->pubkey,X509_PUBKEY_new);
112 M_ASN1_New(ret->challenge,ASN1_IA5STRING_new);
113 return(ret);
114 M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKAC_NEW);
115 }
116
117void NETSCAPE_SPKAC_free(a)
118NETSCAPE_SPKAC *a;
119 {
120 if (a == NULL) return;
121 X509_PUBKEY_free(a->pubkey);
122 ASN1_IA5STRING_free(a->challenge);
123 Free((char *)a);
124 }
125
126int i2d_NETSCAPE_SPKI(a,pp)
127NETSCAPE_SPKI *a;
128unsigned char **pp;
129 {
130 M_ASN1_I2D_vars(a);
131
132 M_ASN1_I2D_len(a->spkac, i2d_NETSCAPE_SPKAC);
133 M_ASN1_I2D_len(a->sig_algor, i2d_X509_ALGOR);
134 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
135
136 M_ASN1_I2D_seq_total();
137
138 M_ASN1_I2D_put(a->spkac, i2d_NETSCAPE_SPKAC);
139 M_ASN1_I2D_put(a->sig_algor, i2d_X509_ALGOR);
140 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
141
142 M_ASN1_I2D_finish();
143 }
144
145NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(a,pp,length)
146NETSCAPE_SPKI **a;
147unsigned char **pp;
148long length;
149 {
150 M_ASN1_D2I_vars(a,NETSCAPE_SPKI *,NETSCAPE_SPKI_new);
151
152 M_ASN1_D2I_Init();
153 M_ASN1_D2I_start_sequence();
154 M_ASN1_D2I_get(ret->spkac,d2i_NETSCAPE_SPKAC);
155 M_ASN1_D2I_get(ret->sig_algor,d2i_X509_ALGOR);
156 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
157 M_ASN1_D2I_Finish(a,NETSCAPE_SPKI_free,ASN1_F_D2I_NETSCAPE_SPKI);
158 }
159
160NETSCAPE_SPKI *NETSCAPE_SPKI_new()
161 {
162 NETSCAPE_SPKI *ret=NULL;
163
164 M_ASN1_New_Malloc(ret,NETSCAPE_SPKI);
165 M_ASN1_New(ret->spkac,NETSCAPE_SPKAC_new);
166 M_ASN1_New(ret->sig_algor,X509_ALGOR_new);
167 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
168 return(ret);
169 M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKI_NEW);
170 }
171
172void NETSCAPE_SPKI_free(a)
173NETSCAPE_SPKI *a;
174 {
175 if (a == NULL) return;
176 NETSCAPE_SPKAC_free(a->spkac);
177 X509_ALGOR_free(a->sig_algor);
178 ASN1_BIT_STRING_free(a->signature);
179 Free((char *)a);
180 }
181
diff --git a/src/lib/libcrypto/asn1/x_val.c b/src/lib/libcrypto/asn1/x_val.c
new file mode 100644
index 0000000000..a9c390f88c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_val.c
@@ -0,0 +1,118 @@
1/* crypto/asn1/x_val.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 "asn1_mac.h"
62
63/* ASN1err(ASN1_F_X509_VAL_NEW,ERR_R_MALLOC_FAILURE);
64 * ASN1err(ASN1_F_X509_VAL_FREE,ERR_R_MALLOC_FAILURE);
65 * ASN1err(ASN1_F_D2I_X509_VAL,ERR_R_MALLOC_FAILURE);
66 */
67
68int i2d_X509_VAL(a,pp)
69X509_VAL *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->notBefore,i2d_ASN1_UTCTIME);
75 M_ASN1_I2D_len(a->notAfter,i2d_ASN1_UTCTIME);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->notBefore,i2d_ASN1_UTCTIME);
80 M_ASN1_I2D_put(a->notAfter,i2d_ASN1_UTCTIME);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_VAL *d2i_X509_VAL(a,pp,length)
86X509_VAL **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_VAL *,X509_VAL_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->notBefore,d2i_ASN1_UTCTIME);
95 M_ASN1_D2I_get(ret->notAfter,d2i_ASN1_UTCTIME);
96 M_ASN1_D2I_Finish(a,X509_VAL_free,ASN1_F_D2I_X509_VAL);
97 }
98
99X509_VAL *X509_VAL_new()
100 {
101 X509_VAL *ret=NULL;
102
103 M_ASN1_New_Malloc(ret,X509_VAL);
104 M_ASN1_New(ret->notBefore,ASN1_UTCTIME_new);
105 M_ASN1_New(ret->notAfter,ASN1_UTCTIME_new);
106 return(ret);
107 M_ASN1_New_Error(ASN1_F_X509_VAL_NEW);
108 }
109
110void X509_VAL_free(a)
111X509_VAL *a;
112 {
113 if (a == NULL) return;
114 ASN1_UTCTIME_free(a->notBefore);
115 ASN1_UTCTIME_free(a->notAfter);
116 Free((char *)a);
117 }
118
diff --git a/src/lib/libcrypto/asn1/x_x509.c b/src/lib/libcrypto/asn1/x_x509.c
new file mode 100644
index 0000000000..bc466ce0f6
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_x509.c
@@ -0,0 +1,158 @@
1/* crypto/asn1/x_x509.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 "evp.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_NEW,ASN1_R_BAD_GET_OBJECT);
67 */
68
69static ASN1_METHOD meth={
70 (int (*)()) i2d_X509,
71 (char *(*)())d2i_X509,
72 (char *(*)())X509_new,
73 (void (*)()) X509_free};
74
75ASN1_METHOD *X509_asn1_meth()
76 {
77 return(&meth);
78 }
79
80int i2d_X509(a,pp)
81X509 *a;
82unsigned char **pp;
83 {
84 M_ASN1_I2D_vars(a);
85
86 M_ASN1_I2D_len(a->cert_info, i2d_X509_CINF);
87 M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR);
88 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
89
90 M_ASN1_I2D_seq_total();
91
92 M_ASN1_I2D_put(a->cert_info, i2d_X509_CINF);
93 M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR);
94 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
95
96 M_ASN1_I2D_finish();
97 }
98
99X509 *d2i_X509(a,pp,length)
100X509 **a;
101unsigned char **pp;
102long length;
103 {
104 M_ASN1_D2I_vars(a,X509 *,X509_new);
105
106 M_ASN1_D2I_Init();
107 M_ASN1_D2I_start_sequence();
108 M_ASN1_D2I_get(ret->cert_info,d2i_X509_CINF);
109 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
110 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
111if (ret->name != NULL) Free(ret->name);
112ret->name=X509_NAME_oneline(ret->cert_info->subject,NULL,0);
113
114 M_ASN1_D2I_Finish(a,X509_free,ASN1_F_D2I_X509);
115 }
116
117X509 *X509_new()
118 {
119 X509 *ret=NULL;
120
121 M_ASN1_New_Malloc(ret,X509);
122 ret->references=1;
123 ret->valid=0;
124 ret->name=NULL;
125 M_ASN1_New(ret->cert_info,X509_CINF_new);
126 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
127 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
128 return(ret);
129 M_ASN1_New_Error(ASN1_F_X509_NEW);
130 }
131
132void X509_free(a)
133X509 *a;
134 {
135 int i;
136
137 if (a == NULL) return;
138
139 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509);
140#ifdef REF_PRINT
141 REF_PRINT("X509",a);
142#endif
143 if (i > 0) return;
144#ifdef REF_CHECK
145 if (i < 0)
146 {
147 fprintf(stderr,"X509_free, bad reference count\n");
148 abort();
149 }
150#endif
151
152 X509_CINF_free(a->cert_info);
153 X509_ALGOR_free(a->sig_alg);
154 ASN1_BIT_STRING_free(a->signature);
155 if (a->name != NULL) Free(a->name);
156 Free((char *)a);
157 }
158