Replaced system SQLite with SQLCipher to support encrypted database

This commit is contained in:
Oleksii Zghurskyi
2025-06-07 18:11:17 +03:00
parent f4198d62a7
commit 177d74700f
534 changed files with 362771 additions and 21 deletions

View File

@@ -0,0 +1,157 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_asn1_maps.c
ASN.1 DER, a collection of maps to convert between different representations, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
A Map from ltc_asn1_type to the regularly used ASN.1 identifier
*/
const int der_asn1_type_to_identifier_map[] =
{
/* 0 */
-1, /* LTC_ASN1_EOL, */
1, /* LTC_ASN1_BOOLEAN, */
2, /* LTC_ASN1_INTEGER, */
2, /* LTC_ASN1_SHORT_INTEGER, */
3, /* LTC_ASN1_BIT_STRING, */
/* 5 */
4, /* LTC_ASN1_OCTET_STRING, */
5, /* LTC_ASN1_NULL, */
6, /* LTC_ASN1_OBJECT_IDENTIFIER, */
22, /* LTC_ASN1_IA5_STRING, */
19, /* LTC_ASN1_PRINTABLE_STRING, */
/* 10 */
12, /* LTC_ASN1_UTF8_STRING, */
23, /* LTC_ASN1_UTCTIME, */
-1, /* LTC_ASN1_CHOICE, */
48, /* LTC_ASN1_SEQUENCE, */
49, /* LTC_ASN1_SET, */
/* 15 */
49, /* LTC_ASN1_SETOF, */
3, /* LTC_ASN1_RAW_BIT_STRING, */
20, /* LTC_ASN1_TELETEX_STRING, */
24, /* LTC_ASN1_GENERALIZEDTIME, */
-1, /* LTC_ASN1_CUSTOM_TYPE, */
};
const unsigned long der_asn1_type_to_identifier_map_sz = sizeof(der_asn1_type_to_identifier_map)/sizeof(der_asn1_type_to_identifier_map[0]);
/**
A Map from the ASN.1 Class to its string
*/
const char* der_asn1_class_to_string_map[] =
{
"UNIVERSAL",
"APPLICATION",
"CONTEXT-SPECIFIC",
"PRIVATE",
};
const unsigned long der_asn1_class_to_string_map_sz = sizeof(der_asn1_class_to_string_map)/sizeof(der_asn1_class_to_string_map[0]);
/**
A Map from the ASN.1 P/C-bit to its string
*/
const char* der_asn1_pc_to_string_map[] =
{
"PRIMITIVE",
"CONSTRUCTED",
};
const unsigned long der_asn1_pc_to_string_map_sz = sizeof(der_asn1_pc_to_string_map)/sizeof(der_asn1_pc_to_string_map[0]);
/**
A Map from the ASN.1 tag to its string
*/
const char* der_asn1_tag_to_string_map[] =
{
"Reserved for use by the encoding rules",
"Boolean type",
"Integer type",
"Bitstring type",
"Octetstring type",
"Null type",
"Object identifier type",
"Object descriptor type",
"External type and Instance-of type",
"Real type",
"Enumerated type",
"Embedded-pdv type",
"UTF8String type",
"Relative object identifier type",
"The time type",
"Reserved for future editions of this Recommendation | International Standard",
"Sequence and Sequence-of types",
"Set and Set-of types",
"NumericString type",
"PrintableString type",
"TeletexString (T61String) type",
"VideotexString type",
"IA5String type",
"UTCTime type",
"GeneralizedTime type",
"GraphicString type",
"VisibleString (ISO646String) type",
"GeneralString type",
"UniversalString type",
"UnrestrictedCharacterString type",
"BMPString type",
"Date type",
"TimeOfDay type",
"DateTime type",
"Duration type",
"OID internationalized resource identifier type",
"Relative OID internationalized resource identifier type",
};
const unsigned long der_asn1_tag_to_string_map_sz = sizeof(der_asn1_tag_to_string_map)/sizeof(der_asn1_tag_to_string_map[0]);
/**
A Map from ASN.1 Tags to ltc_asn1_type
*/
const ltc_asn1_type der_asn1_tag_to_type_map[] =
{
/* 0 */
LTC_ASN1_EOL, /* Reserved for use by the encoding rules */
LTC_ASN1_BOOLEAN, /* Boolean type */
LTC_ASN1_INTEGER, /* Integer type */
LTC_ASN1_BIT_STRING, /* Bitstring type */
LTC_ASN1_OCTET_STRING, /* Octetstring type */
/* 5 */
LTC_ASN1_NULL, /* Null type */
LTC_ASN1_OBJECT_IDENTIFIER, /* Object identifier type */
LTC_ASN1_CUSTOM_TYPE, /* Object descriptor type */
LTC_ASN1_CUSTOM_TYPE, /* External type and Instance-of type */
LTC_ASN1_CUSTOM_TYPE, /* Real type */
/* 10 */
LTC_ASN1_CUSTOM_TYPE, /* Enumerated type */
LTC_ASN1_CUSTOM_TYPE, /* Embedded-pdv type */
LTC_ASN1_UTF8_STRING, /* UTF8String type */
LTC_ASN1_CUSTOM_TYPE, /* Relative object identifier type */
LTC_ASN1_CUSTOM_TYPE, /* The time type */
/* 15 */
LTC_ASN1_EOL, /* Reserved for future editions of this Recommendation | International Standard */
LTC_ASN1_SEQUENCE, /* Sequence and Sequence-of types */
LTC_ASN1_SET, /* Set and Set-of types */
LTC_ASN1_CUSTOM_TYPE, /* NumericString types */
LTC_ASN1_PRINTABLE_STRING, /* PrintableString types */
/* 20 */
LTC_ASN1_TELETEX_STRING, /* TeletexString (T61String) types */
LTC_ASN1_CUSTOM_TYPE, /* VideotexString types */
LTC_ASN1_IA5_STRING, /* IA5String types */
LTC_ASN1_UTCTIME, /* UTCTime types */
LTC_ASN1_GENERALIZEDTIME, /* GeneralizedTime types */
/* 25 */
LTC_ASN1_CUSTOM_TYPE, /* GraphicString types */
LTC_ASN1_CUSTOM_TYPE, /* VisibleString (ISO646String) types */
LTC_ASN1_CUSTOM_TYPE, /* GeneralString types */
LTC_ASN1_CUSTOM_TYPE, /* UniversalString types */
LTC_ASN1_CUSTOM_TYPE, /* UnrestrictedCharacterString types */
/* 30 */
LTC_ASN1_CUSTOM_TYPE, /* BMPString types */
};
const unsigned long der_asn1_tag_to_type_map_sz = sizeof(der_asn1_tag_to_type_map)/sizeof(der_asn1_tag_to_type_map[0]);
#endif

