Skip to content

Commit

Permalink
feat: add compatibility code to bssl-compat layer
Browse files Browse the repository at this point in the history
- Add new compatibility functions to existing bssl-compat layer
- Implement symbol conflict resolution logic
- Ensure proper symbol mapping between different BoringSSL versions
  • Loading branch information
KINGJUYONG committed Dec 29, 2024
1 parent ef24076 commit c95305e
Show file tree
Hide file tree
Showing 17 changed files with 1,043 additions and 76 deletions.
16 changes: 16 additions & 0 deletions bssl-compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ add_library(bssl-compat SHARED
# source/SSL_set_enforce_rsa_key_usage.cc # Added for compatibility!
# source/EVP_DigestInit.cc # Added for compatibility!
# source/SSL_CTX_get_cert_store.cc # Added for compatibility!
source/AES.cc
source/EVP_MD_CTX_init.cc # Added for compatibility!
source/EVP_MD_CTX_cleanup.cc # Added for compatibility!
source/BN_init.cc # Added for compatibility!
source/BN_num_bytes.cc # Added for compatibility!
source/BN_bn2le_padded.cc # Added for compatibility!
source/ASN1_TIME_free.cc
source/BASIC_CONSTRAINTS_free.cc
source/BASIC_CONSTRAINTS_new.cc
Expand All @@ -70,6 +73,7 @@ add_library(bssl-compat SHARED
source/BN_cmp_word.cc
source/BN_bn2hex.cc
source/BN_bin2bn.cc
source/BN_bn2bin.cc
source/c2i_ASN1_INTEGER.cc
source/CRYPTO_BUFFER_free.c
source/CRYPTO_BUFFER.h
Expand Down Expand Up @@ -108,6 +112,10 @@ add_library(bssl-compat SHARED
source/internal.h
source/log.c
source/log.h
source/MD5_Init.cc
source/MD5_Transform.cc
source/MD5_Update.cc
source/MD5_Final.cc
source/NAME_CONSTRAINTS_free.cc
source/NAME_CONSTRAINTS_new.cc
source/ossl.c
Expand All @@ -132,7 +140,12 @@ add_library(bssl-compat SHARED
source/RSA_private_key_from_bytes.cc
source/RSA_public_key_from_bytes.cc
source/RSA_sign_pss_mgf1.cc
source/RSA_add_pkcs1_prefix.cc # Added for compatibility!
source/SHA1.cc
source/SHA224.cc
source/SHA256.cc
source/SHA384.cc
source/SHA512.cc
source/SSL_add_file_cert_subjects_to_stack.cc
source/SSL_CIPHER_get_kx_nid.cc
source/SSL_CIPHER_get_min_version.cc
Expand Down Expand Up @@ -212,6 +225,7 @@ add_library(bssl-compat SHARED
source/X509_STORE_CTX_set0_trusted_stack.cc
source/X509_STORE_CTX_set_verify_cb.cc
source/X509_verify_cert_error_string.cc
source/X509_VERIFY_PARAM_set_time.cc
source/X509_VERIFY_PARAM_set_time_posix.cc
)

Expand Down Expand Up @@ -352,6 +366,7 @@ target_add_bssl_function(bssl-compat
BN_add_word
BN_add_word
BN_add_word
BN_is_word
BN_dup
BN_free
BN_hex2bn
Expand Down Expand Up @@ -462,6 +477,7 @@ target_add_bssl_function(bssl-compat
RSA_sign
RSA_size
RSA_verify
# RSA_add_pkcs1_prefix # Added for compatibility!
RSA_padding_add_PKCS1_PSS_mgf1 # Added for compatibility!
SHA1
SHA224
Expand Down
33 changes: 33 additions & 0 deletions bssl-compat/source/AES.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <openssl/base.h>
#include <openssl/aes.h>
#include <ossl.h>

struct AES_KEY {
uint32_t rd_key[60];
int rounds;
};

extern "C" int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) {
return ossl.ossl_AES_set_encrypt_key(userKey, bits, reinterpret_cast<ossl_AES_KEY*>(key));
}

extern "C" int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) {
return ossl.ossl_AES_set_decrypt_key(userKey, bits, reinterpret_cast<ossl_AES_KEY*>(key));
}

extern "C" void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
ossl.ossl_AES_encrypt(in, out, reinterpret_cast<const ossl_AES_KEY*>(key));
}

extern "C" void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
ossl.ossl_AES_decrypt(in, out, reinterpret_cast<const ossl_AES_KEY*>(key));
}

extern "C" void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key, const int enc) {
ossl.ossl_AES_ecb_encrypt(in, out, reinterpret_cast<const ossl_AES_KEY*>(key), enc);
}

extern "C" void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length,
const AES_KEY *key, unsigned char *ivec, const int enc) {
ossl.ossl_AES_cbc_encrypt(in, out, length, reinterpret_cast<const ossl_AES_KEY*>(key), ivec, enc);
}
35 changes: 35 additions & 0 deletions bssl-compat/source/BN_bn2bin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <openssl/bn.h>
#include <ossl.h>
#include <string.h>

