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