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 if (a == NULL) {
73 OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
74 return -1;
75 }
76
77 if (a->length == 0) {
78 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
79 return -1;
80 }
81
82 int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
83 if (pp == NULL || objsize == -1) {
84 return objsize;
85 }
86
87 unsigned char *p, *allocated = NULL;
88 if (*pp == NULL) {
89 if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
90 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
91 return -1;
92 }
93 } else {
94 p = *pp;
95 }
96
97 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
98 OPENSSL_memcpy(p, a->data, a->length);
99
100 /*
101 * If a new buffer was allocated, just return it back.
102 * If not, return the incremented buffer pointer.
103 */
104 *pp = allocated != NULL ? allocated : p + a->length;
105 return objsize;
106 }
107
i2t_ASN1_OBJECT(char * buf,int buf_len,const ASN1_OBJECT * a)108 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
109 {
110 return OBJ_obj2txt(buf, buf_len, a, 0);
111 }
112
write_str(BIO * bp,const char * str)113 static int write_str(BIO *bp, const char *str)
114 {
115 int len = strlen(str);
116 return BIO_write(bp, str, len) == len ? len : -1;
117 }
118
i2a_ASN1_OBJECT(BIO * bp,const ASN1_OBJECT * a)119 int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
120 {
121 if (a == NULL || a->data == NULL) {
122 return write_str(bp, "NULL");
123 }
124
125 char buf[80], *allocated = NULL;
126 const char *str = buf;
127 int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
128 if (len > (int)sizeof(buf) - 1) {
129 /* The input was truncated. Allocate a buffer that fits. */
130 allocated = OPENSSL_malloc(len + 1);
131 if (allocated == NULL) {
132 return -1;
133 }
134 len = i2t_ASN1_OBJECT(allocated, len + 1, a);
135 str = allocated;
136 }
137 if (len <= 0) {
138 str = "<INVALID>";
139 }
140
141 int ret = write_str(bp, str);
142 OPENSSL_free(allocated);
143 return ret;
144 }
145
d2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long length)146 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
147 long length)
148 {
149 long len;
150 int tag, xclass;
151 const unsigned char *p = *pp;
152 int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
153 if (inf & 0x80) {
154 OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
155 return NULL;
156 }
157
158 if (inf & V_ASN1_CONSTRUCTED) {
159 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
160 return NULL;
161 }
162
163 if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
164 OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
165 return NULL;
166 }
167 ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
168 if (ret) {
169 *pp = p;
170 }
171 return ret;
172 }
173
c2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long len)174 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
175 long len)
176 {
177 ASN1_OBJECT *ret = NULL;
178 const unsigned char *p;
179 unsigned char *data;
180 int i, length;
181
182 /*
183 * Sanity check OID encoding. Need at least one content octet. MSB must
184 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
185 * see: X.690 8.19.2
186 */
187 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
188 p[len - 1] & 0x80) {
189 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
190 return NULL;
191 }
192 /* Now 0 < len <= INT_MAX, so the cast is safe. */
193 length = (int)len;
194 for (i = 0; i < length; i++, p++) {
195 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
196 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
197 return NULL;
198 }
199 }
200
201 if ((a == NULL) || ((*a) == NULL) ||
202 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
203 if ((ret = ASN1_OBJECT_new()) == NULL)
204 return (NULL);
205 } else {
206 ret = (*a);
207 }
208
209 p = *pp;
210 /* detach data from object */
211 data = (unsigned char *)ret->data;
212 ret->data = NULL;
213 /* once detached we can change it */
214 if ((data == NULL) || (ret->length < length)) {
215 ret->length = 0;
216 if (data != NULL)
217 OPENSSL_free(data);
218 data = (unsigned char *)OPENSSL_malloc(length);
219 if (data == NULL) {
220 i = ERR_R_MALLOC_FAILURE;
221 goto err;
222 }
223 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
224 }
225 OPENSSL_memcpy(data, p, length);
226 /* If there are dynamic strings, free them here, and clear the flag */
227 if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
228 OPENSSL_free((char *)ret->sn);
229 OPENSSL_free((char *)ret->ln);
230 ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
231 }
232 /* reattach data to object, after which it remains const */
233 ret->data = data;
234 ret->length = length;
235 ret->sn = NULL;
236 ret->ln = NULL;
237 p += length;
238
239 if (a != NULL)
240 (*a) = ret;
241 *pp = p;
242 return (ret);
243 err:
244 OPENSSL_PUT_ERROR(ASN1, i);
245 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
246 ASN1_OBJECT_free(ret);
247 return (NULL);
248 }
249
ASN1_OBJECT_new(void)250 ASN1_OBJECT *ASN1_OBJECT_new(void)
251 {
252 ASN1_OBJECT *ret;
253
254 ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
255 if (ret == NULL) {
256 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
257 return (NULL);
258 }
259 ret->length = 0;
260 ret->data = NULL;
261 ret->nid = 0;
262 ret->sn = NULL;
263 ret->ln = NULL;
264 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
265 return (ret);
266 }
267
ASN1_OBJECT_free(ASN1_OBJECT * a)268 void ASN1_OBJECT_free(ASN1_OBJECT *a)
269 {
270 if (a == NULL)
271 return;
272 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
273 OPENSSL_free((void *)a->sn);
274 OPENSSL_free((void *)a->ln);
275 a->sn = a->ln = NULL;
276 }
277 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
278 OPENSSL_free((void *)a->data);
279 a->data = NULL;
280 a->length = 0;
281 }
282 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
283 OPENSSL_free(a);
284 }
285
ASN1_OBJECT_create(int nid,const unsigned char * data,int len,const char * sn,const char * ln)286 ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
287 const char *sn, const char *ln)
288 {
289 ASN1_OBJECT o;
290
291 o.sn = sn;
292 o.ln = ln;
293 o.data = data;
294 o.nid = nid;
295 o.length = len;
296 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
297 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
298 return (OBJ_dup(&o));
299 }
300