#define OPENSSL_free free

extern "C" int BN_bn2bin(const BIGNUM *a, unsigned char *to) {
if (!a || !to) {
return 0;
}

char *hex_str = ossl.ossl_BN_bn2hex(a);
if (!hex_str) {
return 0;
}

int hex_len = strlen(hex_str);
int num_bytes = (hex_len + 1) / 2;

for (int i = 0; i < num_bytes; i++) {
unsigned int byte_val;
char hex_byte[3] = {0};

size_t hex_index = hex_len - 2 * (i + 1);
if (hex_index < hex_len) {
hex_byte[0] = hex_len > hex_index + 1 ? hex_str[hex_index] : '0';
hex_byte[1] = hex_len > hex_index + 1 ? hex_str[hex_index + 1] : hex_str[hex_index];
sscanf(hex_byte, "%x", &byte_val);
to[num_bytes - 1 - i] = byte_val & 0xFF;
}
}

OPENSSL_free(hex_str);
return num_bytes;
}
45 changes: 45 additions & 0 deletions bssl-compat/source/BN_bn2le_padded.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <openssl/bn.h>
#include <string.h>

#define OPENSSL_free free

static unsigned BN_num_bytes(const BIGNUM *bn) {
return (BN_num_bits(bn) + 7) / 8;
}


extern "C" int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *bn) {
size_t num_bytes = BN_num_bytes(bn);

if (num_bytes > len) {
return 0;
}

memset(out, 0, len);

if (num_bytes > 0) {
char* hex_str = BN_bn2hex(bn);
if (!hex_str) {
return 0;
}

size_t hex_len = strlen(hex_str);

for (size_t i = 0; i < num_bytes; i++) {
unsigned int byte_val;
char hex_byte[3] = {0};

size_t hex_index = hex_len - 2 * (i + 1);
if (hex_index < hex_len) {
hex_byte[0] = hex_str[hex_index];
hex_byte[1] = hex_str[hex_index + 1];
sscanf(hex_byte, "%x", &byte_val);
out[i] = byte_val & 0xFF;
}
}

OPENSSL_free(hex_str);
}

return 1;
}
5 changes: 5 additions & 0 deletions bssl-compat/source/BN_num_bytes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <openssl/bn.h>

extern "C" unsigned BN_num_bytes(const BIGNUM *bn) {
return (BN_num_bits(bn) + 7) / 8;
}
158 changes: 158 additions & 0 deletions bssl-compat/source/MD5_Final.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <openssl/md5.h>
#include <string.h>
#include <ossl.h>

typedef struct {
uint32_t state[4];
uint64_t count;
unsigned char buffer[64];
} MD5_CTX;

