/*
* Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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 "internal/thread_once.h"
#include "internal/tsan_assist.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.txt and obj_mac.{num,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 LHASH_OF(ADDED_OBJ) *added = NULL;
static CRYPTO_RWLOCK *ossl_obj_lock = NULL;
#ifdef TSAN_REQUIRES_LOCKING
static CRYPTO_RWLOCK *ossl_obj_nid_lock = NULL;
#endif
static CRYPTO_ONCE ossl_obj_lock_init = CRYPTO_ONCE_STATIC_INIT;
static ossl_inline void objs_free_locks(void)
{
CRYPTO_THREAD_lock_free(ossl_obj_lock);
ossl_obj_lock = NULL;
#ifdef TSAN_REQUIRES_LOCKING
CRYPTO_THREAD_lock_free(ossl_obj_nid_lock);
ossl_obj_nid_lock = NULL;
#endif
}
DEFINE_RUN_ONCE_STATIC(obj_lock_initialise)
{
ossl_obj_lock = CRYPTO_THREAD_lock_new();
if (ossl_obj_lock == NULL)
return 0;
#ifdef TSAN_REQUIRES_LOCKING
ossl_obj_nid_lock = CRYPTO_THREAD_lock_new();
if (ossl_obj_nid_lock == NULL) {
objs_free_locks();
return 0;
}
#endif
return 1;
}
static ossl_inline int ossl_init_added_lock(void)
{
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
/* Make sure we've loaded config before checking for any "added" objects */
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
#endif
return RUN_ONCE(&ossl_obj_lock_init, obj_lock_initialise);
}
static ossl_inline int ossl_obj_write_lock(int lock)
{
if (!lock)
return 1;
if (!ossl_init_added_lock())
return 0;
return CRYPTO_THREAD_write_lock(ossl_obj_lock);
}
static ossl_inline int ossl_obj_read_lock(int lock)
{
if (!lock)
return 1;
if (!ossl_init_added_lock())
return 0;
return CRYPTO_THREAD_read_lock(ossl_obj_lock);
}
static ossl_inline void ossl_obj_unlock(int lock)
{
if (lock)
CRYPTO_THREAD_unlock(ossl_obj_lock);
}
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 = (unsigned long)a->length << 20UL;
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->