1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 #include "internal/cryptlib.h"
61 #include <openssl/buffer.h>
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64 #include <openssl/bn.h>
65 #include "internal/asn1_int.h"
66 #include "asn1_locl.h"
68 int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
73 if ((a == NULL) || (a->data == NULL))
76 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
81 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
82 memcpy(p, a->data, a->length);
89 int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
91 int i, first, len = 0, c, use_bn;
92 char ftmp[24], *tmp = ftmp;
93 int tmpsize = sizeof ftmp;
106 if ((c >= '0') && (c <= '2')) {
109 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
114 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
122 if ((c != '.') && (c != ' ')) {
123 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
133 if ((c == ' ') || (c == '.'))
135 if ((c < '0') || (c > '9')) {
136 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
139 if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
143 if (bl == NULL || !BN_set_word(bl, l))
147 if (!BN_mul_word(bl, 10L)
148 || !BN_add_word(bl, c - '0'))
151 l = l * 10L + (long)(c - '0');
154 if ((first < 2) && (l >= 40)) {
155 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
156 ASN1_R_SECOND_NUMBER_TOO_LARGE);
160 if (!BN_add_word(bl, first * 40))
163 l += (long)first *40;
168 blsize = BN_num_bits(bl);
169 blsize = (blsize + 6) / 7;
170 if (blsize > tmpsize) {
173 tmpsize = blsize + 32;
174 tmp = OPENSSL_malloc(tmpsize);
179 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
183 tmp[i++] = (unsigned char)l & 0x7f;
191 if (len + i > olen) {
192 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
196 out[len++] = tmp[i] | 0x80;
212 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
214 return OBJ_obj2txt(buf, buf_len, a, 0);
217 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
219 char buf[80], *p = buf;
222 if ((a == NULL) || (a->data == NULL))
223 return (BIO_write(bp, "NULL", 4));
224 i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
225 if (i > (int)(sizeof(buf) - 1)) {
226 p = OPENSSL_malloc(i + 1);
229 i2t_ASN1_OBJECT(p, i + 1, a);
232 i = BIO_write(bp, "<INVALID>", 9);
233 i += BIO_dump(bp, (const char *)a->data, a->length);
242 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
245 const unsigned char *p;
249 ASN1_OBJECT *ret = NULL;
251 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
253 i = ASN1_R_BAD_OBJECT_HEADER;
257 if (tag != V_ASN1_OBJECT) {
258 i = ASN1_R_EXPECTING_AN_OBJECT;
261 ret = c2i_ASN1_OBJECT(a, &p, len);
266 ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
270 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
273 ASN1_OBJECT *ret = NULL, tobj;
274 const unsigned char *p;
279 * Sanity check OID encoding. Need at least one content octet. MSB must
280 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
283 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
285 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
288 /* Now 0 < len <= INT_MAX, so the cast is safe. */
291 * Try to lookup OID in table: these are all valid encodings so if we get
292 * a match we know the OID is valid.
294 tobj.nid = NID_undef;
296 tobj.length = length;
298 i = OBJ_obj2nid(&tobj);
299 if (i != NID_undef) {
301 * Return shared registered OID object: this improves efficiency
302 * because we don't have to return a dynamically allocated OID
303 * and NID lookups can use the cached value.
305 ret = OBJ_nid2obj(i);
307 ASN1_OBJECT_free(*a);
313 for (i = 0; i < length; i++, p++) {
314 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
315 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
321 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
324 if ((a == NULL) || ((*a) == NULL) ||
325 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
326 if ((ret = ASN1_OBJECT_new()) == NULL)
332 /* detach data from object */
333 data = (unsigned char *)ret->data;
335 /* once detached we can change it */
336 if ((data == NULL) || (ret->length < length)) {
339 data = OPENSSL_malloc(length);
341 i = ERR_R_MALLOC_FAILURE;
344 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
346 memcpy(data, p, length);
347 /* reattach data to object, after which it remains const */
349 ret->length = length;
352 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
360 ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
361 if ((a == NULL) || (*a != ret))
362 ASN1_OBJECT_free(ret);
366 ASN1_OBJECT *ASN1_OBJECT_new(void)
370 ret = OPENSSL_zalloc(sizeof(*ret));
372 ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
375 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
379 void ASN1_OBJECT_free(ASN1_OBJECT *a)
383 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
384 #ifndef CONST_STRICT /* disable purely for compile-time strict
385 * const checking. Doing this on a "real"
386 * compile will cause memory leaks */
387 OPENSSL_free((void*)a->sn);
388 OPENSSL_free((void*)a->ln);
390 a->sn = a->ln = NULL;
392 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
393 OPENSSL_free((void*)a->data);
397 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
401 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
402 const char *sn, const char *ln)
411 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
412 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
413 return (OBJ_dup(&o));