View File

@@ -0,0 +1,123 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_decode_asn1_identifier.c
ASN.1 DER, decode the ASN.1 Identifier, Steffen Jaeckel
*/
#ifdef LTC_DER
/* c.f. X.680 & X.690, some decisions backed by X.690 ch. 10.2 */
static const unsigned char tag_constructed_map[] =
{
/* 0 */
255,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
/* 5 */
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
/* 10 */
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
/* 15 */
255,
LTC_ASN1_PC_CONSTRUCTED,
LTC_ASN1_PC_CONSTRUCTED,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
/* 20 */
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
/* 25 */
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
LTC_ASN1_PC_PRIMITIVE,
};
static const unsigned long tag_constructed_map_sz = sizeof(tag_constructed_map)/sizeof(tag_constructed_map[0]);
/**
Decode the ASN.1 Identifier
@param id Where to store the decoded Identifier
@param in Where to read the Identifier from
@param inlen [in/out] The size of in available/read
@return CRYPT_OK if successful
*/
int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id)
{
ulong64 tmp;
unsigned long tag_len;
int err;
LTC_ARGCHK(id != NULL);
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(inlen != NULL);
if (*inlen == 0) {
return CRYPT_BUFFER_OVERFLOW;
}
tag_len = 1;
id->klass = (in[0] >> 6) & 0x3;
id->pc = (in[0] >> 5) & 0x1;
id->tag = in[0] & 0x1f;
err = CRYPT_OK;
if (id->tag == 0x1f) {
id->tag = 0;
do {
if (*inlen < tag_len) {
/* break the loop and trigger the BOF error-code */
tmp = 0xff;
break;
}
id->tag <<= 7;
id->tag |= in[tag_len] & 0x7f;
tmp = in[tag_len] & 0x80;
tag_len++;
} while ((tmp != 0) && (tag_len < 10));
if (tmp != 0) {
err = CRYPT_BUFFER_OVERFLOW;
} else if (id->tag < 0x1f) {
err = CRYPT_PK_ASN1_ERROR;
}
}
if (err != CRYPT_OK) {
id->pc = 0;
id->klass = 0;
id->tag = 0;
} else {
*inlen = tag_len;
if ((id->klass == LTC_ASN1_CL_UNIVERSAL) &&
(id->tag < der_asn1_tag_to_type_map_sz) &&
(id->tag < tag_constructed_map_sz) &&
(id->pc == tag_constructed_map[id->tag])) {
id->type = der_asn1_tag_to_type_map[id->tag];
} else {
if ((id->klass == LTC_ASN1_CL_UNIVERSAL) && (id->tag == 0)) {
id->type = LTC_ASN1_EOL;
} else {
id->type = LTC_ASN1_CUSTOM_TYPE;
}
}
}
return err;
}
#endif

