summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1')
-rw-r--r--src/lib/libcrypto/asn1/a_bitstr.c248
-rw-r--r--src/lib/libcrypto/asn1/a_bool.c114
-rw-r--r--src/lib/libcrypto/asn1/a_bytes.c314
-rw-r--r--src/lib/libcrypto/asn1/a_d2i_fp.c286
-rw-r--r--src/lib/libcrypto/asn1/a_digest.c111
-rw-r--r--src/lib/libcrypto/asn1/a_dup.c109
-rw-r--r--src/lib/libcrypto/asn1/a_enum.c182
-rw-r--r--src/lib/libcrypto/asn1/a_i2d_fp.c163
-rw-r--r--src/lib/libcrypto/asn1/a_int.c458
-rw-r--r--src/lib/libcrypto/asn1/a_mbstr.c400
-rw-r--r--src/lib/libcrypto/asn1/a_object.c403
-rw-r--r--src/lib/libcrypto/asn1/a_octet.c71
-rw-r--r--src/lib/libcrypto/asn1/a_print.c127
-rw-r--r--src/lib/libcrypto/asn1/a_set.c241
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c298
-rw-r--r--src/lib/libcrypto/asn1/a_strex.c574
-rw-r--r--src/lib/libcrypto/asn1/a_strnid.c290
-rw-r--r--src/lib/libcrypto/asn1/a_time.c198
-rw-r--r--src/lib/libcrypto/asn1/a_type.c159
-rw-r--r--src/lib/libcrypto/asn1/a_utf8.c211
-rw-r--r--src/lib/libcrypto/asn1/a_verify.c197
-rw-r--r--src/lib/libcrypto/asn1/ameth_lib.c450
-rw-r--r--src/lib/libcrypto/asn1/asn1.h1402
-rw-r--r--src/lib/libcrypto/asn1/asn1_err.c329
-rw-r--r--src/lib/libcrypto/asn1/asn1_gen.c854
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c482
-rw-r--r--src/lib/libcrypto/asn1/asn1_locl.h134
-rw-r--r--src/lib/libcrypto/asn1/asn1_mac.h578
-rw-r--r--src/lib/libcrypto/asn1/asn1_par.c437
-rw-r--r--src/lib/libcrypto/asn1/asn1t.h960
-rw-r--r--src/lib/libcrypto/asn1/asn_mime.c942
-rw-r--r--src/lib/libcrypto/asn1/asn_moid.c160
-rw-r--r--src/lib/libcrypto/asn1/asn_pack.c191
-rw-r--r--src/lib/libcrypto/asn1/bio_asn1.c495
-rw-r--r--src/lib/libcrypto/asn1/bio_ndef.c243
-rw-r--r--src/lib/libcrypto/asn1/charmap.h15
-rw-r--r--src/lib/libcrypto/asn1/charmap.pl80
-rw-r--r--src/lib/libcrypto/asn1/d2i_pr.c170
-rw-r--r--src/lib/libcrypto/asn1/d2i_pu.c139
-rw-r--r--src/lib/libcrypto/asn1/evp_asn1.c189
-rw-r--r--src/lib/libcrypto/asn1/f_enum.c207
-rw-r--r--src/lib/libcrypto/asn1/f_int.c219
-rw-r--r--src/lib/libcrypto/asn1/f_string.c212
-rw-r--r--src/lib/libcrypto/asn1/i2d_pr.c80
-rw-r--r--src/lib/libcrypto/asn1/i2d_pu.c95
-rw-r--r--src/lib/libcrypto/asn1/n_pkey.c343
-rw-r--r--src/lib/libcrypto/asn1/nsseq.c83
-rw-r--r--src/lib/libcrypto/asn1/p5_pbe.c148
-rw-r--r--src/lib/libcrypto/asn1/p5_pbev2.c235
-rw-r--r--src/lib/libcrypto/asn1/p8_pkey.c155
-rw-r--r--src/lib/libcrypto/asn1/t_bitst.c102
-rw-r--r--src/lib/libcrypto/asn1/t_crl.c133
-rw-r--r--src/lib/libcrypto/asn1/t_pkey.c114
-rw-r--r--src/lib/libcrypto/asn1/t_req.c266
-rw-r--r--src/lib/libcrypto/asn1/t_spki.c107
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c493
-rw-r--r--src/lib/libcrypto/asn1/t_x509a.c110
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c1347
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c691
-rw-r--r--src/lib/libcrypto/asn1/tasn_fre.c266
-rw-r--r--src/lib/libcrypto/asn1/tasn_new.c396
-rw-r--r--src/lib/libcrypto/asn1/tasn_prn.c627
-rw-r--r--src/lib/libcrypto/asn1/tasn_typ.c148
-rw-r--r--src/lib/libcrypto/asn1/tasn_utl.c279
-rw-r--r--src/lib/libcrypto/asn1/x_algor.c130
-rw-r--r--src/lib/libcrypto/asn1/x_attrib.c118
-rw-r--r--src/lib/libcrypto/asn1/x_bignum.c139
-rw-r--r--src/lib/libcrypto/asn1/x_crl.c527
-rw-r--r--src/lib/libcrypto/asn1/x_exten.c76
-rw-r--r--src/lib/libcrypto/asn1/x_info.c114
-rw-r--r--src/lib/libcrypto/asn1/x_long.c179
-rw-r--r--src/lib/libcrypto/asn1/x_name.c520
-rw-r--r--src/lib/libcrypto/asn1/x_nx509.c72
-rw-r--r--src/lib/libcrypto/asn1/x_pkey.c151
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c373
-rw-r--r--src/lib/libcrypto/asn1/x_req.c113
-rw-r--r--src/lib/libcrypto/asn1/x_sig.c69
-rw-r--r--src/lib/libcrypto/asn1/x_spki.c81
-rw-r--r--src/lib/libcrypto/asn1/x_val.c69
-rw-r--r--src/lib/libcrypto/asn1/x_x509.c194
-rw-r--r--src/lib/libcrypto/asn1/x_x509a.c180
81 files changed, 0 insertions, 23365 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c
deleted file mode 100644
index 34179960b8..0000000000
--- a/src/lib/libcrypto/asn1/a_bitstr.c
+++ /dev/null
@@ -1,248 +0,0 @@
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 <openssl/asn1.h>
62
63int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
64{ return M_ASN1_BIT_STRING_set(x, d, len); }
65
66int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
67 {
68 int ret,j,bits,len;
69 unsigned char *p,*d;
70
71 if (a == NULL) return(0);
72
73 len=a->length;
74
75 if (len > 0)
76 {
77 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT)
78 {
79 bits=(int)a->flags&0x07;
80 }
81 else
82 {
83 for ( ; len > 0; len--)
84 {
85 if (a->data[len-1]) break;
86 }
87 j=a->data[len-1];
88 if (j & 0x01) bits=0;
89 else if (j & 0x02) bits=1;
90 else if (j & 0x04) bits=2;
91 else if (j & 0x08) bits=3;
92 else if (j & 0x10) bits=4;
93 else if (j & 0x20) bits=5;
94 else if (j & 0x40) bits=6;
95 else if (j & 0x80) bits=7;
96 else bits=0; /* should not happen */
97 }
98 }
99 else
100 bits=0;
101
102 ret=1+len;
103 if (pp == NULL) return(ret);
104
105 p= *pp;
106
107 *(p++)=(unsigned char)bits;
108 d=a->data;
109 memcpy(p,d,len);
110 p+=len;
111 if (len > 0) p[-1]&=(0xff<<bits);
112 *pp=p;
113 return(ret);
114 }
115
116ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
117 const unsigned char **pp, long len)
118 {
119 ASN1_BIT_STRING *ret=NULL;
120 const unsigned char *p;
121 unsigned char *s;
122 int i;
123
124 if (len < 1)
125 {
126 i=ASN1_R_STRING_TOO_SHORT;
127 goto err;
128 }
129
130 if ((a == NULL) || ((*a) == NULL))
131 {
132 if ((ret=M_ASN1_BIT_STRING_new()) == NULL) return(NULL);
133 }
134 else
135 ret=(*a);
136
137 p= *pp;
138 i= *(p++);
139 /* We do this to preserve the settings. If we modify
140 * the settings, via the _set_bit function, we will recalculate
141 * on output */
142 ret->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
143 ret->flags|=(ASN1_STRING_FLAG_BITS_LEFT|(i&0x07)); /* set */
144
145 if (len-- > 1) /* using one because of the bits left byte */
146 {
147 s=(unsigned char *)OPENSSL_malloc((int)len);
148 if (s == NULL)
149 {
150 i=ERR_R_MALLOC_FAILURE;
151 goto err;
152 }
153 memcpy(s,p,(int)len);
154 s[len-1]&=(0xff<<i);
155 p+=len;
156 }
157 else
158 s=NULL;
159
160 ret->length=(int)len;
161 if (ret->data != NULL) OPENSSL_free(ret->data);
162 ret->data=s;
163 ret->type=V_ASN1_BIT_STRING;
164 if (a != NULL) (*a)=ret;
165 *pp=p;
166 return(ret);
167err:
168 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING,i);
169 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
170 M_ASN1_BIT_STRING_free(ret);
171 return(NULL);
172 }
173
174/* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
175 */
176int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
177 {
178 int w,v,iv;
179 unsigned char *c;
180
181 w=n/8;
182 v=1<<(7-(n&0x07));
183 iv= ~v;
184 if (!value) v=0;
185
186 if (a == NULL)
187 return 0;
188
189 a->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear, set on write */
190
191 if ((a->length < (w+1)) || (a->data == NULL))
192 {
193 if (!value) return(1); /* Don't need to set */
194 if (a->data == NULL)
195 c=(unsigned char *)OPENSSL_malloc(w+1);
196 else
197 c=(unsigned char *)OPENSSL_realloc_clean(a->data,
198 a->length,
199 w+1);
200 if (c == NULL)
201 {
202 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT,ERR_R_MALLOC_FAILURE);
203 return 0;
204 }
205 if (w+1-a->length > 0) memset(c+a->length, 0, w+1-a->length);
206 a->data=c;
207 a->length=w+1;
208 }
209 a->data[w]=((a->data[w])&iv)|v;
210 while ((a->length > 0) && (a->data[a->length-1] == 0))
211 a->length--;
212 return(1);
213 }
214
215int ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n)
216 {
217 int w,v;
218
219 w=n/8;
220 v=1<<(7-(n&0x07));
221 if ((a == NULL) || (a->length < (w+1)) || (a->data == NULL))
222 return(0);
223 return((a->data[w]&v) != 0);
224 }
225
226/*
227 * Checks if the given bit string contains only bits specified by
228 * the flags vector. Returns 0 if there is at least one bit set in 'a'
229 * which is not specified in 'flags', 1 otherwise.
230 * 'len' is the length of 'flags'.
231 */
232int ASN1_BIT_STRING_check(ASN1_BIT_STRING *a,
233 unsigned char *flags, int flags_len)
234 {
235 int i, ok;
236 /* Check if there is one bit set at all. */
237 if (!a || !a->data) return 1;
238
239 /* Check each byte of the internal representation of the bit string. */
240 ok = 1;
241 for (i = 0; i < a->length && ok; ++i)
242 {
243 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
244 /* We are done if there is an unneeded bit set. */
245 ok = (a->data[i] & mask) == 0;
246 }
247 return ok;
248 }
diff --git a/src/lib/libcrypto/asn1/a_bool.c b/src/lib/libcrypto/asn1/a_bool.c
deleted file mode 100644
index 331acdf053..0000000000
--- a/src/lib/libcrypto/asn1/a_bool.c
+++ /dev/null
@@ -1,114 +0,0 @@
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 <openssl/asn1t.h>
62
63int i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
64 {
65 int r;
66 unsigned char *p;
67
68 r=ASN1_object_size(0,1,V_ASN1_BOOLEAN);
69 if (pp == NULL) return(r);
70 p= *pp;
71
72 ASN1_put_object(&p,0,1,V_ASN1_BOOLEAN,V_ASN1_UNIVERSAL);
73 *(p++)= (unsigned char)a;
74 *pp=p;
75 return(r);
76 }
77
78int d2i_ASN1_BOOLEAN(int *a, const unsigned char **pp, long length)
79 {
80 int ret= -1;
81 const unsigned char *p;
82 long len;
83 int inf,tag,xclass;
84 int i=0;
85
86 p= *pp;
87 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
88 if (inf & 0x80)
89 {
90 i=ASN1_R_BAD_OBJECT_HEADER;
91 goto err;
92 }
93
94 if (tag != V_ASN1_BOOLEAN)
95 {
96 i=ASN1_R_EXPECTING_A_BOOLEAN;
97 goto err;
98 }
99
100 if (len != 1)
101 {
102 i=ASN1_R_BOOLEAN_IS_WRONG_LENGTH;
103 goto err;
104 }
105 ret= (int)*(p++);
106 if (a != NULL) (*a)=ret;
107 *pp=p;
108 return(ret);
109err:
110 ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,i);
111 return(ret);
112 }
113
114
diff --git a/src/lib/libcrypto/asn1/a_bytes.c b/src/lib/libcrypto/asn1/a_bytes.c
deleted file mode 100644
index 92d630cdba..0000000000
--- a/src/lib/libcrypto/asn1/a_bytes.c
+++ /dev/null
@@ -1,314 +0,0 @@
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 <openssl/asn1.h>
62
63static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
64/* type is a 'bitmap' of acceptable string types.
65 */
66ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
67 long length, int type)
68 {
69 ASN1_STRING *ret=NULL;
70 const unsigned char *p;
71 unsigned char *s;
72 long len;
73 int inf,tag,xclass;
74 int i=0;
75
76 p= *pp;
77 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
78 if (inf & 0x80) goto err;
79
80 if (tag >= 32)
81 {
82 i=ASN1_R_TAG_VALUE_TOO_HIGH;
83 goto err;
84 }
85 if (!(ASN1_tag2bit(tag) & type))
86 {
87 i=ASN1_R_WRONG_TYPE;
88 goto err;
89 }
90
91 /* If a bit-string, exit early */
92 if (tag == V_ASN1_BIT_STRING)
93 return(d2i_ASN1_BIT_STRING(a,pp,length));
94
95 if ((a == NULL) || ((*a) == NULL))
96 {
97 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
98 }
99 else
100 ret=(*a);
101
102 if (len != 0)
103 {
104 s=(unsigned char *)OPENSSL_malloc((int)len+1);
105 if (s == NULL)
106 {
107 i=ERR_R_MALLOC_FAILURE;
108 goto err;
109 }
110 memcpy(s,p,(int)len);
111 s[len]='\0';
112 p+=len;
113 }
114 else
115 s=NULL;
116
117 if (ret->data != NULL) OPENSSL_free(ret->data);
118 ret->length=(int)len;
119 ret->data=s;
120 ret->type=tag;
121 if (a != NULL) (*a)=ret;
122 *pp=p;
123 return(ret);
124err:
125 ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i);
126 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
127 ASN1_STRING_free(ret);
128 return(NULL);
129 }
130
131int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
132 {
133 int ret,r,constructed;
134 unsigned char *p;
135
136 if (a == NULL) return(0);
137
138 if (tag == V_ASN1_BIT_STRING)
139 return(i2d_ASN1_BIT_STRING(a,pp));
140
141 ret=a->length;
142 r=ASN1_object_size(0,ret,tag);
143 if (pp == NULL) return(r);
144 p= *pp;
145
146 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
147 constructed=1;
148 else
149 constructed=0;
150 ASN1_put_object(&p,constructed,ret,tag,xclass);
151 memcpy(p,a->data,a->length);
152 p+=a->length;
153 *pp= p;
154 return(r);
155 }
156
157ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
158 long length, int Ptag, int Pclass)
159 {
160 ASN1_STRING *ret=NULL;
161 const unsigned char *p;
162 unsigned char *s;
163 long len;
164 int inf,tag,xclass;
165 int i=0;
166
167 if ((a == NULL) || ((*a) == NULL))
168 {
169 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
170 }
171 else
172 ret=(*a);
173
174 p= *pp;
175 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
176 if (inf & 0x80)
177 {
178 i=ASN1_R_BAD_OBJECT_HEADER;
179 goto err;
180 }
181
182 if (tag != Ptag)
183 {
184 i=ASN1_R_WRONG_TAG;
185 goto err;
186 }
187
188 if (inf & V_ASN1_CONSTRUCTED)
189 {
190 ASN1_const_CTX c;
191
192 c.pp=pp;
193 c.p=p;
194 c.inf=inf;
195 c.slen=len;
196 c.tag=Ptag;
197 c.xclass=Pclass;
198 c.max=(length == 0)?0:(p+length);
199 if (!asn1_collate_primitive(ret,&c))
200 goto err;
201 else
202 {
203 p=c.p;
204 }
205 }
206 else
207 {
208 if (len != 0)
209 {
210 if ((ret->length < len) || (ret->data == NULL))
211 {
212 if (ret->data != NULL) OPENSSL_free(ret->data);
213 s=(unsigned char *)OPENSSL_malloc((int)len + 1);
214 if (s == NULL)
215 {
216 i=ERR_R_MALLOC_FAILURE;
217 goto err;
218 }
219 }
220 else
221 s=ret->data;
222 memcpy(s,p,(int)len);
223 s[len] = '\0';
224 p+=len;
225 }
226 else
227 {
228 s=NULL;
229 if (ret->data != NULL) OPENSSL_free(ret->data);
230 }
231
232 ret->length=(int)len;
233 ret->data=s;
234 ret->type=Ptag;
235 }
236
237 if (a != NULL) (*a)=ret;
238 *pp=p;
239 return(ret);
240err:
241 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
242 ASN1_STRING_free(ret);
243 ASN1err(ASN1_F_D2I_ASN1_BYTES,i);
244 return(NULL);
245 }
246
247
248/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
249 * them into the one structure that is then returned */
250/* There have been a few bug fixes for this function from
251 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
252static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
253 {
254 ASN1_STRING *os=NULL;
255 BUF_MEM b;
256 int num;
257
258 b.length=0;
259 b.max=0;
260 b.data=NULL;
261
262 if (a == NULL)
263 {
264 c->error=ERR_R_PASSED_NULL_PARAMETER;
265 goto err;
266 }
267
268 num=0;
269 for (;;)
270 {
271 if (c->inf & 1)
272 {
273 c->eos=ASN1_const_check_infinite_end(&c->p,
274 (long)(c->max-c->p));
275 if (c->eos) break;
276 }
277 else
278 {
279 if (c->slen <= 0) break;
280 }
281
282 c->q=c->p;
283 if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
284 == NULL)
285 {
286 c->error=ERR_R_ASN1_LIB;
287 goto err;
288 }
289
290 if (!BUF_MEM_grow_clean(&b,num+os->length))
291 {
292 c->error=ERR_R_BUF_LIB;
293 goto err;
294 }
295 memcpy(&(b.data[num]),os->data,os->length);
296 if (!(c->inf & 1))
297 c->slen-=(c->p-c->q);
298 num+=os->length;
299 }
300
301 if (!asn1_const_Finish(c)) goto err;
302
303 a->length=num;
304 if (a->data != NULL) OPENSSL_free(a->data);
305 a->data=(unsigned char *)b.data;
306 if (os != NULL) ASN1_STRING_free(os);
307 return(1);
308err:
309 ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error);
310 if (os != NULL) ASN1_STRING_free(os);
311 if (b.data != NULL) OPENSSL_free(b.data);
312 return(0);
313 }
314
diff --git a/src/lib/libcrypto/asn1/a_d2i_fp.c b/src/lib/libcrypto/asn1/a_d2i_fp.c
deleted file mode 100644
index 52b2ebdb63..0000000000
--- a/src/lib/libcrypto/asn1/a_d2i_fp.c
+++ /dev/null
@@ -1,286 +0,0 @@
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 <limits.h>
61#include "cryptlib.h"
62#include <openssl/buffer.h>
63#include <openssl/asn1_mac.h>
64
65static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
66
67#ifndef NO_OLD_ASN1
68#ifndef OPENSSL_NO_FP_API
69
70void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
71 {
72 BIO *b;
73 void *ret;
74
75 if ((b=BIO_new(BIO_s_file())) == NULL)
76 {
77 ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB);
78 return(NULL);
79 }
80 BIO_set_fp(b,in,BIO_NOCLOSE);
81 ret=ASN1_d2i_bio(xnew,d2i,b,x);
82 BIO_free(b);
83 return(ret);
84 }
85#endif
86
87void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
88 {
89 BUF_MEM *b = NULL;
90 const unsigned char *p;
91 void *ret=NULL;
92 int len;
93
94 len = asn1_d2i_read_bio(in, &b);
95 if(len < 0) goto err;
96
97 p=(unsigned char *)b->data;
98 ret=d2i(x,&p,len);
99err:
100 if (b != NULL) BUF_MEM_free(b);
101 return(ret);
102 }
103
104#endif
105
106void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
107 {
108 BUF_MEM *b = NULL;
109 const unsigned char *p;
110 void *ret=NULL;
111 int len;
112
113 len = asn1_d2i_read_bio(in, &b);
114 if(len < 0) goto err;
115
116 p=(const unsigned char *)b->data;
117 ret=ASN1_item_d2i(x,&p,len, it);
118err:
119 if (b != NULL) BUF_MEM_free(b);
120 return(ret);
121 }
122
123#ifndef OPENSSL_NO_FP_API
124void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
125 {
126 BIO *b;
127 char *ret;
128
129 if ((b=BIO_new(BIO_s_file())) == NULL)
130 {
131 ASN1err(ASN1_F_ASN1_ITEM_D2I_FP,ERR_R_BUF_LIB);
132 return(NULL);
133 }
134 BIO_set_fp(b,in,BIO_NOCLOSE);
135 ret=ASN1_item_d2i_bio(it,b,x);
136 BIO_free(b);
137 return(ret);
138 }
139#endif
140
141#define HEADER_SIZE 8
142static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
143 {
144 BUF_MEM *b;
145 unsigned char *p;
146 int i;
147 ASN1_const_CTX c;
148 size_t want=HEADER_SIZE;
149 int eos=0;
150 size_t off=0;
151 size_t len=0;
152
153 b=BUF_MEM_new();
154 if (b == NULL)
155 {
156 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
157 return -1;
158 }
159
160 ERR_clear_error();
161 for (;;)
162 {
163 if (want >= (len-off))
164 {
165 want-=(len-off);
166
167 if (len + want < len || !BUF_MEM_grow_clean(b,len+want))
168 {
169 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
170 goto err;
171 }
172 i=BIO_read(in,&(b->data[len]),want);
173 if ((i < 0) && ((len-off) == 0))
174 {
175 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_NOT_ENOUGH_DATA);
176 goto err;
177 }
178 if (i > 0)
179 {
180 if (len+i < len)
181 {
182 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);
183 goto err;
184 }
185 len+=i;
186 }
187 }
188 /* else data already loaded */
189
190 p=(unsigned char *)&(b->data[off]);
191 c.p=p;
192 c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
193 len-off);
194 if (c.inf & 0x80)
195 {
196 unsigned long e;
197
198 e=ERR_GET_REASON(ERR_peek_error());
199 if (e != ASN1_R_TOO_LONG)
200 goto err;
201 else
202 ERR_clear_error(); /* clear error */
203 }
204 i=c.p-p;/* header length */
205 off+=i; /* end of data */
206
207 if (c.inf & 1)
208 {
209 /* no data body so go round again */
210 eos++;
211 if (eos < 0)
212 {
213 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_HEADER_TOO_LONG);
214 goto err;
215 }
216 want=HEADER_SIZE;
217 }
218 else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
219 {
220 /* eos value, so go back and read another header */
221 eos--;
222 if (eos <= 0)
223 break;
224 else
225 want=HEADER_SIZE;
226 }
227 else
228 {
229 /* suck in c.slen bytes of data */
230 want=c.slen;
231 if (want > (len-off))
232 {
233 want-=(len-off);
234 if (want > INT_MAX /* BIO_read takes an int length */ ||
235 len+want < len)
236 {
237 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);
238 goto err;
239 }
240 if (!BUF_MEM_grow_clean(b,len+want))
241 {
242 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
243 goto err;
244 }
245 while (want > 0)
246 {
247 i=BIO_read(in,&(b->data[len]),want);
248 if (i <= 0)
249 {
250 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
251 ASN1_R_NOT_ENOUGH_DATA);
252 goto err;
253 }
254 /* This can't overflow because
255 * |len+want| didn't overflow. */
256 len+=i;
257 want-=i;
258 }
259 }
260 if (off + c.slen < off)
261 {
262 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);
263 goto err;
264 }
265 off+=c.slen;
266 if (eos <= 0)
267 {
268 break;
269 }
270 else
271 want=HEADER_SIZE;
272 }
273 }
274
275 if (off > INT_MAX)
276 {
277 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);
278 goto err;
279 }
280
281 *pb = b;
282 return off;
283err:
284 if (b != NULL) BUF_MEM_free(b);
285 return -1;
286 }
diff --git a/src/lib/libcrypto/asn1/a_digest.c b/src/lib/libcrypto/asn1/a_digest.c
deleted file mode 100644
index d00d9e22b1..0000000000
--- a/src/lib/libcrypto/asn1/a_digest.c
+++ /dev/null
@@ -1,111 +0,0 @@
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
62#include "cryptlib.h"
63
64#ifndef NO_SYS_TYPES_H
65# include <sys/types.h>
66#endif
67
68#include <openssl/err.h>
69#include <openssl/evp.h>
70#include <openssl/buffer.h>
71#include <openssl/x509.h>
72
73#ifndef NO_ASN1_OLD
74
75int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
76 unsigned char *md, unsigned int *len)
77 {
78 int i;
79 unsigned char *str,*p;
80
81 i=i2d(data,NULL);
82 if ((str=(unsigned char *)OPENSSL_malloc(i)) == NULL)
83 {
84 ASN1err(ASN1_F_ASN1_DIGEST,ERR_R_MALLOC_FAILURE);
85 return(0);
86 }
87 p=str;
88 i2d(data,&p);
89
90 EVP_Digest(str, i, md, len, type, NULL);
91 OPENSSL_free(str);
92 return(1);
93 }
94
95#endif
96
97
98int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
99 unsigned char *md, unsigned int *len)
100 {
101 int i;
102 unsigned char *str = NULL;
103
104 i=ASN1_item_i2d(asn,&str, it);
105 if (!str) return(0);
106
107 EVP_Digest(str, i, md, len, type, NULL);
108 OPENSSL_free(str);
109 return(1);
110 }
111
diff --git a/src/lib/libcrypto/asn1/a_dup.c b/src/lib/libcrypto/asn1/a_dup.c
deleted file mode 100644
index d98992548a..0000000000
--- a/src/lib/libcrypto/asn1/a_dup.c
+++ /dev/null
@@ -1,109 +0,0 @@
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 <openssl/asn1.h>
62
63#ifndef NO_OLD_ASN1
64
65void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x)
66 {
67 unsigned char *b,*p;
68 const unsigned char *p2;
69 int i;
70 char *ret;
71
72 if (x == NULL) return(NULL);
73
74 i=i2d(x,NULL);
75 b=OPENSSL_malloc(i+10);
76 if (b == NULL)
77 { ASN1err(ASN1_F_ASN1_DUP,ERR_R_MALLOC_FAILURE); return(NULL); }
78 p= b;
79 i=i2d(x,&p);
80 p2= b;
81 ret=d2i(NULL,&p2,i);
82 OPENSSL_free(b);
83 return(ret);
84 }
85
86#endif
87
88/* ASN1_ITEM version of dup: this follows the model above except we don't need
89 * to allocate the buffer. At some point this could be rewritten to directly dup
90 * the underlying structure instead of doing and encode and decode.
91 */
92
93void *ASN1_item_dup(const ASN1_ITEM *it, void *x)
94 {
95 unsigned char *b = NULL;
96 const unsigned char *p;
97 long i;
98 void *ret;
99
100 if (x == NULL) return(NULL);
101
102 i=ASN1_item_i2d(x,&b,it);
103 if (b == NULL)
104 { ASN1err(ASN1_F_ASN1_ITEM_DUP,ERR_R_MALLOC_FAILURE); return(NULL); }
105 p= b;
106 ret=ASN1_item_d2i(NULL,&p,i, it);
107 OPENSSL_free(b);
108 return(ret);
109 }
diff --git a/src/lib/libcrypto/asn1/a_enum.c b/src/lib/libcrypto/asn1/a_enum.c
deleted file mode 100644
index fe9aa13b9c..0000000000
--- a/src/lib/libcrypto/asn1/a_enum.c
+++ /dev/null
@@ -1,182 +0,0 @@
1/* crypto/asn1/a_enum.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1.h>
62#include <openssl/bn.h>
63
64/*
65 * Code for ENUMERATED type: identical to INTEGER apart from a different tag.
66 * for comments on encoding see a_int.c
67 */
68
69int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
70 {
71 int j,k;
72 unsigned int i;
73 unsigned char buf[sizeof(long)+1];
74 long d;
75
76 a->type=V_ASN1_ENUMERATED;
77 if (a->length < (int)(sizeof(long)+1))
78 {
79 if (a->data != NULL)
80 OPENSSL_free(a->data);
81 if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL)
82 memset((char *)a->data,0,sizeof(long)+1);
83 }
84 if (a->data == NULL)
85 {
86 ASN1err(ASN1_F_ASN1_ENUMERATED_SET,ERR_R_MALLOC_FAILURE);
87 return(0);
88 }
89 d=v;
90 if (d < 0)
91 {
92 d= -d;
93 a->type=V_ASN1_NEG_ENUMERATED;
94 }
95
96 for (i=0; i<sizeof(long); i++)
97 {
98 if (d == 0) break;
99 buf[i]=(int)d&0xff;
100 d>>=8;
101 }
102 j=0;
103 for (k=i-1; k >=0; k--)
104 a->data[j++]=buf[k];
105 a->length=j;
106 return(1);
107 }
108
109long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a)
110 {
111 int neg=0,i;
112 long r=0;
113
114 if (a == NULL) return(0L);
115 i=a->type;
116 if (i == V_ASN1_NEG_ENUMERATED)
117 neg=1;
118 else if (i != V_ASN1_ENUMERATED)
119 return -1;
120
121 if (a->length > (int)sizeof(long))
122 {
123 /* hmm... a bit ugly */
124 return(0xffffffffL);
125 }
126 if (a->data == NULL)
127 return 0;
128
129 for (i=0; i<a->length; i++)
130 {
131 r<<=8;
132 r|=(unsigned char)a->data[i];
133 }
134 if (neg) r= -r;
135 return(r);
136 }
137
138ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(BIGNUM *bn, ASN1_ENUMERATED *ai)
139 {
140 ASN1_ENUMERATED *ret;
141 int len,j;
142
143 if (ai == NULL)
144 ret=M_ASN1_ENUMERATED_new();
145 else
146 ret=ai;
147 if (ret == NULL)
148 {
149 ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED,ERR_R_NESTED_ASN1_ERROR);
150 goto err;
151 }
152 if(BN_is_negative(bn)) ret->type = V_ASN1_NEG_ENUMERATED;
153 else ret->type=V_ASN1_ENUMERATED;
154 j=BN_num_bits(bn);
155 len=((j == 0)?0:((j/8)+1));
156 if (ret->length < len+4)
157 {
158 unsigned char *new_data=OPENSSL_realloc(ret->data, len+4);
159 if (!new_data)
160 {
161 ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
162 goto err;
163 }
164 ret->data=new_data;
165 }
166
167 ret->length=BN_bn2bin(bn,ret->data);
168 return(ret);
169err:
170 if (ret != ai) M_ASN1_ENUMERATED_free(ret);
171 return(NULL);
172 }
173
174BIGNUM *ASN1_ENUMERATED_to_BN(ASN1_ENUMERATED *ai, BIGNUM *bn)
175 {
176 BIGNUM *ret;
177
178 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
179 ASN1err(ASN1_F_ASN1_ENUMERATED_TO_BN,ASN1_R_BN_LIB);
180 else if(ai->type == V_ASN1_NEG_ENUMERATED) BN_set_negative(ret,1);
181 return(ret);
182 }
diff --git a/src/lib/libcrypto/asn1/a_i2d_fp.c b/src/lib/libcrypto/asn1/a_i2d_fp.c
deleted file mode 100644
index a3ad76d356..0000000000
--- a/src/lib/libcrypto/asn1/a_i2d_fp.c
+++ /dev/null
@@ -1,163 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/asn1.h>
63
64#ifndef NO_OLD_ASN1
65
66#ifndef OPENSSL_NO_FP_API
67int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x)
68 {
69 BIO *b;
70 int ret;
71
72 if ((b=BIO_new(BIO_s_file())) == NULL)
73 {
74 ASN1err(ASN1_F_ASN1_I2D_FP,ERR_R_BUF_LIB);
75 return(0);
76 }
77 BIO_set_fp(b,out,BIO_NOCLOSE);
78 ret=ASN1_i2d_bio(i2d,b,x);
79 BIO_free(b);
80 return(ret);
81 }
82#endif
83
84int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x)
85 {
86 char *b;
87 unsigned char *p;
88 int i,j=0,n,ret=1;
89
90 n=i2d(x,NULL);
91 b=(char *)OPENSSL_malloc(n);
92 if (b == NULL)
93 {
94 ASN1err(ASN1_F_ASN1_I2D_BIO,ERR_R_MALLOC_FAILURE);
95 return(0);
96 }
97
98 p=(unsigned char *)b;
99 i2d(x,&p);
100
101 for (;;)
102 {
103 i=BIO_write(out,&(b[j]),n);
104 if (i == n) break;
105 if (i <= 0)
106 {
107 ret=0;
108 break;
109 }
110 j+=i;
111 n-=i;
112 }
113 OPENSSL_free(b);
114 return(ret);
115 }
116
117#endif
118
119#ifndef OPENSSL_NO_FP_API
120int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x)
121 {
122 BIO *b;
123 int ret;
124
125 if ((b=BIO_new(BIO_s_file())) == NULL)
126 {
127 ASN1err(ASN1_F_ASN1_ITEM_I2D_FP,ERR_R_BUF_LIB);
128 return(0);
129 }
130 BIO_set_fp(b,out,BIO_NOCLOSE);
131 ret=ASN1_item_i2d_bio(it,b,x);
132 BIO_free(b);
133 return(ret);
134 }
135#endif
136
137int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x)
138 {
139 unsigned char *b = NULL;
140 int i,j=0,n,ret=1;
141
142 n = ASN1_item_i2d(x, &b, it);
143 if (b == NULL)
144 {
145 ASN1err(ASN1_F_ASN1_ITEM_I2D_BIO,ERR_R_MALLOC_FAILURE);
146 return(0);
147 }
148
149 for (;;)
150 {
151 i=BIO_write(out,&(b[j]),n);
152 if (i == n) break;
153 if (i <= 0)
154 {
155 ret=0;
156 break;
157 }
158 j+=i;
159 n-=i;
160 }
161 OPENSSL_free(b);
162 return(ret);
163 }
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c
deleted file mode 100644
index 3348b8762c..0000000000
--- a/src/lib/libcrypto/asn1/a_int.c
+++ /dev/null
@@ -1,458 +0,0 @@
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 <openssl/asn1.h>
62#include <openssl/bn.h>
63
64ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
65{ return M_ASN1_INTEGER_dup(x);}
66
67int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
68 {
69 int neg, ret;
70 /* Compare signs */
71 neg = x->type & V_ASN1_NEG;
72 if (neg != (y->type & V_ASN1_NEG))
73 {
74 if (neg)
75 return -1;
76 else
77 return 1;
78 }
79
80 ret = ASN1_STRING_cmp(x, y);
81
82 if (neg)
83 return -ret;
84 else
85 return ret;
86 }
87
88
89/*
90 * This converts an ASN1 INTEGER into its content encoding.
91 * The internal representation is an ASN1_STRING whose data is a big endian
92 * representation of the value, ignoring the sign. The sign is determined by
93 * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
94 *
95 * Positive integers are no problem: they are almost the same as the DER
96 * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
97 *
98 * Negative integers are a bit trickier...
99 * The DER representation of negative integers is in 2s complement form.
100 * The internal form is converted by complementing each octet and finally
101 * adding one to the result. This can be done less messily with a little trick.
102 * If the internal form has trailing zeroes then they will become FF by the
103 * complement and 0 by the add one (due to carry) so just copy as many trailing
104 * zeros to the destination as there are in the source. The carry will add one
105 * to the last none zero octet: so complement this octet and add one and finally
106 * complement any left over until you get to the start of the string.
107 *
108 * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
109 * with 0xff. However if the first byte is 0x80 and one of the following bytes
110 * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
111 * followed by optional zeros isn't padded.
112 */
113
114int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
115 {
116 int pad=0,ret,i,neg;
117 unsigned char *p,*n,pb=0;
118
119 if ((a == NULL) || (a->data == NULL)) return(0);
120 neg=a->type & V_ASN1_NEG;
121 if (a->length == 0)
122 ret=1;
123 else
124 {
125 ret=a->length;
126 i=a->data[0];
127 if (!neg && (i > 127)) {
128 pad=1;
129 pb=0;
130 } else if(neg) {
131 if(i>128) {
132 pad=1;
133 pb=0xFF;
134 } else if(i == 128) {
135 /*
136 * Special case: if any other bytes non zero we pad:
137 * otherwise we don't.
138 */
139 for(i = 1; i < a->length; i++) if(a->data[i]) {
140 pad=1;
141 pb=0xFF;
142 break;
143 }
144 }
145 }
146 ret+=pad;
147 }
148 if (pp == NULL) return(ret);
149 p= *pp;
150
151 if (pad) *(p++)=pb;
152 if (a->length == 0) *(p++)=0;
153 else if (!neg) memcpy(p,a->data,(unsigned int)a->length);
154 else {
155 /* Begin at the end of the encoding */
156 n=a->data + a->length - 1;
157 p += a->length - 1;
158 i = a->length;
159 /* Copy zeros to destination as long as source is zero */
160 while(!*n) {
161 *(p--) = 0;
162 n--;
163 i--;
164 }
165 /* Complement and increment next octet */
166 *(p--) = ((*(n--)) ^ 0xff) + 1;
167 i--;
168 /* Complement any octets left */
169 for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
170 }
171
172 *pp+=ret;
173 return(ret);
174 }
175
176/* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
177
178ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
179 long len)
180 {
181 ASN1_INTEGER *ret=NULL;
182 const unsigned char *p, *pend;
183 unsigned char *to,*s;
184 int i;
185
186 if ((a == NULL) || ((*a) == NULL))
187 {
188 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
189 ret->type=V_ASN1_INTEGER;
190 }
191 else
192 ret=(*a);
193
194 p= *pp;
195 pend = p + len;
196
197 /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
198 * signifies a missing NULL parameter. */
199 s=(unsigned char *)OPENSSL_malloc((int)len+1);
200 if (s == NULL)
201 {
202 i=ERR_R_MALLOC_FAILURE;
203 goto err;
204 }
205 to=s;
206 if(!len) {
207 /* Strictly speaking this is an illegal INTEGER but we
208 * tolerate it.
209 */
210 ret->type=V_ASN1_INTEGER;
211 } else if (*p & 0x80) /* a negative number */
212 {
213 ret->type=V_ASN1_NEG_INTEGER;
214 if ((*p == 0xff) && (len != 1)) {
215 p++;
216 len--;
217 }
218 i = len;
219 p += i - 1;
220 to += i - 1;
221 while((!*p) && i) {
222 *(to--) = 0;
223 i--;
224 p--;
225 }
226 /* Special case: if all zeros then the number will be of
227 * the form FF followed by n zero bytes: this corresponds to
228 * 1 followed by n zero bytes. We've already written n zeros
229 * so we just append an extra one and set the first byte to
230 * a 1. This is treated separately because it is the only case
231 * where the number of bytes is larger than len.
232 */
233 if(!i) {
234 *s = 1;
235 s[len] = 0;
236 len++;
237 } else {
238 *(to--) = (*(p--) ^ 0xff) + 1;
239 i--;
240 for(;i > 0; i--) *(to--) = *(p--) ^ 0xff;
241 }
242 } else {
243 ret->type=V_ASN1_INTEGER;
244 if ((*p == 0) && (len != 1))
245 {
246 p++;
247 len--;
248 }
249 memcpy(s,p,(int)len);
250 }
251
252 if (ret->data != NULL) OPENSSL_free(ret->data);
253 ret->data=s;
254 ret->length=(int)len;
255 if (a != NULL) (*a)=ret;
256 *pp=pend;
257 return(ret);
258err:
259 ASN1err(ASN1_F_C2I_ASN1_INTEGER,i);
260 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
261 M_ASN1_INTEGER_free(ret);
262 return(NULL);
263 }
264
265
266/* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
267 * ASN1 integers: some broken software can encode a positive INTEGER
268 * with its MSB set as negative (it doesn't add a padding zero).
269 */
270
271ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
272 long length)
273 {
274 ASN1_INTEGER *ret=NULL;
275 const unsigned char *p;
276 unsigned char *s;
277 long len;
278 int inf,tag,xclass;
279 int i;
280
281 if ((a == NULL) || ((*a) == NULL))
282 {
283 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
284 ret->type=V_ASN1_INTEGER;
285 }
286 else
287 ret=(*a);
288
289 p= *pp;
290 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
291 if (inf & 0x80)
292 {
293 i=ASN1_R_BAD_OBJECT_HEADER;
294 goto err;
295 }
296
297 if (tag != V_ASN1_INTEGER)
298 {
299 i=ASN1_R_EXPECTING_AN_INTEGER;
300 goto err;
301 }
302
303 /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
304 * signifies a missing NULL parameter. */
305 s=(unsigned char *)OPENSSL_malloc((int)len+1);
306 if (s == NULL)
307 {
308 i=ERR_R_MALLOC_FAILURE;
309 goto err;
310 }
311 ret->type=V_ASN1_INTEGER;
312 if(len) {
313 if ((*p == 0) && (len != 1))
314 {
315 p++;
316 len--;
317 }
318 memcpy(s,p,(int)len);
319 p+=len;
320 }
321
322 if (ret->data != NULL) OPENSSL_free(ret->data);
323 ret->data=s;
324 ret->length=(int)len;
325 if (a != NULL) (*a)=ret;
326 *pp=p;
327 return(ret);
328err:
329 ASN1err(ASN1_F_D2I_ASN1_UINTEGER,i);
330 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
331 M_ASN1_INTEGER_free(ret);
332 return(NULL);
333 }
334
335int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
336 {
337 int j,k;
338 unsigned int i;
339 unsigned char buf[sizeof(long)+1];
340 long d;
341
342 a->type=V_ASN1_INTEGER;
343 if (a->length < (int)(sizeof(long)+1))
344 {
345 if (a->data != NULL)
346 OPENSSL_free(a->data);
347 if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL)
348 memset((char *)a->data,0,sizeof(long)+1);
349 }
350 if (a->data == NULL)
351 {
352 ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
353 return(0);
354 }
355 d=v;
356 if (d < 0)
357 {
358 d= -d;
359 a->type=V_ASN1_NEG_INTEGER;
360 }
361
362 for (i=0; i<sizeof(long); i++)
363 {
364 if (d == 0) break;
365 buf[i]=(int)d&0xff;
366 d>>=8;
367 }
368 j=0;
369 for (k=i-1; k >=0; k--)
370 a->data[j++]=buf[k];
371 a->length=j;
372 return(1);
373 }
374
375long ASN1_INTEGER_get(const ASN1_INTEGER *a)
376 {
377 int neg=0,i;
378 long r=0;
379
380 if (a == NULL) return(0L);
381 i=a->type;
382 if (i == V_ASN1_NEG_INTEGER)
383 neg=1;
384 else if (i != V_ASN1_INTEGER)
385 return -1;
386
387 if (a->length > (int)sizeof(long))
388 {
389 /* hmm... a bit ugly */
390 return(0xffffffffL);
391 }
392 if (a->data == NULL)
393 return 0;
394
395 for (i=0; i<a->length; i++)
396 {
397 r<<=8;
398 r|=(unsigned char)a->data[i];
399 }
400 if (neg) r= -r;
401 return(r);
402 }
403
404ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
405 {
406 ASN1_INTEGER *ret;
407 int len,j;
408
409 if (ai == NULL)
410 ret=M_ASN1_INTEGER_new();
411 else
412 ret=ai;
413 if (ret == NULL)
414 {
415 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
416 goto err;
417 }
418 if (BN_is_negative(bn))
419 ret->type = V_ASN1_NEG_INTEGER;
420 else ret->type=V_ASN1_INTEGER;
421 j=BN_num_bits(bn);
422 len=((j == 0)?0:((j/8)+1));
423 if (ret->length < len+4)
424 {
425 unsigned char *new_data=OPENSSL_realloc(ret->data, len+4);
426 if (!new_data)
427 {
428 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
429 goto err;
430 }
431 ret->data=new_data;
432 }
433 ret->length=BN_bn2bin(bn,ret->data);
434 /* Correct zero case */
435 if(!ret->length)
436 {
437 ret->data[0] = 0;
438 ret->length = 1;
439 }
440 return(ret);
441err:
442 if (ret != ai) M_ASN1_INTEGER_free(ret);
443 return(NULL);
444 }
445
446BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
447 {
448 BIGNUM *ret;
449
450 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
451 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
452 else if(ai->type == V_ASN1_NEG_INTEGER)
453 BN_set_negative(ret, 1);
454 return(ret);
455 }
456
457IMPLEMENT_STACK_OF(ASN1_INTEGER)
458IMPLEMENT_ASN1_SET_OF(ASN1_INTEGER)
diff --git a/src/lib/libcrypto/asn1/a_mbstr.c b/src/lib/libcrypto/asn1/a_mbstr.c
deleted file mode 100644
index 1538e0a4fc..0000000000
--- a/src/lib/libcrypto/asn1/a_mbstr.c
+++ /dev/null
@@ -1,400 +0,0 @@
1/* a_mbstr.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include "cryptlib.h"
62#include <openssl/asn1.h>
63
64static int traverse_string(const unsigned char *p, int len, int inform,
65 int (*rfunc)(unsigned long value, void *in), void *arg);
66static int in_utf8(unsigned long value, void *arg);
67static int out_utf8(unsigned long value, void *arg);
68static int type_str(unsigned long value, void *arg);
69static int cpy_asc(unsigned long value, void *arg);
70static int cpy_bmp(unsigned long value, void *arg);
71static int cpy_univ(unsigned long value, void *arg);
72static int cpy_utf8(unsigned long value, void *arg);
73static int is_printable(unsigned long value);
74
75/* These functions take a string in UTF8, ASCII or multibyte form and
76 * a mask of permissible ASN1 string types. It then works out the minimal
77 * type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
78 * and creates a string of the correct type with the supplied data.
79 * Yes this is horrible: it has to be :-(
80 * The 'ncopy' form checks minimum and maximum size limits too.
81 */
82
83int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
84 int inform, unsigned long mask)
85{
86 return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
87}
88
89int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
90 int inform, unsigned long mask,
91 long minsize, long maxsize)
92{
93 int str_type;
94 int ret;
95 char free_out;
96 int outform, outlen = 0;
97 ASN1_STRING *dest;
98 unsigned char *p;
99 int nchar;
100 char strbuf[32];
101 int (*cpyfunc)(unsigned long,void *) = NULL;
102 if(len == -1) len = strlen((const char *)in);
103 if(!mask) mask = DIRSTRING_TYPE;
104
105 /* First do a string check and work out the number of characters */
106 switch(inform) {
107
108 case MBSTRING_BMP:
109 if(len & 1) {
110 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
111 ASN1_R_INVALID_BMPSTRING_LENGTH);
112 return -1;
113 }
114 nchar = len >> 1;
115 break;
116
117 case MBSTRING_UNIV:
118 if(len & 3) {
119 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
120 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
121 return -1;
122 }
123 nchar = len >> 2;
124 break;
125
126 case MBSTRING_UTF8:
127 nchar = 0;
128 /* This counts the characters and does utf8 syntax checking */
129 ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
130 if(ret < 0) {
131 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
132 ASN1_R_INVALID_UTF8STRING);
133 return -1;
134 }
135 break;
136
137 case MBSTRING_ASC:
138 nchar = len;
139 break;
140
141 default:
142 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
143 return -1;
144 }
145
146 if((minsize > 0) && (nchar < minsize)) {
147 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
148 BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
149 ERR_add_error_data(2, "minsize=", strbuf);
150 return -1;
151 }
152
153 if((maxsize > 0) && (nchar > maxsize)) {
154 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
155 BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
156 ERR_add_error_data(2, "maxsize=", strbuf);
157 return -1;
158 }
159
160 /* Now work out minimal type (if any) */
161 if(traverse_string(in, len, inform, type_str, &mask) < 0) {
162 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
163 return -1;
164 }
165
166
167 /* Now work out output format and string type */
168 outform = MBSTRING_ASC;
169 if(mask & B_ASN1_PRINTABLESTRING) str_type = V_ASN1_PRINTABLESTRING;
170 else if(mask & B_ASN1_IA5STRING) str_type = V_ASN1_IA5STRING;
171 else if(mask & B_ASN1_T61STRING) str_type = V_ASN1_T61STRING;
172 else if(mask & B_ASN1_BMPSTRING) {
173 str_type = V_ASN1_BMPSTRING;
174 outform = MBSTRING_BMP;
175 } else if(mask & B_ASN1_UNIVERSALSTRING) {
176 str_type = V_ASN1_UNIVERSALSTRING;
177 outform = MBSTRING_UNIV;
178 } else {
179 str_type = V_ASN1_UTF8STRING;
180 outform = MBSTRING_UTF8;
181 }
182 if(!out) return str_type;
183 if(*out) {
184 free_out = 0;
185 dest = *out;
186 if(dest->data) {
187 dest->length = 0;
188 OPENSSL_free(dest->data);
189 dest->data = NULL;
190 }
191 dest->type = str_type;
192 } else {
193 free_out = 1;
194 dest = ASN1_STRING_type_new(str_type);
195 if(!dest) {
196 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
197 ERR_R_MALLOC_FAILURE);
198 return -1;
199 }
200 *out = dest;
201 }
202 /* If both the same type just copy across */
203 if(inform == outform) {
204 if(!ASN1_STRING_set(dest, in, len)) {
205 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,ERR_R_MALLOC_FAILURE);
206 return -1;
207 }
208 return str_type;
209 }
210
211 /* Work out how much space the destination will need */
212 switch(outform) {
213 case MBSTRING_ASC:
214 outlen = nchar;
215 cpyfunc = cpy_asc;
216 break;
217
218 case MBSTRING_BMP:
219 outlen = nchar << 1;
220 cpyfunc = cpy_bmp;
221 break;
222
223 case MBSTRING_UNIV:
224 outlen = nchar << 2;
225 cpyfunc = cpy_univ;
226 break;
227
228 case MBSTRING_UTF8:
229 outlen = 0;
230 traverse_string(in, len, inform, out_utf8, &outlen);
231 cpyfunc = cpy_utf8;
232 break;
233 }
234 if(!(p = OPENSSL_malloc(outlen + 1))) {
235 if(free_out) ASN1_STRING_free(dest);
236 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,ERR_R_MALLOC_FAILURE);
237 return -1;
238 }
239 dest->length = outlen;
240 dest->data = p;
241 p[outlen] = 0;
242 traverse_string(in, len, inform, cpyfunc, &p);
243 return str_type;
244}
245
246/* This function traverses a string and passes the value of each character
247 * to an optional function along with a void * argument.
248 */
249
250static int traverse_string(const unsigned char *p, int len, int inform,
251 int (*rfunc)(unsigned long value, void *in), void *arg)
252{
253 unsigned long value;
254 int ret;
255 while(len) {
256 if(inform == MBSTRING_ASC) {
257 value = *p++;
258 len--;
259 } else if(inform == MBSTRING_BMP) {
260 value = *p++ << 8;
261 value |= *p++;
262 len -= 2;
263 } else if(inform == MBSTRING_UNIV) {
264 value = ((unsigned long)*p++) << 24;
265 value |= ((unsigned long)*p++) << 16;
266 value |= *p++ << 8;
267 value |= *p++;
268 len -= 4;
269 } else {
270 ret = UTF8_getc(p, len, &value);
271 if(ret < 0) return -1;
272 len -= ret;
273 p += ret;
274 }
275 if(rfunc) {
276 ret = rfunc(value, arg);
277 if(ret <= 0) return ret;
278 }
279 }
280 return 1;
281}
282
283/* Various utility functions for traverse_string */
284
285/* Just count number of characters */
286
287static int in_utf8(unsigned long value, void *arg)
288{
289 int *nchar;
290 nchar = arg;
291 (*nchar)++;
292 return 1;
293}
294
295/* Determine size of output as a UTF8 String */
296
297static int out_utf8(unsigned long value, void *arg)
298{
299 int *outlen;
300 outlen = arg;
301 *outlen += UTF8_putc(NULL, -1, value);
302 return 1;
303}
304
305/* Determine the "type" of a string: check each character against a
306 * supplied "mask".
307 */
308
309static int type_str(unsigned long value, void *arg)
310{
311 unsigned long types;
312 types = *((unsigned long *)arg);
313 if((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
314 types &= ~B_ASN1_PRINTABLESTRING;
315 if((types & B_ASN1_IA5STRING) && (value > 127))
316 types &= ~B_ASN1_IA5STRING;
317 if((types & B_ASN1_T61STRING) && (value > 0xff))
318 types &= ~B_ASN1_T61STRING;
319 if((types & B_ASN1_BMPSTRING) && (value > 0xffff))
320 types &= ~B_ASN1_BMPSTRING;
321 if(!types) return -1;
322 *((unsigned long *)arg) = types;
323 return 1;
324}
325
326/* Copy one byte per character ASCII like strings */
327
328static int cpy_asc(unsigned long value, void *arg)
329{
330 unsigned char **p, *q;
331 p = arg;
332 q = *p;
333 *q = (unsigned char) value;
334 (*p)++;
335 return 1;
336}
337
338/* Copy two byte per character BMPStrings */
339
340static int cpy_bmp(unsigned long value, void *arg)
341{
342 unsigned char **p, *q;
343 p = arg;
344 q = *p;
345 *q++ = (unsigned char) ((value >> 8) & 0xff);
346 *q = (unsigned char) (value & 0xff);
347 *p += 2;
348 return 1;
349}
350
351/* Copy four byte per character UniversalStrings */
352
353static int cpy_univ(unsigned long value, void *arg)
354{
355 unsigned char **p, *q;
356 p = arg;
357 q = *p;
358 *q++ = (unsigned char) ((value >> 24) & 0xff);
359 *q++ = (unsigned char) ((value >> 16) & 0xff);
360 *q++ = (unsigned char) ((value >> 8) & 0xff);
361 *q = (unsigned char) (value & 0xff);
362 *p += 4;
363 return 1;
364}
365
366/* Copy to a UTF8String */
367
368static int cpy_utf8(unsigned long value, void *arg)
369{
370 unsigned char **p;
371 int ret;
372 p = arg;
373 /* We already know there is enough room so pass 0xff as the length */
374 ret = UTF8_putc(*p, 0xff, value);
375 *p += ret;
376 return 1;
377}
378
379/* Return 1 if the character is permitted in a PrintableString */
380static int is_printable(unsigned long value)
381{
382 int ch;
383 if(value > 0x7f) return 0;
384 ch = (int) value;
385 /* Note: we can't use 'isalnum' because certain accented
386 * characters may count as alphanumeric in some environments.
387 */
388#ifndef CHARSET_EBCDIC
389 if((ch >= 'a') && (ch <= 'z')) return 1;
390 if((ch >= 'A') && (ch <= 'Z')) return 1;
391 if((ch >= '0') && (ch <= '9')) return 1;
392 if ((ch == ' ') || strchr("'()+,-./:=?", ch)) return 1;
393#else /*CHARSET_EBCDIC*/
394 if((ch >= os_toascii['a']) && (ch <= os_toascii['z'])) return 1;
395 if((ch >= os_toascii['A']) && (ch <= os_toascii['Z'])) return 1;
396 if((ch >= os_toascii['0']) && (ch <= os_toascii['9'])) return 1;
397 if ((ch == os_toascii[' ']) || strchr("'()+,-./:=?", os_toebcdic[ch])) return 1;
398#endif /*CHARSET_EBCDIC*/
399 return 0;
400}
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
deleted file mode 100644
index 3978c9150d..0000000000
--- a/src/lib/libcrypto/asn1/a_object.c
+++ /dev/null
@@ -1,403 +0,0 @@
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 <limits.h>
61#include "cryptlib.h"
62#include <openssl/buffer.h>
63#include <openssl/asn1.h>
64#include <openssl/objects.h>
65#include <openssl/bn.h>
66
67int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
68 {
69 unsigned char *p;
70 int objsize;
71
72 if ((a == NULL) || (a->data == NULL)) return(0);
73
74 objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT);
75 if (pp == NULL) return objsize;
76
77 p= *pp;
78 ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
79 memcpy(p,a->data,a->length);
80 p+=a->length;
81
82 *pp=p;
83 return(objsize);
84 }
85
86int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
87 {
88 int i,first,len=0,c, use_bn;
89 char ftmp[24], *tmp = ftmp;
90 int tmpsize = sizeof ftmp;
91 const char *p;
92 unsigned long l;
93 BIGNUM *bl = NULL;
94
95 if (num == 0)
96 return(0);
97 else if (num == -1)
98 num=strlen(buf);
99
100 p=buf;
101 c= *(p++);
102 num--;
103 if ((c >= '0') && (c <= '2'))
104 {
105 first= c-'0';
106 }
107 else
108 {
109 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
110 goto err;
111 }
112
113 if (num <= 0)
114 {
115 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
116 goto err;
117 }
118 c= *(p++);
119 num--;
120 for (;;)
121 {
122 if (num <= 0) break;
123 if ((c != '.') && (c != ' '))
124 {
125 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
126 goto err;
127 }
128 l=0;
129 use_bn = 0;
130 for (;;)
131 {
132 if (num <= 0) break;
133 num--;
134 c= *(p++);
135 if ((c == ' ') || (c == '.'))
136 break;
137 if ((c < '0') || (c > '9'))
138 {
139 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
140 goto err;
141 }
142 if (!use_bn && l >= ((ULONG_MAX - 80) / 10L))
143 {
144 use_bn = 1;
145 if (!bl)
146 bl = BN_new();
147 if (!bl || !BN_set_word(bl, l))
148 goto err;
149 }
150 if (use_bn)
151 {
152 if (!BN_mul_word(bl, 10L)
153 || !BN_add_word(bl, c-'0'))
154 goto err;
155 }
156 else
157 l=l*10L+(long)(c-'0');
158 }
159 if (len == 0)
160 {
161 if ((first < 2) && (l >= 40))
162 {
163 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
164 goto err;
165 }
166 if (use_bn)
167 {
168 if (!BN_add_word(bl, first * 40))
169 goto err;
170 }
171 else
172 l+=(long)first*40;
173 }
174 i=0;
175 if (use_bn)
176 {
177 int blsize;
178 blsize = BN_num_bits(bl);
179 blsize = (blsize + 6)/7;
180 if (blsize > tmpsize)
181 {
182 if (tmp != ftmp)
183 OPENSSL_free(tmp);
184 tmpsize = blsize + 32;
185 tmp = OPENSSL_malloc(tmpsize);
186 if (!tmp)
187 goto err;
188 }
189 while(blsize--)
190 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
191 }
192 else
193 {
194
195 for (;;)
196 {
197 tmp[i++]=(unsigned char)l&0x7f;
198 l>>=7L;
199 if (l == 0L) break;
200 }
201
202 }
203 if (out != NULL)
204 {
205 if (len+i > olen)
206 {
207 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
208 goto err;
209 }
210 while (--i > 0)
211 out[len++]=tmp[i]|0x80;
212 out[len++]=tmp[0];
213 }
214 else
215 len+=i;
216 }
217 if (tmp != ftmp)
218 OPENSSL_free(tmp);
219 if (bl)
220 BN_free(bl);
221 return(len);
222err:
223 if (tmp != ftmp)
224 OPENSSL_free(tmp);
225 if (bl)
226 BN_free(bl);
227 return(0);
228 }
229
230int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
231{
232 return OBJ_obj2txt(buf, buf_len, a, 0);
233}
234
235int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
236 {
237 char buf[80], *p = buf;
238 int i;
239
240 if ((a == NULL) || (a->data == NULL))
241 return(BIO_write(bp,"NULL",4));
242 i=i2t_ASN1_OBJECT(buf,sizeof buf,a);
243 if (i > (int)(sizeof(buf) - 1))
244 {
245 p = OPENSSL_malloc(i + 1);
246 if (!p)
247 return -1;
248 i2t_ASN1_OBJECT(p,i + 1,a);
249 }
250 if (i <= 0)
251 return BIO_write(bp, "<INVALID>", 9);
252 BIO_write(bp,p,i);
253 if (p != buf)
254 OPENSSL_free(p);
255 return(i);
256 }
257
258ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
259 long length)
260{
261 const unsigned char *p;
262 long len;
263 int tag,xclass;
264 int inf,i;
265 ASN1_OBJECT *ret = NULL;
266 p= *pp;
267 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
268 if (inf & 0x80)
269 {
270 i=ASN1_R_BAD_OBJECT_HEADER;
271 goto err;
272 }
273
274 if (tag != V_ASN1_OBJECT)
275 {
276 i=ASN1_R_EXPECTING_AN_OBJECT;
277 goto err;
278 }
279 ret = c2i_ASN1_OBJECT(a, &p, len);
280 if(ret) *pp = p;
281 return ret;
282err:
283 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
284 return(NULL);
285}
286ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
287 long len)
288 {
289 ASN1_OBJECT *ret=NULL;
290 const unsigned char *p;
291 unsigned char *data;
292 int i;
293 /* Sanity check OID encoding: can't have leading 0x80 in
294 * subidentifiers, see: X.690 8.19.2
295 */
296 for (i = 0, p = *pp; i < len; i++, p++)
297 {
298 if (*p == 0x80 && (!i || !(p[-1] & 0x80)))
299 {
300 ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING);
301 return NULL;
302 }
303 }
304
305 /* only the ASN1_OBJECTs from the 'table' will have values
306 * for ->sn or ->ln */
307 if ((a == NULL) || ((*a) == NULL) ||
308 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
309 {
310 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
311 }
312 else ret=(*a);
313
314 p= *pp;
315 /* detach data from object */
316 data = (unsigned char *)ret->data;
317 ret->data = NULL;
318 /* once detached we can change it */
319 if ((data == NULL) || (ret->length < len))
320 {
321 ret->length=0;
322 if (data != NULL) OPENSSL_free(data);
323 data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
324 if (data == NULL)
325 { i=ERR_R_MALLOC_FAILURE; goto err; }
326 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
327 }
328 memcpy(data,p,(int)len);
329 /* reattach data to object, after which it remains const */
330 ret->data =data;
331 ret->length=(int)len;
332 ret->sn=NULL;
333 ret->ln=NULL;
334 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
335 p+=len;
336
337 if (a != NULL) (*a)=ret;
338 *pp=p;
339 return(ret);
340err:
341 ASN1err(ASN1_F_C2I_ASN1_OBJECT,i);
342 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
343 ASN1_OBJECT_free(ret);
344 return(NULL);
345 }
346
347ASN1_OBJECT *ASN1_OBJECT_new(void)
348 {
349 ASN1_OBJECT *ret;
350
351 ret=(ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
352 if (ret == NULL)
353 {
354 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
355 return(NULL);
356 }
357 ret->length=0;
358 ret->data=NULL;
359 ret->nid=0;
360 ret->sn=NULL;
361 ret->ln=NULL;
362 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
363 return(ret);
364 }
365
366void ASN1_OBJECT_free(ASN1_OBJECT *a)
367 {
368 if (a == NULL) return;
369 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
370 {
371#ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */
372 if (a->sn != NULL) OPENSSL_free((void *)a->sn);
373 if (a->ln != NULL) OPENSSL_free((void *)a->ln);
374#endif
375 a->sn=a->ln=NULL;
376 }
377 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
378 {
379 if (a->data != NULL) OPENSSL_free((void *)a->data);
380 a->data=NULL;
381 a->length=0;
382 }
383 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
384 OPENSSL_free(a);
385 }
386
387ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
388 const char *sn, const char *ln)
389 {
390 ASN1_OBJECT o;
391
392 o.sn=sn;
393 o.ln=ln;
394 o.data=data;
395 o.nid=nid;
396 o.length=len;
397 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
398 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
399 return(OBJ_dup(&o));
400 }
401
402IMPLEMENT_STACK_OF(ASN1_OBJECT)
403IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT)
diff --git a/src/lib/libcrypto/asn1/a_octet.c b/src/lib/libcrypto/asn1/a_octet.c
deleted file mode 100644
index e8725e44f1..0000000000
--- a/src/lib/libcrypto/asn1/a_octet.c
+++ /dev/null
@@ -1,71 +0,0 @@
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 <openssl/asn1.h>
62
63ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *x)
64{ return M_ASN1_OCTET_STRING_dup(x); }
65
66int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, const ASN1_OCTET_STRING *b)
67{ return M_ASN1_OCTET_STRING_cmp(a, b); }
68
69int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *x, const unsigned char *d, int len)
70{ return M_ASN1_OCTET_STRING_set(x, d, len); }
71
diff --git a/src/lib/libcrypto/asn1/a_print.c b/src/lib/libcrypto/asn1/a_print.c
deleted file mode 100644
index d18e772320..0000000000
--- a/src/lib/libcrypto/asn1/a_print.c
+++ /dev/null
@@ -1,127 +0,0 @@
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 <openssl/asn1.h>
62
63int ASN1_PRINTABLE_type(const unsigned char *s, int len)
64 {
65 int c;
66 int ia5=0;
67 int t61=0;
68
69 if (len <= 0) len= -1;
70 if (s == NULL) return(V_ASN1_PRINTABLESTRING);
71
72 while ((*s) && (len-- != 0))
73 {
74 c= *(s++);
75#ifndef CHARSET_EBCDIC
76 if (!( ((c >= 'a') && (c <= 'z')) ||
77 ((c >= 'A') && (c <= 'Z')) ||
78 (c == ' ') ||
79 ((c >= '0') && (c <= '9')) ||
80 (c == ' ') || (c == '\'') ||
81 (c == '(') || (c == ')') ||
82 (c == '+') || (c == ',') ||
83 (c == '-') || (c == '.') ||
84 (c == '/') || (c == ':') ||
85 (c == '=') || (c == '?')))
86 ia5=1;
87 if (c&0x80)
88 t61=1;
89#else
90 if (!isalnum(c) && (c != ' ') &&
91 strchr("'()+,-./:=?", c) == NULL)
92 ia5=1;
93 if (os_toascii[c] & 0x80)
94 t61=1;
95#endif
96 }
97 if (t61) return(V_ASN1_T61STRING);
98 if (ia5) return(V_ASN1_IA5STRING);
99 return(V_ASN1_PRINTABLESTRING);
100 }
101
102int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
103 {
104 int i;
105 unsigned char *p;
106
107 if (s->type != V_ASN1_UNIVERSALSTRING) return(0);
108 if ((s->length%4) != 0) return(0);
109 p=s->data;
110 for (i=0; i<s->length; i+=4)
111 {
112 if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
113 break;
114 else
115 p+=4;
116 }
117 if (i < s->length) return(0);
118 p=s->data;
119 for (i=3; i<s->length; i+=4)
120 {
121 *(p++)=s->data[i];
122 }
123 *(p)='\0';
124 s->length/=4;
125 s->type=ASN1_PRINTABLE_type(s->data,s->length);
126 return(1);
127 }
diff --git a/src/lib/libcrypto/asn1/a_set.c b/src/lib/libcrypto/asn1/a_set.c
deleted file mode 100644
index d726c8d3a8..0000000000
--- a/src/lib/libcrypto/asn1/a_set.c
+++ /dev/null
@@ -1,241 +0,0 @@
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 <openssl/asn1_mac.h>
62
63#ifndef NO_ASN1_OLD
64
65typedef struct
66 {
67 unsigned char *pbData;
68 int cbData;
69 } MYBLOB;
70
71/* SetBlobCmp
72 * This function compares two elements of SET_OF block
73 */
74static int SetBlobCmp(const void *elem1, const void *elem2 )
75 {
76 const MYBLOB *b1 = (const MYBLOB *)elem1;
77 const MYBLOB *b2 = (const MYBLOB *)elem2;
78 int r;
79
80 r = memcmp(b1->pbData, b2->pbData,
81 b1->cbData < b2->cbData ? b1->cbData : b2->cbData);
82 if(r != 0)
83 return r;
84 return b1->cbData-b2->cbData;
85 }
86
87/* int is_set: if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */
88int i2d_ASN1_SET(STACK_OF(OPENSSL_BLOCK) *a, unsigned char **pp,
89 i2d_of_void *i2d, int ex_tag, int ex_class,
90 int is_set)
91 {
92 int ret=0,r;
93 int i;
94 unsigned char *p;
95 unsigned char *pStart, *pTempMem;
96 MYBLOB *rgSetBlob;
97 int totSize;
98
99 if (a == NULL) return(0);
100 for (i=sk_OPENSSL_BLOCK_num(a)-1; i>=0; i--)
101 ret+=i2d(sk_OPENSSL_BLOCK_value(a,i),NULL);
102 r=ASN1_object_size(1,ret,ex_tag);
103 if (pp == NULL) return(r);
104
105 p= *pp;
106 ASN1_put_object(&p,1,ret,ex_tag,ex_class);
107
108/* Modified by gp@nsj.co.jp */
109 /* And then again by Ben */
110 /* And again by Steve */
111
112 if(!is_set || (sk_OPENSSL_BLOCK_num(a) < 2))
113 {
114 for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
115 i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
116
117 *pp=p;
118 return(r);
119 }
120
121 pStart = p; /* Catch the beg of Setblobs*/
122 /* In this array we will store the SET blobs */
123 rgSetBlob = OPENSSL_malloc(sk_OPENSSL_BLOCK_num(a) * sizeof(MYBLOB));
124 if (rgSetBlob == NULL)
125 {
126 ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
127 return(0);
128 }
129
130 for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
131 {
132 rgSetBlob[i].pbData = p; /* catch each set encode blob */
133 i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
134 rgSetBlob[i].cbData = p - rgSetBlob[i].pbData; /* Length of this
135SetBlob
136*/
137 }
138 *pp=p;
139 totSize = p - pStart; /* This is the total size of all set blobs */
140
141 /* Now we have to sort the blobs. I am using a simple algo.
142 *Sort ptrs *Copy to temp-mem *Copy from temp-mem to user-mem*/
143 qsort( rgSetBlob, sk_OPENSSL_BLOCK_num(a), sizeof(MYBLOB), SetBlobCmp);
144 if (!(pTempMem = OPENSSL_malloc(totSize)))
145 {
146 ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
147 return(0);
148 }
149
150/* Copy to temp mem */
151 p = pTempMem;
152 for(i=0; i<sk_OPENSSL_BLOCK_num(a); ++i)
153 {
154 memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);
155 p += rgSetBlob[i].cbData;
156 }
157
158/* Copy back to user mem*/
159 memcpy(pStart, pTempMem, totSize);
160 OPENSSL_free(pTempMem);
161 OPENSSL_free(rgSetBlob);
162
163 return(r);
164 }
165
166STACK_OF(OPENSSL_BLOCK) *d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a,
167 const unsigned char **pp,
168 long length, d2i_of_void *d2i,
169 void (*free_func)(OPENSSL_BLOCK), int ex_tag,
170 int ex_class)
171 {
172 ASN1_const_CTX c;
173 STACK_OF(OPENSSL_BLOCK) *ret=NULL;
174
175 if ((a == NULL) || ((*a) == NULL))
176 {
177 if ((ret=sk_OPENSSL_BLOCK_new_null()) == NULL)
178 {
179 ASN1err(ASN1_F_D2I_ASN1_SET,ERR_R_MALLOC_FAILURE);
180 goto err;
181 }
182 }
183 else
184 ret=(*a);
185
186 c.p= *pp;
187 c.max=(length == 0)?0:(c.p+length);
188
189 c.inf=ASN1_get_object(&c.p,&c.slen,&c.tag,&c.xclass,c.max-c.p);
190 if (c.inf & 0x80) goto err;
191 if (ex_class != c.xclass)
192 {
193 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_CLASS);
194 goto err;
195 }
196 if (ex_tag != c.tag)
197 {
198 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_TAG);
199 goto err;
200 }
201 if ((c.slen+c.p) > c.max)
202 {
203 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR);
204 goto err;
205 }
206 /* check for infinite constructed - it can be as long
207 * as the amount of data passed to us */
208 if (c.inf == (V_ASN1_CONSTRUCTED+1))
209 c.slen=length+ *pp-c.p;
210 c.max=c.p+c.slen;
211
212 while (c.p < c.max)
213 {
214 char *s;
215
216 if (M_ASN1_D2I_end_sequence()) break;
217 /* XXX: This was called with 4 arguments, incorrectly, it seems
218 if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) */
219 if ((s=d2i(NULL,&c.p,c.slen)) == NULL)
220 {
221 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_ERROR_PARSING_SET_ELEMENT);
222 asn1_add_error(*pp,(int)(c.p- *pp));
223 goto err;
224 }
225 if (!sk_OPENSSL_BLOCK_push(ret,s)) goto err;
226 }
227 if (a != NULL) (*a)=ret;
228 *pp=c.p;
229 return(ret);
230err:
231 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
232 {
233 if (free_func != NULL)
234 sk_OPENSSL_BLOCK_pop_free(ret,free_func);
235 else
236 sk_OPENSSL_BLOCK_free(ret);
237 }
238 return(NULL);
239 }
240
241#endif
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
deleted file mode 100644
index ff63bfc7be..0000000000
--- a/src/lib/libcrypto/asn1/a_sign.c
+++ /dev/null
@@ -1,298 +0,0 @@
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 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#include <stdio.h>
113#include <time.h>
114
115#include "cryptlib.h"
116
117#ifndef NO_SYS_TYPES_H
118# include <sys/types.h>
119#endif
120
121#include <openssl/bn.h>
122#include <openssl/evp.h>
123#include <openssl/x509.h>
124#include <openssl/objects.h>
125#include <openssl/buffer.h>
126#include "asn1_locl.h"
127
128#ifndef NO_ASN1_OLD
129
130int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
131 ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey,
132 const EVP_MD *type)
133 {
134 EVP_MD_CTX ctx;
135 unsigned char *p,*buf_in=NULL,*buf_out=NULL;
136 int i,inl=0,outl=0,outll=0;
137 X509_ALGOR *a;
138
139 EVP_MD_CTX_init(&ctx);
140 for (i=0; i<2; i++)
141 {
142 if (i == 0)
143 a=algor1;
144 else
145 a=algor2;
146 if (a == NULL) continue;
147 if (type->pkey_type == NID_dsaWithSHA1)
148 {
149 /* special case: RFC 2459 tells us to omit 'parameters'
150 * with id-dsa-with-sha1 */
151 ASN1_TYPE_free(a->parameter);
152 a->parameter = NULL;
153 }
154 else if ((a->parameter == NULL) ||
155 (a->parameter->type != V_ASN1_NULL))
156 {
157 ASN1_TYPE_free(a->parameter);
158 if ((a->parameter=ASN1_TYPE_new()) == NULL) goto err;
159 a->parameter->type=V_ASN1_NULL;
160 }
161 ASN1_OBJECT_free(a->algorithm);
162 a->algorithm=OBJ_nid2obj(type->pkey_type);
163 if (a->algorithm == NULL)
164 {
165 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_UNKNOWN_OBJECT_TYPE);
166 goto err;
167 }
168 if (a->algorithm->length == 0)
169 {
170 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
171 goto err;
172 }
173 }
174 inl=i2d(data,NULL);
175 buf_in=(unsigned char *)OPENSSL_malloc((unsigned int)inl);
176 outll=outl=EVP_PKEY_size(pkey);
177 buf_out=(unsigned char *)OPENSSL_malloc((unsigned int)outl);
178 if ((buf_in == NULL) || (buf_out == NULL))
179 {
180 outl=0;
181 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_MALLOC_FAILURE);
182 goto err;
183 }
184 p=buf_in;
185
186 i2d(data,&p);
187 EVP_SignInit_ex(&ctx,type, NULL);
188 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
189 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
190 (unsigned int *)&outl,pkey))
191 {
192 outl=0;
193 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_EVP_LIB);
194 goto err;
195 }
196 if (signature->data != NULL) OPENSSL_free(signature->data);
197 signature->data=buf_out;
198 buf_out=NULL;
199 signature->length=outl;
200 /* In the interests of compatibility, I'll make sure that
201 * the bit string has a 'not-used bits' value of 0
202 */
203 signature->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
204 signature->flags|=ASN1_STRING_FLAG_BITS_LEFT;
205err:
206 EVP_MD_CTX_cleanup(&ctx);
207 if (buf_in != NULL)
208 { OPENSSL_cleanse((char *)buf_in,(unsigned int)inl); OPENSSL_free(buf_in); }
209 if (buf_out != NULL)
210 { OPENSSL_cleanse((char *)buf_out,outll); OPENSSL_free(buf_out); }
211 return(outl);
212 }
213
214#endif
215
216int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
217 ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey,
218 const EVP_MD *type)
219 {
220 EVP_MD_CTX ctx;
221 unsigned char *buf_in=NULL,*buf_out=NULL;
222 int inl=0,outl=0,outll=0;
223 int signid, paramtype;
224
225 if (type == NULL)
226 {
227 int def_nid;
228 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
229 type = EVP_get_digestbynid(def_nid);
230 }
231
232 if (type == NULL)
233 {
234 ASN1err(ASN1_F_ASN1_ITEM_SIGN, ASN1_R_NO_DEFAULT_DIGEST);
235 return 0;
236 }
237
238 if (type->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
239 {
240 if (!pkey->ameth ||
241 !OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(type),
242 pkey->ameth->pkey_id))
243 {
244 ASN1err(ASN1_F_ASN1_ITEM_SIGN,
245 ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
246 return 0;
247 }
248 }
249 else
250 signid = type->pkey_type;
251
252 if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
253 paramtype = V_ASN1_NULL;
254 else
255 paramtype = V_ASN1_UNDEF;
256
257 if (algor1)
258 X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
259 if (algor2)
260 X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
261
262 EVP_MD_CTX_init(&ctx);
263 inl=ASN1_item_i2d(asn,&buf_in, it);
264 outll=outl=EVP_PKEY_size(pkey);
265 buf_out=(unsigned char *)OPENSSL_malloc((unsigned int)outl);
266 if ((buf_in == NULL) || (buf_out == NULL))
267 {
268 outl=0;
269 ASN1err(ASN1_F_ASN1_ITEM_SIGN,ERR_R_MALLOC_FAILURE);
270 goto err;
271 }
272
273 EVP_SignInit_ex(&ctx,type, NULL);
274 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
275 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
276 (unsigned int *)&outl,pkey))
277 {
278 outl=0;
279 ASN1err(ASN1_F_ASN1_ITEM_SIGN,ERR_R_EVP_LIB);
280 goto err;
281 }
282 if (signature->data != NULL) OPENSSL_free(signature->data);
283 signature->data=buf_out;
284 buf_out=NULL;
285 signature->length=outl;
286 /* In the interests of compatibility, I'll make sure that
287 * the bit string has a 'not-used bits' value of 0
288 */
289 signature->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
290 signature->flags|=ASN1_STRING_FLAG_BITS_LEFT;
291err:
292 EVP_MD_CTX_cleanup(&ctx);
293 if (buf_in != NULL)
294 { OPENSSL_cleanse((char *)buf_in,(unsigned int)inl); OPENSSL_free(buf_in); }
295 if (buf_out != NULL)
296 { OPENSSL_cleanse((char *)buf_out,outll); OPENSSL_free(buf_out); }
297 return(outl);
298 }
diff --git a/src/lib/libcrypto/asn1/a_strex.c b/src/lib/libcrypto/asn1/a_strex.c
deleted file mode 100644
index 264ebf2393..0000000000
--- a/src/lib/libcrypto/asn1/a_strex.c
+++ /dev/null
@@ -1,574 +0,0 @@
1/* a_strex.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include "cryptlib.h"
62#include <openssl/crypto.h>
63#include <openssl/x509.h>
64#include <openssl/asn1.h>
65
66#include "charmap.h"
67
68/* ASN1_STRING_print_ex() and X509_NAME_print_ex().
69 * Enhanced string and name printing routines handling
70 * multibyte characters, RFC2253 and a host of other
71 * options.
72 */
73
74
75#define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
76
77#define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
78 ASN1_STRFLGS_ESC_QUOTE | \
79 ASN1_STRFLGS_ESC_CTRL | \
80 ASN1_STRFLGS_ESC_MSB)
81
82
83/* Three IO functions for sending data to memory, a BIO and
84 * and a FILE pointer.
85 */
86#if 0 /* never used */
87static int send_mem_chars(void *arg, const void *buf, int len)
88{
89 unsigned char **out = arg;
90 if(!out) return 1;
91 memcpy(*out, buf, len);
92 *out += len;
93 return 1;
94}
95#endif
96
97static int send_bio_chars(void *arg, const void *buf, int len)
98{
99 if(!arg) return 1;
100 if(BIO_write(arg, buf, len) != len) return 0;
101 return 1;
102}
103
104static int send_fp_chars(void *arg, const void *buf, int len)
105{
106 if(!arg) return 1;
107 if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
108 return 1;
109}
110
111typedef int char_io(void *arg, const void *buf, int len);
112
113/* This function handles display of
114 * strings, one character at a time.
115 * It is passed an unsigned long for each
116 * character because it could come from 2 or even
117 * 4 byte forms.
118 */
119
120static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
121{
122 unsigned char chflgs, chtmp;
123 char tmphex[HEX_SIZE(long)+3];
124
125 if(c > 0xffffffffL)
126 return -1;
127 if(c > 0xffff) {
128 BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
129 if(!io_ch(arg, tmphex, 10)) return -1;
130 return 10;
131 }
132 if(c > 0xff) {
133 BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
134 if(!io_ch(arg, tmphex, 6)) return -1;
135 return 6;
136 }
137 chtmp = (unsigned char)c;
138 if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
139 else chflgs = char_type[chtmp] & flags;
140 if(chflgs & CHARTYPE_BS_ESC) {
141 /* If we don't escape with quotes, signal we need quotes */
142 if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
143 if(do_quotes) *do_quotes = 1;
144 if(!io_ch(arg, &chtmp, 1)) return -1;
145 return 1;
146 }
147 if(!io_ch(arg, "\\", 1)) return -1;
148 if(!io_ch(arg, &chtmp, 1)) return -1;
149 return 2;
150 }
151 if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
152 BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
153 if(!io_ch(arg, tmphex, 3)) return -1;
154 return 3;
155 }
156 /* If we get this far and do any escaping at all must escape
157 * the escape character itself: backslash.
158 */
159 if (chtmp == '\\' && flags & ESC_FLAGS) {
160 if(!io_ch(arg, "\\\\", 2)) return -1;
161 return 2;
162 }
163 if(!io_ch(arg, &chtmp, 1)) return -1;
164 return 1;
165}
166
167#define BUF_TYPE_WIDTH_MASK 0x7
168#define BUF_TYPE_CONVUTF8 0x8
169
170/* This function sends each character in a buffer to
171 * do_esc_char(). It interprets the content formats
172 * and converts to or from UTF8 as appropriate.
173 */
174
175static int do_buf(unsigned char *buf, int buflen,
176 int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
177{
178 int i, outlen, len;
179 unsigned char orflags, *p, *q;
180 unsigned long c;
181 p = buf;
182 q = buf + buflen;
183 outlen = 0;
184 while(p != q) {
185 if(p == buf && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_FIRST_ESC_2253;
186 else orflags = 0;
187 switch(type & BUF_TYPE_WIDTH_MASK) {
188 case 4:
189 c = ((unsigned long)*p++) << 24;
190 c |= ((unsigned long)*p++) << 16;
191 c |= ((unsigned long)*p++) << 8;
192 c |= *p++;
193 break;
194
195 case 2:
196 c = ((unsigned long)*p++) << 8;
197 c |= *p++;
198 break;
199
200 case 1:
201 c = *p++;
202 break;
203
204 case 0:
205 i = UTF8_getc(p, buflen, &c);
206 if(i < 0) return -1; /* Invalid UTF8String */
207 p += i;
208 break;
209 default:
210 return -1; /* invalid width */
211 }
212 if (p == q && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_LAST_ESC_2253;
213 if(type & BUF_TYPE_CONVUTF8) {
214 unsigned char utfbuf[6];
215 int utflen;
216 utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
217 for(i = 0; i < utflen; i++) {
218 /* We don't need to worry about setting orflags correctly
219 * because if utflen==1 its value will be correct anyway
220 * otherwise each character will be > 0x7f and so the
221 * character will never be escaped on first and last.
222 */
223 len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
224 if(len < 0) return -1;
225 outlen += len;
226 }
227 } else {
228 len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
229 if(len < 0) return -1;
230 outlen += len;
231 }
232 }
233 return outlen;
234}
235
236/* This function hex dumps a buffer of characters */
237
238static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
239{
240 static const char hexdig[] = "0123456789ABCDEF";
241 unsigned char *p, *q;
242 char hextmp[2];
243 if(arg) {
244 p = buf;
245 q = buf + buflen;
246 while(p != q) {
247 hextmp[0] = hexdig[*p >> 4];
248 hextmp[1] = hexdig[*p & 0xf];
249 if(!io_ch(arg, hextmp, 2)) return -1;
250 p++;
251 }
252 }
253 return buflen << 1;
254}
255
256/* "dump" a string. This is done when the type is unknown,
257 * or the flags request it. We can either dump the content
258 * octets or the entire DER encoding. This uses the RFC2253
259 * #01234 format.
260 */
261
262static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
263{
264 /* Placing the ASN1_STRING in a temp ASN1_TYPE allows
265 * the DER encoding to readily obtained
266 */
267 ASN1_TYPE t;
268 unsigned char *der_buf, *p;
269 int outlen, der_len;
270
271 if(!io_ch(arg, "#", 1)) return -1;
272 /* If we don't dump DER encoding just dump content octets */
273 if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
274 outlen = do_hex_dump(io_ch, arg, str->data, str->length);
275 if(outlen < 0) return -1;
276 return outlen + 1;
277 }
278 t.type = str->type;
279 t.value.ptr = (char *)str;
280 der_len = i2d_ASN1_TYPE(&t, NULL);
281 der_buf = OPENSSL_malloc(der_len);
282 if(!der_buf) return -1;
283 p = der_buf;
284 i2d_ASN1_TYPE(&t, &p);
285 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
286 OPENSSL_free(der_buf);
287 if(outlen < 0) return -1;
288 return outlen + 1;
289}
290
291/* Lookup table to convert tags to character widths,
292 * 0 = UTF8 encoded, -1 is used for non string types
293 * otherwise it is the number of bytes per character
294 */
295
296static const signed char tag2nbyte[] = {
297 -1, -1, -1, -1, -1, /* 0-4 */
298 -1, -1, -1, -1, -1, /* 5-9 */
299 -1, -1, 0, -1, /* 10-13 */
300 -1, -1, -1, -1, /* 15-17 */
301 -1, 1, 1, /* 18-20 */
302 -1, 1, 1, 1, /* 21-24 */
303 -1, 1, -1, /* 25-27 */
304 4, -1, 2 /* 28-30 */
305};
306
307/* This is the main function, print out an
308 * ASN1_STRING taking note of various escape
309 * and display options. Returns number of
310 * characters written or -1 if an error
311 * occurred.
312 */
313
314static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
315{
316 int outlen, len;
317 int type;
318 char quotes;
319 unsigned char flags;
320 quotes = 0;
321 /* Keep a copy of escape flags */
322 flags = (unsigned char)(lflags & ESC_FLAGS);
323
324 type = str->type;
325
326 outlen = 0;
327
328
329 if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
330 const char *tagname;
331 tagname = ASN1_tag2str(type);
332 outlen += strlen(tagname);
333 if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
334 outlen++;
335 }
336
337 /* Decide what to do with type, either dump content or display it */
338
339 /* Dump everything */
340 if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
341 /* Ignore the string type */
342 else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
343 else {
344 /* Else determine width based on type */
345 if((type > 0) && (type < 31)) type = tag2nbyte[type];
346 else type = -1;
347 if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
348 }
349
350 if(type == -1) {
351 len = do_dump(lflags, io_ch, arg, str);
352 if(len < 0) return -1;
353 outlen += len;
354 return outlen;
355 }
356
357 if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
358 /* Note: if string is UTF8 and we want
359 * to convert to UTF8 then we just interpret
360 * it as 1 byte per character to avoid converting
361 * twice.
362 */
363 if(!type) type = 1;
364 else type |= BUF_TYPE_CONVUTF8;
365 }
366
367 len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
368 if(len < 0) return -1;
369 outlen += len;
370 if(quotes) outlen += 2;
371 if(!arg) return outlen;
372 if(quotes && !io_ch(arg, "\"", 1)) return -1;
373 if(do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
374 return -1;
375 if(quotes && !io_ch(arg, "\"", 1)) return -1;
376 return outlen;
377}
378
379/* Used for line indenting: print 'indent' spaces */
380
381static int do_indent(char_io *io_ch, void *arg, int indent)
382{
383 int i;
384 for(i = 0; i < indent; i++)
385 if(!io_ch(arg, " ", 1)) return 0;
386 return 1;
387}
388
389#define FN_WIDTH_LN 25
390#define FN_WIDTH_SN 10
391
392static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
393 int indent, unsigned long flags)
394{
395 int i, prev = -1, orflags, cnt;
396 int fn_opt, fn_nid;
397 ASN1_OBJECT *fn;
398 ASN1_STRING *val;
399 X509_NAME_ENTRY *ent;
400 char objtmp[80];
401 const char *objbuf;
402 int outlen, len;
403 char *sep_dn, *sep_mv, *sep_eq;
404 int sep_dn_len, sep_mv_len, sep_eq_len;
405 if(indent < 0) indent = 0;
406 outlen = indent;
407 if(!do_indent(io_ch, arg, indent)) return -1;
408 switch (flags & XN_FLAG_SEP_MASK)
409 {
410 case XN_FLAG_SEP_MULTILINE:
411 sep_dn = "\n";
412 sep_dn_len = 1;
413 sep_mv = " + ";
414 sep_mv_len = 3;
415 break;
416
417 case XN_FLAG_SEP_COMMA_PLUS:
418 sep_dn = ",";
419 sep_dn_len = 1;
420 sep_mv = "+";
421 sep_mv_len = 1;
422 indent = 0;
423 break;
424
425 case XN_FLAG_SEP_CPLUS_SPC:
426 sep_dn = ", ";
427 sep_dn_len = 2;
428 sep_mv = " + ";
429 sep_mv_len = 3;
430 indent = 0;
431 break;
432
433 case XN_FLAG_SEP_SPLUS_SPC:
434 sep_dn = "; ";
435 sep_dn_len = 2;
436 sep_mv = " + ";
437 sep_mv_len = 3;
438 indent = 0;
439 break;
440
441 default:
442 return -1;
443 }
444
445 if(flags & XN_FLAG_SPC_EQ) {
446 sep_eq = " = ";
447 sep_eq_len = 3;
448 } else {
449 sep_eq = "=";
450 sep_eq_len = 1;
451 }
452
453 fn_opt = flags & XN_FLAG_FN_MASK;
454
455 cnt = X509_NAME_entry_count(n);
456 for(i = 0; i < cnt; i++) {
457 if(flags & XN_FLAG_DN_REV)
458 ent = X509_NAME_get_entry(n, cnt - i - 1);
459 else ent = X509_NAME_get_entry(n, i);
460 if(prev != -1) {
461 if(prev == ent->set) {
462 if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
463 outlen += sep_mv_len;
464 } else {
465 if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
466 outlen += sep_dn_len;
467 if(!do_indent(io_ch, arg, indent)) return -1;
468 outlen += indent;
469 }
470 }
471 prev = ent->set;
472 fn = X509_NAME_ENTRY_get_object(ent);
473 val = X509_NAME_ENTRY_get_data(ent);
474 fn_nid = OBJ_obj2nid(fn);
475 if(fn_opt != XN_FLAG_FN_NONE) {
476 int objlen, fld_len;
477 if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
478 OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
479 fld_len = 0; /* XXX: what should this be? */
480 objbuf = objtmp;
481 } else {
482 if(fn_opt == XN_FLAG_FN_SN) {
483 fld_len = FN_WIDTH_SN;
484 objbuf = OBJ_nid2sn(fn_nid);
485 } else if(fn_opt == XN_FLAG_FN_LN) {
486 fld_len = FN_WIDTH_LN;
487 objbuf = OBJ_nid2ln(fn_nid);
488 } else {
489 fld_len = 0; /* XXX: what should this be? */
490 objbuf = "";
491 }
492 }
493 objlen = strlen(objbuf);
494 if(!io_ch(arg, objbuf, objlen)) return -1;
495 if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
496 if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
497 outlen += fld_len - objlen;
498 }
499 if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
500 outlen += objlen + sep_eq_len;
501 }
502 /* If the field name is unknown then fix up the DER dump
503 * flag. We might want to limit this further so it will
504 * DER dump on anything other than a few 'standard' fields.
505 */
506 if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
507 orflags = ASN1_STRFLGS_DUMP_ALL;
508 else orflags = 0;
509
510 len = do_print_ex(io_ch, arg, flags | orflags, val);
511 if(len < 0) return -1;
512 outlen += len;
513 }
514 return outlen;
515}
516
517/* Wrappers round the main functions */
518
519int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
520{
521 if(flags == XN_FLAG_COMPAT)
522 return X509_NAME_print(out, nm, indent);
523 return do_name_ex(send_bio_chars, out, nm, indent, flags);
524}
525
526#ifndef OPENSSL_NO_FP_API
527int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
528{
529 if(flags == XN_FLAG_COMPAT)
530 {
531 BIO *btmp;
532 int ret;
533 btmp = BIO_new_fp(fp, BIO_NOCLOSE);
534 if(!btmp) return -1;
535 ret = X509_NAME_print(btmp, nm, indent);
536 BIO_free(btmp);
537 return ret;
538 }
539 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
540}
541#endif
542
543int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
544{
545 return do_print_ex(send_bio_chars, out, flags, str);
546}
547
548#ifndef OPENSSL_NO_FP_API
549int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
550{
551 return do_print_ex(send_fp_chars, fp, flags, str);
552}
553#endif
554
555/* Utility function: convert any string type to UTF8, returns number of bytes
556 * in output string or a negative error code
557 */
558
559int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
560{
561 ASN1_STRING stmp, *str = &stmp;
562 int mbflag, type, ret;
563 if(!in) return -1;
564 type = in->type;
565 if((type < 0) || (type > 30)) return -1;
566 mbflag = tag2nbyte[type];
567 if(mbflag == -1) return -1;
568 mbflag |= MBSTRING_FLAG;
569 stmp.data = NULL;
570 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
571 if(ret < 0) return ret;
572 *out = stmp.data;
573 return stmp.length;
574}
diff --git a/src/lib/libcrypto/asn1/a_strnid.c b/src/lib/libcrypto/asn1/a_strnid.c
deleted file mode 100644
index 2fc48c1551..0000000000
--- a/src/lib/libcrypto/asn1/a_strnid.c
+++ /dev/null
@@ -1,290 +0,0 @@
1/* a_strnid.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include "cryptlib.h"
62#include <openssl/asn1.h>
63#include <openssl/objects.h>
64
65
66static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67static void st_free(ASN1_STRING_TABLE *tbl);
68static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
69 const ASN1_STRING_TABLE * const *b);
70
71
72/* This is the global mask for the mbstring functions: this is use to
73 * mask out certain types (such as BMPString and UTF8String) because
74 * certain software (e.g. Netscape) has problems with them.
75 */
76
77static unsigned long global_mask = 0xFFFFFFFFL;
78
79void ASN1_STRING_set_default_mask(unsigned long mask)
80{
81 global_mask = mask;
82}
83
84unsigned long ASN1_STRING_get_default_mask(void)
85{
86 return global_mask;
87}
88
89/* This function sets the default to various "flavours" of configuration.
90 * based on an ASCII string. Currently this is:
91 * MASK:XXXX : a numerical mask value.
92 * nobmp : Don't use BMPStrings (just Printable, T61).
93 * pkix : PKIX recommendation in RFC2459.
94 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
95 * default: the default value, Printable, T61, BMP.
96 */
97
98int ASN1_STRING_set_default_mask_asc(const char *p)
99{
100 unsigned long mask;
101 char *end;
102 if(!strncmp(p, "MASK:", 5)) {
103 if(!p[5]) return 0;
104 mask = strtoul(p + 5, &end, 0);
105 if(*end) return 0;
106 } else if(!strcmp(p, "nombstr"))
107 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
108 else if(!strcmp(p, "pkix"))
109 mask = ~((unsigned long)B_ASN1_T61STRING);
110 else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING;
111 else if(!strcmp(p, "default"))
112 mask = 0xFFFFFFFFL;
113 else return 0;
114 ASN1_STRING_set_default_mask(mask);
115 return 1;
116}
117
118/* The following function generates an ASN1_STRING based on limits in a table.
119 * Frequently the types and length of an ASN1_STRING are restricted by a
120 * corresponding OID. For example certificates and certificate requests.
121 */
122
123ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
124 int inlen, int inform, int nid)
125{
126 ASN1_STRING_TABLE *tbl;
127 ASN1_STRING *str = NULL;
128 unsigned long mask;
129 int ret;
130 if(!out) out = &str;
131 tbl = ASN1_STRING_TABLE_get(nid);
132 if(tbl) {
133 mask = tbl->mask;
134 if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask;
135 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
136 tbl->minsize, tbl->maxsize);
137 } else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask);
138 if(ret <= 0) return NULL;
139 return *out;
140}
141
142/* Now the tables and helper functions for the string table:
143 */
144
145/* size limits: this stuff is taken straight from RFC3280 */
146
147#define ub_name 32768
148#define ub_common_name 64
149#define ub_locality_name 128
150#define ub_state_name 128
151#define ub_organization_name 64
152#define ub_organization_unit_name 64
153#define ub_title 64
154#define ub_email_address 128
155#define ub_serial_number 64
156
157
158/* This table must be kept in NID order */
159
160static const ASN1_STRING_TABLE tbl_standard[] = {
161{NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
162{NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
163{NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
164{NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
165{NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
166{NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
167{NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
168{NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
169{NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
170{NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
171{NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
172{NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
173{NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
174{NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
175{NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
176{NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
177{NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
178{NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
179{NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
180};
181
182static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
183 const ASN1_STRING_TABLE * const *b)
184{
185 return (*a)->nid - (*b)->nid;
186}
187
188DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
189
190static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
191{
192 return a->nid - b->nid;
193}
194
195IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
196
197ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
198{
199 int idx;
200 ASN1_STRING_TABLE *ttmp;
201 ASN1_STRING_TABLE fnd;
202 fnd.nid = nid;
203 ttmp = OBJ_bsearch_table(&fnd, tbl_standard,
204 sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE));
205 if(ttmp) return ttmp;
206 if(!stable) return NULL;
207 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
208 if(idx < 0) return NULL;
209 return sk_ASN1_STRING_TABLE_value(stable, idx);
210}
211
212int ASN1_STRING_TABLE_add(int nid,
213 long minsize, long maxsize, unsigned long mask,
214 unsigned long flags)
215{
216 ASN1_STRING_TABLE *tmp;
217 char new_nid = 0;
218 flags &= ~STABLE_FLAGS_MALLOC;
219 if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
220 if(!stable) {
221 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
222 return 0;
223 }
224 if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
225 tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
226 if(!tmp) {
227 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
228 ERR_R_MALLOC_FAILURE);
229 return 0;
230 }
231 tmp->flags = flags | STABLE_FLAGS_MALLOC;
232 tmp->nid = nid;
233 new_nid = 1;
234 } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
235 if(minsize != -1) tmp->minsize = minsize;
236 if(maxsize != -1) tmp->maxsize = maxsize;
237 tmp->mask = mask;
238 if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
239 return 1;
240}
241
242void ASN1_STRING_TABLE_cleanup(void)
243{
244 STACK_OF(ASN1_STRING_TABLE) *tmp;
245 tmp = stable;
246 if(!tmp) return;
247 stable = NULL;
248 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
249}
250
251static void st_free(ASN1_STRING_TABLE *tbl)
252{
253 if(tbl->flags & STABLE_FLAGS_MALLOC) OPENSSL_free(tbl);
254}
255
256
257IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)
258
259#ifdef STRING_TABLE_TEST
260
261main()
262{
263 ASN1_STRING_TABLE *tmp;
264 int i, last_nid = -1;
265
266 for (tmp = tbl_standard, i = 0;
267 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
268 {
269 if (tmp->nid < last_nid)
270 {
271 last_nid = 0;
272 break;
273 }
274 last_nid = tmp->nid;
275 }
276
277 if (last_nid != 0)
278 {
279 printf("Table order OK\n");
280 exit(0);
281 }
282
283 for (tmp = tbl_standard, i = 0;
284 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
285 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
286 OBJ_nid2ln(tmp->nid));
287
288}
289
290#endif
diff --git a/src/lib/libcrypto/asn1/a_time.c b/src/lib/libcrypto/asn1/a_time.c
deleted file mode 100644
index e2eb9b243e..0000000000
--- a/src/lib/libcrypto/asn1/a_time.c
+++ /dev/null
@@ -1,198 +0,0 @@
1/* crypto/asn1/a_time.c */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56
57/* This is an implementation of the ASN1 Time structure which is:
58 * Time ::= CHOICE {
59 * utcTime UTCTime,
60 * generalTime GeneralizedTime }
61 * written by Steve Henson.
62 */
63
64#include <stdio.h>
65#include <time.h>
66#include "cryptlib.h"
67#include "o_time.h"
68#include <openssl/asn1t.h>
69
70IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
71
72IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
73
74#if 0
75int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **pp)
76 {
77#ifdef CHARSET_EBCDIC
78 /* KLUDGE! We convert to ascii before writing DER */
79 char tmp[24];
80 ASN1_STRING tmpstr;
81
82 if(a->type == V_ASN1_UTCTIME || a->type == V_ASN1_GENERALIZEDTIME) {
83 int len;
84
85 tmpstr = *(ASN1_STRING *)a;
86 len = tmpstr.length;
87 ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
88 tmpstr.data = tmp;
89 a = (ASN1_GENERALIZEDTIME *) &tmpstr;
90 }
91#endif
92 if(a->type == V_ASN1_UTCTIME || a->type == V_ASN1_GENERALIZEDTIME)
93 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
94 a->type ,V_ASN1_UNIVERSAL));
95 ASN1err(ASN1_F_I2D_ASN1_TIME,ASN1_R_EXPECTING_A_TIME);
96 return -1;
97 }
98#endif
99
100
101ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
102 {
103 return ASN1_TIME_adj(s, t, 0, 0);
104 }
105
106ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
107 int offset_day, long offset_sec)
108 {
109 struct tm *ts;
110 struct tm data;
111
112 ts=OPENSSL_gmtime(&t,&data);
113 if (ts == NULL)
114 {
115 ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
116 return NULL;
117 }
118 if (offset_day || offset_sec)
119 {
120 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
121 return NULL;
122 }
123 if((ts->tm_year >= 50) && (ts->tm_year < 150))
124 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
125 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
126 }
127
128int ASN1_TIME_check(ASN1_TIME *t)
129 {
130 if (t->type == V_ASN1_GENERALIZEDTIME)
131 return ASN1_GENERALIZEDTIME_check(t);
132 else if (t->type == V_ASN1_UTCTIME)
133 return ASN1_UTCTIME_check(t);
134 return 0;
135 }
136
137/* Convert an ASN1_TIME structure to GeneralizedTime */
138ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out)
139 {
140 ASN1_GENERALIZEDTIME *ret;
141 char *str;
142 int newlen;
143
144 if (!ASN1_TIME_check(t)) return NULL;
145
146 if (!out || !*out)
147 {
148 if (!(ret = ASN1_GENERALIZEDTIME_new ()))
149 return NULL;
150 if (out) *out = ret;
151 }
152 else ret = *out;
153
154 /* If already GeneralizedTime just copy across */
155 if (t->type == V_ASN1_GENERALIZEDTIME)
156 {
157 if(!ASN1_STRING_set(ret, t->data, t->length))
158 return NULL;
159 return ret;
160 }
161
162 /* grow the string */
163 if (!ASN1_STRING_set(ret, NULL, t->length + 2))
164 return NULL;
165 /* ASN1_STRING_set() allocated 'len + 1' bytes. */
166 newlen = t->length + 2 + 1;
167 str = (char *)ret->data;
168 /* Work out the century and prepend */
169 if (t->data[0] >= '5') BUF_strlcpy(str, "19", newlen);
170 else BUF_strlcpy(str, "20", newlen);
171
172 BUF_strlcat(str, (char *)t->data, newlen);
173
174 return ret;
175 }
176
177int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
178 {
179 ASN1_TIME t;
180
181 t.length = strlen(str);
182 t.data = (unsigned char *)str;
183 t.flags = 0;
184
185 t.type = V_ASN1_UTCTIME;
186
187 if (!ASN1_TIME_check(&t))
188 {
189 t.type = V_ASN1_GENERALIZEDTIME;
190 if (!ASN1_TIME_check(&t))
191 return 0;
192 }
193
194 if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
195 return 0;
196
197 return 1;
198 }
diff --git a/src/lib/libcrypto/asn1/a_type.c b/src/lib/libcrypto/asn1/a_type.c
deleted file mode 100644
index a45d2f9d12..0000000000
--- a/src/lib/libcrypto/asn1/a_type.c
+++ /dev/null
@@ -1,159 +0,0 @@
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 <openssl/asn1t.h>
62#include <openssl/objects.h>
63
64int ASN1_TYPE_get(ASN1_TYPE *a)
65 {
66 if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
67 return(a->type);
68 else
69 return(0);
70 }
71
72void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
73 {
74 if (a->value.ptr != NULL)
75 {
76 ASN1_TYPE **tmp_a = &a;
77 ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
78 }
79 a->type=type;
80 if (type == V_ASN1_BOOLEAN)
81 a->value.boolean = value ? 0xff : 0;
82 else
83 a->value.ptr=value;
84 }
85
86int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
87 {
88 if (!value || (type == V_ASN1_BOOLEAN))
89 {
90 void *p = (void *)value;
91 ASN1_TYPE_set(a, type, p);
92 }
93 else if (type == V_ASN1_OBJECT)
94 {
95 ASN1_OBJECT *odup;
96 odup = OBJ_dup(value);
97 if (!odup)
98 return 0;
99 ASN1_TYPE_set(a, type, odup);
100 }
101 else
102 {
103 ASN1_STRING *sdup;
104 sdup = ASN1_STRING_dup(value);
105 if (!sdup)
106 return 0;
107 ASN1_TYPE_set(a, type, sdup);
108 }
109 return 1;
110 }
111
112IMPLEMENT_STACK_OF(ASN1_TYPE)
113IMPLEMENT_ASN1_SET_OF(ASN1_TYPE)
114
115/* Returns 0 if they are equal, != 0 otherwise. */
116int ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b)
117 {
118 int result = -1;
119
120 if (!a || !b || a->type != b->type) return -1;
121
122 switch (a->type)
123 {
124 case V_ASN1_OBJECT:
125 result = OBJ_cmp(a->value.object, b->value.object);
126 break;
127 case V_ASN1_NULL:
128 result = 0; /* They do not have content. */
129 break;
130 case V_ASN1_INTEGER:
131 case V_ASN1_NEG_INTEGER:
132 case V_ASN1_ENUMERATED:
133 case V_ASN1_NEG_ENUMERATED:
134 case V_ASN1_BIT_STRING:
135 case V_ASN1_OCTET_STRING:
136 case V_ASN1_SEQUENCE:
137 case V_ASN1_SET:
138 case V_ASN1_NUMERICSTRING:
139 case V_ASN1_PRINTABLESTRING:
140 case V_ASN1_T61STRING:
141 case V_ASN1_VIDEOTEXSTRING:
142 case V_ASN1_IA5STRING:
143 case V_ASN1_UTCTIME:
144 case V_ASN1_GENERALIZEDTIME:
145 case V_ASN1_GRAPHICSTRING:
146 case V_ASN1_VISIBLESTRING:
147 case V_ASN1_GENERALSTRING:
148 case V_ASN1_UNIVERSALSTRING:
149 case V_ASN1_BMPSTRING:
150 case V_ASN1_UTF8STRING:
151 case V_ASN1_OTHER:
152 default:
153 result = ASN1_STRING_cmp((ASN1_STRING *) a->value.ptr,
154 (ASN1_STRING *) b->value.ptr);
155 break;
156 }
157
158 return result;
159 }
diff --git a/src/lib/libcrypto/asn1/a_utf8.c b/src/lib/libcrypto/asn1/a_utf8.c
deleted file mode 100644
index 508e11e527..0000000000
--- a/src/lib/libcrypto/asn1/a_utf8.c
+++ /dev/null
@@ -1,211 +0,0 @@
1/* crypto/asn1/a_utf8.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1.h>
62
63
64/* UTF8 utilities */
65
66/* This parses a UTF8 string one character at a time. It is passed a pointer
67 * to the string and the length of the string. It sets 'value' to the value of
68 * the current character. It returns the number of characters read or a
69 * negative error code:
70 * -1 = string too short
71 * -2 = illegal character
72 * -3 = subsequent characters not of the form 10xxxxxx
73 * -4 = character encoded incorrectly (not minimal length).
74 */
75
76int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
77{
78 const unsigned char *p;
79 unsigned long value;
80 int ret;
81 if(len <= 0) return 0;
82 p = str;
83
84 /* Check syntax and work out the encoded value (if correct) */
85 if((*p & 0x80) == 0) {
86 value = *p++ & 0x7f;
87 ret = 1;
88 } else if((*p & 0xe0) == 0xc0) {
89 if(len < 2) return -1;
90 if((p[1] & 0xc0) != 0x80) return -3;
91 value = (*p++ & 0x1f) << 6;
92 value |= *p++ & 0x3f;
93 if(value < 0x80) return -4;
94 ret = 2;
95 } else if((*p & 0xf0) == 0xe0) {
96 if(len < 3) return -1;
97 if( ((p[1] & 0xc0) != 0x80)
98 || ((p[2] & 0xc0) != 0x80) ) return -3;
99 value = (*p++ & 0xf) << 12;
100 value |= (*p++ & 0x3f) << 6;
101 value |= *p++ & 0x3f;
102 if(value < 0x800) return -4;
103 ret = 3;
104 } else if((*p & 0xf8) == 0xf0) {
105 if(len < 4) return -1;
106 if( ((p[1] & 0xc0) != 0x80)
107 || ((p[2] & 0xc0) != 0x80)
108 || ((p[3] & 0xc0) != 0x80) ) return -3;
109 value = ((unsigned long)(*p++ & 0x7)) << 18;
110 value |= (*p++ & 0x3f) << 12;
111 value |= (*p++ & 0x3f) << 6;
112 value |= *p++ & 0x3f;
113 if(value < 0x10000) return -4;
114 ret = 4;
115 } else if((*p & 0xfc) == 0xf8) {
116 if(len < 5) return -1;
117 if( ((p[1] & 0xc0) != 0x80)
118 || ((p[2] & 0xc0) != 0x80)
119 || ((p[3] & 0xc0) != 0x80)
120 || ((p[4] & 0xc0) != 0x80) ) return -3;
121 value = ((unsigned long)(*p++ & 0x3)) << 24;
122 value |= ((unsigned long)(*p++ & 0x3f)) << 18;
123 value |= ((unsigned long)(*p++ & 0x3f)) << 12;
124 value |= (*p++ & 0x3f) << 6;
125 value |= *p++ & 0x3f;
126 if(value < 0x200000) return -4;
127 ret = 5;
128 } else if((*p & 0xfe) == 0xfc) {
129 if(len < 6) return -1;
130 if( ((p[1] & 0xc0) != 0x80)
131 || ((p[2] & 0xc0) != 0x80)
132 || ((p[3] & 0xc0) != 0x80)
133 || ((p[4] & 0xc0) != 0x80)
134 || ((p[5] & 0xc0) != 0x80) ) return -3;
135 value = ((unsigned long)(*p++ & 0x1)) << 30;
136 value |= ((unsigned long)(*p++ & 0x3f)) << 24;
137 value |= ((unsigned long)(*p++ & 0x3f)) << 18;
138 value |= ((unsigned long)(*p++ & 0x3f)) << 12;
139 value |= (*p++ & 0x3f) << 6;
140 value |= *p++ & 0x3f;
141 if(value < 0x4000000) return -4;
142 ret = 6;
143 } else return -2;
144 *val = value;
145 return ret;
146}
147
148/* This takes a character 'value' and writes the UTF8 encoded value in
149 * 'str' where 'str' is a buffer containing 'len' characters. Returns
150 * the number of characters written or -1 if 'len' is too small. 'str' can
151 * be set to NULL in which case it just returns the number of characters.
152 * It will need at most 6 characters.
153 */
154
155int UTF8_putc(unsigned char *str, int len, unsigned long value)
156{
157 if(!str) len = 6; /* Maximum we will need */
158 else if(len <= 0) return -1;
159 if(value < 0x80) {
160 if(str) *str = (unsigned char)value;
161 return 1;
162 }
163 if(value < 0x800) {
164 if(len < 2) return -1;
165 if(str) {
166 *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
167 *str = (unsigned char)((value & 0x3f) | 0x80);
168 }
169 return 2;
170 }
171 if(value < 0x10000) {
172 if(len < 3) return -1;
173 if(str) {
174 *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
175 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
176 *str = (unsigned char)((value & 0x3f) | 0x80);
177 }
178 return 3;
179 }
180 if(value < 0x200000) {
181 if(len < 4) return -1;
182 if(str) {
183 *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
184 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
185 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
186 *str = (unsigned char)((value & 0x3f) | 0x80);
187 }
188 return 4;
189 }
190 if(value < 0x4000000) {
191 if(len < 5) return -1;
192 if(str) {
193 *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
194 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
195 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
196 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
197 *str = (unsigned char)((value & 0x3f) | 0x80);
198 }
199 return 5;
200 }
201 if(len < 6) return -1;
202 if(str) {
203 *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
204 *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
205 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
206 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
207 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
208 *str = (unsigned char)((value & 0x3f) | 0x80);
209 }
210 return 6;
211}
diff --git a/src/lib/libcrypto/asn1/a_verify.c b/src/lib/libcrypto/asn1/a_verify.c
deleted file mode 100644
index cecdb13c70..0000000000
--- a/src/lib/libcrypto/asn1/a_verify.c
+++ /dev/null
@@ -1,197 +0,0 @@
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
62#include "cryptlib.h"
63#include "asn1_locl.h"
64
65#ifndef NO_SYS_TYPES_H
66# include <sys/types.h>
67#endif
68
69#include <openssl/bn.h>
70#include <openssl/x509.h>
71#include <openssl/objects.h>
72#include <openssl/buffer.h>
73#include <openssl/evp.h>
74
75#ifndef NO_ASN1_OLD
76
77int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
78 char *data, EVP_PKEY *pkey)
79 {
80 EVP_MD_CTX ctx;
81 const EVP_MD *type;
82 unsigned char *p,*buf_in=NULL;
83 int ret= -1,i,inl;
84
85 EVP_MD_CTX_init(&ctx);
86 i=OBJ_obj2nid(a->algorithm);
87 type=EVP_get_digestbyname(OBJ_nid2sn(i));
88 if (type == NULL)
89 {
90 ASN1err(ASN1_F_ASN1_VERIFY,ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
91 goto err;
92 }
93
94 inl=i2d(data,NULL);
95 buf_in=OPENSSL_malloc((unsigned int)inl);
96 if (buf_in == NULL)
97 {
98 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_MALLOC_FAILURE);
99 goto err;
100 }
101 p=buf_in;
102
103 i2d(data,&p);
104 EVP_VerifyInit_ex(&ctx,type, NULL);
105 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
106
107 OPENSSL_cleanse(buf_in,(unsigned int)inl);
108 OPENSSL_free(buf_in);
109
110 if (EVP_VerifyFinal(&ctx,(unsigned char *)signature->data,
111 (unsigned int)signature->length,pkey) <= 0)
112 {
113 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB);
114 ret=0;
115 goto err;
116 }
117 /* we don't need to zero the 'ctx' because we just checked
118 * public information */
119 /* memset(&ctx,0,sizeof(ctx)); */
120 ret=1;
121err:
122 EVP_MD_CTX_cleanup(&ctx);
123 return(ret);
124 }
125
126#endif
127
128
129int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a, ASN1_BIT_STRING *signature,
130 void *asn, EVP_PKEY *pkey)
131 {
132 EVP_MD_CTX ctx;
133 const EVP_MD *type = NULL;
134 unsigned char *buf_in=NULL;
135 int ret= -1,inl;
136
137 int mdnid, pknid;
138
139 EVP_MD_CTX_init(&ctx);
140
141 /* Convert signature OID into digest and public key OIDs */
142 if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid))
143 {
144 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
145 goto err;
146 }
147 type=EVP_get_digestbynid(mdnid);
148 if (type == NULL)
149 {
150 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
151 goto err;
152 }
153
154 /* Check public key OID matches public key type */
155 if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id)
156 {
157 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ASN1_R_WRONG_PUBLIC_KEY_TYPE);
158 goto err;
159 }
160
161 if (!EVP_VerifyInit_ex(&ctx,type, NULL))
162 {
163 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ERR_R_EVP_LIB);
164 ret=0;
165 goto err;
166 }
167
168 inl = ASN1_item_i2d(asn, &buf_in, it);
169
170 if (buf_in == NULL)
171 {
172 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ERR_R_MALLOC_FAILURE);
173 goto err;
174 }
175
176 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
177
178 OPENSSL_cleanse(buf_in,(unsigned int)inl);
179 OPENSSL_free(buf_in);
180
181 if (EVP_VerifyFinal(&ctx,(unsigned char *)signature->data,
182 (unsigned int)signature->length,pkey) <= 0)
183 {
184 ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ERR_R_EVP_LIB);
185 ret=0;
186 goto err;
187 }
188 /* we don't need to zero the 'ctx' because we just checked
189 * public information */
190 /* memset(&ctx,0,sizeof(ctx)); */
191 ret=1;
192err:
193 EVP_MD_CTX_cleanup(&ctx);
194 return(ret);
195 }
196
197
diff --git a/src/lib/libcrypto/asn1/ameth_lib.c b/src/lib/libcrypto/asn1/ameth_lib.c
deleted file mode 100644
index 5a581b90ea..0000000000
--- a/src/lib/libcrypto/asn1/ameth_lib.c
+++ /dev/null
@@ -1,450 +0,0 @@
1/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2 * project 2006.
3 */
4/* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
58#include <stdio.h>
59#include "cryptlib.h"
60#include <openssl/asn1t.h>
61#include <openssl/x509.h>
62#ifndef OPENSSL_NO_ENGINE
63#include <openssl/engine.h>
64#endif
65#include "asn1_locl.h"
66
67extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[];
68extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
69extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
70extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
71extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
72
73/* Keep this sorted in type order !! */
74static const EVP_PKEY_ASN1_METHOD *standard_methods[] =
75 {
76#ifndef OPENSSL_NO_RSA
77 &rsa_asn1_meths[0],
78 &rsa_asn1_meths[1],
79#endif
80#ifndef OPENSSL_NO_DH
81 &dh_asn1_meth,
82#endif
83#ifndef OPENSSL_NO_DSA
84 &dsa_asn1_meths[0],
85 &dsa_asn1_meths[1],
86 &dsa_asn1_meths[2],
87 &dsa_asn1_meths[3],
88 &dsa_asn1_meths[4],
89#endif
90#ifndef OPENSSL_NO_EC
91 &eckey_asn1_meth,
92#endif
93 &hmac_asn1_meth
94 };
95
96typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
97DECLARE_STACK_OF(EVP_PKEY_ASN1_METHOD)
98static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
99
100
101
102#ifdef TEST
103void main()
104 {
105 int i;
106 for (i = 0;
107 i < sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *);
108 i++)
109 fprintf(stderr, "Number %d id=%d (%s)\n", i,
110 standard_methods[i]->pkey_id,
111 OBJ_nid2sn(standard_methods[i]->pkey_id));
112 }
113#endif
114
115DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
116 const EVP_PKEY_ASN1_METHOD *, ameth);
117
118static int ameth_cmp(const EVP_PKEY_ASN1_METHOD * const *a,
119 const EVP_PKEY_ASN1_METHOD * const *b)
120 {
121 return ((*a)->pkey_id - (*b)->pkey_id);
122 }
123
124IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
125 const EVP_PKEY_ASN1_METHOD *, ameth);
126
127int EVP_PKEY_asn1_get_count(void)
128 {
129 int num = sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *);
130 if (app_methods)
131 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
132 return num;
133 }
134
135const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
136 {
137 int num = sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *);
138 if (idx < 0)
139 return NULL;
140 if (idx < num)
141 return standard_methods[idx];
142 idx -= num;
143 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
144 }
145
146static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
147 {
148 EVP_PKEY_ASN1_METHOD tmp;
149 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
150 tmp.pkey_id = type;
151 if (app_methods)
152 {
153 int idx;
154 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
155 if (idx >= 0)
156 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
157 }
158 ret = OBJ_bsearch_ameth(&t, standard_methods,
159 sizeof(standard_methods)
160 /sizeof(EVP_PKEY_ASN1_METHOD *));
161 if (!ret || !*ret)
162 return NULL;
163 return *ret;
164 }
165
166/* Find an implementation of an ASN1 algorithm. If 'pe' is not NULL
167 * also search through engines and set *pe to a functional reference
168 * to the engine implementing 'type' or NULL if no engine implements
169 * it.
170 */
171
172const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
173 {
174 const EVP_PKEY_ASN1_METHOD *t;
175
176 for (;;)
177 {
178 t = pkey_asn1_find(type);
179 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
180 break;
181 type = t->pkey_base_id;
182 }
183 if (pe)
184 {
185#ifndef OPENSSL_NO_ENGINE
186 ENGINE *e;
187 /* type will contain the final unaliased type */
188 e = ENGINE_get_pkey_asn1_meth_engine(type);
189 if (e)
190 {
191 *pe = e;
192 return ENGINE_get_pkey_asn1_meth(e, type);
193 }
194#endif
195 *pe = NULL;
196 }
197 return t;
198 }
199
200const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
201 const char *str, int len)
202 {
203 int i;
204 const EVP_PKEY_ASN1_METHOD *ameth;
205 if (len == -1)
206 len = strlen(str);
207 if (pe)
208 {
209#ifndef OPENSSL_NO_ENGINE
210 ENGINE *e;
211 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
212 if (ameth)
213 {
214 /* Convert structural into
215 * functional reference
216 */
217 if (!ENGINE_init(e))
218 ameth = NULL;
219 ENGINE_free(e);
220 *pe = e;
221 return ameth;
222 }
223#endif
224 *pe = NULL;
225 }
226 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++)
227 {
228 ameth = EVP_PKEY_asn1_get0(i);
229 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
230 continue;
231 if (((int)strlen(ameth->pem_str) == len) &&
232 !strncasecmp(ameth->pem_str, str, len))
233 return ameth;
234 }
235 return NULL;
236 }
237
238int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
239 {
240 if (app_methods == NULL)
241 {
242 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
243 if (!app_methods)
244 return 0;
245 }
246 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
247 return 0;
248 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
249 return 1;
250 }
251
252int EVP_PKEY_asn1_add_alias(int to, int from)
253 {
254 EVP_PKEY_ASN1_METHOD *ameth;
255 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
256 if (!ameth)
257 return 0;
258 ameth->pkey_base_id = to;
259 return EVP_PKEY_asn1_add0(ameth);
260 }
261
262int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id, int *ppkey_flags,
263 const char **pinfo, const char **ppem_str,
264 const EVP_PKEY_ASN1_METHOD *ameth)
265 {
266 if (!ameth)
267 return 0;
268 if (ppkey_id)
269 *ppkey_id = ameth->pkey_id;
270 if (ppkey_base_id)
271 *ppkey_base_id = ameth->pkey_base_id;
272 if (ppkey_flags)
273 *ppkey_flags = ameth->pkey_flags;
274 if (pinfo)
275 *pinfo = ameth->info;
276 if (ppem_str)
277 *ppem_str = ameth->pem_str;
278 return 1;
279 }
280
281const EVP_PKEY_ASN1_METHOD* EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
282 {
283 return pkey->ameth;
284 }
285
286EVP_PKEY_ASN1_METHOD* EVP_PKEY_asn1_new(int id, int flags,
287 const char *pem_str, const char *info)
288 {
289 EVP_PKEY_ASN1_METHOD *ameth;
290 ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD));
291 if (!ameth)
292 return NULL;
293
294 ameth->pkey_id = id;
295 ameth->pkey_base_id = id;
296 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
297
298 if (info)
299 {
300 ameth->info = BUF_strdup(info);
301 if (!ameth->info)
302 goto err;
303 }
304 else
305 ameth->info = NULL;
306
307 if (pem_str)
308 {
309 ameth->pem_str = BUF_strdup(pem_str);
310 if (!ameth->pem_str)
311 goto err;
312 }
313 else
314 ameth->pem_str = NULL;
315
316 ameth->pub_decode = 0;
317 ameth->pub_encode = 0;
318 ameth->pub_cmp = 0;
319 ameth->pub_print = 0;
320
321 ameth->priv_decode = 0;
322 ameth->priv_encode = 0;
323 ameth->priv_print = 0;
324
325 ameth->old_priv_encode = 0;
326 ameth->old_priv_decode = 0;
327
328 ameth->pkey_size = 0;
329 ameth->pkey_bits = 0;
330
331 ameth->param_decode = 0;
332 ameth->param_encode = 0;
333 ameth->param_missing = 0;
334 ameth->param_copy = 0;
335 ameth->param_cmp = 0;
336 ameth->param_print = 0;
337
338 ameth->pkey_free = 0;
339 ameth->pkey_ctrl = 0;
340
341 return ameth;
342
343 err:
344
345 EVP_PKEY_asn1_free(ameth);
346 return NULL;
347
348 }
349
350void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
351 const EVP_PKEY_ASN1_METHOD *src)
352 {
353
354 dst->pub_decode = src->pub_decode;
355 dst->pub_encode = src->pub_encode;
356 dst->pub_cmp = src->pub_cmp;
357 dst->pub_print = src->pub_print;
358
359 dst->priv_decode = src->priv_decode;
360 dst->priv_encode = src->priv_encode;
361 dst->priv_print = src->priv_print;
362
363 dst->old_priv_encode = src->old_priv_encode;
364 dst->old_priv_decode = src->old_priv_decode;
365
366 dst->pkey_size = src->pkey_size;
367 dst->pkey_bits = src->pkey_bits;
368
369 dst->param_decode = src->param_decode;
370 dst->param_encode = src->param_encode;
371 dst->param_missing = src->param_missing;
372 dst->param_copy = src->param_copy;
373 dst->param_cmp = src->param_cmp;
374 dst->param_print = src->param_print;
375
376 dst->pkey_free = src->pkey_free;
377 dst->pkey_ctrl = src->pkey_ctrl;
378
379 }
380
381void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
382 {
383 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC))
384 {
385 if (ameth->pem_str)
386 OPENSSL_free(ameth->pem_str);
387 if (ameth->info)
388 OPENSSL_free(ameth->info);
389 OPENSSL_free(ameth);
390 }
391 }
392
393void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
394 int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub),
395 int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk),
396 int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b),
397 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent,
398 ASN1_PCTX *pctx),
399 int (*pkey_size)(const EVP_PKEY *pk),
400 int (*pkey_bits)(const EVP_PKEY *pk))
401 {
402 ameth->pub_decode = pub_decode;
403 ameth->pub_encode = pub_encode;
404 ameth->pub_cmp = pub_cmp;
405 ameth->pub_print = pub_print;
406 ameth->pkey_size = pkey_size;
407 ameth->pkey_bits = pkey_bits;
408 }
409
410void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
411 int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf),
412 int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk),
413 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
414 ASN1_PCTX *pctx))
415 {
416 ameth->priv_decode = priv_decode;
417 ameth->priv_encode = priv_encode;
418 ameth->priv_print = priv_print;
419 }
420
421void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
422 int (*param_decode)(EVP_PKEY *pkey,
423 const unsigned char **pder, int derlen),
424 int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder),
425 int (*param_missing)(const EVP_PKEY *pk),
426 int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from),
427 int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b),
428 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
429 ASN1_PCTX *pctx))
430 {
431 ameth->param_decode = param_decode;
432 ameth->param_encode = param_encode;
433 ameth->param_missing = param_missing;
434 ameth->param_copy = param_copy;
435 ameth->param_cmp = param_cmp;
436 ameth->param_print = param_print;
437 }
438
439void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
440 void (*pkey_free)(EVP_PKEY *pkey))
441 {
442 ameth->pkey_free = pkey_free;
443 }
444
445void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
446 int (*pkey_ctrl)(EVP_PKEY *pkey, int op,
447 long arg1, void *arg2))
448 {
449 ameth->pkey_ctrl = pkey_ctrl;
450 }
diff --git a/src/lib/libcrypto/asn1/asn1.h b/src/lib/libcrypto/asn1/asn1.h
deleted file mode 100644
index 59540e4e79..0000000000
--- a/src/lib/libcrypto/asn1/asn1.h
+++ /dev/null
@@ -1,1402 +0,0 @@
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#include <time.h>
63#include <openssl/e_os2.h>
64#ifndef OPENSSL_NO_BIO
65#include <openssl/bio.h>
66#endif
67#include <openssl/stack.h>
68#include <openssl/safestack.h>
69
70#include <openssl/symhacks.h>
71
72#include <openssl/ossl_typ.h>
73#ifndef OPENSSL_NO_DEPRECATED
74#include <openssl/bn.h>
75#endif
76
77#ifdef OPENSSL_BUILD_SHLIBCRYPTO
78# undef OPENSSL_EXTERN
79# define OPENSSL_EXTERN OPENSSL_EXPORT
80#endif
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85
86#define V_ASN1_UNIVERSAL 0x00
87#define V_ASN1_APPLICATION 0x40
88#define V_ASN1_CONTEXT_SPECIFIC 0x80
89#define V_ASN1_PRIVATE 0xc0
90
91#define V_ASN1_CONSTRUCTED 0x20
92#define V_ASN1_PRIMITIVE_TAG 0x1f
93#define V_ASN1_PRIMATIVE_TAG 0x1f
94
95#define V_ASN1_APP_CHOOSE -2 /* let the recipient choose */
96#define V_ASN1_OTHER -3 /* used in ASN1_TYPE */
97#define V_ASN1_ANY -4 /* used in ASN1 template code */
98
99#define V_ASN1_NEG 0x100 /* negative flag */
100
101#define V_ASN1_UNDEF -1
102#define V_ASN1_EOC 0
103#define V_ASN1_BOOLEAN 1 /**/
104#define V_ASN1_INTEGER 2
105#define V_ASN1_NEG_INTEGER (2 | V_ASN1_NEG)
106#define V_ASN1_BIT_STRING 3
107#define V_ASN1_OCTET_STRING 4
108#define V_ASN1_NULL 5
109#define V_ASN1_OBJECT 6
110#define V_ASN1_OBJECT_DESCRIPTOR 7
111#define V_ASN1_EXTERNAL 8
112#define V_ASN1_REAL 9
113#define V_ASN1_ENUMERATED 10
114#define V_ASN1_NEG_ENUMERATED (10 | V_ASN1_NEG)
115#define V_ASN1_UTF8STRING 12
116#define V_ASN1_SEQUENCE 16
117#define V_ASN1_SET 17
118#define V_ASN1_NUMERICSTRING 18 /**/
119#define V_ASN1_PRINTABLESTRING 19
120#define V_ASN1_T61STRING 20
121#define V_ASN1_TELETEXSTRING 20 /* alias */
122#define V_ASN1_VIDEOTEXSTRING 21 /**/
123#define V_ASN1_IA5STRING 22
124#define V_ASN1_UTCTIME 23
125#define V_ASN1_GENERALIZEDTIME 24 /**/
126#define V_ASN1_GRAPHICSTRING 25 /**/
127#define V_ASN1_ISO64STRING 26 /**/
128#define V_ASN1_VISIBLESTRING 26 /* alias */
129#define V_ASN1_GENERALSTRING 27 /**/
130#define V_ASN1_UNIVERSALSTRING 28 /**/
131#define V_ASN1_BMPSTRING 30
132
133/* For use with d2i_ASN1_type_bytes() */
134#define B_ASN1_NUMERICSTRING 0x0001
135#define B_ASN1_PRINTABLESTRING 0x0002
136#define B_ASN1_T61STRING 0x0004
137#define B_ASN1_TELETEXSTRING 0x0004
138#define B_ASN1_VIDEOTEXSTRING 0x0008
139#define B_ASN1_IA5STRING 0x0010
140#define B_ASN1_GRAPHICSTRING 0x0020
141#define B_ASN1_ISO64STRING 0x0040
142#define B_ASN1_VISIBLESTRING 0x0040
143#define B_ASN1_GENERALSTRING 0x0080
144#define B_ASN1_UNIVERSALSTRING 0x0100
145#define B_ASN1_OCTET_STRING 0x0200
146#define B_ASN1_BIT_STRING 0x0400
147#define B_ASN1_BMPSTRING 0x0800
148#define B_ASN1_UNKNOWN 0x1000
149#define B_ASN1_UTF8STRING 0x2000
150#define B_ASN1_UTCTIME 0x4000
151#define B_ASN1_GENERALIZEDTIME 0x8000
152#define B_ASN1_SEQUENCE 0x10000
153
154/* For use with ASN1_mbstring_copy() */
155#define MBSTRING_FLAG 0x1000
156#define MBSTRING_UTF8 (MBSTRING_FLAG)
157#define MBSTRING_ASC (MBSTRING_FLAG|1)
158#define MBSTRING_BMP (MBSTRING_FLAG|2)
159#define MBSTRING_UNIV (MBSTRING_FLAG|4)
160
161#define SMIME_OLDMIME 0x400
162#define SMIME_CRLFEOL 0x800
163#define SMIME_STREAM 0x1000
164
165struct X509_algor_st;
166DECLARE_STACK_OF(X509_ALGOR)
167
168#define DECLARE_ASN1_SET_OF(type) /* filled in by mkstack.pl */
169#define IMPLEMENT_ASN1_SET_OF(type) /* nothing, no longer needed */
170
171/* We MUST make sure that, except for constness, asn1_ctx_st and
172 asn1_const_ctx are exactly the same. Fortunately, as soon as
173 the old ASN1 parsing macros are gone, we can throw this away
174 as well... */
175typedef struct asn1_ctx_st
176 {
177 unsigned char *p;/* work char pointer */
178 int eos; /* end of sequence read for indefinite encoding */
179 int error; /* error code to use when returning an error */
180 int inf; /* constructed if 0x20, indefinite is 0x21 */
181 int tag; /* tag from last 'get object' */
182 int xclass; /* class from last 'get object' */
183 long slen; /* length of last 'get object' */
184 unsigned char *max; /* largest value of p allowed */
185 unsigned char *q;/* temporary variable */
186 unsigned char **pp;/* variable */
187 int line; /* used in error processing */
188 } ASN1_CTX;
189
190typedef struct asn1_const_ctx_st
191 {
192 const unsigned char *p;/* work char pointer */
193 int eos; /* end of sequence read for indefinite encoding */
194 int error; /* error code to use when returning an error */
195 int inf; /* constructed if 0x20, indefinite is 0x21 */
196 int tag; /* tag from last 'get object' */
197 int xclass; /* class from last 'get object' */
198 long slen; /* length of last 'get object' */
199 const unsigned char *max; /* largest value of p allowed */
200 const unsigned char *q;/* temporary variable */
201 const unsigned char **pp;/* variable */
202 int line; /* used in error processing */
203 } ASN1_const_CTX;
204
205/* These are used internally in the ASN1_OBJECT to keep track of
206 * whether the names and data need to be free()ed */
207#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
208#define ASN1_OBJECT_FLAG_CRITICAL 0x02 /* critical x509v3 object id */
209#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
210#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
211typedef struct asn1_object_st
212 {
213 const char *sn,*ln;
214 int nid;
215 int length;
216 const unsigned char *data; /* data remains const after init */
217 int flags; /* Should we free this one */
218 } ASN1_OBJECT;
219
220#define ASN1_STRING_FLAG_BITS_LEFT 0x08 /* Set if 0x07 has bits left value */
221/* This indicates that the ASN1_STRING is not a real value but just a place
222 * holder for the location where indefinite length constructed data should
223 * be inserted in the memory buffer
224 */
225#define ASN1_STRING_FLAG_NDEF 0x010
226
227/* This flag is used by the CMS code to indicate that a string is not
228 * complete and is a place holder for content when it had all been
229 * accessed. The flag will be reset when content has been written to it.
230 */
231
232#define ASN1_STRING_FLAG_CONT 0x020
233/* This flag is used by ASN1 code to indicate an ASN1_STRING is an MSTRING
234 * type.
235 */
236#define ASN1_STRING_FLAG_MSTRING 0x040
237/* This is the base type that holds just about everything :-) */
238typedef struct asn1_string_st
239 {
240 int length;
241 int type;
242 unsigned char *data;
243 /* The value of the following field depends on the type being
244 * held. It is mostly being used for BIT_STRING so if the
245 * input data has a non-zero 'unused bits' value, it will be
246 * handled correctly */
247 long flags;
248 } ASN1_STRING;
249
250/* ASN1_ENCODING structure: this is used to save the received
251 * encoding of an ASN1 type. This is useful to get round
252 * problems with invalid encodings which can break signatures.
253 */
254
255typedef struct ASN1_ENCODING_st
256 {
257 unsigned char *enc; /* DER encoding */
258 long len; /* Length of encoding */
259 int modified; /* set to 1 if 'enc' is invalid */
260 } ASN1_ENCODING;
261
262/* Used with ASN1 LONG type: if a long is set to this it is omitted */
263#define ASN1_LONG_UNDEF 0x7fffffffL
264
265#define STABLE_FLAGS_MALLOC 0x01
266#define STABLE_NO_MASK 0x02
267#define DIRSTRING_TYPE \
268 (B_ASN1_PRINTABLESTRING|B_ASN1_T61STRING|B_ASN1_BMPSTRING|B_ASN1_UTF8STRING)
269#define PKCS9STRING_TYPE (DIRSTRING_TYPE|B_ASN1_IA5STRING)
270
271typedef struct asn1_string_table_st {
272 int nid;
273 long minsize;
274 long maxsize;
275 unsigned long mask;
276 unsigned long flags;
277} ASN1_STRING_TABLE;
278
279DECLARE_STACK_OF(ASN1_STRING_TABLE)
280
281/* size limits: this stuff is taken straight from RFC2459 */
282
283#define ub_name 32768
284#define ub_common_name 64
285#define ub_locality_name 128
286#define ub_state_name 128
287#define ub_organization_name 64
288#define ub_organization_unit_name 64
289#define ub_title 64
290#define ub_email_address 128
291
292/* Declarations for template structures: for full definitions
293 * see asn1t.h
294 */
295typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE;
296typedef struct ASN1_ITEM_st ASN1_ITEM;
297typedef struct ASN1_TLC_st ASN1_TLC;
298/* This is just an opaque pointer */
299typedef struct ASN1_VALUE_st ASN1_VALUE;
300
301/* Declare ASN1 functions: the implement macro in in asn1t.h */
302
303#define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type)
304
305#define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \
306 DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type)
307
308#define DECLARE_ASN1_FUNCTIONS_name(type, name) \
309 DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
310 DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name)
311
312#define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \
313 DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
314 DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name)
315
316#define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \
317 type *d2i_##name(type **a, const unsigned char **in, long len); \
318 int i2d_##name(type *a, unsigned char **out); \
319 DECLARE_ASN1_ITEM(itname)
320
321#define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \
322 type *d2i_##name(type **a, const unsigned char **in, long len); \
323 int i2d_##name(const type *a, unsigned char **out); \
324 DECLARE_ASN1_ITEM(name)
325
326#define DECLARE_ASN1_NDEF_FUNCTION(name) \
327 int i2d_##name##_NDEF(name *a, unsigned char **out);
328
329#define DECLARE_ASN1_FUNCTIONS_const(name) \
330 DECLARE_ASN1_ALLOC_FUNCTIONS(name) \
331 DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name)
332
333#define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
334 type *name##_new(void); \
335 void name##_free(type *a);
336
337#define DECLARE_ASN1_PRINT_FUNCTION(stname) \
338 DECLARE_ASN1_PRINT_FUNCTION_fname(stname, stname)
339
340#define DECLARE_ASN1_PRINT_FUNCTION_fname(stname, fname) \
341 int fname##_print_ctx(BIO *out, stname *x, int indent, \
342 const ASN1_PCTX *pctx);
343
344#define D2I_OF(type) type *(*)(type **,const unsigned char **,long)
345#define I2D_OF(type) int (*)(type *,unsigned char **)
346#define I2D_OF_const(type) int (*)(const type *,unsigned char **)
347
348#define CHECKED_D2I_OF(type, d2i) \
349 ((d2i_of_void*) (1 ? d2i : ((D2I_OF(type))0)))
350#define CHECKED_I2D_OF(type, i2d) \
351 ((i2d_of_void*) (1 ? i2d : ((I2D_OF(type))0)))
352#define CHECKED_NEW_OF(type, xnew) \
353 ((void *(*)(void)) (1 ? xnew : ((type *(*)(void))0)))
354#define CHECKED_PTR_OF(type, p) \
355 ((void*) (1 ? p : (type*)0))
356#define CHECKED_PPTR_OF(type, p) \
357 ((void**) (1 ? p : (type**)0))
358
359#define TYPEDEF_D2I_OF(type) typedef type *d2i_of_##type(type **,const unsigned char **,long)
360#define TYPEDEF_I2D_OF(type) typedef int i2d_of_##type(type *,unsigned char **)
361#define TYPEDEF_D2I2D_OF(type) TYPEDEF_D2I_OF(type); TYPEDEF_I2D_OF(type)
362
363TYPEDEF_D2I2D_OF(void);
364
365/* The following macros and typedefs allow an ASN1_ITEM
366 * to be embedded in a structure and referenced. Since
367 * the ASN1_ITEM pointers need to be globally accessible
368 * (possibly from shared libraries) they may exist in
369 * different forms. On platforms that support it the
370 * ASN1_ITEM structure itself will be globally exported.
371 * Other platforms will export a function that returns
372 * an ASN1_ITEM pointer.
373 *
374 * To handle both cases transparently the macros below
375 * should be used instead of hard coding an ASN1_ITEM
376 * pointer in a structure.
377 *
378 * The structure will look like this:
379 *
380 * typedef struct SOMETHING_st {
381 * ...
382 * ASN1_ITEM_EXP *iptr;
383 * ...
384 * } SOMETHING;
385 *
386 * It would be initialised as e.g.:
387 *
388 * SOMETHING somevar = {...,ASN1_ITEM_ref(X509),...};
389 *
390 * and the actual pointer extracted with:
391 *
392 * const ASN1_ITEM *it = ASN1_ITEM_ptr(somevar.iptr);
393 *
394 * Finally an ASN1_ITEM pointer can be extracted from an
395 * appropriate reference with: ASN1_ITEM_rptr(X509). This
396 * would be used when a function takes an ASN1_ITEM * argument.
397 *
398 */
399
400#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
401
402/* ASN1_ITEM pointer exported type */
403typedef const ASN1_ITEM ASN1_ITEM_EXP;
404
405/* Macro to obtain ASN1_ITEM pointer from exported type */
406#define ASN1_ITEM_ptr(iptr) (iptr)
407
408/* Macro to include ASN1_ITEM pointer from base type */
409#define ASN1_ITEM_ref(iptr) (&(iptr##_it))
410
411#define ASN1_ITEM_rptr(ref) (&(ref##_it))
412
413#define DECLARE_ASN1_ITEM(name) \
414 OPENSSL_EXTERN const ASN1_ITEM name##_it;
415
416#else
417
418/* Platforms that can't easily handle shared global variables are declared
419 * as functions returning ASN1_ITEM pointers.
420 */
421
422/* ASN1_ITEM pointer exported type */
423typedef const ASN1_ITEM * ASN1_ITEM_EXP(void);
424
425/* Macro to obtain ASN1_ITEM pointer from exported type */
426#define ASN1_ITEM_ptr(iptr) (iptr())
427
428/* Macro to include ASN1_ITEM pointer from base type */
429#define ASN1_ITEM_ref(iptr) (iptr##_it)
430
431#define ASN1_ITEM_rptr(ref) (ref##_it())
432
433#define DECLARE_ASN1_ITEM(name) \
434 const ASN1_ITEM * name##_it(void);
435
436#endif
437
438/* Parameters used by ASN1_STRING_print_ex() */
439
440/* These determine which characters to escape:
441 * RFC2253 special characters, control characters and
442 * MSB set characters
443 */
444
445#define ASN1_STRFLGS_ESC_2253 1
446#define ASN1_STRFLGS_ESC_CTRL 2
447#define ASN1_STRFLGS_ESC_MSB 4
448
449
450/* This flag determines how we do escaping: normally
451 * RC2253 backslash only, set this to use backslash and
452 * quote.
453 */
454
455#define ASN1_STRFLGS_ESC_QUOTE 8
456
457
458/* These three flags are internal use only. */
459
460/* Character is a valid PrintableString character */
461#define CHARTYPE_PRINTABLESTRING 0x10
462/* Character needs escaping if it is the first character */
463#define CHARTYPE_FIRST_ESC_2253 0x20
464/* Character needs escaping if it is the last character */
465#define CHARTYPE_LAST_ESC_2253 0x40
466
467/* NB the internal flags are safely reused below by flags
468 * handled at the top level.
469 */
470
471/* If this is set we convert all character strings
472 * to UTF8 first
473 */
474
475#define ASN1_STRFLGS_UTF8_CONVERT 0x10
476
477/* If this is set we don't attempt to interpret content:
478 * just assume all strings are 1 byte per character. This
479 * will produce some pretty odd looking output!
480 */
481
482#define ASN1_STRFLGS_IGNORE_TYPE 0x20
483
484/* If this is set we include the string type in the output */
485#define ASN1_STRFLGS_SHOW_TYPE 0x40
486
487/* This determines which strings to display and which to
488 * 'dump' (hex dump of content octets or DER encoding). We can
489 * only dump non character strings or everything. If we
490 * don't dump 'unknown' they are interpreted as character
491 * strings with 1 octet per character and are subject to
492 * the usual escaping options.
493 */
494
495#define ASN1_STRFLGS_DUMP_ALL 0x80
496#define ASN1_STRFLGS_DUMP_UNKNOWN 0x100
497
498/* These determine what 'dumping' does, we can dump the
499 * content octets or the DER encoding: both use the
500 * RFC2253 #XXXXX notation.
501 */
502
503#define ASN1_STRFLGS_DUMP_DER 0x200
504
505/* All the string flags consistent with RFC2253,
506 * escaping control characters isn't essential in
507 * RFC2253 but it is advisable anyway.
508 */
509
510#define ASN1_STRFLGS_RFC2253 (ASN1_STRFLGS_ESC_2253 | \
511 ASN1_STRFLGS_ESC_CTRL | \
512 ASN1_STRFLGS_ESC_MSB | \
513 ASN1_STRFLGS_UTF8_CONVERT | \
514 ASN1_STRFLGS_DUMP_UNKNOWN | \
515 ASN1_STRFLGS_DUMP_DER)
516
517DECLARE_STACK_OF(ASN1_INTEGER)
518DECLARE_ASN1_SET_OF(ASN1_INTEGER)
519
520DECLARE_STACK_OF(ASN1_GENERALSTRING)
521
522typedef struct asn1_type_st
523 {
524 int type;
525 union {
526 char *ptr;
527 ASN1_BOOLEAN boolean;
528 ASN1_STRING * asn1_string;
529 ASN1_OBJECT * object;
530 ASN1_INTEGER * integer;
531 ASN1_ENUMERATED * enumerated;
532 ASN1_BIT_STRING * bit_string;
533 ASN1_OCTET_STRING * octet_string;
534 ASN1_PRINTABLESTRING * printablestring;
535 ASN1_T61STRING * t61string;
536 ASN1_IA5STRING * ia5string;
537 ASN1_GENERALSTRING * generalstring;
538 ASN1_BMPSTRING * bmpstring;
539 ASN1_UNIVERSALSTRING * universalstring;
540 ASN1_UTCTIME * utctime;
541 ASN1_GENERALIZEDTIME * generalizedtime;
542 ASN1_VISIBLESTRING * visiblestring;
543 ASN1_UTF8STRING * utf8string;
544 /* set and sequence are left complete and still
545 * contain the set or sequence bytes */
546 ASN1_STRING * set;
547 ASN1_STRING * sequence;
548 ASN1_VALUE * asn1_value;
549 } value;
550 } ASN1_TYPE;
551
552DECLARE_STACK_OF(ASN1_TYPE)
553DECLARE_ASN1_SET_OF(ASN1_TYPE)
554
555typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY;
556
557DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
558DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SET_ANY)
559
560typedef struct NETSCAPE_X509_st
561 {
562 ASN1_OCTET_STRING *header;
563 X509 *cert;
564 } NETSCAPE_X509;
565
566/* This is used to contain a list of bit names */
567typedef struct BIT_STRING_BITNAME_st {
568 int bitnum;
569 const char *lname;
570 const char *sname;
571} BIT_STRING_BITNAME;
572
573
574#define M_ASN1_STRING_length(x) ((x)->length)
575#define M_ASN1_STRING_length_set(x, n) ((x)->length = (n))
576#define M_ASN1_STRING_type(x) ((x)->type)
577#define M_ASN1_STRING_data(x) ((x)->data)
578
579/* Macros for string operations */
580#define M_ASN1_BIT_STRING_new() (ASN1_BIT_STRING *)\
581 ASN1_STRING_type_new(V_ASN1_BIT_STRING)
582#define M_ASN1_BIT_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
583#define M_ASN1_BIT_STRING_dup(a) (ASN1_BIT_STRING *)\
584 ASN1_STRING_dup((const ASN1_STRING *)a)
585#define M_ASN1_BIT_STRING_cmp(a,b) ASN1_STRING_cmp(\
586 (const ASN1_STRING *)a,(const ASN1_STRING *)b)
587#define M_ASN1_BIT_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
588
589#define M_ASN1_INTEGER_new() (ASN1_INTEGER *)\
590 ASN1_STRING_type_new(V_ASN1_INTEGER)
591#define M_ASN1_INTEGER_free(a) ASN1_STRING_free((ASN1_STRING *)a)
592#define M_ASN1_INTEGER_dup(a) (ASN1_INTEGER *)\
593 ASN1_STRING_dup((const ASN1_STRING *)a)
594#define M_ASN1_INTEGER_cmp(a,b) ASN1_STRING_cmp(\
595 (const ASN1_STRING *)a,(const ASN1_STRING *)b)
596
597#define M_ASN1_ENUMERATED_new() (ASN1_ENUMERATED *)\
598 ASN1_STRING_type_new(V_ASN1_ENUMERATED)
599#define M_ASN1_ENUMERATED_free(a) ASN1_STRING_free((ASN1_STRING *)a)
600#define M_ASN1_ENUMERATED_dup(a) (ASN1_ENUMERATED *)\
601 ASN1_STRING_dup((const ASN1_STRING *)a)
602#define M_ASN1_ENUMERATED_cmp(a,b) ASN1_STRING_cmp(\
603 (const ASN1_STRING *)a,(const ASN1_STRING *)b)
604
605#define M_ASN1_OCTET_STRING_new() (ASN1_OCTET_STRING *)\
606 ASN1_STRING_type_new(V_ASN1_OCTET_STRING)
607#define M_ASN1_OCTET_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
608#define M_ASN1_OCTET_STRING_dup(a) (ASN1_OCTET_STRING *)\
609 ASN1_STRING_dup((const ASN1_STRING *)a)
610#define M_ASN1_OCTET_STRING_cmp(a,b) ASN1_STRING_cmp(\
611 (const ASN1_STRING *)a,(const ASN1_STRING *)b)
612#define M_ASN1_OCTET_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
613#define M_ASN1_OCTET_STRING_print(a,b) ASN1_STRING_print(a,(ASN1_STRING *)b)
614#define M_i2d_ASN1_OCTET_STRING(a,pp) \
615 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_OCTET_STRING,\
616 V_ASN1_UNIVERSAL)
617
618#define B_ASN1_TIME \
619 B_ASN1_UTCTIME | \
620 B_ASN1_GENERALIZEDTIME
621
622#define B_ASN1_PRINTABLE \
623 B_ASN1_NUMERICSTRING| \
624 B_ASN1_PRINTABLESTRING| \
625 B_ASN1_T61STRING| \
626 B_ASN1_IA5STRING| \
627 B_ASN1_BIT_STRING| \
628 B_ASN1_UNIVERSALSTRING|\
629 B_ASN1_BMPSTRING|\
630 B_ASN1_UTF8STRING|\
631 B_ASN1_SEQUENCE|\
632 B_ASN1_UNKNOWN
633
634#define B_ASN1_DIRECTORYSTRING \
635 B_ASN1_PRINTABLESTRING| \
636 B_ASN1_TELETEXSTRING|\
637 B_ASN1_BMPSTRING|\
638 B_ASN1_UNIVERSALSTRING|\
639 B_ASN1_UTF8STRING
640
641#define B_ASN1_DISPLAYTEXT \
642 B_ASN1_IA5STRING| \
643 B_ASN1_VISIBLESTRING| \
644 B_ASN1_BMPSTRING|\
645 B_ASN1_UTF8STRING
646
647#define M_ASN1_PRINTABLE_new() ASN1_STRING_type_new(V_ASN1_T61STRING)
648#define M_ASN1_PRINTABLE_free(a) ASN1_STRING_free((ASN1_STRING *)a)
649#define M_i2d_ASN1_PRINTABLE(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\
650 pp,a->type,V_ASN1_UNIVERSAL)
651#define M_d2i_ASN1_PRINTABLE(a,pp,l) \
652 d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \
653 B_ASN1_PRINTABLE)
654
655#define M_DIRECTORYSTRING_new() ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING)
656#define M_DIRECTORYSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
657#define M_i2d_DIRECTORYSTRING(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\
658 pp,a->type,V_ASN1_UNIVERSAL)
659#define M_d2i_DIRECTORYSTRING(a,pp,l) \
660 d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \
661 B_ASN1_DIRECTORYSTRING)
662
663#define M_DISPLAYTEXT_new() ASN1_STRING_type_new(V_ASN1_VISIBLESTRING)
664#define M_DISPLAYTEXT_free(a) ASN1_STRING_free((ASN1_STRING *)a)
665#define M_i2d_DISPLAYTEXT(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\
666 pp,a->type,V_ASN1_UNIVERSAL)
667#define M_d2i_DISPLAYTEXT(a,pp,l) \
668 d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \
669 B_ASN1_DISPLAYTEXT)
670
671#define M_ASN1_PRINTABLESTRING_new() (ASN1_PRINTABLESTRING *)\
672 ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING)
673#define M_ASN1_PRINTABLESTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
674#define M_i2d_ASN1_PRINTABLESTRING(a,pp) \
675 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_PRINTABLESTRING,\
676 V_ASN1_UNIVERSAL)
677#define M_d2i_ASN1_PRINTABLESTRING(a,pp,l) \
678 (ASN1_PRINTABLESTRING *)d2i_ASN1_type_bytes\
679 ((ASN1_STRING **)a,pp,l,B_ASN1_PRINTABLESTRING)
680
681#define M_ASN1_T61STRING_new() (ASN1_T61STRING *)\
682 ASN1_STRING_type_new(V_ASN1_T61STRING)
683#define M_ASN1_T61STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
684#define M_i2d_ASN1_T61STRING(a,pp) \
685 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_T61STRING,\
686 V_ASN1_UNIVERSAL)
687#define M_d2i_ASN1_T61STRING(a,pp,l) \
688 (ASN1_T61STRING *)d2i_ASN1_type_bytes\
689 ((ASN1_STRING **)a,pp,l,B_ASN1_T61STRING)
690
691#define M_ASN1_IA5STRING_new() (ASN1_IA5STRING *)\
692 ASN1_STRING_type_new(V_ASN1_IA5STRING)
693#define M_ASN1_IA5STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
694#define M_ASN1_IA5STRING_dup(a) \
695 (ASN1_IA5STRING *)ASN1_STRING_dup((const ASN1_STRING *)a)
696#define M_i2d_ASN1_IA5STRING(a,pp) \
697 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_IA5STRING,\
698 V_ASN1_UNIVERSAL)
699#define M_d2i_ASN1_IA5STRING(a,pp,l) \
700 (ASN1_IA5STRING *)d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l,\
701 B_ASN1_IA5STRING)
702
703#define M_ASN1_UTCTIME_new() (ASN1_UTCTIME *)\
704 ASN1_STRING_type_new(V_ASN1_UTCTIME)
705#define M_ASN1_UTCTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
706#define M_ASN1_UTCTIME_dup(a) (ASN1_UTCTIME *)\
707 ASN1_STRING_dup((const ASN1_STRING *)a)
708
709#define M_ASN1_GENERALIZEDTIME_new() (ASN1_GENERALIZEDTIME *)\
710 ASN1_STRING_type_new(V_ASN1_GENERALIZEDTIME)
711#define M_ASN1_GENERALIZEDTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
712#define M_ASN1_GENERALIZEDTIME_dup(a) (ASN1_GENERALIZEDTIME *)ASN1_STRING_dup(\
713 (const ASN1_STRING *)a)
714
715#define M_ASN1_TIME_new() (ASN1_TIME *)\
716 ASN1_STRING_type_new(V_ASN1_UTCTIME)
717#define M_ASN1_TIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
718#define M_ASN1_TIME_dup(a) (ASN1_TIME *)\
719 ASN1_STRING_dup((const ASN1_STRING *)a)
720
721#define M_ASN1_GENERALSTRING_new() (ASN1_GENERALSTRING *)\
722 ASN1_STRING_type_new(V_ASN1_GENERALSTRING)
723#define M_ASN1_GENERALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
724#define M_i2d_ASN1_GENERALSTRING(a,pp) \
725 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_GENERALSTRING,\
726 V_ASN1_UNIVERSAL)
727#define M_d2i_ASN1_GENERALSTRING(a,pp,l) \
728 (ASN1_GENERALSTRING *)d2i_ASN1_type_bytes\
729 ((ASN1_STRING **)a,pp,l,B_ASN1_GENERALSTRING)
730
731#define M_ASN1_UNIVERSALSTRING_new() (ASN1_UNIVERSALSTRING *)\
732 ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING)
733#define M_ASN1_UNIVERSALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
734#define M_i2d_ASN1_UNIVERSALSTRING(a,pp) \
735 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_UNIVERSALSTRING,\
736 V_ASN1_UNIVERSAL)
737#define M_d2i_ASN1_UNIVERSALSTRING(a,pp,l) \
738 (ASN1_UNIVERSALSTRING *)d2i_ASN1_type_bytes\
739 ((ASN1_STRING **)a,pp,l,B_ASN1_UNIVERSALSTRING)
740
741#define M_ASN1_BMPSTRING_new() (ASN1_BMPSTRING *)\
742 ASN1_STRING_type_new(V_ASN1_BMPSTRING)
743#define M_ASN1_BMPSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
744#define M_i2d_ASN1_BMPSTRING(a,pp) \
745 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_BMPSTRING,\
746 V_ASN1_UNIVERSAL)
747#define M_d2i_ASN1_BMPSTRING(a,pp,l) \
748 (ASN1_BMPSTRING *)d2i_ASN1_type_bytes\
749 ((ASN1_STRING **)a,pp,l,B_ASN1_BMPSTRING)
750
751#define M_ASN1_VISIBLESTRING_new() (ASN1_VISIBLESTRING *)\
752 ASN1_STRING_type_new(V_ASN1_VISIBLESTRING)
753#define M_ASN1_VISIBLESTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
754#define M_i2d_ASN1_VISIBLESTRING(a,pp) \
755 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_VISIBLESTRING,\
756 V_ASN1_UNIVERSAL)
757#define M_d2i_ASN1_VISIBLESTRING(a,pp,l) \
758 (ASN1_VISIBLESTRING *)d2i_ASN1_type_bytes\
759 ((ASN1_STRING **)a,pp,l,B_ASN1_VISIBLESTRING)
760
761#define M_ASN1_UTF8STRING_new() (ASN1_UTF8STRING *)\
762 ASN1_STRING_type_new(V_ASN1_UTF8STRING)
763#define M_ASN1_UTF8STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
764#define M_i2d_ASN1_UTF8STRING(a,pp) \
765 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_UTF8STRING,\
766 V_ASN1_UNIVERSAL)
767#define M_d2i_ASN1_UTF8STRING(a,pp,l) \
768 (ASN1_UTF8STRING *)d2i_ASN1_type_bytes\
769 ((ASN1_STRING **)a,pp,l,B_ASN1_UTF8STRING)
770
771 /* for the is_set parameter to i2d_ASN1_SET */
772#define IS_SEQUENCE 0
773#define IS_SET 1
774
775DECLARE_ASN1_FUNCTIONS_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
776
777int ASN1_TYPE_get(ASN1_TYPE *a);
778void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value);
779int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value);
780int ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b);
781
782ASN1_OBJECT * ASN1_OBJECT_new(void );
783void ASN1_OBJECT_free(ASN1_OBJECT *a);
784int i2d_ASN1_OBJECT(ASN1_OBJECT *a,unsigned char **pp);
785ASN1_OBJECT * c2i_ASN1_OBJECT(ASN1_OBJECT **a,const unsigned char **pp,
786 long length);
787ASN1_OBJECT * d2i_ASN1_OBJECT(ASN1_OBJECT **a,const unsigned char **pp,
788 long length);
789
790DECLARE_ASN1_ITEM(ASN1_OBJECT)
791
792DECLARE_STACK_OF(ASN1_OBJECT)
793DECLARE_ASN1_SET_OF(ASN1_OBJECT)
794
795ASN1_STRING * ASN1_STRING_new(void);
796void ASN1_STRING_free(ASN1_STRING *a);
797int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str);
798ASN1_STRING * ASN1_STRING_dup(const ASN1_STRING *a);
799ASN1_STRING * ASN1_STRING_type_new(int type );
800int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b);
801 /* Since this is used to store all sorts of things, via macros, for now, make
802 its data void * */
803int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);
804void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len);
805int ASN1_STRING_length(const ASN1_STRING *x);
806void ASN1_STRING_length_set(ASN1_STRING *x, int n);
807int ASN1_STRING_type(ASN1_STRING *x);
808unsigned char * ASN1_STRING_data(ASN1_STRING *x);
809
810DECLARE_ASN1_FUNCTIONS(ASN1_BIT_STRING)
811int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a,unsigned char **pp);
812ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,const unsigned char **pp,
813 long length);
814int ASN1_BIT_STRING_set(ASN1_BIT_STRING *a, unsigned char *d,
815 int length );
816int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value);
817int ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n);
818int ASN1_BIT_STRING_check(ASN1_BIT_STRING *a,
819 unsigned char *flags, int flags_len);
820
821#ifndef OPENSSL_NO_BIO
822int ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs,
823 BIT_STRING_BITNAME *tbl, int indent);
824#endif
825int ASN1_BIT_STRING_num_asc(char *name, BIT_STRING_BITNAME *tbl);
826int ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, char *name, int value,
827 BIT_STRING_BITNAME *tbl);
828
829int i2d_ASN1_BOOLEAN(int a,unsigned char **pp);
830int d2i_ASN1_BOOLEAN(int *a,const unsigned char **pp,long length);
831
832DECLARE_ASN1_FUNCTIONS(ASN1_INTEGER)
833int i2c_ASN1_INTEGER(ASN1_INTEGER *a,unsigned char **pp);
834ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a,const unsigned char **pp,
835 long length);
836ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a,const unsigned char **pp,
837 long length);
838ASN1_INTEGER * ASN1_INTEGER_dup(const ASN1_INTEGER *x);
839int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y);
840
841DECLARE_ASN1_FUNCTIONS(ASN1_ENUMERATED)
842
843int ASN1_UTCTIME_check(ASN1_UTCTIME *a);
844ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s,time_t t);
845ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
846 int offset_day, long offset_sec);
847int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
848int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
849#if 0
850time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s);
851#endif
852
853int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *a);
854ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,time_t t);
855ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
856 time_t t, int offset_day, long offset_sec);
857int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str);
858
859DECLARE_ASN1_FUNCTIONS(ASN1_OCTET_STRING)
860ASN1_OCTET_STRING * ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *a);
861int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, const ASN1_OCTET_STRING *b);
862int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data, int len);
863
864DECLARE_ASN1_FUNCTIONS(ASN1_VISIBLESTRING)
865DECLARE_ASN1_FUNCTIONS(ASN1_UNIVERSALSTRING)
866DECLARE_ASN1_FUNCTIONS(ASN1_UTF8STRING)
867DECLARE_ASN1_FUNCTIONS(ASN1_NULL)
868DECLARE_ASN1_FUNCTIONS(ASN1_BMPSTRING)
869
870int UTF8_getc(const unsigned char *str, int len, unsigned long *val);
871int UTF8_putc(unsigned char *str, int len, unsigned long value);
872
873DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE)
874
875DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING)
876DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DISPLAYTEXT)
877DECLARE_ASN1_FUNCTIONS(ASN1_PRINTABLESTRING)
878DECLARE_ASN1_FUNCTIONS(ASN1_T61STRING)
879DECLARE_ASN1_FUNCTIONS(ASN1_IA5STRING)
880DECLARE_ASN1_FUNCTIONS(ASN1_GENERALSTRING)
881DECLARE_ASN1_FUNCTIONS(ASN1_UTCTIME)
882DECLARE_ASN1_FUNCTIONS(ASN1_GENERALIZEDTIME)
883DECLARE_ASN1_FUNCTIONS(ASN1_TIME)
884
885DECLARE_ASN1_ITEM(ASN1_OCTET_STRING_NDEF)
886
887ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s,time_t t);
888ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s,time_t t,
889 int offset_day, long offset_sec);
890int ASN1_TIME_check(ASN1_TIME *t);
891ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out);
892int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
893
894int i2d_ASN1_SET(STACK_OF(OPENSSL_BLOCK) *a, unsigned char **pp,
895 i2d_of_void *i2d, int ex_tag, int ex_class,
896 int is_set);
897STACK_OF(OPENSSL_BLOCK) *d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a,
898 const unsigned char **pp,
899 long length, d2i_of_void *d2i,
900 void (*free_func)(OPENSSL_BLOCK), int ex_tag,
901 int ex_class);
902
903#ifndef OPENSSL_NO_BIO
904int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a);
905int a2i_ASN1_INTEGER(BIO *bp,ASN1_INTEGER *bs,char *buf,int size);
906int i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a);
907int a2i_ASN1_ENUMERATED(BIO *bp,ASN1_ENUMERATED *bs,char *buf,int size);
908int i2a_ASN1_OBJECT(BIO *bp,ASN1_OBJECT *a);
909int a2i_ASN1_STRING(BIO *bp,ASN1_STRING *bs,char *buf,int size);
910int i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type);
911#endif
912int i2t_ASN1_OBJECT(char *buf,int buf_len,ASN1_OBJECT *a);
913
914int a2d_ASN1_OBJECT(unsigned char *out,int olen, const char *buf, int num);
915ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data,int len,
916 const char *sn, const char *ln);
917
918int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
919long ASN1_INTEGER_get(const ASN1_INTEGER *a);
920ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai);
921BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai,BIGNUM *bn);
922
923int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
924long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a);
925ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(BIGNUM *bn, ASN1_ENUMERATED *ai);
926BIGNUM *ASN1_ENUMERATED_to_BN(ASN1_ENUMERATED *ai,BIGNUM *bn);
927
928/* General */
929/* given a string, return the correct type, max is the maximum length */
930int ASN1_PRINTABLE_type(const unsigned char *s, int max);
931
932int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass);
933ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
934 long length, int Ptag, int Pclass);
935unsigned long ASN1_tag2bit(int tag);
936/* type is one or more of the B_ASN1_ values. */
937ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a,const unsigned char **pp,
938 long length,int type);
939
940/* PARSING */
941int asn1_Finish(ASN1_CTX *c);
942int asn1_const_Finish(ASN1_const_CTX *c);
943
944/* SPECIALS */
945int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
946 int *pclass, long omax);
947int ASN1_check_infinite_end(unsigned char **p,long len);
948int ASN1_const_check_infinite_end(const unsigned char **p,long len);
949void ASN1_put_object(unsigned char **pp, int constructed, int length,
950 int tag, int xclass);
951int ASN1_put_eoc(unsigned char **pp);
952int ASN1_object_size(int constructed, int length, int tag);
953
954/* Used to implement other functions */
955void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x);
956
957#define ASN1_dup_of(type,i2d,d2i,x) \
958 ((type*)ASN1_dup(CHECKED_I2D_OF(type, i2d), \
959 CHECKED_D2I_OF(type, d2i), \
960 CHECKED_PTR_OF(type, x)))
961
962#define ASN1_dup_of_const(type,i2d,d2i,x) \
963 ((type*)ASN1_dup(CHECKED_I2D_OF(const type, i2d), \
964 CHECKED_D2I_OF(type, d2i), \
965 CHECKED_PTR_OF(const type, x)))
966
967void *ASN1_item_dup(const ASN1_ITEM *it, void *x);
968
969/* ASN1 alloc/free macros for when a type is only used internally */
970
971#define M_ASN1_new_of(type) (type *)ASN1_item_new(ASN1_ITEM_rptr(type))
972#define M_ASN1_free_of(x, type) \
973 ASN1_item_free(CHECKED_PTR_OF(type, x), ASN1_ITEM_rptr(type))
974
975#ifndef OPENSSL_NO_FP_API
976void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x);
977
978#define ASN1_d2i_fp_of(type,xnew,d2i,in,x) \
979 ((type*)ASN1_d2i_fp(CHECKED_NEW_OF(type, xnew), \
980 CHECKED_D2I_OF(type, d2i), \
981 in, \
982 CHECKED_PPTR_OF(type, x)))
983
984void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x);
985int ASN1_i2d_fp(i2d_of_void *i2d,FILE *out,void *x);
986
987#define ASN1_i2d_fp_of(type,i2d,out,x) \
988 (ASN1_i2d_fp(CHECKED_I2D_OF(type, i2d), \
989 out, \
990 CHECKED_PTR_OF(type, x)))
991
992#define ASN1_i2d_fp_of_const(type,i2d,out,x) \
993 (ASN1_i2d_fp(CHECKED_I2D_OF(const type, i2d), \
994 out, \
995 CHECKED_PTR_OF(const type, x)))
996
997int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x);
998int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags);
999#endif
1000
1001int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in);
1002
1003#ifndef OPENSSL_NO_BIO
1004void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x);
1005
1006#define ASN1_d2i_bio_of(type,xnew,d2i,in,x) \
1007 ((type*)ASN1_d2i_bio( CHECKED_NEW_OF(type, xnew), \
1008 CHECKED_D2I_OF(type, d2i), \
1009 in, \
1010 CHECKED_PPTR_OF(type, x)))
1011
1012void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x);
1013int ASN1_i2d_bio(i2d_of_void *i2d,BIO *out, unsigned char *x);
1014
1015#define ASN1_i2d_bio_of(type,i2d,out,x) \
1016 (ASN1_i2d_bio(CHECKED_I2D_OF(type, i2d), \
1017 out, \
1018 CHECKED_PTR_OF(type, x)))
1019
1020#define ASN1_i2d_bio_of_const(type,i2d,out,x) \
1021 (ASN1_i2d_bio(CHECKED_I2D_OF(const type, i2d), \
1022 out, \
1023 CHECKED_PTR_OF(const type, x)))
1024
1025int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x);
1026int ASN1_UTCTIME_print(BIO *fp, const ASN1_UTCTIME *a);
1027int ASN1_GENERALIZEDTIME_print(BIO *fp, const ASN1_GENERALIZEDTIME *a);
1028int ASN1_TIME_print(BIO *fp, const ASN1_TIME *a);
1029int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v);
1030int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags);
1031int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
1032 unsigned char *buf, int off);
1033int ASN1_parse(BIO *bp,const unsigned char *pp,long len,int indent);
1034int ASN1_parse_dump(BIO *bp,const unsigned char *pp,long len,int indent,int dump);
1035#endif
1036const char *ASN1_tag2str(int tag);
1037
1038/* Used to load and write netscape format cert */
1039
1040DECLARE_ASN1_FUNCTIONS(NETSCAPE_X509)
1041
1042int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s);
1043
1044int ASN1_TYPE_set_octetstring(ASN1_TYPE *a,
1045 unsigned char *data, int len);
1046int ASN1_TYPE_get_octetstring(ASN1_TYPE *a,
1047 unsigned char *data, int max_len);
1048int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num,
1049 unsigned char *data, int len);
1050int ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a,long *num,
1051 unsigned char *data, int max_len);
1052
1053STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len,
1054 d2i_of_void *d2i, void (*free_func)(OPENSSL_BLOCK));
1055unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d,
1056 unsigned char **buf, int *len );
1057void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i);
1058void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it);
1059ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d,
1060 ASN1_OCTET_STRING **oct);
1061
1062#define ASN1_pack_string_of(type,obj,i2d,oct) \
1063 (ASN1_pack_string(CHECKED_PTR_OF(type, obj), \
1064 CHECKED_I2D_OF(type, i2d), \
1065 oct))
1066
1067ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_OCTET_STRING **oct);
1068
1069void ASN1_STRING_set_default_mask(unsigned long mask);
1070int ASN1_STRING_set_default_mask_asc(const char *p);
1071unsigned long ASN1_STRING_get_default_mask(void);
1072int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
1073 int inform, unsigned long mask);
1074int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
1075 int inform, unsigned long mask,
1076 long minsize, long maxsize);
1077
1078ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
1079 const unsigned char *in, int inlen, int inform, int nid);
1080ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid);
1081int ASN1_STRING_TABLE_add(int, long, long, unsigned long, unsigned long);
1082void ASN1_STRING_TABLE_cleanup(void);
1083
1084/* ASN1 template functions */
1085
1086/* Old API compatible functions */
1087ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it);
1088void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it);
1089ASN1_VALUE * ASN1_item_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it);
1090int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it);
1091int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it);
1092
1093void ASN1_add_oid_module(void);
1094
1095ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf);
1096ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf);
1097
1098/* ASN1 Print flags */
1099
1100/* Indicate missing OPTIONAL fields */
1101#define ASN1_PCTX_FLAGS_SHOW_ABSENT 0x001
1102/* Mark start and end of SEQUENCE */
1103#define ASN1_PCTX_FLAGS_SHOW_SEQUENCE 0x002
1104/* Mark start and end of SEQUENCE/SET OF */
1105#define ASN1_PCTX_FLAGS_SHOW_SSOF 0x004
1106/* Show the ASN1 type of primitives */
1107#define ASN1_PCTX_FLAGS_SHOW_TYPE 0x008
1108/* Don't show ASN1 type of ANY */
1109#define ASN1_PCTX_FLAGS_NO_ANY_TYPE 0x010
1110/* Don't show ASN1 type of MSTRINGs */
1111#define ASN1_PCTX_FLAGS_NO_MSTRING_TYPE 0x020
1112/* Don't show field names in SEQUENCE */
1113#define ASN1_PCTX_FLAGS_NO_FIELD_NAME 0x040
1114/* Show structure names of each SEQUENCE field */
1115#define ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME 0x080
1116/* Don't show structure name even at top level */
1117#define ASN1_PCTX_FLAGS_NO_STRUCT_NAME 0x100
1118
1119int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
1120 const ASN1_ITEM *it, const ASN1_PCTX *pctx);
1121ASN1_PCTX *ASN1_PCTX_new(void);
1122void ASN1_PCTX_free(ASN1_PCTX *p);
1123unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p);
1124void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags);
1125unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p);
1126void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags);
1127unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p);
1128void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags);
1129unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p);
1130void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags);
1131unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p);
1132void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags);
1133
1134BIO_METHOD *BIO_f_asn1(void);
1135
1136BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it);
1137
1138int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
1139 const ASN1_ITEM *it);
1140int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
1141 const char *hdr,
1142 const ASN1_ITEM *it);
1143int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
1144 int ctype_nid, int econt_nid,
1145 STACK_OF(X509_ALGOR) *mdalgs,
1146 const ASN1_ITEM *it);
1147ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it);
1148int SMIME_crlf_copy(BIO *in, BIO *out, int flags);
1149int SMIME_text(BIO *in, BIO *out);
1150
1151/* BEGIN ERROR CODES */
1152/* The following lines are auto generated by the script mkerr.pl. Any changes
1153 * made after this point may be overwritten when the script is next run.
1154 */
1155void ERR_load_ASN1_strings(void);
1156
1157/* Error codes for the ASN1 functions. */
1158
1159/* Function codes. */
1160#define ASN1_F_A2D_ASN1_OBJECT 100
1161#define ASN1_F_A2I_ASN1_ENUMERATED 101
1162#define ASN1_F_A2I_ASN1_INTEGER 102
1163#define ASN1_F_A2I_ASN1_STRING 103
1164#define ASN1_F_APPEND_EXP 176
1165#define ASN1_F_ASN1_BIT_STRING_SET_BIT 183
1166#define ASN1_F_ASN1_CB 177
1167#define ASN1_F_ASN1_CHECK_TLEN 104
1168#define ASN1_F_ASN1_COLLATE_PRIMITIVE 105
1169#define ASN1_F_ASN1_COLLECT 106
1170#define ASN1_F_ASN1_D2I_EX_PRIMITIVE 108
1171#define ASN1_F_ASN1_D2I_FP 109
1172#define ASN1_F_ASN1_D2I_READ_BIO 107
1173#define ASN1_F_ASN1_DIGEST 184
1174#define ASN1_F_ASN1_DO_ADB 110
1175#define ASN1_F_ASN1_DUP 111
1176#define ASN1_F_ASN1_ENUMERATED_SET 112
1177#define ASN1_F_ASN1_ENUMERATED_TO_BN 113
1178#define ASN1_F_ASN1_EX_C2I 204
1179#define ASN1_F_ASN1_FIND_END 190
1180#define ASN1_F_ASN1_GENERALIZEDTIME_ADJ 216
1181#define ASN1_F_ASN1_GENERALIZEDTIME_SET 185
1182#define ASN1_F_ASN1_GENERATE_V3 178
1183#define ASN1_F_ASN1_GET_OBJECT 114
1184#define ASN1_F_ASN1_HEADER_NEW 115
1185#define ASN1_F_ASN1_I2D_BIO 116
1186#define ASN1_F_ASN1_I2D_FP 117
1187#define ASN1_F_ASN1_INTEGER_SET 118
1188#define ASN1_F_ASN1_INTEGER_TO_BN 119
1189#define ASN1_F_ASN1_ITEM_D2I_FP 206
1190#define ASN1_F_ASN1_ITEM_DUP 191
1191#define ASN1_F_ASN1_ITEM_EX_COMBINE_NEW 121
1192#define ASN1_F_ASN1_ITEM_EX_D2I 120
1193#define ASN1_F_ASN1_ITEM_I2D_BIO 192
1194#define ASN1_F_ASN1_ITEM_I2D_FP 193
1195#define ASN1_F_ASN1_ITEM_PACK 198
1196#define ASN1_F_ASN1_ITEM_SIGN 195
1197#define ASN1_F_ASN1_ITEM_UNPACK 199
1198#define ASN1_F_ASN1_ITEM_VERIFY 197
1199#define ASN1_F_ASN1_MBSTRING_NCOPY 122
1200#define ASN1_F_ASN1_OBJECT_NEW 123
1201#define ASN1_F_ASN1_OUTPUT_DATA 214
1202#define ASN1_F_ASN1_PACK_STRING 124
1203#define ASN1_F_ASN1_PCTX_NEW 205
1204#define ASN1_F_ASN1_PKCS5_PBE_SET 125
1205#define ASN1_F_ASN1_SEQ_PACK 126
1206#define ASN1_F_ASN1_SEQ_UNPACK 127
1207#define ASN1_F_ASN1_SIGN 128
1208#define ASN1_F_ASN1_STR2TYPE 179
1209#define ASN1_F_ASN1_STRING_SET 186
1210#define ASN1_F_ASN1_STRING_TABLE_ADD 129
1211#define ASN1_F_ASN1_STRING_TYPE_NEW 130
1212#define ASN1_F_ASN1_TEMPLATE_EX_D2I 132
1213#define ASN1_F_ASN1_TEMPLATE_NEW 133
1214#define ASN1_F_ASN1_TEMPLATE_NOEXP_D2I 131
1215#define ASN1_F_ASN1_TIME_ADJ 217
1216#define ASN1_F_ASN1_TIME_SET 175
1217#define ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING 134
1218#define ASN1_F_ASN1_TYPE_GET_OCTETSTRING 135
1219#define ASN1_F_ASN1_UNPACK_STRING 136
1220#define ASN1_F_ASN1_UTCTIME_ADJ 218
1221#define ASN1_F_ASN1_UTCTIME_SET 187
1222#define ASN1_F_ASN1_VERIFY 137
1223#define ASN1_F_B64_READ_ASN1 209
1224#define ASN1_F_B64_WRITE_ASN1 210
1225#define ASN1_F_BIO_NEW_NDEF 208
1226#define ASN1_F_BITSTR_CB 180
1227#define ASN1_F_BN_TO_ASN1_ENUMERATED 138
1228#define ASN1_F_BN_TO_ASN1_INTEGER 139
1229#define ASN1_F_C2I_ASN1_BIT_STRING 189
1230#define ASN1_F_C2I_ASN1_INTEGER 194
1231#define ASN1_F_C2I_ASN1_OBJECT 196
1232#define ASN1_F_COLLECT_DATA 140
1233#define ASN1_F_D2I_ASN1_BIT_STRING 141
1234#define ASN1_F_D2I_ASN1_BOOLEAN 142
1235#define ASN1_F_D2I_ASN1_BYTES 143
1236#define ASN1_F_D2I_ASN1_GENERALIZEDTIME 144
1237#define ASN1_F_D2I_ASN1_HEADER 145
1238#define ASN1_F_D2I_ASN1_INTEGER 146
1239#define ASN1_F_D2I_ASN1_OBJECT 147
1240#define ASN1_F_D2I_ASN1_SET 148
1241#define ASN1_F_D2I_ASN1_TYPE_BYTES 149
1242#define ASN1_F_D2I_ASN1_UINTEGER 150
1243#define ASN1_F_D2I_ASN1_UTCTIME 151
1244#define ASN1_F_D2I_AUTOPRIVATEKEY 207
1245#define ASN1_F_D2I_NETSCAPE_RSA 152
1246#define ASN1_F_D2I_NETSCAPE_RSA_2 153
1247#define ASN1_F_D2I_PRIVATEKEY 154
1248#define ASN1_F_D2I_PUBLICKEY 155
1249#define ASN1_F_D2I_RSA_NET 200
1250#define ASN1_F_D2I_RSA_NET_2 201
1251#define ASN1_F_D2I_X509 156
1252#define ASN1_F_D2I_X509_CINF 157
1253#define ASN1_F_D2I_X509_PKEY 159
1254#define ASN1_F_I2D_ASN1_BIO_STREAM 211
1255#define ASN1_F_I2D_ASN1_SET 188
1256#define ASN1_F_I2D_ASN1_TIME 160
1257#define ASN1_F_I2D_DSA_PUBKEY 161
1258#define ASN1_F_I2D_EC_PUBKEY 181
1259#define ASN1_F_I2D_PRIVATEKEY 163
1260#define ASN1_F_I2D_PUBLICKEY 164
1261#define ASN1_F_I2D_RSA_NET 162
1262#define ASN1_F_I2D_RSA_PUBKEY 165
1263#define ASN1_F_LONG_C2I 166
1264#define ASN1_F_OID_MODULE_INIT 174
1265#define ASN1_F_PARSE_TAGGING 182
1266#define ASN1_F_PKCS5_PBE2_SET_IV 167
1267#define ASN1_F_PKCS5_PBE_SET 202
1268#define ASN1_F_PKCS5_PBE_SET0_ALGOR 215
1269#define ASN1_F_SMIME_READ_ASN1 212
1270#define ASN1_F_SMIME_TEXT 213
1271#define ASN1_F_X509_CINF_NEW 168
1272#define ASN1_F_X509_CRL_ADD0_REVOKED 169
1273#define ASN1_F_X509_INFO_NEW 170
1274#define ASN1_F_X509_NAME_ENCODE 203
1275#define ASN1_F_X509_NAME_EX_D2I 158
1276#define ASN1_F_X509_NAME_EX_NEW 171
1277#define ASN1_F_X509_NEW 172
1278#define ASN1_F_X509_PKEY_NEW 173
1279
1280/* Reason codes. */
1281#define ASN1_R_ADDING_OBJECT 171
1282#define ASN1_R_ASN1_PARSE_ERROR 203
1283#define ASN1_R_ASN1_SIG_PARSE_ERROR 204
1284#define ASN1_R_AUX_ERROR 100
1285#define ASN1_R_BAD_CLASS 101
1286#define ASN1_R_BAD_OBJECT_HEADER 102
1287#define ASN1_R_BAD_PASSWORD_READ 103
1288#define ASN1_R_BAD_TAG 104
1289#define ASN1_R_BMPSTRING_IS_WRONG_LENGTH 214
1290#define ASN1_R_BN_LIB 105
1291#define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 106
1292#define ASN1_R_BUFFER_TOO_SMALL 107
1293#define ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 108
1294#define ASN1_R_DATA_IS_WRONG 109
1295#define ASN1_R_DECODE_ERROR 110
1296#define ASN1_R_DECODING_ERROR 111
1297#define ASN1_R_DEPTH_EXCEEDED 174
1298#define ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 198
1299#define ASN1_R_ENCODE_ERROR 112
1300#define ASN1_R_ERROR_GETTING_TIME 173
1301#define ASN1_R_ERROR_LOADING_SECTION 172
1302#define ASN1_R_ERROR_PARSING_SET_ELEMENT 113
1303#define ASN1_R_ERROR_SETTING_CIPHER_PARAMS 114
1304#define ASN1_R_EXPECTING_AN_INTEGER 115
1305#define ASN1_R_EXPECTING_AN_OBJECT 116
1306#define ASN1_R_EXPECTING_A_BOOLEAN 117
1307#define ASN1_R_EXPECTING_A_TIME 118
1308#define ASN1_R_EXPLICIT_LENGTH_MISMATCH 119
1309#define ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED 120
1310#define ASN1_R_FIELD_MISSING 121
1311#define ASN1_R_FIRST_NUM_TOO_LARGE 122
1312#define ASN1_R_HEADER_TOO_LONG 123
1313#define ASN1_R_ILLEGAL_BITSTRING_FORMAT 175
1314#define ASN1_R_ILLEGAL_BOOLEAN 176
1315#define ASN1_R_ILLEGAL_CHARACTERS 124
1316#define ASN1_R_ILLEGAL_FORMAT 177
1317#define ASN1_R_ILLEGAL_HEX 178
1318#define ASN1_R_ILLEGAL_IMPLICIT_TAG 179
1319#define ASN1_R_ILLEGAL_INTEGER 180
1320#define ASN1_R_ILLEGAL_NESTED_TAGGING 181
1321#define ASN1_R_ILLEGAL_NULL 125
1322#define ASN1_R_ILLEGAL_NULL_VALUE 182
1323#define ASN1_R_ILLEGAL_OBJECT 183
1324#define ASN1_R_ILLEGAL_OPTIONAL_ANY 126
1325#define ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE 170
1326#define ASN1_R_ILLEGAL_TAGGED_ANY 127
1327#define ASN1_R_ILLEGAL_TIME_VALUE 184
1328#define ASN1_R_INTEGER_NOT_ASCII_FORMAT 185
1329#define ASN1_R_INTEGER_TOO_LARGE_FOR_LONG 128
1330#define ASN1_R_INVALID_BMPSTRING_LENGTH 129
1331#define ASN1_R_INVALID_DIGIT 130
1332#define ASN1_R_INVALID_MIME_TYPE 205
1333#define ASN1_R_INVALID_MODIFIER 186
1334#define ASN1_R_INVALID_NUMBER 187
1335#define ASN1_R_INVALID_OBJECT_ENCODING 216
1336#define ASN1_R_INVALID_SEPARATOR 131
1337#define ASN1_R_INVALID_TIME_FORMAT 132
1338#define ASN1_R_INVALID_UNIVERSALSTRING_LENGTH 133
1339#define ASN1_R_INVALID_UTF8STRING 134
1340#define ASN1_R_IV_TOO_LARGE 135
1341#define ASN1_R_LENGTH_ERROR 136
1342#define ASN1_R_LIST_ERROR 188
1343#define ASN1_R_MIME_NO_CONTENT_TYPE 206
1344#define ASN1_R_MIME_PARSE_ERROR 207
1345#define ASN1_R_MIME_SIG_PARSE_ERROR 208
1346#define ASN1_R_MISSING_EOC 137
1347#define ASN1_R_MISSING_SECOND_NUMBER 138
1348#define ASN1_R_MISSING_VALUE 189
1349#define ASN1_R_MSTRING_NOT_UNIVERSAL 139
1350#define ASN1_R_MSTRING_WRONG_TAG 140
1351#define ASN1_R_NESTED_ASN1_STRING 197
1352#define ASN1_R_NON_HEX_CHARACTERS 141
1353#define ASN1_R_NOT_ASCII_FORMAT 190
1354#define ASN1_R_NOT_ENOUGH_DATA 142
1355#define ASN1_R_NO_CONTENT_TYPE 209
1356#define ASN1_R_NO_DEFAULT_DIGEST 201
1357#define ASN1_R_NO_MATCHING_CHOICE_TYPE 143
1358#define ASN1_R_NO_MULTIPART_BODY_FAILURE 210
1359#define ASN1_R_NO_MULTIPART_BOUNDARY 211
1360#define ASN1_R_NO_SIG_CONTENT_TYPE 212
1361#define ASN1_R_NULL_IS_WRONG_LENGTH 144
1362#define ASN1_R_OBJECT_NOT_ASCII_FORMAT 191
1363#define ASN1_R_ODD_NUMBER_OF_CHARS 145
1364#define ASN1_R_PRIVATE_KEY_HEADER_MISSING 146
1365#define ASN1_R_SECOND_NUMBER_TOO_LARGE 147
1366#define ASN1_R_SEQUENCE_LENGTH_MISMATCH 148
1367#define ASN1_R_SEQUENCE_NOT_CONSTRUCTED 149
1368#define ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG 192
1369#define ASN1_R_SHORT_LINE 150
1370#define ASN1_R_SIG_INVALID_MIME_TYPE 213
1371#define ASN1_R_STREAMING_NOT_SUPPORTED 202
1372#define ASN1_R_STRING_TOO_LONG 151
1373#define ASN1_R_STRING_TOO_SHORT 152
1374#define ASN1_R_TAG_VALUE_TOO_HIGH 153
1375#define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 154
1376#define ASN1_R_TIME_NOT_ASCII_FORMAT 193
1377#define ASN1_R_TOO_LONG 155
1378#define ASN1_R_TYPE_NOT_CONSTRUCTED 156
1379#define ASN1_R_UNABLE_TO_DECODE_RSA_KEY 157
1380#define ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY 158
1381#define ASN1_R_UNEXPECTED_EOC 159
1382#define ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH 215
1383#define ASN1_R_UNKNOWN_FORMAT 160
1384#define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 161
1385#define ASN1_R_UNKNOWN_OBJECT_TYPE 162
1386#define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 163
1387#define ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM 199
1388#define ASN1_R_UNKNOWN_TAG 194
1389#define ASN1_R_UNKOWN_FORMAT 195
1390#define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE 164
1391#define ASN1_R_UNSUPPORTED_CIPHER 165
1392#define ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM 166
1393#define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 167
1394#define ASN1_R_UNSUPPORTED_TYPE 196
1395#define ASN1_R_WRONG_PUBLIC_KEY_TYPE 200
1396#define ASN1_R_WRONG_TAG 168
1397#define ASN1_R_WRONG_TYPE 169
1398
1399#ifdef __cplusplus
1400}
1401#endif
1402#endif
diff --git a/src/lib/libcrypto/asn1/asn1_err.c b/src/lib/libcrypto/asn1/asn1_err.c
deleted file mode 100644
index 6e04d08f31..0000000000
--- a/src/lib/libcrypto/asn1/asn1_err.c
+++ /dev/null
@@ -1,329 +0,0 @@
1/* crypto/asn1/asn1_err.c */
2/* ====================================================================
3 * Copyright (c) 1999-2009 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62#include <openssl/err.h>
63#include <openssl/asn1.h>
64
65/* BEGIN ERROR CODES */
66#ifndef OPENSSL_NO_ERR
67
68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_ASN1,func,0)
69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_ASN1,0,reason)
70
71static ERR_STRING_DATA ASN1_str_functs[]=
72 {
73{ERR_FUNC(ASN1_F_A2D_ASN1_OBJECT), "a2d_ASN1_OBJECT"},
74{ERR_FUNC(ASN1_F_A2I_ASN1_ENUMERATED), "a2i_ASN1_ENUMERATED"},
75{ERR_FUNC(ASN1_F_A2I_ASN1_INTEGER), "a2i_ASN1_INTEGER"},
76{ERR_FUNC(ASN1_F_A2I_ASN1_STRING), "a2i_ASN1_STRING"},
77{ERR_FUNC(ASN1_F_APPEND_EXP), "APPEND_EXP"},
78{ERR_FUNC(ASN1_F_ASN1_BIT_STRING_SET_BIT), "ASN1_BIT_STRING_set_bit"},
79{ERR_FUNC(ASN1_F_ASN1_CB), "ASN1_CB"},
80{ERR_FUNC(ASN1_F_ASN1_CHECK_TLEN), "ASN1_CHECK_TLEN"},
81{ERR_FUNC(ASN1_F_ASN1_COLLATE_PRIMITIVE), "ASN1_COLLATE_PRIMITIVE"},
82{ERR_FUNC(ASN1_F_ASN1_COLLECT), "ASN1_COLLECT"},
83{ERR_FUNC(ASN1_F_ASN1_D2I_EX_PRIMITIVE), "ASN1_D2I_EX_PRIMITIVE"},
84{ERR_FUNC(ASN1_F_ASN1_D2I_FP), "ASN1_d2i_fp"},
85{ERR_FUNC(ASN1_F_ASN1_D2I_READ_BIO), "ASN1_D2I_READ_BIO"},
86{ERR_FUNC(ASN1_F_ASN1_DIGEST), "ASN1_digest"},
87{ERR_FUNC(ASN1_F_ASN1_DO_ADB), "ASN1_DO_ADB"},
88{ERR_FUNC(ASN1_F_ASN1_DUP), "ASN1_dup"},
89{ERR_FUNC(ASN1_F_ASN1_ENUMERATED_SET), "ASN1_ENUMERATED_set"},
90{ERR_FUNC(ASN1_F_ASN1_ENUMERATED_TO_BN), "ASN1_ENUMERATED_to_BN"},
91{ERR_FUNC(ASN1_F_ASN1_EX_C2I), "ASN1_EX_C2I"},
92{ERR_FUNC(ASN1_F_ASN1_FIND_END), "ASN1_FIND_END"},
93{ERR_FUNC(ASN1_F_ASN1_GENERALIZEDTIME_ADJ), "ASN1_GENERALIZEDTIME_adj"},
94{ERR_FUNC(ASN1_F_ASN1_GENERALIZEDTIME_SET), "ASN1_GENERALIZEDTIME_set"},
95{ERR_FUNC(ASN1_F_ASN1_GENERATE_V3), "ASN1_generate_v3"},
96{ERR_FUNC(ASN1_F_ASN1_GET_OBJECT), "ASN1_get_object"},
97{ERR_FUNC(ASN1_F_ASN1_HEADER_NEW), "ASN1_HEADER_NEW"},
98{ERR_FUNC(ASN1_F_ASN1_I2D_BIO), "ASN1_i2d_bio"},
99{ERR_FUNC(ASN1_F_ASN1_I2D_FP), "ASN1_i2d_fp"},
100{ERR_FUNC(ASN1_F_ASN1_INTEGER_SET), "ASN1_INTEGER_set"},
101{ERR_FUNC(ASN1_F_ASN1_INTEGER_TO_BN), "ASN1_INTEGER_to_BN"},
102{ERR_FUNC(ASN1_F_ASN1_ITEM_D2I_FP), "ASN1_item_d2i_fp"},
103{ERR_FUNC(ASN1_F_ASN1_ITEM_DUP), "ASN1_item_dup"},
104{ERR_FUNC(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW), "ASN1_ITEM_EX_COMBINE_NEW"},
105{ERR_FUNC(ASN1_F_ASN1_ITEM_EX_D2I), "ASN1_ITEM_EX_D2I"},
106{ERR_FUNC(ASN1_F_ASN1_ITEM_I2D_BIO), "ASN1_item_i2d_bio"},
107{ERR_FUNC(ASN1_F_ASN1_ITEM_I2D_FP), "ASN1_item_i2d_fp"},
108{ERR_FUNC(ASN1_F_ASN1_ITEM_PACK), "ASN1_item_pack"},
109{ERR_FUNC(ASN1_F_ASN1_ITEM_SIGN), "ASN1_item_sign"},
110{ERR_FUNC(ASN1_F_ASN1_ITEM_UNPACK), "ASN1_item_unpack"},
111{ERR_FUNC(ASN1_F_ASN1_ITEM_VERIFY), "ASN1_item_verify"},
112{ERR_FUNC(ASN1_F_ASN1_MBSTRING_NCOPY), "ASN1_mbstring_ncopy"},
113{ERR_FUNC(ASN1_F_ASN1_OBJECT_NEW), "ASN1_OBJECT_new"},
114{ERR_FUNC(ASN1_F_ASN1_OUTPUT_DATA), "ASN1_OUTPUT_DATA"},
115{ERR_FUNC(ASN1_F_ASN1_PACK_STRING), "ASN1_pack_string"},
116{ERR_FUNC(ASN1_F_ASN1_PCTX_NEW), "ASN1_PCTX_new"},
117{ERR_FUNC(ASN1_F_ASN1_PKCS5_PBE_SET), "ASN1_PKCS5_PBE_SET"},
118{ERR_FUNC(ASN1_F_ASN1_SEQ_PACK), "ASN1_seq_pack"},
119{ERR_FUNC(ASN1_F_ASN1_SEQ_UNPACK), "ASN1_seq_unpack"},
120{ERR_FUNC(ASN1_F_ASN1_SIGN), "ASN1_sign"},
121{ERR_FUNC(ASN1_F_ASN1_STR2TYPE), "ASN1_STR2TYPE"},
122{ERR_FUNC(ASN1_F_ASN1_STRING_SET), "ASN1_STRING_set"},
123{ERR_FUNC(ASN1_F_ASN1_STRING_TABLE_ADD), "ASN1_STRING_TABLE_add"},
124{ERR_FUNC(ASN1_F_ASN1_STRING_TYPE_NEW), "ASN1_STRING_type_new"},
125{ERR_FUNC(ASN1_F_ASN1_TEMPLATE_EX_D2I), "ASN1_TEMPLATE_EX_D2I"},
126{ERR_FUNC(ASN1_F_ASN1_TEMPLATE_NEW), "ASN1_TEMPLATE_NEW"},
127{ERR_FUNC(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I), "ASN1_TEMPLATE_NOEXP_D2I"},
128{ERR_FUNC(ASN1_F_ASN1_TIME_ADJ), "ASN1_TIME_adj"},
129{ERR_FUNC(ASN1_F_ASN1_TIME_SET), "ASN1_TIME_set"},
130{ERR_FUNC(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING), "ASN1_TYPE_get_int_octetstring"},
131{ERR_FUNC(ASN1_F_ASN1_TYPE_GET_OCTETSTRING), "ASN1_TYPE_get_octetstring"},
132{ERR_FUNC(ASN1_F_ASN1_UNPACK_STRING), "ASN1_unpack_string"},
133{ERR_FUNC(ASN1_F_ASN1_UTCTIME_ADJ), "ASN1_UTCTIME_adj"},
134{ERR_FUNC(ASN1_F_ASN1_UTCTIME_SET), "ASN1_UTCTIME_set"},
135{ERR_FUNC(ASN1_F_ASN1_VERIFY), "ASN1_verify"},
136{ERR_FUNC(ASN1_F_B64_READ_ASN1), "B64_READ_ASN1"},
137{ERR_FUNC(ASN1_F_B64_WRITE_ASN1), "B64_WRITE_ASN1"},
138{ERR_FUNC(ASN1_F_BIO_NEW_NDEF), "BIO_new_NDEF"},
139{ERR_FUNC(ASN1_F_BITSTR_CB), "BITSTR_CB"},
140{ERR_FUNC(ASN1_F_BN_TO_ASN1_ENUMERATED), "BN_to_ASN1_ENUMERATED"},
141{ERR_FUNC(ASN1_F_BN_TO_ASN1_INTEGER), "BN_to_ASN1_INTEGER"},
142{ERR_FUNC(ASN1_F_C2I_ASN1_BIT_STRING), "c2i_ASN1_BIT_STRING"},
143{ERR_FUNC(ASN1_F_C2I_ASN1_INTEGER), "c2i_ASN1_INTEGER"},
144{ERR_FUNC(ASN1_F_C2I_ASN1_OBJECT), "c2i_ASN1_OBJECT"},
145{ERR_FUNC(ASN1_F_COLLECT_DATA), "COLLECT_DATA"},
146{ERR_FUNC(ASN1_F_D2I_ASN1_BIT_STRING), "D2I_ASN1_BIT_STRING"},
147{ERR_FUNC(ASN1_F_D2I_ASN1_BOOLEAN), "d2i_ASN1_BOOLEAN"},
148{ERR_FUNC(ASN1_F_D2I_ASN1_BYTES), "d2i_ASN1_bytes"},
149{ERR_FUNC(ASN1_F_D2I_ASN1_GENERALIZEDTIME), "D2I_ASN1_GENERALIZEDTIME"},
150{ERR_FUNC(ASN1_F_D2I_ASN1_HEADER), "D2I_ASN1_HEADER"},
151{ERR_FUNC(ASN1_F_D2I_ASN1_INTEGER), "D2I_ASN1_INTEGER"},
152{ERR_FUNC(ASN1_F_D2I_ASN1_OBJECT), "d2i_ASN1_OBJECT"},
153{ERR_FUNC(ASN1_F_D2I_ASN1_SET), "d2i_ASN1_SET"},
154{ERR_FUNC(ASN1_F_D2I_ASN1_TYPE_BYTES), "d2i_ASN1_type_bytes"},
155{ERR_FUNC(ASN1_F_D2I_ASN1_UINTEGER), "d2i_ASN1_UINTEGER"},
156{ERR_FUNC(ASN1_F_D2I_ASN1_UTCTIME), "D2I_ASN1_UTCTIME"},
157{ERR_FUNC(ASN1_F_D2I_AUTOPRIVATEKEY), "d2i_AutoPrivateKey"},
158{ERR_FUNC(ASN1_F_D2I_NETSCAPE_RSA), "d2i_Netscape_RSA"},
159{ERR_FUNC(ASN1_F_D2I_NETSCAPE_RSA_2), "D2I_NETSCAPE_RSA_2"},
160{ERR_FUNC(ASN1_F_D2I_PRIVATEKEY), "d2i_PrivateKey"},
161{ERR_FUNC(ASN1_F_D2I_PUBLICKEY), "d2i_PublicKey"},
162{ERR_FUNC(ASN1_F_D2I_RSA_NET), "d2i_RSA_NET"},
163{ERR_FUNC(ASN1_F_D2I_RSA_NET_2), "D2I_RSA_NET_2"},
164{ERR_FUNC(ASN1_F_D2I_X509), "D2I_X509"},
165{ERR_FUNC(ASN1_F_D2I_X509_CINF), "D2I_X509_CINF"},
166{ERR_FUNC(ASN1_F_D2I_X509_PKEY), "d2i_X509_PKEY"},
167{ERR_FUNC(ASN1_F_I2D_ASN1_BIO_STREAM), "i2d_ASN1_bio_stream"},
168{ERR_FUNC(ASN1_F_I2D_ASN1_SET), "i2d_ASN1_SET"},
169{ERR_FUNC(ASN1_F_I2D_ASN1_TIME), "I2D_ASN1_TIME"},
170{ERR_FUNC(ASN1_F_I2D_DSA_PUBKEY), "i2d_DSA_PUBKEY"},
171{ERR_FUNC(ASN1_F_I2D_EC_PUBKEY), "i2d_EC_PUBKEY"},
172{ERR_FUNC(ASN1_F_I2D_PRIVATEKEY), "i2d_PrivateKey"},
173{ERR_FUNC(ASN1_F_I2D_PUBLICKEY), "i2d_PublicKey"},
174{ERR_FUNC(ASN1_F_I2D_RSA_NET), "i2d_RSA_NET"},
175{ERR_FUNC(ASN1_F_I2D_RSA_PUBKEY), "i2d_RSA_PUBKEY"},
176{ERR_FUNC(ASN1_F_LONG_C2I), "LONG_C2I"},
177{ERR_FUNC(ASN1_F_OID_MODULE_INIT), "OID_MODULE_INIT"},
178{ERR_FUNC(ASN1_F_PARSE_TAGGING), "PARSE_TAGGING"},
179{ERR_FUNC(ASN1_F_PKCS5_PBE2_SET_IV), "PKCS5_pbe2_set_iv"},
180{ERR_FUNC(ASN1_F_PKCS5_PBE_SET), "PKCS5_pbe_set"},
181{ERR_FUNC(ASN1_F_PKCS5_PBE_SET0_ALGOR), "PKCS5_pbe_set0_algor"},
182{ERR_FUNC(ASN1_F_SMIME_READ_ASN1), "SMIME_read_ASN1"},
183{ERR_FUNC(ASN1_F_SMIME_TEXT), "SMIME_text"},
184{ERR_FUNC(ASN1_F_X509_CINF_NEW), "X509_CINF_NEW"},
185{ERR_FUNC(ASN1_F_X509_CRL_ADD0_REVOKED), "X509_CRL_add0_revoked"},
186{ERR_FUNC(ASN1_F_X509_INFO_NEW), "X509_INFO_new"},
187{ERR_FUNC(ASN1_F_X509_NAME_ENCODE), "X509_NAME_ENCODE"},
188{ERR_FUNC(ASN1_F_X509_NAME_EX_D2I), "X509_NAME_EX_D2I"},
189{ERR_FUNC(ASN1_F_X509_NAME_EX_NEW), "X509_NAME_EX_NEW"},
190{ERR_FUNC(ASN1_F_X509_NEW), "X509_NEW"},
191{ERR_FUNC(ASN1_F_X509_PKEY_NEW), "X509_PKEY_new"},
192{0,NULL}
193 };
194
195static ERR_STRING_DATA ASN1_str_reasons[]=
196 {
197{ERR_REASON(ASN1_R_ADDING_OBJECT) ,"adding object"},
198{ERR_REASON(ASN1_R_ASN1_PARSE_ERROR) ,"asn1 parse error"},
199{ERR_REASON(ASN1_R_ASN1_SIG_PARSE_ERROR) ,"asn1 sig parse error"},
200{ERR_REASON(ASN1_R_AUX_ERROR) ,"aux error"},
201{ERR_REASON(ASN1_R_BAD_CLASS) ,"bad class"},
202{ERR_REASON(ASN1_R_BAD_OBJECT_HEADER) ,"bad object header"},
203{ERR_REASON(ASN1_R_BAD_PASSWORD_READ) ,"bad password read"},
204{ERR_REASON(ASN1_R_BAD_TAG) ,"bad tag"},
205{ERR_REASON(ASN1_R_BMPSTRING_IS_WRONG_LENGTH),"bmpstring is wrong length"},
206{ERR_REASON(ASN1_R_BN_LIB) ,"bn lib"},
207{ERR_REASON(ASN1_R_BOOLEAN_IS_WRONG_LENGTH),"boolean is wrong length"},
208{ERR_REASON(ASN1_R_BUFFER_TOO_SMALL) ,"buffer too small"},
209{ERR_REASON(ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER),"cipher has no object identifier"},
210{ERR_REASON(ASN1_R_DATA_IS_WRONG) ,"data is wrong"},
211{ERR_REASON(ASN1_R_DECODE_ERROR) ,"decode error"},
212{ERR_REASON(ASN1_R_DECODING_ERROR) ,"decoding error"},
213{ERR_REASON(ASN1_R_DEPTH_EXCEEDED) ,"depth exceeded"},
214{ERR_REASON(ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED),"digest and key type not supported"},
215{ERR_REASON(ASN1_R_ENCODE_ERROR) ,"encode error"},
216{ERR_REASON(ASN1_R_ERROR_GETTING_TIME) ,"error getting time"},
217{ERR_REASON(ASN1_R_ERROR_LOADING_SECTION),"error loading section"},
218{ERR_REASON(ASN1_R_ERROR_PARSING_SET_ELEMENT),"error parsing set element"},
219{ERR_REASON(ASN1_R_ERROR_SETTING_CIPHER_PARAMS),"error setting cipher params"},
220{ERR_REASON(ASN1_R_EXPECTING_AN_INTEGER) ,"expecting an integer"},
221{ERR_REASON(ASN1_R_EXPECTING_AN_OBJECT) ,"expecting an object"},
222{ERR_REASON(ASN1_R_EXPECTING_A_BOOLEAN) ,"expecting a boolean"},
223{ERR_REASON(ASN1_R_EXPECTING_A_TIME) ,"expecting a time"},
224{ERR_REASON(ASN1_R_EXPLICIT_LENGTH_MISMATCH),"explicit length mismatch"},
225{ERR_REASON(ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED),"explicit tag not constructed"},
226{ERR_REASON(ASN1_R_FIELD_MISSING) ,"field missing"},
227{ERR_REASON(ASN1_R_FIRST_NUM_TOO_LARGE) ,"first num too large"},
228{ERR_REASON(ASN1_R_HEADER_TOO_LONG) ,"header too long"},
229{ERR_REASON(ASN1_R_ILLEGAL_BITSTRING_FORMAT),"illegal bitstring format"},
230{ERR_REASON(ASN1_R_ILLEGAL_BOOLEAN) ,"illegal boolean"},
231{ERR_REASON(ASN1_R_ILLEGAL_CHARACTERS) ,"illegal characters"},
232{ERR_REASON(ASN1_R_ILLEGAL_FORMAT) ,"illegal format"},
233{ERR_REASON(ASN1_R_ILLEGAL_HEX) ,"illegal hex"},
234{ERR_REASON(ASN1_R_ILLEGAL_IMPLICIT_TAG) ,"illegal implicit tag"},
235{ERR_REASON(ASN1_R_ILLEGAL_INTEGER) ,"illegal integer"},
236{ERR_REASON(ASN1_R_ILLEGAL_NESTED_TAGGING),"illegal nested tagging"},
237{ERR_REASON(ASN1_R_ILLEGAL_NULL) ,"illegal null"},
238{ERR_REASON(ASN1_R_ILLEGAL_NULL_VALUE) ,"illegal null value"},
239{ERR_REASON(ASN1_R_ILLEGAL_OBJECT) ,"illegal object"},
240{ERR_REASON(ASN1_R_ILLEGAL_OPTIONAL_ANY) ,"illegal optional any"},
241{ERR_REASON(ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE),"illegal options on item template"},
242{ERR_REASON(ASN1_R_ILLEGAL_TAGGED_ANY) ,"illegal tagged any"},
243{ERR_REASON(ASN1_R_ILLEGAL_TIME_VALUE) ,"illegal time value"},
244{ERR_REASON(ASN1_R_INTEGER_NOT_ASCII_FORMAT),"integer not ascii format"},
245{ERR_REASON(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG),"integer too large for long"},
246{ERR_REASON(ASN1_R_INVALID_BMPSTRING_LENGTH),"invalid bmpstring length"},
247{ERR_REASON(ASN1_R_INVALID_DIGIT) ,"invalid digit"},
248{ERR_REASON(ASN1_R_INVALID_MIME_TYPE) ,"invalid mime type"},
249{ERR_REASON(ASN1_R_INVALID_MODIFIER) ,"invalid modifier"},
250{ERR_REASON(ASN1_R_INVALID_NUMBER) ,"invalid number"},
251{ERR_REASON(ASN1_R_INVALID_OBJECT_ENCODING),"invalid object encoding"},
252{ERR_REASON(ASN1_R_INVALID_SEPARATOR) ,"invalid separator"},
253{ERR_REASON(ASN1_R_INVALID_TIME_FORMAT) ,"invalid time format"},
254{ERR_REASON(ASN1_R_INVALID_UNIVERSALSTRING_LENGTH),"invalid universalstring length"},
255{ERR_REASON(ASN1_R_INVALID_UTF8STRING) ,"invalid utf8string"},
256{ERR_REASON(ASN1_R_IV_TOO_LARGE) ,"iv too large"},
257{ERR_REASON(ASN1_R_LENGTH_ERROR) ,"length error"},
258{ERR_REASON(ASN1_R_LIST_ERROR) ,"list error"},
259{ERR_REASON(ASN1_R_MIME_NO_CONTENT_TYPE) ,"mime no content type"},
260{ERR_REASON(ASN1_R_MIME_PARSE_ERROR) ,"mime parse error"},
261{ERR_REASON(ASN1_R_MIME_SIG_PARSE_ERROR) ,"mime sig parse error"},
262{ERR_REASON(ASN1_R_MISSING_EOC) ,"missing eoc"},
263{ERR_REASON(ASN1_R_MISSING_SECOND_NUMBER),"missing second number"},
264{ERR_REASON(ASN1_R_MISSING_VALUE) ,"missing value"},
265{ERR_REASON(ASN1_R_MSTRING_NOT_UNIVERSAL),"mstring not universal"},
266{ERR_REASON(ASN1_R_MSTRING_WRONG_TAG) ,"mstring wrong tag"},
267{ERR_REASON(ASN1_R_NESTED_ASN1_STRING) ,"nested asn1 string"},
268{ERR_REASON(ASN1_R_NON_HEX_CHARACTERS) ,"non hex characters"},
269{ERR_REASON(ASN1_R_NOT_ASCII_FORMAT) ,"not ascii format"},
270{ERR_REASON(ASN1_R_NOT_ENOUGH_DATA) ,"not enough data"},
271{ERR_REASON(ASN1_R_NO_CONTENT_TYPE) ,"no content type"},
272{ERR_REASON(ASN1_R_NO_DEFAULT_DIGEST) ,"no default digest"},
273{ERR_REASON(ASN1_R_NO_MATCHING_CHOICE_TYPE),"no matching choice type"},
274{ERR_REASON(ASN1_R_NO_MULTIPART_BODY_FAILURE),"no multipart body failure"},
275{ERR_REASON(ASN1_R_NO_MULTIPART_BOUNDARY),"no multipart boundary"},
276{ERR_REASON(ASN1_R_NO_SIG_CONTENT_TYPE) ,"no sig content type"},
277{ERR_REASON(ASN1_R_NULL_IS_WRONG_LENGTH) ,"null is wrong length"},
278{ERR_REASON(ASN1_R_OBJECT_NOT_ASCII_FORMAT),"object not ascii format"},
279{ERR_REASON(ASN1_R_ODD_NUMBER_OF_CHARS) ,"odd number of chars"},
280{ERR_REASON(ASN1_R_PRIVATE_KEY_HEADER_MISSING),"private key header missing"},
281{ERR_REASON(ASN1_R_SECOND_NUMBER_TOO_LARGE),"second number too large"},
282{ERR_REASON(ASN1_R_SEQUENCE_LENGTH_MISMATCH),"sequence length mismatch"},
283{ERR_REASON(ASN1_R_SEQUENCE_NOT_CONSTRUCTED),"sequence not constructed"},
284{ERR_REASON(ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG),"sequence or set needs config"},
285{ERR_REASON(ASN1_R_SHORT_LINE) ,"short line"},
286{ERR_REASON(ASN1_R_SIG_INVALID_MIME_TYPE),"sig invalid mime type"},
287{ERR_REASON(ASN1_R_STREAMING_NOT_SUPPORTED),"streaming not supported"},
288{ERR_REASON(ASN1_R_STRING_TOO_LONG) ,"string too long"},
289{ERR_REASON(ASN1_R_STRING_TOO_SHORT) ,"string too short"},
290{ERR_REASON(ASN1_R_TAG_VALUE_TOO_HIGH) ,"tag value too high"},
291{ERR_REASON(ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD),"the asn1 object identifier is not known for this md"},
292{ERR_REASON(ASN1_R_TIME_NOT_ASCII_FORMAT),"time not ascii format"},
293{ERR_REASON(ASN1_R_TOO_LONG) ,"too long"},
294{ERR_REASON(ASN1_R_TYPE_NOT_CONSTRUCTED) ,"type not constructed"},
295{ERR_REASON(ASN1_R_UNABLE_TO_DECODE_RSA_KEY),"unable to decode rsa key"},
296{ERR_REASON(ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY),"unable to decode rsa private key"},
297{ERR_REASON(ASN1_R_UNEXPECTED_EOC) ,"unexpected eoc"},
298{ERR_REASON(ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH),"universalstring is wrong length"},
299{ERR_REASON(ASN1_R_UNKNOWN_FORMAT) ,"unknown format"},
300{ERR_REASON(ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM),"unknown message digest algorithm"},
301{ERR_REASON(ASN1_R_UNKNOWN_OBJECT_TYPE) ,"unknown object type"},
302{ERR_REASON(ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE),"unknown public key type"},
303{ERR_REASON(ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM),"unknown signature algorithm"},
304{ERR_REASON(ASN1_R_UNKNOWN_TAG) ,"unknown tag"},
305{ERR_REASON(ASN1_R_UNKOWN_FORMAT) ,"unkown format"},
306{ERR_REASON(ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE),"unsupported any defined by type"},
307{ERR_REASON(ASN1_R_UNSUPPORTED_CIPHER) ,"unsupported cipher"},
308{ERR_REASON(ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM),"unsupported encryption algorithm"},
309{ERR_REASON(ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE),"unsupported public key type"},
310{ERR_REASON(ASN1_R_UNSUPPORTED_TYPE) ,"unsupported type"},
311{ERR_REASON(ASN1_R_WRONG_PUBLIC_KEY_TYPE),"wrong public key type"},
312{ERR_REASON(ASN1_R_WRONG_TAG) ,"wrong tag"},
313{ERR_REASON(ASN1_R_WRONG_TYPE) ,"wrong type"},
314{0,NULL}
315 };
316
317#endif
318
319void ERR_load_ASN1_strings(void)
320 {
321#ifndef OPENSSL_NO_ERR
322
323 if (ERR_func_error_string(ASN1_str_functs[0].error) == NULL)
324 {
325 ERR_load_strings(0,ASN1_str_functs);
326 ERR_load_strings(0,ASN1_str_reasons);
327 }
328#endif
329 }
diff --git a/src/lib/libcrypto/asn1/asn1_gen.c b/src/lib/libcrypto/asn1/asn1_gen.c
deleted file mode 100644
index 4fc241908f..0000000000
--- a/src/lib/libcrypto/asn1/asn1_gen.c
+++ /dev/null
@@ -1,854 +0,0 @@
1/* asn1_gen.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2002 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include "cryptlib.h"
60#include <openssl/asn1.h>
61#include <openssl/x509v3.h>
62
63#define ASN1_GEN_FLAG 0x10000
64#define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
65#define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
66#define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
67#define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
68#define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
69#define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
70#define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
71#define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
72
73#define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
74
75#define ASN1_FLAG_EXP_MAX 20
76
77/* Input formats */
78
79/* ASCII: default */
80#define ASN1_GEN_FORMAT_ASCII 1
81/* UTF8 */
82#define ASN1_GEN_FORMAT_UTF8 2
83/* Hex */
84#define ASN1_GEN_FORMAT_HEX 3
85/* List of bits */
86#define ASN1_GEN_FORMAT_BITLIST 4
87
88
89struct tag_name_st
90 {
91 const char *strnam;
92 int len;
93 int tag;
94 };
95
96typedef struct
97 {
98 int exp_tag;
99 int exp_class;
100 int exp_constructed;
101 int exp_pad;
102 long exp_len;
103 } tag_exp_type;
104
105typedef struct
106 {
107 int imp_tag;
108 int imp_class;
109 int utype;
110 int format;
111 const char *str;
112 tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
113 int exp_count;
114 } tag_exp_arg;
115
116static int bitstr_cb(const char *elem, int len, void *bitstr);
117static int asn1_cb(const char *elem, int len, void *bitstr);
118static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed, int exp_pad, int imp_ok);
119static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass);
120static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf);
121static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
122static int asn1_str2tag(const char *tagstr, int len);
123
124ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
125 {
126 X509V3_CTX cnf;
127
128 if (!nconf)
129 return ASN1_generate_v3(str, NULL);
130
131 X509V3_set_nconf(&cnf, nconf);
132 return ASN1_generate_v3(str, &cnf);
133 }
134
135ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
136 {
137 ASN1_TYPE *ret;
138 tag_exp_arg asn1_tags;
139 tag_exp_type *etmp;
140
141 int i, len;
142
143 unsigned char *orig_der = NULL, *new_der = NULL;
144 const unsigned char *cpy_start;
145 unsigned char *p;
146 const unsigned char *cp;
147 int cpy_len;
148 long hdr_len;
149 int hdr_constructed = 0, hdr_tag, hdr_class;
150 int r;
151
152 asn1_tags.imp_tag = -1;
153 asn1_tags.imp_class = -1;
154 asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
155 asn1_tags.exp_count = 0;
156 if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0)
157 return NULL;
158
159 if ((asn1_tags.utype == V_ASN1_SEQUENCE) || (asn1_tags.utype == V_ASN1_SET))
160 {
161 if (!cnf)
162 {
163 ASN1err(ASN1_F_ASN1_GENERATE_V3, ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG);
164 return NULL;
165 }
166 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf);
167 }
168 else
169 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
170
171 if (!ret)
172 return NULL;
173
174 /* If no tagging return base type */
175 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
176 return ret;
177
178 /* Generate the encoding */
179 cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
180 ASN1_TYPE_free(ret);
181 ret = NULL;
182 /* Set point to start copying for modified encoding */
183 cpy_start = orig_der;
184
185 /* Do we need IMPLICIT tagging? */
186 if (asn1_tags.imp_tag != -1)
187 {
188 /* If IMPLICIT we will replace the underlying tag */
189 /* Skip existing tag+len */
190 r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class, cpy_len);
191 if (r & 0x80)
192 goto err;
193 /* Update copy length */
194 cpy_len -= cpy_start - orig_der;
195 /* For IMPLICIT tagging the length should match the
196 * original length and constructed flag should be
197 * consistent.
198 */
199 if (r & 0x1)
200 {
201 /* Indefinite length constructed */
202 hdr_constructed = 2;
203 hdr_len = 0;
204 }
205 else
206 /* Just retain constructed flag */
207 hdr_constructed = r & V_ASN1_CONSTRUCTED;
208 /* Work out new length with IMPLICIT tag: ignore constructed
209 * because it will mess up if indefinite length
210 */
211 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
212 }
213 else
214 len = cpy_len;
215
216 /* Work out length in any EXPLICIT, starting from end */
217
218 for(i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1; i < asn1_tags.exp_count; i++, etmp--)
219 {
220 /* Content length: number of content octets + any padding */
221 len += etmp->exp_pad;
222 etmp->exp_len = len;
223 /* Total object length: length including new header */
224 len = ASN1_object_size(0, len, etmp->exp_tag);
225 }
226
227 /* Allocate buffer for new encoding */
228
229 new_der = OPENSSL_malloc(len);
230 if (!new_der)
231 goto err;
232
233 /* Generate tagged encoding */
234
235 p = new_der;
236
237 /* Output explicit tags first */
238
239 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count; i++, etmp++)
240 {
241 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
242 etmp->exp_tag, etmp->exp_class);
243 if (etmp->exp_pad)
244 *p++ = 0;
245 }
246
247 /* If IMPLICIT, output tag */
248
249 if (asn1_tags.imp_tag != -1)
250 {
251 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
252 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
253 || asn1_tags.imp_tag == V_ASN1_SET) )
254 hdr_constructed = V_ASN1_CONSTRUCTED;
255 ASN1_put_object(&p, hdr_constructed, hdr_len,
256 asn1_tags.imp_tag, asn1_tags.imp_class);
257 }
258
259 /* Copy across original encoding */
260 memcpy(p, cpy_start, cpy_len);
261
262 cp = new_der;
263
264 /* Obtain new ASN1_TYPE structure */
265 ret = d2i_ASN1_TYPE(NULL, &cp, len);
266
267 err:
268 if (orig_der)
269 OPENSSL_free(orig_der);
270 if (new_der)
271 OPENSSL_free(new_der);
272
273 return ret;
274
275 }
276
277static int asn1_cb(const char *elem, int len, void *bitstr)
278 {
279 tag_exp_arg *arg = bitstr;
280 int i;
281 int utype;
282 int vlen = 0;
283 const char *p, *vstart = NULL;
284
285 int tmp_tag, tmp_class;
286
287 for(i = 0, p = elem; i < len; p++, i++)
288 {
289 /* Look for the ':' in name value pairs */
290 if (*p == ':')
291 {
292 vstart = p + 1;
293 vlen = len - (vstart - elem);
294 len = p - elem;
295 break;
296 }
297 }
298
299 utype = asn1_str2tag(elem, len);
300
301 if (utype == -1)
302 {
303 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
304 ERR_add_error_data(2, "tag=", elem);
305 return -1;
306 }
307
308 /* If this is not a modifier mark end of string and exit */
309 if (!(utype & ASN1_GEN_FLAG))
310 {
311 arg->utype = utype;
312 arg->str = vstart;
313 /* If no value and not end of string, error */
314 if (!vstart && elem[len])
315 {
316 ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
317 return -1;
318 }
319 return 0;
320 }
321
322 switch(utype)
323 {
324
325 case ASN1_GEN_FLAG_IMP:
326 /* Check for illegal multiple IMPLICIT tagging */
327 if (arg->imp_tag != -1)
328 {
329 ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
330 return -1;
331 }
332 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
333 return -1;
334 break;
335
336 case ASN1_GEN_FLAG_EXP:
337
338 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
339 return -1;
340 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
341 return -1;
342 break;
343
344 case ASN1_GEN_FLAG_SEQWRAP:
345 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
346 return -1;
347 break;
348
349 case ASN1_GEN_FLAG_SETWRAP:
350 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
351 return -1;
352 break;
353
354 case ASN1_GEN_FLAG_BITWRAP:
355 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
356 return -1;
357 break;
358
359 case ASN1_GEN_FLAG_OCTWRAP:
360 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
361 return -1;
362 break;
363
364 case ASN1_GEN_FLAG_FORMAT:
365 if (!strncmp(vstart, "ASCII", 5))
366 arg->format = ASN1_GEN_FORMAT_ASCII;
367 else if (!strncmp(vstart, "UTF8", 4))
368 arg->format = ASN1_GEN_FORMAT_UTF8;
369 else if (!strncmp(vstart, "HEX", 3))
370 arg->format = ASN1_GEN_FORMAT_HEX;
371 else if (!strncmp(vstart, "BITLIST", 3))
372 arg->format = ASN1_GEN_FORMAT_BITLIST;
373 else
374 {
375 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKOWN_FORMAT);
376 return -1;
377 }
378 break;
379
380 }
381
382 return 1;
383
384 }
385
386static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
387 {
388 char erch[2];
389 long tag_num;
390 char *eptr;
391 if (!vstart)
392 return 0;
393 tag_num = strtoul(vstart, &eptr, 10);
394 /* Check we haven't gone past max length: should be impossible */
395 if (eptr && *eptr && (eptr > vstart + vlen))
396 return 0;
397 if (tag_num < 0)
398 {
399 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
400 return 0;
401 }
402 *ptag = tag_num;
403 /* If we have non numeric characters, parse them */
404 if (eptr)
405 vlen -= eptr - vstart;
406 else
407 vlen = 0;
408 if (vlen)
409 {
410 switch (*eptr)
411 {
412
413 case 'U':
414 *pclass = V_ASN1_UNIVERSAL;
415 break;
416
417 case 'A':
418 *pclass = V_ASN1_APPLICATION;
419 break;
420
421 case 'P':
422 *pclass = V_ASN1_PRIVATE;
423 break;
424
425 case 'C':
426 *pclass = V_ASN1_CONTEXT_SPECIFIC;
427 break;
428
429 default:
430 erch[0] = *eptr;
431 erch[1] = 0;
432 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
433 ERR_add_error_data(2, "Char=", erch);
434 return 0;
435 break;
436
437 }
438 }
439 else
440 *pclass = V_ASN1_CONTEXT_SPECIFIC;
441
442 return 1;
443
444 }
445
446/* Handle multiple types: SET and SEQUENCE */
447
448static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
449 {
450 ASN1_TYPE *ret = NULL;
451 STACK_OF(ASN1_TYPE) *sk = NULL;
452 STACK_OF(CONF_VALUE) *sect = NULL;
453 unsigned char *der = NULL;
454 int derlen;
455 int i;
456 sk = sk_ASN1_TYPE_new_null();
457 if (!sk)
458 goto bad;
459 if (section)
460 {
461 if (!cnf)
462 goto bad;
463 sect = X509V3_get_section(cnf, (char *)section);
464 if (!sect)
465 goto bad;
466 for (i = 0; i < sk_CONF_VALUE_num(sect); i++)
467 {
468 ASN1_TYPE *typ = ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
469 if (!typ)
470 goto bad;
471 if (!sk_ASN1_TYPE_push(sk, typ))
472 goto bad;
473 }
474 }
475
476 /* Now we has a STACK of the components, convert to the correct form */
477
478 if (utype == V_ASN1_SET)
479 derlen = i2d_ASN1_SET_ANY(sk, &der);
480 else
481 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
482
483 if (derlen < 0)
484 goto bad;
485
486 if (!(ret = ASN1_TYPE_new()))
487 goto bad;
488
489 if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
490 goto bad;
491
492 ret->type = utype;
493
494 ret->value.asn1_string->data = der;
495 ret->value.asn1_string->length = derlen;
496
497 der = NULL;
498
499 bad:
500
501 if (der)
502 OPENSSL_free(der);
503
504 if (sk)
505 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
506 if (sect)
507 X509V3_section_free(cnf, sect);
508
509 return ret;
510 }
511
512static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed, int exp_pad, int imp_ok)
513 {
514 tag_exp_type *exp_tmp;
515 /* Can only have IMPLICIT if permitted */
516 if ((arg->imp_tag != -1) && !imp_ok)
517 {
518 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
519 return 0;
520 }
521
522 if (arg->exp_count == ASN1_FLAG_EXP_MAX)
523 {
524 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
525 return 0;
526 }
527
528 exp_tmp = &arg->exp_list[arg->exp_count++];
529
530 /* If IMPLICIT set tag to implicit value then
531 * reset implicit tag since it has been used.
532 */
533 if (arg->imp_tag != -1)
534 {
535 exp_tmp->exp_tag = arg->imp_tag;
536 exp_tmp->exp_class = arg->imp_class;
537 arg->imp_tag = -1;
538 arg->imp_class = -1;
539 }
540 else
541 {
542 exp_tmp->exp_tag = exp_tag;
543 exp_tmp->exp_class = exp_class;
544 }
545 exp_tmp->exp_constructed = exp_constructed;
546 exp_tmp->exp_pad = exp_pad;
547
548 return 1;
549 }
550
551
552static int asn1_str2tag(const char *tagstr, int len)
553 {
554 unsigned int i;
555 static const struct tag_name_st *tntmp, tnst [] = {
556 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
557 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
558 ASN1_GEN_STR("NULL", V_ASN1_NULL),
559 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
560 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
561 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
562 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
563 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
564 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
565 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
566 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
567 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
568 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
569 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
570 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
571 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
572 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
573 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
574 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
575 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
576 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
577 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
578 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
579 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
580 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
581 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
582 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
583 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
584 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
585 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
586 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
587 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
588 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
589 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
590 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
591 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
592
593 /* Special cases */
594 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
595 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
596 ASN1_GEN_STR("SET", V_ASN1_SET),
597 /* type modifiers */
598 /* Explicit tag */
599 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
600 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
601 /* Implicit tag */
602 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
603 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
604 /* OCTET STRING wrapper */
605 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
606 /* SEQUENCE wrapper */
607 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
608 /* SET wrapper */
609 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
610 /* BIT STRING wrapper */
611 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
612 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
613 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
614 };
615
616 if (len == -1)
617 len = strlen(tagstr);
618
619 tntmp = tnst;
620 for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++)
621 {
622 if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
623 return tntmp->tag;
624 }
625
626 return -1;
627 }
628
629static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
630 {
631 ASN1_TYPE *atmp = NULL;
632
633 CONF_VALUE vtmp;
634
635 unsigned char *rdata;
636 long rdlen;
637
638 int no_unused = 1;
639
640 if (!(atmp = ASN1_TYPE_new()))
641 {
642 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
643 return NULL;
644 }
645
646 if (!str)
647 str = "";
648
649 switch(utype)
650 {
651
652 case V_ASN1_NULL:
653 if (str && *str)
654 {
655 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
656 goto bad_form;
657 }
658 break;
659
660 case V_ASN1_BOOLEAN:
661 if (format != ASN1_GEN_FORMAT_ASCII)
662 {
663 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
664 goto bad_form;
665 }
666 vtmp.name = NULL;
667 vtmp.section = NULL;
668 vtmp.value = (char *)str;
669 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
670 {
671 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
672 goto bad_str;
673 }
674 break;
675
676 case V_ASN1_INTEGER:
677 case V_ASN1_ENUMERATED:
678 if (format != ASN1_GEN_FORMAT_ASCII)
679 {
680 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
681 goto bad_form;
682 }
683 if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str)))
684 {
685 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
686 goto bad_str;
687 }
688 break;
689
690 case V_ASN1_OBJECT:
691 if (format != ASN1_GEN_FORMAT_ASCII)
692 {
693 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
694 goto bad_form;
695 }
696 if (!(atmp->value.object = OBJ_txt2obj(str, 0)))
697 {
698 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
699 goto bad_str;
700 }
701 break;
702
703 case V_ASN1_UTCTIME:
704 case V_ASN1_GENERALIZEDTIME:
705 if (format != ASN1_GEN_FORMAT_ASCII)
706 {
707 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
708 goto bad_form;
709 }
710 if (!(atmp->value.asn1_string = ASN1_STRING_new()))
711 {
712 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
713 goto bad_str;
714 }
715 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1))
716 {
717 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
718 goto bad_str;
719 }
720 atmp->value.asn1_string->type = utype;
721 if (!ASN1_TIME_check(atmp->value.asn1_string))
722 {
723 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
724 goto bad_str;
725 }
726
727 break;
728
729 case V_ASN1_BMPSTRING:
730 case V_ASN1_PRINTABLESTRING:
731 case V_ASN1_IA5STRING:
732 case V_ASN1_T61STRING:
733 case V_ASN1_UTF8STRING:
734 case V_ASN1_VISIBLESTRING:
735 case V_ASN1_UNIVERSALSTRING:
736 case V_ASN1_GENERALSTRING:
737 case V_ASN1_NUMERICSTRING:
738
739 if (format == ASN1_GEN_FORMAT_ASCII)
740 format = MBSTRING_ASC;
741 else if (format == ASN1_GEN_FORMAT_UTF8)
742 format = MBSTRING_UTF8;
743 else
744 {
745 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
746 goto bad_form;
747 }
748
749
750 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
751 -1, format, ASN1_tag2bit(utype)) <= 0)
752 {
753 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
754 goto bad_str;
755 }
756
757
758 break;
759
760 case V_ASN1_BIT_STRING:
761
762 case V_ASN1_OCTET_STRING:
763
764 if (!(atmp->value.asn1_string = ASN1_STRING_new()))
765 {
766 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
767 goto bad_form;
768 }
769
770 if (format == ASN1_GEN_FORMAT_HEX)
771 {
772
773 if (!(rdata = string_to_hex((char *)str, &rdlen)))
774 {
775 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
776 goto bad_str;
777 }
778
779 atmp->value.asn1_string->data = rdata;
780 atmp->value.asn1_string->length = rdlen;
781 atmp->value.asn1_string->type = utype;
782
783 }
784 else if (format == ASN1_GEN_FORMAT_ASCII)
785 ASN1_STRING_set(atmp->value.asn1_string, str, -1);
786 else if ((format == ASN1_GEN_FORMAT_BITLIST) && (utype == V_ASN1_BIT_STRING))
787 {
788 if (!CONF_parse_list(str, ',', 1, bitstr_cb, atmp->value.bit_string))
789 {
790 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
791 goto bad_str;
792 }
793 no_unused = 0;
794
795 }
796 else
797 {
798 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
799 goto bad_form;
800 }
801
802 if ((utype == V_ASN1_BIT_STRING) && no_unused)
803 {
804 atmp->value.asn1_string->flags
805 &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
806 atmp->value.asn1_string->flags
807 |= ASN1_STRING_FLAG_BITS_LEFT;
808 }
809
810
811 break;
812
813 default:
814 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
815 goto bad_str;
816 break;
817 }
818
819
820 atmp->type = utype;
821 return atmp;
822
823
824 bad_str:
825 ERR_add_error_data(2, "string=", str);
826 bad_form:
827
828 ASN1_TYPE_free(atmp);
829 return NULL;
830
831 }
832
833static int bitstr_cb(const char *elem, int len, void *bitstr)
834 {
835 long bitnum;
836 char *eptr;
837 if (!elem)
838 return 0;
839 bitnum = strtoul(elem, &eptr, 10);
840 if (eptr && *eptr && (eptr != elem + len))
841 return 0;
842 if (bitnum < 0)
843 {
844 ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
845 return 0;
846 }
847 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1))
848 {
849 ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
850 return 0;
851 }
852 return 1;
853 }
854
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
deleted file mode 100644
index 1bcb44aee2..0000000000
--- a/src/lib/libcrypto/asn1/asn1_lib.c
+++ /dev/null
@@ -1,482 +0,0 @@
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 <limits.h>
61#include "cryptlib.h"
62#include <openssl/asn1.h>
63#include <openssl/asn1_mac.h>
64
65static int asn1_get_length(const unsigned char **pp,int *inf,long *rl,int max);
66static void asn1_put_length(unsigned char **pp, int length);
67const char ASN1_version[]="ASN.1" OPENSSL_VERSION_PTEXT;
68
69static int _asn1_check_infinite_end(const unsigned char **p, long len)
70 {
71 /* If there is 0 or 1 byte left, the length check should pick
72 * things up */
73 if (len <= 0)
74 return(1);
75 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0))
76 {
77 (*p)+=2;
78 return(1);
79 }
80 return(0);
81 }
82
83int ASN1_check_infinite_end(unsigned char **p, long len)
84 {
85 return _asn1_check_infinite_end((const unsigned char **)p, len);
86 }
87
88int ASN1_const_check_infinite_end(const unsigned char **p, long len)
89 {
90 return _asn1_check_infinite_end(p, len);
91 }
92
93
94int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
95 int *pclass, long omax)
96 {
97 int i,ret;
98 long l;
99 const unsigned char *p= *pp;
100 int tag,xclass,inf;
101 long max=omax;
102
103 if (!max) goto err;
104 ret=(*p&V_ASN1_CONSTRUCTED);
105 xclass=(*p&V_ASN1_PRIVATE);
106 i= *p&V_ASN1_PRIMITIVE_TAG;
107 if (i == V_ASN1_PRIMITIVE_TAG)
108 { /* high-tag */
109 p++;
110 if (--max == 0) goto err;
111 l=0;
112 while (*p&0x80)
113 {
114 l<<=7L;
115 l|= *(p++)&0x7f;
116 if (--max == 0) goto err;
117 if (l > (INT_MAX >> 7L)) goto err;
118 }
119 l<<=7L;
120 l|= *(p++)&0x7f;
121 tag=(int)l;
122 if (--max == 0) goto err;
123 }
124 else
125 {
126 tag=i;
127 p++;
128 if (--max == 0) goto err;
129 }
130 *ptag=tag;
131 *pclass=xclass;
132 if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
133
134#if 0
135 fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
136 (int)p,*plength,omax,(int)*pp,(int)(p+ *plength),
137 (int)(omax+ *pp));
138
139#endif
140 if (*plength > (omax - (p - *pp)))
141 {
142 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);
143 /* Set this so that even if things are not long enough
144 * the values are set correctly */
145 ret|=0x80;
146 }
147 *pp=p;
148 return(ret|inf);
149err:
150 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG);
151 return(0x80);
152 }
153
154static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max)
155 {
156 const unsigned char *p= *pp;
157 unsigned long ret=0;
158 unsigned int i;
159
160 if (max-- < 1) return(0);
161 if (*p == 0x80)
162 {
163 *inf=1;
164 ret=0;
165 p++;
166 }
167 else
168 {
169 *inf=0;
170 i= *p&0x7f;
171 if (*(p++) & 0x80)
172 {
173 if (i > sizeof(long))
174 return 0;
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 if (ret > LONG_MAX)
187 return 0;
188 *pp=p;
189 *rl=(long)ret;
190 return(1);
191 }
192
193/* class 0 is constructed
194 * constructed == 2 for indefinite length constructed */
195void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
196 int xclass)
197 {
198 unsigned char *p= *pp;
199 int i, ttag;
200
201 i=(constructed)?V_ASN1_CONSTRUCTED:0;
202 i|=(xclass&V_ASN1_PRIVATE);
203 if (tag < 31)
204 *(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);
205 else
206 {
207 *(p++)=i|V_ASN1_PRIMITIVE_TAG;
208 for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7;
209 ttag = i;
210 while(i-- > 0)
211 {
212 p[i] = tag & 0x7f;
213 if(i != (ttag - 1)) p[i] |= 0x80;
214 tag >>= 7;
215 }
216 p += ttag;
217 }
218 if (constructed == 2)
219 *(p++)=0x80;
220 else
221 asn1_put_length(&p,length);
222 *pp=p;
223 }
224
225int ASN1_put_eoc(unsigned char **pp)
226 {
227 unsigned char *p = *pp;
228 *p++ = 0;
229 *p++ = 0;
230 *pp = p;
231 return 2;
232 }
233
234static void asn1_put_length(unsigned char **pp, int length)
235 {
236 unsigned char *p= *pp;
237 int i,l;
238 if (length <= 127)
239 *(p++)=(unsigned char)length;
240 else
241 {
242 l=length;
243 for (i=0; l > 0; i++)
244 l>>=8;
245 *(p++)=i|0x80;
246 l=i;
247 while (i-- > 0)
248 {
249 p[i]=length&0xff;
250 length>>=8;
251 }
252 p+=l;
253 }
254 *pp=p;
255 }
256
257int ASN1_object_size(int constructed, int length, int tag)
258 {
259 int ret;
260
261 ret=length;
262 ret++;
263 if (tag >= 31)
264 {
265 while (tag > 0)
266 {
267 tag>>=7;
268 ret++;
269 }
270 }
271 if (constructed == 2)
272 return ret + 3;
273 ret++;
274 if (length > 127)
275 {
276 while (length > 0)
277 {
278 length>>=8;
279 ret++;
280 }
281 }
282 return(ret);
283 }
284
285static int _asn1_Finish(ASN1_const_CTX *c)
286 {
287 if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos))
288 {
289 if (!ASN1_const_check_infinite_end(&c->p,c->slen))
290 {
291 c->error=ERR_R_MISSING_ASN1_EOS;
292 return(0);
293 }
294 }
295 if ( ((c->slen != 0) && !(c->inf & 1)) ||
296 ((c->slen < 0) && (c->inf & 1)))
297 {
298 c->error=ERR_R_ASN1_LENGTH_MISMATCH;
299 return(0);
300 }
301 return(1);
302 }
303
304int asn1_Finish(ASN1_CTX *c)
305 {
306 return _asn1_Finish((ASN1_const_CTX *)c);
307 }
308
309int asn1_const_Finish(ASN1_const_CTX *c)
310 {
311 return _asn1_Finish(c);
312 }
313
314int asn1_GetSequence(ASN1_const_CTX *c, long *length)
315 {
316 const unsigned char *q;
317
318 q=c->p;
319 c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
320 *length);
321 if (c->inf & 0x80)
322 {
323 c->error=ERR_R_BAD_GET_ASN1_OBJECT_CALL;
324 return(0);
325 }
326 if (c->tag != V_ASN1_SEQUENCE)
327 {
328 c->error=ERR_R_EXPECTING_AN_ASN1_SEQUENCE;
329 return(0);
330 }
331 (*length)-=(c->p-q);
332 if (c->max && (*length < 0))
333 {
334 c->error=ERR_R_ASN1_LENGTH_MISMATCH;
335 return(0);
336 }
337 if (c->inf == (1|V_ASN1_CONSTRUCTED))
338 c->slen= *length+ *(c->pp)-c->p;
339 c->eos=0;
340 return(1);
341 }
342
343int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
344 {
345 if (str == NULL)
346 return 0;
347 dst->type = str->type;
348 if (!ASN1_STRING_set(dst,str->data,str->length))
349 return 0;
350 dst->flags = str->flags;
351 return 1;
352 }
353
354ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
355 {
356 ASN1_STRING *ret;
357 if (!str)
358 return NULL;
359 ret=ASN1_STRING_new();
360 if (!ret)
361 return NULL;
362 if (!ASN1_STRING_copy(ret,str))
363 {
364 ASN1_STRING_free(ret);
365 return NULL;
366 }
367 return ret;
368 }
369
370int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
371 {
372 unsigned char *c;
373 const char *data=_data;
374
375 if (len < 0)
376 {
377 if (data == NULL)
378 return(0);
379 else
380 len=strlen(data);
381 }
382 if ((str->length < len) || (str->data == NULL))
383 {
384 c=str->data;
385 if (c == NULL)
386 str->data=OPENSSL_malloc(len+1);
387 else
388 str->data=OPENSSL_realloc(c,len+1);
389
390 if (str->data == NULL)
391 {
392 ASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE);
393 str->data=c;
394 return(0);
395 }
396 }
397 str->length=len;
398 if (data != NULL)
399 {
400 memcpy(str->data,data,len);
401 /* an allowance for strings :-) */
402 str->data[len]='\0';
403 }
404 return(1);
405 }
406
407void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
408 {
409 if (str->data)
410 OPENSSL_free(str->data);
411 str->data = data;
412 str->length = len;
413 }
414
415ASN1_STRING *ASN1_STRING_new(void)
416 {
417 return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
418 }
419
420
421ASN1_STRING *ASN1_STRING_type_new(int type)
422 {
423 ASN1_STRING *ret;
424
425 ret=(ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
426 if (ret == NULL)
427 {
428 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);
429 return(NULL);
430 }
431 ret->length=0;
432 ret->type=type;
433 ret->data=NULL;
434 ret->flags=0;
435 return(ret);
436 }
437
438void ASN1_STRING_free(ASN1_STRING *a)
439 {
440 if (a == NULL) return;
441 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
442 OPENSSL_free(a->data);
443 OPENSSL_free(a);
444 }
445
446int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
447 {
448 int i;
449
450 i=(a->length-b->length);
451 if (i == 0)
452 {
453 i=memcmp(a->data,b->data,a->length);
454 if (i == 0)
455 return(a->type-b->type);
456 else
457 return(i);
458 }
459 else
460 return(i);
461 }
462
463void asn1_add_error(const unsigned char *address, int offset)
464 {
465 char buf1[DECIMAL_SIZE(address)+1],buf2[DECIMAL_SIZE(offset)+1];
466
467 BIO_snprintf(buf1,sizeof buf1,"%lu",(unsigned long)address);
468 BIO_snprintf(buf2,sizeof buf2,"%d",offset);
469 ERR_add_error_data(4,"address=",buf1," offset=",buf2);
470 }
471
472int ASN1_STRING_length(const ASN1_STRING *x)
473{ return M_ASN1_STRING_length(x); }
474
475void ASN1_STRING_length_set(ASN1_STRING *x, int len)
476{ M_ASN1_STRING_length_set(x, len); return; }
477
478int ASN1_STRING_type(ASN1_STRING *x)
479{ return M_ASN1_STRING_type(x); }
480
481unsigned char * ASN1_STRING_data(ASN1_STRING *x)
482{ return M_ASN1_STRING_data(x); }
diff --git a/src/lib/libcrypto/asn1/asn1_locl.h b/src/lib/libcrypto/asn1/asn1_locl.h
deleted file mode 100644
index 5aa65e28f5..0000000000
--- a/src/lib/libcrypto/asn1/asn1_locl.h
+++ /dev/null
@@ -1,134 +0,0 @@
1/* asn1t.h */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59/* Internal ASN1 structures and functions: not for application use */
60
61/* ASN1 print context structure */
62
63struct asn1_pctx_st
64 {
65 unsigned long flags;
66 unsigned long nm_flags;
67 unsigned long cert_flags;
68 unsigned long oid_flags;
69 unsigned long str_flags;
70 } /* ASN1_PCTX */;
71
72/* ASN1 public key method structure */
73
74struct evp_pkey_asn1_method_st
75 {
76 int pkey_id;
77 int pkey_base_id;
78 unsigned long pkey_flags;
79
80 char *pem_str;
81 char *info;
82
83 int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub);
84 int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
85 int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
86 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent,
87 ASN1_PCTX *pctx);
88
89 int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf);
90 int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
91 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
92 ASN1_PCTX *pctx);
93
94 int (*pkey_size)(const EVP_PKEY *pk);
95 int (*pkey_bits)(const EVP_PKEY *pk);
96
97 int (*param_decode)(EVP_PKEY *pkey,
98 const unsigned char **pder, int derlen);
99 int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder);
100 int (*param_missing)(const EVP_PKEY *pk);
101 int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
102 int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
103 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
104 ASN1_PCTX *pctx);
105
106 void (*pkey_free)(EVP_PKEY *pkey);
107 int (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2);
108
109 /* Legacy functions for old PEM */
110
111 int (*old_priv_decode)(EVP_PKEY *pkey,
112 const unsigned char **pder, int derlen);
113 int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder);
114
115 } /* EVP_PKEY_ASN1_METHOD */;
116
117/* Method to handle CRL access.
118 * In general a CRL could be very large (several Mb) and can consume large
119 * amounts of resources if stored in memory by multiple processes.
120 * This method allows general CRL operations to be redirected to more
121 * efficient callbacks: for example a CRL entry database.
122 */
123
124#define X509_CRL_METHOD_DYNAMIC 1
125
126struct x509_crl_method_st
127 {
128 int flags;
129 int (*crl_init)(X509_CRL *crl);
130 int (*crl_free)(X509_CRL *crl);
131 int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
132 ASN1_INTEGER *ser, X509_NAME *issuer);
133 int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk);
134 };
diff --git a/src/lib/libcrypto/asn1/asn1_mac.h b/src/lib/libcrypto/asn1/asn1_mac.h
deleted file mode 100644
index 87bd0e9e1d..0000000000
--- a/src/lib/libcrypto/asn1/asn1_mac.h
+++ /dev/null
@@ -1,578 +0,0 @@
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#include <openssl/asn1.h>
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68#ifndef ASN1_MAC_ERR_LIB
69#define ASN1_MAC_ERR_LIB ERR_LIB_ASN1
70#endif
71
72#define ASN1_MAC_H_err(f,r,line) \
73 ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),__FILE__,(line))
74
75#define M_ASN1_D2I_vars(a,type,func) \
76 ASN1_const_CTX c; \
77 type ret=NULL; \
78 \
79 c.pp=(const unsigned char **)pp; \
80 c.q= *(const unsigned char **)pp; \
81 c.error=ERR_R_NESTED_ASN1_ERROR; \
82 if ((a == NULL) || ((*a) == NULL)) \
83 { if ((ret=(type)func()) == NULL) \
84 { c.line=__LINE__; goto err; } } \
85 else ret=(*a);
86
87#define M_ASN1_D2I_Init() \
88 c.p= *(const unsigned char **)pp; \
89 c.max=(length == 0)?0:(c.p+length);
90
91#define M_ASN1_D2I_Finish_2(a) \
92 if (!asn1_const_Finish(&c)) \
93 { c.line=__LINE__; goto err; } \
94 *(const unsigned char **)pp=c.p; \
95 if (a != NULL) (*a)=ret; \
96 return(ret);
97
98#define M_ASN1_D2I_Finish(a,func,e) \
99 M_ASN1_D2I_Finish_2(a); \
100err:\
101 ASN1_MAC_H_err((e),c.error,c.line); \
102 asn1_add_error(*(const unsigned char **)pp,(int)(c.q- *pp)); \
103 if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
104 return(NULL)
105
106#define M_ASN1_D2I_start_sequence() \
107 if (!asn1_GetSequence(&c,&length)) \
108 { c.line=__LINE__; goto err; }
109/* Begin reading ASN1 without a surrounding sequence */
110#define M_ASN1_D2I_begin() \
111 c.slen = length;
112
113/* End reading ASN1 with no check on length */
114#define M_ASN1_D2I_Finish_nolen(a, func, e) \
115 *pp=c.p; \
116 if (a != NULL) (*a)=ret; \
117 return(ret); \
118err:\
119 ASN1_MAC_H_err((e),c.error,c.line); \
120 asn1_add_error(*pp,(int)(c.q- *pp)); \
121 if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
122 return(NULL)
123
124#define M_ASN1_D2I_end_sequence() \
125 (((c.inf&1) == 0)?(c.slen <= 0): \
126 (c.eos=ASN1_const_check_infinite_end(&c.p,c.slen)))
127
128/* Don't use this with d2i_ASN1_BOOLEAN() */
129#define M_ASN1_D2I_get(b, func) \
130 c.q=c.p; \
131 if (func(&(b),&c.p,c.slen) == NULL) \
132 {c.line=__LINE__; goto err; } \
133 c.slen-=(c.p-c.q);
134
135/* Don't use this with d2i_ASN1_BOOLEAN() */
136#define M_ASN1_D2I_get_x(type,b,func) \
137 c.q=c.p; \
138 if (((D2I_OF(type))func)(&(b),&c.p,c.slen) == NULL) \
139 {c.line=__LINE__; goto err; } \
140 c.slen-=(c.p-c.q);
141
142/* use this instead () */
143#define M_ASN1_D2I_get_int(b,func) \
144 c.q=c.p; \
145 if (func(&(b),&c.p,c.slen) < 0) \
146 {c.line=__LINE__; goto err; } \
147 c.slen-=(c.p-c.q);
148
149#define M_ASN1_D2I_get_opt(b,func,type) \
150 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
151 == (V_ASN1_UNIVERSAL|(type)))) \
152 { \
153 M_ASN1_D2I_get(b,func); \
154 }
155
156#define M_ASN1_D2I_get_int_opt(b,func,type) \
157 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
158 == (V_ASN1_UNIVERSAL|(type)))) \
159 { \
160 M_ASN1_D2I_get_int(b,func); \
161 }
162
163#define M_ASN1_D2I_get_imp(b,func, type) \
164 M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
165 c.q=c.p; \
166 if (func(&(b),&c.p,c.slen) == NULL) \
167 {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
168 c.slen-=(c.p-c.q);\
169 M_ASN1_next_prev=_tmp;
170
171#define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
172 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
173 (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
174 { \
175 unsigned char _tmp = M_ASN1_next; \
176 M_ASN1_D2I_get_imp(b,func, type);\
177 }
178
179#define M_ASN1_D2I_get_set(r,func,free_func) \
180 M_ASN1_D2I_get_imp_set(r,func,free_func, \
181 V_ASN1_SET,V_ASN1_UNIVERSAL);
182
183#define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
184 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
185 V_ASN1_SET,V_ASN1_UNIVERSAL);
186
187#define M_ASN1_D2I_get_set_opt(r,func,free_func) \
188 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
189 V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
190 { M_ASN1_D2I_get_set(r,func,free_func); }
191
192#define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \
193 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
194 V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
195 { M_ASN1_D2I_get_set_type(type,r,func,free_func); }
196
197#define M_ASN1_I2D_len_SET_opt(a,f) \
198 if ((a != NULL) && (sk_num(a) != 0)) \
199 M_ASN1_I2D_len_SET(a,f);
200
201#define M_ASN1_I2D_put_SET_opt(a,f) \
202 if ((a != NULL) && (sk_num(a) != 0)) \
203 M_ASN1_I2D_put_SET(a,f);
204
205#define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
206 if ((a != NULL) && (sk_num(a) != 0)) \
207 M_ASN1_I2D_put_SEQUENCE(a,f);
208
209#define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \
210 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
211 M_ASN1_I2D_put_SEQUENCE_type(type,a,f);
212
213#define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
214 if ((c.slen != 0) && \
215 (M_ASN1_next == \
216 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
217 { \
218 M_ASN1_D2I_get_imp_set(b,func,free_func,\
219 tag,V_ASN1_CONTEXT_SPECIFIC); \
220 }
221
222#define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
223 if ((c.slen != 0) && \
224 (M_ASN1_next == \
225 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
226 { \
227 M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
228 tag,V_ASN1_CONTEXT_SPECIFIC); \
229 }
230
231#define M_ASN1_D2I_get_seq(r,func,free_func) \
232 M_ASN1_D2I_get_imp_set(r,func,free_func,\
233 V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
234
235#define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
236 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
237 V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
238
239#define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
240 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
241 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
242 { M_ASN1_D2I_get_seq(r,func,free_func); }
243
244#define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \
245 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
246 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
247 { M_ASN1_D2I_get_seq_type(type,r,func,free_func); }
248
249#define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
250 M_ASN1_D2I_get_imp_set(r,func,free_func,\
251 x,V_ASN1_CONTEXT_SPECIFIC);
252
253#define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
254 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
255 x,V_ASN1_CONTEXT_SPECIFIC);
256
257#define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
258 c.q=c.p; \
259 if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\
260 (void (*)())free_func,a,b) == NULL) \
261 { c.line=__LINE__; goto err; } \
262 c.slen-=(c.p-c.q);
263
264#define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
265 c.q=c.p; \
266 if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
267 free_func,a,b) == NULL) \
268 { c.line=__LINE__; goto err; } \
269 c.slen-=(c.p-c.q);
270
271#define M_ASN1_D2I_get_set_strings(r,func,a,b) \
272 c.q=c.p; \
273 if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
274 { c.line=__LINE__; goto err; } \
275 c.slen-=(c.p-c.q);
276
277#define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
278 if ((c.slen != 0L) && (M_ASN1_next == \
279 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
280 { \
281 int Tinf,Ttag,Tclass; \
282 long Tlen; \
283 \
284 c.q=c.p; \
285 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
286 if (Tinf & 0x80) \
287 { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
288 c.line=__LINE__; goto err; } \
289 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
290 Tlen = c.slen - (c.p - c.q) - 2; \
291 if (func(&(r),&c.p,Tlen) == NULL) \
292 { c.line=__LINE__; goto err; } \
293 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
294 Tlen = c.slen - (c.p - c.q); \
295 if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \
296 { c.error=ERR_R_MISSING_ASN1_EOS; \
297 c.line=__LINE__; goto err; } \
298 }\
299 c.slen-=(c.p-c.q); \
300 }
301
302#define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
303 if ((c.slen != 0) && (M_ASN1_next == \
304 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
305 { \
306 int Tinf,Ttag,Tclass; \
307 long Tlen; \
308 \
309 c.q=c.p; \
310 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
311 if (Tinf & 0x80) \
312 { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
313 c.line=__LINE__; goto err; } \
314 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
315 Tlen = c.slen - (c.p - c.q) - 2; \
316 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
317 (void (*)())free_func, \
318 b,V_ASN1_UNIVERSAL) == NULL) \
319 { c.line=__LINE__; goto err; } \
320 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
321 Tlen = c.slen - (c.p - c.q); \
322 if(!ASN1_check_infinite_end(&c.p, Tlen)) \
323 { c.error=ERR_R_MISSING_ASN1_EOS; \
324 c.line=__LINE__; goto err; } \
325 }\
326 c.slen-=(c.p-c.q); \
327 }
328
329#define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \
330 if ((c.slen != 0) && (M_ASN1_next == \
331 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
332 { \
333 int Tinf,Ttag,Tclass; \
334 long Tlen; \
335 \
336 c.q=c.p; \
337 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
338 if (Tinf & 0x80) \
339 { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
340 c.line=__LINE__; goto err; } \
341 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
342 Tlen = c.slen - (c.p - c.q) - 2; \
343 if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \
344 free_func,b,V_ASN1_UNIVERSAL) == NULL) \
345 { c.line=__LINE__; goto err; } \
346 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
347 Tlen = c.slen - (c.p - c.q); \
348 if(!ASN1_check_infinite_end(&c.p, Tlen)) \
349 { c.error=ERR_R_MISSING_ASN1_EOS; \
350 c.line=__LINE__; goto err; } \
351 }\
352 c.slen-=(c.p-c.q); \
353 }
354
355/* New macros */
356#define M_ASN1_New_Malloc(ret,type) \
357 if ((ret=(type *)OPENSSL_malloc(sizeof(type))) == NULL) \
358 { c.line=__LINE__; goto err2; }
359
360#define M_ASN1_New(arg,func) \
361 if (((arg)=func()) == NULL) return(NULL)
362
363#define M_ASN1_New_Error(a) \
364/* err: ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
365 return(NULL);*/ \
366 err2: ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
367 return(NULL)
368
369
370/* BIG UGLY WARNING! This is so damn ugly I wanna puke. Unfortunately,
371 some macros that use ASN1_const_CTX still insist on writing in the input
372 stream. ARGH! ARGH! ARGH! Let's get rid of this macro package.
373 Please? -- Richard Levitte */
374#define M_ASN1_next (*((unsigned char *)(c.p)))
375#define M_ASN1_next_prev (*((unsigned char *)(c.q)))
376
377/*************************************************/
378
379#define M_ASN1_I2D_vars(a) int r=0,ret=0; \
380 unsigned char *p; \
381 if (a == NULL) return(0)
382
383/* Length Macros */
384#define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
385#define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
386
387#define M_ASN1_I2D_len_SET(a,f) \
388 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
389
390#define M_ASN1_I2D_len_SET_type(type,a,f) \
391 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
392 V_ASN1_UNIVERSAL,IS_SET);
393
394#define M_ASN1_I2D_len_SEQUENCE(a,f) \
395 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
396 IS_SEQUENCE);
397
398#define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
399 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
400 V_ASN1_UNIVERSAL,IS_SEQUENCE)
401
402#define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
403 if ((a != NULL) && (sk_num(a) != 0)) \
404 M_ASN1_I2D_len_SEQUENCE(a,f);
405
406#define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
407 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
408 M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
409
410#define M_ASN1_I2D_len_IMP_SET(a,f,x) \
411 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
412
413#define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
414 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
415 V_ASN1_CONTEXT_SPECIFIC,IS_SET);
416
417#define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
418 if ((a != NULL) && (sk_num(a) != 0)) \
419 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
420 IS_SET);
421
422#define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
423 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
424 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
425 V_ASN1_CONTEXT_SPECIFIC,IS_SET);
426
427#define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
428 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
429 IS_SEQUENCE);
430
431#define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
432 if ((a != NULL) && (sk_num(a) != 0)) \
433 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
434 IS_SEQUENCE);
435
436#define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
437 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
438 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
439 V_ASN1_CONTEXT_SPECIFIC, \
440 IS_SEQUENCE);
441
442#define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
443 if (a != NULL)\
444 { \
445 v=f(a,NULL); \
446 ret+=ASN1_object_size(1,v,mtag); \
447 }
448
449#define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
450 if ((a != NULL) && (sk_num(a) != 0))\
451 { \
452 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
453 ret+=ASN1_object_size(1,v,mtag); \
454 }
455
456#define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
457 if ((a != NULL) && (sk_num(a) != 0))\
458 { \
459 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
460 IS_SEQUENCE); \
461 ret+=ASN1_object_size(1,v,mtag); \
462 }
463
464#define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
465 if ((a != NULL) && (sk_##type##_num(a) != 0))\
466 { \
467 v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
468 V_ASN1_UNIVERSAL, \
469 IS_SEQUENCE); \
470 ret+=ASN1_object_size(1,v,mtag); \
471 }
472
473/* Put Macros */
474#define M_ASN1_I2D_put(a,f) f(a,&p)
475
476#define M_ASN1_I2D_put_IMP_opt(a,f,t) \
477 if (a != NULL) \
478 { \
479 unsigned char *q=p; \
480 f(a,&p); \
481 *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
482 }
483
484#define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
485 V_ASN1_UNIVERSAL,IS_SET)
486#define M_ASN1_I2D_put_SET_type(type,a,f) \
487 i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
488#define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
489 V_ASN1_CONTEXT_SPECIFIC,IS_SET)
490#define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
491 i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
492#define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
493 V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
494
495#define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
496 V_ASN1_UNIVERSAL,IS_SEQUENCE)
497
498#define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
499 i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
500 IS_SEQUENCE)
501
502#define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
503 if ((a != NULL) && (sk_num(a) != 0)) \
504 M_ASN1_I2D_put_SEQUENCE(a,f);
505
506#define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
507 if ((a != NULL) && (sk_num(a) != 0)) \
508 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
509 IS_SET); }
510
511#define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
512 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
513 { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
514 V_ASN1_CONTEXT_SPECIFIC, \
515 IS_SET); }
516
517#define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
518 if ((a != NULL) && (sk_num(a) != 0)) \
519 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
520 IS_SEQUENCE); }
521
522#define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
523 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
524 { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
525 V_ASN1_CONTEXT_SPECIFIC, \
526 IS_SEQUENCE); }
527
528#define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
529 if (a != NULL) \
530 { \
531 ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
532 f(a,&p); \
533 }
534
535#define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
536 if ((a != NULL) && (sk_num(a) != 0)) \
537 { \
538 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
539 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
540 }
541
542#define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
543 if ((a != NULL) && (sk_num(a) != 0)) \
544 { \
545 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
546 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
547 }
548
549#define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
550 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
551 { \
552 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
553 i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
554 IS_SEQUENCE); \
555 }
556
557#define M_ASN1_I2D_seq_total() \
558 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
559 if (pp == NULL) return(r); \
560 p= *pp; \
561 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
562
563#define M_ASN1_I2D_INF_seq_start(tag,ctx) \
564 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
565 *(p++)=0x80
566
567#define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
568
569#define M_ASN1_I2D_finish() *pp=p; \
570 return(r);
571
572int asn1_GetSequence(ASN1_const_CTX *c, long *length);
573void asn1_add_error(const unsigned char *address,int offset);
574#ifdef __cplusplus
575}
576#endif
577
578#endif
diff --git a/src/lib/libcrypto/asn1/asn1_par.c b/src/lib/libcrypto/asn1/asn1_par.c
deleted file mode 100644
index aaca69aebd..0000000000
--- a/src/lib/libcrypto/asn1/asn1_par.c
+++ /dev/null
@@ -1,437 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/objects.h>
63#include <openssl/asn1.h>
64
65static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
66 int indent);
67static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
68 int offset, int depth, int indent, int dump);
69static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
70 int indent)
71 {
72 static const char fmt[]="%-18s";
73 char str[128];
74 const char *p;
75
76 if (constructed & V_ASN1_CONSTRUCTED)
77 p="cons: ";
78 else
79 p="prim: ";
80 if (BIO_write(bp,p,6) < 6) goto err;
81 BIO_indent(bp,indent,128);
82
83 p=str;
84 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
85 BIO_snprintf(str,sizeof str,"priv [ %d ] ",tag);
86 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
87 BIO_snprintf(str,sizeof str,"cont [ %d ]",tag);
88 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
89 BIO_snprintf(str,sizeof str,"appl [ %d ]",tag);
90 else if (tag > 30)
91 BIO_snprintf(str,sizeof str,"<ASN1 %d>",tag);
92 else
93 p = ASN1_tag2str(tag);
94
95 if (BIO_printf(bp,fmt,p) <= 0)
96 goto err;
97 return(1);
98err:
99 return(0);
100 }
101
102int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
103 {
104 return(asn1_parse2(bp,&pp,len,0,0,indent,0));
105 }
106
107int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump)
108 {
109 return(asn1_parse2(bp,&pp,len,0,0,indent,dump));
110 }
111
112static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offset,
113 int depth, int indent, int dump)
114 {
115 const unsigned char *p,*ep,*tot,*op,*opp;
116 long len;
117 int tag,xclass,ret=0;
118 int nl,hl,j,r;
119 ASN1_OBJECT *o=NULL;
120 ASN1_OCTET_STRING *os=NULL;
121 /* ASN1_BMPSTRING *bmp=NULL;*/
122 int dump_indent;
123
124#if 0
125 dump_indent = indent;
126#else
127 dump_indent = 6; /* Because we know BIO_dump_indent() */
128#endif
129 p= *pp;
130 tot=p+length;
131 op=p-1;
132 while ((p < tot) && (op < p))
133 {
134 op=p;
135 j=ASN1_get_object(&p,&len,&tag,&xclass,length);
136#ifdef LINT
137 j=j;
138#endif
139 if (j & 0x80)
140 {
141 if (BIO_write(bp,"Error in encoding\n",18) <= 0)
142 goto end;
143 ret=0;
144 goto end;
145 }
146 hl=(p-op);
147 length-=hl;
148 /* if j == 0x21 it is a constructed indefinite length object */
149 if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))
150 <= 0) goto end;
151
152 if (j != (V_ASN1_CONSTRUCTED | 1))
153 {
154 if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",
155 depth,(long)hl,len) <= 0)
156 goto end;
157 }
158 else
159 {
160 if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ",
161 depth,(long)hl) <= 0)
162 goto end;
163 }
164 if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))
165 goto end;
166 if (j & V_ASN1_CONSTRUCTED)
167 {
168 ep=p+len;
169 if (BIO_write(bp,"\n",1) <= 0) goto end;
170 if (len > length)
171 {
172 BIO_printf(bp,
173 "length is greater than %ld\n",length);
174 ret=0;
175 goto end;
176 }
177 if ((j == 0x21) && (len == 0))
178 {
179 for (;;)
180 {
181 r=asn1_parse2(bp,&p,(long)(tot-p),
182 offset+(p - *pp),depth+1,
183 indent,dump);
184 if (r == 0) { ret=0; goto end; }
185 if ((r == 2) || (p >= tot)) break;
186 }
187 }
188 else
189 while (p < ep)
190 {
191 r=asn1_parse2(bp,&p,(long)len,
192 offset+(p - *pp),depth+1,
193 indent,dump);
194 if (r == 0) { ret=0; goto end; }
195 }
196 }
197 else if (xclass != 0)
198 {
199 p+=len;
200 if (BIO_write(bp,"\n",1) <= 0) goto end;
201 }
202 else
203 {
204 nl=0;
205 if ( (tag == V_ASN1_PRINTABLESTRING) ||
206 (tag == V_ASN1_T61STRING) ||
207 (tag == V_ASN1_IA5STRING) ||
208 (tag == V_ASN1_VISIBLESTRING) ||
209 (tag == V_ASN1_NUMERICSTRING) ||
210 (tag == V_ASN1_UTF8STRING) ||
211 (tag == V_ASN1_UTCTIME) ||
212 (tag == V_ASN1_GENERALIZEDTIME))
213 {
214 if (BIO_write(bp,":",1) <= 0) goto end;
215 if ((len > 0) &&
216 BIO_write(bp,(const char *)p,(int)len)
217 != (int)len)
218 goto end;
219 }
220 else if (tag == V_ASN1_OBJECT)
221 {
222 opp=op;
223 if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
224 {
225 if (BIO_write(bp,":",1) <= 0) goto end;
226 i2a_ASN1_OBJECT(bp,o);
227 }
228 else
229 {
230 if (BIO_write(bp,":BAD OBJECT",11) <= 0)
231 goto end;
232 }
233 }
234 else if (tag == V_ASN1_BOOLEAN)
235 {
236 int ii;
237
238 opp=op;
239 ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
240 if (ii < 0)
241 {
242 if (BIO_write(bp,"Bad boolean\n",12) <= 0)
243 goto end;
244 }
245 BIO_printf(bp,":%d",ii);
246 }
247 else if (tag == V_ASN1_BMPSTRING)
248 {
249 /* do the BMP thang */
250 }
251 else if (tag == V_ASN1_OCTET_STRING)
252 {
253 int i,printable=1;
254
255 opp=op;
256 os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
257 if (os != NULL && os->length > 0)
258 {
259 opp = os->data;
260 /* testing whether the octet string is
261 * printable */
262 for (i=0; i<os->length; i++)
263 {
264 if (( (opp[i] < ' ') &&
265 (opp[i] != '\n') &&
266 (opp[i] != '\r') &&
267 (opp[i] != '\t')) ||
268 (opp[i] > '~'))
269 {
270 printable=0;
271 break;
272 }
273 }
274 if (printable)
275 /* printable string */
276 {
277 if (BIO_write(bp,":",1) <= 0)
278 goto end;
279 if (BIO_write(bp,(const char *)opp,
280 os->length) <= 0)
281 goto end;
282 }
283 else if (!dump)
284 /* not printable => print octet string
285 * as hex dump */
286 {
287 if (BIO_write(bp,"[HEX DUMP]:",11) <= 0)
288 goto end;
289 for (i=0; i<os->length; i++)
290 {
291 if (BIO_printf(bp,"%02X"
292 , opp[i]) <= 0)
293 goto end;
294 }
295 }
296 else
297 /* print the normal dump */
298 {
299 if (!nl)
300 {
301 if (BIO_write(bp,"\n",1) <= 0)
302 goto end;
303 }
304 if (BIO_dump_indent(bp,
305 (const char *)opp,
306 ((dump == -1 || dump >
307 os->length)?os->length:dump),
308 dump_indent) <= 0)
309 goto end;
310 nl=1;
311 }
312 }
313 if (os != NULL)
314 {
315 M_ASN1_OCTET_STRING_free(os);
316 os=NULL;
317 }
318 }
319 else if (tag == V_ASN1_INTEGER)
320 {
321 ASN1_INTEGER *bs;
322 int i;
323
324 opp=op;
325 bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
326 if (bs != NULL)
327 {
328 if (BIO_write(bp,":",1) <= 0) goto end;
329 if (bs->type == V_ASN1_NEG_INTEGER)
330 if (BIO_write(bp,"-",1) <= 0)
331 goto end;
332 for (i=0; i<bs->length; i++)
333 {
334 if (BIO_printf(bp,"%02X",
335 bs->data[i]) <= 0)
336 goto end;
337 }
338 if (bs->length == 0)
339 {
340 if (BIO_write(bp,"00",2) <= 0)
341 goto end;
342 }
343 }
344 else
345 {
346 if (BIO_write(bp,"BAD INTEGER",11) <= 0)
347 goto end;
348 }
349 M_ASN1_INTEGER_free(bs);
350 }
351 else if (tag == V_ASN1_ENUMERATED)
352 {
353 ASN1_ENUMERATED *bs;
354 int i;
355
356 opp=op;
357 bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl);
358 if (bs != NULL)
359 {
360 if (BIO_write(bp,":",1) <= 0) goto end;
361 if (bs->type == V_ASN1_NEG_ENUMERATED)
362 if (BIO_write(bp,"-",1) <= 0)
363 goto end;
364 for (i=0; i<bs->length; i++)
365 {
366 if (BIO_printf(bp,"%02X",
367 bs->data[i]) <= 0)
368 goto end;
369 }
370 if (bs->length == 0)
371 {
372 if (BIO_write(bp,"00",2) <= 0)
373 goto end;
374 }
375 }
376 else
377 {
378 if (BIO_write(bp,"BAD ENUMERATED",11) <= 0)
379 goto end;
380 }
381 M_ASN1_ENUMERATED_free(bs);
382 }
383 else if (len > 0 && dump)
384 {
385 if (!nl)
386 {
387 if (BIO_write(bp,"\n",1) <= 0)
388 goto end;
389 }
390 if (BIO_dump_indent(bp,(const char *)p,
391 ((dump == -1 || dump > len)?len:dump),
392 dump_indent) <= 0)
393 goto end;
394 nl=1;
395 }
396
397 if (!nl)
398 {
399 if (BIO_write(bp,"\n",1) <= 0) goto end;
400 }
401 p+=len;
402 if ((tag == V_ASN1_EOC) && (xclass == 0))
403 {
404 ret=2; /* End of sequence */
405 goto end;
406 }
407 }
408 length-=len;
409 }
410 ret=1;
411end:
412 if (o != NULL) ASN1_OBJECT_free(o);
413 if (os != NULL) M_ASN1_OCTET_STRING_free(os);
414 *pp=p;
415 return(ret);
416 }
417
418const char *ASN1_tag2str(int tag)
419{
420 static const char * const tag2str[] = {
421 "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
422 "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
423 "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", /* 10-13 */
424 "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", /* 15-17 */
425 "NUMERICSTRING", "PRINTABLESTRING", "T61STRING", /* 18-20 */
426 "VIDEOTEXSTRING", "IA5STRING", "UTCTIME","GENERALIZEDTIME", /* 21-24 */
427 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", /* 25-27 */
428 "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING" /* 28-30 */
429 };
430
431 if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
432 tag &= ~0x100;
433
434 if(tag < 0 || tag > 30) return "(unknown)";
435 return tag2str[tag];
436}
437
diff --git a/src/lib/libcrypto/asn1/asn1t.h b/src/lib/libcrypto/asn1/asn1t.h
deleted file mode 100644
index d230e4bf70..0000000000
--- a/src/lib/libcrypto/asn1/asn1t.h
+++ /dev/null
@@ -1,960 +0,0 @@
1/* asn1t.h */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58#ifndef HEADER_ASN1T_H
59#define HEADER_ASN1T_H
60
61#include <stddef.h>
62#include <openssl/e_os2.h>
63#include <openssl/asn1.h>
64
65#ifdef OPENSSL_BUILD_SHLIBCRYPTO
66# undef OPENSSL_EXTERN
67# define OPENSSL_EXTERN OPENSSL_EXPORT
68#endif
69
70/* ASN1 template defines, structures and functions */
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
76
77#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
78
79/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
81
82
83/* Macros for start and end of ASN1_ITEM definition */
84
85#define ASN1_ITEM_start(itname) \
86 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87
88#define ASN1_ITEM_end(itname) \
89 };
90
91#else
92
93/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
94#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95
96
97/* Macros for start and end of ASN1_ITEM definition */
98
99#define ASN1_ITEM_start(itname) \
100 const ASN1_ITEM * itname##_it(void) \
101 { \
102 static const ASN1_ITEM local_it = {
103
104#define ASN1_ITEM_end(itname) \
105 }; \
106 return &local_it; \
107 }
108
109#endif
110
111
112/* Macros to aid ASN1 template writing */
113
114#define ASN1_ITEM_TEMPLATE(tname) \
115 static const ASN1_TEMPLATE tname##_item_tt
116
117#define ASN1_ITEM_TEMPLATE_END(tname) \
118 ;\
119 ASN1_ITEM_start(tname) \
120 ASN1_ITYPE_PRIMITIVE,\
121 -1,\
122 &tname##_item_tt,\
123 0,\
124 NULL,\
125 0,\
126 #tname \
127 ASN1_ITEM_end(tname)
128
129
130/* This is a ASN1 type which just embeds a template */
131
132/* This pair helps declare a SEQUENCE. We can do:
133 *
134 * ASN1_SEQUENCE(stname) = {
135 * ... SEQUENCE components ...
136 * } ASN1_SEQUENCE_END(stname)
137 *
138 * This will produce an ASN1_ITEM called stname_it
139 * for a structure called stname.
140 *
141 * If you want the same structure but a different
142 * name then use:
143 *
144 * ASN1_SEQUENCE(itname) = {
145 * ... SEQUENCE components ...
146 * } ASN1_SEQUENCE_END_name(stname, itname)
147 *
148 * This will create an item called itname_it using
149 * a structure called stname.
150 */
151
152#define ASN1_SEQUENCE(tname) \
153 static const ASN1_TEMPLATE tname##_seq_tt[]
154
155#define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
156
157#define ASN1_SEQUENCE_END_name(stname, tname) \
158 ;\
159 ASN1_ITEM_start(tname) \
160 ASN1_ITYPE_SEQUENCE,\
161 V_ASN1_SEQUENCE,\
162 tname##_seq_tt,\
163 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
164 NULL,\
165 sizeof(stname),\
166 #stname \
167 ASN1_ITEM_end(tname)
168
169#define ASN1_NDEF_SEQUENCE(tname) \
170 ASN1_SEQUENCE(tname)
171
172#define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
173 ASN1_SEQUENCE_cb(tname, cb)
174
175#define ASN1_SEQUENCE_cb(tname, cb) \
176 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
177 ASN1_SEQUENCE(tname)
178
179#define ASN1_BROKEN_SEQUENCE(tname) \
180 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
181 ASN1_SEQUENCE(tname)
182
183#define ASN1_SEQUENCE_ref(tname, cb, lck) \
184 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
185 ASN1_SEQUENCE(tname)
186
187#define ASN1_SEQUENCE_enc(tname, enc, cb) \
188 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
189 ASN1_SEQUENCE(tname)
190
191#define ASN1_NDEF_SEQUENCE_END(tname) \
192 ;\
193 ASN1_ITEM_start(tname) \
194 ASN1_ITYPE_NDEF_SEQUENCE,\
195 V_ASN1_SEQUENCE,\
196 tname##_seq_tt,\
197 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
198 NULL,\
199 sizeof(tname),\
200 #tname \
201 ASN1_ITEM_end(tname)
202
203#define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
204
205#define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
206
207#define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
208
209#define ASN1_SEQUENCE_END_ref(stname, tname) \
210 ;\
211 ASN1_ITEM_start(tname) \
212 ASN1_ITYPE_SEQUENCE,\
213 V_ASN1_SEQUENCE,\
214 tname##_seq_tt,\
215 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
216 &tname##_aux,\
217 sizeof(stname),\
218 #stname \
219 ASN1_ITEM_end(tname)
220
221#define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
222 ;\
223 ASN1_ITEM_start(tname) \
224 ASN1_ITYPE_NDEF_SEQUENCE,\
225 V_ASN1_SEQUENCE,\
226 tname##_seq_tt,\
227 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
228 &tname##_aux,\
229 sizeof(stname),\
230 #stname \
231 ASN1_ITEM_end(tname)
232
233
234/* This pair helps declare a CHOICE type. We can do:
235 *
236 * ASN1_CHOICE(chname) = {
237 * ... CHOICE options ...
238 * ASN1_CHOICE_END(chname)
239 *
240 * This will produce an ASN1_ITEM called chname_it
241 * for a structure called chname. The structure
242 * definition must look like this:
243 * typedef struct {
244 * int type;
245 * union {
246 * ASN1_SOMETHING *opt1;
247 * ASN1_SOMEOTHER *opt2;
248 * } value;
249 * } chname;
250 *
251 * the name of the selector must be 'type'.
252 * to use an alternative selector name use the
253 * ASN1_CHOICE_END_selector() version.
254 */
255
256#define ASN1_CHOICE(tname) \
257 static const ASN1_TEMPLATE tname##_ch_tt[]
258
259#define ASN1_CHOICE_cb(tname, cb) \
260 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
261 ASN1_CHOICE(tname)
262
263#define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
264
265#define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
266
267#define ASN1_CHOICE_END_selector(stname, tname, selname) \
268 ;\
269 ASN1_ITEM_start(tname) \
270 ASN1_ITYPE_CHOICE,\
271 offsetof(stname,selname) ,\
272 tname##_ch_tt,\
273 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
274 NULL,\
275 sizeof(stname),\
276 #stname \
277 ASN1_ITEM_end(tname)
278
279#define ASN1_CHOICE_END_cb(stname, tname, selname) \
280 ;\
281 ASN1_ITEM_start(tname) \
282 ASN1_ITYPE_CHOICE,\
283 offsetof(stname,selname) ,\
284 tname##_ch_tt,\
285 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
286 &tname##_aux,\
287 sizeof(stname),\
288 #stname \
289 ASN1_ITEM_end(tname)
290
291/* This helps with the template wrapper form of ASN1_ITEM */
292
293#define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
294 (flags), (tag), 0,\
295 #name, ASN1_ITEM_ref(type) }
296
297/* These help with SEQUENCE or CHOICE components */
298
299/* used to declare other types */
300
301#define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
302 (flags), (tag), offsetof(stname, field),\
303 #field, ASN1_ITEM_ref(type) }
304
305/* used when the structure is combined with the parent */
306
307#define ASN1_EX_COMBINE(flags, tag, type) { \
308 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
309
310/* implicit and explicit helper macros */
311
312#define ASN1_IMP_EX(stname, field, type, tag, ex) \
313 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
314
315#define ASN1_EXP_EX(stname, field, type, tag, ex) \
316 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
317
318/* Any defined by macros: the field used is in the table itself */
319
320#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
321#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
322#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
323#else
324#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
325#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
326#endif
327/* Plain simple type */
328#define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
329
330/* OPTIONAL simple type */
331#define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
332
333/* IMPLICIT tagged simple type */
334#define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
335
336/* IMPLICIT tagged OPTIONAL simple type */
337#define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
338
339/* Same as above but EXPLICIT */
340
341#define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
342#define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
343
344/* SEQUENCE OF type */
345#define ASN1_SEQUENCE_OF(stname, field, type) \
346 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
347
348/* OPTIONAL SEQUENCE OF */
349#define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
350 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
351
352/* Same as above but for SET OF */
353
354#define ASN1_SET_OF(stname, field, type) \
355 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
356
357#define ASN1_SET_OF_OPT(stname, field, type) \
358 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
359
360/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
361
362#define ASN1_IMP_SET_OF(stname, field, type, tag) \
363 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
364
365#define ASN1_EXP_SET_OF(stname, field, type, tag) \
366 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
367
368#define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
369 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
370
371#define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
372 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
373
374#define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
375 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
376
377#define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
378 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
379
380#define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
381 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
382
383#define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
384 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
385
386/* EXPLICIT using indefinite length constructed form */
387#define ASN1_NDEF_EXP(stname, field, type, tag) \
388 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
389
390/* EXPLICIT OPTIONAL using indefinite length constructed form */
391#define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
392 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
393
394/* Macros for the ASN1_ADB structure */
395
396#define ASN1_ADB(name) \
397 static const ASN1_ADB_TABLE name##_adbtbl[]
398
399#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
400
401#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
402 ;\
403 static const ASN1_ADB name##_adb = {\
404 flags,\
405 offsetof(name, field),\
406 app_table,\
407 name##_adbtbl,\
408 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
409 def,\
410 none\
411 }
412
413#else
414
415#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
416 ;\
417 static const ASN1_ITEM *name##_adb(void) \
418 { \
419 static const ASN1_ADB internal_adb = \
420 {\
421 flags,\
422 offsetof(name, field),\
423 app_table,\
424 name##_adbtbl,\
425 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
426 def,\
427 none\
428 }; \
429 return (const ASN1_ITEM *) &internal_adb; \
430 } \
431 void dummy_function(void)
432
433#endif
434
435#define ADB_ENTRY(val, template) {val, template}
436
437#define ASN1_ADB_TEMPLATE(name) \
438 static const ASN1_TEMPLATE name##_tt
439
440/* This is the ASN1 template structure that defines
441 * a wrapper round the actual type. It determines the
442 * actual position of the field in the value structure,
443 * various flags such as OPTIONAL and the field name.
444 */
445
446struct ASN1_TEMPLATE_st {
447unsigned long flags; /* Various flags */
448long tag; /* tag, not used if no tagging */
449unsigned long offset; /* Offset of this field in structure */
450#ifndef NO_ASN1_FIELD_NAMES
451const char *field_name; /* Field name */
452#endif
453ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
454};
455
456/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
457
458#define ASN1_TEMPLATE_item(t) (t->item_ptr)
459#define ASN1_TEMPLATE_adb(t) (t->item_ptr)
460
461typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
462typedef struct ASN1_ADB_st ASN1_ADB;
463
464struct ASN1_ADB_st {
465 unsigned long flags; /* Various flags */
466 unsigned long offset; /* Offset of selector field */
467 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
468 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
469 long tblcount; /* Number of entries in tbl */
470 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
471 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
472};
473
474struct ASN1_ADB_TABLE_st {
475 long value; /* NID for an object or value for an int */
476 const ASN1_TEMPLATE tt; /* item for this value */
477};
478
479/* template flags */
480
481/* Field is optional */
482#define ASN1_TFLG_OPTIONAL (0x1)
483
484/* Field is a SET OF */
485#define ASN1_TFLG_SET_OF (0x1 << 1)
486
487/* Field is a SEQUENCE OF */
488#define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
489
490/* Special case: this refers to a SET OF that
491 * will be sorted into DER order when encoded *and*
492 * the corresponding STACK will be modified to match
493 * the new order.
494 */
495#define ASN1_TFLG_SET_ORDER (0x3 << 1)
496
497/* Mask for SET OF or SEQUENCE OF */
498#define ASN1_TFLG_SK_MASK (0x3 << 1)
499
500/* These flags mean the tag should be taken from the
501 * tag field. If EXPLICIT then the underlying type
502 * is used for the inner tag.
503 */
504
505/* IMPLICIT tagging */
506#define ASN1_TFLG_IMPTAG (0x1 << 3)
507
508
509/* EXPLICIT tagging, inner tag from underlying type */
510#define ASN1_TFLG_EXPTAG (0x2 << 3)
511
512#define ASN1_TFLG_TAG_MASK (0x3 << 3)
513
514/* context specific IMPLICIT */
515#define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
516
517/* context specific EXPLICIT */
518#define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
519
520/* If tagging is in force these determine the
521 * type of tag to use. Otherwise the tag is
522 * determined by the underlying type. These
523 * values reflect the actual octet format.
524 */
525
526/* Universal tag */
527#define ASN1_TFLG_UNIVERSAL (0x0<<6)
528/* Application tag */
529#define ASN1_TFLG_APPLICATION (0x1<<6)
530/* Context specific tag */
531#define ASN1_TFLG_CONTEXT (0x2<<6)
532/* Private tag */
533#define ASN1_TFLG_PRIVATE (0x3<<6)
534
535#define ASN1_TFLG_TAG_CLASS (0x3<<6)
536
537/* These are for ANY DEFINED BY type. In this case
538 * the 'item' field points to an ASN1_ADB structure
539 * which contains a table of values to decode the
540 * relevant type
541 */
542
543#define ASN1_TFLG_ADB_MASK (0x3<<8)
544
545#define ASN1_TFLG_ADB_OID (0x1<<8)
546
547#define ASN1_TFLG_ADB_INT (0x1<<9)
548
549/* This flag means a parent structure is passed
550 * instead of the field: this is useful is a
551 * SEQUENCE is being combined with a CHOICE for
552 * example. Since this means the structure and
553 * item name will differ we need to use the
554 * ASN1_CHOICE_END_name() macro for example.
555 */
556
557#define ASN1_TFLG_COMBINE (0x1<<10)
558
559/* This flag when present in a SEQUENCE OF, SET OF
560 * or EXPLICIT causes indefinite length constructed
561 * encoding to be used if required.
562 */
563
564#define ASN1_TFLG_NDEF (0x1<<11)
565
566/* This is the actual ASN1 item itself */
567
568struct ASN1_ITEM_st {
569char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
570long utype; /* underlying type */
571const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
572long tcount; /* Number of templates if SEQUENCE or CHOICE */
573const void *funcs; /* functions that handle this type */
574long size; /* Structure size (usually)*/
575#ifndef NO_ASN1_FIELD_NAMES
576const char *sname; /* Structure name */
577#endif
578};
579
580/* These are values for the itype field and
581 * determine how the type is interpreted.
582 *
583 * For PRIMITIVE types the underlying type
584 * determines the behaviour if items is NULL.
585 *
586 * Otherwise templates must contain a single
587 * template and the type is treated in the
588 * same way as the type specified in the template.
589 *
590 * For SEQUENCE types the templates field points
591 * to the members, the size field is the
592 * structure size.
593 *
594 * For CHOICE types the templates field points
595 * to each possible member (typically a union)
596 * and the 'size' field is the offset of the
597 * selector.
598 *
599 * The 'funcs' field is used for application
600 * specific functions.
601 *
602 * For COMPAT types the funcs field gives a
603 * set of functions that handle this type, this
604 * supports the old d2i, i2d convention.
605 *
606 * The EXTERN type uses a new style d2i/i2d.
607 * The new style should be used where possible
608 * because it avoids things like the d2i IMPLICIT
609 * hack.
610 *
611 * MSTRING is a multiple string type, it is used
612 * for a CHOICE of character strings where the
613 * actual strings all occupy an ASN1_STRING
614 * structure. In this case the 'utype' field
615 * has a special meaning, it is used as a mask
616 * of acceptable types using the B_ASN1 constants.
617 *
618 * NDEF_SEQUENCE is the same as SEQUENCE except
619 * that it will use indefinite length constructed
620 * encoding if requested.
621 *
622 */
623
624#define ASN1_ITYPE_PRIMITIVE 0x0
625
626#define ASN1_ITYPE_SEQUENCE 0x1
627
628#define ASN1_ITYPE_CHOICE 0x2
629
630#define ASN1_ITYPE_COMPAT 0x3
631
632#define ASN1_ITYPE_EXTERN 0x4
633
634#define ASN1_ITYPE_MSTRING 0x5
635
636#define ASN1_ITYPE_NDEF_SEQUENCE 0x6
637
638/* Cache for ASN1 tag and length, so we
639 * don't keep re-reading it for things
640 * like CHOICE
641 */
642
643struct ASN1_TLC_st{
644 char valid; /* Values below are valid */
645 int ret; /* return value */
646 long plen; /* length */
647 int ptag; /* class value */
648 int pclass; /* class value */
649 int hdrlen; /* header length */
650};
651
652/* Typedefs for ASN1 function pointers */
653
654typedef ASN1_VALUE * ASN1_new_func(void);
655typedef void ASN1_free_func(ASN1_VALUE *a);
656typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
657typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
658
659typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
660 int tag, int aclass, char opt, ASN1_TLC *ctx);
661
662typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
663typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
664typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
665
666typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
667 int indent, const char *fname,
668 const ASN1_PCTX *pctx);
669
670typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
671typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
672typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
673
674typedef struct ASN1_COMPAT_FUNCS_st {
675 ASN1_new_func *asn1_new;
676 ASN1_free_func *asn1_free;
677 ASN1_d2i_func *asn1_d2i;
678 ASN1_i2d_func *asn1_i2d;
679} ASN1_COMPAT_FUNCS;
680
681typedef struct ASN1_EXTERN_FUNCS_st {
682 void *app_data;
683 ASN1_ex_new_func *asn1_ex_new;
684 ASN1_ex_free_func *asn1_ex_free;
685 ASN1_ex_free_func *asn1_ex_clear;
686 ASN1_ex_d2i *asn1_ex_d2i;
687 ASN1_ex_i2d *asn1_ex_i2d;
688 ASN1_ex_print_func *asn1_ex_print;
689} ASN1_EXTERN_FUNCS;
690
691typedef struct ASN1_PRIMITIVE_FUNCS_st {
692 void *app_data;
693 unsigned long flags;
694 ASN1_ex_new_func *prim_new;
695 ASN1_ex_free_func *prim_free;
696 ASN1_ex_free_func *prim_clear;
697 ASN1_primitive_c2i *prim_c2i;
698 ASN1_primitive_i2c *prim_i2c;
699 ASN1_primitive_print *prim_print;
700} ASN1_PRIMITIVE_FUNCS;
701
702/* This is the ASN1_AUX structure: it handles various
703 * miscellaneous requirements. For example the use of
704 * reference counts and an informational callback.
705 *
706 * The "informational callback" is called at various
707 * points during the ASN1 encoding and decoding. It can
708 * be used to provide minor customisation of the structures
709 * used. This is most useful where the supplied routines
710 * *almost* do the right thing but need some extra help
711 * at a few points. If the callback returns zero then
712 * it is assumed a fatal error has occurred and the
713 * main operation should be abandoned.
714 *
715 * If major changes in the default behaviour are required
716 * then an external type is more appropriate.
717 */
718
719typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
720 void *exarg);
721
722typedef struct ASN1_AUX_st {
723 void *app_data;
724 int flags;
725 int ref_offset; /* Offset of reference value */
726 int ref_lock; /* Lock type to use */
727 ASN1_aux_cb *asn1_cb;
728 int enc_offset; /* Offset of ASN1_ENCODING structure */
729} ASN1_AUX;
730
731/* For print related callbacks exarg points to this structure */
732typedef struct ASN1_PRINT_ARG_st {
733 BIO *out;
734 int indent;
735 const ASN1_PCTX *pctx;
736} ASN1_PRINT_ARG;
737
738/* For streaming related callbacks exarg points to this structure */
739typedef struct ASN1_STREAM_ARG_st {
740 /* BIO to stream through */
741 BIO *out;
742 /* BIO with filters appended */
743 BIO *ndef_bio;
744 /* Streaming I/O boundary */
745 unsigned char **boundary;
746} ASN1_STREAM_ARG;
747
748/* Flags in ASN1_AUX */
749
750/* Use a reference count */
751#define ASN1_AFLG_REFCOUNT 1
752/* Save the encoding of structure (useful for signatures) */
753#define ASN1_AFLG_ENCODING 2
754/* The Sequence length is invalid */
755#define ASN1_AFLG_BROKEN 4
756
757/* operation values for asn1_cb */
758
759#define ASN1_OP_NEW_PRE 0
760#define ASN1_OP_NEW_POST 1
761#define ASN1_OP_FREE_PRE 2
762#define ASN1_OP_FREE_POST 3
763#define ASN1_OP_D2I_PRE 4
764#define ASN1_OP_D2I_POST 5
765#define ASN1_OP_I2D_PRE 6
766#define ASN1_OP_I2D_POST 7
767#define ASN1_OP_PRINT_PRE 8
768#define ASN1_OP_PRINT_POST 9
769#define ASN1_OP_STREAM_PRE 10
770#define ASN1_OP_STREAM_POST 11
771#define ASN1_OP_DETACHED_PRE 12
772#define ASN1_OP_DETACHED_POST 13
773
774/* Macro to implement a primitive type */
775#define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
776#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
777 ASN1_ITEM_start(itname) \
778 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
779 ASN1_ITEM_end(itname)
780
781/* Macro to implement a multi string type */
782#define IMPLEMENT_ASN1_MSTRING(itname, mask) \
783 ASN1_ITEM_start(itname) \
784 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
785 ASN1_ITEM_end(itname)
786
787/* Macro to implement an ASN1_ITEM in terms of old style funcs */
788
789#define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
790
791#define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
792 static const ASN1_COMPAT_FUNCS sname##_ff = { \
793 (ASN1_new_func *)sname##_new, \
794 (ASN1_free_func *)sname##_free, \
795 (ASN1_d2i_func *)d2i_##sname, \
796 (ASN1_i2d_func *)i2d_##sname, \
797 }; \
798 ASN1_ITEM_start(sname) \
799 ASN1_ITYPE_COMPAT, \
800 tag, \
801 NULL, \
802 0, \
803 &sname##_ff, \
804 0, \
805 #sname \
806 ASN1_ITEM_end(sname)
807
808#define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
809 ASN1_ITEM_start(sname) \
810 ASN1_ITYPE_EXTERN, \
811 tag, \
812 NULL, \
813 0, \
814 &fptrs, \
815 0, \
816 #sname \
817 ASN1_ITEM_end(sname)
818
819/* Macro to implement standard functions in terms of ASN1_ITEM structures */
820
821#define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
822
823#define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
824
825#define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
826 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
827
828#define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
829 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
830
831#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
832 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
833
834#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
835 pre stname *fname##_new(void) \
836 { \
837 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
838 } \
839 pre void fname##_free(stname *a) \
840 { \
841 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
842 }
843
844#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
845 stname *fname##_new(void) \
846 { \
847 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
848 } \
849 void fname##_free(stname *a) \
850 { \
851 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
852 }
853
854#define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
855 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
856 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
857
858#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
859 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
860 { \
861 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
862 } \
863 int i2d_##fname(stname *a, unsigned char **out) \
864 { \
865 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
866 }
867
868#define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
869 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
870 { \
871 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
872 }
873
874/* This includes evil casts to remove const: they will go away when full
875 * ASN1 constification is done.
876 */
877#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
878 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
879 { \
880 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
881 } \
882 int i2d_##fname(const stname *a, unsigned char **out) \
883 { \
884 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
885 }
886
887#define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
888 stname * stname##_dup(stname *x) \
889 { \
890 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
891 }
892
893#define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
894 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
895
896#define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
897 int fname##_print_ctx(BIO *out, stname *x, int indent, \
898 const ASN1_PCTX *pctx) \
899 { \
900 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
901 ASN1_ITEM_rptr(itname), pctx); \
902 }
903
904#define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
905 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
906
907#define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
908 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
909 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
910
911/* external definitions for primitive types */
912
913DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
914DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
915DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
916DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
917DECLARE_ASN1_ITEM(CBIGNUM)
918DECLARE_ASN1_ITEM(BIGNUM)
919DECLARE_ASN1_ITEM(LONG)
920DECLARE_ASN1_ITEM(ZLONG)
921
922DECLARE_STACK_OF(ASN1_VALUE)
923
924/* Functions used internally by the ASN1 code */
925
926int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
927void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
928int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
929int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
930
931void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
932int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
933int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
934 int tag, int aclass, char opt, ASN1_TLC *ctx);
935
936int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
937int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
938void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
939
940int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
941int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
942
943int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
944int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
945
946ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
947
948const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
949
950int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
951
952void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
953void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
954int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
955int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
956
957#ifdef __cplusplus
958}
959#endif
960#endif
diff --git a/src/lib/libcrypto/asn1/asn_mime.c b/src/lib/libcrypto/asn1/asn_mime.c
deleted file mode 100644
index c1d1b12291..0000000000
--- a/src/lib/libcrypto/asn1/asn_mime.c
+++ /dev/null
@@ -1,942 +0,0 @@
1/* asn_mime.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 */
54
55#include <stdio.h>
56#include <ctype.h>
57#include "cryptlib.h"
58#include <openssl/rand.h>
59#include <openssl/x509.h>
60#include <openssl/asn1.h>
61#include <openssl/asn1t.h>
62#include "asn1_locl.h"
63
64/* Generalised MIME like utilities for streaming ASN1. Although many
65 * have a PKCS7/CMS like flavour others are more general purpose.
66 */
67
68/* MIME format structures
69 * Note that all are translated to lower case apart from
70 * parameter values. Quotes are stripped off
71 */
72
73typedef struct {
74char *param_name; /* Param name e.g. "micalg" */
75char *param_value; /* Param value e.g. "sha1" */
76} MIME_PARAM;
77
78DECLARE_STACK_OF(MIME_PARAM)
79IMPLEMENT_STACK_OF(MIME_PARAM)
80
81typedef struct {
82char *name; /* Name of line e.g. "content-type" */
83char *value; /* Value of line e.g. "text/plain" */
84STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
85} MIME_HEADER;
86
87DECLARE_STACK_OF(MIME_HEADER)
88IMPLEMENT_STACK_OF(MIME_HEADER)
89
90static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
91 const ASN1_ITEM *it);
92static char * strip_ends(char *name);
93static char * strip_start(char *name);
94static char * strip_end(char *name);
95static MIME_HEADER *mime_hdr_new(char *name, char *value);
96static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
97static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
98static int mime_hdr_cmp(const MIME_HEADER * const *a,
99 const MIME_HEADER * const *b);
100static int mime_param_cmp(const MIME_PARAM * const *a,
101 const MIME_PARAM * const *b);
102static void mime_param_free(MIME_PARAM *param);
103static int mime_bound_check(char *line, int linelen, char *bound, int blen);
104static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
105static int strip_eol(char *linebuf, int *plen);
106static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
107static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
108static void mime_hdr_free(MIME_HEADER *hdr);
109
110#define MAX_SMLEN 1024
111#define mime_debug(x) /* x */
112
113/* Output an ASN1 structure in BER format streaming if necessary */
114
115int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
116 const ASN1_ITEM *it)
117 {
118 /* If streaming create stream BIO and copy all content through it */
119 if (flags & SMIME_STREAM)
120 {
121 BIO *bio, *tbio;
122 bio = BIO_new_NDEF(out, val, it);
123 if (!bio)
124 {
125 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,ERR_R_MALLOC_FAILURE);
126 return 0;
127 }
128 SMIME_crlf_copy(in, bio, flags);
129 (void)BIO_flush(bio);
130 /* Free up successive BIOs until we hit the old output BIO */
131 do
132 {
133 tbio = BIO_pop(bio);
134 BIO_free(bio);
135 bio = tbio;
136 } while (bio != out);
137 }
138 /* else just write out ASN1 structure which will have all content
139 * stored internally
140 */
141 else
142 ASN1_item_i2d_bio(it, out, val);
143 return 1;
144 }
145
146/* Base 64 read and write of ASN1 structure */
147
148static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
149 const ASN1_ITEM *it)
150 {
151 BIO *b64;
152 int r;
153 b64 = BIO_new(BIO_f_base64());
154 if(!b64)
155 {
156 ASN1err(ASN1_F_B64_WRITE_ASN1,ERR_R_MALLOC_FAILURE);
157 return 0;
158 }
159 /* prepend the b64 BIO so all data is base64 encoded.
160 */
161 out = BIO_push(b64, out);
162 r = i2d_ASN1_bio_stream(out, val, in, flags, it);
163 (void)BIO_flush(out);
164 BIO_pop(out);
165 BIO_free(b64);
166 return r;
167 }
168
169/* Streaming ASN1 PEM write */
170
171int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
172 const char *hdr,
173 const ASN1_ITEM *it)
174 {
175 int r;
176 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
177 r = B64_write_ASN1(out, val, in, flags, it);
178 BIO_printf(out, "-----END %s-----\n", hdr);
179 return r;
180 }
181
182static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
183{
184 BIO *b64;
185 ASN1_VALUE *val;
186 if(!(b64 = BIO_new(BIO_f_base64()))) {
187 ASN1err(ASN1_F_B64_READ_ASN1,ERR_R_MALLOC_FAILURE);
188 return 0;
189 }
190 bio = BIO_push(b64, bio);
191 val = ASN1_item_d2i_bio(it, bio, NULL);
192 if(!val)
193 ASN1err(ASN1_F_B64_READ_ASN1,ASN1_R_DECODE_ERROR);
194 (void)BIO_flush(bio);
195 bio = BIO_pop(bio);
196 BIO_free(b64);
197 return val;
198}
199
200/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
201
202static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
203 {
204 const EVP_MD *md;
205 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
206 have_unknown = 0;
207 write_comma = 0;
208 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++)
209 {
210 if (write_comma)
211 BIO_write(out, ",", 1);
212 write_comma = 1;
213 md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
214 md = EVP_get_digestbynid(md_nid);
215 if (md && md->md_ctrl)
216 {
217 int rv;
218 char *micstr;
219 rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
220 if (rv > 0)
221 {
222 BIO_puts(out, micstr);
223 OPENSSL_free(micstr);
224 continue;
225 }
226 if (rv != -2)
227 goto err;
228 }
229 switch(md_nid)
230 {
231 case NID_sha1:
232 BIO_puts(out, "sha1");
233 break;
234
235 case NID_md5:
236 BIO_puts(out, "md5");
237 break;
238
239 case NID_sha256:
240 BIO_puts(out, "sha-256");
241 break;
242
243 case NID_sha384:
244 BIO_puts(out, "sha-384");
245 break;
246
247 case NID_sha512:
248 BIO_puts(out, "sha-512");
249 break;
250
251 case NID_id_GostR3411_94:
252 BIO_puts(out, "gostr3411-94");
253 goto err;
254 break;
255
256 default:
257 if (have_unknown)
258 write_comma = 0;
259 else
260 {
261 BIO_puts(out, "unknown");
262 have_unknown = 1;
263 }
264 break;
265
266 }
267 }
268
269 ret = 1;
270 err:
271
272 return ret;
273
274 }
275
276/* SMIME sender */
277
278int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
279 int ctype_nid, int econt_nid,
280 STACK_OF(X509_ALGOR) *mdalgs,
281 const ASN1_ITEM *it)
282{
283 char bound[33], c;
284 int i;
285 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
286 const char *msg_type=NULL;
287 if (flags & SMIME_OLDMIME)
288 mime_prefix = "application/x-pkcs7-";
289 else
290 mime_prefix = "application/pkcs7-";
291
292 if (flags & SMIME_CRLFEOL)
293 mime_eol = "\r\n";
294 else
295 mime_eol = "\n";
296 if((flags & SMIME_DETACHED) && data) {
297 /* We want multipart/signed */
298 /* Generate a random boundary */
299 RAND_pseudo_bytes((unsigned char *)bound, 32);
300 for(i = 0; i < 32; i++) {
301 c = bound[i] & 0xf;
302 if(c < 10) c += '0';
303 else c += 'A' - 10;
304 bound[i] = c;
305 }
306 bound[32] = 0;
307 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
308 BIO_printf(bio, "Content-Type: multipart/signed;");
309 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
310 BIO_puts(bio, " micalg=\"");
311 asn1_write_micalg(bio, mdalgs);
312 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
313 bound, mime_eol, mime_eol);
314 BIO_printf(bio, "This is an S/MIME signed message%s%s",
315 mime_eol, mime_eol);
316 /* Now write out the first part */
317 BIO_printf(bio, "------%s%s", bound, mime_eol);
318 if (!asn1_output_data(bio, data, val, flags, it))
319 return 0;
320 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
321
322 /* Headers for signature */
323
324 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
325 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
326 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
327 mime_eol);
328 BIO_printf(bio, "Content-Disposition: attachment;");
329 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
330 mime_eol, mime_eol);
331 B64_write_ASN1(bio, val, NULL, 0, it);
332 BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
333 mime_eol, mime_eol);
334 return 1;
335 }
336
337 /* Determine smime-type header */
338
339 if (ctype_nid == NID_pkcs7_enveloped)
340 msg_type = "enveloped-data";
341 else if (ctype_nid == NID_pkcs7_signed)
342 {
343 if (econt_nid == NID_id_smime_ct_receipt)
344 msg_type = "signed-receipt";
345 else if (sk_X509_ALGOR_num(mdalgs) >= 0)
346 msg_type = "signed-data";
347 else
348 msg_type = "certs-only";
349 }
350 else if (ctype_nid == NID_id_smime_ct_compressedData)
351 {
352 msg_type = "compressed-data";
353 cname = "smime.p7z";
354 }
355 /* MIME headers */
356 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
357 BIO_printf(bio, "Content-Disposition: attachment;");
358 BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
359 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
360 if (msg_type)
361 BIO_printf(bio, " smime-type=%s;", msg_type);
362 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
363 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
364 mime_eol, mime_eol);
365 if (!B64_write_ASN1(bio, val, data, flags, it))
366 return 0;
367 BIO_printf(bio, "%s", mime_eol);
368 return 1;
369}
370
371/* Handle output of ASN1 data */
372
373
374static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
375 const ASN1_ITEM *it)
376 {
377 BIO *tmpbio;
378 const ASN1_AUX *aux = it->funcs;
379 ASN1_STREAM_ARG sarg;
380
381 if (!(flags & SMIME_DETACHED))
382 {
383 SMIME_crlf_copy(data, out, flags);
384 return 1;
385 }
386
387 if (!aux || !aux->asn1_cb)
388 {
389 ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
390 ASN1_R_STREAMING_NOT_SUPPORTED);
391 return 0;
392 }
393
394 sarg.out = out;
395 sarg.ndef_bio = NULL;
396 sarg.boundary = NULL;
397
398 /* Let ASN1 code prepend any needed BIOs */
399
400 if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
401 return 0;
402
403 /* Copy data across, passing through filter BIOs for processing */
404 SMIME_crlf_copy(data, sarg.ndef_bio, flags);
405
406 /* Finalize structure */
407 if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
408 return 0;
409
410 /* Now remove any digests prepended to the BIO */
411
412 while (sarg.ndef_bio != out)
413 {
414 tmpbio = BIO_pop(sarg.ndef_bio);
415 BIO_free(sarg.ndef_bio);
416 sarg.ndef_bio = tmpbio;
417 }
418
419 return 1;
420
421 }
422
423/* SMIME reader: handle multipart/signed and opaque signing.
424 * in multipart case the content is placed in a memory BIO
425 * pointed to by "bcont". In opaque this is set to NULL
426 */
427
428ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
429{
430 BIO *asnin;
431 STACK_OF(MIME_HEADER) *headers = NULL;
432 STACK_OF(BIO) *parts = NULL;
433 MIME_HEADER *hdr;
434 MIME_PARAM *prm;
435 ASN1_VALUE *val;
436 int ret;
437
438 if(bcont) *bcont = NULL;
439
440 if (!(headers = mime_parse_hdr(bio))) {
441 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_PARSE_ERROR);
442 return NULL;
443 }
444
445 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
446 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
447 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
448 return NULL;
449 }
450
451 /* Handle multipart/signed */
452
453 if(!strcmp(hdr->value, "multipart/signed")) {
454 /* Split into two parts */
455 prm = mime_param_find(hdr, "boundary");
456 if(!prm || !prm->param_value) {
457 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
458 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
459 return NULL;
460 }
461 ret = multi_split(bio, prm->param_value, &parts);
462 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
463 if(!ret || (sk_BIO_num(parts) != 2) ) {
464 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
465 sk_BIO_pop_free(parts, BIO_vfree);
466 return NULL;
467 }
468
469 /* Parse the signature piece */
470 asnin = sk_BIO_value(parts, 1);
471
472 if (!(headers = mime_parse_hdr(asnin))) {
473 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_SIG_PARSE_ERROR);
474 sk_BIO_pop_free(parts, BIO_vfree);
475 return NULL;
476 }
477
478 /* Get content type */
479
480 if(!(hdr = mime_hdr_find(headers, "content-type")) ||
481 !hdr->value) {
482 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
483 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
484 return NULL;
485 }
486
487 if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
488 strcmp(hdr->value, "application/pkcs7-signature")) {
489 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
490 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_SIG_INVALID_MIME_TYPE);
491 ERR_add_error_data(2, "type: ", hdr->value);
492 sk_BIO_pop_free(parts, BIO_vfree);
493 return NULL;
494 }
495 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
496 /* Read in ASN1 */
497 if(!(val = b64_read_asn1(asnin, it))) {
498 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_ASN1_SIG_PARSE_ERROR);
499 sk_BIO_pop_free(parts, BIO_vfree);
500 return NULL;
501 }
502
503 if(bcont) {
504 *bcont = sk_BIO_value(parts, 0);
505 BIO_free(asnin);
506 sk_BIO_free(parts);
507 } else sk_BIO_pop_free(parts, BIO_vfree);
508 return val;
509 }
510
511 /* OK, if not multipart/signed try opaque signature */
512
513 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
514 strcmp (hdr->value, "application/pkcs7-mime")) {
515 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_INVALID_MIME_TYPE);
516 ERR_add_error_data(2, "type: ", hdr->value);
517 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
518 return NULL;
519 }
520
521 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
522
523 if(!(val = b64_read_asn1(bio, it))) {
524 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
525 return NULL;
526 }
527 return val;
528
529}
530
531/* Copy text from one BIO to another making the output CRLF at EOL */
532int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
533{
534 BIO *bf;
535 char eol;
536 int len;
537 char linebuf[MAX_SMLEN];
538 /* Buffer output so we don't write one line at a time. This is
539 * useful when streaming as we don't end up with one OCTET STRING
540 * per line.
541 */
542 bf = BIO_new(BIO_f_buffer());
543 if (!bf)
544 return 0;
545 out = BIO_push(bf, out);
546 if(flags & SMIME_BINARY)
547 {
548 while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
549 BIO_write(out, linebuf, len);
550 }
551 else
552 {
553 if(flags & SMIME_TEXT)
554 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
555 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0)
556 {
557 eol = strip_eol(linebuf, &len);
558 if (len)
559 BIO_write(out, linebuf, len);
560 if(eol) BIO_write(out, "\r\n", 2);
561 }
562 }
563 (void)BIO_flush(out);
564 BIO_pop(out);
565 BIO_free(bf);
566 return 1;
567}
568
569/* Strip off headers if they are text/plain */
570int SMIME_text(BIO *in, BIO *out)
571{
572 char iobuf[4096];
573 int len;
574 STACK_OF(MIME_HEADER) *headers;
575 MIME_HEADER *hdr;
576
577 if (!(headers = mime_parse_hdr(in))) {
578 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_PARSE_ERROR);
579 return 0;
580 }
581 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
582 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_NO_CONTENT_TYPE);
583 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
584 return 0;
585 }
586 if (strcmp (hdr->value, "text/plain")) {
587 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_INVALID_MIME_TYPE);
588 ERR_add_error_data(2, "type: ", hdr->value);
589 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
590 return 0;
591 }
592 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
593 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
594 BIO_write(out, iobuf, len);
595 if (len < 0)
596 return 0;
597 return 1;
598}
599
600/* Split a multipart/XXX message body into component parts: result is
601 * canonical parts in a STACK of bios
602 */
603
604static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
605{
606 char linebuf[MAX_SMLEN];
607 int len, blen;
608 int eol = 0, next_eol = 0;
609 BIO *bpart = NULL;
610 STACK_OF(BIO) *parts;
611 char state, part, first;
612
613 blen = strlen(bound);
614 part = 0;
615 state = 0;
616 first = 1;
617 parts = sk_BIO_new_null();
618 *ret = parts;
619 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
620 state = mime_bound_check(linebuf, len, bound, blen);
621 if(state == 1) {
622 first = 1;
623 part++;
624 } else if(state == 2) {
625 sk_BIO_push(parts, bpart);
626 return 1;
627 } else if(part) {
628 /* Strip CR+LF from linebuf */
629 next_eol = strip_eol(linebuf, &len);
630 if(first) {
631 first = 0;
632 if(bpart) sk_BIO_push(parts, bpart);
633 bpart = BIO_new(BIO_s_mem());
634 BIO_set_mem_eof_return(bpart, 0);
635 } else if (eol)
636 BIO_write(bpart, "\r\n", 2);
637 eol = next_eol;
638 if (len)
639 BIO_write(bpart, linebuf, len);
640 }
641 }
642 return 0;
643}
644
645/* This is the big one: parse MIME header lines up to message body */
646
647#define MIME_INVALID 0
648#define MIME_START 1
649#define MIME_TYPE 2
650#define MIME_NAME 3
651#define MIME_VALUE 4
652#define MIME_QUOTE 5
653#define MIME_COMMENT 6
654
655
656static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
657{
658 char *p, *q, c;
659 char *ntmp;
660 char linebuf[MAX_SMLEN];
661 MIME_HEADER *mhdr = NULL;
662 STACK_OF(MIME_HEADER) *headers;
663 int len, state, save_state = 0;
664
665 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
666 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
667 /* If whitespace at line start then continuation line */
668 if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
669 else state = MIME_START;
670 ntmp = NULL;
671 /* Go through all characters */
672 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
673
674 /* State machine to handle MIME headers
675 * if this looks horrible that's because it *is*
676 */
677
678 switch(state) {
679 case MIME_START:
680 if(c == ':') {
681 state = MIME_TYPE;
682 *p = 0;
683 ntmp = strip_ends(q);
684 q = p + 1;
685 }
686 break;
687
688 case MIME_TYPE:
689 if(c == ';') {
690 mime_debug("Found End Value\n");
691 *p = 0;
692 mhdr = mime_hdr_new(ntmp, strip_ends(q));
693 sk_MIME_HEADER_push(headers, mhdr);
694 ntmp = NULL;
695 q = p + 1;
696 state = MIME_NAME;
697 } else if(c == '(') {
698 save_state = state;
699 state = MIME_COMMENT;
700 }
701 break;
702
703 case MIME_COMMENT:
704 if(c == ')') {
705 state = save_state;
706 }
707 break;
708
709 case MIME_NAME:
710 if(c == '=') {
711 state = MIME_VALUE;
712 *p = 0;
713 ntmp = strip_ends(q);
714 q = p + 1;
715 }
716 break ;
717
718 case MIME_VALUE:
719 if(c == ';') {
720 state = MIME_NAME;
721 *p = 0;
722 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
723 ntmp = NULL;
724 q = p + 1;
725 } else if (c == '"') {
726 mime_debug("Found Quote\n");
727 state = MIME_QUOTE;
728 } else if(c == '(') {
729 save_state = state;
730 state = MIME_COMMENT;
731 }
732 break;
733
734 case MIME_QUOTE:
735 if(c == '"') {
736 mime_debug("Found Match Quote\n");
737 state = MIME_VALUE;
738 }
739 break;
740 }
741 }
742
743 if(state == MIME_TYPE) {
744 mhdr = mime_hdr_new(ntmp, strip_ends(q));
745 sk_MIME_HEADER_push(headers, mhdr);
746 } else if(state == MIME_VALUE)
747 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
748 if(p == linebuf) break; /* Blank line means end of headers */
749}
750
751return headers;
752
753}
754
755static char *strip_ends(char *name)
756{
757 return strip_end(strip_start(name));
758}
759
760/* Strip a parameter of whitespace from start of param */
761static char *strip_start(char *name)
762{
763 char *p, c;
764 /* Look for first non white space or quote */
765 for(p = name; (c = *p) ;p++) {
766 if(c == '"') {
767 /* Next char is start of string if non null */
768 if(p[1]) return p + 1;
769 /* Else null string */
770 return NULL;
771 }
772 if(!isspace((unsigned char)c)) return p;
773 }
774 return NULL;
775}
776
777/* As above but strip from end of string : maybe should handle brackets? */
778static char *strip_end(char *name)
779{
780 char *p, c;
781 if(!name) return NULL;
782 /* Look for first non white space or quote */
783 for(p = name + strlen(name) - 1; p >= name ;p--) {
784 c = *p;
785 if(c == '"') {
786 if(p - 1 == name) return NULL;
787 *p = 0;
788 return name;
789 }
790 if(isspace((unsigned char)c)) *p = 0;
791 else return name;
792 }
793 return NULL;
794}
795
796static MIME_HEADER *mime_hdr_new(char *name, char *value)
797{
798 MIME_HEADER *mhdr;
799 char *tmpname, *tmpval, *p;
800 int c;
801 if(name) {
802 if(!(tmpname = BUF_strdup(name))) return NULL;
803 for(p = tmpname ; *p; p++) {
804 c = *p;
805 if(isupper(c)) {
806 c = tolower(c);
807 *p = c;
808 }
809 }
810 } else tmpname = NULL;
811 if(value) {
812 if(!(tmpval = BUF_strdup(value))) return NULL;
813 for(p = tmpval ; *p; p++) {
814 c = *p;
815 if(isupper(c)) {
816 c = tolower(c);
817 *p = c;
818 }
819 }
820 } else tmpval = NULL;
821 mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
822 if(!mhdr) return NULL;
823 mhdr->name = tmpname;
824 mhdr->value = tmpval;
825 if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
826 return mhdr;
827}
828
829static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
830{
831 char *tmpname, *tmpval, *p;
832 int c;
833 MIME_PARAM *mparam;
834 if(name) {
835 tmpname = BUF_strdup(name);
836 if(!tmpname) return 0;
837 for(p = tmpname ; *p; p++) {
838 c = *p;
839 if(isupper(c)) {
840 c = tolower(c);
841 *p = c;
842 }
843 }
844 } else tmpname = NULL;
845 if(value) {
846 tmpval = BUF_strdup(value);
847 if(!tmpval) return 0;
848 } else tmpval = NULL;
849 /* Parameter values are case sensitive so leave as is */
850 mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
851 if(!mparam) return 0;
852 mparam->param_name = tmpname;
853 mparam->param_value = tmpval;
854 sk_MIME_PARAM_push(mhdr->params, mparam);
855 return 1;
856}
857
858static int mime_hdr_cmp(const MIME_HEADER * const *a,
859 const MIME_HEADER * const *b)
860{
861 return(strcmp((*a)->name, (*b)->name));
862}
863
864static int mime_param_cmp(const MIME_PARAM * const *a,
865 const MIME_PARAM * const *b)
866{
867 return(strcmp((*a)->param_name, (*b)->param_name));
868}
869
870/* Find a header with a given name (if possible) */
871
872static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
873{
874 MIME_HEADER htmp;
875 int idx;
876 htmp.name = name;
877 idx = sk_MIME_HEADER_find(hdrs, &htmp);
878 if(idx < 0) return NULL;
879 return sk_MIME_HEADER_value(hdrs, idx);
880}
881
882static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
883{
884 MIME_PARAM param;
885 int idx;
886 param.param_name = name;
887 idx = sk_MIME_PARAM_find(hdr->params, &param);
888 if(idx < 0) return NULL;
889 return sk_MIME_PARAM_value(hdr->params, idx);
890}
891
892static void mime_hdr_free(MIME_HEADER *hdr)
893{
894 if(hdr->name) OPENSSL_free(hdr->name);
895 if(hdr->value) OPENSSL_free(hdr->value);
896 if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
897 OPENSSL_free(hdr);
898}
899
900static void mime_param_free(MIME_PARAM *param)
901{
902 if(param->param_name) OPENSSL_free(param->param_name);
903 if(param->param_value) OPENSSL_free(param->param_value);
904 OPENSSL_free(param);
905}
906
907/* Check for a multipart boundary. Returns:
908 * 0 : no boundary
909 * 1 : part boundary
910 * 2 : final boundary
911 */
912static int mime_bound_check(char *line, int linelen, char *bound, int blen)
913{
914 if(linelen == -1) linelen = strlen(line);
915 if(blen == -1) blen = strlen(bound);
916 /* Quickly eliminate if line length too short */
917 if(blen + 2 > linelen) return 0;
918 /* Check for part boundary */
919 if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
920 if(!strncmp(line + blen + 2, "--", 2)) return 2;
921 else return 1;
922 }
923 return 0;
924}
925
926static int strip_eol(char *linebuf, int *plen)
927 {
928 int len = *plen;
929 char *p, c;
930 int is_eol = 0;
931 p = linebuf + len - 1;
932 for (p = linebuf + len - 1; len > 0; len--, p--)
933 {
934 c = *p;
935 if (c == '\n')
936 is_eol = 1;
937 else if (c != '\r')
938 break;
939 }
940 *plen = len;
941 return is_eol;
942 }
diff --git a/src/lib/libcrypto/asn1/asn_moid.c b/src/lib/libcrypto/asn1/asn_moid.c
deleted file mode 100644
index 1ea6a59248..0000000000
--- a/src/lib/libcrypto/asn1/asn_moid.c
+++ /dev/null
@@ -1,160 +0,0 @@
1/* asn_moid.c */
2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include <openssl/crypto.h>
62#include "cryptlib.h"
63#include <openssl/conf.h>
64#include <openssl/dso.h>
65#include <openssl/x509.h>
66
67/* Simple ASN1 OID module: add all objects in a given section */
68
69static int do_create(char *value, char *name);
70
71static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
72 {
73 int i;
74 const char *oid_section;
75 STACK_OF(CONF_VALUE) *sktmp;
76 CONF_VALUE *oval;
77 oid_section = CONF_imodule_get_value(md);
78 if(!(sktmp = NCONF_get_section(cnf, oid_section)))
79 {
80 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
81 return 0;
82 }
83 for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++)
84 {
85 oval = sk_CONF_VALUE_value(sktmp, i);
86 if(!do_create(oval->value, oval->name))
87 {
88 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
89 return 0;
90 }
91 }
92 return 1;
93 }
94
95static void oid_module_finish(CONF_IMODULE *md)
96 {
97 OBJ_cleanup();
98 }
99
100void ASN1_add_oid_module(void)
101 {
102 CONF_module_add("oid_section", oid_module_init, oid_module_finish);
103 }
104
105/* Create an OID based on a name value pair. Accept two formats.
106 * shortname = 1.2.3.4
107 * shortname = some long name, 1.2.3.4
108 */
109
110
111static int do_create(char *value, char *name)
112 {
113 int nid;
114 ASN1_OBJECT *oid;
115 char *ln, *ostr, *p, *lntmp;
116 p = strrchr(value, ',');
117 if (!p)
118 {
119 ln = name;
120 ostr = value;
121 }
122 else
123 {
124 ln = NULL;
125 ostr = p + 1;
126 if (!*ostr)
127 return 0;
128 while(isspace((unsigned char)*ostr)) ostr++;
129 }
130
131 nid = OBJ_create(ostr, name, ln);
132
133 if (nid == NID_undef)
134 return 0;
135
136 if (p)
137 {
138 ln = value;
139 while(isspace((unsigned char)*ln)) ln++;
140 p--;
141 while(isspace((unsigned char)*p))
142 {
143 if (p == ln)
144 return 0;
145 p--;
146 }
147 p++;
148 lntmp = OPENSSL_malloc((p - ln) + 1);
149 if (lntmp == NULL)
150 return 0;
151 memcpy(lntmp, ln, p - ln);
152 lntmp[p - ln] = 0;
153 oid = OBJ_nid2obj(nid);
154 oid->ln = lntmp;
155 }
156
157 return 1;
158 }
159
160
diff --git a/src/lib/libcrypto/asn1/asn_pack.c b/src/lib/libcrypto/asn1/asn_pack.c
deleted file mode 100644
index ad738217d7..0000000000
--- a/src/lib/libcrypto/asn1/asn_pack.c
+++ /dev/null
@@ -1,191 +0,0 @@
1/* asn_pack.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1.h>
62
63#ifndef NO_ASN1_OLD
64
65/* ASN1 packing and unpacking functions */
66
67/* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
68
69STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len,
70 d2i_of_void *d2i, void (*free_func)(OPENSSL_BLOCK))
71{
72 STACK_OF(OPENSSL_BLOCK) *sk;
73 const unsigned char *pbuf;
74 pbuf = buf;
75 if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
76 V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
77 ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR);
78 return sk;
79}
80
81/* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
82 * OPENSSL_malloc'ed buffer
83 */
84
85unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d,
86 unsigned char **buf, int *len)
87{
88 int safelen;
89 unsigned char *safe, *p;
90 if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
91 V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
92 ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR);
93 return NULL;
94 }
95 if (!(safe = OPENSSL_malloc (safelen))) {
96 ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE);
97 return NULL;
98 }
99 p = safe;
100 i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
101 IS_SEQUENCE);
102 if (len) *len = safelen;
103 if (buf) *buf = safe;
104 return safe;
105}
106
107/* Extract an ASN1 object from an ASN1_STRING */
108
109void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
110{
111 const unsigned char *p;
112 char *ret;
113
114 p = oct->data;
115 if(!(ret = d2i(NULL, &p, oct->length)))
116 ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR);
117 return ret;
118}
119
120/* Pack an ASN1 object into an ASN1_STRING */
121
122ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
123{
124 unsigned char *p;
125 ASN1_STRING *octmp;
126
127 if (!oct || !*oct) {
128 if (!(octmp = ASN1_STRING_new ())) {
129 ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
130 return NULL;
131 }
132 if (oct) *oct = octmp;
133 } else octmp = *oct;
134
135 if (!(octmp->length = i2d(obj, NULL))) {
136 ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
137 return NULL;
138 }
139 if (!(p = OPENSSL_malloc (octmp->length))) {
140 ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
141 return NULL;
142 }
143 octmp->data = p;
144 i2d (obj, &p);
145 return octmp;
146}
147
148#endif
149
150/* ASN1_ITEM versions of the above */
151
152ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
153{
154 ASN1_STRING *octmp;
155
156 if (!oct || !*oct) {
157 if (!(octmp = ASN1_STRING_new ())) {
158 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
159 return NULL;
160 }
161 if (oct) *oct = octmp;
162 } else octmp = *oct;
163
164 if(octmp->data) {
165 OPENSSL_free(octmp->data);
166 octmp->data = NULL;
167 }
168
169 if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
170 ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR);
171 return NULL;
172 }
173 if (!octmp->data) {
174 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
175 return NULL;
176 }
177 return octmp;
178}
179
180/* Extract an ASN1 object from an ASN1_STRING */
181
182void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
183{
184 const unsigned char *p;
185 void *ret;
186
187 p = oct->data;
188 if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
189 ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR);
190 return ret;
191}
diff --git a/src/lib/libcrypto/asn1/bio_asn1.c b/src/lib/libcrypto/asn1/bio_asn1.c
deleted file mode 100644
index dc7efd551c..0000000000
--- a/src/lib/libcrypto/asn1/bio_asn1.c
+++ /dev/null
@@ -1,495 +0,0 @@
1/* bio_asn1.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59/* Experimental ASN1 BIO. When written through the data is converted
60 * to an ASN1 string type: default is OCTET STRING. Additional functions
61 * can be provided to add prefix and suffix data.
62 */
63
64#include <string.h>
65#include <openssl/bio.h>
66#include <openssl/asn1.h>
67
68/* Must be large enough for biggest tag+length */
69#define DEFAULT_ASN1_BUF_SIZE 20
70
71typedef enum
72 {
73 ASN1_STATE_START,
74 ASN1_STATE_PRE_COPY,
75 ASN1_STATE_HEADER,
76 ASN1_STATE_HEADER_COPY,
77 ASN1_STATE_DATA_COPY,
78 ASN1_STATE_POST_COPY,
79 ASN1_STATE_DONE
80 } asn1_bio_state_t;
81
82typedef struct BIO_ASN1_EX_FUNCS_st
83 {
84 asn1_ps_func *ex_func;
85 asn1_ps_func *ex_free_func;
86 } BIO_ASN1_EX_FUNCS;
87
88typedef struct BIO_ASN1_BUF_CTX_t
89 {
90 /* Internal state */
91 asn1_bio_state_t state;
92 /* Internal buffer */
93 unsigned char *buf;
94 /* Size of buffer */
95 int bufsize;
96 /* Current position in buffer */
97 int bufpos;
98 /* Current buffer length */
99 int buflen;
100 /* Amount of data to copy */
101 int copylen;
102 /* Class and tag to use */
103 int asn1_class, asn1_tag;
104 asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
105 /* Extra buffer for prefix and suffix data */
106 unsigned char *ex_buf;
107 int ex_len;
108 int ex_pos;
109 void *ex_arg;
110 } BIO_ASN1_BUF_CTX;
111
112
113static int asn1_bio_write(BIO *h, const char *buf,int num);
114static int asn1_bio_read(BIO *h, char *buf, int size);
115static int asn1_bio_puts(BIO *h, const char *str);
116static int asn1_bio_gets(BIO *h, char *str, int size);
117static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
118static int asn1_bio_new(BIO *h);
119static int asn1_bio_free(BIO *data);
120static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
121
122static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
123static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
124 asn1_ps_func *cleanup, asn1_bio_state_t next);
125static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
126 asn1_ps_func *setup,
127 asn1_bio_state_t ex_state,
128 asn1_bio_state_t other_state);
129
130static BIO_METHOD methods_asn1=
131 {
132 BIO_TYPE_ASN1,
133 "asn1",
134 asn1_bio_write,
135 asn1_bio_read,
136 asn1_bio_puts,
137 asn1_bio_gets,
138 asn1_bio_ctrl,
139 asn1_bio_new,
140 asn1_bio_free,
141 asn1_bio_callback_ctrl,
142 };
143
144BIO_METHOD *BIO_f_asn1(void)
145 {
146 return(&methods_asn1);
147 }
148
149
150static int asn1_bio_new(BIO *b)
151 {
152 BIO_ASN1_BUF_CTX *ctx;
153 ctx = OPENSSL_malloc(sizeof(BIO_ASN1_BUF_CTX));
154 if (!ctx)
155 return 0;
156 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE))
157 return 0;
158 b->init = 1;
159 b->ptr = (char *)ctx;
160 b->flags = 0;
161 return 1;
162 }
163
164static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
165 {
166 ctx->buf = OPENSSL_malloc(size);
167 if (!ctx->buf)
168 return 0;
169 ctx->bufsize = size;
170 ctx->bufpos = 0;
171 ctx->buflen = 0;
172 ctx->copylen = 0;
173 ctx->asn1_class = V_ASN1_UNIVERSAL;
174 ctx->asn1_tag = V_ASN1_OCTET_STRING;
175 ctx->ex_buf = 0;
176 ctx->ex_pos = 0;
177 ctx->ex_len = 0;
178 ctx->state = ASN1_STATE_START;
179 return 1;
180 }
181
182static int asn1_bio_free(BIO *b)
183 {
184 BIO_ASN1_BUF_CTX *ctx;
185 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
186 if (ctx == NULL)
187 return 0;
188 if (ctx->buf)
189 OPENSSL_free(ctx->buf);
190 OPENSSL_free(ctx);
191 b->init = 0;
192 b->ptr = NULL;
193 b->flags = 0;
194 return 1;
195 }
196
197static int asn1_bio_write(BIO *b, const char *in , int inl)
198 {
199 BIO_ASN1_BUF_CTX *ctx;
200 int wrmax, wrlen, ret;
201 unsigned char *p;
202 if (!in || (inl < 0) || (b->next_bio == NULL))
203 return 0;
204 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
205 if (ctx == NULL)
206 return 0;
207
208 wrlen = 0;
209 ret = -1;
210
211 for(;;)
212 {
213 switch (ctx->state)
214 {
215
216 /* Setup prefix data, call it */
217 case ASN1_STATE_START:
218 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
219 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
220 return 0;
221 break;
222
223 /* Copy any pre data first */
224 case ASN1_STATE_PRE_COPY:
225
226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
227 ASN1_STATE_HEADER);
228
229 if (ret <= 0)
230 goto done;
231
232 break;
233
234 case ASN1_STATE_HEADER:
235 ctx->buflen =
236 ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
237 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
238 p = ctx->buf;
239 ASN1_put_object(&p, 0, inl,
240 ctx->asn1_tag, ctx->asn1_class);
241 ctx->copylen = inl;
242 ctx->state = ASN1_STATE_HEADER_COPY;
243
244 break;
245
246 case ASN1_STATE_HEADER_COPY:
247 ret = BIO_write(b->next_bio,
248 ctx->buf + ctx->bufpos, ctx->buflen);
249 if (ret <= 0)
250 goto done;
251
252 ctx->buflen -= ret;
253 if (ctx->buflen)
254 ctx->bufpos += ret;
255 else
256 {
257 ctx->bufpos = 0;
258 ctx->state = ASN1_STATE_DATA_COPY;
259 }
260
261 break;
262
263 case ASN1_STATE_DATA_COPY:
264
265 if (inl > ctx->copylen)
266 wrmax = ctx->copylen;
267 else
268 wrmax = inl;
269 ret = BIO_write(b->next_bio, in, wrmax);
270 if (ret <= 0)
271 break;
272 wrlen += ret;
273 ctx->copylen -= ret;
274 in += ret;
275 inl -= ret;
276
277 if (ctx->copylen == 0)
278 ctx->state = ASN1_STATE_HEADER;
279
280 if (inl == 0)
281 goto done;
282
283 break;
284
285 default:
286 BIO_clear_retry_flags(b);
287 return 0;
288
289 }
290
291 }
292
293 done:
294 BIO_clear_retry_flags(b);
295 BIO_copy_next_retry(b);
296
297 return (wrlen > 0) ? wrlen : ret;
298
299 }
300
301static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
302 asn1_ps_func *cleanup, asn1_bio_state_t next)
303 {
304 int ret;
305 if (ctx->ex_len <= 0)
306 return 1;
307 for(;;)
308 {
309 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
310 ctx->ex_len);
311 if (ret <= 0)
312 break;
313 ctx->ex_len -= ret;
314 if (ctx->ex_len > 0)
315 ctx->ex_pos += ret;
316 else
317 {
318 if(cleanup)
319 cleanup(b, &ctx->ex_buf, &ctx->ex_len,
320 &ctx->ex_arg);
321 ctx->state = next;
322 ctx->ex_pos = 0;
323 break;
324 }
325 }
326 return ret;
327 }
328
329static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
330 asn1_ps_func *setup,
331 asn1_bio_state_t ex_state,
332 asn1_bio_state_t other_state)
333 {
334 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg))
335 {
336 BIO_clear_retry_flags(b);
337 return 0;
338 }
339 if (ctx->ex_len > 0)
340 ctx->state = ex_state;
341 else
342 ctx->state = other_state;
343 return 1;
344 }
345
346static int asn1_bio_read(BIO *b, char *in , int inl)
347 {
348 if (!b->next_bio)
349 return 0;
350 return BIO_read(b->next_bio, in , inl);
351 }
352
353static int asn1_bio_puts(BIO *b, const char *str)
354 {
355 return asn1_bio_write(b, str, strlen(str));
356 }
357
358static int asn1_bio_gets(BIO *b, char *str, int size)
359 {
360 if (!b->next_bio)
361 return 0;
362 return BIO_gets(b->next_bio, str , size);
363 }
364
365static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
366 {
367 if (b->next_bio == NULL) return(0);
368 return BIO_callback_ctrl(b->next_bio,cmd,fp);
369 }
370
371static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
372 {
373 BIO_ASN1_BUF_CTX *ctx;
374 BIO_ASN1_EX_FUNCS *ex_func;
375 long ret = 1;
376 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
377 if (ctx == NULL)
378 return 0;
379 switch(cmd)
380 {
381
382 case BIO_C_SET_PREFIX:
383 ex_func = arg2;
384 ctx->prefix = ex_func->ex_func;
385 ctx->prefix_free = ex_func->ex_free_func;
386 break;
387
388 case BIO_C_GET_PREFIX:
389 ex_func = arg2;
390 ex_func->ex_func = ctx->prefix;
391 ex_func->ex_free_func = ctx->prefix_free;
392 break;
393
394 case BIO_C_SET_SUFFIX:
395 ex_func = arg2;
396 ctx->suffix = ex_func->ex_func;
397 ctx->suffix_free = ex_func->ex_free_func;
398 break;
399
400 case BIO_C_GET_SUFFIX:
401 ex_func = arg2;
402 ex_func->ex_func = ctx->suffix;
403 ex_func->ex_free_func = ctx->suffix_free;
404 break;
405
406 case BIO_C_SET_EX_ARG:
407 ctx->ex_arg = arg2;
408 break;
409
410 case BIO_C_GET_EX_ARG:
411 *(void **)arg2 = ctx->ex_arg;
412 break;
413
414 case BIO_CTRL_FLUSH:
415 if (!b->next_bio)
416 return 0;
417
418 /* Call post function if possible */
419 if (ctx->state == ASN1_STATE_HEADER)
420 {
421 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
422 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
423 return 0;
424 }
425
426 if (ctx->state == ASN1_STATE_POST_COPY)
427 {
428 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
429 ASN1_STATE_DONE);
430 if (ret <= 0)
431 return ret;
432 }
433
434 if (ctx->state == ASN1_STATE_DONE)
435 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
436 else
437 {
438 BIO_clear_retry_flags(b);
439 return 0;
440 }
441 break;
442
443
444 default:
445 if (!b->next_bio)
446 return 0;
447 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
448
449 }
450
451 return ret;
452 }
453
454static int asn1_bio_set_ex(BIO *b, int cmd,
455 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
456 {
457 BIO_ASN1_EX_FUNCS extmp;
458 extmp.ex_func = ex_func;
459 extmp.ex_free_func = ex_free_func;
460 return BIO_ctrl(b, cmd, 0, &extmp);
461 }
462
463static int asn1_bio_get_ex(BIO *b, int cmd,
464 asn1_ps_func **ex_func, asn1_ps_func **ex_free_func)
465 {
466 BIO_ASN1_EX_FUNCS extmp;
467 int ret;
468 ret = BIO_ctrl(b, cmd, 0, &extmp);
469 if (ret > 0)
470 {
471 *ex_func = extmp.ex_func;
472 *ex_free_func = extmp.ex_free_func;
473 }
474 return ret;
475 }
476
477int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
478 {
479 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
480 }
481
482int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
483 {
484 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
485 }
486
487int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
488 {
489 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
490 }
491
492int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
493 {
494 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
495 }
diff --git a/src/lib/libcrypto/asn1/bio_ndef.c b/src/lib/libcrypto/asn1/bio_ndef.c
deleted file mode 100644
index b91f97a1b1..0000000000
--- a/src/lib/libcrypto/asn1/bio_ndef.c
+++ /dev/null
@@ -1,243 +0,0 @@
1/* bio_ndef.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 */
54
55#include <openssl/asn1.h>
56#include <openssl/asn1t.h>
57#include <openssl/bio.h>
58#include <openssl/err.h>
59
60#include <stdio.h>
61
62/* Experimental NDEF ASN1 BIO support routines */
63
64/* The usage is quite simple, initialize an ASN1 structure,
65 * get a BIO from it then any data written through the BIO
66 * will end up translated to approptiate format on the fly.
67 * The data is streamed out and does *not* need to be
68 * all held in memory at once.
69 *
70 * When the BIO is flushed the output is finalized and any
71 * signatures etc written out.
72 *
73 * The BIO is a 'proper' BIO and can handle non blocking I/O
74 * correctly.
75 *
76 * The usage is simple. The implementation is *not*...
77 */
78
79/* BIO support data stored in the ASN1 BIO ex_arg */
80
81typedef struct ndef_aux_st
82 {
83 /* ASN1 structure this BIO refers to */
84 ASN1_VALUE *val;
85 const ASN1_ITEM *it;
86 /* Top of the BIO chain */
87 BIO *ndef_bio;
88 /* Output BIO */
89 BIO *out;
90 /* Boundary where content is inserted */
91 unsigned char **boundary;
92 /* DER buffer start */
93 unsigned char *derbuf;
94 } NDEF_SUPPORT;
95
96static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
97static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
98static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
99static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
100
101BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
102 {
103 NDEF_SUPPORT *ndef_aux = NULL;
104 BIO *asn_bio = NULL;
105 const ASN1_AUX *aux = it->funcs;
106 ASN1_STREAM_ARG sarg;
107
108 if (!aux || !aux->asn1_cb)
109 {
110 ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
111 return NULL;
112 }
113 ndef_aux = OPENSSL_malloc(sizeof(NDEF_SUPPORT));
114 asn_bio = BIO_new(BIO_f_asn1());
115
116 /* ASN1 bio needs to be next to output BIO */
117
118 out = BIO_push(asn_bio, out);
119
120 if (!ndef_aux || !asn_bio || !out)
121 goto err;
122
123 BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
124 BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
125
126 /* Now let callback prepend any digest, cipher etc BIOs
127 * ASN1 structure needs.
128 */
129
130 sarg.out = out;
131 sarg.ndef_bio = NULL;
132 sarg.boundary = NULL;
133
134 if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
135 goto err;
136
137 ndef_aux->val = val;
138 ndef_aux->it = it;
139 ndef_aux->ndef_bio = sarg.ndef_bio;
140 ndef_aux->boundary = sarg.boundary;
141 ndef_aux->out = out;
142
143 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
144
145 return sarg.ndef_bio;
146
147 err:
148 if (asn_bio)
149 BIO_free(asn_bio);
150 if (ndef_aux)
151 OPENSSL_free(ndef_aux);
152 return NULL;
153 }
154
155static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
156 {
157 NDEF_SUPPORT *ndef_aux;
158 unsigned char *p;
159 int derlen;
160
161 if (!parg)
162 return 0;
163
164 ndef_aux = *(NDEF_SUPPORT **)parg;
165
166 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
167 p = OPENSSL_malloc(derlen);
168 ndef_aux->derbuf = p;
169 *pbuf = p;
170 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
171
172 if (!*ndef_aux->boundary)
173 return 0;
174
175 *plen = *ndef_aux->boundary - *pbuf;
176
177 return 1;
178 }
179
180static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
181 {
182 NDEF_SUPPORT *ndef_aux;
183
184 if (!parg)
185 return 0;
186
187 ndef_aux = *(NDEF_SUPPORT **)parg;
188
189 if (ndef_aux->derbuf)
190 OPENSSL_free(ndef_aux->derbuf);
191
192 ndef_aux->derbuf = NULL;
193 *pbuf = NULL;
194 *plen = 0;
195 return 1;
196 }
197
198static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
199 {
200 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
201 if (!ndef_prefix_free(b, pbuf, plen, parg))
202 return 0;
203 OPENSSL_free(*pndef_aux);
204 *pndef_aux = NULL;
205 return 1;
206 }
207
208static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
209 {
210 NDEF_SUPPORT *ndef_aux;
211 unsigned char *p;
212 int derlen;
213 const ASN1_AUX *aux;
214 ASN1_STREAM_ARG sarg;
215
216 if (!parg)
217 return 0;
218
219 ndef_aux = *(NDEF_SUPPORT **)parg;
220
221 aux = ndef_aux->it->funcs;
222
223 /* Finalize structures */
224 sarg.ndef_bio = ndef_aux->ndef_bio;
225 sarg.out = ndef_aux->out;
226 sarg.boundary = ndef_aux->boundary;
227 if (aux->asn1_cb(ASN1_OP_STREAM_POST,
228 &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
229 return 0;
230
231 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
232 p = OPENSSL_malloc(derlen);
233 ndef_aux->derbuf = p;
234 *pbuf = p;
235 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
236
237 if (!*ndef_aux->boundary)
238 return 0;
239 *pbuf = *ndef_aux->boundary;
240 *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
241
242 return 1;
243 }
diff --git a/src/lib/libcrypto/asn1/charmap.h b/src/lib/libcrypto/asn1/charmap.h
deleted file mode 100644
index b55e638725..0000000000
--- a/src/lib/libcrypto/asn1/charmap.h
+++ /dev/null
@@ -1,15 +0,0 @@
1/* Auto generated with chartype.pl script.
2 * Mask of various character properties
3 */
4
5static const unsigned char char_type[] = {
6 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
7 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
8120, 0, 1,40, 0, 0, 0,16,16,16, 0,25,25,16,16,16,
916,16,16,16,16,16,16,16,16,16,16, 9, 9,16, 9,16,
10 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
1116,16,16,16,16,16,16,16,16,16,16, 0, 1, 0, 0, 0,
12 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
1316,16,16,16,16,16,16,16,16,16,16, 0, 0, 0, 0, 2
14};
15
diff --git a/src/lib/libcrypto/asn1/charmap.pl b/src/lib/libcrypto/asn1/charmap.pl
deleted file mode 100644
index 2875c59867..0000000000
--- a/src/lib/libcrypto/asn1/charmap.pl
+++ /dev/null
@@ -1,80 +0,0 @@
1#!/usr/local/bin/perl -w
2
3use strict;
4
5my ($i, @arr);
6
7# Set up an array with the type of ASCII characters
8# Each set bit represents a character property.
9
10# RFC2253 character properties
11my $RFC2253_ESC = 1; # Character escaped with \
12my $ESC_CTRL = 2; # Escaped control character
13# These are used with RFC1779 quoting using "
14my $NOESC_QUOTE = 8; # Not escaped if quoted
15my $PSTRING_CHAR = 0x10; # Valid PrintableString character
16my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
17my $RFC2253_LAST_ESC = 0x40; # Escaped with \ if last character
18
19for($i = 0; $i < 128; $i++) {
20 # Set the RFC2253 escape characters (control)
21 $arr[$i] = 0;
22 if(($i < 32) || ($i > 126)) {
23 $arr[$i] |= $ESC_CTRL;
24 }
25
26 # Some PrintableString characters
27 if( ( ( $i >= ord("a")) && ( $i <= ord("z")) )
28 || ( ( $i >= ord("A")) && ( $i <= ord("Z")) )
29 || ( ( $i >= ord("0")) && ( $i <= ord("9")) ) ) {
30 $arr[$i] |= $PSTRING_CHAR;
31 }
32}
33
34# Now setup the rest
35
36# Remaining RFC2253 escaped characters
37
38$arr[ord(" ")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC | $RFC2253_LAST_ESC;
39$arr[ord("#")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC;
40
41$arr[ord(",")] |= $NOESC_QUOTE | $RFC2253_ESC;
42$arr[ord("+")] |= $NOESC_QUOTE | $RFC2253_ESC;
43$arr[ord("\"")] |= $RFC2253_ESC;
44$arr[ord("\\")] |= $RFC2253_ESC;
45$arr[ord("<")] |= $NOESC_QUOTE | $RFC2253_ESC;
46$arr[ord(">")] |= $NOESC_QUOTE | $RFC2253_ESC;
47$arr[ord(";")] |= $NOESC_QUOTE | $RFC2253_ESC;
48
49# Remaining PrintableString characters
50
51$arr[ord(" ")] |= $PSTRING_CHAR;
52$arr[ord("'")] |= $PSTRING_CHAR;
53$arr[ord("(")] |= $PSTRING_CHAR;
54$arr[ord(")")] |= $PSTRING_CHAR;
55$arr[ord("+")] |= $PSTRING_CHAR;
56$arr[ord(",")] |= $PSTRING_CHAR;
57$arr[ord("-")] |= $PSTRING_CHAR;
58$arr[ord(".")] |= $PSTRING_CHAR;
59$arr[ord("/")] |= $PSTRING_CHAR;
60$arr[ord(":")] |= $PSTRING_CHAR;
61$arr[ord("=")] |= $PSTRING_CHAR;
62$arr[ord("?")] |= $PSTRING_CHAR;
63
64# Now generate the C code
65
66print <<EOF;
67/* Auto generated with chartype.pl script.
68 * Mask of various character properties
69 */
70
71static unsigned char char_type[] = {
72EOF
73
74for($i = 0; $i < 128; $i++) {
75 print("\n") if($i && (($i % 16) == 0));
76 printf("%2d", $arr[$i]);
77 print(",") if ($i != 127);
78}
79print("\n};\n\n");
80
diff --git a/src/lib/libcrypto/asn1/d2i_pr.c b/src/lib/libcrypto/asn1/d2i_pr.c
deleted file mode 100644
index 2828944777..0000000000
--- a/src/lib/libcrypto/asn1/d2i_pr.c
+++ /dev/null
@@ -1,170 +0,0 @@
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 <openssl/bn.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#ifndef OPENSSL_NO_ENGINE
65#include <openssl/engine.h>
66#endif
67#include <openssl/x509.h>
68#include <openssl/asn1.h>
69#include "asn1_locl.h"
70
71EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
72 long length)
73 {
74 EVP_PKEY *ret;
75
76 if ((a == NULL) || (*a == NULL))
77 {
78 if ((ret=EVP_PKEY_new()) == NULL)
79 {
80 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_EVP_LIB);
81 return(NULL);
82 }
83 }
84 else
85 {
86 ret= *a;
87#ifndef OPENSSL_NO_ENGINE
88 if (ret->engine)
89 {
90 ENGINE_finish(ret->engine);
91 ret->engine = NULL;
92 }
93#endif
94 }
95
96 if (!EVP_PKEY_set_type(ret, type))
97 {
98 ASN1err(ASN1_F_D2I_PRIVATEKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
99 goto err;
100 }
101
102 if (!ret->ameth->old_priv_decode ||
103 !ret->ameth->old_priv_decode(ret, pp, length))
104 {
105 if (ret->ameth->priv_decode)
106 {
107 PKCS8_PRIV_KEY_INFO *p8=NULL;
108 p8=d2i_PKCS8_PRIV_KEY_INFO(NULL,pp,length);
109 if (!p8) goto err;
110 EVP_PKEY_free(ret);
111 ret = EVP_PKCS82PKEY(p8);
112 PKCS8_PRIV_KEY_INFO_free(p8);
113
114 }
115 else
116 {
117 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
118 goto err;
119 }
120 }
121 if (a != NULL) (*a)=ret;
122 return(ret);
123err:
124 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
125 return(NULL);
126 }
127
128/* This works like d2i_PrivateKey() except it automatically works out the type */
129
130EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
131 long length)
132{
133 STACK_OF(ASN1_TYPE) *inkey;
134 const unsigned char *p;
135 int keytype;
136 p = *pp;
137 /* Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE):
138 * by analyzing it we can determine the passed structure: this
139 * assumes the input is surrounded by an ASN1 SEQUENCE.
140 */
141 inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
142 /* Since we only need to discern "traditional format" RSA and DSA
143 * keys we can just count the elements.
144 */
145 if(sk_ASN1_TYPE_num(inkey) == 6)
146 keytype = EVP_PKEY_DSA;
147 else if (sk_ASN1_TYPE_num(inkey) == 4)
148 keytype = EVP_PKEY_EC;
149 else if (sk_ASN1_TYPE_num(inkey) == 3)
150 { /* This seems to be PKCS8, not traditional format */
151 PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL,pp,length);
152 EVP_PKEY *ret;
153
154 sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
155 if (!p8)
156 {
157 ASN1err(ASN1_F_D2I_AUTOPRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
158 return NULL;
159 }
160 ret = EVP_PKCS82PKEY(p8);
161 PKCS8_PRIV_KEY_INFO_free(p8);
162 if (a) {
163 *a = ret;
164 }
165 return ret;
166 }
167 else keytype = EVP_PKEY_RSA;
168 sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
169 return d2i_PrivateKey(keytype, a, pp, length);
170}
diff --git a/src/lib/libcrypto/asn1/d2i_pu.c b/src/lib/libcrypto/asn1/d2i_pu.c
deleted file mode 100644
index c8f39ceb03..0000000000
--- a/src/lib/libcrypto/asn1/d2i_pu.c
+++ /dev/null
@@ -1,139 +0,0 @@
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 <openssl/bn.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/asn1.h>
65#ifndef OPENSSL_NO_RSA
66#include <openssl/rsa.h>
67#endif
68#ifndef OPENSSL_NO_DSA
69#include <openssl/dsa.h>
70#endif
71#ifndef OPENSSL_NO_EC
72#include <openssl/ec.h>
73#endif
74
75EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
76 long length)
77 {
78 EVP_PKEY *ret;
79
80 if ((a == NULL) || (*a == NULL))
81 {
82 if ((ret=EVP_PKEY_new()) == NULL)
83 {
84 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
85 return(NULL);
86 }
87 }
88 else ret= *a;
89
90 if (!EVP_PKEY_set_type(ret, type))
91 {
92 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
93 goto err;
94 }
95
96 switch (EVP_PKEY_id(ret))
97 {
98#ifndef OPENSSL_NO_RSA
99 case EVP_PKEY_RSA:
100 if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,
101 (const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
102 {
103 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
104 goto err;
105 }
106 break;
107#endif
108#ifndef OPENSSL_NO_DSA
109 case EVP_PKEY_DSA:
110 if (!d2i_DSAPublicKey(&(ret->pkey.dsa),
111 (const unsigned char **)pp,length)) /* TMP UGLY CAST */
112 {
113 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
114 goto err;
115 }
116 break;
117#endif
118#ifndef OPENSSL_NO_EC
119 case EVP_PKEY_EC:
120 if (!o2i_ECPublicKey(&(ret->pkey.ec),
121 (const unsigned char **)pp, length))
122 {
123 ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
124 goto err;
125 }
126 break;
127#endif
128 default:
129 ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
130 goto err;
131 /* break; */
132 }
133 if (a != NULL) (*a)=ret;
134 return(ret);
135err:
136 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
137 return(NULL);
138 }
139
diff --git a/src/lib/libcrypto/asn1/evp_asn1.c b/src/lib/libcrypto/asn1/evp_asn1.c
deleted file mode 100644
index f3d9804860..0000000000
--- a/src/lib/libcrypto/asn1/evp_asn1.c
+++ /dev/null
@@ -1,189 +0,0 @@
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 <openssl/asn1.h>
62#include <openssl/asn1_mac.h>
63
64int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
65 {
66 ASN1_STRING *os;
67
68 if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0);
69 if (!M_ASN1_OCTET_STRING_set(os,data,len)) return(0);
70 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
71 return(1);
72 }
73
74/* int max_len: for returned value */
75int ASN1_TYPE_get_octetstring(ASN1_TYPE *a, unsigned char *data,
76 int max_len)
77 {
78 int ret,num;
79 unsigned char *p;
80
81 if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL))
82 {
83 ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
84 return(-1);
85 }
86 p=M_ASN1_STRING_data(a->value.octet_string);
87 ret=M_ASN1_STRING_length(a->value.octet_string);
88 if (ret < max_len)
89 num=ret;
90 else
91 num=max_len;
92 memcpy(data,p,num);
93 return(ret);
94 }
95
96int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
97 int len)
98 {
99 int n,size;
100 ASN1_OCTET_STRING os,*osp;
101 ASN1_INTEGER in;
102 unsigned char *p;
103 unsigned char buf[32]; /* when they have 256bit longs,
104 * I'll be in trouble */
105 in.data=buf;
106 in.length=32;
107 os.data=data;
108 os.type=V_ASN1_OCTET_STRING;
109 os.length=len;
110 ASN1_INTEGER_set(&in,num);
111 n = i2d_ASN1_INTEGER(&in,NULL);
112 n+=M_i2d_ASN1_OCTET_STRING(&os,NULL);
113
114 size=ASN1_object_size(1,n,V_ASN1_SEQUENCE);
115
116 if ((osp=ASN1_STRING_new()) == NULL) return(0);
117 /* Grow the 'string' */
118 if (!ASN1_STRING_set(osp,NULL,size))
119 {
120 ASN1_STRING_free(osp);
121 return(0);
122 }
123
124 M_ASN1_STRING_length_set(osp, size);
125 p=M_ASN1_STRING_data(osp);
126
127 ASN1_put_object(&p,1,n,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
128 i2d_ASN1_INTEGER(&in,&p);
129 M_i2d_ASN1_OCTET_STRING(&os,&p);
130
131 ASN1_TYPE_set(a,V_ASN1_SEQUENCE,osp);
132 return(1);
133 }
134
135/* we return the actual length..., num may be missing, in which
136 * case, set it to zero */
137/* int max_len: for returned value */
138int ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a, long *num, unsigned char *data,
139 int max_len)
140 {
141 int ret= -1,n;
142 ASN1_INTEGER *ai=NULL;
143 ASN1_OCTET_STRING *os=NULL;
144 const unsigned char *p;
145 long length;
146 ASN1_const_CTX c;
147
148 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
149 {
150 goto err;
151 }
152 p=M_ASN1_STRING_data(a->value.sequence);
153 length=M_ASN1_STRING_length(a->value.sequence);
154
155 c.pp= &p;
156 c.p=p;
157 c.max=p+length;
158 c.error=ASN1_R_DATA_IS_WRONG;
159
160 M_ASN1_D2I_start_sequence();
161 c.q=c.p;
162 if ((ai=d2i_ASN1_INTEGER(NULL,&c.p,c.slen)) == NULL) goto err;
163 c.slen-=(c.p-c.q);
164 c.q=c.p;
165 if ((os=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) goto err;
166 c.slen-=(c.p-c.q);
167 if (!M_ASN1_D2I_end_sequence()) goto err;
168
169 if (num != NULL)
170 *num=ASN1_INTEGER_get(ai);
171
172 ret=M_ASN1_STRING_length(os);
173 if (max_len > ret)
174 n=ret;
175 else
176 n=max_len;
177
178 if (data != NULL)
179 memcpy(data,M_ASN1_STRING_data(os),n);
180 if (0)
181 {
182err:
183 ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
184 }
185 if (os != NULL) M_ASN1_OCTET_STRING_free(os);
186 if (ai != NULL) M_ASN1_INTEGER_free(ai);
187 return(ret);
188 }
189
diff --git a/src/lib/libcrypto/asn1/f_enum.c b/src/lib/libcrypto/asn1/f_enum.c
deleted file mode 100644
index 56e3cc8df2..0000000000
--- a/src/lib/libcrypto/asn1/f_enum.c
+++ /dev/null
@@ -1,207 +0,0 @@
1/* crypto/asn1/f_enum.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/buffer.h>
62#include <openssl/asn1.h>
63
64/* Based on a_int.c: equivalent ENUMERATED functions */
65
66int i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a)
67 {
68 int i,n=0;
69 static const 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_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
100 {
101 int ret=0;
102 int i,j,k,m,n,again,bufsize;
103 unsigned char *s=NULL,*sp;
104 unsigned char *bufp;
105 int num=0,slen=0,first=1;
106
107 bs->type=V_ASN1_ENUMERATED;
108
109 bufsize=BIO_gets(bp,buf,size);
110 for (;;)
111 {
112 if (bufsize < 1) goto err_sl;
113 i=bufsize;
114 if (buf[i-1] == '\n') buf[--i]='\0';
115 if (i == 0) goto err_sl;
116 if (buf[i-1] == '\r') buf[--i]='\0';
117 if (i == 0) goto err_sl;
118 again=(buf[i-1] == '\\');
119
120 for (j=0; j<i; j++)
121 {
122 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
123 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
124 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
125 {
126 i=j;
127 break;
128 }
129 }
130 buf[i]='\0';
131 /* We have now cleared all the crap off the end of the
132 * line */
133 if (i < 2) goto err_sl;
134
135 bufp=(unsigned char *)buf;
136 if (first)
137 {
138 first=0;
139 if ((bufp[0] == '0') && (buf[1] == '0'))
140 {
141 bufp+=2;
142 i-=2;
143 }
144 }
145 k=0;
146 i-=again;
147 if (i%2 != 0)
148 {
149 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_ODD_NUMBER_OF_CHARS);
150 goto err;
151 }
152 i/=2;
153 if (num+i > slen)
154 {
155 if (s == NULL)
156 sp=(unsigned char *)OPENSSL_malloc(
157 (unsigned int)num+i*2);
158 else
159 sp=(unsigned char *)OPENSSL_realloc(s,
160 (unsigned int)num+i*2);
161 if (sp == NULL)
162 {
163 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
164 if (s != NULL) OPENSSL_free(s);
165 goto err;
166 }
167 s=sp;
168 slen=num+i*2;
169 }
170 for (j=0; j<i; j++,k+=2)
171 {
172 for (n=0; n<2; n++)
173 {
174 m=bufp[k+n];
175 if ((m >= '0') && (m <= '9'))
176 m-='0';
177 else if ((m >= 'a') && (m <= 'f'))
178 m=m-'a'+10;
179 else if ((m >= 'A') && (m <= 'F'))
180 m=m-'A'+10;
181 else
182 {
183 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_NON_HEX_CHARACTERS);
184 goto err;
185 }
186 s[num+j]<<=4;
187 s[num+j]|=m;
188 }
189 }
190 num+=i;
191 if (again)
192 bufsize=BIO_gets(bp,buf,size);
193 else
194 break;
195 }
196 bs->length=num;
197 bs->data=s;
198 ret=1;
199err:
200 if (0)
201 {
202err_sl:
203 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_SHORT_LINE);
204 }
205 return(ret);
206 }
207
diff --git a/src/lib/libcrypto/asn1/f_int.c b/src/lib/libcrypto/asn1/f_int.c
deleted file mode 100644
index 9494e597ab..0000000000
--- a/src/lib/libcrypto/asn1/f_int.c
+++ /dev/null
@@ -1,219 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/asn1.h>
63
64int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a)
65 {
66 int i,n=0;
67 static const char *h="0123456789ABCDEF";
68 char buf[2];
69
70 if (a == NULL) return(0);
71
72 if (a->type & V_ASN1_NEG)
73 {
74 if (BIO_write(bp, "-", 1) != 1) goto err;
75 n = 1;
76 }
77
78 if (a->length == 0)
79 {
80 if (BIO_write(bp,"00",2) != 2) goto err;
81 n += 2;
82 }
83 else
84 {
85 for (i=0; i<a->length; i++)
86 {
87 if ((i != 0) && (i%35 == 0))
88 {
89 if (BIO_write(bp,"\\\n",2) != 2) goto err;
90 n+=2;
91 }
92 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
93 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
94 if (BIO_write(bp,buf,2) != 2) goto err;
95 n+=2;
96 }
97 }
98 return(n);
99err:
100 return(-1);
101 }
102
103int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int 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#ifndef CHARSET_EBCDIC
127 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
128 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
129 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
130#else
131 /* This #ifdef is not strictly necessary, since
132 * the characters A...F a...f 0...9 are contiguous
133 * (yes, even in EBCDIC - but not the whole alphabet).
134 * Nevertheless, isxdigit() is faster.
135 */
136 if (!isxdigit(buf[j]))
137#endif
138 {
139 i=j;
140 break;
141 }
142 }
143 buf[i]='\0';
144 /* We have now cleared all the crap off the end of the
145 * line */
146 if (i < 2) goto err_sl;
147
148 bufp=(unsigned char *)buf;
149 if (first)
150 {
151 first=0;
152 if ((bufp[0] == '0') && (buf[1] == '0'))
153 {
154 bufp+=2;
155 i-=2;
156 }
157 }
158 k=0;
159 i-=again;
160 if (i%2 != 0)
161 {
162 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS);
163 goto err;
164 }
165 i/=2;
166 if (num+i > slen)
167 {
168 if (s == NULL)
169 sp=(unsigned char *)OPENSSL_malloc(
170 (unsigned int)num+i*2);
171 else
172 sp=OPENSSL_realloc_clean(s,slen,num+i*2);
173 if (sp == NULL)
174 {
175 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
176 if (s != NULL) OPENSSL_free(s);
177 goto err;
178 }
179 s=sp;
180 slen=num+i*2;
181 }
182 for (j=0; j<i; j++,k+=2)
183 {
184 for (n=0; n<2; n++)
185 {
186 m=bufp[k+n];
187 if ((m >= '0') && (m <= '9'))
188 m-='0';
189 else if ((m >= 'a') && (m <= 'f'))
190 m=m-'a'+10;
191 else if ((m >= 'A') && (m <= 'F'))
192 m=m-'A'+10;
193 else
194 {
195 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS);
196 goto err;
197 }
198 s[num+j]<<=4;
199 s[num+j]|=m;
200 }
201 }
202 num+=i;
203 if (again)
204 bufsize=BIO_gets(bp,buf,size);
205 else
206 break;
207 }
208 bs->length=num;
209 bs->data=s;
210 ret=1;
211err:
212 if (0)
213 {
214err_sl:
215 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE);
216 }
217 return(ret);
218 }
219
diff --git a/src/lib/libcrypto/asn1/f_string.c b/src/lib/libcrypto/asn1/f_string.c
deleted file mode 100644
index 968698a798..0000000000
--- a/src/lib/libcrypto/asn1/f_string.c
+++ /dev/null
@@ -1,212 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/asn1.h>
63
64int i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type)
65 {
66 int i,n=0;
67 static const char *h="0123456789ABCDEF";
68 char buf[2];
69
70 if (a == NULL) return(0);
71
72 if (a->length == 0)
73 {
74 if (BIO_write(bp,"0",1) != 1) goto err;
75 n=1;
76 }
77 else
78 {
79 for (i=0; i<a->length; i++)
80 {
81 if ((i != 0) && (i%35 == 0))
82 {
83 if (BIO_write(bp,"\\\n",2) != 2) goto err;
84 n+=2;
85 }
86 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
87 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
88 if (BIO_write(bp,buf,2) != 2) goto err;
89 n+=2;
90 }
91 }
92 return(n);
93err:
94 return(-1);
95 }
96
97int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
98 {
99 int ret=0;
100 int i,j,k,m,n,again,bufsize;
101 unsigned char *s=NULL,*sp;
102 unsigned char *bufp;
103 int num=0,slen=0,first=1;
104
105 bufsize=BIO_gets(bp,buf,size);
106 for (;;)
107 {
108 if (bufsize < 1)
109 {
110 if (first)
111 break;
112 else
113 goto err_sl;
114 }
115 first=0;
116
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=i-1; j>0; j--)
125 {
126#ifndef CHARSET_EBCDIC
127 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
128 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
129 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
130#else
131 /* This #ifdef is not strictly necessary, since
132 * the characters A...F a...f 0...9 are contiguous
133 * (yes, even in EBCDIC - but not the whole alphabet).
134 * Nevertheless, isxdigit() is faster.
135 */
136 if (!isxdigit(buf[j]))
137#endif
138 {
139 i=j;
140 break;
141 }
142 }
143 buf[i]='\0';
144 /* We have now cleared all the crap off the end of the
145 * line */
146 if (i < 2) goto err_sl;
147
148 bufp=(unsigned char *)buf;
149
150 k=0;
151 i-=again;
152 if (i%2 != 0)
153 {
154 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS);
155 goto err;
156 }
157 i/=2;
158 if (num+i > slen)
159 {
160 if (s == NULL)
161 sp=(unsigned char *)OPENSSL_malloc(
162 (unsigned int)num+i*2);
163 else
164 sp=(unsigned char *)OPENSSL_realloc(s,
165 (unsigned int)num+i*2);
166 if (sp == NULL)
167 {
168 ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE);
169 if (s != NULL) OPENSSL_free(s);
170 goto err;
171 }
172 s=sp;
173 slen=num+i*2;
174 }
175 for (j=0; j<i; j++,k+=2)
176 {
177 for (n=0; n<2; n++)
178 {
179 m=bufp[k+n];
180 if ((m >= '0') && (m <= '9'))
181 m-='0';
182 else if ((m >= 'a') && (m <= 'f'))
183 m=m-'a'+10;
184 else if ((m >= 'A') && (m <= 'F'))
185 m=m-'A'+10;
186 else
187 {
188 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS);
189 goto err;
190 }
191 s[num+j]<<=4;
192 s[num+j]|=m;
193 }
194 }
195 num+=i;
196 if (again)
197 bufsize=BIO_gets(bp,buf,size);
198 else
199 break;
200 }
201 bs->length=num;
202 bs->data=s;
203 ret=1;
204err:
205 if (0)
206 {
207err_sl:
208 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE);
209 }
210 return(ret);
211 }
212
diff --git a/src/lib/libcrypto/asn1/i2d_pr.c b/src/lib/libcrypto/asn1/i2d_pr.c
deleted file mode 100644
index e398b62666..0000000000
--- a/src/lib/libcrypto/asn1/i2d_pr.c
+++ /dev/null
@@ -1,80 +0,0 @@
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 <openssl/evp.h>
62#include <openssl/x509.h>
63#include "asn1_locl.h"
64
65int i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp)
66 {
67 if (a->ameth && a->ameth->old_priv_encode)
68 {
69 return a->ameth->old_priv_encode(a, pp);
70 }
71 if (a->ameth && a->ameth->priv_encode) {
72 PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
73 int ret = i2d_PKCS8_PRIV_KEY_INFO(p8,pp);
74 PKCS8_PRIV_KEY_INFO_free(p8);
75 return ret;
76 }
77 ASN1err(ASN1_F_I2D_PRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
78 return(-1);
79 }
80
diff --git a/src/lib/libcrypto/asn1/i2d_pu.c b/src/lib/libcrypto/asn1/i2d_pu.c
deleted file mode 100644
index 34286dbd35..0000000000
--- a/src/lib/libcrypto/asn1/i2d_pu.c
+++ /dev/null
@@ -1,95 +0,0 @@
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 <openssl/bn.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#ifndef OPENSSL_NO_RSA
65#include <openssl/rsa.h>
66#endif
67#ifndef OPENSSL_NO_DSA
68#include <openssl/dsa.h>
69#endif
70#ifndef OPENSSL_NO_EC
71#include <openssl/ec.h>
72#endif
73
74int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp)
75 {
76 switch (a->type)
77 {
78#ifndef OPENSSL_NO_RSA
79 case EVP_PKEY_RSA:
80 return(i2d_RSAPublicKey(a->pkey.rsa,pp));
81#endif
82#ifndef OPENSSL_NO_DSA
83 case EVP_PKEY_DSA:
84 return(i2d_DSAPublicKey(a->pkey.dsa,pp));
85#endif
86#ifndef OPENSSL_NO_EC
87 case EVP_PKEY_EC:
88 return(i2o_ECPublicKey(a->pkey.ec, pp));
89#endif
90 default:
91 ASN1err(ASN1_F_I2D_PUBLICKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
92 return(-1);
93 }
94 }
95
diff --git a/src/lib/libcrypto/asn1/n_pkey.c b/src/lib/libcrypto/asn1/n_pkey.c
deleted file mode 100644
index e7d0439062..0000000000
--- a/src/lib/libcrypto/asn1/n_pkey.c
+++ /dev/null
@@ -1,343 +0,0 @@
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#ifndef OPENSSL_NO_RSA
62#include <openssl/rsa.h>
63#include <openssl/objects.h>
64#include <openssl/asn1t.h>
65#include <openssl/asn1_mac.h>
66#include <openssl/evp.h>
67#include <openssl/x509.h>
68
69
70#ifndef OPENSSL_NO_RC4
71
72typedef struct netscape_pkey_st
73 {
74 long version;
75 X509_ALGOR *algor;
76 ASN1_OCTET_STRING *private_key;
77 } NETSCAPE_PKEY;
78
79typedef struct netscape_encrypted_pkey_st
80 {
81 ASN1_OCTET_STRING *os;
82 /* This is the same structure as DigestInfo so use it:
83 * although this isn't really anything to do with
84 * digests.
85 */
86 X509_SIG *enckey;
87 } NETSCAPE_ENCRYPTED_PKEY;
88
89
90ASN1_BROKEN_SEQUENCE(NETSCAPE_ENCRYPTED_PKEY) = {
91 ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, os, ASN1_OCTET_STRING),
92 ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, enckey, X509_SIG)
93} ASN1_BROKEN_SEQUENCE_END(NETSCAPE_ENCRYPTED_PKEY)
94
95DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
96DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY,NETSCAPE_ENCRYPTED_PKEY)
97IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
98
99ASN1_SEQUENCE(NETSCAPE_PKEY) = {
100 ASN1_SIMPLE(NETSCAPE_PKEY, version, LONG),
101 ASN1_SIMPLE(NETSCAPE_PKEY, algor, X509_ALGOR),
102 ASN1_SIMPLE(NETSCAPE_PKEY, private_key, ASN1_OCTET_STRING)
103} ASN1_SEQUENCE_END(NETSCAPE_PKEY)
104
105DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
106DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_PKEY,NETSCAPE_PKEY)
107IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
108
109static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
110 int (*cb)(char *buf, int len, const char *prompt,
111 int verify),
112 int sgckey);
113
114int i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
115 int (*cb)(char *buf, int len, const char *prompt,
116 int verify))
117{
118 return i2d_RSA_NET(a, pp, cb, 0);
119}
120
121int i2d_RSA_NET(const RSA *a, unsigned char **pp,
122 int (*cb)(char *buf, int len, const char *prompt, int verify),
123 int sgckey)
124 {
125 int i, j, ret = 0;
126 int rsalen, pkeylen, olen;
127 NETSCAPE_PKEY *pkey = NULL;
128 NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
129 unsigned char buf[256],*zz;
130 unsigned char key[EVP_MAX_KEY_LENGTH];
131 EVP_CIPHER_CTX ctx;
132
133 if (a == NULL) return(0);
134
135 if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
136 if ((enckey=NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) goto err;
137 pkey->version = 0;
138
139 pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
140 if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
141 pkey->algor->parameter->type=V_ASN1_NULL;
142
143 rsalen = i2d_RSAPrivateKey(a, NULL);
144
145 /* Fake some octet strings just for the initial length
146 * calculation.
147 */
148
149 pkey->private_key->length=rsalen;
150
151 pkeylen=i2d_NETSCAPE_PKEY(pkey,NULL);
152
153 enckey->enckey->digest->length = pkeylen;
154
155 enckey->os->length = 11; /* "private-key" */
156
157 enckey->enckey->algor->algorithm=OBJ_nid2obj(NID_rc4);
158 if ((enckey->enckey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
159 enckey->enckey->algor->parameter->type=V_ASN1_NULL;
160
161 if (pp == NULL)
162 {
163 olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL);
164 NETSCAPE_PKEY_free(pkey);
165 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
166 return olen;
167 }
168
169
170 /* Since its RC4 encrypted length is actual length */
171 if ((zz=(unsigned char *)OPENSSL_malloc(rsalen)) == NULL)
172 {
173 ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
174 goto err;
175 }
176
177 pkey->private_key->data = zz;
178 /* Write out private key encoding */
179 i2d_RSAPrivateKey(a,&zz);
180
181 if ((zz=OPENSSL_malloc(pkeylen)) == NULL)
182 {
183 ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
184 goto err;
185 }
186
187 if (!ASN1_STRING_set(enckey->os, "private-key", -1))
188 {
189 ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
190 goto err;
191 }
192 enckey->enckey->digest->data = zz;
193 i2d_NETSCAPE_PKEY(pkey,&zz);
194
195 /* Wipe the private key encoding */
196 OPENSSL_cleanse(pkey->private_key->data, rsalen);
197
198 if (cb == NULL)
199 cb=EVP_read_pw_string;
200 i=cb((char *)buf,256,"Enter Private Key password:",1);
201 if (i != 0)
202 {
203 ASN1err(ASN1_F_I2D_RSA_NET,ASN1_R_BAD_PASSWORD_READ);
204 goto err;
205 }
206 i = strlen((char *)buf);
207 /* If the key is used for SGC the algorithm is modified a little. */
208 if(sgckey) {
209 EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL);
210 memcpy(buf + 16, "SGCKEYSALT", 10);
211 i = 26;
212 }
213
214 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
215 OPENSSL_cleanse(buf,256);
216
217 /* Encrypt private key in place */
218 zz = enckey->enckey->digest->data;
219 EVP_CIPHER_CTX_init(&ctx);
220 EVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL);
221 EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen);
222 EVP_EncryptFinal_ex(&ctx,zz + i,&j);
223 EVP_CIPHER_CTX_cleanup(&ctx);
224
225 ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);
226err:
227 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
228 NETSCAPE_PKEY_free(pkey);
229 return(ret);
230 }
231
232
233RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
234 int (*cb)(char *buf, int len, const char *prompt,
235 int verify))
236{
237 return d2i_RSA_NET(a, pp, length, cb, 0);
238}
239
240RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
241 int (*cb)(char *buf, int len, const char *prompt, int verify),
242 int sgckey)
243 {
244 RSA *ret=NULL;
245 const unsigned char *p;
246 NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
247
248 p = *pp;
249
250 enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length);
251 if(!enckey) {
252 ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_DECODING_ERROR);
253 return NULL;
254 }
255
256 if ((enckey->os->length != 11) || (strncmp("private-key",
257 (char *)enckey->os->data,11) != 0))
258 {
259 ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
260 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
261 return NULL;
262 }
263 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4)
264 {
265 ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
266 goto err;
267 }
268 if (cb == NULL)
269 cb=EVP_read_pw_string;
270 if ((ret=d2i_RSA_NET_2(a, enckey->enckey->digest,cb, sgckey)) == NULL) goto err;
271
272 *pp = p;
273
274 err:
275 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
276 return ret;
277
278 }
279
280static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
281 int (*cb)(char *buf, int len, const char *prompt,
282 int verify), int sgckey)
283 {
284 NETSCAPE_PKEY *pkey=NULL;
285 RSA *ret=NULL;
286 int i,j;
287 unsigned char buf[256];
288 const unsigned char *zz;
289 unsigned char key[EVP_MAX_KEY_LENGTH];
290 EVP_CIPHER_CTX ctx;
291
292 i=cb((char *)buf,256,"Enter Private Key password:",0);
293 if (i != 0)
294 {
295 ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_BAD_PASSWORD_READ);
296 goto err;
297 }
298
299 i = strlen((char *)buf);
300 if(sgckey){
301 EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL);
302 memcpy(buf + 16, "SGCKEYSALT", 10);
303 i = 26;
304 }
305
306 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
307 OPENSSL_cleanse(buf,256);
308
309 EVP_CIPHER_CTX_init(&ctx);
310 EVP_DecryptInit_ex(&ctx,EVP_rc4(),NULL, key,NULL);
311 EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
312 EVP_DecryptFinal_ex(&ctx,&(os->data[i]),&j);
313 EVP_CIPHER_CTX_cleanup(&ctx);
314 os->length=i+j;
315
316 zz=os->data;
317
318 if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
319 {
320 ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
321 goto err;
322 }
323
324 zz=pkey->private_key->data;
325 if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
326 {
327 ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
328 goto err;
329 }
330err:
331 NETSCAPE_PKEY_free(pkey);
332 return(ret);
333 }
334
335#endif /* OPENSSL_NO_RC4 */
336
337#else /* !OPENSSL_NO_RSA */
338
339# if PEDANTIC
340static void *dummy=&dummy;
341# endif
342
343#endif
diff --git a/src/lib/libcrypto/asn1/nsseq.c b/src/lib/libcrypto/asn1/nsseq.c
deleted file mode 100644
index b8c4202230..0000000000
--- a/src/lib/libcrypto/asn1/nsseq.c
+++ /dev/null
@@ -1,83 +0,0 @@
1/* nsseq.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <openssl/asn1t.h>
62#include <openssl/x509.h>
63#include <openssl/objects.h>
64
65static int nsseq_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
66 void *exarg)
67{
68 if(operation == ASN1_OP_NEW_POST) {
69 NETSCAPE_CERT_SEQUENCE *nsseq;
70 nsseq = (NETSCAPE_CERT_SEQUENCE *)*pval;
71 nsseq->type = OBJ_nid2obj(NID_netscape_cert_sequence);
72 }
73 return 1;
74}
75
76/* Netscape certificate sequence structure */
77
78ASN1_SEQUENCE_cb(NETSCAPE_CERT_SEQUENCE, nsseq_cb) = {
79 ASN1_SIMPLE(NETSCAPE_CERT_SEQUENCE, type, ASN1_OBJECT),
80 ASN1_EXP_SEQUENCE_OF_OPT(NETSCAPE_CERT_SEQUENCE, certs, X509, 0)
81} ASN1_SEQUENCE_END_cb(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
82
83IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_CERT_SEQUENCE)
diff --git a/src/lib/libcrypto/asn1/p5_pbe.c b/src/lib/libcrypto/asn1/p5_pbe.c
deleted file mode 100644
index 94bc38b99f..0000000000
--- a/src/lib/libcrypto/asn1/p5_pbe.c
+++ /dev/null
@@ -1,148 +0,0 @@
1/* p5_pbe.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1t.h>
62#include <openssl/x509.h>
63#include <openssl/rand.h>
64
65/* PKCS#5 password based encryption structure */
66
67ASN1_SEQUENCE(PBEPARAM) = {
68 ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
69 ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
70} ASN1_SEQUENCE_END(PBEPARAM)
71
72IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
73
74
75/* Set an algorithm identifier for a PKCS#5 PBE algorithm */
76
77int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
78 const unsigned char *salt, int saltlen)
79 {
80 PBEPARAM *pbe=NULL;
81 ASN1_STRING *pbe_str=NULL;
82 unsigned char *sstr;
83
84 pbe = PBEPARAM_new();
85 if (!pbe)
86 {
87 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE);
88 goto err;
89 }
90 if(iter <= 0)
91 iter = PKCS5_DEFAULT_ITER;
92 if (!ASN1_INTEGER_set(pbe->iter, iter))
93 {
94 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE);
95 goto err;
96 }
97 if (!saltlen)
98 saltlen = PKCS5_SALT_LEN;
99 if (!ASN1_STRING_set(pbe->salt, NULL, saltlen))
100 {
101 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE);
102 goto err;
103 }
104 sstr = ASN1_STRING_data(pbe->salt);
105 if (salt)
106 memcpy(sstr, salt, saltlen);
107 else if (RAND_pseudo_bytes(sstr, saltlen) < 0)
108 goto err;
109
110 if(!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str))
111 {
112 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE);
113 goto err;
114 }
115
116 PBEPARAM_free(pbe);
117 pbe = NULL;
118
119 if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
120 return 1;
121
122err:
123 if (pbe != NULL)
124 PBEPARAM_free(pbe);
125 if (pbe_str != NULL)
126 ASN1_STRING_free(pbe_str);
127 return 0;
128 }
129
130/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
131
132X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
133 const unsigned char *salt, int saltlen)
134 {
135 X509_ALGOR *ret;
136 ret = X509_ALGOR_new();
137 if (!ret)
138 {
139 ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE);
140 return NULL;
141 }
142
143 if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen))
144 return ret;
145
146 X509_ALGOR_free(ret);
147 return NULL;
148 }
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c
deleted file mode 100644
index cb49b6651d..0000000000
--- a/src/lib/libcrypto/asn1/p5_pbev2.c
+++ /dev/null
@@ -1,235 +0,0 @@
1/* p5_pbev2.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999-2004.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1t.h>
62#include <openssl/x509.h>
63#include <openssl/rand.h>
64
65/* PKCS#5 v2.0 password based encryption structures */
66
67ASN1_SEQUENCE(PBE2PARAM) = {
68 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
69 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
70} ASN1_SEQUENCE_END(PBE2PARAM)
71
72IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
73
74ASN1_SEQUENCE(PBKDF2PARAM) = {
75 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
76 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
77 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
78 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
79} ASN1_SEQUENCE_END(PBKDF2PARAM)
80
81IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
82
83/* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
84 * yes I know this is horrible!
85 *
86 * Extended version to allow application supplied PRF NID and IV.
87 */
88
89X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
90 unsigned char *salt, int saltlen,
91 unsigned char *aiv, int prf_nid)
92{
93 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
94 int alg_nid;
95 EVP_CIPHER_CTX ctx;
96 unsigned char iv[EVP_MAX_IV_LENGTH];
97 PBKDF2PARAM *kdf = NULL;
98 PBE2PARAM *pbe2 = NULL;
99 ASN1_OCTET_STRING *osalt = NULL;
100 ASN1_OBJECT *obj;
101
102 alg_nid = EVP_CIPHER_type(cipher);
103 if(alg_nid == NID_undef) {
104 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
105 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
106 goto err;
107 }
108 obj = OBJ_nid2obj(alg_nid);
109
110 if(!(pbe2 = PBE2PARAM_new())) goto merr;
111
112 /* Setup the AlgorithmIdentifier for the encryption scheme */
113 scheme = pbe2->encryption;
114
115 scheme->algorithm = obj;
116 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr;
117
118 /* Create random IV */
119 if (EVP_CIPHER_iv_length(cipher))
120 {
121 if (aiv)
122 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
123 else if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
124 goto err;
125 }
126
127 EVP_CIPHER_CTX_init(&ctx);
128
129 /* Dummy cipherinit to just setup the IV, and PRF */
130 EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0);
131 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
132 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
133 ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
134 EVP_CIPHER_CTX_cleanup(&ctx);
135 goto err;
136 }
137 /* If prf NID unspecified see if cipher has a preference.
138 * An error is OK here: just means use default PRF.
139 */
140 if ((prf_nid == -1) &&
141 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0)
142 {
143 ERR_clear_error();
144 prf_nid = NID_hmacWithSHA1;
145 }
146 EVP_CIPHER_CTX_cleanup(&ctx);
147
148 if(!(kdf = PBKDF2PARAM_new())) goto merr;
149 if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr;
150
151 if (!saltlen) saltlen = PKCS5_SALT_LEN;
152 if (!(osalt->data = OPENSSL_malloc (saltlen))) goto merr;
153 osalt->length = saltlen;
154 if (salt) memcpy (osalt->data, salt, saltlen);
155 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) goto merr;
156
157 if(iter <= 0) iter = PKCS5_DEFAULT_ITER;
158 if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr;
159
160 /* Now include salt in kdf structure */
161 kdf->salt->value.octet_string = osalt;
162 kdf->salt->type = V_ASN1_OCTET_STRING;
163 osalt = NULL;
164
165 /* If its RC2 then we'd better setup the key length */
166
167 if(alg_nid == NID_rc2_cbc) {
168 if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr;
169 if(!ASN1_INTEGER_set (kdf->keylength,
170 EVP_CIPHER_key_length(cipher))) goto merr;
171 }
172
173 /* prf can stay NULL if we are using hmacWithSHA1 */
174 if (prf_nid != NID_hmacWithSHA1)
175 {
176 kdf->prf = X509_ALGOR_new();
177 if (!kdf->prf)
178 goto merr;
179 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
180 V_ASN1_NULL, NULL);
181 }
182
183 /* Now setup the PBE2PARAM keyfunc structure */
184
185 pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
186
187 /* Encode PBKDF2PARAM into parameter of pbe2 */
188
189 if(!(pbe2->keyfunc->parameter = ASN1_TYPE_new())) goto merr;
190
191 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
192 &pbe2->keyfunc->parameter->value.sequence)) goto merr;
193 pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE;
194
195 PBKDF2PARAM_free(kdf);
196 kdf = NULL;
197
198 /* Now set up top level AlgorithmIdentifier */
199
200 if(!(ret = X509_ALGOR_new())) goto merr;
201 if(!(ret->parameter = ASN1_TYPE_new())) goto merr;
202
203 ret->algorithm = OBJ_nid2obj(NID_pbes2);
204
205 /* Encode PBE2PARAM into parameter */
206
207 if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
208 &ret->parameter->value.sequence)) goto merr;
209 ret->parameter->type = V_ASN1_SEQUENCE;
210
211 PBE2PARAM_free(pbe2);
212 pbe2 = NULL;
213
214 return ret;
215
216 merr:
217 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,ERR_R_MALLOC_FAILURE);
218
219 err:
220 PBE2PARAM_free(pbe2);
221 /* Note 'scheme' is freed as part of pbe2 */
222 M_ASN1_OCTET_STRING_free(osalt);
223 PBKDF2PARAM_free(kdf);
224 X509_ALGOR_free(kalg);
225 X509_ALGOR_free(ret);
226
227 return NULL;
228
229}
230
231X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
232 unsigned char *salt, int saltlen)
233 {
234 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
235 }
diff --git a/src/lib/libcrypto/asn1/p8_pkey.c b/src/lib/libcrypto/asn1/p8_pkey.c
deleted file mode 100644
index 17b68d386d..0000000000
--- a/src/lib/libcrypto/asn1/p8_pkey.c
+++ /dev/null
@@ -1,155 +0,0 @@
1/* p8_pkey.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1t.h>
62#include <openssl/x509.h>
63
64/* Minor tweak to operation: zero private key data */
65static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
66 void *exarg)
67{
68 /* Since the structure must still be valid use ASN1_OP_FREE_PRE */
69 if(operation == ASN1_OP_FREE_PRE) {
70 PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
71 if (key->pkey->value.octet_string)
72 OPENSSL_cleanse(key->pkey->value.octet_string->data,
73 key->pkey->value.octet_string->length);
74 }
75 return 1;
76}
77
78ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
79 ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
80 ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
81 ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_ANY),
82 ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0)
83} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
84
85IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
86
87int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
88 int version,
89 int ptype, void *pval,
90 unsigned char *penc, int penclen)
91 {
92 unsigned char **ppenc = NULL;
93 if (version >= 0)
94 {
95 if (!ASN1_INTEGER_set(priv->version, version))
96 return 0;
97 }
98 if (penc)
99 {
100 int pmtype;
101 ASN1_OCTET_STRING *oct;
102 oct = ASN1_OCTET_STRING_new();
103 if (!oct)
104 return 0;
105 oct->data = penc;
106 ppenc = &oct->data;
107 oct->length = penclen;
108 if (priv->broken == PKCS8_NO_OCTET)
109 pmtype = V_ASN1_SEQUENCE;
110 else
111 pmtype = V_ASN1_OCTET_STRING;
112 ASN1_TYPE_set(priv->pkey, pmtype, oct);
113 }
114 if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
115 {
116 /* If call fails do not swallow 'enc' */
117 if (ppenc)
118 *ppenc = NULL;
119 return 0;
120 }
121 return 1;
122 }
123
124int PKCS8_pkey_get0(ASN1_OBJECT **ppkalg,
125 const unsigned char **pk, int *ppklen,
126 X509_ALGOR **pa,
127 PKCS8_PRIV_KEY_INFO *p8)
128 {
129 if (ppkalg)
130 *ppkalg = p8->pkeyalg->algorithm;
131 if(p8->pkey->type == V_ASN1_OCTET_STRING)
132 {
133 p8->broken = PKCS8_OK;
134 if (pk)
135 {
136 *pk = p8->pkey->value.octet_string->data;
137 *ppklen = p8->pkey->value.octet_string->length;
138 }
139 }
140 else if (p8->pkey->type == V_ASN1_SEQUENCE)
141 {
142 p8->broken = PKCS8_NO_OCTET;
143 if (pk)
144 {
145 *pk = p8->pkey->value.sequence->data;
146 *ppklen = p8->pkey->value.sequence->length;
147 }
148 }
149 else
150 return 0;
151 if (pa)
152 *pa = p8->pkeyalg;
153 return 1;
154 }
155
diff --git a/src/lib/libcrypto/asn1/t_bitst.c b/src/lib/libcrypto/asn1/t_bitst.c
deleted file mode 100644
index 2e59a25fa1..0000000000
--- a/src/lib/libcrypto/asn1/t_bitst.c
+++ /dev/null
@@ -1,102 +0,0 @@
1/* t_bitst.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/conf.h>
62#include <openssl/x509v3.h>
63
64int ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs,
65 BIT_STRING_BITNAME *tbl, int indent)
66{
67 BIT_STRING_BITNAME *bnam;
68 char first = 1;
69 BIO_printf(out, "%*s", indent, "");
70 for(bnam = tbl; bnam->lname; bnam++) {
71 if(ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) {
72 if(!first) BIO_puts(out, ", ");
73 BIO_puts(out, bnam->lname);
74 first = 0;
75 }
76 }
77 BIO_puts(out, "\n");
78 return 1;
79}
80
81int ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, char *name, int value,
82 BIT_STRING_BITNAME *tbl)
83{
84 int bitnum;
85 bitnum = ASN1_BIT_STRING_num_asc(name, tbl);
86 if(bitnum < 0) return 0;
87 if(bs) {
88 if(!ASN1_BIT_STRING_set_bit(bs, bitnum, value))
89 return 0;
90 }
91 return 1;
92}
93
94int ASN1_BIT_STRING_num_asc(char *name, BIT_STRING_BITNAME *tbl)
95{
96 BIT_STRING_BITNAME *bnam;
97 for(bnam = tbl; bnam->lname; bnam++) {
98 if(!strcmp(bnam->sname, name) ||
99 !strcmp(bnam->lname, name) ) return bnam->bitnum;
100 }
101 return -1;
102}
diff --git a/src/lib/libcrypto/asn1/t_crl.c b/src/lib/libcrypto/asn1/t_crl.c
deleted file mode 100644
index ee5a687ce8..0000000000
--- a/src/lib/libcrypto/asn1/t_crl.c
+++ /dev/null
@@ -1,133 +0,0 @@
1/* t_crl.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/buffer.h>
62#include <openssl/bn.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65#include <openssl/x509v3.h>
66
67#ifndef OPENSSL_NO_FP_API
68int X509_CRL_print_fp(FILE *fp, X509_CRL *x)
69 {
70 BIO *b;
71 int ret;
72
73 if ((b=BIO_new(BIO_s_file())) == NULL)
74 {
75 X509err(X509_F_X509_CRL_PRINT_FP,ERR_R_BUF_LIB);
76 return(0);
77 }
78 BIO_set_fp(b,fp,BIO_NOCLOSE);
79 ret=X509_CRL_print(b, x);
80 BIO_free(b);
81 return(ret);
82 }
83#endif
84
85int X509_CRL_print(BIO *out, X509_CRL *x)
86{
87 STACK_OF(X509_REVOKED) *rev;
88 X509_REVOKED *r;
89 long l;
90 int i;
91 char *p;
92
93 BIO_printf(out, "Certificate Revocation List (CRL):\n");
94 l = X509_CRL_get_version(x);
95 BIO_printf(out, "%8sVersion %lu (0x%lx)\n", "", l+1, l);
96 i = OBJ_obj2nid(x->sig_alg->algorithm);
97 BIO_printf(out, "%8sSignature Algorithm: %s\n", "",
98 (i == NID_undef) ? "NONE" : OBJ_nid2ln(i));
99 p=X509_NAME_oneline(X509_CRL_get_issuer(x),NULL,0);
100 BIO_printf(out,"%8sIssuer: %s\n","",p);
101 OPENSSL_free(p);
102 BIO_printf(out,"%8sLast Update: ","");
103 ASN1_TIME_print(out,X509_CRL_get_lastUpdate(x));
104 BIO_printf(out,"\n%8sNext Update: ","");
105 if (X509_CRL_get_nextUpdate(x))
106 ASN1_TIME_print(out,X509_CRL_get_nextUpdate(x));
107 else BIO_printf(out,"NONE");
108 BIO_printf(out,"\n");
109
110 X509V3_extensions_print(out, "CRL extensions",
111 x->crl->extensions, 0, 8);
112
113 rev = X509_CRL_get_REVOKED(x);
114
115 if(sk_X509_REVOKED_num(rev) > 0)
116 BIO_printf(out, "Revoked Certificates:\n");
117 else BIO_printf(out, "No Revoked Certificates.\n");
118
119 for(i = 0; i < sk_X509_REVOKED_num(rev); i++) {
120 r = sk_X509_REVOKED_value(rev, i);
121 BIO_printf(out," Serial Number: ");
122 i2a_ASN1_INTEGER(out,r->serialNumber);
123 BIO_printf(out,"\n Revocation Date: ");
124 ASN1_TIME_print(out,r->revocationDate);
125 BIO_printf(out,"\n");
126 X509V3_extensions_print(out, "CRL entry extensions",
127 r->extensions, 0, 8);
128 }
129 X509_signature_print(out, x->sig_alg, x->signature);
130
131 return 1;
132
133}
diff --git a/src/lib/libcrypto/asn1/t_pkey.c b/src/lib/libcrypto/asn1/t_pkey.c
deleted file mode 100644
index 9dd18f6579..0000000000
--- a/src/lib/libcrypto/asn1/t_pkey.c
+++ /dev/null
@@ -1,114 +0,0 @@
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 <openssl/objects.h>
62#include <openssl/buffer.h>
63#include <openssl/bn.h>
64
65int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
66 unsigned char *buf, int off)
67 {
68 int n,i;
69 const char *neg;
70
71 if (num == NULL) return(1);
72 neg = (BN_is_negative(num))?"-":"";
73 if(!BIO_indent(bp,off,128))
74 return 0;
75 if (BN_is_zero(num))
76 {
77 if (BIO_printf(bp, "%s 0\n", number) <= 0)
78 return 0;
79 return 1;
80 }
81
82 if (BN_num_bytes(num) <= BN_BYTES)
83 {
84 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
85 (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
86 <= 0) return(0);
87 }
88 else
89 {
90 buf[0]=0;
91 if (BIO_printf(bp,"%s%s",number,
92 (neg[0] == '-')?" (Negative)":"") <= 0)
93 return(0);
94 n=BN_bn2bin(num,&buf[1]);
95
96 if (buf[1] & 0x80)
97 n++;
98 else buf++;
99
100 for (i=0; i<n; i++)
101 {
102 if ((i%15) == 0)
103 {
104 if(BIO_puts(bp,"\n") <= 0
105 || !BIO_indent(bp,off+4,128))
106 return 0;
107 }
108 if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
109 <= 0) return(0);
110 }
111 if (BIO_write(bp,"\n",1) <= 0) return(0);
112 }
113 return(1);
114 }
diff --git a/src/lib/libcrypto/asn1/t_req.c b/src/lib/libcrypto/asn1/t_req.c
deleted file mode 100644
index ea1794e3e0..0000000000
--- a/src/lib/libcrypto/asn1/t_req.c
+++ /dev/null
@@ -1,266 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/bn.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65#include <openssl/x509v3.h>
66#ifndef OPENSSL_NO_RSA
67#include <openssl/rsa.h>
68#endif
69#ifndef OPENSSL_NO_DSA
70#include <openssl/dsa.h>
71#endif
72
73#ifndef OPENSSL_NO_FP_API
74int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
75 {
76 BIO *b;
77 int ret;
78
79 if ((b=BIO_new(BIO_s_file())) == NULL)
80 {
81 X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
82 return(0);
83 }
84 BIO_set_fp(b,fp,BIO_NOCLOSE);
85 ret=X509_REQ_print(b, x);
86 BIO_free(b);
87 return(ret);
88 }
89#endif
90
91int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags, unsigned long cflag)
92 {
93 unsigned long l;
94 int i;
95 const char *neg;
96 X509_REQ_INFO *ri;
97 EVP_PKEY *pkey;
98 STACK_OF(X509_ATTRIBUTE) *sk;
99 STACK_OF(X509_EXTENSION) *exts;
100 char mlch = ' ';
101 int nmindent = 0;
102
103 if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
104 mlch = '\n';
105 nmindent = 12;
106 }
107
108 if(nmflags == X509_FLAG_COMPAT)
109 nmindent = 16;
110
111
112 ri=x->req_info;
113 if(!(cflag & X509_FLAG_NO_HEADER))
114 {
115 if (BIO_write(bp,"Certificate Request:\n",21) <= 0) goto err;
116 if (BIO_write(bp," Data:\n",10) <= 0) goto err;
117 }
118 if(!(cflag & X509_FLAG_NO_VERSION))
119 {
120 neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
121 l=0;
122 for (i=0; i<ri->version->length; i++)
123 { l<<=8; l+=ri->version->data[i]; }
124 if(BIO_printf(bp,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,
125 l) <= 0)
126 goto err;
127 }
128 if(!(cflag & X509_FLAG_NO_SUBJECT))
129 {
130 if (BIO_printf(bp," Subject:%c",mlch) <= 0) goto err;
131 if (X509_NAME_print_ex(bp,ri->subject,nmindent, nmflags) < 0) goto err;
132 if (BIO_write(bp,"\n",1) <= 0) goto err;
133 }
134 if(!(cflag & X509_FLAG_NO_PUBKEY))
135 {
136 if (BIO_write(bp," Subject Public Key Info:\n",33) <= 0)
137 goto err;
138 if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
139 goto err;
140 if (i2a_ASN1_OBJECT(bp, ri->pubkey->algor->algorithm) <= 0)
141 goto err;
142 if (BIO_puts(bp, "\n") <= 0)
143 goto err;
144
145 pkey=X509_REQ_get_pubkey(x);
146 if (pkey == NULL)
147 {
148 BIO_printf(bp,"%12sUnable to load Public Key\n","");
149 ERR_print_errors(bp);
150 }
151 else
152 {
153 EVP_PKEY_print_public(bp, pkey, 16, NULL);
154 EVP_PKEY_free(pkey);
155 }
156 }
157
158 if(!(cflag & X509_FLAG_NO_ATTRIBUTES))
159 {
160 /* may not be */
161 if(BIO_printf(bp,"%8sAttributes:\n","") <= 0)
162 goto err;
163
164 sk=x->req_info->attributes;
165 if (sk_X509_ATTRIBUTE_num(sk) == 0)
166 {
167 if(BIO_printf(bp,"%12sa0:00\n","") <= 0)
168 goto err;
169 }
170 else
171 {
172 for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
173 {
174 ASN1_TYPE *at;
175 X509_ATTRIBUTE *a;
176 ASN1_BIT_STRING *bs=NULL;
177 ASN1_TYPE *t;
178 int j,type=0,count=1,ii=0;
179
180 a=sk_X509_ATTRIBUTE_value(sk,i);
181 if(X509_REQ_extension_nid(OBJ_obj2nid(a->object)))
182 continue;
183 if(BIO_printf(bp,"%12s","") <= 0)
184 goto err;
185 if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
186 {
187 if (a->single)
188 {
189 t=a->value.single;
190 type=t->type;
191 bs=t->value.bit_string;
192 }
193 else
194 {
195 ii=0;
196 count=sk_ASN1_TYPE_num(a->value.set);
197get_next:
198 at=sk_ASN1_TYPE_value(a->value.set,ii);
199 type=at->type;
200 bs=at->value.asn1_string;
201 }
202 }
203 for (j=25-j; j>0; j--)
204 if (BIO_write(bp," ",1) != 1) goto err;
205 if (BIO_puts(bp,":") <= 0) goto err;
206 if ( (type == V_ASN1_PRINTABLESTRING) ||
207 (type == V_ASN1_T61STRING) ||
208 (type == V_ASN1_IA5STRING))
209 {
210 if (BIO_write(bp,(char *)bs->data,bs->length)
211 != bs->length)
212 goto err;
213 BIO_puts(bp,"\n");
214 }
215 else
216 {
217 BIO_puts(bp,"unable to print attribute\n");
218 }
219 if (++ii < count) goto get_next;
220 }
221 }
222 }
223 if(!(cflag & X509_FLAG_NO_EXTENSIONS))
224 {
225 exts = X509_REQ_get_extensions(x);
226 if(exts)
227 {
228 BIO_printf(bp,"%8sRequested Extensions:\n","");
229 for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
230 {
231 ASN1_OBJECT *obj;
232 X509_EXTENSION *ex;
233 int j;
234 ex=sk_X509_EXTENSION_value(exts, i);
235 if (BIO_printf(bp,"%12s","") <= 0) goto err;
236 obj=X509_EXTENSION_get_object(ex);
237 i2a_ASN1_OBJECT(bp,obj);
238 j=X509_EXTENSION_get_critical(ex);
239 if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
240 goto err;
241 if(!X509V3_EXT_print(bp, ex, cflag, 16))
242 {
243 BIO_printf(bp, "%16s", "");
244 M_ASN1_OCTET_STRING_print(bp,ex->value);
245 }
246 if (BIO_write(bp,"\n",1) <= 0) goto err;
247 }
248 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
249 }
250 }
251
252 if(!(cflag & X509_FLAG_NO_SIGDUMP))
253 {
254 if(!X509_signature_print(bp, x->sig_alg, x->signature)) goto err;
255 }
256
257 return(1);
258err:
259 X509err(X509_F_X509_REQ_PRINT_EX,ERR_R_BUF_LIB);
260 return(0);
261 }
262
263int X509_REQ_print(BIO *bp, X509_REQ *x)
264 {
265 return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
266 }
diff --git a/src/lib/libcrypto/asn1/t_spki.c b/src/lib/libcrypto/asn1/t_spki.c
deleted file mode 100644
index 079c081a81..0000000000
--- a/src/lib/libcrypto/asn1/t_spki.c
+++ /dev/null
@@ -1,107 +0,0 @@
1/* t_spki.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/x509.h>
62#include <openssl/asn1.h>
63#ifndef OPENSSL_NO_RSA
64#include <openssl/rsa.h>
65#endif
66#ifndef OPENSSL_NO_DSA
67#include <openssl/dsa.h>
68#endif
69#include <openssl/bn.h>
70
71/* Print out an SPKI */
72
73int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki)
74{
75 EVP_PKEY *pkey;
76 ASN1_IA5STRING *chal;
77 int i, n;
78 char *s;
79 BIO_printf(out, "Netscape SPKI:\n");
80 i=OBJ_obj2nid(spki->spkac->pubkey->algor->algorithm);
81 BIO_printf(out," Public Key Algorithm: %s\n",
82 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
83 pkey = X509_PUBKEY_get(spki->spkac->pubkey);
84 if(!pkey) BIO_printf(out, " Unable to load public key\n");
85 else
86 {
87 EVP_PKEY_print_public(out, pkey, 4, NULL);
88 EVP_PKEY_free(pkey);
89 }
90 chal = spki->spkac->challenge;
91 if(chal->length)
92 BIO_printf(out, " Challenge String: %s\n", chal->data);
93 i=OBJ_obj2nid(spki->sig_algor->algorithm);
94 BIO_printf(out," Signature Algorithm: %s",
95 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
96
97 n=spki->signature->length;
98 s=(char *)spki->signature->data;
99 for (i=0; i<n; i++)
100 {
101 if ((i%18) == 0) BIO_write(out,"\n ",7);
102 BIO_printf(out,"%02x%s",(unsigned char)s[i],
103 ((i+1) == n)?"":":");
104 }
105 BIO_write(out,"\n",1);
106 return 1;
107}
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
deleted file mode 100644
index e061f2ffad..0000000000
--- a/src/lib/libcrypto/asn1/t_x509.c
+++ /dev/null
@@ -1,493 +0,0 @@
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 <openssl/buffer.h>
62#include <openssl/bn.h>
63#ifndef OPENSSL_NO_RSA
64#include <openssl/rsa.h>
65#endif
66#ifndef OPENSSL_NO_DSA
67#include <openssl/dsa.h>
68#endif
69#ifndef OPENSSL_NO_EC
70#include <openssl/ec.h>
71#endif
72#include <openssl/objects.h>
73#include <openssl/x509.h>
74#include <openssl/x509v3.h>
75
76#ifndef OPENSSL_NO_FP_API
77int X509_print_fp(FILE *fp, X509 *x)
78 {
79 return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
80 }
81
82int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
83 {
84 BIO *b;
85 int ret;
86
87 if ((b=BIO_new(BIO_s_file())) == NULL)
88 {
89 X509err(X509_F_X509_PRINT_EX_FP,ERR_R_BUF_LIB);
90 return(0);
91 }
92 BIO_set_fp(b,fp,BIO_NOCLOSE);
93 ret=X509_print_ex(b, x, nmflag, cflag);
94 BIO_free(b);
95 return(ret);
96 }
97#endif
98
99int X509_print(BIO *bp, X509 *x)
100{
101 return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
102}
103
104int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
105 {
106 long l;
107 int ret=0,i;
108 char *m=NULL,mlch = ' ';
109 int nmindent = 0;
110 X509_CINF *ci;
111 ASN1_INTEGER *bs;
112 EVP_PKEY *pkey=NULL;
113 const char *neg;
114
115 if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
116 mlch = '\n';
117 nmindent = 12;
118 }
119
120 if(nmflags == X509_FLAG_COMPAT)
121 nmindent = 16;
122
123 ci=x->cert_info;
124 if(!(cflag & X509_FLAG_NO_HEADER))
125 {
126 if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err;
127 if (BIO_write(bp," Data:\n",10) <= 0) goto err;
128 }
129 if(!(cflag & X509_FLAG_NO_VERSION))
130 {
131 l=X509_get_version(x);
132 if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err;
133 }
134 if(!(cflag & X509_FLAG_NO_SERIAL))
135 {
136
137 if (BIO_write(bp," Serial Number:",22) <= 0) goto err;
138
139 bs=X509_get_serialNumber(x);
140 if (bs->length <= 4)
141 {
142 l=ASN1_INTEGER_get(bs);
143 if (l < 0)
144 {
145 l= -l;
146 neg="-";
147 }
148 else
149 neg="";
150 if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
151 goto err;
152 }
153 else
154 {
155 neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
156 if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err;
157
158 for (i=0; i<bs->length; i++)
159 {
160 if (BIO_printf(bp,"%02x%c",bs->data[i],
161 ((i+1 == bs->length)?'\n':':')) <= 0)
162 goto err;
163 }
164 }
165
166 }
167
168 if(!(cflag & X509_FLAG_NO_SIGNAME))
169 {
170 if (BIO_printf(bp,"%8sSignature Algorithm: ","") <= 0)
171 goto err;
172 if (i2a_ASN1_OBJECT(bp, ci->signature->algorithm) <= 0)
173 goto err;
174 if (BIO_puts(bp, "\n") <= 0)
175 goto err;
176 }
177
178 if(!(cflag & X509_FLAG_NO_ISSUER))
179 {
180 if (BIO_printf(bp," Issuer:%c",mlch) <= 0) goto err;
181 if (X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags) < 0) goto err;
182 if (BIO_write(bp,"\n",1) <= 0) goto err;
183 }
184 if(!(cflag & X509_FLAG_NO_VALIDITY))
185 {
186 if (BIO_write(bp," Validity\n",17) <= 0) goto err;
187 if (BIO_write(bp," Not Before: ",24) <= 0) goto err;
188 if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err;
189 if (BIO_write(bp,"\n Not After : ",25) <= 0) goto err;
190 if (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err;
191 if (BIO_write(bp,"\n",1) <= 0) goto err;
192 }
193 if(!(cflag & X509_FLAG_NO_SUBJECT))
194 {
195 if (BIO_printf(bp," Subject:%c",mlch) <= 0) goto err;
196 if (X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags) < 0) goto err;
197 if (BIO_write(bp,"\n",1) <= 0) goto err;
198 }
199 if(!(cflag & X509_FLAG_NO_PUBKEY))
200 {
201 if (BIO_write(bp," Subject Public Key Info:\n",33) <= 0)
202 goto err;
203 if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
204 goto err;
205 if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
206 goto err;
207 if (BIO_puts(bp, "\n") <= 0)
208 goto err;
209
210 pkey=X509_get_pubkey(x);
211 if (pkey == NULL)
212 {
213 BIO_printf(bp,"%12sUnable to load Public Key\n","");
214 ERR_print_errors(bp);
215 }
216 else
217 {
218 EVP_PKEY_print_public(bp, pkey, 16, NULL);
219 EVP_PKEY_free(pkey);
220 }
221 }
222
223 if (!(cflag & X509_FLAG_NO_EXTENSIONS))
224 X509V3_extensions_print(bp, "X509v3 extensions",
225 ci->extensions, cflag, 8);
226
227 if(!(cflag & X509_FLAG_NO_SIGDUMP))
228 {
229 if(X509_signature_print(bp, x->sig_alg, x->signature) <= 0) goto err;
230 }
231 if(!(cflag & X509_FLAG_NO_AUX))
232 {
233 if (!X509_CERT_AUX_print(bp, x->aux, 0)) goto err;
234 }
235 ret=1;
236err:
237 if (m != NULL) OPENSSL_free(m);
238 return(ret);
239 }
240
241int X509_ocspid_print (BIO *bp, X509 *x)
242 {
243 unsigned char *der=NULL ;
244 unsigned char *dertmp;
245 int derlen;
246 int i;
247 unsigned char SHA1md[SHA_DIGEST_LENGTH];
248
249 /* display the hash of the subject as it would appear
250 in OCSP requests */
251 if (BIO_printf(bp," Subject OCSP hash: ") <= 0)
252 goto err;
253 derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
254 if ((der = dertmp = (unsigned char *)OPENSSL_malloc (derlen)) == NULL)
255 goto err;
256 i2d_X509_NAME(x->cert_info->subject, &dertmp);
257
258 EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL);
259 for (i=0; i < SHA_DIGEST_LENGTH; i++)
260 {
261 if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
262 }
263 OPENSSL_free (der);
264 der=NULL;
265
266 /* display the hash of the public key as it would appear
267 in OCSP requests */
268 if (BIO_printf(bp,"\n Public key OCSP hash: ") <= 0)
269 goto err;
270
271 EVP_Digest(x->cert_info->key->public_key->data,
272 x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL);
273 for (i=0; i < SHA_DIGEST_LENGTH; i++)
274 {
275 if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
276 goto err;
277 }
278 BIO_printf(bp,"\n");
279
280 return (1);
281err:
282 if (der != NULL) OPENSSL_free(der);
283 return(0);
284 }
285
286int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
287{
288 unsigned char *s;
289 int i, n;
290 if (BIO_puts(bp," Signature Algorithm: ") <= 0) return 0;
291 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0;
292
293 n=sig->length;
294 s=sig->data;
295 for (i=0; i<n; i++)
296 {
297 if ((i%18) == 0)
298 if (BIO_write(bp,"\n ",9) <= 0) return 0;
299 if (BIO_printf(bp,"%02x%s",s[i],
300 ((i+1) == n)?"":":") <= 0) return 0;
301 }
302 if (BIO_write(bp,"\n",1) != 1) return 0;
303 return 1;
304}
305
306int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
307 {
308 int i,n;
309 char buf[80];
310 const char *p;
311
312 if (v == NULL) return(0);
313 n=0;
314 p=(const char *)v->data;
315 for (i=0; i<v->length; i++)
316 {
317 if ((p[i] > '~') || ((p[i] < ' ') &&
318 (p[i] != '\n') && (p[i] != '\r')))
319 buf[n]='.';
320 else
321 buf[n]=p[i];
322 n++;
323 if (n >= 80)
324 {
325 if (BIO_write(bp,buf,n) <= 0)
326 return(0);
327 n=0;
328 }
329 }
330 if (n > 0)
331 if (BIO_write(bp,buf,n) <= 0)
332 return(0);
333 return(1);
334 }
335
336int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
337{
338 if(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm);
339 if(tm->type == V_ASN1_GENERALIZEDTIME)
340 return ASN1_GENERALIZEDTIME_print(bp, tm);
341 BIO_write(bp,"Bad time value",14);
342 return(0);
343}
344
345static const char *mon[12]=
346 {
347 "Jan","Feb","Mar","Apr","May","Jun",
348 "Jul","Aug","Sep","Oct","Nov","Dec"
349 };
350
351int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
352 {
353 char *v;
354 int gmt=0;
355 int i;
356 int y=0,M=0,d=0,h=0,m=0,s=0;
357 char *f = NULL;
358 int f_len = 0;
359
360 i=tm->length;
361 v=(char *)tm->data;
362
363 if (i < 12) goto err;
364 if (v[i-1] == 'Z') gmt=1;
365 for (i=0; i<12; i++)
366 if ((v[i] > '9') || (v[i] < '0')) goto err;
367 y= (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0');
368 M= (v[4]-'0')*10+(v[5]-'0');
369 if ((M > 12) || (M < 1)) goto err;
370 d= (v[6]-'0')*10+(v[7]-'0');
371 h= (v[8]-'0')*10+(v[9]-'0');
372 m= (v[10]-'0')*10+(v[11]-'0');
373 if (tm->length >= 14 &&
374 (v[12] >= '0') && (v[12] <= '9') &&
375 (v[13] >= '0') && (v[13] <= '9'))
376 {
377 s= (v[12]-'0')*10+(v[13]-'0');
378 /* Check for fractions of seconds. */
379 if (tm->length >= 15 && v[14] == '.')
380 {
381 int l = tm->length;
382 f = &v[14]; /* The decimal point. */
383 f_len = 1;
384 while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
385 ++f_len;
386 }
387 }
388
389 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d%.*s %d%s",
390 mon[M-1],d,h,m,s,f_len,f,y,(gmt)?" GMT":"") <= 0)
391 return(0);
392 else
393 return(1);
394err:
395 BIO_write(bp,"Bad time value",14);
396 return(0);
397 }
398
399int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
400 {
401 const char *v;
402 int gmt=0;
403 int i;
404 int y=0,M=0,d=0,h=0,m=0,s=0;
405
406 i=tm->length;
407 v=(const char *)tm->data;
408
409 if (i < 10) goto err;
410 if (v[i-1] == 'Z') gmt=1;
411 for (i=0; i<10; i++)
412 if ((v[i] > '9') || (v[i] < '0')) goto err;
413 y= (v[0]-'0')*10+(v[1]-'0');
414 if (y < 50) y+=100;
415 M= (v[2]-'0')*10+(v[3]-'0');
416 if ((M > 12) || (M < 1)) goto err;
417 d= (v[4]-'0')*10+(v[5]-'0');
418 h= (v[6]-'0')*10+(v[7]-'0');
419 m= (v[8]-'0')*10+(v[9]-'0');
420 if (tm->length >=12 &&
421 (v[10] >= '0') && (v[10] <= '9') &&
422 (v[11] >= '0') && (v[11] <= '9'))
423 s= (v[10]-'0')*10+(v[11]-'0');
424
425 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
426 mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)
427 return(0);
428 else
429 return(1);
430err:
431 BIO_write(bp,"Bad time value",14);
432 return(0);
433 }
434
435int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
436 {
437 char *s,*c,*b;
438 int ret=0,l,i;
439
440 l=80-2-obase;
441
442 b=X509_NAME_oneline(name,NULL,0);
443 if (!*b)
444 {
445 OPENSSL_free(b);
446 return 1;
447 }
448 s=b+1; /* skip the first slash */
449
450 c=s;
451 for (;;)
452 {
453#ifndef CHARSET_EBCDIC
454 if ( ((*s == '/') &&
455 ((s[1] >= 'A') && (s[1] <= 'Z') && (
456 (s[2] == '=') ||
457 ((s[2] >= 'A') && (s[2] <= 'Z') &&
458 (s[3] == '='))
459 ))) ||
460 (*s == '\0'))
461#else
462 if ( ((*s == '/') &&
463 (isupper(s[1]) && (
464 (s[2] == '=') ||
465 (isupper(s[2]) &&
466 (s[3] == '='))
467 ))) ||
468 (*s == '\0'))
469#endif
470 {
471 i=s-c;
472 if (BIO_write(bp,c,i) != i) goto err;
473 c=s+1; /* skip following slash */
474 if (*s != '\0')
475 {
476 if (BIO_write(bp,", ",2) != 2) goto err;
477 }
478 l--;
479 }
480 if (*s == '\0') break;
481 s++;
482 l--;
483 }
484
485 ret=1;
486 if (0)
487 {
488err:
489 X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
490 }
491 OPENSSL_free(b);
492 return(ret);
493 }
diff --git a/src/lib/libcrypto/asn1/t_x509a.c b/src/lib/libcrypto/asn1/t_x509a.c
deleted file mode 100644
index 8b18801a17..0000000000
--- a/src/lib/libcrypto/asn1/t_x509a.c
+++ /dev/null
@@ -1,110 +0,0 @@
1/* t_x509a.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/evp.h>
62#include <openssl/asn1.h>
63#include <openssl/x509.h>
64
65/* X509_CERT_AUX and string set routines
66 */
67
68int X509_CERT_AUX_print(BIO *out, X509_CERT_AUX *aux, int indent)
69{
70 char oidstr[80], first;
71 int i;
72 if(!aux) return 1;
73 if(aux->trust) {
74 first = 1;
75 BIO_printf(out, "%*sTrusted Uses:\n%*s",
76 indent, "", indent + 2, "");
77 for(i = 0; i < sk_ASN1_OBJECT_num(aux->trust); i++) {
78 if(!first) BIO_puts(out, ", ");
79 else first = 0;
80 OBJ_obj2txt(oidstr, sizeof oidstr,
81 sk_ASN1_OBJECT_value(aux->trust, i), 0);
82 BIO_puts(out, oidstr);
83 }
84 BIO_puts(out, "\n");
85 } else BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
86 if(aux->reject) {
87 first = 1;
88 BIO_printf(out, "%*sRejected Uses:\n%*s",
89 indent, "", indent + 2, "");
90 for(i = 0; i < sk_ASN1_OBJECT_num(aux->reject); i++) {
91 if(!first) BIO_puts(out, ", ");
92 else first = 0;
93 OBJ_obj2txt(oidstr, sizeof oidstr,
94 sk_ASN1_OBJECT_value(aux->reject, i), 0);
95 BIO_puts(out, oidstr);
96 }
97 BIO_puts(out, "\n");
98 } else BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
99 if(aux->alias) BIO_printf(out, "%*sAlias: %s\n", indent, "",
100 aux->alias->data);
101 if(aux->keyid) {
102 BIO_printf(out, "%*sKey Id: ", indent, "");
103 for(i = 0; i < aux->keyid->length; i++)
104 BIO_printf(out, "%s%02X",
105 i ? ":" : "",
106 aux->keyid->data[i]);
107 BIO_write(out,"\n",1);
108 }
109 return 1;
110}
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
deleted file mode 100644
index 87d7dfdf5c..0000000000
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ /dev/null
@@ -1,1347 +0,0 @@
1/* tasn_dec.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <string.h>
62#include <openssl/asn1.h>
63#include <openssl/asn1t.h>
64#include <openssl/objects.h>
65#include <openssl/buffer.h>
66#include <openssl/err.h>
67
68static int asn1_check_eoc(const unsigned char **in, long len);
69static int asn1_find_end(const unsigned char **in, long len, char inf);
70
71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth);
73
74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75
76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 char *inf, char *cst,
78 const unsigned char **in, long len,
79 int exptag, int expclass, char opt,
80 ASN1_TLC *ctx);
81
82static int asn1_template_ex_d2i(ASN1_VALUE **pval,
83 const unsigned char **in, long len,
84 const ASN1_TEMPLATE *tt, char opt,
85 ASN1_TLC *ctx);
86static int asn1_template_noexp_d2i(ASN1_VALUE **val,
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
89 ASN1_TLC *ctx);
90static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
91 const unsigned char **in, long len,
92 const ASN1_ITEM *it,
93 int tag, int aclass, char opt, ASN1_TLC *ctx);
94
95/* Table to convert tags to bit values, used for MSTRING type */
96static const unsigned long tag2bit[32] = {
970, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
98B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
99B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
100B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
101B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
102B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
106 };
107
108unsigned long ASN1_tag2bit(int tag)
109 {
110 if ((tag < 0) || (tag > 30)) return 0;
111 return tag2bit[tag];
112 }
113
114/* Macro to initialize and invalidate the cache */
115
116#define asn1_tlc_clear(c) if (c) (c)->valid = 0
117/* Version to avoid compiler warning about 'c' always non-NULL */
118#define asn1_tlc_clear_nc(c) (c)->valid = 0
119
120/* Decode an ASN1 item, this currently behaves just
121 * like a standard 'd2i' function. 'in' points to
122 * a buffer to read the data from, in future we will
123 * have more advanced versions that can input data
124 * a piece at a time and this will simply be a special
125 * case.
126 */
127
128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
129 const unsigned char **in, long len, const ASN1_ITEM *it)
130 {
131 ASN1_TLC c;
132 ASN1_VALUE *ptmpval = NULL;
133 if (!pval)
134 pval = &ptmpval;
135 asn1_tlc_clear_nc(&c);
136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
137 return *pval;
138 return NULL;
139 }
140
141int ASN1_template_d2i(ASN1_VALUE **pval,
142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
143 {
144 ASN1_TLC c;
145 asn1_tlc_clear_nc(&c);
146 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
147 }
148
149
150/* Decode an item, taking care of IMPLICIT tagging, if any.
151 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
152 */
153
154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
155 const ASN1_ITEM *it,
156 int tag, int aclass, char opt, ASN1_TLC *ctx)
157 {
158 const ASN1_TEMPLATE *tt, *errtt = NULL;
159 const ASN1_COMPAT_FUNCS *cf;
160 const ASN1_EXTERN_FUNCS *ef;
161 const ASN1_AUX *aux = it->funcs;
162 ASN1_aux_cb *asn1_cb;
163 const unsigned char *p = NULL, *q;
164 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
165 unsigned char imphack = 0, oclass;
166 char seq_eoc, seq_nolen, cst, isopt;
167 long tmplen;
168 int i;
169 int otag;
170 int ret = 0;
171 ASN1_VALUE **pchptr, *ptmpval;
172 if (!pval)
173 return 0;
174 if (aux && aux->asn1_cb)
175 asn1_cb = aux->asn1_cb;
176 else asn1_cb = 0;
177
178 switch(it->itype)
179 {
180 case ASN1_ITYPE_PRIMITIVE:
181 if (it->templates)
182 {
183 /* tagging or OPTIONAL is currently illegal on an item
184 * template because the flags can't get passed down.
185 * In practice this isn't a problem: we include the
186 * relevant flags from the item template in the
187 * template itself.
188 */
189 if ((tag != -1) || opt)
190 {
191 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
192 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
193 goto err;
194 }
195 return asn1_template_ex_d2i(pval, in, len,
196 it->templates, opt, ctx);
197 }
198 return asn1_d2i_ex_primitive(pval, in, len, it,
199 tag, aclass, opt, ctx);
200 break;
201
202 case ASN1_ITYPE_MSTRING:
203 p = *in;
204 /* Just read in tag and class */
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx);
207 if (!ret)
208 {
209 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
210 ERR_R_NESTED_ASN1_ERROR);
211 goto err;
212 }
213
214 /* Must be UNIVERSAL class */
215 if (oclass != V_ASN1_UNIVERSAL)
216 {
217 /* If OPTIONAL, assume this is OK */
218 if (opt) return -1;
219 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
220 ASN1_R_MSTRING_NOT_UNIVERSAL);
221 goto err;
222 }
223 /* Check tag matches bit map */
224 if (!(ASN1_tag2bit(otag) & it->utype))
225 {
226 /* If OPTIONAL, assume this is OK */
227 if (opt)
228 return -1;
229 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
230 ASN1_R_MSTRING_WRONG_TAG);
231 goto err;
232 }
233 return asn1_d2i_ex_primitive(pval, in, len,
234 it, otag, 0, 0, ctx);
235
236 case ASN1_ITYPE_EXTERN:
237 /* Use new style d2i */
238 ef = it->funcs;
239 return ef->asn1_ex_d2i(pval, in, len,
240 it, tag, aclass, opt, ctx);
241
242 case ASN1_ITYPE_COMPAT:
243 /* we must resort to old style evil hackery */
244 cf = it->funcs;
245
246 /* If OPTIONAL see if it is there */
247 if (opt)
248 {
249 int exptag;
250 p = *in;
251 if (tag == -1)
252 exptag = it->utype;
253 else exptag = tag;
254 /* Don't care about anything other than presence
255 * of expected tag */
256
257 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
258 &p, len, exptag, aclass, 1, ctx);
259 if (!ret)
260 {
261 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
262 ERR_R_NESTED_ASN1_ERROR);
263 goto err;
264 }
265 if (ret == -1)
266 return -1;
267 }
268
269 /* This is the old style evil hack IMPLICIT handling:
270 * since the underlying code is expecting a tag and
271 * class other than the one present we change the
272 * buffer temporarily then change it back afterwards.
273 * This doesn't and never did work for tags > 30.
274 *
275 * Yes this is *horrible* but it is only needed for
276 * old style d2i which will hopefully not be around
277 * for much longer.
278 * FIXME: should copy the buffer then modify it so
279 * the input buffer can be const: we should *always*
280 * copy because the old style d2i might modify the
281 * buffer.
282 */
283
284 if (tag != -1)
285 {
286 wp = *(unsigned char **)in;
287 imphack = *wp;
288 if (p == NULL)
289 {
290 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
291 ERR_R_NESTED_ASN1_ERROR);
292 goto err;
293 }
294 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
295 | it->utype);
296 }
297
298 ptmpval = cf->asn1_d2i(pval, in, len);
299
300 if (tag != -1)
301 *wp = imphack;
302
303 if (ptmpval)
304 return 1;
305
306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
307 goto err;
308
309
310 case ASN1_ITYPE_CHOICE:
311 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
312 goto auxerr;
313
314 /* Allocate structure */
315 if (!*pval && !ASN1_item_ex_new(pval, it))
316 {
317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
318 ERR_R_NESTED_ASN1_ERROR);
319 goto err;
320 }
321 /* CHOICE type, try each possibility in turn */
322 p = *in;
323 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
324 {
325 pchptr = asn1_get_field_ptr(pval, tt);
326 /* We mark field as OPTIONAL so its absence
327 * can be recognised.
328 */
329 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
330 /* If field not present, try the next one */
331 if (ret == -1)
332 continue;
333 /* If positive return, read OK, break loop */
334 if (ret > 0)
335 break;
336 /* Otherwise must be an ASN1 parsing error */
337 errtt = tt;
338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
339 ERR_R_NESTED_ASN1_ERROR);
340 goto err;
341 }
342
343 /* Did we fall off the end without reading anything? */
344 if (i == it->tcount)
345 {
346 /* If OPTIONAL, this is OK */
347 if (opt)
348 {
349 /* Free and zero it */
350 ASN1_item_ex_free(pval, it);
351 return -1;
352 }
353 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
354 ASN1_R_NO_MATCHING_CHOICE_TYPE);
355 goto err;
356 }
357
358 asn1_set_choice_selector(pval, i, it);
359 *in = p;
360 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
361 goto auxerr;
362 return 1;
363
364 case ASN1_ITYPE_NDEF_SEQUENCE:
365 case ASN1_ITYPE_SEQUENCE:
366 p = *in;
367 tmplen = len;
368
369 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
370 if (tag == -1)
371 {
372 tag = V_ASN1_SEQUENCE;
373 aclass = V_ASN1_UNIVERSAL;
374 }
375 /* Get SEQUENCE length and update len, p */
376 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
377 &p, len, tag, aclass, opt, ctx);
378 if (!ret)
379 {
380 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
381 ERR_R_NESTED_ASN1_ERROR);
382 goto err;
383 }
384 else if (ret == -1)
385 return -1;
386 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
387 {
388 len = tmplen - (p - *in);
389 seq_nolen = 1;
390 }
391 /* If indefinite we don't do a length check */
392 else seq_nolen = seq_eoc;
393 if (!cst)
394 {
395 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
396 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
397 goto err;
398 }
399
400 if (!*pval && !ASN1_item_ex_new(pval, it))
401 {
402 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
403 ERR_R_NESTED_ASN1_ERROR);
404 goto err;
405 }
406
407 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
408 goto auxerr;
409
410 /* Get each field entry */
411 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
412 {
413 const ASN1_TEMPLATE *seqtt;
414 ASN1_VALUE **pseqval;
415 seqtt = asn1_do_adb(pval, tt, 1);
416 if (!seqtt)
417 goto err;
418 pseqval = asn1_get_field_ptr(pval, seqtt);
419 /* Have we ran out of data? */
420 if (!len)
421 break;
422 q = p;
423 if (asn1_check_eoc(&p, len))
424 {
425 if (!seq_eoc)
426 {
427 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
428 ASN1_R_UNEXPECTED_EOC);
429 goto err;
430 }
431 len -= p - q;
432 seq_eoc = 0;
433 q = p;
434 break;
435 }
436 /* This determines the OPTIONAL flag value. The field
437 * cannot be omitted if it is the last of a SEQUENCE
438 * and there is still data to be read. This isn't
439 * strictly necessary but it increases efficiency in
440 * some cases.
441 */
442 if (i == (it->tcount - 1))
443 isopt = 0;
444 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
445 /* attempt to read in field, allowing each to be
446 * OPTIONAL */
447
448 ret = asn1_template_ex_d2i(pseqval, &p, len,
449 seqtt, isopt, ctx);
450 if (!ret)
451 {
452 errtt = seqtt;
453 goto err;
454 }
455 else if (ret == -1)
456 {
457 /* OPTIONAL component absent.
458 * Free and zero the field.
459 */
460 ASN1_template_free(pseqval, seqtt);
461 continue;
462 }
463 /* Update length */
464 len -= p - q;
465 }
466
467 /* Check for EOC if expecting one */
468 if (seq_eoc && !asn1_check_eoc(&p, len))
469 {
470 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
471 goto err;
472 }
473 /* Check all data read */
474 if (!seq_nolen && len)
475 {
476 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
477 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
478 goto err;
479 }
480
481 /* If we get here we've got no more data in the SEQUENCE,
482 * however we may not have read all fields so check all
483 * remaining are OPTIONAL and clear any that are.
484 */
485 for (; i < it->tcount; tt++, i++)
486 {
487 const ASN1_TEMPLATE *seqtt;
488 seqtt = asn1_do_adb(pval, tt, 1);
489 if (!seqtt)
490 goto err;
491 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
492 {
493 ASN1_VALUE **pseqval;
494 pseqval = asn1_get_field_ptr(pval, seqtt);
495 ASN1_template_free(pseqval, seqtt);
496 }
497 else
498 {
499 errtt = seqtt;
500 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
501 ASN1_R_FIELD_MISSING);
502 goto err;
503 }
504 }
505 /* Save encoding */
506 if (!asn1_enc_save(pval, *in, p - *in, it))
507 goto auxerr;
508 *in = p;
509 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
510 goto auxerr;
511 return 1;
512
513 default:
514 return 0;
515 }
516 auxerr:
517 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
518 err:
519 ASN1_item_ex_free(pval, it);
520 if (errtt)
521 ERR_add_error_data(4, "Field=", errtt->field_name,
522 ", Type=", it->sname);
523 else
524 ERR_add_error_data(2, "Type=", it->sname);
525 return 0;
526 }
527
528/* Templates are handled with two separate functions.
529 * One handles any EXPLICIT tag and the other handles the rest.
530 */
531
532static int asn1_template_ex_d2i(ASN1_VALUE **val,
533 const unsigned char **in, long inlen,
534 const ASN1_TEMPLATE *tt, char opt,
535 ASN1_TLC *ctx)
536 {
537 int flags, aclass;
538 int ret;
539 long len;
540 const unsigned char *p, *q;
541 char exp_eoc;
542 if (!val)
543 return 0;
544 flags = tt->flags;
545 aclass = flags & ASN1_TFLG_TAG_CLASS;
546
547 p = *in;
548
549 /* Check if EXPLICIT tag expected */
550 if (flags & ASN1_TFLG_EXPTAG)
551 {
552 char cst;
553 /* Need to work out amount of data available to the inner
554 * content and where it starts: so read in EXPLICIT header to
555 * get the info.
556 */
557 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
558 &p, inlen, tt->tag, aclass, opt, ctx);
559 q = p;
560 if (!ret)
561 {
562 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
563 ERR_R_NESTED_ASN1_ERROR);
564 return 0;
565 }
566 else if (ret == -1)
567 return -1;
568 if (!cst)
569 {
570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
571 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
572 return 0;
573 }
574 /* We've found the field so it can't be OPTIONAL now */
575 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
576 if (!ret)
577 {
578 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
579 ERR_R_NESTED_ASN1_ERROR);
580 return 0;
581 }
582 /* We read the field in OK so update length */
583 len -= p - q;
584 if (exp_eoc)
585 {
586 /* If NDEF we must have an EOC here */
587 if (!asn1_check_eoc(&p, len))
588 {
589 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
590 ASN1_R_MISSING_EOC);
591 goto err;
592 }
593 }
594 else
595 {
596 /* Otherwise we must hit the EXPLICIT tag end or its
597 * an error */
598 if (len)
599 {
600 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
601 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
602 goto err;
603 }
604 }
605 }
606 else
607 return asn1_template_noexp_d2i(val, in, inlen,
608 tt, opt, ctx);
609
610 *in = p;
611 return 1;
612
613 err:
614 ASN1_template_free(val, tt);
615 return 0;
616 }
617
618static int asn1_template_noexp_d2i(ASN1_VALUE **val,
619 const unsigned char **in, long len,
620 const ASN1_TEMPLATE *tt, char opt,
621 ASN1_TLC *ctx)
622 {
623 int flags, aclass;
624 int ret;
625 const unsigned char *p, *q;
626 if (!val)
627 return 0;
628 flags = tt->flags;
629 aclass = flags & ASN1_TFLG_TAG_CLASS;
630
631 p = *in;
632 q = p;
633
634 if (flags & ASN1_TFLG_SK_MASK)
635 {
636 /* SET OF, SEQUENCE OF */
637 int sktag, skaclass;
638 char sk_eoc;
639 /* First work out expected inner tag value */
640 if (flags & ASN1_TFLG_IMPTAG)
641 {
642 sktag = tt->tag;
643 skaclass = aclass;
644 }
645 else
646 {
647 skaclass = V_ASN1_UNIVERSAL;
648 if (flags & ASN1_TFLG_SET_OF)
649 sktag = V_ASN1_SET;
650 else
651 sktag = V_ASN1_SEQUENCE;
652 }
653 /* Get the tag */
654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
655 &p, len, sktag, skaclass, opt, ctx);
656 if (!ret)
657 {
658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
659 ERR_R_NESTED_ASN1_ERROR);
660 return 0;
661 }
662 else if (ret == -1)
663 return -1;
664 if (!*val)
665 *val = (ASN1_VALUE *)sk_new_null();
666 else
667 {
668 /* We've got a valid STACK: free up any items present */
669 STACK_OF(ASN1_VALUE) *sktmp
670 = (STACK_OF(ASN1_VALUE) *)*val;
671 ASN1_VALUE *vtmp;
672 while(sk_ASN1_VALUE_num(sktmp) > 0)
673 {
674 vtmp = sk_ASN1_VALUE_pop(sktmp);
675 ASN1_item_ex_free(&vtmp,
676 ASN1_ITEM_ptr(tt->item));
677 }
678 }
679
680 if (!*val)
681 {
682 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
683 ERR_R_MALLOC_FAILURE);
684 goto err;
685 }
686
687 /* Read as many items as we can */
688 while(len > 0)
689 {
690 ASN1_VALUE *skfield;
691 q = p;
692 /* See if EOC found */
693 if (asn1_check_eoc(&p, len))
694 {
695 if (!sk_eoc)
696 {
697 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
698 ASN1_R_UNEXPECTED_EOC);
699 goto err;
700 }
701 len -= p - q;
702 sk_eoc = 0;
703 break;
704 }
705 skfield = NULL;
706 if (!ASN1_item_ex_d2i(&skfield, &p, len,
707 ASN1_ITEM_ptr(tt->item),
708 -1, 0, 0, ctx))
709 {
710 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
711 ERR_R_NESTED_ASN1_ERROR);
712 goto err;
713 }
714 len -= p - q;
715 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
716 skfield))
717 {
718 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
719 ERR_R_MALLOC_FAILURE);
720 goto err;
721 }
722 }
723 if (sk_eoc)
724 {
725 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
726 goto err;
727 }
728 }
729 else if (flags & ASN1_TFLG_IMPTAG)
730 {
731 /* IMPLICIT tagging */
732 ret = ASN1_item_ex_d2i(val, &p, len,
733 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
734 if (!ret)
735 {
736 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
737 ERR_R_NESTED_ASN1_ERROR);
738 goto err;
739 }
740 else if (ret == -1)
741 return -1;
742 }
743 else
744 {
745 /* Nothing special */
746 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
747 -1, 0, opt, ctx);
748 if (!ret)
749 {
750 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
751 ERR_R_NESTED_ASN1_ERROR);
752 goto err;
753 }
754 else if (ret == -1)
755 return -1;
756 }
757
758 *in = p;
759 return 1;
760
761 err:
762 ASN1_template_free(val, tt);
763 return 0;
764 }
765
766static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
767 const unsigned char **in, long inlen,
768 const ASN1_ITEM *it,
769 int tag, int aclass, char opt, ASN1_TLC *ctx)
770 {
771 int ret = 0, utype;
772 long plen;
773 char cst, inf, free_cont = 0;
774 const unsigned char *p;
775 BUF_MEM buf;
776 const unsigned char *cont = NULL;
777 long len;
778 if (!pval)
779 {
780 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
781 return 0; /* Should never happen */
782 }
783
784 if (it->itype == ASN1_ITYPE_MSTRING)
785 {
786 utype = tag;
787 tag = -1;
788 }
789 else
790 utype = it->utype;
791
792 if (utype == V_ASN1_ANY)
793 {
794 /* If type is ANY need to figure out type from tag */
795 unsigned char oclass;
796 if (tag >= 0)
797 {
798 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
799 ASN1_R_ILLEGAL_TAGGED_ANY);
800 return 0;
801 }
802 if (opt)
803 {
804 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
805 ASN1_R_ILLEGAL_OPTIONAL_ANY);
806 return 0;
807 }
808 p = *in;
809 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
810 &p, inlen, -1, 0, 0, ctx);
811 if (!ret)
812 {
813 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
814 ERR_R_NESTED_ASN1_ERROR);
815 return 0;
816 }
817 if (oclass != V_ASN1_UNIVERSAL)
818 utype = V_ASN1_OTHER;
819 }
820 if (tag == -1)
821 {
822 tag = utype;
823 aclass = V_ASN1_UNIVERSAL;
824 }
825 p = *in;
826 /* Check header */
827 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
828 &p, inlen, tag, aclass, opt, ctx);
829 if (!ret)
830 {
831 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
832 return 0;
833 }
834 else if (ret == -1)
835 return -1;
836 ret = 0;
837 /* SEQUENCE, SET and "OTHER" are left in encoded form */
838 if ((utype == V_ASN1_SEQUENCE)
839 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
840 {
841 /* Clear context cache for type OTHER because the auto clear
842 * when we have a exact match wont work
843 */
844 if (utype == V_ASN1_OTHER)
845 {
846 asn1_tlc_clear(ctx);
847 }
848 /* SEQUENCE and SET must be constructed */
849 else if (!cst)
850 {
851 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
852 ASN1_R_TYPE_NOT_CONSTRUCTED);
853 return 0;
854 }
855
856 cont = *in;
857 /* If indefinite length constructed find the real end */
858 if (inf)
859 {
860 if (!asn1_find_end(&p, plen, inf))
861 goto err;
862 len = p - cont;
863 }
864 else
865 {
866 len = p - cont + plen;
867 p += plen;
868 buf.data = NULL;
869 }
870 }
871 else if (cst)
872 {
873 buf.length = 0;
874 buf.max = 0;
875 buf.data = NULL;
876 /* Should really check the internal tags are correct but
877 * some things may get this wrong. The relevant specs
878 * say that constructed string types should be OCTET STRINGs
879 * internally irrespective of the type. So instead just check
880 * for UNIVERSAL class and ignore the tag.
881 */
882 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
883 {
884 free_cont = 1;
885 goto err;
886 }
887 len = buf.length;
888 /* Append a final null to string */
889 if (!BUF_MEM_grow_clean(&buf, len + 1))
890 {
891 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
892 ERR_R_MALLOC_FAILURE);
893 return 0;
894 }
895 buf.data[len] = 0;
896 cont = (const unsigned char *)buf.data;
897 free_cont = 1;
898 }
899 else
900 {
901 cont = p;
902 len = plen;
903 p += plen;
904 }
905
906 /* We now have content length and type: translate into a structure */
907 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
908 goto err;
909
910 *in = p;
911 ret = 1;
912 err:
913 if (free_cont && buf.data) OPENSSL_free(buf.data);
914 return ret;
915 }
916
917/* Translate ASN1 content octets into a structure */
918
919int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
920 int utype, char *free_cont, const ASN1_ITEM *it)
921 {
922 ASN1_VALUE **opval = NULL;
923 ASN1_STRING *stmp;
924 ASN1_TYPE *typ = NULL;
925 int ret = 0;
926 const ASN1_PRIMITIVE_FUNCS *pf;
927 ASN1_INTEGER **tint;
928 pf = it->funcs;
929
930 if (pf && pf->prim_c2i)
931 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
932 /* If ANY type clear type and set pointer to internal value */
933 if (it->utype == V_ASN1_ANY)
934 {
935 if (!*pval)
936 {
937 typ = ASN1_TYPE_new();
938 if (typ == NULL)
939 goto err;
940 *pval = (ASN1_VALUE *)typ;
941 }
942 else
943 typ = (ASN1_TYPE *)*pval;
944
945 if (utype != typ->type)
946 ASN1_TYPE_set(typ, utype, NULL);
947 opval = pval;
948 pval = &typ->value.asn1_value;
949 }
950 switch(utype)
951 {
952 case V_ASN1_OBJECT:
953 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
954 goto err;
955 break;
956
957 case V_ASN1_NULL:
958 if (len)
959 {
960 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ASN1_R_NULL_IS_WRONG_LENGTH);
962 goto err;
963 }
964 *pval = (ASN1_VALUE *)1;
965 break;
966
967 case V_ASN1_BOOLEAN:
968 if (len != 1)
969 {
970 ASN1err(ASN1_F_ASN1_EX_C2I,
971 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
972 goto err;
973 }
974 else
975 {
976 ASN1_BOOLEAN *tbool;
977 tbool = (ASN1_BOOLEAN *)pval;
978 *tbool = *cont;
979 }
980 break;
981
982 case V_ASN1_BIT_STRING:
983 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
984 goto err;
985 break;
986
987 case V_ASN1_INTEGER:
988 case V_ASN1_NEG_INTEGER:
989 case V_ASN1_ENUMERATED:
990 case V_ASN1_NEG_ENUMERATED:
991 tint = (ASN1_INTEGER **)pval;
992 if (!c2i_ASN1_INTEGER(tint, &cont, len))
993 goto err;
994 /* Fixup type to match the expected form */
995 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
996 break;
997
998 case V_ASN1_OCTET_STRING:
999 case V_ASN1_NUMERICSTRING:
1000 case V_ASN1_PRINTABLESTRING:
1001 case V_ASN1_T61STRING:
1002 case V_ASN1_VIDEOTEXSTRING:
1003 case V_ASN1_IA5STRING:
1004 case V_ASN1_UTCTIME:
1005 case V_ASN1_GENERALIZEDTIME:
1006 case V_ASN1_GRAPHICSTRING:
1007 case V_ASN1_VISIBLESTRING:
1008 case V_ASN1_GENERALSTRING:
1009 case V_ASN1_UNIVERSALSTRING:
1010 case V_ASN1_BMPSTRING:
1011 case V_ASN1_UTF8STRING:
1012 case V_ASN1_OTHER:
1013 case V_ASN1_SET:
1014 case V_ASN1_SEQUENCE:
1015 default:
1016 if (utype == V_ASN1_BMPSTRING && (len & 1))
1017 {
1018 ASN1err(ASN1_F_ASN1_EX_C2I,
1019 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1020 goto err;
1021 }
1022 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1023 {
1024 ASN1err(ASN1_F_ASN1_EX_C2I,
1025 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1026 goto err;
1027 }
1028 /* All based on ASN1_STRING and handled the same */
1029 if (!*pval)
1030 {
1031 stmp = ASN1_STRING_type_new(utype);
1032 if (!stmp)
1033 {
1034 ASN1err(ASN1_F_ASN1_EX_C2I,
1035 ERR_R_MALLOC_FAILURE);
1036 goto err;
1037 }
1038 *pval = (ASN1_VALUE *)stmp;
1039 }
1040 else
1041 {
1042 stmp = (ASN1_STRING *)*pval;
1043 stmp->type = utype;
1044 }
1045 /* If we've already allocated a buffer use it */
1046 if (*free_cont)
1047 {
1048 if (stmp->data)
1049 OPENSSL_free(stmp->data);
1050 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1051 stmp->length = len;
1052 *free_cont = 0;
1053 }
1054 else
1055 {
1056 if (!ASN1_STRING_set(stmp, cont, len))
1057 {
1058 ASN1err(ASN1_F_ASN1_EX_C2I,
1059 ERR_R_MALLOC_FAILURE);
1060 ASN1_STRING_free(stmp);
1061 *pval = NULL;
1062 goto err;
1063 }
1064 }
1065 break;
1066 }
1067 /* If ASN1_ANY and NULL type fix up value */
1068 if (typ && (utype == V_ASN1_NULL))
1069 typ->value.ptr = NULL;
1070
1071 ret = 1;
1072 err:
1073 if (!ret)
1074 {
1075 ASN1_TYPE_free(typ);
1076 if (opval)
1077 *opval = NULL;
1078 }
1079 return ret;
1080 }
1081
1082
1083/* This function finds the end of an ASN1 structure when passed its maximum
1084 * length, whether it is indefinite length and a pointer to the content.
1085 * This is more efficient than calling asn1_collect because it does not
1086 * recurse on each indefinite length header.
1087 */
1088
1089static int asn1_find_end(const unsigned char **in, long len, char inf)
1090 {
1091 int expected_eoc;
1092 long plen;
1093 const unsigned char *p = *in, *q;
1094 /* If not indefinite length constructed just add length */
1095 if (inf == 0)
1096 {
1097 *in += len;
1098 return 1;
1099 }
1100 expected_eoc = 1;
1101 /* Indefinite length constructed form. Find the end when enough EOCs
1102 * are found. If more indefinite length constructed headers
1103 * are encountered increment the expected eoc count otherwise just
1104 * skip to the end of the data.
1105 */
1106 while (len > 0)
1107 {
1108 if(asn1_check_eoc(&p, len))
1109 {
1110 expected_eoc--;
1111 if (expected_eoc == 0)
1112 break;
1113 len -= 2;
1114 continue;
1115 }
1116 q = p;
1117 /* Just read in a header: only care about the length */
1118 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1119 -1, 0, 0, NULL))
1120 {
1121 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1122 return 0;
1123 }
1124 if (inf)
1125 expected_eoc++;
1126 else
1127 p += plen;
1128 len -= p - q;
1129 }
1130 if (expected_eoc)
1131 {
1132 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1133 return 0;
1134 }
1135 *in = p;
1136 return 1;
1137 }
1138/* This function collects the asn1 data from a constructred string
1139 * type into a buffer. The values of 'in' and 'len' should refer
1140 * to the contents of the constructed type and 'inf' should be set
1141 * if it is indefinite length.
1142 */
1143
1144#ifndef ASN1_MAX_STRING_NEST
1145/* This determines how many levels of recursion are permitted in ASN1
1146 * string types. If it is not limited stack overflows can occur. If set
1147 * to zero no recursion is allowed at all. Although zero should be adequate
1148 * examples exist that require a value of 1. So 5 should be more than enough.
1149 */
1150#define ASN1_MAX_STRING_NEST 5
1151#endif
1152
1153
1154static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1155 char inf, int tag, int aclass, int depth)
1156 {
1157 const unsigned char *p, *q;
1158 long plen;
1159 char cst, ininf;
1160 p = *in;
1161 inf &= 1;
1162 /* If no buffer and not indefinite length constructed just pass over
1163 * the encoded data */
1164 if (!buf && !inf)
1165 {
1166 *in += len;
1167 return 1;
1168 }
1169 while(len > 0)
1170 {
1171 q = p;
1172 /* Check for EOC */
1173 if (asn1_check_eoc(&p, len))
1174 {
1175 /* EOC is illegal outside indefinite length
1176 * constructed form */
1177 if (!inf)
1178 {
1179 ASN1err(ASN1_F_ASN1_COLLECT,
1180 ASN1_R_UNEXPECTED_EOC);
1181 return 0;
1182 }
1183 inf = 0;
1184 break;
1185 }
1186
1187 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1188 len, tag, aclass, 0, NULL))
1189 {
1190 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1191 return 0;
1192 }
1193
1194 /* If indefinite length constructed update max length */
1195 if (cst)
1196 {
1197 if (depth >= ASN1_MAX_STRING_NEST)
1198 {
1199 ASN1err(ASN1_F_ASN1_COLLECT,
1200 ASN1_R_NESTED_ASN1_STRING);
1201 return 0;
1202 }
1203 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1204 depth + 1))
1205 return 0;
1206 }
1207 else if (plen && !collect_data(buf, &p, plen))
1208 return 0;
1209 len -= p - q;
1210 }
1211 if (inf)
1212 {
1213 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1214 return 0;
1215 }
1216 *in = p;
1217 return 1;
1218 }
1219
1220static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1221 {
1222 int len;
1223 if (buf)
1224 {
1225 len = buf->length;
1226 if (!BUF_MEM_grow_clean(buf, len + plen))
1227 {
1228 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1229 return 0;
1230 }
1231 memcpy(buf->data + len, *p, plen);
1232 }
1233 *p += plen;
1234 return 1;
1235 }
1236
1237/* Check for ASN1 EOC and swallow it if found */
1238
1239static int asn1_check_eoc(const unsigned char **in, long len)
1240 {
1241 const unsigned char *p;
1242 if (len < 2) return 0;
1243 p = *in;
1244 if (!p[0] && !p[1])
1245 {
1246 *in += 2;
1247 return 1;
1248 }
1249 return 0;
1250 }
1251
1252/* Check an ASN1 tag and length: a bit like ASN1_get_object
1253 * but it sets the length for indefinite length constructed
1254 * form, we don't know the exact length but we can set an
1255 * upper bound to the amount of data available minus the
1256 * header length just read.
1257 */
1258
1259static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1260 char *inf, char *cst,
1261 const unsigned char **in, long len,
1262 int exptag, int expclass, char opt,
1263 ASN1_TLC *ctx)
1264 {
1265 int i;
1266 int ptag, pclass;
1267 long plen;
1268 const unsigned char *p, *q;
1269 p = *in;
1270 q = p;
1271
1272 if (ctx && ctx->valid)
1273 {
1274 i = ctx->ret;
1275 plen = ctx->plen;
1276 pclass = ctx->pclass;
1277 ptag = ctx->ptag;
1278 p += ctx->hdrlen;
1279 }
1280 else
1281 {
1282 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1283 if (ctx)
1284 {
1285 ctx->ret = i;
1286 ctx->plen = plen;
1287 ctx->pclass = pclass;
1288 ctx->ptag = ptag;
1289 ctx->hdrlen = p - q;
1290 ctx->valid = 1;
1291 /* If definite length, and no error, length +
1292 * header can't exceed total amount of data available.
1293 */
1294 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1295 {
1296 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1297 ASN1_R_TOO_LONG);
1298 asn1_tlc_clear(ctx);
1299 return 0;
1300 }
1301 }
1302 }
1303
1304 if (i & 0x80)
1305 {
1306 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1307 asn1_tlc_clear(ctx);
1308 return 0;
1309 }
1310 if (exptag >= 0)
1311 {
1312 if ((exptag != ptag) || (expclass != pclass))
1313 {
1314 /* If type is OPTIONAL, not an error:
1315 * indicate missing type.
1316 */
1317 if (opt) return -1;
1318 asn1_tlc_clear(ctx);
1319 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1320 return 0;
1321 }
1322 /* We have a tag and class match:
1323 * assume we are going to do something with it */
1324 asn1_tlc_clear(ctx);
1325 }
1326
1327 if (i & 1)
1328 plen = len - (p - q);
1329
1330 if (inf)
1331 *inf = i & 1;
1332
1333 if (cst)
1334 *cst = i & V_ASN1_CONSTRUCTED;
1335
1336 if (olen)
1337 *olen = plen;
1338
1339 if (oclass)
1340 *oclass = pclass;
1341
1342 if (otag)
1343 *otag = ptag;
1344
1345 *in = p;
1346 return 1;
1347 }
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
deleted file mode 100644
index 936ad1f767..0000000000
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ /dev/null
@@ -1,691 +0,0 @@
1/* tasn_enc.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <string.h>
62#include "cryptlib.h"
63#include <openssl/asn1.h>
64#include <openssl/asn1t.h>
65#include <openssl/objects.h>
66
67static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
68 const ASN1_ITEM *it,
69 int tag, int aclass);
70static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
71 int skcontlen, const ASN1_ITEM *item,
72 int do_sort, int iclass);
73static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
74 const ASN1_TEMPLATE *tt,
75 int tag, int aclass);
76static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
77 const ASN1_ITEM *it, int flags);
78
79/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
80 * to use indefinite length constructed encoding, where appropriate
81 */
82
83int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
84 const ASN1_ITEM *it)
85 {
86 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
87 }
88
89int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
90 {
91 return asn1_item_flags_i2d(val, out, it, 0);
92 }
93
94/* Encode an ASN1 item, this is use by the
95 * standard 'i2d' function. 'out' points to
96 * a buffer to output the data to.
97 *
98 * The new i2d has one additional feature. If the output
99 * buffer is NULL (i.e. *out == NULL) then a buffer is
100 * allocated and populated with the encoding.
101 */
102
103static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
104 const ASN1_ITEM *it, int flags)
105 {
106 if (out && !*out)
107 {
108 unsigned char *p, *buf;
109 int len;
110 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
111 if (len <= 0)
112 return len;
113 buf = OPENSSL_malloc(len);
114 if (!buf)
115 return -1;
116 p = buf;
117 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
118 *out = buf;
119 return len;
120 }
121
122 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
123 }
124
125/* Encode an item, taking care of IMPLICIT tagging (if any).
126 * This function performs the normal item handling: it can be
127 * used in external types.
128 */
129
130int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
131 const ASN1_ITEM *it, int tag, int aclass)
132 {
133 const ASN1_TEMPLATE *tt = NULL;
134 unsigned char *p = NULL;
135 int i, seqcontlen, seqlen, ndef = 1;
136 const ASN1_COMPAT_FUNCS *cf;
137 const ASN1_EXTERN_FUNCS *ef;
138 const ASN1_AUX *aux = it->funcs;
139 ASN1_aux_cb *asn1_cb = 0;
140
141 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
142 return 0;
143
144 if (aux && aux->asn1_cb)
145 asn1_cb = aux->asn1_cb;
146
147 switch(it->itype)
148 {
149
150 case ASN1_ITYPE_PRIMITIVE:
151 if (it->templates)
152 return asn1_template_ex_i2d(pval, out, it->templates,
153 tag, aclass);
154 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
155 break;
156
157 case ASN1_ITYPE_MSTRING:
158 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
159
160 case ASN1_ITYPE_CHOICE:
161 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
162 return 0;
163 i = asn1_get_choice_selector(pval, it);
164 if ((i >= 0) && (i < it->tcount))
165 {
166 ASN1_VALUE **pchval;
167 const ASN1_TEMPLATE *chtt;
168 chtt = it->templates + i;
169 pchval = asn1_get_field_ptr(pval, chtt);
170 return asn1_template_ex_i2d(pchval, out, chtt,
171 -1, aclass);
172 }
173 /* Fixme: error condition if selector out of range */
174 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
175 return 0;
176 break;
177
178 case ASN1_ITYPE_EXTERN:
179 /* If new style i2d it does all the work */
180 ef = it->funcs;
181 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
182
183 case ASN1_ITYPE_COMPAT:
184 /* old style hackery... */
185 cf = it->funcs;
186 if (out)
187 p = *out;
188 i = cf->asn1_i2d(*pval, out);
189 /* Fixup for IMPLICIT tag: note this messes up for tags > 30,
190 * but so did the old code. Tags > 30 are very rare anyway.
191 */
192 if (out && (tag != -1))
193 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
194 return i;
195
196 case ASN1_ITYPE_NDEF_SEQUENCE:
197 /* Use indefinite length constructed if requested */
198 if (aclass & ASN1_TFLG_NDEF) ndef = 2;
199 /* fall through */
200
201 case ASN1_ITYPE_SEQUENCE:
202 i = asn1_enc_restore(&seqcontlen, out, pval, it);
203 /* An error occurred */
204 if (i < 0)
205 return 0;
206 /* We have a valid cached encoding... */
207 if (i > 0)
208 return seqcontlen;
209 /* Otherwise carry on */
210 seqcontlen = 0;
211 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
212 if (tag == -1)
213 {
214 tag = V_ASN1_SEQUENCE;
215 /* Retain any other flags in aclass */
216 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
217 | V_ASN1_UNIVERSAL;
218 }
219 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
220 return 0;
221 /* First work out sequence content length */
222 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
223 {
224 const ASN1_TEMPLATE *seqtt;
225 ASN1_VALUE **pseqval;
226 seqtt = asn1_do_adb(pval, tt, 1);
227 if (!seqtt)
228 return 0;
229 pseqval = asn1_get_field_ptr(pval, seqtt);
230 /* FIXME: check for errors in enhanced version */
231 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
232 -1, aclass);
233 }
234
235 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
236 if (!out)
237 return seqlen;
238 /* Output SEQUENCE header */
239 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
240 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
241 {
242 const ASN1_TEMPLATE *seqtt;
243 ASN1_VALUE **pseqval;
244 seqtt = asn1_do_adb(pval, tt, 1);
245 if (!seqtt)
246 return 0;
247 pseqval = asn1_get_field_ptr(pval, seqtt);
248 /* FIXME: check for errors in enhanced version */
249 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
250 }
251 if (ndef == 2)
252 ASN1_put_eoc(out);
253 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
254 return 0;
255 return seqlen;
256
257 default:
258 return 0;
259
260 }
261 return 0;
262 }
263
264int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
265 const ASN1_TEMPLATE *tt)
266 {
267 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
268 }
269
270static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
271 const ASN1_TEMPLATE *tt, int tag, int iclass)
272 {
273 int i, ret, flags, ttag, tclass, ndef;
274 flags = tt->flags;
275 /* Work out tag and class to use: tagging may come
276 * either from the template or the arguments, not both
277 * because this would create ambiguity. Additionally
278 * the iclass argument may contain some additional flags
279 * which should be noted and passed down to other levels.
280 */
281 if (flags & ASN1_TFLG_TAG_MASK)
282 {
283 /* Error if argument and template tagging */
284 if (tag != -1)
285 /* FIXME: error code here */
286 return -1;
287 /* Get tagging from template */
288 ttag = tt->tag;
289 tclass = flags & ASN1_TFLG_TAG_CLASS;
290 }
291 else if (tag != -1)
292 {
293 /* No template tagging, get from arguments */
294 ttag = tag;
295 tclass = iclass & ASN1_TFLG_TAG_CLASS;
296 }
297 else
298 {
299 ttag = -1;
300 tclass = 0;
301 }
302 /*
303 * Remove any class mask from iflag.
304 */
305 iclass &= ~ASN1_TFLG_TAG_CLASS;
306
307 /* At this point 'ttag' contains the outer tag to use,
308 * 'tclass' is the class and iclass is any flags passed
309 * to this function.
310 */
311
312 /* if template and arguments require ndef, use it */
313 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
314 ndef = 2;
315 else ndef = 1;
316
317 if (flags & ASN1_TFLG_SK_MASK)
318 {
319 /* SET OF, SEQUENCE OF */
320 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
321 int isset, sktag, skaclass;
322 int skcontlen, sklen;
323 ASN1_VALUE *skitem;
324
325 if (!*pval)
326 return 0;
327
328 if (flags & ASN1_TFLG_SET_OF)
329 {
330 isset = 1;
331 /* 2 means we reorder */
332 if (flags & ASN1_TFLG_SEQUENCE_OF)
333 isset = 2;
334 }
335 else isset = 0;
336
337 /* Work out inner tag value: if EXPLICIT
338 * or no tagging use underlying type.
339 */
340 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG))
341 {
342 sktag = ttag;
343 skaclass = tclass;
344 }
345 else
346 {
347 skaclass = V_ASN1_UNIVERSAL;
348 if (isset)
349 sktag = V_ASN1_SET;
350 else sktag = V_ASN1_SEQUENCE;
351 }
352
353 /* Determine total length of items */
354 skcontlen = 0;
355 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
356 {
357 skitem = sk_ASN1_VALUE_value(sk, i);
358 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
359 ASN1_ITEM_ptr(tt->item),
360 -1, iclass);
361 }
362 sklen = ASN1_object_size(ndef, skcontlen, sktag);
363 /* If EXPLICIT need length of surrounding tag */
364 if (flags & ASN1_TFLG_EXPTAG)
365 ret = ASN1_object_size(ndef, sklen, ttag);
366 else ret = sklen;
367
368 if (!out)
369 return ret;
370
371 /* Now encode this lot... */
372 /* EXPLICIT tag */
373 if (flags & ASN1_TFLG_EXPTAG)
374 ASN1_put_object(out, ndef, sklen, ttag, tclass);
375 /* SET or SEQUENCE and IMPLICIT tag */
376 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
377 /* And the stuff itself */
378 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
379 isset, iclass);
380 if (ndef == 2)
381 {
382 ASN1_put_eoc(out);
383 if (flags & ASN1_TFLG_EXPTAG)
384 ASN1_put_eoc(out);
385 }
386
387 return ret;
388 }
389
390 if (flags & ASN1_TFLG_EXPTAG)
391 {
392 /* EXPLICIT tagging */
393 /* Find length of tagged item */
394 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item),
395 -1, iclass);
396 if (!i)
397 return 0;
398 /* Find length of EXPLICIT tag */
399 ret = ASN1_object_size(ndef, i, ttag);
400 if (out)
401 {
402 /* Output tag and item */
403 ASN1_put_object(out, ndef, i, ttag, tclass);
404 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
405 -1, iclass);
406 if (ndef == 2)
407 ASN1_put_eoc(out);
408 }
409 return ret;
410 }
411
412 /* Either normal or IMPLICIT tagging: combine class and flags */
413 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
414 ttag, tclass | iclass);
415
416}
417
418/* Temporary structure used to hold DER encoding of items for SET OF */
419
420typedef struct {
421 unsigned char *data;
422 int length;
423 ASN1_VALUE *field;
424} DER_ENC;
425
426static int der_cmp(const void *a, const void *b)
427 {
428 const DER_ENC *d1 = a, *d2 = b;
429 int cmplen, i;
430 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
431 i = memcmp(d1->data, d2->data, cmplen);
432 if (i)
433 return i;
434 return d1->length - d2->length;
435 }
436
437/* Output the content octets of SET OF or SEQUENCE OF */
438
439static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
440 int skcontlen, const ASN1_ITEM *item,
441 int do_sort, int iclass)
442 {
443 int i;
444 ASN1_VALUE *skitem;
445 unsigned char *tmpdat = NULL, *p = NULL;
446 DER_ENC *derlst = NULL, *tder;
447 if (do_sort)
448 {
449 /* Don't need to sort less than 2 items */
450 if (sk_ASN1_VALUE_num(sk) < 2)
451 do_sort = 0;
452 else
453 {
454 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
455 * sizeof(*derlst));
456 tmpdat = OPENSSL_malloc(skcontlen);
457 if (!derlst || !tmpdat)
458 return 0;
459 }
460 }
461 /* If not sorting just output each item */
462 if (!do_sort)
463 {
464 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
465 {
466 skitem = sk_ASN1_VALUE_value(sk, i);
467 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
468 }
469 return 1;
470 }
471 p = tmpdat;
472
473 /* Doing sort: build up a list of each member's DER encoding */
474 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
475 {
476 skitem = sk_ASN1_VALUE_value(sk, i);
477 tder->data = p;
478 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
479 tder->field = skitem;
480 }
481
482 /* Now sort them */
483 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
484 /* Output sorted DER encoding */
485 p = *out;
486 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
487 {
488 memcpy(p, tder->data, tder->length);
489 p += tder->length;
490 }
491 *out = p;
492 /* If do_sort is 2 then reorder the STACK */
493 if (do_sort == 2)
494 {
495 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk);
496 i++, tder++)
497 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
498 }
499 OPENSSL_free(derlst);
500 OPENSSL_free(tmpdat);
501 return 1;
502 }
503
504static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
505 const ASN1_ITEM *it, int tag, int aclass)
506 {
507 int len;
508 int utype;
509 int usetag;
510 int ndef = 0;
511
512 utype = it->utype;
513
514 /* Get length of content octets and maybe find
515 * out the underlying type.
516 */
517
518 len = asn1_ex_i2c(pval, NULL, &utype, it);
519
520 /* If SEQUENCE, SET or OTHER then header is
521 * included in pseudo content octets so don't
522 * include tag+length. We need to check here
523 * because the call to asn1_ex_i2c() could change
524 * utype.
525 */
526 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
527 (utype == V_ASN1_OTHER))
528 usetag = 0;
529 else usetag = 1;
530
531 /* -1 means omit type */
532
533 if (len == -1)
534 return 0;
535
536 /* -2 return is special meaning use ndef */
537 if (len == -2)
538 {
539 ndef = 2;
540 len = 0;
541 }
542
543 /* If not implicitly tagged get tag from underlying type */
544 if (tag == -1) tag = utype;
545
546 /* Output tag+length followed by content octets */
547 if (out)
548 {
549 if (usetag)
550 ASN1_put_object(out, ndef, len, tag, aclass);
551 asn1_ex_i2c(pval, *out, &utype, it);
552 if (ndef)
553 ASN1_put_eoc(out);
554 else
555 *out += len;
556 }
557
558 if (usetag)
559 return ASN1_object_size(ndef, len, tag);
560 return len;
561 }
562
563/* Produce content octets from a structure */
564
565int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
566 const ASN1_ITEM *it)
567 {
568 ASN1_BOOLEAN *tbool = NULL;
569 ASN1_STRING *strtmp;
570 ASN1_OBJECT *otmp;
571 int utype;
572 const unsigned char *cont;
573 unsigned char c;
574 int len;
575 const ASN1_PRIMITIVE_FUNCS *pf;
576 pf = it->funcs;
577 if (pf && pf->prim_i2c)
578 return pf->prim_i2c(pval, cout, putype, it);
579
580 /* Should type be omitted? */
581 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
582 || (it->utype != V_ASN1_BOOLEAN))
583 {
584 if (!*pval) return -1;
585 }
586
587 if (it->itype == ASN1_ITYPE_MSTRING)
588 {
589 /* If MSTRING type set the underlying type */
590 strtmp = (ASN1_STRING *)*pval;
591 utype = strtmp->type;
592 *putype = utype;
593 }
594 else if (it->utype == V_ASN1_ANY)
595 {
596 /* If ANY set type and pointer to value */
597 ASN1_TYPE *typ;
598 typ = (ASN1_TYPE *)*pval;
599 utype = typ->type;
600 *putype = utype;
601 pval = &typ->value.asn1_value;
602 }
603 else utype = *putype;
604
605 switch(utype)
606 {
607 case V_ASN1_OBJECT:
608 otmp = (ASN1_OBJECT *)*pval;
609 cont = otmp->data;
610 len = otmp->length;
611 break;
612
613 case V_ASN1_NULL:
614 cont = NULL;
615 len = 0;
616 break;
617
618 case V_ASN1_BOOLEAN:
619 tbool = (ASN1_BOOLEAN *)pval;
620 if (*tbool == -1)
621 return -1;
622 if (it->utype != V_ASN1_ANY)
623 {
624 /* Default handling if value == size field then omit */
625 if (*tbool && (it->size > 0))
626 return -1;
627 if (!*tbool && !it->size)
628 return -1;
629 }
630 c = (unsigned char)*tbool;
631 cont = &c;
632 len = 1;
633 break;
634
635 case V_ASN1_BIT_STRING:
636 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
637 cout ? &cout : NULL);
638 break;
639
640 case V_ASN1_INTEGER:
641 case V_ASN1_NEG_INTEGER:
642 case V_ASN1_ENUMERATED:
643 case V_ASN1_NEG_ENUMERATED:
644 /* These are all have the same content format
645 * as ASN1_INTEGER
646 */
647 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,
648 cout ? &cout : NULL);
649 break;
650
651 case V_ASN1_OCTET_STRING:
652 case V_ASN1_NUMERICSTRING:
653 case V_ASN1_PRINTABLESTRING:
654 case V_ASN1_T61STRING:
655 case V_ASN1_VIDEOTEXSTRING:
656 case V_ASN1_IA5STRING:
657 case V_ASN1_UTCTIME:
658 case V_ASN1_GENERALIZEDTIME:
659 case V_ASN1_GRAPHICSTRING:
660 case V_ASN1_VISIBLESTRING:
661 case V_ASN1_GENERALSTRING:
662 case V_ASN1_UNIVERSALSTRING:
663 case V_ASN1_BMPSTRING:
664 case V_ASN1_UTF8STRING:
665 case V_ASN1_SEQUENCE:
666 case V_ASN1_SET:
667 default:
668 /* All based on ASN1_STRING and handled the same */
669 strtmp = (ASN1_STRING *)*pval;
670 /* Special handling for NDEF */
671 if ((it->size == ASN1_TFLG_NDEF)
672 && (strtmp->flags & ASN1_STRING_FLAG_NDEF))
673 {
674 if (cout)
675 {
676 strtmp->data = cout;
677 strtmp->length = 0;
678 }
679 /* Special return code */
680 return -2;
681 }
682 cont = strtmp->data;
683 len = strtmp->length;
684
685 break;
686
687 }
688 if (cout && len)
689 memcpy(cout, cont, len);
690 return len;
691 }
diff --git a/src/lib/libcrypto/asn1/tasn_fre.c b/src/lib/libcrypto/asn1/tasn_fre.c
deleted file mode 100644
index 77d3092d31..0000000000
--- a/src/lib/libcrypto/asn1/tasn_fre.c
+++ /dev/null
@@ -1,266 +0,0 @@
1/* tasn_fre.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <openssl/asn1.h>
62#include <openssl/asn1t.h>
63#include <openssl/objects.h>
64
65static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine);
66
67/* Free up an ASN1 structure */
68
69void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
70 {
71 asn1_item_combine_free(&val, it, 0);
72 }
73
74void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
75 {
76 asn1_item_combine_free(pval, it, 0);
77 }
78
79static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
80 {
81 const ASN1_TEMPLATE *tt = NULL, *seqtt;
82 const ASN1_EXTERN_FUNCS *ef;
83 const ASN1_COMPAT_FUNCS *cf;
84 const ASN1_AUX *aux = it->funcs;
85 ASN1_aux_cb *asn1_cb;
86 int i;
87 if (!pval)
88 return;
89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
90 return;
91 if (aux && aux->asn1_cb)
92 asn1_cb = aux->asn1_cb;
93 else
94 asn1_cb = 0;
95
96 switch(it->itype)
97 {
98
99 case ASN1_ITYPE_PRIMITIVE:
100 if (it->templates)
101 ASN1_template_free(pval, it->templates);
102 else
103 ASN1_primitive_free(pval, it);
104 break;
105
106 case ASN1_ITYPE_MSTRING:
107 ASN1_primitive_free(pval, it);
108 break;
109
110 case ASN1_ITYPE_CHOICE:
111 if (asn1_cb)
112 {
113 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
114 if (i == 2)
115 return;
116 }
117 i = asn1_get_choice_selector(pval, it);
118 if ((i >= 0) && (i < it->tcount))
119 {
120 ASN1_VALUE **pchval;
121 tt = it->templates + i;
122 pchval = asn1_get_field_ptr(pval, tt);
123 ASN1_template_free(pchval, tt);
124 }
125 if (asn1_cb)
126 asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
127 if (!combine)
128 {
129 OPENSSL_free(*pval);
130 *pval = NULL;
131 }
132 break;
133
134 case ASN1_ITYPE_COMPAT:
135 cf = it->funcs;
136 if (cf && cf->asn1_free)
137 cf->asn1_free(*pval);
138 break;
139
140 case ASN1_ITYPE_EXTERN:
141 ef = it->funcs;
142 if (ef && ef->asn1_ex_free)
143 ef->asn1_ex_free(pval, it);
144 break;
145
146 case ASN1_ITYPE_NDEF_SEQUENCE:
147 case ASN1_ITYPE_SEQUENCE:
148 if (asn1_do_lock(pval, -1, it) > 0)
149 return;
150 if (asn1_cb)
151 {
152 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
153 if (i == 2)
154 return;
155 }
156 asn1_enc_free(pval, it);
157 /* If we free up as normal we will invalidate any
158 * ANY DEFINED BY field and we wont be able to
159 * determine the type of the field it defines. So
160 * free up in reverse order.
161 */
162 tt = it->templates + it->tcount - 1;
163 for (i = 0; i < it->tcount; tt--, i++)
164 {
165 ASN1_VALUE **pseqval;
166 seqtt = asn1_do_adb(pval, tt, 0);
167 if (!seqtt)
168 continue;
169 pseqval = asn1_get_field_ptr(pval, seqtt);
170 ASN1_template_free(pseqval, seqtt);
171 }
172 if (asn1_cb)
173 asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
174 if (!combine)
175 {
176 OPENSSL_free(*pval);
177 *pval = NULL;
178 }
179 break;
180 }
181 }
182
183void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
184 {
185 int i;
186 if (tt->flags & ASN1_TFLG_SK_MASK)
187 {
188 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
189 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
190 {
191 ASN1_VALUE *vtmp;
192 vtmp = sk_ASN1_VALUE_value(sk, i);
193 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
194 0);
195 }
196 sk_ASN1_VALUE_free(sk);
197 *pval = NULL;
198 }
199 else
200 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
201 tt->flags & ASN1_TFLG_COMBINE);
202 }
203
204void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
205 {
206 int utype;
207 if (it)
208 {
209 const ASN1_PRIMITIVE_FUNCS *pf;
210 pf = it->funcs;
211 if (pf && pf->prim_free)
212 {
213 pf->prim_free(pval, it);
214 return;
215 }
216 }
217 /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
218 if (!it)
219 {
220 ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
221 utype = typ->type;
222 pval = &typ->value.asn1_value;
223 if (!*pval)
224 return;
225 }
226 else if (it->itype == ASN1_ITYPE_MSTRING)
227 {
228 utype = -1;
229 if (!*pval)
230 return;
231 }
232 else
233 {
234 utype = it->utype;
235 if ((utype != V_ASN1_BOOLEAN) && !*pval)
236 return;
237 }
238
239 switch(utype)
240 {
241 case V_ASN1_OBJECT:
242 ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
243 break;
244
245 case V_ASN1_BOOLEAN:
246 if (it)
247 *(ASN1_BOOLEAN *)pval = it->size;
248 else
249 *(ASN1_BOOLEAN *)pval = -1;
250 return;
251
252 case V_ASN1_NULL:
253 break;
254
255 case V_ASN1_ANY:
256 ASN1_primitive_free(pval, NULL);
257 OPENSSL_free(*pval);
258 break;
259
260 default:
261 ASN1_STRING_free((ASN1_STRING *)*pval);
262 *pval = NULL;
263 break;
264 }
265 *pval = NULL;
266 }
diff --git a/src/lib/libcrypto/asn1/tasn_new.c b/src/lib/libcrypto/asn1/tasn_new.c
deleted file mode 100644
index 0d9e78cc7c..0000000000
--- a/src/lib/libcrypto/asn1/tasn_new.c
+++ /dev/null
@@ -1,396 +0,0 @@
1/* tasn_new.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <openssl/asn1.h>
62#include <openssl/objects.h>
63#include <openssl/err.h>
64#include <openssl/asn1t.h>
65#include <string.h>
66
67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68 int combine);
69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72
73ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
74 {
75 ASN1_VALUE *ret = NULL;
76 if (ASN1_item_ex_new(&ret, it) > 0)
77 return ret;
78 return NULL;
79 }
80
81/* Allocate an ASN1 structure */
82
83int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84 {
85 return asn1_item_ex_combine_new(pval, it, 0);
86 }
87
88static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
89 int combine)
90 {
91 const ASN1_TEMPLATE *tt = NULL;
92 const ASN1_COMPAT_FUNCS *cf;
93 const ASN1_EXTERN_FUNCS *ef;
94 const ASN1_AUX *aux = it->funcs;
95 ASN1_aux_cb *asn1_cb;
96 ASN1_VALUE **pseqval;
97 int i;
98 if (aux && aux->asn1_cb)
99 asn1_cb = aux->asn1_cb;
100 else
101 asn1_cb = 0;
102
103 if (!combine) *pval = NULL;
104
105#ifdef CRYPTO_MDEBUG
106 if (it->sname)
107 CRYPTO_push_info(it->sname);
108#endif
109
110 switch(it->itype)
111 {
112
113 case ASN1_ITYPE_EXTERN:
114 ef = it->funcs;
115 if (ef && ef->asn1_ex_new)
116 {
117 if (!ef->asn1_ex_new(pval, it))
118 goto memerr;
119 }
120 break;
121
122 case ASN1_ITYPE_COMPAT:
123 cf = it->funcs;
124 if (cf && cf->asn1_new) {
125 *pval = cf->asn1_new();
126 if (!*pval)
127 goto memerr;
128 }
129 break;
130
131 case ASN1_ITYPE_PRIMITIVE:
132 if (it->templates)
133 {
134 if (!ASN1_template_new(pval, it->templates))
135 goto memerr;
136 }
137 else if (!ASN1_primitive_new(pval, it))
138 goto memerr;
139 break;
140
141 case ASN1_ITYPE_MSTRING:
142 if (!ASN1_primitive_new(pval, it))
143 goto memerr;
144 break;
145
146 case ASN1_ITYPE_CHOICE:
147 if (asn1_cb)
148 {
149 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
150 if (!i)
151 goto auxerr;
152 if (i==2)
153 {
154#ifdef CRYPTO_MDEBUG
155 if (it->sname)
156 CRYPTO_pop_info();
157#endif
158 return 1;
159 }
160 }
161 if (!combine)
162 {
163 *pval = OPENSSL_malloc(it->size);
164 if (!*pval)
165 goto memerr;
166 memset(*pval, 0, it->size);
167 }
168 asn1_set_choice_selector(pval, -1, it);
169 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
170 goto auxerr;
171 break;
172
173 case ASN1_ITYPE_NDEF_SEQUENCE:
174 case ASN1_ITYPE_SEQUENCE:
175 if (asn1_cb)
176 {
177 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
178 if (!i)
179 goto auxerr;
180 if (i==2)
181 {
182#ifdef CRYPTO_MDEBUG
183 if (it->sname)
184 CRYPTO_pop_info();
185#endif
186 return 1;
187 }
188 }
189 if (!combine)
190 {
191 *pval = OPENSSL_malloc(it->size);
192 if (!*pval)
193 goto memerr;
194 memset(*pval, 0, it->size);
195 asn1_do_lock(pval, 0, it);
196 asn1_enc_init(pval, it);
197 }
198 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
199 {
200 pseqval = asn1_get_field_ptr(pval, tt);
201 if (!ASN1_template_new(pseqval, tt))
202 goto memerr;
203 }
204 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
205 goto auxerr;
206 break;
207 }
208#ifdef CRYPTO_MDEBUG
209 if (it->sname) CRYPTO_pop_info();
210#endif
211 return 1;
212
213 memerr:
214 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
215#ifdef CRYPTO_MDEBUG
216 if (it->sname) CRYPTO_pop_info();
217#endif
218 return 0;
219
220 auxerr:
221 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
222 ASN1_item_ex_free(pval, it);
223#ifdef CRYPTO_MDEBUG
224 if (it->sname) CRYPTO_pop_info();
225#endif
226 return 0;
227
228 }
229
230static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
231 {
232 const ASN1_EXTERN_FUNCS *ef;
233
234 switch(it->itype)
235 {
236
237 case ASN1_ITYPE_EXTERN:
238 ef = it->funcs;
239 if (ef && ef->asn1_ex_clear)
240 ef->asn1_ex_clear(pval, it);
241 else *pval = NULL;
242 break;
243
244
245 case ASN1_ITYPE_PRIMITIVE:
246 if (it->templates)
247 asn1_template_clear(pval, it->templates);
248 else
249 asn1_primitive_clear(pval, it);
250 break;
251
252 case ASN1_ITYPE_MSTRING:
253 asn1_primitive_clear(pval, it);
254 break;
255
256 case ASN1_ITYPE_COMPAT:
257 case ASN1_ITYPE_CHOICE:
258 case ASN1_ITYPE_SEQUENCE:
259 case ASN1_ITYPE_NDEF_SEQUENCE:
260 *pval = NULL;
261 break;
262 }
263 }
264
265
266int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
267 {
268 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
269 int ret;
270 if (tt->flags & ASN1_TFLG_OPTIONAL)
271 {
272 asn1_template_clear(pval, tt);
273 return 1;
274 }
275 /* If ANY DEFINED BY nothing to do */
276
277 if (tt->flags & ASN1_TFLG_ADB_MASK)
278 {
279 *pval = NULL;
280 return 1;
281 }
282#ifdef CRYPTO_MDEBUG
283 if (tt->field_name)
284 CRYPTO_push_info(tt->field_name);
285#endif
286 /* If SET OF or SEQUENCE OF, its a STACK */
287 if (tt->flags & ASN1_TFLG_SK_MASK)
288 {
289 STACK_OF(ASN1_VALUE) *skval;
290 skval = sk_ASN1_VALUE_new_null();
291 if (!skval)
292 {
293 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
294 ret = 0;
295 goto done;
296 }
297 *pval = (ASN1_VALUE *)skval;
298 ret = 1;
299 goto done;
300 }
301 /* Otherwise pass it back to the item routine */
302 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
303 done:
304#ifdef CRYPTO_MDEBUG
305 if (it->sname)
306 CRYPTO_pop_info();
307#endif
308 return ret;
309 }
310
311static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
312 {
313 /* If ADB or STACK just NULL the field */
314 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
315 *pval = NULL;
316 else
317 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
318 }
319
320
321/* NB: could probably combine most of the real XXX_new() behaviour and junk
322 * all the old functions.
323 */
324
325int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
326 {
327 ASN1_TYPE *typ;
328 ASN1_STRING *str;
329 int utype;
330
331 if (it && it->funcs)
332 {
333 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
334 if (pf->prim_new)
335 return pf->prim_new(pval, it);
336 }
337
338 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
339 utype = -1;
340 else
341 utype = it->utype;
342 switch(utype)
343 {
344 case V_ASN1_OBJECT:
345 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
346 return 1;
347
348 case V_ASN1_BOOLEAN:
349 *(ASN1_BOOLEAN *)pval = it->size;
350 return 1;
351
352 case V_ASN1_NULL:
353 *pval = (ASN1_VALUE *)1;
354 return 1;
355
356 case V_ASN1_ANY:
357 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
358 if (!typ)
359 return 0;
360 typ->value.ptr = NULL;
361 typ->type = -1;
362 *pval = (ASN1_VALUE *)typ;
363 break;
364
365 default:
366 str = ASN1_STRING_type_new(utype);
367 if (it->itype == ASN1_ITYPE_MSTRING && str)
368 str->flags |= ASN1_STRING_FLAG_MSTRING;
369 *pval = (ASN1_VALUE *)str;
370 break;
371 }
372 if (*pval)
373 return 1;
374 return 0;
375 }
376
377static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
378 {
379 int utype;
380 if (it && it->funcs)
381 {
382 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
383 if (pf->prim_clear)
384 pf->prim_clear(pval, it);
385 else
386 *pval = NULL;
387 return;
388 }
389 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
390 utype = -1;
391 else
392 utype = it->utype;
393 if (utype == V_ASN1_BOOLEAN)
394 *(ASN1_BOOLEAN *)pval = it->size;
395 else *pval = NULL;
396 }
diff --git a/src/lib/libcrypto/asn1/tasn_prn.c b/src/lib/libcrypto/asn1/tasn_prn.c
deleted file mode 100644
index 453698012d..0000000000
--- a/src/lib/libcrypto/asn1/tasn_prn.c
+++ /dev/null
@@ -1,627 +0,0 @@
1/* tasn_prn.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000,2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include "cryptlib.h"
62#include <openssl/asn1.h>
63#include <openssl/asn1t.h>
64#include <openssl/objects.h>
65#include <openssl/buffer.h>
66#include <openssl/err.h>
67#include <openssl/x509v3.h>
68#include "asn1_locl.h"
69
70/* Print routines.
71 */
72
73/* ASN1_PCTX routines */
74
75ASN1_PCTX default_pctx =
76 {
77 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
78 0, /* nm_flags */
79 0, /* cert_flags */
80 0, /* oid_flags */
81 0 /* str_flags */
82 };
83
84
85ASN1_PCTX *ASN1_PCTX_new(void)
86 {
87 ASN1_PCTX *ret;
88 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
89 if (ret == NULL)
90 {
91 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
92 return NULL;
93 }
94 ret->flags = 0;
95 ret->nm_flags = 0;
96 ret->cert_flags = 0;
97 ret->oid_flags = 0;
98 ret->str_flags = 0;
99 return ret;
100 }
101
102void ASN1_PCTX_free(ASN1_PCTX *p)
103 {
104 OPENSSL_free(p);
105 }
106
107unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
108 {
109 return p->flags;
110 }
111
112void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
113 {
114 p->flags = flags;
115 }
116
117unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
118 {
119 return p->nm_flags;
120 }
121
122void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
123 {
124 p->nm_flags = flags;
125 }
126
127unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
128 {
129 return p->cert_flags;
130 }
131
132void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
133 {
134 p->cert_flags = flags;
135 }
136
137unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
138 {
139 return p->oid_flags;
140 }
141
142void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
143 {
144 p->oid_flags = flags;
145 }
146
147unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
148 {
149 return p->str_flags;
150 }
151
152void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
153 {
154 p->str_flags = flags;
155 }
156
157/* Main print routines */
158
159static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
160 const ASN1_ITEM *it,
161 const char *fname, const char *sname,
162 int nohdr, const ASN1_PCTX *pctx);
163
164int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
165 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
166
167static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
168 const ASN1_ITEM *it, int indent,
169 const char *fname, const char *sname,
170 const ASN1_PCTX *pctx);
171
172static int asn1_print_fsname(BIO *out, int indent,
173 const char *fname, const char *sname,
174 const ASN1_PCTX *pctx);
175
176int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
177 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
178 {
179 const char *sname;
180 if (pctx == NULL)
181 pctx = &default_pctx;
182 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
183 sname = NULL;
184 else
185 sname = it->sname;
186 return asn1_item_print_ctx(out, &ifld, indent, it,
187 NULL, sname, 0, pctx);
188 }
189
190static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
191 const ASN1_ITEM *it,
192 const char *fname, const char *sname,
193 int nohdr, const ASN1_PCTX *pctx)
194 {
195 const ASN1_TEMPLATE *tt;
196 const ASN1_EXTERN_FUNCS *ef;
197 ASN1_VALUE **tmpfld;
198 const ASN1_AUX *aux = it->funcs;
199 ASN1_aux_cb *asn1_cb;
200 ASN1_PRINT_ARG parg;
201 int i;
202 if (aux && aux->asn1_cb)
203 {
204 parg.out = out;
205 parg.indent = indent;
206 parg.pctx = pctx;
207 asn1_cb = aux->asn1_cb;
208 }
209 else asn1_cb = 0;
210
211 if(*fld == NULL)
212 {
213 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
214 {
215 if (!nohdr && !asn1_print_fsname(out, indent,
216 fname, sname, pctx))
217 return 0;
218 if (BIO_puts(out, "<ABSENT>\n") <= 0)
219 return 0;
220 }
221 return 1;
222 }
223
224 switch(it->itype)
225 {
226 case ASN1_ITYPE_PRIMITIVE:
227 if(it->templates)
228 {
229 if (!asn1_template_print_ctx(out, fld, indent,
230 it->templates, pctx))
231 return 0;
232 }
233 /* fall thru */
234 case ASN1_ITYPE_MSTRING:
235 if (!asn1_primitive_print(out, fld, it,
236 indent, fname, sname,pctx))
237 return 0;
238 break;
239
240 case ASN1_ITYPE_EXTERN:
241 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
242 return 0;
243 /* Use new style print routine if possible */
244 ef = it->funcs;
245 if (ef && ef->asn1_ex_print)
246 {
247 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
248 if (!i)
249 return 0;
250 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
251 return 0;
252 return 1;
253 }
254 else if (sname &&
255 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
256 return 0;
257 break;
258
259 case ASN1_ITYPE_CHOICE:
260#if 0
261 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
262 return 0;
263#endif
264 /* CHOICE type, get selector */
265 i = asn1_get_choice_selector(fld, it);
266 /* This should never happen... */
267 if((i < 0) || (i >= it->tcount))
268 {
269 if (BIO_printf(out,
270 "ERROR: selector [%d] invalid\n", i) <= 0)
271 return 0;
272 return 1;
273 }
274 tt = it->templates + i;
275 tmpfld = asn1_get_field_ptr(fld, tt);
276 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
277 return 0;
278 break;
279
280 case ASN1_ITYPE_SEQUENCE:
281 case ASN1_ITYPE_NDEF_SEQUENCE:
282 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
283 return 0;
284 if (fname || sname)
285 {
286 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
287 {
288 if (BIO_puts(out, " {\n") <= 0)
289 return 0;
290 }
291 else
292 {
293 if (BIO_puts(out, "\n") <= 0)
294 return 0;
295 }
296 }
297
298 if (asn1_cb)
299 {
300 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
301 if (i == 0)
302 return 0;
303 if (i == 2)
304 return 1;
305 }
306
307 /* Print each field entry */
308 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
309 {
310 const ASN1_TEMPLATE *seqtt;
311 seqtt = asn1_do_adb(fld, tt, 1);
312 tmpfld = asn1_get_field_ptr(fld, seqtt);
313 if (!asn1_template_print_ctx(out, tmpfld,
314 indent + 2, seqtt, pctx))
315 return 0;
316 }
317 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
318 {
319 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
320 return 0;
321 }
322
323 if (asn1_cb)
324 {
325 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
326 if (i == 0)
327 return 0;
328 }
329 break;
330
331 default:
332 BIO_printf(out, "Unprocessed type %d\n", it->itype);
333 return 0;
334 }
335
336 return 1;
337 }
338
339int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
340 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
341 {
342 int i, flags;
343 const char *sname, *fname;
344 flags = tt->flags;
345 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
346 sname = ASN1_ITEM_ptr(tt->item)->sname;
347 else
348 sname = NULL;
349 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
350 fname = NULL;
351 else
352 fname = tt->field_name;
353 if(flags & ASN1_TFLG_SK_MASK)
354 {
355 char *tname;
356 ASN1_VALUE *skitem;
357 STACK_OF(ASN1_VALUE) *stack;
358
359 /* SET OF, SEQUENCE OF */
360 if (fname)
361 {
362 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
363 {
364 if(flags & ASN1_TFLG_SET_OF)
365 tname = "SET";
366 else
367 tname = "SEQUENCE";
368 if (BIO_printf(out, "%*s%s OF %s {\n",
369 indent, "", tname, tt->field_name) <= 0)
370 return 0;
371 }
372 else if (BIO_printf(out, "%*s%s:\n", indent, "",
373 fname) <= 0)
374 return 0;
375 }
376 stack = (STACK_OF(ASN1_VALUE) *)*fld;
377 for(i = 0; i < sk_ASN1_VALUE_num(stack); i++)
378 {
379 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
380 return 0;
381
382 skitem = sk_ASN1_VALUE_value(stack, i);
383 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
384 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
385 return 0;
386 }
387 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
388 return 0;
389 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
390 {
391 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
392 return 0;
393 }
394 return 1;
395 }
396 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
397 fname, sname, 0, pctx);
398 }
399
400static int asn1_print_fsname(BIO *out, int indent,
401 const char *fname, const char *sname,
402 const ASN1_PCTX *pctx)
403 {
404 static char spaces[] = " ";
405 const int nspaces = sizeof(spaces) - 1;
406
407#if 0
408 if (!sname && !fname)
409 return 1;
410#endif
411
412 while (indent > nspaces)
413 {
414 if (BIO_write(out, spaces, nspaces) != nspaces)
415 return 0;
416 indent -= nspaces;
417 }
418 if (BIO_write(out, spaces, indent) != indent)
419 return 0;
420 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
421 sname = NULL;
422 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
423 fname = NULL;
424 if (!sname && !fname)
425 return 1;
426 if (fname)
427 {
428 if (BIO_puts(out, fname) <= 0)
429 return 0;
430 }
431 if (sname)
432 {
433 if (fname)
434 {
435 if (BIO_printf(out, " (%s)", sname) <= 0)
436 return 0;
437 }
438 else
439 {
440 if (BIO_puts(out, sname) <= 0)
441 return 0;
442 }
443 }
444 if (BIO_write(out, ": ", 2) != 2)
445 return 0;
446 return 1;
447 }
448
449static int asn1_print_boolean_ctx(BIO *out, const int bool,
450 const ASN1_PCTX *pctx)
451 {
452 const char *str;
453 switch (bool)
454 {
455 case -1:
456 str = "BOOL ABSENT";
457 break;
458
459 case 0:
460 str = "FALSE";
461 break;
462
463 default:
464 str = "TRUE";
465 break;
466
467 }
468
469 if (BIO_puts(out, str) <= 0)
470 return 0;
471 return 1;
472
473 }
474
475static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
476 const ASN1_PCTX *pctx)
477 {
478 char *s;
479 int ret = 1;
480 s = i2s_ASN1_INTEGER(NULL, str);
481 if (BIO_puts(out, s) <= 0)
482 ret = 0;
483 OPENSSL_free(s);
484 return ret;
485 }
486
487static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
488 const ASN1_PCTX *pctx)
489 {
490 char objbuf[80];
491 const char *ln;
492 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
493 if(!ln)
494 ln = "";
495 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
496 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
497 return 0;
498 return 1;
499 }
500
501static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
502 const ASN1_PCTX *pctx)
503 {
504 if (str->type == V_ASN1_BIT_STRING)
505 {
506 if (BIO_printf(out, " (%ld unused bits)\n",
507 str->flags & 0x7) <= 0)
508 return 0;
509 }
510 else if (BIO_puts(out, "\n") <= 0)
511 return 0;
512 if ((str->length > 0)
513 && BIO_dump_indent(out, (char *)str->data, str->length,
514 indent + 2) <= 0)
515 return 0;
516 return 1;
517 }
518
519static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
520 const ASN1_ITEM *it, int indent,
521 const char *fname, const char *sname,
522 const ASN1_PCTX *pctx)
523 {
524 long utype;
525 ASN1_STRING *str;
526 int ret = 1, needlf = 1;
527 const char *pname;
528 const ASN1_PRIMITIVE_FUNCS *pf;
529 pf = it->funcs;
530 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
531 return 0;
532 if (pf && pf->prim_print)
533 return pf->prim_print(out, fld, it, indent, pctx);
534 str = (ASN1_STRING *)*fld;
535 if (it->itype == ASN1_ITYPE_MSTRING)
536 utype = str->type & ~V_ASN1_NEG;
537 else
538 utype = it->utype;
539 if (utype == V_ASN1_ANY)
540 {
541 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
542 utype = atype->type;
543 fld = &atype->value.asn1_value;
544 str = (ASN1_STRING *)*fld;
545 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
546 pname = NULL;
547 else
548 pname = ASN1_tag2str(utype);
549 }
550 else
551 {
552 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
553 pname = ASN1_tag2str(utype);
554 else
555 pname = NULL;
556 }
557
558 if (utype == V_ASN1_NULL)
559 {
560 if (BIO_puts(out, "NULL\n") <= 0)
561 return 0;
562 return 1;
563 }
564
565 if (pname)
566 {
567 if (BIO_puts(out, pname) <= 0)
568 return 0;
569 if (BIO_puts(out, ":") <= 0)
570 return 0;
571 }
572
573 switch (utype)
574 {
575 case V_ASN1_BOOLEAN:
576 {
577 int bool = *(int *)fld;
578 if (bool == -1)
579 bool = it->size;
580 ret = asn1_print_boolean_ctx(out, bool, pctx);
581 }
582 break;
583
584 case V_ASN1_INTEGER:
585 case V_ASN1_ENUMERATED:
586 ret = asn1_print_integer_ctx(out, str, pctx);
587 break;
588
589 case V_ASN1_UTCTIME:
590 ret = ASN1_UTCTIME_print(out, str);
591 break;
592
593 case V_ASN1_GENERALIZEDTIME:
594 ret = ASN1_GENERALIZEDTIME_print(out, str);
595 break;
596
597 case V_ASN1_OBJECT:
598 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
599 break;
600
601 case V_ASN1_OCTET_STRING:
602 case V_ASN1_BIT_STRING:
603 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
604 needlf = 0;
605 break;
606
607 case V_ASN1_SEQUENCE:
608 case V_ASN1_SET:
609 case V_ASN1_OTHER:
610 if (BIO_puts(out, "\n") <= 0)
611 return 0;
612 if (ASN1_parse_dump(out, str->data, str->length,
613 indent, 0) <= 0)
614 ret = 0;
615 needlf = 0;
616 break;
617
618 default:
619 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
620
621 }
622 if (!ret)
623 return 0;
624 if (needlf && BIO_puts(out, "\n") <= 0)
625 return 0;
626 return 1;
627 }
diff --git a/src/lib/libcrypto/asn1/tasn_typ.c b/src/lib/libcrypto/asn1/tasn_typ.c
deleted file mode 100644
index 6fb1c372da..0000000000
--- a/src/lib/libcrypto/asn1/tasn_typ.c
+++ /dev/null
@@ -1,148 +0,0 @@
1/* tasn_typ.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58#include <stdio.h>
59#include <openssl/asn1.h>
60#include <openssl/asn1t.h>
61
62/* Declarations for string types */
63
64
65IMPLEMENT_ASN1_TYPE(ASN1_INTEGER)
66IMPLEMENT_ASN1_FUNCTIONS(ASN1_INTEGER)
67
68IMPLEMENT_ASN1_TYPE(ASN1_ENUMERATED)
69IMPLEMENT_ASN1_FUNCTIONS(ASN1_ENUMERATED)
70
71IMPLEMENT_ASN1_TYPE(ASN1_BIT_STRING)
72IMPLEMENT_ASN1_FUNCTIONS(ASN1_BIT_STRING)
73
74IMPLEMENT_ASN1_TYPE(ASN1_OCTET_STRING)
75IMPLEMENT_ASN1_FUNCTIONS(ASN1_OCTET_STRING)
76
77IMPLEMENT_ASN1_TYPE(ASN1_NULL)
78IMPLEMENT_ASN1_FUNCTIONS(ASN1_NULL)
79
80IMPLEMENT_ASN1_TYPE(ASN1_OBJECT)
81
82IMPLEMENT_ASN1_TYPE(ASN1_UTF8STRING)
83IMPLEMENT_ASN1_FUNCTIONS(ASN1_UTF8STRING)
84
85IMPLEMENT_ASN1_TYPE(ASN1_PRINTABLESTRING)
86IMPLEMENT_ASN1_FUNCTIONS(ASN1_PRINTABLESTRING)
87
88IMPLEMENT_ASN1_TYPE(ASN1_T61STRING)
89IMPLEMENT_ASN1_FUNCTIONS(ASN1_T61STRING)
90
91IMPLEMENT_ASN1_TYPE(ASN1_IA5STRING)
92IMPLEMENT_ASN1_FUNCTIONS(ASN1_IA5STRING)
93
94IMPLEMENT_ASN1_TYPE(ASN1_GENERALSTRING)
95IMPLEMENT_ASN1_FUNCTIONS(ASN1_GENERALSTRING)
96
97IMPLEMENT_ASN1_TYPE(ASN1_UTCTIME)
98IMPLEMENT_ASN1_FUNCTIONS(ASN1_UTCTIME)
99
100IMPLEMENT_ASN1_TYPE(ASN1_GENERALIZEDTIME)
101IMPLEMENT_ASN1_FUNCTIONS(ASN1_GENERALIZEDTIME)
102
103IMPLEMENT_ASN1_TYPE(ASN1_VISIBLESTRING)
104IMPLEMENT_ASN1_FUNCTIONS(ASN1_VISIBLESTRING)
105
106IMPLEMENT_ASN1_TYPE(ASN1_UNIVERSALSTRING)
107IMPLEMENT_ASN1_FUNCTIONS(ASN1_UNIVERSALSTRING)
108
109IMPLEMENT_ASN1_TYPE(ASN1_BMPSTRING)
110IMPLEMENT_ASN1_FUNCTIONS(ASN1_BMPSTRING)
111
112IMPLEMENT_ASN1_TYPE(ASN1_ANY)
113
114/* Just swallow an ASN1_SEQUENCE in an ASN1_STRING */
115IMPLEMENT_ASN1_TYPE(ASN1_SEQUENCE)
116
117IMPLEMENT_ASN1_FUNCTIONS_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
118
119/* Multistring types */
120
121IMPLEMENT_ASN1_MSTRING(ASN1_PRINTABLE, B_ASN1_PRINTABLE)
122IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE)
123
124IMPLEMENT_ASN1_MSTRING(DISPLAYTEXT, B_ASN1_DISPLAYTEXT)
125IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, DISPLAYTEXT)
126
127IMPLEMENT_ASN1_MSTRING(DIRECTORYSTRING, B_ASN1_DIRECTORYSTRING)
128IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING)
129
130/* Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE */
131IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, -1)
132IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, 1)
133IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
134
135/* Special, OCTET STRING with indefinite length constructed support */
136
137IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF)
138
139ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
140 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
141ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
142
143ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) =
144 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY)
145ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY)
146
147IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
148IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SET_ANY, ASN1_SET_ANY)
diff --git a/src/lib/libcrypto/asn1/tasn_utl.c b/src/lib/libcrypto/asn1/tasn_utl.c
deleted file mode 100644
index ca9ec7a32f..0000000000
--- a/src/lib/libcrypto/asn1/tasn_utl.c
+++ /dev/null
@@ -1,279 +0,0 @@
1/* tasn_utl.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <string.h>
62#include <openssl/asn1.h>
63#include <openssl/asn1t.h>
64#include <openssl/objects.h>
65#include <openssl/err.h>
66
67/* Utility functions for manipulating fields and offsets */
68
69/* Add 'offset' to 'addr' */
70#define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
71
72/* Given an ASN1_ITEM CHOICE type return
73 * the selector value
74 */
75
76int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
77 {
78 int *sel = offset2ptr(*pval, it->utype);
79 return *sel;
80 }
81
82/* Given an ASN1_ITEM CHOICE type set
83 * the selector value, return old value.
84 */
85
86int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
87 {
88 int *sel, ret;
89 sel = offset2ptr(*pval, it->utype);
90 ret = *sel;
91 *sel = value;
92 return ret;
93 }
94
95/* Do reference counting. The value 'op' decides what to do.
96 * if it is +1 then the count is incremented. If op is 0 count is
97 * set to 1. If op is -1 count is decremented and the return value
98 * is the current refrence count or 0 if no reference count exists.
99 */
100
101int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
102 {
103 const ASN1_AUX *aux;
104 int *lck, ret;
105 if ((it->itype != ASN1_ITYPE_SEQUENCE)
106 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
107 return 0;
108 aux = it->funcs;
109 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
110 return 0;
111 lck = offset2ptr(*pval, aux->ref_offset);
112 if (op == 0)
113 {
114 *lck = 1;
115 return 1;
116 }
117 ret = CRYPTO_add(lck, op, aux->ref_lock);
118#ifdef REF_PRINT
119 fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
120#endif
121#ifdef REF_CHECK
122 if (ret < 0)
123 fprintf(stderr, "%s, bad reference count\n", it->sname);
124#endif
125 return ret;
126 }
127
128static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
129 {
130 const ASN1_AUX *aux;
131 if (!pval || !*pval)
132 return NULL;
133 aux = it->funcs;
134 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
135 return NULL;
136 return offset2ptr(*pval, aux->enc_offset);
137 }
138
139void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
140 {
141 ASN1_ENCODING *enc;
142 enc = asn1_get_enc_ptr(pval, it);
143 if (enc)
144 {
145 enc->enc = NULL;
146 enc->len = 0;
147 enc->modified = 1;
148 }
149 }
150
151void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
152 {
153 ASN1_ENCODING *enc;
154 enc = asn1_get_enc_ptr(pval, it);
155 if (enc)
156 {
157 if (enc->enc)
158 OPENSSL_free(enc->enc);
159 enc->enc = NULL;
160 enc->len = 0;
161 enc->modified = 1;
162 }
163 }
164
165int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
166 const ASN1_ITEM *it)
167 {
168 ASN1_ENCODING *enc;
169 enc = asn1_get_enc_ptr(pval, it);
170 if (!enc)
171 return 1;
172
173 if (enc->enc)
174 OPENSSL_free(enc->enc);
175 enc->enc = OPENSSL_malloc(inlen);
176 if (!enc->enc)
177 return 0;
178 memcpy(enc->enc, in, inlen);
179 enc->len = inlen;
180 enc->modified = 0;
181
182 return 1;
183 }
184
185int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
186 const ASN1_ITEM *it)
187 {
188 ASN1_ENCODING *enc;
189 enc = asn1_get_enc_ptr(pval, it);
190 if (!enc || enc->modified)
191 return 0;
192 if (out)
193 {
194 memcpy(*out, enc->enc, enc->len);
195 *out += enc->len;
196 }
197 if (len)
198 *len = enc->len;
199 return 1;
200 }
201
202/* Given an ASN1_TEMPLATE get a pointer to a field */
203ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
204 {
205 ASN1_VALUE **pvaltmp;
206 if (tt->flags & ASN1_TFLG_COMBINE)
207 return pval;
208 pvaltmp = offset2ptr(*pval, tt->offset);
209 /* NOTE for BOOLEAN types the field is just a plain
210 * int so we can't return int **, so settle for
211 * (int *).
212 */
213 return pvaltmp;
214 }
215
216/* Handle ANY DEFINED BY template, find the selector, look up
217 * the relevant ASN1_TEMPLATE in the table and return it.
218 */
219
220const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
221 int nullerr)
222 {
223 const ASN1_ADB *adb;
224 const ASN1_ADB_TABLE *atbl;
225 long selector;
226 ASN1_VALUE **sfld;
227 int i;
228 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
229 return tt;
230
231 /* Else ANY DEFINED BY ... get the table */
232 adb = ASN1_ADB_ptr(tt->item);
233
234 /* Get the selector field */
235 sfld = offset2ptr(*pval, adb->offset);
236
237 /* Check if NULL */
238 if (!sfld)
239 {
240 if (!adb->null_tt)
241 goto err;
242 return adb->null_tt;
243 }
244
245 /* Convert type to a long:
246 * NB: don't check for NID_undef here because it
247 * might be a legitimate value in the table
248 */
249 if (tt->flags & ASN1_TFLG_ADB_OID)
250 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
251 else
252 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
253
254 /* Try to find matching entry in table
255 * Maybe should check application types first to
256 * allow application override? Might also be useful
257 * to have a flag which indicates table is sorted and
258 * we can do a binary search. For now stick to a
259 * linear search.
260 */
261
262 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
263 if (atbl->value == selector)
264 return &atbl->tt;
265
266 /* FIXME: need to search application table too */
267
268 /* No match, return default type */
269 if (!adb->default_tt)
270 goto err;
271 return adb->default_tt;
272
273 err:
274 /* FIXME: should log the value or OID of unsupported type */
275 if (nullerr)
276 ASN1err(ASN1_F_ASN1_DO_ADB,
277 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
278 return NULL;
279 }
diff --git a/src/lib/libcrypto/asn1/x_algor.c b/src/lib/libcrypto/asn1/x_algor.c
deleted file mode 100644
index 99e53429b7..0000000000
--- a/src/lib/libcrypto/asn1/x_algor.c
+++ /dev/null
@@ -1,130 +0,0 @@
1/* x_algor.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stddef.h>
60#include <openssl/x509.h>
61#include <openssl/asn1.h>
62#include <openssl/asn1t.h>
63
64ASN1_SEQUENCE(X509_ALGOR) = {
65 ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
66 ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
67} ASN1_SEQUENCE_END(X509_ALGOR)
68
69ASN1_ITEM_TEMPLATE(X509_ALGORS) =
70 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
71ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
72
73IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
74IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
75IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
76
77IMPLEMENT_STACK_OF(X509_ALGOR)
78IMPLEMENT_ASN1_SET_OF(X509_ALGOR)
79
80int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
81 {
82 if (!alg)
83 return 0;
84 if (ptype != V_ASN1_UNDEF)
85 {
86 if (alg->parameter == NULL)
87 alg->parameter = ASN1_TYPE_new();
88 if (alg->parameter == NULL)
89 return 0;
90 }
91 if (alg)
92 {
93 if (alg->algorithm)
94 ASN1_OBJECT_free(alg->algorithm);
95 alg->algorithm = aobj;
96 }
97 if (ptype == 0)
98 return 1;
99 if (ptype == V_ASN1_UNDEF)
100 {
101 if (alg->parameter)
102 {
103 ASN1_TYPE_free(alg->parameter);
104 alg->parameter = NULL;
105 }
106 }
107 else
108 ASN1_TYPE_set(alg->parameter, ptype, pval);
109 return 1;
110 }
111
112void X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, void **ppval,
113 X509_ALGOR *algor)
114 {
115 if (paobj)
116 *paobj = algor->algorithm;
117 if (pptype)
118 {
119 if (algor->parameter == NULL)
120 {
121 *pptype = V_ASN1_UNDEF;
122 return;
123 }
124 else
125 *pptype = algor->parameter->type;
126 if (ppval)
127 *ppval = algor->parameter->value.ptr;
128 }
129 }
130
diff --git a/src/lib/libcrypto/asn1/x_attrib.c b/src/lib/libcrypto/asn1/x_attrib.c
deleted file mode 100644
index 1e3713f18f..0000000000
--- a/src/lib/libcrypto/asn1/x_attrib.c
+++ /dev/null
@@ -1,118 +0,0 @@
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 <openssl/objects.h>
62#include <openssl/asn1t.h>
63#include <openssl/x509.h>
64
65/* X509_ATTRIBUTE: this has the following form:
66 *
67 * typedef struct x509_attributes_st
68 * {
69 * ASN1_OBJECT *object;
70 * int single;
71 * union {
72 * char *ptr;
73 * STACK_OF(ASN1_TYPE) *set;
74 * ASN1_TYPE *single;
75 * } value;
76 * } X509_ATTRIBUTE;
77 *
78 * this needs some extra thought because the CHOICE type is
79 * merged with the main structure and because the value can
80 * be anything at all we *must* try the SET OF first because
81 * the ASN1_ANY type will swallow anything including the whole
82 * SET OF structure.
83 */
84
85ASN1_CHOICE(X509_ATTRIBUTE_SET) = {
86 ASN1_SET_OF(X509_ATTRIBUTE, value.set, ASN1_ANY),
87 ASN1_SIMPLE(X509_ATTRIBUTE, value.single, ASN1_ANY)
88} ASN1_CHOICE_END_selector(X509_ATTRIBUTE, X509_ATTRIBUTE_SET, single)
89
90ASN1_SEQUENCE(X509_ATTRIBUTE) = {
91 ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
92 /* CHOICE type merged with parent */
93 ASN1_EX_COMBINE(0, 0, X509_ATTRIBUTE_SET)
94} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
95
96IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
97IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
98
99X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
100 {
101 X509_ATTRIBUTE *ret=NULL;
102 ASN1_TYPE *val=NULL;
103
104 if ((ret=X509_ATTRIBUTE_new()) == NULL)
105 return(NULL);
106 ret->object=OBJ_nid2obj(nid);
107 ret->single=0;
108 if ((ret->value.set=sk_ASN1_TYPE_new_null()) == NULL) goto err;
109 if ((val=ASN1_TYPE_new()) == NULL) goto err;
110 if (!sk_ASN1_TYPE_push(ret->value.set,val)) goto err;
111
112 ASN1_TYPE_set(val,atrtype,value);
113 return(ret);
114err:
115 if (ret != NULL) X509_ATTRIBUTE_free(ret);
116 if (val != NULL) ASN1_TYPE_free(val);
117 return(NULL);
118 }
diff --git a/src/lib/libcrypto/asn1/x_bignum.c b/src/lib/libcrypto/asn1/x_bignum.c
deleted file mode 100644
index 9cf3204a1b..0000000000
--- a/src/lib/libcrypto/asn1/x_bignum.c
+++ /dev/null
@@ -1,139 +0,0 @@
1/* x_bignum.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1t.h>
62#include <openssl/bn.h>
63
64/* Custom primitive type for BIGNUM handling. This reads in an ASN1_INTEGER as a
65 * BIGNUM directly. Currently it ignores the sign which isn't a problem since all
66 * BIGNUMs used are non negative and anything that looks negative is normally due
67 * to an encoding error.
68 */
69
70#define BN_SENSITIVE 1
71
72static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
73static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
74
75static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
76static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
77
78static ASN1_PRIMITIVE_FUNCS bignum_pf = {
79 NULL, 0,
80 bn_new,
81 bn_free,
82 0,
83 bn_c2i,
84 bn_i2c
85};
86
87ASN1_ITEM_start(BIGNUM)
88 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"
89ASN1_ITEM_end(BIGNUM)
90
91ASN1_ITEM_start(CBIGNUM)
92 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, BN_SENSITIVE, "BIGNUM"
93ASN1_ITEM_end(CBIGNUM)
94
95static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
96{
97 *pval = (ASN1_VALUE *)BN_new();
98 if(*pval) return 1;
99 else return 0;
100}
101
102static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
103{
104 if(!*pval) return;
105 if(it->size & BN_SENSITIVE) BN_clear_free((BIGNUM *)*pval);
106 else BN_free((BIGNUM *)*pval);
107 *pval = NULL;
108}
109
110static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it)
111{
112 BIGNUM *bn;
113 int pad;
114 if(!*pval) return -1;
115 bn = (BIGNUM *)*pval;
116 /* If MSB set in an octet we need a padding byte */
117 if(BN_num_bits(bn) & 0x7) pad = 0;
118 else pad = 1;
119 if(cont) {
120 if(pad) *cont++ = 0;
121 BN_bn2bin(bn, cont);
122 }
123 return pad + BN_num_bytes(bn);
124}
125
126static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
127 int utype, char *free_cont, const ASN1_ITEM *it)
128{
129 BIGNUM *bn;
130 if(!*pval) bn_new(pval, it);
131 bn = (BIGNUM *)*pval;
132 if(!BN_bin2bn(cont, len, bn)) {
133 bn_free(pval, it);
134 return 0;
135 }
136 return 1;
137}
138
139
diff --git a/src/lib/libcrypto/asn1/x_crl.c b/src/lib/libcrypto/asn1/x_crl.c
deleted file mode 100644
index c51c690ba9..0000000000
--- a/src/lib/libcrypto/asn1/x_crl.c
+++ /dev/null
@@ -1,527 +0,0 @@
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_locl.h"
62#include <openssl/asn1t.h>
63#include <openssl/x509.h>
64#include <openssl/x509v3.h>
65
66static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
67 const X509_REVOKED * const *b);
68static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
69
70ASN1_SEQUENCE(X509_REVOKED) = {
71 ASN1_SIMPLE(X509_REVOKED,serialNumber, ASN1_INTEGER),
72 ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
73 ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
74} ASN1_SEQUENCE_END(X509_REVOKED)
75
76static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
77static int def_crl_lookup(X509_CRL *crl,
78 X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer);
79
80static X509_CRL_METHOD int_crl_meth =
81 {
82 0,
83 0,0,
84 def_crl_lookup,
85 def_crl_verify
86 };
87
88static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
89
90/* The X509_CRL_INFO structure needs a bit of customisation.
91 * Since we cache the original encoding the signature wont be affected by
92 * reordering of the revoked field.
93 */
94static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
95 void *exarg)
96{
97 X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
98
99 if(!a || !a->revoked) return 1;
100 switch(operation) {
101 /* Just set cmp function here. We don't sort because that
102 * would affect the output of X509_CRL_print().
103 */
104 case ASN1_OP_D2I_POST:
105 (void)sk_X509_REVOKED_set_cmp_func(a->revoked,X509_REVOKED_cmp);
106 break;
107 }
108 return 1;
109}
110
111
112ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
113 ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
114 ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
115 ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
116 ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
117 ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
118 ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
119 ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
120} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
121
122/* Set CRL entry issuer according to CRL certificate issuer extension.
123 * Check for unhandled critical CRL entry extensions.
124 */
125
126static int crl_set_issuers(X509_CRL *crl)
127 {
128
129 int i, j;
130 GENERAL_NAMES *gens, *gtmp;
131 STACK_OF(X509_REVOKED) *revoked;
132
133 revoked = X509_CRL_get_REVOKED(crl);
134
135 gens = NULL;
136 for (i = 0; i < sk_X509_REVOKED_num(revoked); i++)
137 {
138 X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
139 STACK_OF(X509_EXTENSION) *exts;
140 ASN1_ENUMERATED *reason;
141 X509_EXTENSION *ext;
142 gtmp = X509_REVOKED_get_ext_d2i(rev,
143 NID_certificate_issuer,
144 &j, NULL);
145 if (!gtmp && (j != -1))
146 {
147 crl->flags |= EXFLAG_INVALID;
148 return 1;
149 }
150
151 if (gtmp)
152 {
153 gens = gtmp;
154 if (!crl->issuers)
155 {
156 crl->issuers = sk_GENERAL_NAMES_new_null();
157 if (!crl->issuers)
158 return 0;
159 }
160 if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
161 return 0;
162 }
163 rev->issuer = gens;
164
165 reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason,
166 &j, NULL);
167 if (!reason && (j != -1))
168 {
169 crl->flags |= EXFLAG_INVALID;
170 return 1;
171 }
172
173 if (reason)
174 {
175 rev->reason = ASN1_ENUMERATED_get(reason);
176 ASN1_ENUMERATED_free(reason);
177 }
178 else
179 rev->reason = CRL_REASON_NONE;
180
181 /* Check for critical CRL entry extensions */
182
183 exts = rev->extensions;
184
185 for (j = 0; j < sk_X509_EXTENSION_num(exts); j++)
186 {
187 ext = sk_X509_EXTENSION_value(exts, j);
188 if (ext->critical > 0)
189 {
190 if (OBJ_obj2nid(ext->object) ==
191 NID_certificate_issuer)
192 continue;
193 crl->flags |= EXFLAG_CRITICAL;
194 break;
195 }
196 }
197
198
199 }
200
201 return 1;
202
203 }
204
205/* The X509_CRL structure needs a bit of customisation. Cache some extensions
206 * and hash of the whole CRL.
207 */
208static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
209 void *exarg)
210 {
211 X509_CRL *crl = (X509_CRL *)*pval;
212 STACK_OF(X509_EXTENSION) *exts;
213 X509_EXTENSION *ext;
214 int idx;
215
216 switch(operation)
217 {
218 case ASN1_OP_NEW_POST:
219 crl->idp = NULL;
220 crl->akid = NULL;
221 crl->flags = 0;
222 crl->idp_flags = 0;
223 crl->idp_reasons = CRLDP_ALL_REASONS;
224 crl->meth = default_crl_method;
225 crl->meth_data = NULL;
226 crl->issuers = NULL;
227 crl->crl_number = NULL;
228 crl->base_crl_number = NULL;
229 break;
230
231 case ASN1_OP_D2I_POST:
232#ifndef OPENSSL_NO_SHA
233 X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
234#endif
235 crl->idp = X509_CRL_get_ext_d2i(crl,
236 NID_issuing_distribution_point, NULL, NULL);
237 if (crl->idp)
238 setup_idp(crl, crl->idp);
239
240 crl->akid = X509_CRL_get_ext_d2i(crl,
241 NID_authority_key_identifier, NULL, NULL);
242
243 crl->crl_number = X509_CRL_get_ext_d2i(crl,
244 NID_crl_number, NULL, NULL);
245
246 crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
247 NID_delta_crl, NULL, NULL);
248 /* Delta CRLs must have CRL number */
249 if (crl->base_crl_number && !crl->crl_number)
250 crl->flags |= EXFLAG_INVALID;
251
252 /* See if we have any unhandled critical CRL extensions and
253 * indicate this in a flag. We only currently handle IDP so
254 * anything else critical sets the flag.
255 *
256 * This code accesses the X509_CRL structure directly:
257 * applications shouldn't do this.
258 */
259
260 exts = crl->crl->extensions;
261
262 for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
263 {
264 int nid;
265 ext = sk_X509_EXTENSION_value(exts, idx);
266 nid = OBJ_obj2nid(ext->object);
267 if (nid == NID_freshest_crl)
268 crl->flags |= EXFLAG_FRESHEST;
269 if (ext->critical > 0)
270 {
271 /* We handle IDP and deltas */
272 if ((nid == NID_issuing_distribution_point)
273 || (nid == NID_delta_crl))
274 break;;
275 crl->flags |= EXFLAG_CRITICAL;
276 break;
277 }
278 }
279
280
281 if (!crl_set_issuers(crl))
282 return 0;
283
284 if (crl->meth->crl_init)
285 {
286 if (crl->meth->crl_init(crl) == 0)
287 return 0;
288 }
289 break;
290
291 case ASN1_OP_FREE_POST:
292 if (crl->meth->crl_free)
293 {
294 if (!crl->meth->crl_free(crl))
295 return 0;
296 }
297 if (crl->akid)
298 AUTHORITY_KEYID_free(crl->akid);
299 if (crl->idp)
300 ISSUING_DIST_POINT_free(crl->idp);
301 ASN1_INTEGER_free(crl->crl_number);
302 ASN1_INTEGER_free(crl->base_crl_number);
303 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
304 break;
305 }
306 return 1;
307 }
308
309/* Convert IDP into a more convenient form */
310
311static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
312 {
313 int idp_only = 0;
314 /* Set various flags according to IDP */
315 crl->idp_flags |= IDP_PRESENT;
316 if (idp->onlyuser > 0)
317 {
318 idp_only++;
319 crl->idp_flags |= IDP_ONLYUSER;
320 }
321 if (idp->onlyCA > 0)
322 {
323 idp_only++;
324 crl->idp_flags |= IDP_ONLYCA;
325 }
326 if (idp->onlyattr > 0)
327 {
328 idp_only++;
329 crl->idp_flags |= IDP_ONLYATTR;
330 }
331
332 if (idp_only > 1)
333 crl->idp_flags |= IDP_INVALID;
334
335 if (idp->indirectCRL > 0)
336 crl->idp_flags |= IDP_INDIRECT;
337
338 if (idp->onlysomereasons)
339 {
340 crl->idp_flags |= IDP_REASONS;
341 if (idp->onlysomereasons->length > 0)
342 crl->idp_reasons = idp->onlysomereasons->data[0];
343 if (idp->onlysomereasons->length > 1)
344 crl->idp_reasons |=
345 (idp->onlysomereasons->data[1] << 8);
346 crl->idp_reasons &= CRLDP_ALL_REASONS;
347 }
348
349 DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
350 }
351
352ASN1_SEQUENCE_ref(X509_CRL, crl_cb, CRYPTO_LOCK_X509_CRL) = {
353 ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
354 ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
355 ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
356} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
357
358IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
359IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
360IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
361IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
362
363static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
364 const X509_REVOKED * const *b)
365 {
366 return(ASN1_STRING_cmp(
367 (ASN1_STRING *)(*a)->serialNumber,
368 (ASN1_STRING *)(*b)->serialNumber));
369 }
370
371int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
372{
373 X509_CRL_INFO *inf;
374 inf = crl->crl;
375 if(!inf->revoked)
376 inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
377 if(!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
378 ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
379 return 0;
380 }
381 inf->enc.modified = 1;
382 return 1;
383}
384
385int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
386 {
387 if (crl->meth->crl_verify)
388 return crl->meth->crl_verify(crl, r);
389 return 0;
390 }
391
392int X509_CRL_get0_by_serial(X509_CRL *crl,
393 X509_REVOKED **ret, ASN1_INTEGER *serial)
394 {
395 if (crl->meth->crl_lookup)
396 return crl->meth->crl_lookup(crl, ret, serial, NULL);
397 return 0;
398 }
399
400int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
401 {
402 if (crl->meth->crl_lookup)
403 return crl->meth->crl_lookup(crl, ret,
404 X509_get_serialNumber(x),
405 X509_get_issuer_name(x));
406 return 0;
407 }
408
409static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
410 {
411 return(ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
412 crl->sig_alg, crl->signature,crl->crl,r));
413 }
414
415static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
416 X509_REVOKED *rev)
417 {
418 int i;
419
420 if (!rev->issuer)
421 {
422 if (!nm)
423 return 1;
424 if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
425 return 1;
426 return 0;
427 }
428
429 if (!nm)
430 nm = X509_CRL_get_issuer(crl);
431
432 for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++)
433 {
434 GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
435 if (gen->type != GEN_DIRNAME)
436 continue;
437 if (!X509_NAME_cmp(nm, gen->d.directoryName))
438 return 1;
439 }
440 return 0;
441
442 }
443
444static int def_crl_lookup(X509_CRL *crl,
445 X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer)
446 {
447 X509_REVOKED rtmp, *rev;
448 int idx;
449 rtmp.serialNumber = serial;
450 /* Sort revoked into serial number order if not already sorted.
451 * Do this under a lock to avoid race condition.
452 */
453 if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
454 {
455 CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
456 sk_X509_REVOKED_sort(crl->crl->revoked);
457 CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
458 }
459 idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
460 if(idx < 0)
461 return 0;
462 /* Need to look for matching name */
463 for(;idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++)
464 {
465 rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
466 if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
467 return 0;
468 if (crl_revoked_issuer_match(crl, issuer, rev))
469 {
470 if (ret)
471 *ret = rev;
472 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
473 return 2;
474 return 1;
475 }
476 }
477 return 0;
478 }
479
480void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
481 {
482 if (meth == NULL)
483 default_crl_method = &int_crl_meth;
484 else
485 default_crl_method = meth;
486 }
487
488X509_CRL_METHOD *X509_CRL_METHOD_new(
489 int (*crl_init)(X509_CRL *crl),
490 int (*crl_free)(X509_CRL *crl),
491 int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
492 ASN1_INTEGER *ser, X509_NAME *issuer),
493 int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk))
494 {
495 X509_CRL_METHOD *m;
496 m = OPENSSL_malloc(sizeof(X509_CRL_METHOD));
497 if (!m)
498 return NULL;
499 m->crl_init = crl_init;
500 m->crl_free = crl_free;
501 m->crl_lookup = crl_lookup;
502 m->crl_verify = crl_verify;
503 m->flags = X509_CRL_METHOD_DYNAMIC;
504 return m;
505 }
506
507void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
508 {
509 if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
510 return;
511 OPENSSL_free(m);
512 }
513
514void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
515 {
516 crl->meth_data = dat;
517 }
518
519void *X509_CRL_get_meth_data(X509_CRL *crl)
520 {
521 return crl->meth_data;
522 }
523
524IMPLEMENT_STACK_OF(X509_REVOKED)
525IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
526IMPLEMENT_STACK_OF(X509_CRL)
527IMPLEMENT_ASN1_SET_OF(X509_CRL)
diff --git a/src/lib/libcrypto/asn1/x_exten.c b/src/lib/libcrypto/asn1/x_exten.c
deleted file mode 100644
index 3a21239926..0000000000
--- a/src/lib/libcrypto/asn1/x_exten.c
+++ /dev/null
@@ -1,76 +0,0 @@
1/* x_exten.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stddef.h>
60#include <openssl/x509.h>
61#include <openssl/asn1.h>
62#include <openssl/asn1t.h>
63
64ASN1_SEQUENCE(X509_EXTENSION) = {
65 ASN1_SIMPLE(X509_EXTENSION, object, ASN1_OBJECT),
66 ASN1_OPT(X509_EXTENSION, critical, ASN1_BOOLEAN),
67 ASN1_SIMPLE(X509_EXTENSION, value, ASN1_OCTET_STRING)
68} ASN1_SEQUENCE_END(X509_EXTENSION)
69
70ASN1_ITEM_TEMPLATE(X509_EXTENSIONS) =
71 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Extension, X509_EXTENSION)
72ASN1_ITEM_TEMPLATE_END(X509_EXTENSIONS)
73
74IMPLEMENT_ASN1_FUNCTIONS(X509_EXTENSION)
75IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_EXTENSIONS, X509_EXTENSIONS, X509_EXTENSIONS)
76IMPLEMENT_ASN1_DUP_FUNCTION(X509_EXTENSION)
diff --git a/src/lib/libcrypto/asn1/x_info.c b/src/lib/libcrypto/asn1/x_info.c
deleted file mode 100644
index d44f6cdb01..0000000000
--- a/src/lib/libcrypto/asn1/x_info.c
+++ /dev/null
@@ -1,114 +0,0 @@
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 <openssl/evp.h>
62#include <openssl/asn1.h>
63#include <openssl/x509.h>
64
65X509_INFO *X509_INFO_new(void)
66 {
67 X509_INFO *ret=NULL;
68
69 ret=(X509_INFO *)OPENSSL_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(X509_INFO *x)
88 {
89 int i;
90
91 if (x == NULL) return;
92
93 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_INFO);
94#ifdef REF_PRINT
95 REF_PRINT("X509_INFO",x);
96#endif
97 if (i > 0) return;
98#ifdef REF_CHECK
99 if (i < 0)
100 {
101 fprintf(stderr,"X509_INFO_free, bad reference count\n");
102 abort();
103 }
104#endif
105
106 if (x->x509 != NULL) X509_free(x->x509);
107 if (x->crl != NULL) X509_CRL_free(x->crl);
108 if (x->x_pkey != NULL) X509_PKEY_free(x->x_pkey);
109 if (x->enc_data != NULL) OPENSSL_free(x->enc_data);
110 OPENSSL_free(x);
111 }
112
113IMPLEMENT_STACK_OF(X509_INFO)
114
diff --git a/src/lib/libcrypto/asn1/x_long.c b/src/lib/libcrypto/asn1/x_long.c
deleted file mode 100644
index 75317418e1..0000000000
--- a/src/lib/libcrypto/asn1/x_long.c
+++ /dev/null
@@ -1,179 +0,0 @@
1/* x_long.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/asn1t.h>
62#include <openssl/bn.h>
63
64/* Custom primitive type for long handling. This converts between an ASN1_INTEGER
65 * and a long directly.
66 */
67
68
69static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
71
72static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
73static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
74static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
75
76static ASN1_PRIMITIVE_FUNCS long_pf = {
77 NULL, 0,
78 long_new,
79 long_free,
80 long_free, /* Clear should set to initial value */
81 long_c2i,
82 long_i2c,
83 long_print
84};
85
86ASN1_ITEM_start(LONG)
87 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
88ASN1_ITEM_end(LONG)
89
90ASN1_ITEM_start(ZLONG)
91 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
92ASN1_ITEM_end(ZLONG)
93
94static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
95{
96 *(long *)pval = it->size;
97 return 1;
98}
99
100static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
101{
102 *(long *)pval = it->size;
103}
104
105static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it)
106{
107 long ltmp;
108 unsigned long utmp;
109 int clen, pad, i;
110 /* this exists to bypass broken gcc optimization */
111 char *cp = (char *)pval;
112
113 /* use memcpy, because we may not be long aligned */
114 memcpy(&ltmp, cp, sizeof(long));
115
116 if(ltmp == it->size) return -1;
117 /* Convert the long to positive: we subtract one if negative so
118 * we can cleanly handle the padding if only the MSB of the leading
119 * octet is set.
120 */
121 if(ltmp < 0) utmp = -ltmp - 1;
122 else utmp = ltmp;
123 clen = BN_num_bits_word(utmp);
124 /* If MSB of leading octet set we need to pad */
125 if(!(clen & 0x7)) pad = 1;
126 else pad = 0;
127
128 /* Convert number of bits to number of octets */
129 clen = (clen + 7) >> 3;
130
131 if(cont) {
132 if(pad) *cont++ = (ltmp < 0) ? 0xff : 0;
133 for(i = clen - 1; i >= 0; i--) {
134 cont[i] = (unsigned char)(utmp & 0xff);
135 if(ltmp < 0) cont[i] ^= 0xff;
136 utmp >>= 8;
137 }
138 }
139 return clen + pad;
140}
141
142static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
143 int utype, char *free_cont, const ASN1_ITEM *it)
144{
145 int neg, i;
146 long ltmp;
147 unsigned long utmp = 0;
148 char *cp = (char *)pval;
149 if(len > (int)sizeof(long)) {
150 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
151 return 0;
152 }
153 /* Is it negative? */
154 if(len && (cont[0] & 0x80)) neg = 1;
155 else neg = 0;
156 utmp = 0;
157 for(i = 0; i < len; i++) {
158 utmp <<= 8;
159 if(neg) utmp |= cont[i] ^ 0xff;
160 else utmp |= cont[i];
161 }
162 ltmp = (long)utmp;
163 if(neg) {
164 ltmp++;
165 ltmp = -ltmp;
166 }
167 if(ltmp == it->size) {
168 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
169 return 0;
170 }
171 memcpy(cp, &ltmp, sizeof(long));
172 return 1;
173}
174
175static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
176 int indent, const ASN1_PCTX *pctx)
177 {
178 return BIO_printf(out, "%ld\n", *(long *)pval);
179 }
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c
deleted file mode 100644
index 49be08b4da..0000000000
--- a/src/lib/libcrypto/asn1/x_name.c
+++ /dev/null
@@ -1,520 +0,0 @@
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 <ctype.h>
61#include "cryptlib.h"
62#include <openssl/asn1t.h>
63#include <openssl/x509.h>
64#include "asn1_locl.h"
65
66typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
67DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
68
69static int x509_name_ex_d2i(ASN1_VALUE **val,
70 const unsigned char **in, long len,
71 const ASN1_ITEM *it,
72 int tag, int aclass, char opt, ASN1_TLC *ctx);
73
74static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
75 const ASN1_ITEM *it, int tag, int aclass);
76static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
77static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
78
79static int x509_name_encode(X509_NAME *a);
80static int x509_name_canon(X509_NAME *a);
81static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
82static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
83 unsigned char **in);
84
85
86static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
87 int indent,
88 const char *fname,
89 const ASN1_PCTX *pctx);
90
91ASN1_SEQUENCE(X509_NAME_ENTRY) = {
92 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
93 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
94} ASN1_SEQUENCE_END(X509_NAME_ENTRY)
95
96IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
97IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
98
99/* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
100 * so declare two template wrappers for this
101 */
102
103ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
104 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
105ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
106
107ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
108 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
109ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
110
111/* Normally that's where it would end: we'd have two nested STACK structures
112 * representing the ASN1. Unfortunately X509_NAME uses a completely different
113 * form and caches encodings so we have to process the internal form and convert
114 * to the external form.
115 */
116
117const ASN1_EXTERN_FUNCS x509_name_ff = {
118 NULL,
119 x509_name_ex_new,
120 x509_name_ex_free,
121 0, /* Default clear behaviour is OK */
122 x509_name_ex_d2i,
123 x509_name_ex_i2d,
124 x509_name_ex_print
125};
126
127IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
128
129IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
130IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
131
132static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
133{
134 X509_NAME *ret = NULL;
135 ret = OPENSSL_malloc(sizeof(X509_NAME));
136 if(!ret) goto memerr;
137 if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
138 goto memerr;
139 if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr;
140 ret->canon_enc = NULL;
141 ret->canon_enclen = 0;
142 ret->modified=1;
143 *val = (ASN1_VALUE *)ret;
144 return 1;
145
146 memerr:
147 ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
148 if (ret)
149 {
150 if (ret->entries)
151 sk_X509_NAME_ENTRY_free(ret->entries);
152 OPENSSL_free(ret);
153 }
154 return 0;
155}
156
157static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
158{
159 X509_NAME *a;
160 if(!pval || !*pval)
161 return;
162 a = (X509_NAME *)*pval;
163
164 BUF_MEM_free(a->bytes);
165 sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
166 if (a->canon_enc)
167 OPENSSL_free(a->canon_enc);
168 OPENSSL_free(a);
169 *pval = NULL;
170}
171
172static int x509_name_ex_d2i(ASN1_VALUE **val,
173 const unsigned char **in, long len, const ASN1_ITEM *it,
174 int tag, int aclass, char opt, ASN1_TLC *ctx)
175{
176 const unsigned char *p = *in, *q;
177 union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
178 ASN1_VALUE *a; } intname = {NULL};
179 union { X509_NAME *x; ASN1_VALUE *a; } nm = {NULL};
180 int i, j, ret;
181 STACK_OF(X509_NAME_ENTRY) *entries;
182 X509_NAME_ENTRY *entry;
183 q = p;
184
185 /* Get internal representation of Name */
186 ret = ASN1_item_ex_d2i(&intname.a,
187 &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
188 tag, aclass, opt, ctx);
189
190 if(ret <= 0) return ret;
191
192 if(*val) x509_name_ex_free(val, NULL);
193 if(!x509_name_ex_new(&nm.a, NULL)) goto err;
194 /* We've decoded it: now cache encoding */
195 if(!BUF_MEM_grow(nm.x->bytes, p - q)) goto err;
196 memcpy(nm.x->bytes->data, q, p - q);
197
198 /* Convert internal representation to X509_NAME structure */
199 for(i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
200 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
201 for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
202 entry = sk_X509_NAME_ENTRY_value(entries, j);
203 entry->set = i;
204 if(!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
205 goto err;
206 }
207 sk_X509_NAME_ENTRY_free(entries);
208 }
209 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
210 ret = x509_name_canon(nm.x);
211 if (!ret)
212 goto err;
213 nm.x->modified = 0;
214 *val = nm.a;
215 *in = p;
216 return ret;
217err:
218 if (nm.x != NULL)
219 X509_NAME_free(nm.x);
220 ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
221 return 0;
222}
223
224static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
225{
226 int ret;
227 X509_NAME *a = (X509_NAME *)*val;
228 if(a->modified) {
229 ret = x509_name_encode(a);
230 if(ret < 0)
231 return ret;
232 ret = x509_name_canon(a);
233 if(ret < 0)
234 return ret;
235 }
236 ret = a->bytes->length;
237 if(out != NULL) {
238 memcpy(*out,a->bytes->data,ret);
239 *out+=ret;
240 }
241 return ret;
242}
243
244static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
245 {
246 sk_X509_NAME_ENTRY_free(ne);
247 }
248
249static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
250 {
251 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
252 }
253
254static int x509_name_encode(X509_NAME *a)
255{
256 union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
257 ASN1_VALUE *a; } intname = {NULL};
258 int len;
259 unsigned char *p;
260 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
261 X509_NAME_ENTRY *entry;
262 int i, set = -1;
263 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
264 if(!intname.s) goto memerr;
265 for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
266 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
267 if(entry->set != set) {
268 entries = sk_X509_NAME_ENTRY_new_null();
269 if(!entries) goto memerr;
270 if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s,
271 entries))
272 goto memerr;
273 set = entry->set;
274 }
275 if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
276 }
277 len = ASN1_item_ex_i2d(&intname.a, NULL,
278 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
279 if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
280 p=(unsigned char *)a->bytes->data;
281 ASN1_item_ex_i2d(&intname.a,
282 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
283 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
284 local_sk_X509_NAME_ENTRY_free);
285 a->modified = 0;
286 return len;
287memerr:
288 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
289 local_sk_X509_NAME_ENTRY_free);
290 ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
291 return -1;
292}
293
294static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
295 int indent,
296 const char *fname,
297 const ASN1_PCTX *pctx)
298 {
299 if (X509_NAME_print_ex(out, (X509_NAME *)*pval,
300 indent, pctx->nm_flags) <= 0)
301 return 0;
302 return 2;
303 }
304
305/* This function generates the canonical encoding of the Name structure.
306 * In it all strings are converted to UTF8, leading, trailing and
307 * multiple spaces collapsed, converted to lower case and the leading
308 * SEQUENCE header removed.
309 *
310 * In future we could also normalize the UTF8 too.
311 *
312 * By doing this comparison of Name structures can be rapidly
313 * perfomed by just using memcmp() of the canonical encoding.
314 * By omitting the leading SEQUENCE name constraints of type
315 * dirName can also be checked with a simple memcmp().
316 */
317
318static int x509_name_canon(X509_NAME *a)
319 {
320 unsigned char *p;
321 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
322 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
323 X509_NAME_ENTRY *entry, *tmpentry = NULL;
324 int i, set = -1, ret = 0;
325
326 if (a->canon_enc)
327 {
328 OPENSSL_free(a->canon_enc);
329 a->canon_enc = NULL;
330 }
331 /* Special case: empty X509_NAME => null encoding */
332 if (sk_X509_NAME_ENTRY_num(a->entries) == 0)
333 {
334 a->canon_enclen = 0;
335 return 1;
336 }
337 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
338 if(!intname)
339 goto err;
340 for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++)
341 {
342 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
343 if(entry->set != set)
344 {
345 entries = sk_X509_NAME_ENTRY_new_null();
346 if(!entries)
347 goto err;
348 if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries))
349 goto err;
350 set = entry->set;
351 }
352 tmpentry = X509_NAME_ENTRY_new();
353 tmpentry->object = OBJ_dup(entry->object);
354 if (!asn1_string_canon(tmpentry->value, entry->value))
355 goto err;
356 if(!sk_X509_NAME_ENTRY_push(entries, tmpentry))
357 goto err;
358 tmpentry = NULL;
359 }
360
361 /* Finally generate encoding */
362
363 a->canon_enclen = i2d_name_canon(intname, NULL);
364
365 p = OPENSSL_malloc(a->canon_enclen);
366
367 if (!p)
368 goto err;
369
370 a->canon_enc = p;
371
372 i2d_name_canon(intname, &p);
373
374 ret = 1;
375
376 err:
377
378 if (tmpentry)
379 X509_NAME_ENTRY_free(tmpentry);
380 if (intname)
381 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
382 local_sk_X509_NAME_ENTRY_pop_free);
383 return ret;
384 }
385
386/* Bitmap of all the types of string that will be canonicalized. */
387
388#define ASN1_MASK_CANON \
389 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
390 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
391 | B_ASN1_VISIBLESTRING)
392
393
394static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
395 {
396 unsigned char *to, *from;
397 int len, i;
398
399 /* If type not in bitmask just copy string across */
400 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON))
401 {
402 out->type = in->type;
403 if (!ASN1_STRING_set(out, in->data, in->length))
404 return 0;
405 return 1;
406 }
407
408 out->type = V_ASN1_UTF8STRING;
409 out->length = ASN1_STRING_to_UTF8(&out->data, in);
410 if (out->length == -1)
411 return 0;
412
413 to = out->data;
414 from = to;
415
416 len = out->length;
417
418 /* Convert string in place to canonical form.
419 * Ultimately we may need to handle a wider range of characters
420 * but for now ignore anything with MSB set and rely on the
421 * isspace() and tolower() functions.
422 */
423
424 /* Ignore leading spaces */
425 while((len > 0) && !(*from & 0x80) && isspace(*from))
426 {
427 from++;
428 len--;
429 }
430
431 to = from + len - 1;
432
433 /* Ignore trailing spaces */
434 while ((len > 0) && !(*to & 0x80) && isspace(*to))
435 {
436 to--;
437 len--;
438 }
439
440 to = out->data;
441
442 i = 0;
443 while(i < len)
444 {
445 /* If MSB set just copy across */
446 if (*from & 0x80)
447 {
448 *to++ = *from++;
449 i++;
450 }
451 /* Collapse multiple spaces */
452 else if (isspace(*from))
453 {
454 /* Copy one space across */
455 *to++ = ' ';
456 /* Ignore subsequent spaces. Note: don't need to
457 * check len here because we know the last
458 * character is a non-space so we can't overflow.
459 */
460 do
461 {
462 from++;
463 i++;
464 }
465 while(!(*from & 0x80) && isspace(*from));
466 }
467 else
468 {
469 *to++ = tolower(*from);
470 from++;
471 i++;
472 }
473 }
474
475 out->length = to - out->data;
476
477 return 1;
478
479 }
480
481static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
482 unsigned char **in)
483 {
484 int i, len, ltmp;
485 ASN1_VALUE *v;
486 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
487
488 len = 0;
489 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++)
490 {
491 v = sk_ASN1_VALUE_value(intname, i);
492 ltmp = ASN1_item_ex_i2d(&v, in,
493 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
494 if (ltmp < 0)
495 return ltmp;
496 len += ltmp;
497 }
498 return len;
499 }
500
501int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
502 {
503 X509_NAME *in;
504
505 if (!xn || !name) return(0);
506
507 if (*xn != name)
508 {
509 in=X509_NAME_dup(name);
510 if (in != NULL)
511 {
512 X509_NAME_free(*xn);
513 *xn=in;
514 }
515 }
516 return(*xn != NULL);
517 }
518
519IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
520IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)
diff --git a/src/lib/libcrypto/asn1/x_nx509.c b/src/lib/libcrypto/asn1/x_nx509.c
deleted file mode 100644
index fbd9a22db3..0000000000
--- a/src/lib/libcrypto/asn1/x_nx509.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/* x_nx509.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stddef.h>
60#include <openssl/x509.h>
61#include <openssl/asn1.h>
62#include <openssl/asn1t.h>
63
64/* Old netscape certificate wrapper format */
65
66ASN1_SEQUENCE(NETSCAPE_X509) = {
67 ASN1_SIMPLE(NETSCAPE_X509, header, ASN1_OCTET_STRING),
68 ASN1_OPT(NETSCAPE_X509, cert, X509)
69} ASN1_SEQUENCE_END(NETSCAPE_X509)
70
71IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_X509)
72
diff --git a/src/lib/libcrypto/asn1/x_pkey.c b/src/lib/libcrypto/asn1/x_pkey.c
deleted file mode 100644
index 8453618426..0000000000
--- a/src/lib/libcrypto/asn1/x_pkey.c
+++ /dev/null
@@ -1,151 +0,0 @@
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 <openssl/evp.h>
62#include <openssl/objects.h>
63#include <openssl/asn1_mac.h>
64#include <openssl/x509.h>
65
66/* need to implement */
67int i2d_X509_PKEY(X509_PKEY *a, unsigned char **pp)
68 {
69 return(0);
70 }
71
72X509_PKEY *d2i_X509_PKEY(X509_PKEY **a, const unsigned char **pp, long length)
73 {
74 int i;
75 M_ASN1_D2I_vars(a,X509_PKEY *,X509_PKEY_new);
76
77 M_ASN1_D2I_Init();
78 M_ASN1_D2I_start_sequence();
79 M_ASN1_D2I_get_x(X509_ALGOR,ret->enc_algor,d2i_X509_ALGOR);
80 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,ret->enc_pkey,d2i_ASN1_OCTET_STRING);
81
82 ret->cipher.cipher=EVP_get_cipherbyname(
83 OBJ_nid2ln(OBJ_obj2nid(ret->enc_algor->algorithm)));
84 if (ret->cipher.cipher == NULL)
85 {
86 c.error=ASN1_R_UNSUPPORTED_CIPHER;
87 c.line=__LINE__;
88 goto err;
89 }
90 if (ret->enc_algor->parameter->type == V_ASN1_OCTET_STRING)
91 {
92 i=ret->enc_algor->parameter->value.octet_string->length;
93 if (i > EVP_MAX_IV_LENGTH)
94 {
95 c.error=ASN1_R_IV_TOO_LARGE;
96 c.line=__LINE__;
97 goto err;
98 }
99 memcpy(ret->cipher.iv,
100 ret->enc_algor->parameter->value.octet_string->data,i);
101 }
102 else
103 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
104 M_ASN1_D2I_Finish(a,X509_PKEY_free,ASN1_F_D2I_X509_PKEY);
105 }
106
107X509_PKEY *X509_PKEY_new(void)
108 {
109 X509_PKEY *ret=NULL;
110 ASN1_CTX c;
111
112 M_ASN1_New_Malloc(ret,X509_PKEY);
113 ret->version=0;
114 M_ASN1_New(ret->enc_algor,X509_ALGOR_new);
115 M_ASN1_New(ret->enc_pkey,M_ASN1_OCTET_STRING_new);
116 ret->dec_pkey=NULL;
117 ret->key_length=0;
118 ret->key_data=NULL;
119 ret->key_free=0;
120 ret->cipher.cipher=NULL;
121 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
122 ret->references=1;
123 return(ret);
124 M_ASN1_New_Error(ASN1_F_X509_PKEY_NEW);
125 }
126
127void X509_PKEY_free(X509_PKEY *x)
128 {
129 int i;
130
131 if (x == NULL) return;
132
133 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_PKEY);
134#ifdef REF_PRINT
135 REF_PRINT("X509_PKEY",x);
136#endif
137 if (i > 0) return;
138#ifdef REF_CHECK
139 if (i < 0)
140 {
141 fprintf(stderr,"X509_PKEY_free, bad reference count\n");
142 abort();
143 }
144#endif
145
146 if (x->enc_algor != NULL) X509_ALGOR_free(x->enc_algor);
147 if (x->enc_pkey != NULL) M_ASN1_OCTET_STRING_free(x->enc_pkey);
148 if (x->dec_pkey != NULL)EVP_PKEY_free(x->dec_pkey);
149 if ((x->key_data != NULL) && (x->key_free)) OPENSSL_free(x->key_data);
150 OPENSSL_free(x);
151 }
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
deleted file mode 100644
index d42b6a2c54..0000000000
--- a/src/lib/libcrypto/asn1/x_pubkey.c
+++ /dev/null
@@ -1,373 +0,0 @@
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 <openssl/asn1t.h>
62#include <openssl/x509.h>
63#include "asn1_locl.h"
64#ifndef OPENSSL_NO_RSA
65#include <openssl/rsa.h>
66#endif
67#ifndef OPENSSL_NO_DSA
68#include <openssl/dsa.h>
69#endif
70
71/* Minor tweak to operation: free up EVP_PKEY */
72static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
73 void *exarg)
74 {
75 if (operation == ASN1_OP_FREE_POST)
76 {
77 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
78 EVP_PKEY_free(pubkey->pkey);
79 }
80 return 1;
81 }
82
83ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
84 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
85 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
86} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
87
88IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
89
90int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
91 {
92 X509_PUBKEY *pk=NULL;
93
94 if (x == NULL) return(0);
95
96 if ((pk=X509_PUBKEY_new()) == NULL) goto error;
97
98 if (pkey->ameth)
99 {
100 if (pkey->ameth->pub_encode)
101 {
102 if (!pkey->ameth->pub_encode(pk, pkey))
103 {
104 X509err(X509_F_X509_PUBKEY_SET,
105 X509_R_PUBLIC_KEY_ENCODE_ERROR);
106 goto error;
107 }
108 }
109 else
110 {
111 X509err(X509_F_X509_PUBKEY_SET,
112 X509_R_METHOD_NOT_SUPPORTED);
113 goto error;
114 }
115 }
116 else
117 {
118 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
119 goto error;
120 }
121
122 if (*x != NULL)
123 X509_PUBKEY_free(*x);
124
125 *x=pk;
126
127 return 1;
128error:
129 if (pk != NULL) X509_PUBKEY_free(pk);
130 return 0;
131 }
132
133EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
134 {
135 EVP_PKEY *ret=NULL;
136
137 if (key == NULL) goto error;
138
139 if (key->pkey != NULL)
140 {
141 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
142 return key->pkey;
143 }
144
145 if (key->public_key == NULL) goto error;
146
147 if ((ret = EVP_PKEY_new()) == NULL)
148 {
149 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
150 goto error;
151 }
152
153 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm)))
154 {
155 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);
156 goto error;
157 }
158
159 if (ret->ameth->pub_decode)
160 {
161 if (!ret->ameth->pub_decode(ret, key))
162 {
163 X509err(X509_F_X509_PUBKEY_GET,
164 X509_R_PUBLIC_KEY_DECODE_ERROR);
165 goto error;
166 }
167 }
168 else
169 {
170 X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);
171 goto error;
172 }
173
174 key->pkey = ret;
175 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
176
177 return ret;
178
179 error:
180 if (ret != NULL)
181 EVP_PKEY_free(ret);
182 return(NULL);
183 }
184
185/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
186 * and encode or decode as X509_PUBKEY
187 */
188
189EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp,
190 long length)
191 {
192 X509_PUBKEY *xpk;
193 EVP_PKEY *pktmp;
194 xpk = d2i_X509_PUBKEY(NULL, pp, length);
195 if(!xpk) return NULL;
196 pktmp = X509_PUBKEY_get(xpk);
197 X509_PUBKEY_free(xpk);
198 if(!pktmp) return NULL;
199 if(a)
200 {
201 EVP_PKEY_free(*a);
202 *a = pktmp;
203 }
204 return pktmp;
205 }
206
207int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
208 {
209 X509_PUBKEY *xpk=NULL;
210 int ret;
211 if(!a) return 0;
212 if(!X509_PUBKEY_set(&xpk, a)) return 0;
213 ret = i2d_X509_PUBKEY(xpk, pp);
214 X509_PUBKEY_free(xpk);
215 return ret;
216 }
217
218/* The following are equivalents but which return RSA and DSA
219 * keys
220 */
221#ifndef OPENSSL_NO_RSA
222RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
223 long length)
224 {
225 EVP_PKEY *pkey;
226 RSA *key;
227 const unsigned char *q;
228 q = *pp;
229 pkey = d2i_PUBKEY(NULL, &q, length);
230 if (!pkey) return NULL;
231 key = EVP_PKEY_get1_RSA(pkey);
232 EVP_PKEY_free(pkey);
233 if (!key) return NULL;
234 *pp = q;
235 if (a)
236 {
237 RSA_free(*a);
238 *a = key;
239 }
240 return key;
241 }
242
243int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
244 {
245 EVP_PKEY *pktmp;
246 int ret;
247 if (!a) return 0;
248 pktmp = EVP_PKEY_new();
249 if (!pktmp)
250 {
251 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
252 return 0;
253 }
254 EVP_PKEY_set1_RSA(pktmp, a);
255 ret = i2d_PUBKEY(pktmp, pp);
256 EVP_PKEY_free(pktmp);
257 return ret;
258 }
259#endif
260
261#ifndef OPENSSL_NO_DSA
262DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
263 long length)
264 {
265 EVP_PKEY *pkey;
266 DSA *key;
267 const unsigned char *q;
268 q = *pp;
269 pkey = d2i_PUBKEY(NULL, &q, length);
270 if (!pkey) return NULL;
271 key = EVP_PKEY_get1_DSA(pkey);
272 EVP_PKEY_free(pkey);
273 if (!key) return NULL;
274 *pp = q;
275 if (a)
276 {
277 DSA_free(*a);
278 *a = key;
279 }
280 return key;
281 }
282
283int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
284 {
285 EVP_PKEY *pktmp;
286 int ret;
287 if(!a) return 0;
288 pktmp = EVP_PKEY_new();
289 if(!pktmp)
290 {
291 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
292 return 0;
293 }
294 EVP_PKEY_set1_DSA(pktmp, a);
295 ret = i2d_PUBKEY(pktmp, pp);
296 EVP_PKEY_free(pktmp);
297 return ret;
298 }
299#endif
300
301#ifndef OPENSSL_NO_EC
302EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
303 {
304 EVP_PKEY *pkey;
305 EC_KEY *key;
306 const unsigned char *q;
307 q = *pp;
308 pkey = d2i_PUBKEY(NULL, &q, length);
309 if (!pkey) return(NULL);
310 key = EVP_PKEY_get1_EC_KEY(pkey);
311 EVP_PKEY_free(pkey);
312 if (!key) return(NULL);
313 *pp = q;
314 if (a)
315 {
316 EC_KEY_free(*a);
317 *a = key;
318 }
319 return(key);
320 }
321
322int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
323 {
324 EVP_PKEY *pktmp;
325 int ret;
326 if (!a) return(0);
327 if ((pktmp = EVP_PKEY_new()) == NULL)
328 {
329 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
330 return(0);
331 }
332 EVP_PKEY_set1_EC_KEY(pktmp, a);
333 ret = i2d_PUBKEY(pktmp, pp);
334 EVP_PKEY_free(pktmp);
335 return(ret);
336 }
337#endif
338
339int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
340 int ptype, void *pval,
341 unsigned char *penc, int penclen)
342 {
343 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
344 return 0;
345 if (penc)
346 {
347 if (pub->public_key->data)
348 OPENSSL_free(pub->public_key->data);
349 pub->public_key->data = penc;
350 pub->public_key->length = penclen;
351 /* Set number of unused bits to zero */
352 pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
353 pub->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
354 }
355 return 1;
356 }
357
358int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
359 const unsigned char **pk, int *ppklen,
360 X509_ALGOR **pa,
361 X509_PUBKEY *pub)
362 {
363 if (ppkalg)
364 *ppkalg = pub->algor->algorithm;
365 if (pk)
366 {
367 *pk = pub->public_key->data;
368 *ppklen = pub->public_key->length;
369 }
370 if (pa)
371 *pa = pub->algor;
372 return 1;
373 }
diff --git a/src/lib/libcrypto/asn1/x_req.c b/src/lib/libcrypto/asn1/x_req.c
deleted file mode 100644
index d57555827c..0000000000
--- a/src/lib/libcrypto/asn1/x_req.c
+++ /dev/null
@@ -1,113 +0,0 @@
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 <openssl/asn1t.h>
62#include <openssl/x509.h>
63
64/* X509_REQ_INFO is handled in an unusual way to get round
65 * invalid encodings. Some broken certificate requests don't
66 * encode the attributes field if it is empty. This is in
67 * violation of PKCS#10 but we need to tolerate it. We do
68 * this by making the attributes field OPTIONAL then using
69 * the callback to initialise it to an empty STACK.
70 *
71 * This means that the field will be correctly encoded unless
72 * we NULL out the field.
73 *
74 * As a result we no longer need the req_kludge field because
75 * the information is now contained in the attributes field:
76 * 1. If it is NULL then it's the invalid omission.
77 * 2. If it is empty it is the correct encoding.
78 * 3. If it is not empty then some attributes are present.
79 *
80 */
81
82static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
83 void *exarg)
84{
85 X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
86
87 if(operation == ASN1_OP_NEW_POST) {
88 rinf->attributes = sk_X509_ATTRIBUTE_new_null();
89 if(!rinf->attributes) return 0;
90 }
91 return 1;
92}
93
94ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
95 ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
96 ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
97 ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
98 /* This isn't really OPTIONAL but it gets round invalid
99 * encodings
100 */
101 ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0)
102} ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
103
104IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
105
106ASN1_SEQUENCE_ref(X509_REQ, 0, CRYPTO_LOCK_X509_REQ) = {
107 ASN1_SIMPLE(X509_REQ, req_info, X509_REQ_INFO),
108 ASN1_SIMPLE(X509_REQ, sig_alg, X509_ALGOR),
109 ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING)
110} ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ)
111
112IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
113IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
diff --git a/src/lib/libcrypto/asn1/x_sig.c b/src/lib/libcrypto/asn1/x_sig.c
deleted file mode 100644
index 42efa86c1c..0000000000
--- a/src/lib/libcrypto/asn1/x_sig.c
+++ /dev/null
@@ -1,69 +0,0 @@
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 <openssl/asn1t.h>
62#include <openssl/x509.h>
63
64ASN1_SEQUENCE(X509_SIG) = {
65 ASN1_SIMPLE(X509_SIG, algor, X509_ALGOR),
66 ASN1_SIMPLE(X509_SIG, digest, ASN1_OCTET_STRING)
67} ASN1_SEQUENCE_END(X509_SIG)
68
69IMPLEMENT_ASN1_FUNCTIONS(X509_SIG)
diff --git a/src/lib/libcrypto/asn1/x_spki.c b/src/lib/libcrypto/asn1/x_spki.c
deleted file mode 100644
index 2aece077c5..0000000000
--- a/src/lib/libcrypto/asn1/x_spki.c
+++ /dev/null
@@ -1,81 +0,0 @@
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 permission
61 */
62
63#include <stdio.h>
64#include "cryptlib.h"
65#include <openssl/x509.h>
66#include <openssl/asn1t.h>
67
68ASN1_SEQUENCE(NETSCAPE_SPKAC) = {
69 ASN1_SIMPLE(NETSCAPE_SPKAC, pubkey, X509_PUBKEY),
70 ASN1_SIMPLE(NETSCAPE_SPKAC, challenge, ASN1_IA5STRING)
71} ASN1_SEQUENCE_END(NETSCAPE_SPKAC)
72
73IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_SPKAC)
74
75ASN1_SEQUENCE(NETSCAPE_SPKI) = {
76 ASN1_SIMPLE(NETSCAPE_SPKI, spkac, NETSCAPE_SPKAC),
77 ASN1_SIMPLE(NETSCAPE_SPKI, sig_algor, X509_ALGOR),
78 ASN1_SIMPLE(NETSCAPE_SPKI, signature, ASN1_BIT_STRING)
79} ASN1_SEQUENCE_END(NETSCAPE_SPKI)
80
81IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_SPKI)
diff --git a/src/lib/libcrypto/asn1/x_val.c b/src/lib/libcrypto/asn1/x_val.c
deleted file mode 100644
index dc17c67758..0000000000
--- a/src/lib/libcrypto/asn1/x_val.c
+++ /dev/null
@@ -1,69 +0,0 @@
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 <openssl/asn1t.h>
62#include <openssl/x509.h>
63
64ASN1_SEQUENCE(X509_VAL) = {
65 ASN1_SIMPLE(X509_VAL, notBefore, ASN1_TIME),
66 ASN1_SIMPLE(X509_VAL, notAfter, ASN1_TIME)
67} ASN1_SEQUENCE_END(X509_VAL)
68
69IMPLEMENT_ASN1_FUNCTIONS(X509_VAL)
diff --git a/src/lib/libcrypto/asn1/x_x509.c b/src/lib/libcrypto/asn1/x_x509.c
deleted file mode 100644
index de3df9eb51..0000000000
--- a/src/lib/libcrypto/asn1/x_x509.c
+++ /dev/null
@@ -1,194 +0,0 @@
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 <openssl/evp.h>
62#include <openssl/asn1t.h>
63#include <openssl/x509.h>
64#include <openssl/x509v3.h>
65
66ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
67 ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
68 ASN1_SIMPLE(X509_CINF, serialNumber, ASN1_INTEGER),
69 ASN1_SIMPLE(X509_CINF, signature, X509_ALGOR),
70 ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
71 ASN1_SIMPLE(X509_CINF, validity, X509_VAL),
72 ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
73 ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
74 ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
75 ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
76 ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3)
77} ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
78
79IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
80/* X509 top level structure needs a bit of customisation */
81
82extern void policy_cache_free(X509_POLICY_CACHE *cache);
83
84static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
85 void *exarg)
86{
87 X509 *ret = (X509 *)*pval;
88
89 switch(operation) {
90
91 case ASN1_OP_NEW_POST:
92 ret->valid=0;
93 ret->name = NULL;
94 ret->ex_flags = 0;
95 ret->ex_pathlen = -1;
96 ret->skid = NULL;
97 ret->akid = NULL;
98#ifndef OPENSSL_NO_RFC3779
99 ret->rfc3779_addr = NULL;
100 ret->rfc3779_asid = NULL;
101#endif
102 ret->aux = NULL;
103 ret->crldp = NULL;
104 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
105 break;
106
107 case ASN1_OP_D2I_POST:
108 if (ret->name != NULL) OPENSSL_free(ret->name);
109 ret->name=X509_NAME_oneline(ret->cert_info->subject,NULL,0);
110 break;
111
112 case ASN1_OP_FREE_POST:
113 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
114 X509_CERT_AUX_free(ret->aux);
115 ASN1_OCTET_STRING_free(ret->skid);
116 AUTHORITY_KEYID_free(ret->akid);
117 CRL_DIST_POINTS_free(ret->crldp);
118 policy_cache_free(ret->policy_cache);
119 GENERAL_NAMES_free(ret->altname);
120 NAME_CONSTRAINTS_free(ret->nc);
121#ifndef OPENSSL_NO_RFC3779
122 sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
123 ASIdentifiers_free(ret->rfc3779_asid);
124#endif
125
126 if (ret->name != NULL) OPENSSL_free(ret->name);
127 break;
128
129 }
130
131 return 1;
132
133}
134
135ASN1_SEQUENCE_ref(X509, x509_cb, CRYPTO_LOCK_X509) = {
136 ASN1_SIMPLE(X509, cert_info, X509_CINF),
137 ASN1_SIMPLE(X509, sig_alg, X509_ALGOR),
138 ASN1_SIMPLE(X509, signature, ASN1_BIT_STRING)
139} ASN1_SEQUENCE_END_ref(X509, X509)
140
141IMPLEMENT_ASN1_FUNCTIONS(X509)
142IMPLEMENT_ASN1_DUP_FUNCTION(X509)
143
144int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
145 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
146 {
147 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp,
148 new_func, dup_func, free_func);
149 }
150
151int X509_set_ex_data(X509 *r, int idx, void *arg)
152 {
153 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
154 }
155
156void *X509_get_ex_data(X509 *r, int idx)
157 {
158 return(CRYPTO_get_ex_data(&r->ex_data,idx));
159 }
160
161/* X509_AUX ASN1 routines. X509_AUX is the name given to
162 * a certificate with extra info tagged on the end. Since these
163 * functions set how a certificate is trusted they should only
164 * be used when the certificate comes from a reliable source
165 * such as local storage.
166 *
167 */
168
169X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
170{
171 const unsigned char *q;
172 X509 *ret;
173 /* Save start position */
174 q = *pp;
175 ret = d2i_X509(a, pp, length);
176 /* If certificate unreadable then forget it */
177 if(!ret) return NULL;
178 /* update length */
179 length -= *pp - q;
180 if(!length) return ret;
181 if(!d2i_X509_CERT_AUX(&ret->aux, pp, length)) goto err;
182 return ret;
183 err:
184 X509_free(ret);
185 return NULL;
186}
187
188int i2d_X509_AUX(X509 *a, unsigned char **pp)
189{
190 int length;
191 length = i2d_X509(a, pp);
192 if(a) length += i2d_X509_CERT_AUX(a->aux, pp);
193 return length;
194}
diff --git a/src/lib/libcrypto/asn1/x_x509a.c b/src/lib/libcrypto/asn1/x_x509a.c
deleted file mode 100644
index b603f82de7..0000000000
--- a/src/lib/libcrypto/asn1/x_x509a.c
+++ /dev/null
@@ -1,180 +0,0 @@
1/* a_x509a.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/evp.h>
62#include <openssl/asn1t.h>
63#include <openssl/x509.h>
64
65/* X509_CERT_AUX routines. These are used to encode additional
66 * user modifiable data about a certificate. This data is
67 * appended to the X509 encoding when the *_X509_AUX routines
68 * are used. This means that the "traditional" X509 routines
69 * will simply ignore the extra data.
70 */
71
72static X509_CERT_AUX *aux_get(X509 *x);
73
74ASN1_SEQUENCE(X509_CERT_AUX) = {
75 ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT),
76 ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0),
77 ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING),
78 ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING),
79 ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1)
80} ASN1_SEQUENCE_END(X509_CERT_AUX)
81
82IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX)
83
84static X509_CERT_AUX *aux_get(X509 *x)
85{
86 if(!x) return NULL;
87 if(!x->aux && !(x->aux = X509_CERT_AUX_new())) return NULL;
88 return x->aux;
89}
90
91int X509_alias_set1(X509 *x, unsigned char *name, int len)
92{
93 X509_CERT_AUX *aux;
94 if (!name)
95 {
96 if (!x || !x->aux || !x->aux->alias)
97 return 1;
98 ASN1_UTF8STRING_free(x->aux->alias);
99 x->aux->alias = NULL;
100 return 1;
101 }
102 if(!(aux = aux_get(x))) return 0;
103 if(!aux->alias && !(aux->alias = ASN1_UTF8STRING_new())) return 0;
104 return ASN1_STRING_set(aux->alias, name, len);
105}
106
107int X509_keyid_set1(X509 *x, unsigned char *id, int len)
108{
109 X509_CERT_AUX *aux;
110 if (!id)
111 {
112 if (!x || !x->aux || !x->aux->keyid)
113 return 1;
114 ASN1_OCTET_STRING_free(x->aux->keyid);
115 x->aux->keyid = NULL;
116 return 1;
117 }
118 if(!(aux = aux_get(x))) return 0;
119 if(!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new())) return 0;
120 return ASN1_STRING_set(aux->keyid, id, len);
121}
122
123unsigned char *X509_alias_get0(X509 *x, int *len)
124{
125 if(!x->aux || !x->aux->alias) return NULL;
126 if(len) *len = x->aux->alias->length;
127 return x->aux->alias->data;
128}
129
130unsigned char *X509_keyid_get0(X509 *x, int *len)
131{
132 if(!x->aux || !x->aux->keyid) return NULL;
133 if(len) *len = x->aux->keyid->length;
134 return x->aux->keyid->data;
135}
136
137int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj)
138{
139 X509_CERT_AUX *aux;
140 ASN1_OBJECT *objtmp;
141 if(!(objtmp = OBJ_dup(obj))) return 0;
142 if(!(aux = aux_get(x))) return 0;
143 if(!aux->trust
144 && !(aux->trust = sk_ASN1_OBJECT_new_null())) return 0;
145 return sk_ASN1_OBJECT_push(aux->trust, objtmp);
146}
147
148int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj)
149{
150 X509_CERT_AUX *aux;
151 ASN1_OBJECT *objtmp;
152 if(!(objtmp = OBJ_dup(obj))) return 0;
153 if(!(aux = aux_get(x))) return 0;
154 if(!aux->reject
155 && !(aux->reject = sk_ASN1_OBJECT_new_null())) return 0;
156 return sk_ASN1_OBJECT_push(aux->reject, objtmp);
157}
158
159void X509_trust_clear(X509 *x)
160{
161 if(x->aux && x->aux->trust) {
162 sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free);
163 x->aux->trust = NULL;
164 }
165}
166
167void X509_reject_clear(X509 *x)
168{
169 if(x->aux && x->aux->reject) {
170 sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free);
171 x->aux->reject = NULL;
172 }
173}
174
175ASN1_SEQUENCE(X509_CERT_PAIR) = {
176 ASN1_EXP_OPT(X509_CERT_PAIR, forward, X509, 0),
177 ASN1_EXP_OPT(X509_CERT_PAIR, reverse, X509, 1)
178} ASN1_SEQUENCE_END(X509_CERT_PAIR)
179
180IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_PAIR)