1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <limits.h>
60 #include <string.h>
61
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65
66 #include "../internal.h"
67 #include "internal.h"
68
69
i2d_ASN1_OBJECT(const ASN1_OBJECT * a,unsigned char ** pp)70 int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
71 {
72 unsigned char *p, *allocated = NULL;
73 int objsize;
74
75 if ((a == NULL) || (a->data == NULL))
76 return (0);
77
78 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
79 if (pp == NULL || objsize == -1)
80 return objsize;
81
82 if (*pp == NULL) {
83 if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
84 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
85 return 0;
86 }
87 } else {
88 p = *pp;
89 }
90
91 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
92 OPENSSL_memcpy(p, a->data, a->length);
93
94 /*
95 * If a new buffer was allocated, just return it back.
96 * If not, return the incremented buffer pointer.
97 */
98 *pp = allocated != NULL ? allocated : p + a->length;
99 return objsize;
100 }
101
i2t_ASN1_OBJECT(char * buf,int buf_len,const ASN1_OBJECT * a)102 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
103 {
104 return OBJ_obj2txt(buf, buf_len, a, 0);
105 }
106
i2a_ASN1_OBJECT(BIO * bp,const ASN1_OBJECT * a)107 int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
108 {
109 char buf[80], *p = buf;
110 int i;
111
112 if ((a == NULL) || (a->data == NULL))
113 return (BIO_write(bp, "NULL", 4));
114 i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
115 if (i > (int)(sizeof(buf) - 1)) {
116 p = OPENSSL_malloc(i + 1);
117 if (!p)
118 return -1;
119 i2t_ASN1_OBJECT(p, i + 1, a);
120 }
121 if (i <= 0)
122 return BIO_write(bp, "<INVALID>", 9);
123 BIO_write(bp, p, i);
124 if (p != buf)
125 OPENSSL_free(p);
126 return (i);
127 }
128
d2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long length)129 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
130 long length)
131 {
132 const unsigned char *p;
133 long len;
134 int tag, xclass;
135 int inf, i;
136 ASN1_OBJECT *ret = NULL;
137 p = *pp;
138 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
139 if (inf & 0x80) {
140 i = ASN1_R_BAD_OBJECT_HEADER;
141 goto err;
142 }
143
144 if (tag != V_ASN1_OBJECT) {
145 i = ASN1_R_EXPECTING_AN_OBJECT;
146 goto err;
147 }
148 ret = c2i_ASN1_OBJECT(a, &p, len);
149 if (ret)
150 *pp = p;
151 return ret;
152 err:
153 OPENSSL_PUT_ERROR(ASN1, i);
154 return (NULL);
155 }
156
c2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long len)157 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
158 long len)
159 {
160 ASN1_OBJECT *ret = NULL;
161 const unsigned char *p;
162 unsigned char *data;
163 int i, length;
164
165 /*
166 * Sanity check OID encoding. Need at least one content octet. MSB must
167 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
168 * see: X.690 8.19.2
169 */
170 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
171 p[len - 1] & 0x80) {
172 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
173 return NULL;
174 }
175 /* Now 0 < len <= INT_MAX, so the cast is safe. */
176 length = (int)len;
177 for (i = 0; i < length; i++, p++) {
178 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
179 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
180 return NULL;
181 }
182 }
183
184 if ((a == NULL) || ((*a) == NULL) ||
185 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
186 if ((ret = ASN1_OBJECT_new()) == NULL)
187 return (NULL);
188 } else {
189 ret = (*a);
190 }
191
192 p = *pp;
193 /* detach data from object */
194 data = (unsigned char *)ret->data;
195 ret->data = NULL;
196 /* once detached we can change it */
197 if ((data == NULL) || (ret->length < length)) {
198 ret->length = 0;
199 if (data != NULL)
200 OPENSSL_free(data);
201 data = (unsigned char *)OPENSSL_malloc(length);
202 if (data == NULL) {
203 i = ERR_R_MALLOC_FAILURE;
204 goto err;
205 }
206 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
207 }
208 OPENSSL_memcpy(data, p, length);
209 /* If there are dynamic strings, free them here, and clear the flag */
210 if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
211 OPENSSL_free((char *)ret->sn);
212 OPENSSL_free((char *)ret->ln);
213 ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
214 }
215 /* reattach data to object, after which it remains const */
216 ret->data = data;
217 ret->length = length;
218 ret->sn = NULL;
219 ret->ln = NULL;
220 p += length;
221
222 if (a != NULL)
223 (*a) = ret;
224 *pp = p;
225 return (ret);
226 err:
227 OPENSSL_PUT_ERROR(ASN1, i);
228 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
229 ASN1_OBJECT_free(ret);
230 return (NULL);
231 }
232
ASN1_OBJECT_new(void)233 ASN1_OBJECT *ASN1_OBJECT_new(void)
234 {
235 ASN1_OBJECT *ret;
236
237 ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
238 if (ret == NULL) {
239 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
240 return (NULL);
241 }
242 ret->length = 0;
243 ret->data = NULL;
244 ret->nid = 0;
245 ret->sn = NULL;
246 ret->ln = NULL;
247 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
248 return (ret);
249 }
250
ASN1_OBJECT_free(ASN1_OBJECT * a)251 void ASN1_OBJECT_free(ASN1_OBJECT *a)
252 {
253 if (a == NULL)
254 return;
255 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
256 OPENSSL_free((void *)a->sn);
257 OPENSSL_free((void *)a->ln);
258 a->sn = a->ln = NULL;
259 }
260 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
261 OPENSSL_free((void *)a->data);
262 a->data = NULL;
263 a->length = 0;
264 }
265 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
266 OPENSSL_free(a);
267 }
268
ASN1_OBJECT_create(int nid,const unsigned char * data,int len,const char * sn,const char * ln)269 ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
270 const char *sn, const char *ln)
271 {
272 ASN1_OBJECT o;
273
274 o.sn = sn;
275 o.ln = ln;
276 o.data = data;
277 o.nid = nid;
278 o.length = len;
279 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
280 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
281 return (OBJ_dup(&o));
282 }
283