1 /* $OpenBSD: a_enum.c,v 1.25 2022/07/09 14:46:43 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 <string.h> 61 62 #include <openssl/asn1.h> 63 #include <openssl/asn1t.h> 64 #include <openssl/bn.h> 65 #include <openssl/buffer.h> 66 #include <openssl/err.h> 67 68 #include "asn1_locl.h" 69 #include "bytestring.h" 70 71 /* 72 * Code for ENUMERATED type: identical to INTEGER apart from a different tag. 73 * for comments on encoding see a_int.c 74 */ 75 76 const ASN1_ITEM ASN1_ENUMERATED_it = { 77 .itype = ASN1_ITYPE_PRIMITIVE, 78 .utype = V_ASN1_ENUMERATED, 79 .sname = "ASN1_ENUMERATED", 80 }; 81 82 ASN1_ENUMERATED * 83 ASN1_ENUMERATED_new(void) 84 { 85 return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it); 86 } 87 88 static void 89 asn1_aenum_clear(ASN1_ENUMERATED *aenum) 90 { 91 freezero(aenum->data, aenum->length); 92 93 memset(aenum, 0, sizeof(*aenum)); 94 95 aenum->type = V_ASN1_ENUMERATED; 96 } 97 98 void 99 ASN1_ENUMERATED_free(ASN1_ENUMERATED *a) 100 { 101 ASN1_item_free((ASN1_VALUE *)a, &ASN1_ENUMERATED_it); 102 } 103 104 int 105 ASN1_ENUMERATED_get_int64(int64_t *out_val, const ASN1_ENUMERATED *aenum) 106 { 107 CBS cbs; 108 109 *out_val = 0; 110 111 if (aenum == NULL || aenum->length < 0) 112 return 0; 113 114 if (aenum->type != V_ASN1_ENUMERATED && 115 aenum->type != V_ASN1_NEG_ENUMERATED) { 116 ASN1error(ASN1_R_WRONG_INTEGER_TYPE); 117 return 0; 118 } 119 120 CBS_init(&cbs, aenum->data, aenum->length); 121 122 return asn1_aint_get_int64(&cbs, (aenum->type == V_ASN1_NEG_ENUMERATED), 123 out_val); 124 } 125 126 int 127 ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *aenum, int64_t val) 128 { 129 asn1_aenum_clear(aenum); 130 131 if (val < 0) { 132 aenum->type = V_ASN1_NEG_ENUMERATED; 133 val = -val; 134 } 135 136 return asn1_aint_set_uint64((uint64_t)val, &aenum->data, &aenum->length); 137 } 138 139 long 140 ASN1_ENUMERATED_get(const ASN1_ENUMERATED *aenum) 141 { 142 int64_t val; 143 144 if (aenum == NULL) 145 return 0; 146 if (!ASN1_ENUMERATED_get_int64(&val, aenum)) 147 return -1; 148 if (val < LONG_MIN || val > LONG_MAX) { 149 /* hmm... a bit ugly, return all ones */ 150 return -1; 151 } 152 153 return (long)val; 154 } 155 156 int 157 ASN1_ENUMERATED_set(ASN1_ENUMERATED *aenum, long val) 158 { 159 return ASN1_ENUMERATED_set_int64(aenum, val); 160 } 161 162 ASN1_ENUMERATED * 163 BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) 164 { 165 ASN1_ENUMERATED *ret; 166 int len, j; 167 168 if (ai == NULL) 169 ret = ASN1_ENUMERATED_new(); 170 else 171 ret = ai; 172 if (ret == NULL) { 173 ASN1error(ERR_R_NESTED_ASN1_ERROR); 174 goto err; 175 } 176 if (BN_is_negative(bn)) 177 ret->type = V_ASN1_NEG_ENUMERATED; 178 else 179 ret->type = V_ASN1_ENUMERATED; 180 j = BN_num_bits(bn); 181 len = ((j == 0) ? 0 : ((j / 8) + 1)); 182 if (ret->length < len + 4) { 183 unsigned char *new_data = realloc(ret->data, len + 4); 184 if (!new_data) { 185 ASN1error(ERR_R_MALLOC_FAILURE); 186 goto err; 187 } 188 ret->data = new_data; 189 } 190 ret->length = BN_bn2bin(bn, ret->data); 191 192 /* Correct zero case */ 193 if (!ret->length) { 194 ret->data[0] = 0; 195 ret->length = 1; 196 } 197 return (ret); 198 199 err: 200 if (ret != ai) 201 ASN1_ENUMERATED_free(ret); 202 return (NULL); 203 } 204 205 BIGNUM * 206 ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) 207 { 208 BIGNUM *ret; 209 210 if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) 211 ASN1error(ASN1_R_BN_LIB); 212 else if (ai->type == V_ASN1_NEG_ENUMERATED) 213 BN_set_negative(ret, 1); 214 return (ret); 215 } 216 217 /* Based on a_int.c: equivalent ENUMERATED functions */ 218 219 int 220 i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a) 221 { 222 int i, n = 0; 223 static const char h[] = "0123456789ABCDEF"; 224 char buf[2]; 225 226 if (a == NULL) 227 return (0); 228 229 if (a->length == 0) { 230 if (BIO_write(bp, "00", 2) != 2) 231 goto err; 232 n = 2; 233 } else { 234 for (i = 0; i < a->length; i++) { 235 if ((i != 0) && (i % 35 == 0)) { 236 if (BIO_write(bp, "\\\n", 2) != 2) 237 goto err; 238 n += 2; 239 } 240 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; 241 buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; 242 if (BIO_write(bp, buf, 2) != 2) 243 goto err; 244 n += 2; 245 } 246 } 247 return (n); 248 249 err: 250 return (-1); 251 } 252 253 int 254 a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size) 255 { 256 int ret = 0; 257 int i, j,k, m,n, again, bufsize; 258 unsigned char *s = NULL, *sp; 259 unsigned char *bufp; 260 int first = 1; 261 size_t num = 0, slen = 0; 262 263 bs->type = V_ASN1_ENUMERATED; 264 265 bufsize = BIO_gets(bp, buf, size); 266 for (;;) { 267 if (bufsize < 1) 268 goto err_sl; 269 i = bufsize; 270 if (buf[i-1] == '\n') 271 buf[--i] = '\0'; 272 if (i == 0) 273 goto err_sl; 274 if (buf[i-1] == '\r') 275 buf[--i] = '\0'; 276 if (i == 0) 277 goto err_sl; 278 if (buf[i - 1] == '\\') { 279 i--; 280 again = 1; 281 } else 282 again = 0; 283 buf[i] = '\0'; 284 if (i < 2) 285 goto err_sl; 286 287 bufp = (unsigned char *)buf; 288 if (first) { 289 first = 0; 290 if ((bufp[0] == '0') && (buf[1] == '0')) { 291 bufp += 2; 292 i -= 2; 293 } 294 } 295 k = 0; 296 if (i % 2 != 0) { 297 ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS); 298 goto err; 299 } 300 i /= 2; 301 if (num + i > slen) { 302 sp = realloc(s, num + i); 303 if (sp == NULL) { 304 ASN1error(ERR_R_MALLOC_FAILURE); 305 goto err; 306 } 307 s = sp; 308 slen = num + i; 309 } 310 for (j = 0; j < i; j++, k += 2) { 311 for (n = 0; n < 2; n++) { 312 m = bufp[k + n]; 313 if ((m >= '0') && (m <= '9')) 314 m -= '0'; 315 else if ((m >= 'a') && (m <= 'f')) 316 m = m - 'a' + 10; 317 else if ((m >= 'A') && (m <= 'F')) 318 m = m - 'A' + 10; 319 else { 320 ASN1error(ASN1_R_NON_HEX_CHARACTERS); 321 goto err; 322 } 323 s[num + j] <<= 4; 324 s[num + j] |= m; 325 } 326 } 327 num += i; 328 if (again) 329 bufsize = BIO_gets(bp, buf, size); 330 else 331 break; 332 } 333 bs->length = num; 334 bs->data = s; 335 return (1); 336 337 err_sl: 338 ASN1error(ASN1_R_SHORT_LINE); 339 err: 340 free(s); 341 return (ret); 342 } 343 344 int 345 i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **out) 346 { 347 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ENUMERATED_it); 348 } 349 350 ASN1_ENUMERATED * 351 d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, const unsigned char **in, long len) 352 { 353 return (ASN1_ENUMERATED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 354 &ASN1_ENUMERATED_it); 355 } 356