Replaced system SQLite with SQLCipher to support encrypted database
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file x509_decode_public_key_from_certificate.c
|
||||
ASN.1 DER/X.509, decode a certificate
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Try to decode the public key from a X.509 certificate
|
||||
@param in The input buffer
|
||||
@param inlen The length of the input buffer
|
||||
@param algorithm One out of the enum #public_key_algorithms
|
||||
@param param_type The parameters' type out of the enum ltc_asn1_type
|
||||
@param parameters The parameters to include
|
||||
@param parameters_len [in/out] The number of parameters to include
|
||||
@param callback The callback
|
||||
@param ctx The context passed to the callback
|
||||
@return CRYPT_OK on success,
|
||||
CRYPT_NOP if no SubjectPublicKeyInfo was found,
|
||||
another error if decoding or memory allocation failed
|
||||
*/
|
||||
int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
|
||||
enum ltc_oid_id algorithm, ltc_asn1_type param_type,
|
||||
ltc_asn1_list* parameters, unsigned long *parameters_len,
|
||||
public_key_decode_cb callback, void *ctx)
|
||||
{
|
||||
int err;
|
||||
unsigned char *tmpbuf = NULL;
|
||||
unsigned long tmpbuf_len;
|
||||
ltc_asn1_list *decoded_list = NULL, *spki;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != 0);
|
||||
LTC_ARGCHK(callback != NULL);
|
||||
|
||||
if ((err = x509_decode_spki(in, inlen, &decoded_list, &spki)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (algorithm == LTC_OID_EC) {
|
||||
err = callback(spki->data, spki->size, ctx);
|
||||
} else {
|
||||
|
||||
tmpbuf_len = inlen;
|
||||
tmpbuf = XCALLOC(1, tmpbuf_len);
|
||||
if (tmpbuf == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto LBL_OUT;
|
||||
}
|
||||
|
||||
err = x509_decode_subject_public_key_info(spki->data, spki->size,
|
||||
algorithm, tmpbuf, &tmpbuf_len,
|
||||
param_type, parameters, parameters_len);
|
||||
if (err == CRYPT_OK) {
|
||||
err = callback(tmpbuf, tmpbuf_len, ctx);
|
||||
goto LBL_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
LBL_OUT:
|
||||
if (decoded_list) der_free_sequence_flexi(decoded_list);
|
||||
if (tmpbuf != NULL) XFREE(tmpbuf);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file x509_decode_public_key_from_certificate.c
|
||||
ASN.1 DER/X.509, decode a SubjectPublicKeyInfo
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* Check if it looks like a SubjectPublicKeyInfo */
|
||||
#define LOOKS_LIKE_SPKI(l) ((l) != NULL) \
|
||||
&& ((l)->type == LTC_ASN1_SEQUENCE) \
|
||||
&& ((l)->child != NULL) \
|
||||
&& ((l)->child->type == LTC_ASN1_OBJECT_IDENTIFIER) \
|
||||
&& ((l)->next != NULL) \
|
||||
&& ((l)->next->type == LTC_ASN1_BIT_STRING)
|
||||
|
||||
/**
|
||||
DER decode a X.509 certificate and return the SubjectPublicKeyInfo
|
||||
@param in The input buffer
|
||||
@param inlen The length of the input buffer
|
||||
@param out [out] A pointer to the decoded linked list (you take ownership of this one and
|
||||
`der_free_sequence_flexi()` it when you're done)
|
||||
@param spki [out] A pointer to the SubjectPublicKeyInfo
|
||||
@return CRYPT_OK on success, CRYPT_NOP if no SubjectPublicKeyInfo was found, another error if decoding failed
|
||||
*/
|
||||
int x509_decode_spki(const unsigned char *in, unsigned long inlen, ltc_asn1_list **out, ltc_asn1_list **spki)
|
||||
{
|
||||
int err;
|
||||
unsigned long tmp_inlen;
|
||||
ltc_asn1_list *decoded_list = NULL, *l;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != 0);
|
||||
|
||||
tmp_inlen = inlen;
|
||||
if ((err = der_decode_sequence_flexi(in, &tmp_inlen, &decoded_list)) == CRYPT_OK) {
|
||||
l = decoded_list;
|
||||
|
||||
err = CRYPT_NOP;
|
||||
|
||||
/* Move 2 levels up in the tree
|
||||
SEQUENCE
|
||||
SEQUENCE
|
||||
...
|
||||
*/
|
||||
if ((l->type == LTC_ASN1_SEQUENCE) && (l->child != NULL)) {
|
||||
l = l->child;
|
||||
if ((l->type == LTC_ASN1_SEQUENCE) && (l->child != NULL)) {
|
||||
l = l->child;
|
||||
|
||||
/* Move forward in the tree until we find this combination
|
||||
...
|
||||
SEQUENCE
|
||||
SEQUENCE
|
||||
OBJECT IDENTIFIER <some PKA OID, e.g. 1.2.840.113549.1.1.1>
|
||||
NULL
|
||||
BIT STRING
|
||||
*/
|
||||
do {
|
||||
/* The additional check for l->data is there to make sure
|
||||
* we won't try to decode a list that has been 'shrunk'
|
||||
*/
|
||||
if ((l->type == LTC_ASN1_SEQUENCE)
|
||||
&& (l->data != NULL)
|
||||
&& LOOKS_LIKE_SPKI(l->child)) {
|
||||
*out = decoded_list;
|
||||
*spki = l;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
l = l->next;
|
||||
} while(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (decoded_list) der_free_sequence_flexi(decoded_list);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,119 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file x509_decode_subject_public_key_info.c
|
||||
ASN.1 DER/X.509, encode a SubjectPublicKeyInfo structure --nmav
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* AlgorithmIdentifier := SEQUENCE {
|
||||
* algorithm OBJECT IDENTIFIER,
|
||||
* parameters ANY DEFINED BY algorithm
|
||||
* }
|
||||
*
|
||||
* SubjectPublicKeyInfo := SEQUENCE {
|
||||
* algorithm AlgorithmIdentifier,
|
||||
* subjectPublicKey BIT STRING
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
Decode a SubjectPublicKeyInfo
|
||||
@param in The input buffer
|
||||
@param inlen The length of the input buffer
|
||||
@param algorithm One out of the enum #public_key_algorithms
|
||||
@param public_key The buffer for the public key
|
||||
@param public_key_len [in/out] The length of the public key buffer and the written length
|
||||
@param parameters_type The parameters' type out of the enum ltc_asn1_type
|
||||
@param parameters The parameters to include
|
||||
@param parameters_len [in/out] The number of parameters to include
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
|
||||
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len,
|
||||
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len)
|
||||
{
|
||||
int err;
|
||||
unsigned long len, alg_id_num, tmplen;
|
||||
const char* oid;
|
||||
unsigned char *tmpbuf;
|
||||
unsigned long tmpoid[16];
|
||||
unsigned long *_parameters_len;
|
||||
ltc_asn1_list alg_id[2];
|
||||
ltc_asn1_list subject_pubkey[2];
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen != 0);
|
||||
LTC_ARGCHK(public_key_len != NULL);
|
||||
|
||||
if (parameters_type != LTC_ASN1_EOL) {
|
||||
if ((parameters == NULL) || (parameters_len == NULL)) {
|
||||
tmplen = 0;
|
||||
_parameters_len = &tmplen;
|
||||
} else {
|
||||
_parameters_len = parameters_len;
|
||||
}
|
||||
}
|
||||
|
||||
err = pk_get_oid(algorithm, &oid);
|
||||
if (err != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* see if the OpenSSL DER format RSA public key will work */
|
||||
tmpbuf = XCALLOC(1, inlen);
|
||||
if (tmpbuf == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* this includes the internal hash ID and optional params (NULL in this case) */
|
||||
LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
|
||||
if (parameters_type == LTC_ASN1_EOL) {
|
||||
alg_id_num = 1;
|
||||
} else {
|
||||
LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, *_parameters_len);
|
||||
alg_id_num = 2;
|
||||
}
|
||||
|
||||
/* the actual format of the SSL DER key is odd, it stores a RSAPublicKey
|
||||
* in a **BIT** string ... so we have to extract it then proceed to convert bit to octet
|
||||
*/
|
||||
LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, alg_id_num);
|
||||
LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, inlen*8U);
|
||||
|
||||
err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
|
||||
if (err != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if (parameters_type != LTC_ASN1_EOL) {
|
||||
*_parameters_len = alg_id[1].size;
|
||||
}
|
||||
|
||||
if ((err = pk_oid_cmp_with_asn1(oid, &alg_id[0])) != CRYPT_OK) {
|
||||
/* OID mismatch */
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
len = subject_pubkey[1].size/8;
|
||||
if (*public_key_len >= len) {
|
||||
XMEMCPY(public_key, subject_pubkey[1].data, len);
|
||||
*public_key_len = len;
|
||||
} else {
|
||||
*public_key_len = len;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
|
||||
XFREE(tmpbuf);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file x509_encode_subject_public_key_info.c
|
||||
ASN.1 DER/X.509, encode a SubjectPublicKeyInfo structure --nmav
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* AlgorithmIdentifier := SEQUENCE {
|
||||
* algorithm OBJECT IDENTIFIER,
|
||||
* parameters ANY DEFINED BY algorithm
|
||||
* }
|
||||
*
|
||||
* SubjectPublicKeyInfo := SEQUENCE {
|
||||
* algorithm AlgorithmIdentifier,
|
||||
* subjectPublicKey BIT STRING
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
Encode a SubjectPublicKeyInfo
|
||||
@param out The output buffer
|
||||
@param outlen [in/out] Length of buffer and resulting length of output
|
||||
@param algorithm One out of the enum #public_key_algorithms
|
||||
@param public_key The buffer for the public key
|
||||
@param public_key_len The length of the public key buffer
|
||||
@param parameters_type The parameters' type out of the enum ltc_asn1_type
|
||||
@param parameters The parameters to include
|
||||
@param parameters_len The number of parameters to include
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
|
||||
enum ltc_oid_id algorithm, const void* public_key, unsigned long public_key_len,
|
||||
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len)
|
||||
{
|
||||
int err;
|
||||
ltc_asn1_list alg_id[2];
|
||||
const char *OID;
|
||||
unsigned long oid[16], oidlen;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if ((err = pk_get_oid(algorithm, &OID)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
oidlen = sizeof(oid)/sizeof(oid[0]);
|
||||
if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
|
||||
LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, parameters_len);
|
||||
|
||||
return der_encode_sequence_multi(out, outlen,
|
||||
LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
|
||||
LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
|
||||
LTC_ASN1_EOL, 0UL, NULL);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user