/src/openssl111/crypto/objects/obj_dat.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the OpenSSL license (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 "crypto/ctype.h" | 
| 12 |  | #include <limits.h> | 
| 13 |  | #include "internal/cryptlib.h" | 
| 14 |  | #include <openssl/lhash.h> | 
| 15 |  | #include <openssl/asn1.h> | 
| 16 |  | #include "crypto/objects.h" | 
| 17 |  | #include <openssl/bn.h> | 
| 18 |  | #include "crypto/asn1.h" | 
| 19 |  | #include "obj_local.h" | 
| 20 |  |  | 
| 21 |  | /* obj_dat.h is generated from objects.h by obj_dat.pl */ | 
| 22 |  | #include "obj_dat.h" | 
| 23 |  |  | 
| 24 |  | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); | 
| 25 |  | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); | 
| 26 |  | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); | 
| 27 |  |  | 
| 28 | 0 | #define ADDED_DATA      0 | 
| 29 | 0 | #define ADDED_SNAME     1 | 
| 30 | 0 | #define ADDED_LNAME     2 | 
| 31 | 0 | #define ADDED_NID       3 | 
| 32 |  |  | 
| 33 |  | struct added_obj_st { | 
| 34 |  |     int type; | 
| 35 |  |     ASN1_OBJECT *obj; | 
| 36 |  | }; | 
| 37 |  |  | 
| 38 |  | static int new_nid = NUM_NID; | 
| 39 |  | static LHASH_OF(ADDED_OBJ) *added = NULL; | 
| 40 |  |  | 
| 41 |  | static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) | 
| 42 | 8.61M | { | 
| 43 | 8.61M |     return strcmp((*a)->sn, nid_objs[*b].sn); | 
| 44 | 8.61M | } | 
| 45 |  |  | 
| 46 |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); | 
| 47 |  |  | 
| 48 |  | static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) | 
| 49 | 4.48M | { | 
| 50 | 4.48M |     return strcmp((*a)->ln, nid_objs[*b].ln); | 
| 51 | 4.48M | } | 
| 52 |  |  | 
| 53 |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); | 
| 54 |  |  | 
| 55 |  | static unsigned long added_obj_hash(const ADDED_OBJ *ca) | 
| 56 | 0 | { | 
| 57 | 0 |     const ASN1_OBJECT *a; | 
| 58 | 0 |     int i; | 
| 59 | 0 |     unsigned long ret = 0; | 
| 60 | 0 |     unsigned char *p; | 
| 61 |  | 
 | 
| 62 | 0 |     a = ca->obj; | 
| 63 | 0 |     switch (ca->type) { | 
| 64 | 0 |     case ADDED_DATA: | 
| 65 | 0 |         ret = a->length << 20L; | 
| 66 | 0 |         p = (unsigned char *)a->data; | 
| 67 | 0 |         for (i = 0; i < a->length; i++) | 
| 68 | 0 |             ret ^= p[i] << ((i * 3) % 24); | 
| 69 | 0 |         break; | 
| 70 | 0 |     case ADDED_SNAME: | 
| 71 | 0 |         ret = OPENSSL_LH_strhash(a->sn); | 
| 72 | 0 |         break; | 
| 73 | 0 |     case ADDED_LNAME: | 
| 74 | 0 |         ret = OPENSSL_LH_strhash(a->ln); | 
| 75 | 0 |         break; | 
| 76 | 0 |     case ADDED_NID: | 
| 77 | 0 |         ret = a->nid; | 
| 78 | 0 |         break; | 
| 79 | 0 |     default: | 
| 80 |  |         /* abort(); */ | 
| 81 | 0 |         return 0; | 
| 82 | 0 |     } | 
| 83 | 0 |     ret &= 0x3fffffffL; | 
| 84 | 0 |     ret |= ((unsigned long)ca->type) << 30L; | 
| 85 | 0 |     return ret; | 
| 86 | 0 | } | 
| 87 |  |  | 
| 88 |  | static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb) | 
| 89 | 0 | { | 
| 90 | 0 |     ASN1_OBJECT *a, *b; | 
| 91 | 0 |     int i; | 
| 92 |  | 
 | 
| 93 | 0 |     i = ca->type - cb->type; | 
| 94 | 0 |     if (i) | 
| 95 | 0 |         return i; | 
| 96 | 0 |     a = ca->obj; | 
| 97 | 0 |     b = cb->obj; | 
| 98 | 0 |     switch (ca->type) { | 
| 99 | 0 |     case ADDED_DATA: | 
| 100 | 0 |         i = (a->length - b->length); | 
| 101 | 0 |         if (i) | 
| 102 | 0 |             return i; | 
| 103 | 0 |         return memcmp(a->data, b->data, (size_t)a->length); | 
| 104 | 0 |     case ADDED_SNAME: | 
| 105 | 0 |         if (a->sn == NULL) | 
| 106 | 0 |             return -1; | 
| 107 | 0 |         else if (b->sn == NULL) | 
| 108 | 0 |             return 1; | 
| 109 | 0 |         else | 
| 110 | 0 |             return strcmp(a->sn, b->sn); | 
| 111 | 0 |     case ADDED_LNAME: | 
| 112 | 0 |         if (a->ln == NULL) | 
| 113 | 0 |             return -1; | 
| 114 | 0 |         else if (b->ln == NULL) | 
| 115 | 0 |             return 1; | 
| 116 | 0 |         else | 
| 117 | 0 |             return strcmp(a->ln, b->ln); | 
| 118 | 0 |     case ADDED_NID: | 
| 119 | 0 |         return a->nid - b->nid; | 
| 120 | 0 |     default: | 
| 121 |  |         /* abort(); */ | 
| 122 | 0 |         return 0; | 
| 123 | 0 |     } | 
| 124 | 0 | } | 
| 125 |  |  | 
| 126 |  | static int init_added(void) | 
| 127 | 0 | { | 
| 128 | 0 |     if (added != NULL) | 
| 129 | 0 |         return 1; | 
| 130 | 0 |     added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp); | 
| 131 | 0 |     return added != NULL; | 
| 132 | 0 | } | 
| 133 |  |  | 
| 134 |  | static void cleanup1_doall(ADDED_OBJ *a) | 
| 135 | 0 | { | 
| 136 | 0 |     a->obj->nid = 0; | 
| 137 | 0 |     a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC | | 
| 138 | 0 |         ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA; | 
| 139 | 0 | } | 
| 140 |  |  | 
| 141 |  | static void cleanup2_doall(ADDED_OBJ *a) | 
| 142 | 0 | { | 
| 143 | 0 |     a->obj->nid++; | 
| 144 | 0 | } | 
| 145 |  |  | 
| 146 |  | static void cleanup3_doall(ADDED_OBJ *a) | 
| 147 | 0 | { | 
| 148 | 0 |     if (--a->obj->nid == 0) | 
| 149 | 0 |         ASN1_OBJECT_free(a->obj); | 
| 150 | 0 |     OPENSSL_free(a); | 
| 151 | 0 | } | 
| 152 |  |  | 
| 153 |  | void obj_cleanup_int(void) | 
| 154 | 22 | { | 
| 155 | 22 |     if (added == NULL) | 
| 156 | 22 |         return; | 
| 157 | 0 |     lh_ADDED_OBJ_set_down_load(added, 0); | 
| 158 | 0 |     lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */ | 
| 159 | 0 |     lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */ | 
| 160 | 0 |     lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */ | 
| 161 | 0 |     lh_ADDED_OBJ_free(added); | 
| 162 | 0 |     added = NULL; | 
| 163 | 0 | } | 
| 164 |  |  | 
| 165 |  | int OBJ_new_nid(int num) | 
| 166 | 0 | { | 
| 167 | 0 |     int i; | 
| 168 |  | 
 | 
| 169 | 0 |     i = new_nid; | 
| 170 | 0 |     new_nid += num; | 
| 171 | 0 |     return i; | 
| 172 | 0 | } | 
| 173 |  |  | 
| 174 |  | int OBJ_add_object(const ASN1_OBJECT *obj) | 
| 175 | 0 | { | 
| 176 | 0 |     ASN1_OBJECT *o; | 
| 177 | 0 |     ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop; | 
| 178 | 0 |     int i; | 
| 179 |  | 
 | 
| 180 | 0 |     if (added == NULL) | 
| 181 | 0 |         if (!init_added()) | 
| 182 | 0 |             return 0; | 
| 183 | 0 |     if ((o = OBJ_dup(obj)) == NULL) | 
| 184 | 0 |         goto err; | 
| 185 | 0 |     if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) | 
| 186 | 0 |         goto err2; | 
| 187 | 0 |     if ((o->length != 0) && (obj->data != NULL)) | 
| 188 | 0 |         if ((ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) | 
| 189 | 0 |             goto err2; | 
| 190 | 0 |     if (o->sn != NULL) | 
| 191 | 0 |         if ((ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) | 
| 192 | 0 |             goto err2; | 
| 193 | 0 |     if (o->ln != NULL) | 
| 194 | 0 |         if ((ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) | 
| 195 | 0 |             goto err2; | 
| 196 |  |  | 
| 197 | 0 |     for (i = ADDED_DATA; i <= ADDED_NID; i++) { | 
| 198 | 0 |         if (ao[i] != NULL) { | 
| 199 | 0 |             ao[i]->type = i; | 
| 200 | 0 |             ao[i]->obj = o; | 
| 201 | 0 |             aop = lh_ADDED_OBJ_insert(added, ao[i]); | 
| 202 |  |             /* memory leak, but should not normally matter */ | 
| 203 | 0 |             OPENSSL_free(aop); | 
| 204 | 0 |         } | 
| 205 | 0 |     } | 
| 206 | 0 |     o->flags &= | 
| 207 | 0 |         ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | | 
| 208 | 0 |           ASN1_OBJECT_FLAG_DYNAMIC_DATA); | 
| 209 |  | 
 | 
| 210 | 0 |     return o->nid; | 
| 211 | 0 |  err2: | 
| 212 | 0 |     OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE); | 
| 213 | 0 |  err: | 
| 214 | 0 |     for (i = ADDED_DATA; i <= ADDED_NID; i++) | 
| 215 | 0 |         OPENSSL_free(ao[i]); | 
| 216 | 0 |     ASN1_OBJECT_free(o); | 
| 217 | 0 |     return NID_undef; | 
| 218 | 0 | } | 
| 219 |  |  | 
| 220 |  | ASN1_OBJECT *OBJ_nid2obj(int n) | 
| 221 | 10.5M | { | 
| 222 | 10.5M |     ADDED_OBJ ad, *adp; | 
| 223 | 10.5M |     ASN1_OBJECT ob; | 
| 224 |  |  | 
| 225 | 10.5M |     if ((n >= 0) && (n < NUM_NID)) { | 
| 226 | 10.5M |         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { | 
| 227 | 0 |             OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID); | 
| 228 | 0 |             return NULL; | 
| 229 | 0 |         } | 
| 230 | 10.5M |         return (ASN1_OBJECT *)&(nid_objs[n]); | 
| 231 | 10.5M |     } else if (added == NULL) { | 
| 232 | 0 |         OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID); | 
| 233 | 0 |         return NULL; | 
| 234 | 0 |     } else { | 
| 235 | 0 |         ad.type = ADDED_NID; | 
| 236 | 0 |         ad.obj = &ob; | 
| 237 | 0 |         ob.nid = n; | 
| 238 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 239 | 0 |         if (adp != NULL) | 
| 240 | 0 |             return adp->obj; | 
| 241 | 0 |         else { | 
| 242 | 0 |             OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID); | 
| 243 | 0 |             return NULL; | 
| 244 | 0 |         } | 
| 245 | 0 |     } | 
| 246 | 10.5M | } | 
| 247 |  |  | 
| 248 |  | const char *OBJ_nid2sn(int n) | 
| 249 | 49.1k | { | 
| 250 | 49.1k |     ADDED_OBJ ad, *adp; | 
| 251 | 49.1k |     ASN1_OBJECT ob; | 
| 252 |  |  | 
| 253 | 49.1k |     if ((n >= 0) && (n < NUM_NID)) { | 
| 254 | 49.1k |         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { | 
| 255 | 0 |             OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID); | 
| 256 | 0 |             return NULL; | 
| 257 | 0 |         } | 
| 258 | 49.1k |         return nid_objs[n].sn; | 
| 259 | 49.1k |     } else if (added == NULL) | 
| 260 | 0 |         return NULL; | 
| 261 | 0 |     else { | 
| 262 | 0 |         ad.type = ADDED_NID; | 
| 263 | 0 |         ad.obj = &ob; | 
| 264 | 0 |         ob.nid = n; | 
| 265 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 266 | 0 |         if (adp != NULL) | 
| 267 | 0 |             return adp->obj->sn; | 
| 268 | 0 |         else { | 
| 269 | 0 |             OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID); | 
| 270 | 0 |             return NULL; | 
| 271 | 0 |         } | 
| 272 | 0 |     } | 
| 273 | 49.1k | } | 
| 274 |  |  | 
| 275 |  | const char *OBJ_nid2ln(int n) | 
| 276 | 543k | { | 
| 277 | 543k |     ADDED_OBJ ad, *adp; | 
| 278 | 543k |     ASN1_OBJECT ob; | 
| 279 |  |  | 
| 280 | 543k |     if ((n >= 0) && (n < NUM_NID)) { | 
| 281 | 543k |         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { | 
| 282 | 0 |             OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID); | 
| 283 | 0 |             return NULL; | 
| 284 | 0 |         } | 
| 285 | 543k |         return nid_objs[n].ln; | 
| 286 | 543k |     } else if (added == NULL) | 
| 287 | 0 |         return NULL; | 
| 288 | 0 |     else { | 
| 289 | 0 |         ad.type = ADDED_NID; | 
| 290 | 0 |         ad.obj = &ob; | 
| 291 | 0 |         ob.nid = n; | 
| 292 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 293 | 0 |         if (adp != NULL) | 
| 294 | 0 |             return adp->obj->ln; | 
| 295 | 0 |         else { | 
| 296 | 0 |             OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID); | 
| 297 | 0 |             return NULL; | 
| 298 | 0 |         } | 
| 299 | 0 |     } | 
| 300 | 543k | } | 
| 301 |  |  | 
| 302 |  | static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp) | 
| 303 | 186M | { | 
| 304 | 186M |     int j; | 
| 305 | 186M |     const ASN1_OBJECT *a = *ap; | 
| 306 | 186M |     const ASN1_OBJECT *b = &nid_objs[*bp]; | 
| 307 |  |  | 
| 308 | 186M |     j = (a->length - b->length); | 
| 309 | 186M |     if (j) | 
| 310 | 115M |         return j; | 
| 311 | 71.0M |     if (a->length == 0) | 
| 312 | 0 |         return 0; | 
| 313 | 71.0M |     return memcmp(a->data, b->data, a->length); | 
| 314 | 71.0M | } | 
| 315 |  |  | 
| 316 |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); | 
| 317 |  |  | 
| 318 |  | int OBJ_obj2nid(const ASN1_OBJECT *a) | 
| 319 | 17.8M | { | 
| 320 | 17.8M |     const unsigned int *op; | 
| 321 | 17.8M |     ADDED_OBJ ad, *adp; | 
| 322 |  |  | 
| 323 | 17.8M |     if (a == NULL) | 
| 324 | 0 |         return NID_undef; | 
| 325 | 17.8M |     if (a->nid != 0) | 
| 326 | 6.70M |         return a->nid; | 
| 327 |  |  | 
| 328 | 11.1M |     if (a->length == 0) | 
| 329 | 117k |         return NID_undef; | 
| 330 |  |  | 
| 331 | 11.0M |     if (added != NULL) { | 
| 332 | 0 |         ad.type = ADDED_DATA; | 
| 333 | 0 |         ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */ | 
| 334 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 335 | 0 |         if (adp != NULL) | 
| 336 | 0 |             return adp->obj->nid; | 
| 337 | 0 |     } | 
| 338 | 11.0M |     op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); | 
| 339 | 11.0M |     if (op == NULL) | 
| 340 | 7.76M |         return NID_undef; | 
| 341 | 3.29M |     return nid_objs[*op].nid; | 
| 342 | 11.0M | } | 
| 343 |  |  | 
| 344 |  | /* | 
| 345 |  |  * Convert an object name into an ASN1_OBJECT if "noname" is not set then | 
| 346 |  |  * search for short and long names first. This will convert the "dotted" form | 
| 347 |  |  * into an object: unlike OBJ_txt2nid it can be used with any objects, not | 
| 348 |  |  * just registered ones. | 
| 349 |  |  */ | 
| 350 |  |  | 
| 351 |  | ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) | 
| 352 |  | { | 
| 353 |  |     int nid = NID_undef; | 
| 354 |  |     ASN1_OBJECT *op; | 
| 355 |  |     unsigned char *buf; | 
| 356 |  |     unsigned char *p; | 
| 357 |  |     const unsigned char *cp; | 
| 358 |  |     int i, j; | 
| 359 |  |  | 
| 360 |  |     if (!no_name) { | 
| 361 |  |         if (((nid = OBJ_sn2nid(s)) != NID_undef) || | 
| 362 |  |             ((nid = OBJ_ln2nid(s)) != NID_undef)) | 
| 363 |  |             return OBJ_nid2obj(nid); | 
| 364 |  |     } | 
| 365 |  |  | 
| 366 |  |     /* Work out size of content octets */ | 
| 367 |  |     i = a2d_ASN1_OBJECT(NULL, 0, s, -1); | 
| 368 |  |     if (i <= 0) { | 
| 369 |  |         /* Don't clear the error */ | 
| 370 |  |         /* | 
| 371 |  |          * ERR_clear_error(); | 
| 372 |  |          */ | 
| 373 |  |         return NULL; | 
| 374 |  |     } | 
| 375 |  |     /* Work out total size */ | 
| 376 |  |     j = ASN1_object_size(0, i, V_ASN1_OBJECT); | 
| 377 |  |     if (j < 0) | 
| 378 |  |         return NULL; | 
| 379 |  |  | 
| 380 |  |     if ((buf = OPENSSL_malloc(j)) == NULL) { | 
| 381 |  |         OBJerr(OBJ_F_OBJ_TXT2OBJ, ERR_R_MALLOC_FAILURE); | 
| 382 |  |         return NULL; | 
| 383 |  |     } | 
| 384 |  |  | 
| 385 |  |     p = buf; | 
| 386 |  |     /* Write out tag+length */ | 
| 387 |  |     ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); | 
| 388 |  |     /* Write out contents */ | 
| 389 |  |     a2d_ASN1_OBJECT(p, i, s, -1); | 
| 390 |  |  | 
| 391 |  |     cp = buf; | 
| 392 |  |     op = d2i_ASN1_OBJECT(NULL, &cp, j); | 
| 393 |  |     OPENSSL_free(buf); | 
| 394 |  |     return op; | 
| 395 |  | } | 
| 396 |  |  | 
| 397 |  | int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) | 
| 398 | 2.83M | { | 
| 399 | 2.83M |     int i, n = 0, len, nid, first, use_bn; | 
| 400 | 2.83M |     BIGNUM *bl; | 
| 401 | 2.83M |     unsigned long l; | 
| 402 | 2.83M |     const unsigned char *p; | 
| 403 | 2.83M |     char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2]; | 
| 404 |  |  | 
| 405 |  |     /* Ensure that, at every state, |buf| is NUL-terminated. */ | 
| 406 | 2.83M |     if (buf && buf_len > 0) | 
| 407 | 2.83M |         buf[0] = '\0'; | 
| 408 |  |  | 
| 409 | 2.83M |     if ((a == NULL) || (a->data == NULL)) | 
| 410 | 7 |         return 0; | 
| 411 |  |  | 
| 412 | 2.83M |     if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) { | 
| 413 | 1.41M |         const char *s; | 
| 414 | 1.41M |         s = OBJ_nid2ln(nid); | 
| 415 | 1.41M |         if (s == NULL) | 
| 416 | 0 |             s = OBJ_nid2sn(nid); | 
| 417 | 1.41M |         if (s) { | 
| 418 | 1.41M |             if (buf) | 
| 419 | 1.41M |                 OPENSSL_strlcpy(buf, s, buf_len); | 
| 420 | 1.41M |             n = strlen(s); | 
| 421 | 1.41M |             return n; | 
| 422 | 1.41M |         } | 
| 423 | 1.41M |     } | 
| 424 |  |  | 
| 425 | 1.42M |     len = a->length; | 
| 426 | 1.42M |     p = a->data; | 
| 427 |  |  | 
| 428 | 1.42M |     first = 1; | 
| 429 | 1.42M |     bl = NULL; | 
| 430 |  |  | 
| 431 |  |     /* | 
| 432 |  |      * RFC 2578 (STD 58) says this about OBJECT IDENTIFIERs: | 
| 433 |  |      * | 
| 434 |  |      * > 3.5. OBJECT IDENTIFIER values | 
| 435 |  |      * > | 
| 436 |  |      * > An OBJECT IDENTIFIER value is an ordered list of non-negative | 
| 437 |  |      * > numbers. For the SMIv2, each number in the list is referred to as a | 
| 438 |  |      * > sub-identifier, there are at most 128 sub-identifiers in a value, | 
| 439 |  |      * > and each sub-identifier has a maximum value of 2^32-1 (4294967295 | 
| 440 |  |      * > decimal). | 
| 441 |  |      * | 
| 442 |  |      * So a legitimate OID according to this RFC is at most (32 * 128 / 7), | 
| 443 |  |      * i.e. 586 bytes long. | 
| 444 |  |      * | 
| 445 |  |      * Ref: https://datatracker.ietf.org/doc/html/rfc2578#section-3.5 | 
| 446 |  |      */ | 
| 447 | 1.42M |     if (len > 586) | 
| 448 | 2.51k |         goto err; | 
| 449 |  |  | 
| 450 | 4.55M |     while (len > 0) { | 
| 451 | 3.12M |         l = 0; | 
| 452 | 3.12M |         use_bn = 0; | 
| 453 | 5.01M |         for (;;) { | 
| 454 | 5.01M |             unsigned char c = *p++; | 
| 455 | 5.01M |             len--; | 
| 456 | 5.01M |             if ((len == 0) && (c & 0x80)) | 
| 457 | 0 |                 goto err; | 
| 458 | 5.01M |             if (use_bn) { | 
| 459 | 940k |                 if (!BN_add_word(bl, c & 0x7f)) | 
| 460 | 0 |                     goto err; | 
| 461 | 940k |             } else | 
| 462 | 4.07M |                 l |= c & 0x7f; | 
| 463 | 5.01M |             if (!(c & 0x80)) | 
| 464 | 3.12M |                 break; | 
| 465 | 1.88M |             if (!use_bn && (l > (ULONG_MAX >> 7L))) { | 
| 466 | 55.7k |                 if (bl == NULL && (bl = BN_new()) == NULL) | 
| 467 | 0 |                     goto err; | 
| 468 | 55.7k |                 if (!BN_set_word(bl, l)) | 
| 469 | 0 |                     goto err; | 
| 470 | 55.7k |                 use_bn = 1; | 
| 471 | 55.7k |             } | 
| 472 | 1.88M |             if (use_bn) { | 
| 473 | 940k |                 if (!BN_lshift(bl, bl, 7)) | 
| 474 | 0 |                     goto err; | 
| 475 | 940k |             } else | 
| 476 | 948k |                 l <<= 7L; | 
| 477 | 1.88M |         } | 
| 478 |  |  | 
| 479 | 3.12M |         if (first) { | 
| 480 | 1.42M |             first = 0; | 
| 481 | 1.42M |             if (l >= 80) { | 
| 482 | 351k |                 i = 2; | 
| 483 | 351k |                 if (use_bn) { | 
| 484 | 16.9k |                     if (!BN_sub_word(bl, 80)) | 
| 485 | 0 |                         goto err; | 
| 486 | 16.9k |                 } else | 
| 487 | 334k |                     l -= 80; | 
| 488 | 1.07M |             } else { | 
| 489 | 1.07M |                 i = (int)(l / 40); | 
| 490 | 1.07M |                 l -= (long)(i * 40); | 
| 491 | 1.07M |             } | 
| 492 | 1.42M |             if (buf && (buf_len > 1)) { | 
| 493 | 1.42M |                 *buf++ = i + '0'; | 
| 494 | 1.42M |                 *buf = '\0'; | 
| 495 | 1.42M |                 buf_len--; | 
| 496 | 1.42M |             } | 
| 497 | 1.42M |             n++; | 
| 498 | 1.42M |         } | 
| 499 |  |  | 
| 500 | 3.12M |         if (use_bn) { | 
| 501 | 55.7k |             char *bndec; | 
| 502 | 55.7k |             bndec = BN_bn2dec(bl); | 
| 503 | 55.7k |             if (!bndec) | 
| 504 | 0 |                 goto err; | 
| 505 | 55.7k |             i = strlen(bndec); | 
| 506 | 55.7k |             if (buf) { | 
| 507 | 55.7k |                 if (buf_len > 1) { | 
| 508 | 40.1k |                     *buf++ = '.'; | 
| 509 | 40.1k |                     *buf = '\0'; | 
| 510 | 40.1k |                     buf_len--; | 
| 511 | 40.1k |                 } | 
| 512 | 55.7k |                 OPENSSL_strlcpy(buf, bndec, buf_len); | 
| 513 | 55.7k |                 if (i > buf_len) { | 
| 514 | 28.0k |                     buf += buf_len; | 
| 515 | 28.0k |                     buf_len = 0; | 
| 516 | 28.0k |                 } else { | 
| 517 | 27.6k |                     buf += i; | 
| 518 | 27.6k |                     buf_len -= i; | 
| 519 | 27.6k |                 } | 
| 520 | 55.7k |             } | 
| 521 | 55.7k |             n++; | 
| 522 | 55.7k |             n += i; | 
| 523 | 55.7k |             OPENSSL_free(bndec); | 
| 524 | 3.07M |         } else { | 
| 525 | 3.07M |             BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l); | 
| 526 | 3.07M |             i = strlen(tbuf); | 
| 527 | 3.07M |             if (buf && (buf_len > 0)) { | 
| 528 | 2.79M |                 OPENSSL_strlcpy(buf, tbuf, buf_len); | 
| 529 | 2.79M |                 if (i > buf_len) { | 
| 530 | 6.45k |                     buf += buf_len; | 
| 531 | 6.45k |                     buf_len = 0; | 
| 532 | 2.79M |                 } else { | 
| 533 | 2.79M |                     buf += i; | 
| 534 | 2.79M |                     buf_len -= i; | 
| 535 | 2.79M |                 } | 
| 536 | 2.79M |             } | 
| 537 | 3.07M |             n += i; | 
| 538 | 3.07M |             l = 0; | 
| 539 | 3.07M |         } | 
| 540 | 3.12M |     } | 
| 541 |  |  | 
| 542 | 1.42M |     BN_free(bl); | 
| 543 | 1.42M |     return n; | 
| 544 |  |  | 
| 545 | 2.51k |  err: | 
| 546 | 2.51k |     BN_free(bl); | 
| 547 | 2.51k |     return -1; | 
| 548 | 1.42M | } | 
| 549 |  |  | 
| 550 |  | int OBJ_txt2nid(const char *s) | 
| 551 | 3.00k | { | 
| 552 | 3.00k |     ASN1_OBJECT *obj; | 
| 553 | 3.00k |     int nid; | 
| 554 | 3.00k |     obj = OBJ_txt2obj(s, 0); | 
| 555 | 3.00k |     nid = OBJ_obj2nid(obj); | 
| 556 | 3.00k |     ASN1_OBJECT_free(obj); | 
| 557 | 3.00k |     return nid; | 
| 558 | 3.00k | } | 
| 559 |  |  | 
| 560 |  | int OBJ_ln2nid(const char *s) | 
| 561 | 297k | { | 
| 562 | 297k |     ASN1_OBJECT o; | 
| 563 | 297k |     const ASN1_OBJECT *oo = &o; | 
| 564 | 297k |     ADDED_OBJ ad, *adp; | 
| 565 | 297k |     const unsigned int *op; | 
| 566 |  |  | 
| 567 | 297k |     o.ln = s; | 
| 568 | 297k |     if (added != NULL) { | 
| 569 | 0 |         ad.type = ADDED_LNAME; | 
| 570 | 0 |         ad.obj = &o; | 
| 571 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 572 | 0 |         if (adp != NULL) | 
| 573 | 0 |             return adp->obj->nid; | 
| 574 | 0 |     } | 
| 575 | 297k |     op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN); | 
| 576 | 297k |     if (op == NULL) | 
| 577 | 282k |         return NID_undef; | 
| 578 | 14.8k |     return nid_objs[*op].nid; | 
| 579 | 297k | } | 
| 580 |  |  | 
| 581 |  | int OBJ_sn2nid(const char *s) | 
| 582 | 480k | { | 
| 583 | 480k |     ASN1_OBJECT o; | 
| 584 | 480k |     const ASN1_OBJECT *oo = &o; | 
| 585 | 480k |     ADDED_OBJ ad, *adp; | 
| 586 | 480k |     const unsigned int *op; | 
| 587 |  |  | 
| 588 | 480k |     o.sn = s; | 
| 589 | 480k |     if (added != NULL) { | 
| 590 | 0 |         ad.type = ADDED_SNAME; | 
| 591 | 0 |         ad.obj = &o; | 
| 592 | 0 |         adp = lh_ADDED_OBJ_retrieve(added, &ad); | 
| 593 | 0 |         if (adp != NULL) | 
| 594 | 0 |             return adp->obj->nid; | 
| 595 | 0 |     } | 
| 596 | 480k |     op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN); | 
| 597 | 480k |     if (op == NULL) | 
| 598 | 293k |         return NID_undef; | 
| 599 | 186k |     return nid_objs[*op].nid; | 
| 600 | 480k | } | 
| 601 |  |  | 
| 602 |  | const void *OBJ_bsearch_(const void *key, const void *base, int num, int size, | 
| 603 |  |                          int (*cmp) (const void *, const void *)) | 
| 604 | 24.7M | { | 
| 605 | 24.7M |     return OBJ_bsearch_ex_(key, base, num, size, cmp, 0); | 
| 606 | 24.7M | } | 
| 607 |  |  | 
| 608 |  | const void *OBJ_bsearch_ex_(const void *key, const void *base_, int num, | 
| 609 |  |                             int size, | 
| 610 |  |                             int (*cmp) (const void *, const void *), | 
| 611 |  |                             int flags) | 
| 612 | 6.83M | { | 
| 613 | 6.83M |     const char *base = base_; | 
| 614 | 6.83M |     int l, h, i = 0, c = 0; | 
| 615 | 6.83M |     const char *p = NULL; | 
| 616 |  |  | 
| 617 | 6.83M |     if (num == 0) | 
| 618 | 0 |         return NULL; | 
| 619 | 6.83M |     l = 0; | 
| 620 | 6.83M |     h = num; | 
| 621 | 65.6M |     while (l < h) { | 
| 622 | 60.9M |         i = (l + h) / 2; | 
| 623 | 60.9M |         p = &(base[i * size]); | 
| 624 | 60.9M |         c = (*cmp) (key, p); | 
| 625 | 60.9M |         if (c < 0) | 
| 626 | 44.0M |             h = i; | 
| 627 | 16.9M |         else if (c > 0) | 
| 628 | 14.7M |             l = i + 1; | 
| 629 | 2.13M |         else | 
| 630 | 2.13M |             break; | 
| 631 | 60.9M |     } | 
| 632 |  | #ifdef CHARSET_EBCDIC | 
| 633 |  |     /* | 
| 634 |  |      * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I | 
| 635 |  |      * don't have perl (yet), we revert to a *LINEAR* search when the object | 
| 636 |  |      * wasn't found in the binary search. | 
| 637 |  |      */ | 
| 638 |  |     if (c != 0) { | 
| 639 |  |         for (i = 0; i < num; ++i) { | 
| 640 |  |             p = &(base[i * size]); | 
| 641 |  |             c = (*cmp) (key, p); | 
| 642 |  |             if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))) | 
| 643 |  |                 return p; | 
| 644 |  |         } | 
| 645 |  |     } | 
| 646 |  | #endif | 
| 647 | 6.83M |     if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)) | 
| 648 | 4.69M |         p = NULL; | 
| 649 | 2.13M |     else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) { | 
| 650 | 6.43k |         while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0) | 
| 651 | 0 |             i--; | 
| 652 | 6.43k |         p = &(base[i * size]); | 
| 653 | 6.43k |     } | 
| 654 | 6.83M |     return p; | 
| 655 | 6.83M | } | 
| 656 |  |  | 
| 657 |  | /* | 
| 658 |  |  * Parse a BIO sink to create some extra oid's objects. | 
| 659 |  |  * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN> | 
| 660 |  |  */ | 
| 661 |  | int OBJ_create_objects(BIO *in) | 
| 662 | 0 | { | 
| 663 | 0 |     char buf[512]; | 
| 664 | 0 |     int i, num = 0; | 
| 665 | 0 |     char *o, *s, *l = NULL; | 
| 666 |  | 
 | 
| 667 | 0 |     for (;;) { | 
| 668 | 0 |         s = o = NULL; | 
| 669 | 0 |         i = BIO_gets(in, buf, 512); | 
| 670 | 0 |         if (i <= 0) | 
| 671 | 0 |             return num; | 
| 672 | 0 |         buf[i - 1] = '\0'; | 
| 673 | 0 |         if (!ossl_isalnum(buf[0])) | 
| 674 | 0 |             return num; | 
| 675 | 0 |         o = s = buf; | 
| 676 | 0 |         while (ossl_isdigit(*s) || *s == '.') | 
| 677 | 0 |             s++; | 
| 678 | 0 |         if (*s != '\0') { | 
| 679 | 0 |             *(s++) = '\0'; | 
| 680 | 0 |             while (ossl_isspace(*s)) | 
| 681 | 0 |                 s++; | 
| 682 | 0 |             if (*s == '\0') { | 
| 683 | 0 |                 s = NULL; | 
| 684 | 0 |             } else { | 
| 685 | 0 |                 l = s; | 
| 686 | 0 |                 while (*l != '\0' && !ossl_isspace(*l)) | 
| 687 | 0 |                     l++; | 
| 688 | 0 |                 if (*l != '\0') { | 
| 689 | 0 |                     *(l++) = '\0'; | 
| 690 | 0 |                     while (ossl_isspace(*l)) | 
| 691 | 0 |                         l++; | 
| 692 | 0 |                     if (*l == '\0') { | 
| 693 | 0 |                         l = NULL; | 
| 694 | 0 |                     } | 
| 695 | 0 |                 } else { | 
| 696 | 0 |                     l = NULL; | 
| 697 | 0 |                 } | 
| 698 | 0 |             } | 
| 699 | 0 |         } else { | 
| 700 | 0 |             s = NULL; | 
| 701 | 0 |         } | 
| 702 | 0 |         if (*o == '\0') | 
| 703 | 0 |             return num; | 
| 704 | 0 |         if (!OBJ_create(o, s, l)) | 
| 705 | 0 |             return num; | 
| 706 | 0 |         num++; | 
| 707 | 0 |     } | 
| 708 | 0 | } | 
| 709 |  |  | 
| 710 |  | int OBJ_create(const char *oid, const char *sn, const char *ln) | 
| 711 | 0 | { | 
| 712 | 0 |     ASN1_OBJECT *tmpoid = NULL; | 
| 713 | 0 |     int ok = 0; | 
| 714 |  |  | 
| 715 |  |     /* Check to see if short or long name already present */ | 
| 716 | 0 |     if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef) | 
| 717 | 0 |             || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) { | 
| 718 | 0 |         OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS); | 
| 719 | 0 |         return 0; | 
| 720 | 0 |     } | 
| 721 |  |  | 
| 722 |  |     /* Convert numerical OID string to an ASN1_OBJECT structure */ | 
| 723 | 0 |     tmpoid = OBJ_txt2obj(oid, 1); | 
| 724 | 0 |     if (tmpoid == NULL) | 
| 725 | 0 |         return 0; | 
| 726 |  |  | 
| 727 |  |     /* If NID is not NID_undef then object already exists */ | 
| 728 | 0 |     if (OBJ_obj2nid(tmpoid) != NID_undef) { | 
| 729 | 0 |         OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS); | 
| 730 | 0 |         goto err; | 
| 731 | 0 |     } | 
| 732 |  |  | 
| 733 | 0 |     tmpoid->nid = OBJ_new_nid(1); | 
| 734 | 0 |     tmpoid->sn = (char *)sn; | 
| 735 | 0 |     tmpoid->ln = (char *)ln; | 
| 736 |  | 
 | 
| 737 | 0 |     ok = OBJ_add_object(tmpoid); | 
| 738 |  | 
 | 
| 739 | 0 |     tmpoid->sn = NULL; | 
| 740 | 0 |     tmpoid->ln = NULL; | 
| 741 |  | 
 | 
| 742 | 0 |  err: | 
| 743 | 0 |     ASN1_OBJECT_free(tmpoid); | 
| 744 | 0 |     return ok; | 
| 745 | 0 | } | 
| 746 |  |  | 
| 747 |  | size_t OBJ_length(const ASN1_OBJECT *obj) | 
| 748 | 253k | { | 
| 749 | 253k |     if (obj == NULL) | 
| 750 | 0 |         return 0; | 
| 751 | 253k |     return obj->length; | 
| 752 | 253k | } | 
| 753 |  |  | 
| 754 |  | const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj) | 
| 755 | 2.52k | { | 
| 756 | 2.52k |     if (obj == NULL) | 
| 757 | 0 |         return NULL; | 
| 758 | 2.52k |     return obj->data; | 
| 759 | 2.52k | } |