| 1 |
/* |
| 2 |
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <limits.h> |
| 12 |
#include "crypto/ctype.h" |
| 13 |
#include "internal/cryptlib.h" |
| 14 |
#include <openssl/buffer.h> |
| 15 |
#include <openssl/asn1.h> |
| 16 |
#include <openssl/objects.h> |
| 17 |
#include <openssl/bn.h> |
| 18 |
#include "crypto/asn1.h" |
| 19 |
#include "asn1_local.h" |
| 20 |
|
| 21 |
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) |
| 22 |
{ |
| 23 |
unsigned char *p, *allocated = NULL; |
| 24 |
int objsize; |
| 25 |
|
| 26 |
if ((a == NULL) || (a->data == NULL)) |
| 27 |
return 0; |
| 28 |
|
| 29 |
objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT); |
| 30 |
if (pp == NULL || objsize == -1) |
| 31 |
return objsize; |
| 32 |
|
| 33 |
if (*pp == NULL) { |
| 34 |
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) |
| 35 |
return 0; |
| 36 |
} else { |
| 37 |
p = *pp; |
| 38 |
} |
| 39 |
|
| 40 |
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
| 41 |
memcpy(p, a->data, a->length); |
| 42 |
|
| 43 |
/* |
| 44 |
* If a new buffer was allocated, just return it back. |
| 45 |
* If not, return the incremented buffer pointer. |
| 46 |
*/ |
| 47 |
*pp = allocated != NULL ? allocated : p + a->length; |
| 48 |
return objsize; |
| 49 |
} |
| 50 |
|
| 51 |
int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) |
| 52 |
{ |
| 53 |
int i, first, len = 0, c, use_bn; |
| 54 |
char ftmp[24], *tmp = ftmp; |
| 55 |
int tmpsize = sizeof(ftmp); |
| 56 |
const char *p; |
| 57 |
unsigned long l; |
| 58 |
BIGNUM *bl = NULL; |
| 59 |
|
| 60 |
if (num == 0) { |
| 61 |
return 0; |
| 62 |
} else if (num == -1) { |
| 63 |
size_t num_s = strlen(buf); |
| 64 |
|
| 65 |
if (num_s >= INT_MAX) { |
| 66 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG); |
| 67 |
goto err; |
| 68 |
} |
| 69 |
num = (int)num_s; |
| 70 |
} |
| 71 |
|
| 72 |
p = buf; |
| 73 |
c = *(p++); |
| 74 |
num--; |
| 75 |
if ((c >= '0') && (c <= '2')) { |
| 76 |
first = c - '0'; |
| 77 |
} else { |
| 78 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE); |
| 79 |
goto err; |
| 80 |
} |
| 81 |
|
| 82 |
if (num <= 0) { |
| 83 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER); |
| 84 |
goto err; |
| 85 |
} |
| 86 |
c = *(p++); |
| 87 |
num--; |
| 88 |
for (;;) { |
| 89 |
if (num <= 0) |
| 90 |
break; |
| 91 |
if ((c != '.') && (c != ' ')) { |
| 92 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR); |
| 93 |
goto err; |
| 94 |
} |
| 95 |
l = 0; |
| 96 |
use_bn = 0; |
| 97 |
for (;;) { |
| 98 |
if (num <= 0) |
| 99 |
break; |
| 100 |
num--; |
| 101 |
c = *(p++); |
| 102 |
if ((c == ' ') || (c == '.')) |
| 103 |
break; |
| 104 |
if (!ossl_isdigit(c)) { |
| 105 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT); |
| 106 |
goto err; |
| 107 |
} |
| 108 |
if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) { |
| 109 |
use_bn = 1; |
| 110 |
if (bl == NULL) |
| 111 |
bl = BN_new(); |
| 112 |
if (bl == NULL || !BN_set_word(bl, l)) |
| 113 |
goto err; |
| 114 |
} |
| 115 |
if (use_bn) { |
| 116 |
if (!BN_mul_word(bl, 10L) |
| 117 |
|| !BN_add_word(bl, c - '0')) |
| 118 |
goto err; |
| 119 |
} else |
| 120 |
l = l * 10L + (long)(c - '0'); |
| 121 |
} |
| 122 |
if (len == 0) { |
| 123 |
if ((first < 2) && (l >= 40)) { |
| 124 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE); |
| 125 |
goto err; |
| 126 |
} |
| 127 |
if (use_bn) { |
| 128 |
if (!BN_add_word(bl, first * 40)) |
| 129 |
goto err; |
| 130 |
} else |
| 131 |
l += (long)first * 40; |
| 132 |
} |
| 133 |
i = 0; |
| 134 |
if (use_bn) { |
| 135 |
int blsize; |
| 136 |
blsize = BN_num_bits(bl); |
| 137 |
blsize = (blsize + 6) / 7; |
| 138 |
if (blsize > tmpsize) { |
| 139 |
if (tmp != ftmp) |
| 140 |
OPENSSL_free(tmp); |
| 141 |
tmpsize = blsize + 32; |
| 142 |
tmp = OPENSSL_malloc(tmpsize); |
| 143 |
if (tmp == NULL) |
| 144 |
goto err; |
| 145 |
} |
| 146 |
while (blsize--) { |
| 147 |
BN_ULONG t = BN_div_word(bl, 0x80L); |
| 148 |
if (t == (BN_ULONG)-1) |
| 149 |
goto err; |
| 150 |
tmp[i++] = (unsigned char)t; |
| 151 |
} |
| 152 |
} else { |
| 153 |
|
| 154 |
for (;;) { |
| 155 |
tmp[i++] = (unsigned char)l & 0x7f; |
| 156 |
l >>= 7L; |
| 157 |
if (l == 0L) |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
if (out != NULL) { |
| 162 |
if (len + i > olen) { |
| 163 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL); |
| 164 |
goto err; |
| 165 |
} |
| 166 |
while (--i > 0) |
| 167 |
out[len++] = tmp[i] | 0x80; |
| 168 |
out[len++] = tmp[0]; |
| 169 |
} else |
| 170 |
len += i; |
| 171 |
} |
| 172 |
if (tmp != ftmp) |
| 173 |
OPENSSL_free(tmp); |
| 174 |
BN_free(bl); |
| 175 |
return len; |
| 176 |
err: |
| 177 |
if (tmp != ftmp) |
| 178 |
OPENSSL_free(tmp); |
| 179 |
BN_free(bl); |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) |
| 184 |
{ |
| 185 |
return OBJ_obj2txt(buf, buf_len, a, 0); |
| 186 |
} |
| 187 |
|
| 188 |
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) |
| 189 |
{ |
| 190 |
char buf[80], *p = buf; |
| 191 |
int i; |
| 192 |
|
| 193 |
if ((a == NULL) || (a->data == NULL)) |
| 194 |
return BIO_write(bp, "NULL", 4); |
| 195 |
i = i2t_ASN1_OBJECT(buf, sizeof(buf), a); |
| 196 |
if (i > (int)(sizeof(buf) - 1)) { |
| 197 |
if (i > INT_MAX - 1) { /* catch an integer overflow */ |
| 198 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
if ((p = OPENSSL_malloc(i + 1)) == NULL) |
| 202 |
return -1; |
| 203 |
i2t_ASN1_OBJECT(p, i + 1, a); |
| 204 |
} |
| 205 |
if (i <= 0) { |
| 206 |
i = BIO_write(bp, "<INVALID>", 9); |
| 207 |
if (i > 0) |
| 208 |
i += BIO_dump(bp, (const char *)a->data, a->length); |
| 209 |
return i; |
| 210 |
} |
| 211 |
BIO_write(bp, p, i); |
| 212 |
if (p != buf) |
| 213 |
OPENSSL_free(p); |
| 214 |
return i; |
| 215 |
} |
| 216 |
|
| 217 |
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
| 218 |
long length) |
| 219 |
{ |
| 220 |
const unsigned char *p; |
| 221 |
long len; |
| 222 |
int tag, xclass; |
| 223 |
int inf, i; |
| 224 |
ASN1_OBJECT *ret = NULL; |
| 225 |
p = *pp; |
| 226 |
inf = ASN1_get_object(&p, &len, &tag, &xclass, length); |
| 227 |
if (inf & 0x80) { |
| 228 |
i = ASN1_R_BAD_OBJECT_HEADER; |
| 229 |
goto err; |
| 230 |
} |
| 231 |
|
| 232 |
if (tag != V_ASN1_OBJECT) { |
| 233 |
i = ASN1_R_EXPECTING_AN_OBJECT; |
| 234 |
goto err; |
| 235 |
} |
| 236 |
ret = ossl_c2i_ASN1_OBJECT(a, &p, len); |
| 237 |
if (ret) |
| 238 |
*pp = p; |
| 239 |
return ret; |
| 240 |
err: |
| 241 |
ERR_raise(ERR_LIB_ASN1, i); |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
| 246 |
long len) |
| 247 |
{ |
| 248 |
ASN1_OBJECT *ret = NULL, tobj; |
| 249 |
const unsigned char *p; |
| 250 |
unsigned char *data; |
| 251 |
int i, length; |
| 252 |
|
| 253 |
/* |
| 254 |
* Sanity check OID encoding. Need at least one content octet. MSB must |
| 255 |
* be clear in the last octet. can't have leading 0x80 in subidentifiers, |
| 256 |
* see: X.690 8.19.2 |
| 257 |
*/ |
| 258 |
if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL || p[len - 1] & 0x80) { |
| 259 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING); |
| 260 |
return NULL; |
| 261 |
} |
| 262 |
/* Now 0 < len <= INT_MAX, so the cast is safe. */ |
| 263 |
length = (int)len; |
| 264 |
/* |
| 265 |
* Try to lookup OID in table: these are all valid encodings so if we get |
| 266 |
* a match we know the OID is valid. |
| 267 |
*/ |
| 268 |
tobj.nid = NID_undef; |
| 269 |
tobj.data = p; |
| 270 |
tobj.length = length; |
| 271 |
tobj.flags = 0; |
| 272 |
i = OBJ_obj2nid(&tobj); |
| 273 |
if (i != NID_undef) { |
| 274 |
/* |
| 275 |
* Return shared registered OID object: this improves efficiency |
| 276 |
* because we don't have to return a dynamically allocated OID |
| 277 |
* and NID lookups can use the cached value. |
| 278 |
*/ |
| 279 |
ret = OBJ_nid2obj(i); |
| 280 |
if (a) { |
| 281 |
ASN1_OBJECT_free(*a); |
| 282 |
*a = ret; |
| 283 |
} |
| 284 |
*pp += len; |
| 285 |
return ret; |
| 286 |
} |
| 287 |
for (i = 0; i < length; i++, p++) { |
| 288 |
if (*p == 0x80 && (!i || !(p[-1] & 0x80))) { |
| 289 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING); |
| 290 |
return NULL; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
if ((a == NULL) || ((*a) == NULL) || !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { |
| 295 |
if ((ret = ASN1_OBJECT_new()) == NULL) |
| 296 |
return NULL; |
| 297 |
} else { |
| 298 |
ret = (*a); |
| 299 |
} |
| 300 |
|
| 301 |
p = *pp; |
| 302 |
/* detach data from object */ |
| 303 |
data = (unsigned char *)ret->data; |
| 304 |
ret->data = NULL; |
| 305 |
/* once detached we can change it */ |
| 306 |
if ((data == NULL) || (ret->length < length)) { |
| 307 |
ret->length = 0; |
| 308 |
OPENSSL_free(data); |
| 309 |
data = OPENSSL_malloc(length); |
| 310 |
if (data == NULL) |
| 311 |
goto err; |
| 312 |
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
| 313 |
} |
| 314 |
memcpy(data, p, length); |
| 315 |
/* If there are dynamic strings, free them here, and clear the flag */ |
| 316 |
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) { |
| 317 |
OPENSSL_free((char *)ret->sn); |
| 318 |
OPENSSL_free((char *)ret->ln); |
| 319 |
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS; |
| 320 |
} |
| 321 |
/* reattach data to object, after which it remains const */ |
| 322 |
ret->data = data; |
| 323 |
ret->length = length; |
| 324 |
ret->sn = NULL; |
| 325 |
ret->ln = NULL; |
| 326 |
/* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ |
| 327 |
p += length; |
| 328 |
|
| 329 |
if (a != NULL) |
| 330 |
(*a) = ret; |
| 331 |
*pp = p; |
| 332 |
return ret; |
| 333 |
err: |
| 334 |
ERR_raise(ERR_LIB_ASN1, i); |
| 335 |
if ((a == NULL) || (*a != ret)) |
| 336 |
ASN1_OBJECT_free(ret); |
| 337 |
return NULL; |
| 338 |
} |
| 339 |
|
| 340 |
ASN1_OBJECT *ASN1_OBJECT_new(void) |
| 341 |
{ |
| 342 |
ASN1_OBJECT *ret; |
| 343 |
|
| 344 |
ret = OPENSSL_zalloc(sizeof(*ret)); |
| 345 |
if (ret == NULL) |
| 346 |
return NULL; |
| 347 |
ret->flags = ASN1_OBJECT_FLAG_DYNAMIC; |
| 348 |
return ret; |
| 349 |
} |
| 350 |
|
| 351 |
void ASN1_OBJECT_free(ASN1_OBJECT *a) |
| 352 |
{ |
| 353 |
if (a == NULL) |
| 354 |
return; |
| 355 |
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { |
| 356 |
#ifndef CONST_STRICT |
| 357 |
/* |
| 358 |
* Disable purely for compile-time strict const checking. Doing this |
| 359 |
* on a "real" compile will cause memory leaks |
| 360 |
*/ |
| 361 |
OPENSSL_free((void *)a->sn); |
| 362 |
OPENSSL_free((void *)a->ln); |
| 363 |
#endif |
| 364 |
a->sn = a->ln = NULL; |
| 365 |
} |
| 366 |
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { |
| 367 |
OPENSSL_free((void *)a->data); |
| 368 |
a->data = NULL; |
| 369 |
a->length = 0; |
| 370 |
} |
| 371 |
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) |
| 372 |
OPENSSL_free(a); |
| 373 |
} |
| 374 |
|
| 375 |
ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, |
| 376 |
const char *sn, const char *ln) |
| 377 |
{ |
| 378 |
ASN1_OBJECT o; |
| 379 |
|
| 380 |
o.sn = sn; |
| 381 |
o.ln = ln; |
| 382 |
o.data = data; |
| 383 |
o.nid = nid; |
| 384 |
o.length = len; |
| 385 |
o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
| 386 |
return OBJ_dup(&o); |
| 387 |
} |