View File

@@ -0,0 +1,59 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_decode_asn1_length.c
ASN.1 DER, decode the ASN.1 Length field, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
Decode the ASN.1 Length field
@param in Where to read the length field from
@param inlen [in/out] The size of in available/read
@param outlen [out] The decoded ASN.1 length
@return CRYPT_OK if successful
*/
int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen)
{
unsigned long real_len, decoded_len, offset, i;
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(inlen != NULL);
if (*inlen < 1) {
return CRYPT_BUFFER_OVERFLOW;
}
real_len = in[0];
if (real_len < 128) {
decoded_len = real_len;
offset = 1;
} else {
real_len &= 0x7F;
if (real_len == 0) {
return CRYPT_PK_ASN1_ERROR;
}
if (real_len > sizeof(decoded_len)) {
return CRYPT_OVERFLOW;
}
if (real_len > (*inlen - 1)) {
return CRYPT_BUFFER_OVERFLOW;
}
decoded_len = 0;
offset = 1 + real_len;
for (i = 0; i < real_len; i++) {
decoded_len = (decoded_len << 8) | in[1 + i];
}
}
if (outlen != NULL) *outlen = decoded_len;
if (decoded_len > (*inlen - offset)) return CRYPT_OVERFLOW;
*inlen = offset;
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,86 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_encode_asn1_identifier.c
ASN.1 DER, encode the ASN.1 Identifier, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
Encode the ASN.1 Identifier
@param id The ASN.1 Identifer to encode
@param out Where to write the identifier to
@param outlen [in/out] The size of out available/written
@return CRYPT_OK if successful
*/
int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen)
{
ulong64 tmp;
unsigned long tag_len;
LTC_ARGCHK(id != NULL);
LTC_ARGCHK(outlen != NULL);
if (id->type != LTC_ASN1_CUSTOM_TYPE) {
if ((unsigned)id->type >= der_asn1_type_to_identifier_map_sz) {
return CRYPT_INVALID_ARG;
}
if (der_asn1_type_to_identifier_map[id->type] == -1) {
return CRYPT_INVALID_ARG;
}
if (out != NULL) {
*out = der_asn1_type_to_identifier_map[id->type];
}
*outlen = 1;
return CRYPT_OK;
}
if (id->klass < LTC_ASN1_CL_UNIVERSAL || id->klass > LTC_ASN1_CL_PRIVATE) {
return CRYPT_INVALID_ARG;
}
if (id->pc < LTC_ASN1_PC_PRIMITIVE || id->pc > LTC_ASN1_PC_CONSTRUCTED) {
return CRYPT_INVALID_ARG;
}
if (id->tag > (ULONG_MAX >> (8 + 7))) {
return CRYPT_INVALID_ARG;
}
if (out != NULL) {
if (*outlen < 1) {
return CRYPT_BUFFER_OVERFLOW;
}
out[0] = id->klass << 6 | id->pc << 5;
}
if (id->tag < 0x1f) {
if (out != NULL) {
out[0] |= id->tag & 0x1f;
}
*outlen = 1;
} else {
tag_len = 0;
tmp = id->tag;
do {
tag_len++;
tmp >>= 7;
} while (tmp);
if (out != NULL) {
if (*outlen < tag_len + 1) {
return CRYPT_BUFFER_OVERFLOW;
}
out[0] |= 0x1f;
for (tmp = 1; tmp <= tag_len; ++tmp) {
out[tmp] = ((id->tag >> (7 * (tag_len - tmp))) & 0x7f) | 0x80;
}
out[tag_len] &= ~0x80;
}
*outlen = tag_len + 1;
}
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,117 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_encode_asn1_length.c
ASN.1 DER, encode the ASN.1 length field, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
Encode the ASN.1 length field
@param len The length to encode
@param out Where to write the length field to
@param outlen [in/out] The size of out available/written
@return CRYPT_OK if successful
*/
int der_encode_asn1_length(unsigned long len, unsigned char *out, unsigned long *outlen)
{
unsigned long x, y;
LTC_ARGCHK(outlen != NULL);
x = len;
y = 0;
while(x != 0) {
y++;
x >>= 8;
}
if (y == 0) {
return CRYPT_PK_ASN1_ERROR;
}
if (out == NULL) {
if (len < 128) {
x = y;
} else {
x = y + 1;
}
} else {
if (*outlen < y) {
return CRYPT_BUFFER_OVERFLOW;
}
x = 0;
if (len < 128) {
out[x++] = (unsigned char)len;
} else if (len <= 0xffUL) {
out[x++] = 0x81;
out[x++] = (unsigned char)len;
} else if (len <= 0xffffUL) {
out[x++] = 0x82;
out[x++] = (unsigned char)((len>>8UL)&255);
out[x++] = (unsigned char)(len&255);
} else if (len <= 0xffffffUL) {
out[x++] = 0x83;
out[x++] = (unsigned char)((len>>16UL)&255);
out[x++] = (unsigned char)((len>>8UL)&255);
out[x++] = (unsigned char)(len&255);
#if ULONG_MAX != ULLONG_MAX
} else {
out[x++] = 0x84;
out[x++] = (unsigned char)((len>>24UL)&255);
out[x++] = (unsigned char)((len>>16UL)&255);
out[x++] = (unsigned char)((len>>8UL)&255);
out[x++] = (unsigned char)(len&255);
}
#else
} else if (len <= 0xffffffffUL) {
out[x++] = 0x84;
out[x++] = (unsigned char)((len>>24UL)&255);
out[x++] = (unsigned char)((len>>16UL)&255);
out[x++] = (unsigned char)((len>>8UL)&255);
out[x++] = (unsigned char)(len&255);
} else if (len <= 0xffffffffffULL) {
out[x++] = 0x85;
out[x++] = (unsigned char)((len>>32ULL)&255);
out[x++] = (unsigned char)((len>>24ULL)&255);
out[x++] = (unsigned char)((len>>16ULL)&255);
out[x++] = (unsigned char)((len>>8ULL)&255);
out[x++] = (unsigned char)(len&255);
} else if (len <= 0xffffffffffffULL) {
out[x++] = 0x86;
out[x++] = (unsigned char)((len>>40ULL)&255);
out[x++] = (unsigned char)((len>>32ULL)&255);
out[x++] = (unsigned char)((len>>24ULL)&255);
out[x++] = (unsigned char)((len>>16ULL)&255);
out[x++] = (unsigned char)((len>>8ULL)&255);
out[x++] = (unsigned char)(len&255);
} else if (len <= 0xffffffffffffffULL) {
out[x++] = 0x87;
out[x++] = (unsigned char)((len>>48ULL)&255);
out[x++] = (unsigned char)((len>>40ULL)&255);
out[x++] = (unsigned char)((len>>32ULL)&255);
out[x++] = (unsigned char)((len>>24ULL)&255);
out[x++] = (unsigned char)((len>>16ULL)&255);
out[x++] = (unsigned char)((len>>8ULL)&255);
out[x++] = (unsigned char)(len&255);
} else {
out[x++] = 0x88;
out[x++] = (unsigned char)((len>>56ULL)&255);
out[x++] = (unsigned char)((len>>48ULL)&255);
out[x++] = (unsigned char)((len>>40ULL)&255);
out[x++] = (unsigned char)((len>>32ULL)&255);
out[x++] = (unsigned char)((len>>24ULL)&255);
out[x++] = (unsigned char)((len>>16ULL)&255);
out[x++] = (unsigned char)((len>>8ULL)&255);
out[x++] = (unsigned char)(len&255);
}
#endif
}
*outlen = x;
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,23 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_length_asn1_identifier.c
ASN.1 DER, determine the length when encoding the ASN.1 Identifier, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
Determine the length required when encoding the ASN.1 Identifier
@param id The ASN.1 identifier to encode
@param idlen [out] The required length to encode list
@return CRYPT_OK if successful
*/
int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen)
{
return der_encode_asn1_identifier(id, NULL, idlen);
}
#endif

View File

@@ -0,0 +1,22 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file der_length_asn1_length.c
ASN.1 DER, determine the length of the ASN.1 length field, Steffen Jaeckel
*/
#ifdef LTC_DER
/**
Determine the length required to encode len in the ASN.1 length field
@param len The length to encode
@param outlen [out] The length that's required to store len
@return CRYPT_OK if successful
*/
int der_length_asn1_length(unsigned long len, unsigned long *outlen)
{
return der_encode_asn1_length(len, NULL, outlen);
}
#endif