Replaced system SQLite with SQLCipher to support encrypted database
This commit is contained in:
106
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c
Normal file
106
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
typedef struct {
|
||||
enum ltc_oid_id id;
|
||||
enum ltc_pka_id pka;
|
||||
const char* oid;
|
||||
} oid_table_entry;
|
||||
|
||||
static const oid_table_entry pka_oids[] = {
|
||||
{ LTC_OID_UNDEF, LTC_PKA_UNDEF, NULL },
|
||||
{ LTC_OID_RSA, LTC_PKA_RSA, "1.2.840.113549.1.1.1" },
|
||||
{ LTC_OID_DSA, LTC_PKA_DSA, "1.2.840.10040.4.1" },
|
||||
{ LTC_OID_EC, LTC_PKA_EC, "1.2.840.10045.2.1" },
|
||||
{ LTC_OID_EC_PRIMEF, LTC_PKA_EC, "1.2.840.10045.1.1" },
|
||||
{ LTC_OID_X25519, LTC_PKA_X25519, "1.3.101.110" },
|
||||
{ LTC_OID_ED25519, LTC_PKA_ED25519, "1.3.101.112" },
|
||||
{ LTC_OID_DH, LTC_PKA_DH, "1.2.840.113549.1.3.1" },
|
||||
};
|
||||
|
||||
static LTC_INLINE const oid_table_entry* s_get_entry(enum ltc_oid_id id)
|
||||
{
|
||||
if (id < LTC_OID_NUM)
|
||||
return &pka_oids[id];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the OID requested.
|
||||
@return CRYPT_OK if valid
|
||||
*/
|
||||
int pk_get_oid(enum ltc_oid_id id, const char **st)
|
||||
{
|
||||
const oid_table_entry* e = s_get_entry(id);
|
||||
LTC_ARGCHK(st != NULL);
|
||||
if (e != NULL) {
|
||||
*st = e->oid;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the PKA ID requested.
|
||||
@return CRYPT_OK if valid
|
||||
*/
|
||||
int pk_get_pka_id(enum ltc_oid_id id, enum ltc_pka_id *pka)
|
||||
{
|
||||
const oid_table_entry* e = s_get_entry(id);
|
||||
LTC_ARGCHK(pka != NULL);
|
||||
if (e != NULL) {
|
||||
*pka = e->pka;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the OID ID requested.
|
||||
@return CRYPT_OK if valid
|
||||
*/
|
||||
int pk_get_oid_id(enum ltc_pka_id pka, enum ltc_oid_id *oid)
|
||||
{
|
||||
unsigned int i;
|
||||
LTC_ARGCHK(oid != NULL);
|
||||
for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) {
|
||||
if (pka_oids[i].pka == pka) {
|
||||
*oid = pka_oids[i].id;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the PKA ID of an OID.
|
||||
@return CRYPT_OK if valid
|
||||
*/
|
||||
int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id)
|
||||
{
|
||||
unsigned long i;
|
||||
char tmp[LTC_OID_MAX_STRLEN] = { 0 };
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(oid != NULL);
|
||||
LTC_ARGCHK(id != NULL);
|
||||
|
||||
if (oid->type != LTC_ASN1_OBJECT_IDENTIFIER) return CRYPT_INVALID_ARG;
|
||||
|
||||
i = sizeof(tmp);
|
||||
if ((err = pk_oid_num_to_str(oid->data, oid->size, tmp, &i)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) {
|
||||
if (XSTRCMP(pka_oids[i].oid, tmp) == 0) {
|
||||
*id = pka_oids[i].id;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#endif
|
||||
44
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_cmp.c
Normal file
44
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_cmp.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/*
|
||||
Compare an OID string to an array of `unsigned long`.
|
||||
@return CRYPT_OK if equal
|
||||
*/
|
||||
int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size)
|
||||
{
|
||||
unsigned long i;
|
||||
char tmp[LTC_OID_MAX_STRLEN] = { 0 };
|
||||
int err;
|
||||
|
||||
if (o1 == NULL || o2 == NULL) return CRYPT_ERROR;
|
||||
|
||||
i = sizeof(tmp);
|
||||
if ((err = pk_oid_num_to_str(o2, o2size, tmp, &i)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (XSTRCMP(o1, tmp) != 0) {
|
||||
return CRYPT_PK_INVALID_TYPE;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/*
|
||||
Compare an OID string to an OID element decoded from ASN.1.
|
||||
@return CRYPT_OK if equal
|
||||
*/
|
||||
int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2)
|
||||
{
|
||||
if (o1 == NULL || o2 == NULL) return CRYPT_ERROR;
|
||||
|
||||
if (o2->type != LTC_ASN1_OBJECT_IDENTIFIER) return CRYPT_INVALID_ARG;
|
||||
|
||||
return pk_oid_cmp_with_ulong(o1, o2->data, o2->size);
|
||||
}
|
||||
|
||||
#endif
|
||||
88
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c
Normal file
88
Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wconversion"
|
||||
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
|
||||
|
||||
int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen)
|
||||
{
|
||||
unsigned long i, j, limit, oid_j;
|
||||
size_t OID_len;
|
||||
|
||||
LTC_ARGCHK(oidlen != NULL);
|
||||
|
||||
limit = *oidlen;
|
||||
*oidlen = 0; /* make sure that we return zero oidlen on error */
|
||||
for (i = 0; i < limit; i++) oid[i] = 0;
|
||||
|
||||
if (OID == NULL) return CRYPT_OK;
|
||||
|
||||
OID_len = XSTRLEN(OID);
|
||||
if (OID_len == 0) return CRYPT_OK;
|
||||
|
||||
for (i = 0, j = 0; i < OID_len; i++) {
|
||||
if (OID[i] == '.') {
|
||||
if (++j >= limit) continue;
|
||||
}
|
||||
else if ((OID[i] >= '0') && (OID[i] <= '9')) {
|
||||
if ((j >= limit) || (oid == NULL)) continue;
|
||||
oid_j = oid[j];
|
||||
oid[j] = oid[j] * 10 + (OID[i] - '0');
|
||||
if (oid[j] < oid_j) return CRYPT_OVERFLOW;
|
||||
}
|
||||
else {
|
||||
return CRYPT_ERROR;
|
||||
}
|
||||
}
|
||||
if (j == 0) return CRYPT_ERROR;
|
||||
if (j >= limit) {
|
||||
*oidlen = j;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
*oidlen = j + 1;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen)
|
||||
{
|
||||
int i;
|
||||
unsigned long j, k;
|
||||
char tmp[LTC_OID_MAX_STRLEN] = { 0 };
|
||||
|
||||
LTC_ARGCHK(oid != NULL);
|
||||
LTC_ARGCHK(oidlen < INT_MAX);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
for (i = oidlen - 1, k = 0; i >= 0; i--) {
|
||||
j = oid[i];
|
||||
if (j == 0) {
|
||||
tmp[k] = '0';
|
||||
if (++k >= sizeof(tmp)) return CRYPT_ERROR;
|
||||
}
|
||||
else {
|
||||
while (j > 0) {
|
||||
tmp[k] = '0' + (j % 10);
|
||||
if (++k >= sizeof(tmp)) return CRYPT_ERROR;
|
||||
j /= 10;
|
||||
}
|
||||
}
|
||||
if (i > 0) {
|
||||
tmp[k] = '.';
|
||||
if (++k >= sizeof(tmp)) return CRYPT_ERROR;
|
||||
}
|
||||
}
|
||||
if (*outlen < k + 1) {
|
||||
*outlen = k + 1;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
LTC_ARGCHK(OID != NULL);
|
||||
for (j = 0; j < k; j++) OID[j] = tmp[k - j - 1];
|
||||
OID[k] = '\0';
|
||||
*outlen = k; /* the length without terminating NUL byte */
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
Reference in New Issue
Block a user