xref: /openbsd/lib/libcrypto/asn1/asn1_old_lib.c (revision acf64401)
1 /* $OpenBSD: asn1_old_lib.c,v 1.6 2023/07/05 21:23:36 beck Exp $ */
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 <limits.h>
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/err.h>
65 
66 #include "asn1_local.h"
67 
68 static void asn1_put_length(unsigned char **pp, int length);
69 
70 int
ASN1_get_object(const unsigned char ** pp,long * plength,int * ptag,int * pclass,long omax)71 ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
72     int *pclass, long omax)
73 {
74 	int constructed, indefinite;
75 	uint32_t tag_number;
76 	uint8_t tag_class;
77 	size_t length;
78 	CBS cbs;
79 	int ret = 0;
80 
81 	*pclass = 0;
82 	*ptag = 0;
83 	*plength = 0;
84 
85 	CBS_init(&cbs, *pp, omax);
86 
87 	if (!asn1_get_object_cbs(&cbs, 0, &tag_class, &constructed, &tag_number,
88 	    &indefinite, &length)) {
89 		ASN1error(ASN1_R_HEADER_TOO_LONG);
90 		return 0x80;
91 	}
92 
93 	if (tag_number > INT_MAX) {
94 		ASN1error(ASN1_R_HEADER_TOO_LONG);
95 		return 0x80;
96 	}
97 
98 	/*
99 	 * API insanity ahead... in this case we add an error to the stack and
100 	 * signal an error by setting the 8th bit in the return value... but we
101 	 * still provide all of the decoded data.
102 	 */
103 	if (length > CBS_len(&cbs) || length > LONG_MAX) {
104 		ASN1error(ASN1_R_TOO_LONG);
105 		ret = 0x80;
106 	}
107 
108 	*pclass = tag_class << 6;
109 	*ptag = tag_number;
110 	*plength = length;
111 
112 	*pp = CBS_data(&cbs);
113 
114 	if (constructed)
115 		ret |= 1 << 5;
116 	if (indefinite)
117 		ret |= 1;
118 
119 	return ret;
120 }
121 LCRYPTO_ALIAS(ASN1_get_object);
122 
123 /* class 0 is constructed
124  * constructed == 2 for indefinite length constructed */
125 void
ASN1_put_object(unsigned char ** pp,int constructed,int length,int tag,int xclass)126 ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
127     int xclass)
128 {
129 	unsigned char *p = *pp;
130 	int i, ttag;
131 
132 	i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
133 	i |= (xclass & V_ASN1_PRIVATE);
134 	if (tag < 31)
135 		*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
136 	else {
137 		*(p++) = i | V_ASN1_PRIMITIVE_TAG;
138 		for(i = 0, ttag = tag; ttag > 0; i++)
139 			ttag >>= 7;
140 		ttag = i;
141 		while (i-- > 0) {
142 			p[i] = tag & 0x7f;
143 			if (i != (ttag - 1))
144 				p[i] |= 0x80;
145 			tag >>= 7;
146 		}
147 		p += ttag;
148 	}
149 	if (constructed == 2)
150 		*(p++) = 0x80;
151 	else
152 		asn1_put_length(&p, length);
153 	*pp = p;
154 }
155 LCRYPTO_ALIAS(ASN1_put_object);
156 
157 int
ASN1_put_eoc(unsigned char ** pp)158 ASN1_put_eoc(unsigned char **pp)
159 {
160 	unsigned char *p = *pp;
161 
162 	*p++ = 0;
163 	*p++ = 0;
164 	*pp = p;
165 	return 2;
166 }
167 LCRYPTO_ALIAS(ASN1_put_eoc);
168 
169 static void
asn1_put_length(unsigned char ** pp,int length)170 asn1_put_length(unsigned char **pp, int length)
171 {
172 	unsigned char *p = *pp;
173 
174 	int i, l;
175 	if (length <= 127)
176 		*(p++) = (unsigned char)length;
177 	else {
178 		l = length;
179 		for (i = 0; l > 0; i++)
180 			l >>= 8;
181 		*(p++) = i | 0x80;
182 		l = i;
183 		while (i-- > 0) {
184 			p[i] = length & 0xff;
185 			length >>= 8;
186 		}
187 		p += l;
188 	}
189 	*pp = p;
190 }
191 
192 int
ASN1_object_size(int constructed,int length,int tag)193 ASN1_object_size(int constructed, int length, int tag)
194 {
195 	int ret;
196 
197 	ret = length;
198 	ret++;
199 	if (tag >= 31) {
200 		while (tag > 0) {
201 			tag >>= 7;
202 			ret++;
203 		}
204 	}
205 	if (constructed == 2)
206 		return ret + 3;
207 	ret++;
208 	if (length > 127) {
209 		while (length > 0) {
210 			length >>= 8;
211 			ret++;
212 		}
213 	}
214 	return (ret);
215 }
216 LCRYPTO_ALIAS(ASN1_object_size);
217