@ -1587,6 +1587,88 @@ TEST(ASN1Test, StringCmp) {
}
}
TEST ( ASN1Test , PrintASN1Object ) {
const struct {
std : : vector < uint8_t > in ;
const char * expected ;
} kDataTests [ ] = {
// Known OIDs print as the name.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x0d , 0x01 , 0x01 , 0x01 } , " rsaEncryption " } ,
// Unknown OIDs print in decimal.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 , 0x09 , 0x00 } ,
" 1.2.840.113554.4.1.72585.0 " } ,
// Inputs which cannot be parsed as OIDs print as "<INVALID>".
{ { 0xff } , " <INVALID> " } ,
// The function has an internal 80-byte buffer. Test inputs at that
// boundary. First, 78 characters.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 ,
0x09 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 } ,
" 1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. "
" 0.0.0.1 " } ,
// 79 characters.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 ,
0x09 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x0a } ,
" 1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. "
" 0.0.0.10 " } ,
// 80 characters.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 ,
0x09 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x64 } ,
" 1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. "
" 0.0.0.100 " } ,
// 81 characters.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 ,
0x09 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x87 , 0x68 } ,
" 1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. "
" 0.0.0.1000 " } ,
// 82 characters.
{ { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x12 , 0x04 , 0x01 , 0x84 , 0xb7 ,
0x09 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xce , 0x10 } ,
" 1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. "
" 0.0.0.10000 " } ,
} ;
for ( const auto & t : kDataTests ) {
SCOPED_TRACE ( Bytes ( t . in ) ) ;
bssl : : UniquePtr < ASN1_OBJECT > obj ( ASN1_OBJECT_create (
NID_undef , t . in . data ( ) , t . in . size ( ) , /*sn=*/ nullptr , /*ln=*/ nullptr ) ) ;
ASSERT_TRUE ( obj ) ;
bssl : : UniquePtr < BIO > bio ( BIO_new ( BIO_s_mem ( ) ) ) ;
ASSERT_TRUE ( bio ) ;
int len = i2a_ASN1_OBJECT ( bio . get ( ) , obj . get ( ) ) ;
EXPECT_EQ ( len , static_cast < int > ( strlen ( t . expected ) ) ) ;
const uint8_t * bio_data ;
size_t bio_len ;
BIO_mem_contents ( bio . get ( ) , & bio_data , & bio_len ) ;
EXPECT_EQ ( t . expected ,
std : : string ( reinterpret_cast < const char * > ( bio_data ) , bio_len ) ) ;
}
// Test writing NULL.
bssl : : UniquePtr < BIO > bio ( BIO_new ( BIO_s_mem ( ) ) ) ;
ASSERT_TRUE ( bio ) ;
int len = i2a_ASN1_OBJECT ( bio . get ( ) , nullptr ) ;
EXPECT_EQ ( len , 4 ) ;
const uint8_t * bio_data ;
size_t bio_len ;
BIO_mem_contents ( bio . get ( ) , & bio_data , & bio_len ) ;
EXPECT_EQ ( " NULL " ,
std : : string ( reinterpret_cast < const char * > ( bio_data ) , bio_len ) ) ;
}
// The ASN.1 macros do not work on Windows shared library builds, where usage of
// |OPENSSL_EXPORT| is a bit stricter.
# if !defined(OPENSSL_WINDOWS) || !defined(BORINGSSL_SHARED_LIBRARY)