/*
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include "crypto/ctype.h"
#include <limits.h>
#include "internal/cryptlib.h"
#include <openssl/lhash.h>
#include <openssl/asn1.h>
#include "crypto/objects.h"
#include <openssl/bn.h>
#include "crypto/asn1.h"
#include "obj_local.h"
/* obj_dat.h is generated from objects.h by obj_dat.pl */
#include "obj_dat.h"
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
#define ADDED_DATA 0
#define ADDED_SNAME 1
#define ADDED_LNAME 2
#define ADDED_NID 3
struct added_obj_st {
int type;
ASN1_OBJECT *obj;
};
static int new_nid = NUM_NID;
static LHASH_OF(ADDED_OBJ) *added = NULL;
static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
{
return strcmp((*a)->sn, nid_objs[*b].sn);
}
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
{
return strcmp((*a)->ln, nid_objs[*b].ln);
}
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
static unsigned long added_obj_hash(const ADDED_OBJ *ca)
{
const ASN1_OBJECT *a;
int i;
unsigned long ret = 0;
unsigned char *p;
a = ca->obj;
switch (ca->type) {
case ADDED_DATA:
ret = a->length << 20L;
p = (unsigned char *)a->data;
for (i = 0; i < a->length; i++)
ret ^= p[i] << ((i * 3) % 24);
break;
case ADDED_SNAME:
ret = OPENSSL_LH_strhash(a->sn);
break;
case ADDED_LNAME:
ret = OPENSSL_LH_strhash(a->ln);
break;
case ADDED_NID:
ret = a->nid;
break;
default:
/* abort(); */
return 0;
}
ret &= 0x3fffffffL;
ret |= ((unsigned long)ca->type) << 30L;
return ret;
}
static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
{
ASN1_OBJECT *a, *b;
int i;
i = ca->type - cb->type;
if (i)
return i;
a = ca->obj;
b = cb->obj;
switch (ca->type) {
case ADDED_DATA:
i = (a->length - b->length);
if (i)
return i;
return memcmp(a->data, b->data, (size_t)a->length);
case ADDED_SNAME:
if (a->sn == NULL)
return -1;
else if (b->sn == NULL)
return 1;
else
return strcmp(a->sn, b->sn);
case ADDED_LNAME:
if (a->ln == NULL)
return -1;
else if (b->ln == NULL)
return 1;
else
return strcmp(a->ln, b->ln);
case ADDED_NID:
return a->nid - b->nid;
default:
/* abort(); */
return 0;
}
}
static int init_added(void)
{
if (added != NULL)
return 1;
added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
return added != NULL;
}
static void cleanup1_doall(ADDED_OBJ *a)
{
a->obj->nid = 0;
a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
}
static void cleanup2_doall(ADDED_OBJ *a)
{
a->obj->nid++;
}
static void cleanup3_doall(ADDED_OBJ *a)
{
if (--a->obj->nid == 0)
ASN1_OBJECT_free(a->obj);
OPENSSL_free(a);
}
void obj_cleanup_int(void)
{
if (added == NULL)
return;
lh_ADDED_OBJ_set_down_load(added, 0);
lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
lh_ADDED_OBJ_free(added);
added = NULL;
}
int OBJ_new_nid(int num)
{
int i;
i = new_nid;
new_nid += num;
return i;
}
int OBJ_add_object(const ASN1_OBJECT *obj)
{
ASN1_OBJECT *o<