1 /* $OpenBSD: asn1_old_lib.c,v 1.4 2022/05/05 19:18:56 jsing 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_locl.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
122 /* class 0 is constructed
123 * constructed == 2 for indefinite length constructed */
124 void
ASN1_put_object(unsigned char ** pp,int constructed,int length,int tag,int xclass)125 ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
126 int xclass)
127 {
128 unsigned char *p = *pp;
129 int i, ttag;
130
131 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
132 i |= (xclass & V_ASN1_PRIVATE);
133 if (tag < 31)
134 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
135 else {
136 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
137 for(i = 0, ttag = tag; ttag > 0; i++)
138 ttag >>= 7;
139 ttag = i;
140 while (i-- > 0) {
141 p[i] = tag & 0x7f;
142 if (i != (ttag - 1))
143 p[i] |= 0x80;
144 tag >>= 7;
145 }
146 p += ttag;
147 }
148 if (constructed == 2)
149 *(p++) = 0x80;
150 else
151 asn1_put_length(&p, length);
152 *pp = p;
153 }
154
155 int
ASN1_put_eoc(unsigned char ** pp)156 ASN1_put_eoc(unsigned char **pp)
157 {
158 unsigned char *p = *pp;
159
160 *p++ = 0;
161 *p++ = 0;
162 *pp = p;
163 return 2;
164 }
165
166 static void
asn1_put_length(unsigned char ** pp,int length)167 asn1_put_length(unsigned char **pp, int length)
168 {
169 unsigned char *p = *pp;
170
171 int i, l;
172 if (length <= 127)
173 *(p++) = (unsigned char)length;
174 else {
175 l = length;
176 for (i = 0; l > 0; i++)
177 l >>= 8;
178 *(p++) = i | 0x80;
179 l = i;
180 while (i-- > 0) {
181 p[i] = length & 0xff;
182 length >>= 8;
183 }
184 p += l;
185 }
186 *pp = p;
187 }
188
189 int
ASN1_object_size(int constructed,int length,int tag)190 ASN1_object_size(int constructed, int length, int tag)
191 {
192 int ret;
193
194 ret = length;
195 ret++;
196 if (tag >= 31) {
197 while (tag > 0) {
198 tag >>= 7;
199 ret++;
200 }
201 }
202 if (constructed == 2)
203 return ret + 3;
204 ret++;
205 if (length > 127) {
206 while (length > 0) {
207 length >>= 8;
208 ret++;
209 }
210 }
211 return (ret);
212 }
213