Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

crash with OpenSSL 1.1.1g decode custom ASN1 stream on windows

built openssl 1.1.1g using vs2013 x86 command

perl Configure VC-WIN32

This code works well on Linux but it doesn't work on Windows. What's wrong?

declaration:

...
typedef struct SES_Signature_0031_st {
    TBS_Sign_0031* toSign;
    ASN1_BIT_STRING* signature;
} SES_Signature_0031;

DECLARE_ASN1_FUNCTIONS(SES_Signature_0031)

implement:

...
ASN1_SEQUENCE(SES_Signature_0031) = {
    ASN1_SIMPLE(SES_Signature_0031, toSign, TBS_Sign_0031),
    ASN1_SIMPLE(SES_Signature_0031, signature, ASN1_BIT_STRING)
}ASN1_SEQUENCE_END(SES_Signature_0031)

IMPLEMENT_ASN1_FUNCTIONS(SES_Signature_0031)

using:

//work well
std::string cert = read_file("./signer.cer", "rb+");
const unsigned char *p_cert_buffer = (unsigned char *)cert.c_str();


X509 *x509 = d2i_X509(nullptr, &p_cert_buffer, cert.length());
ASN1_INTEGER *serial_number = X509_get_serialNumber(x509);

std::cout << to_hex_string(serial_number->data, serial_number->length) << "
";

custom stream:

std::string signed_value = read_file("./SignedValue.dat", "rb+");
const unsigned char *p_signed_value_buffer = (unsigned char *)signed_value.c_str();
//crashed when d2i
SES_Signature_0031 *signature_0031 = d2i_SES_Signature_0031(nullptr, &p_signed_value_buffer, signed_value.length());

stack info:

libcrypto-1_1.dll!ASN1_item_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it)
libcrypto-1_1.dll!ASN1_item_ex_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it, int tag, int aclass, char opt, ASN1_TLC_st * ctx)
libcrypto-1_1.dll!asn1_item_embed_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it, int tag, int aclass, char opt, ASN1_TLC_st * ctx, int depth)
libcrypto-1_1.dll!ASN1_item_ex_new(ASN1_VALUE_st * * pval, const ASN1_ITEM_st * it)
libcrypto-1_1.dll!asn1_item_embed_new(ASN1_VALUE_st * * pval, const ASN1_ITEM_st * it, int embed)
libcrypto-1_1.dll!asn1_template_new(ASN1_VALUE_st * * pval, const ASN1_TEMPLATE_st * tt) line 204

tasn_new.c line 204:

const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);

another usage:

std::string signed_value = read_file("./SignedValue.dat", "rb+");
unsigned char signed_value_buffer[10000] = { 0 };
int signed_value_buffer_len = 10000;
signed_value_buffer_len = signed_value.length();
memcpy(signed_value_buffer, signed_value.c_str(), signed_value.length());
//crashed when d2i
SES_Signature_0031 *signature_0031_2 = d2i_SES_Signature_0031(nullptr, (const unsigned char**)&signed_value_buffer, signed_value_buffer_len);

stack info:

libcrypto-1_1.dll!ASN1_item_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it)
libcrypto-1_1.dll!ASN1_item_ex_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it, int tag, int aclass, char opt, ASN1_TLC_st * ctx)
libcrypto-1_1.dll!asn1_item_embed_d2i(ASN1_VALUE_st * * pval, const unsigned char * * in, long len, const ASN1_ITEM_st * it, int tag, int aclass, char opt, ASN1_TLC_st * ctx, int depth)
libcrypto-1_1.dll!asn1_check_tlen(long * olen, int * otag, unsigned char * oclass, char * inf, char * cst, const unsigned char * * in, long len, int exptag, int expclass, char opt, ASN1_TLC_st * ctx)
libcrypto-1_1.dll!ASN1_get_object(const unsigned char * * pp, long * plength, int * ptag, int * pclass, long omax) line 55

asn1_lib.c line 55:

ret = (*p & V_ASN1_CONSTRUCTED);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Incorrect Linux 64-bit header file was used. After replacing the VC-Win32 compilation and installation header file, the solution was solved


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...