void MD5Transform(MD5_CTX *ctx, const unsigned char *block) {
uint32_t a = ctx->state[0];
uint32_t b = ctx->state[1];
uint32_t c = ctx->state[2];
uint32_t d = ctx->state[3];

#define F(x, y, z) (z ^ (x & (y ^ z)))
#define G(x, y, z) (y ^ (z & (x ^ y)))
#define H(x, y, z) (x ^ y ^ z)
#define I(x, y, z) (y ^ (x | ~z))

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

#define FF(a, b, c, d, x, s, ac) { \
(a) += F((b), (c), (d)) + (x) + (ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G((b), (c), (d)) + (x) + (ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H((b), (c), (d)) + (x) + (ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I((b), (c), (d)) + (x) + (ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}

uint32_t x[16];
for (int i = 0; i < 16; i++) {
x[i] = (uint32_t)block[i*4] |
((uint32_t)block[i*4+1] << 8) |
((uint32_t)block[i*4+2] << 16) |
((uint32_t)block[i*4+3] << 24);
}

FF(a, b, c, d, x[ 0], 7, 0xd76aa478);
FF(d, a, b, c, x[ 1], 12, 0xe8c7b756);
FF(c, d, a, b, x[ 2], 17, 0x242070db);
FF(b, c, d, a, x[ 3], 22, 0xc1bdceee);
FF(a, b, c, d, x[ 4], 7, 0xf57c0faf);
FF(d, a, b, c, x[ 5], 12, 0x4787c62a);
FF(c, d, a, b, x[ 6], 17, 0xa8304613);
FF(b, c, d, a, x[ 7], 22, 0xfd469501);
FF(a, b, c, d, x[ 8], 7, 0x698098d8);
FF(d, a, b, c, x[ 9], 12, 0x8b44f7af);
FF(c, d, a, b, x[10], 17, 0xffff5bb1);
FF(b, c, d, a, x[11], 22, 0x895cd7be);
FF(a, b, c, d, x[12], 7, 0x6b901122);
FF(d, a, b, c, x[13], 12, 0xfd987193);
FF(c, d, a, b, x[14], 17, 0xa679438e);
FF(b, c, d, a, x[15], 22, 0x49b40821);

GG(a, b, c, d, x[ 1], 5, 0xf61e2562);
GG(d, a, b, c, x[ 6], 9, 0xc040b340);
GG(c, d, a, b, x[11], 14, 0x265e5a51);
GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
GG(a, b, c, d, x[ 5], 5, 0xd62f105d);
GG(d, a, b, c, x[10], 9, 0x02441453);
GG(c, d, a, b, x[15], 14, 0xd8a1e681);
GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
GG(a, b, c, d, x[ 9], 5, 0x21e1cde6);
GG(d, a, b, c, x[14], 9, 0xc33707d6);
GG(c, d, a, b, x[ 3], 14, 0xf4d50d87);
GG(b, c, d, a, x[ 8], 20, 0x455a14ed);
GG(a, b, c, d, x[13], 5, 0xa9e3e905);
GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8);
GG(c, d, a, b, x[ 7], 14, 0x676f02d9);
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);

HH(a, b, c, d, x[ 5], 4, 0xfffa3942);
HH(d, a, b, c, x[ 8], 11, 0x8771f681);
HH(c, d, a, b, x[11], 16, 0x6d9d6122);
HH(b, c, d, a, x[14], 23, 0xfde5380c);
HH(a, b, c, d, x[ 1], 4, 0xa4beea44);
HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
HH(b, c, d, a, x[10], 23, 0xbebfbc70);
HH(a, b, c, d, x[13], 4, 0x289b7ec6);
HH(d, a, b, c, x[ 0], 11, 0xeaa127fa);
HH(c, d, a, b, x[ 3], 16, 0xd4ef3085);
HH(b, c, d, a, x[ 6], 23, 0x04881d05);
HH(a, b, c, d, x[ 9], 4, 0xd9d4d039);
HH(d, a, b, c, x[12], 11, 0xe6db99e5);
HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
HH(b, c, d, a, x[ 2], 23, 0xc4ac5665);

II(a, b, c, d, x[ 0], 6, 0xf4292244);
II(d, a, b, c, x[ 7], 10, 0x432aff97);
II(c, d, a, b, x[14], 15, 0xab9423a7);
II(b, c, d, a, x[ 5], 21, 0xfc93a039);
II(a, b, c, d, x[12], 6, 0x655b59c3);
II(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
II(c, d, a, b, x[10], 15, 0xffeff47d);
II(b, c, d, a, x[ 1], 21, 0x85845dd1);
II(a, b, c, d, x[ 8], 6, 0x6fa87e4f);
II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
II(c, d, a, b, x[ 6], 15, 0xa3014314);
II(b, c, d, a, x[13], 21, 0x4e0811a1);
II(a, b, c, d, x[10], 6, 0xf7537e82);
II(d, a, b, c, x[ 7], 10, 0xbd3af235);
II(c, d, a, b, x[ 4], 15, 0x2ad7d2bb);
II(b, c, d, a, x[13], 21, 0xeb86d391);

ctx->state[0] += a;
ctx->state[1] += b;
ctx->state[2] += c;
ctx->state[3] += d;
}

extern "C" int MD5_Final(unsigned char *md, MD5_CTX *ctx) {
if (!md || !ctx) {
return 0;
}

unsigned int have = (unsigned int)((ctx->count) & 0x3f);
ctx->buffer[have++] = 0x80;

unsigned char *p = ctx->buffer + have;
if (have > 56) {
memset(p, 0, 64 - have);
MD5Transform(ctx, ctx->buffer);
have = 0;
p = ctx->buffer;
}

memset(p, 0, 56 - have);

ctx->count *= 8;
*(uint64_t *)(ctx->buffer + 56) = ctx->count;

MD5Transform(ctx, ctx->buffer);

for (int i = 0; i < 4; i++) {
md[i*4] = (ctx->state[i] & 0xFF);
md[i*4+1] = ((ctx->state[i] >> 8) & 0xFF);
md[i*4+2] = ((ctx->state[i] >> 16) & 0xFF);
md[i*4+3] = ((ctx->state[i] >> 24) & 0xFF);
}

return 1;
}
26 changes: 26 additions & 0 deletions bssl-compat/source/MD5_Init.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <openssl/md5.h>
#include <string.h>
#include <ossl.h>

typedef struct {
uint32_t state[4];
uint64_t count;
unsigned char buffer[64];
} MD5_CTX;

extern "C" int MD5_Init(MD5_CTX *md5) {
if (md5 == NULL) {
return 0;
}

md5->state[0] = 0x67452301;
md5->state[1] = 0xefcdab89;
md5->state[2] = 0x98badcfe;
md5->state[3] = 0x10325476;

md5->count = 0;

memset(md5->buffer, 0, sizeof(md5->buffer));

return 1;
}
Loading

0 comments on commit c95305e

Please sign in to comment.