#include <tests/unit.h>
#include <tests/api/test_asn.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/rsa.h>
#if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519)
static int test_SetAsymKeyDer_once(byte* privKey, word32 privKeySz, byte* pubKey,
word32 pubKeySz, byte* trueDer, word32 trueDerSz)
{
EXPECT_DECLS;
byte* calcDer = NULL;
word32 calcDerSz = 0;
ExpectIntEQ(calcDerSz = SetAsymKeyDer(privKey, privKeySz, pubKey, pubKeySz,
NULL, 0, ED25519k), trueDerSz);
ExpectNotNull(calcDer = (byte*)XMALLOC(calcDerSz, NULL,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectIntEQ(calcDerSz = SetAsymKeyDer(privKey, privKeySz, pubKey, pubKeySz,
calcDer, calcDerSz, ED25519k), trueDerSz);
ExpectIntEQ(XMEMCMP(calcDer, trueDer, trueDerSz), 0);
XFREE(calcDer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return EXPECT_RESULT();
}
#endif
int test_SetAsymKeyDer(void)
{
EXPECT_DECLS;
#if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519)
byte algId[] = {43, 101, 112};
byte version[] = {0x0};
byte keyPat = 0xcc;
byte* privKey = NULL;
word32 privKeySz = 0;
byte* pubKey = NULL;
word32 pubKeySz = 0;
byte trueDer[310];
word32 trueDerSz = 0;
privKeySz = 32;
pubKeySz = 0;
trueDerSz = 48;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = trueDerSz - 2;
trueDer[2] = ASN_INTEGER;
trueDer[3] = sizeof(version);
trueDer[4] = version[0];
trueDer[5] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[6] = sizeof(algId) + 2;
trueDer[7] = ASN_OBJECT_ID;
trueDer[8] = sizeof(algId);
trueDer[9] = algId[0];
trueDer[10] = algId[1];
trueDer[11] = algId[2];
trueDer[12] = ASN_OCTET_STRING;
trueDer[13] = privKeySz + 2;
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz;
privKey = &trueDer[16];
XMEMSET(privKey, keyPat, privKeySz);
pubKey = NULL;
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 127;
pubKeySz = 0;
trueDerSz = 145;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = 0x81;
trueDer[15] = privKeySz + 2;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = privKeySz;
privKey = &trueDer[18];
XMEMSET(privKey, keyPat, privKeySz);
pubKey = NULL;
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 128;
pubKeySz = 0;
trueDerSz = 147;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = 0x81;
trueDer[15] = privKeySz + 3;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = 0x81;
trueDer[18] = privKeySz;
privKey = &trueDer[19];
XMEMSET(privKey, keyPat, privKeySz);
pubKey = NULL;
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 255;
pubKeySz = 0;
trueDerSz = 276;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = 0x82;
trueDer[16] = ((privKeySz + 3) >> 8) & 0xff;
trueDer[17] = (privKeySz + 3) & 0xff;
trueDer[18] = ASN_OCTET_STRING;
trueDer[19] = 0x81;
trueDer[20] = privKeySz;
privKey = &trueDer[21];
XMEMSET(privKey, keyPat, privKeySz);
pubKey = NULL;
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 256;
pubKeySz = 0;
trueDerSz = 278;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = 0x82;
trueDer[16] = ((privKeySz + 4) >> 8) & 0xff;
trueDer[17] = (privKeySz + 4) & 0xff;
trueDer[18] = ASN_OCTET_STRING;
trueDer[19] = 0x82;
trueDer[20] = (privKeySz >> 8) & 0xff;
trueDer[21] = privKeySz & 0xff;
privKey = &trueDer[22];
XMEMSET(privKey, keyPat, privKeySz);
pubKey = NULL;
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 32;
pubKeySz = 32;
trueDerSz = 82;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = trueDerSz - 2;
trueDer[2] = ASN_INTEGER;
trueDer[3] = sizeof(version);
trueDer[4] = version[0];
trueDer[5] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[6] = sizeof(algId) + 2;
trueDer[7] = ASN_OBJECT_ID;
trueDer[8] = sizeof(algId);
trueDer[9] = algId[0];
trueDer[10] = algId[1];
trueDer[11] = algId[2];
trueDer[12] = ASN_OCTET_STRING;
trueDer[13] = privKeySz + 2;
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz;
privKey = &trueDer[16];
XMEMSET(privKey, keyPat, privKeySz);
trueDer[48] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[49] = pubKeySz;
pubKey = &trueDer[50];
XMEMSET(pubKey, keyPat, pubKeySz);
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 32;
pubKeySz = 128;
trueDerSz = 180;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = privKeySz + 2;
trueDer[15] = ASN_OCTET_STRING;
trueDer[16] = privKeySz;
privKey = &trueDer[17];
XMEMSET(privKey, keyPat, privKeySz);
trueDer[49] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[50] = 0x81;
trueDer[51] = pubKeySz;
pubKey = &trueDer[52];
XMEMSET(pubKey, keyPat, pubKeySz);
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
privKeySz = 32;
pubKeySz = 256;
trueDerSz = 310;
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz + 2;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = privKeySz;
privKey = &trueDer[18];
XMEMSET(privKey, keyPat, privKeySz);
trueDer[50] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[51] = 0x82;
trueDer[52] = (pubKeySz >> 8) & 0xff;
trueDer[53] = pubKeySz & 0xff;
pubKey = &trueDer[54];
XMEMSET(pubKey, keyPat, pubKeySz);
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
#endif
return EXPECT_RESULT();
}
#ifndef NO_ASN
static int test_GetSetShortInt_once(word32 val, byte* valDer, word32 valDerSz)
{
EXPECT_DECLS;
#ifndef NO_PWDBASED
#if !defined(WOLFSSL_ASN_TEMPLATE) || defined(HAVE_PKCS8) || \
defined(HAVE_PKCS12)
byte outDer[MAX_SHORT_SZ];
word32 outDerSz = 0;
word32 inOutIdx = 0;
word32 maxIdx = MAX_SHORT_SZ;
int value;
ExpectIntLE(2 + valDerSz, MAX_SHORT_SZ);
ExpectIntEQ(outDerSz = SetShortInt(outDer, &inOutIdx, val, maxIdx),
2 + valDerSz);
ExpectIntEQ(outDer[0], ASN_INTEGER);
ExpectIntEQ(outDer[1], valDerSz);
ExpectIntEQ(XMEMCMP(outDer + 2, valDer, valDerSz), 0);
if (val < 0x80000000) {
inOutIdx = 0;
ExpectIntEQ(val, GetShortInt(outDer, &inOutIdx, &value, maxIdx));
}
#endif
#endif
(void)val;
(void)valDer;
(void)valDerSz;
return EXPECT_RESULT();
}
#endif
int test_GetSetShortInt(void)
{
EXPECT_DECLS;
#ifndef NO_ASN
byte valDer[MAX_SHORT_SZ] = {0};
{
valDer[0] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x00, valDer, 1));
valDer[0] = 0x00;
valDer[1] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0xff, valDer, 2));
valDer[0] = 0x01;
valDer[1] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x0100, valDer, 2));
valDer[0] = 0x00;
valDer[1] = 0xff;
valDer[2] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0xffff, valDer, 3));
valDer[0] = 0x01;
valDer[1] = 0x00;
valDer[2] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x010000, valDer, 3));
valDer[0] = 0x00;
valDer[1] = 0xff;
valDer[2] = 0xff;
valDer[3] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0xffffff, valDer, 4));
valDer[0] = 0x01;
valDer[1] = 0x00;
valDer[2] = 0x00;
valDer[3] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x01000000, valDer, 4));
valDer[0] = 0x00;
valDer[1] = 0xff;
valDer[2] = 0xff;
valDer[3] = 0xff;
valDer[4] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0xffffffff, valDer, 5));
}
{
valDer[0] = 0x7f;
EXPECT_TEST(test_GetSetShortInt_once(0x7f, valDer, 1));
valDer[0] = 0x00;
valDer[1] = 0x80;
EXPECT_TEST(test_GetSetShortInt_once(0x80, valDer, 2));
valDer[0] = 0x7f;
valDer[1] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0x7fff, valDer, 2));
valDer[0] = 0x00;
valDer[1] = 0x80;
valDer[2] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x8000, valDer, 3));
valDer[0] = 0x7f;
valDer[1] = 0xff;
valDer[2] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0x7fffff, valDer, 3));
valDer[0] = 0x00;
valDer[1] = 0x80;
valDer[2] = 0x00;
valDer[3] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x800000, valDer, 4));
valDer[0] = 0x7f;
valDer[1] = 0xff;
valDer[2] = 0xff;
valDer[3] = 0xff;
EXPECT_TEST(test_GetSetShortInt_once(0x7fffffff, valDer, 4));
valDer[0] = 0x00;
valDer[1] = 0x80;
valDer[2] = 0x00;
valDer[3] = 0x00;
valDer[4] = 0x00;
EXPECT_TEST(test_GetSetShortInt_once(0x80000000, valDer, 5));
}
{
valDer[0] = 0x01;
EXPECT_TEST(test_GetSetShortInt_once(0x01, valDer, 1));
}
#if !defined(NO_PWDBASED) || defined(WOLFSSL_ASN_EXTRA)
{
word32 idx = 0;
int value;
valDer[0] = ASN_INTEGER;
valDer[1] = 1;
valDer[2] = 0x80;
ExpectIntEQ(GetShortInt(valDer, &idx, &value, 3),
WC_NO_ERR_TRACE(ASN_EXPECT_0_E));
idx = 0;
valDer[0] = ASN_INTEGER;
valDer[1] = 4;
valDer[2] = 0xFF;
valDer[3] = 0xFF;
valDer[4] = 0xFF;
valDer[5] = 0xFF;
ExpectIntEQ(GetShortInt(valDer, &idx, &value, 6),
WC_NO_ERR_TRACE(ASN_EXPECT_0_E));
}
#endif
#endif
return EXPECT_RESULT();
}
int test_wc_IndexSequenceOf(void)
{
EXPECT_DECLS;
#ifndef NO_ASN
const byte int_seq[] = {
0x30, 0x0A,
0x02, 0x01, 0x0A,
0x02, 0x02, 0x00, 0xF0,
0x02, 0x01, 0x7F,
};
const byte bad_seq[] = {
0xA0, 0x01, 0x01,
};
const byte empty_seq[] = {
0x30, 0x00,
};
const byte * element;
word32 elementSz;
ExpectIntEQ(wc_IndexSequenceOf(int_seq, sizeof(int_seq), 0U, &element, &elementSz), 0);
ExpectPtrEq(element, &int_seq[2]);
ExpectIntEQ(elementSz, 3);
ExpectIntEQ(wc_IndexSequenceOf(int_seq, sizeof(int_seq), 1U, &element, &elementSz), 0);
ExpectPtrEq(element, &int_seq[5]);
ExpectIntEQ(elementSz, 4);
ExpectIntEQ(wc_IndexSequenceOf(int_seq, sizeof(int_seq), 2U, &element, &elementSz), 0);
ExpectPtrEq(element, &int_seq[9]);
ExpectIntEQ(elementSz, 3);
ExpectIntEQ(wc_IndexSequenceOf(int_seq, sizeof(int_seq), 3U, &element, &elementSz), WC_NO_ERR_TRACE(BAD_INDEX_E));
ExpectIntEQ(wc_IndexSequenceOf(bad_seq, sizeof(bad_seq), 0U, &element, &elementSz), WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wc_IndexSequenceOf(empty_seq, sizeof(empty_seq), 0U, &element, &elementSz), WC_NO_ERR_TRACE(BAD_INDEX_E));
#endif
return EXPECT_RESULT();
}
int test_wolfssl_local_MatchBaseName(void)
{
EXPECT_DECLS;
#if !defined(NO_CERTS) && !defined(NO_ASN) && !defined(IGNORE_NAME_CONSTRAINTS)
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, "domain.com", 10), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"DOMAIN.COM", 10, "domain.com", 10), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"sub.domain.com", 14, "domain.com", 10), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"a.b.domain.com", 14, "domain.com", 10), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"sub.domain.com", 14, ".domain.com", 11), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"a.b.domain.com", 14, ".domain.com", 11), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"fakedomain.com", 14, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"notdomain.com", 13, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"xexample.com", 12, "example.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"fakedomain.com", 14, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"other.com", 9, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
".domain.com", 11, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, "user@domain.com", 15), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@sub.domain.com", 19, ".domain.com", 11), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, "domain.com", 10), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@sub.domain.com", 19, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"@domain.com", 11, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@", 5, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@@domain.com", 16, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain@extra.com", 21, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"userdomain.com", 14, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@other.com", 14, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@fakedomain.com", 19, ".domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, "@domain.com", 11), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, "user@", 5), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE,
"CN=test", 7, "CN=test", 7), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE,
"CN=test,O=org", 13, "CN=test", 7), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE,
"CN=other", 8, "CN=test", 7), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DIR_TYPE,
"CN=TEST", 7, "CN=test", 7), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
NULL, 10, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, NULL, 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"", 0, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, "", 0), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(0xFF,
"domain.com", 10, "domain.com", 10), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
".", 1, ".", 1), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"a.com", 5, "domain.com", 10), 0);
#endif
return EXPECT_RESULT();
}
int test_wc_DecodeRsaPssParams(void)
{
EXPECT_DECLS;
#if defined(WC_RSA_PSS) && !defined(NO_RSA) && !defined(NO_ASN)
enum wc_HashType hash;
int mgf;
int saltLen;
static const byte pssParamsSha256[] = {
0x30, 0x34,
0xA0, 0x0F,
0x30, 0x0D,
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01,
0x05, 0x00,
0xA1, 0x1C,
0x30, 0x1A,
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
0x01, 0x01, 0x08,
0x30, 0x0D,
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01,
0x05, 0x00,
0xA2, 0x03,
0x02, 0x01, 0x20,
};
static const byte pssParamsHashOnly[] = {
0x30, 0x11,
0xA0, 0x0F,
0x30, 0x0D,
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01,
0x05, 0x00,
};
static const byte pssParamsSaltOnly[] = {
0x30, 0x05,
0xA2, 0x03,
0x02, 0x01, 0x30,
};
static const byte pssParamsNull[] = { 0x05, 0x00 };
static const byte pssParamsEmptySeq[] = { 0x30, 0x00 };
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams((const byte*)"", 0,
&hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA);
ExpectIntEQ(mgf, WC_MGF1SHA1);
ExpectIntEQ(saltLen, 20);
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsNull,
(word32)sizeof(pssParamsNull), &hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA);
ExpectIntEQ(mgf, WC_MGF1SHA1);
ExpectIntEQ(saltLen, 20);
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsEmptySeq,
(word32)sizeof(pssParamsEmptySeq), &hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA);
ExpectIntEQ(mgf, WC_MGF1SHA1);
ExpectIntEQ(saltLen, 20);
#ifndef NO_SHA256
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsSha256,
(word32)sizeof(pssParamsSha256), &hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA256);
ExpectIntEQ(mgf, WC_MGF1SHA256);
ExpectIntEQ(saltLen, 32);
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsHashOnly,
(word32)sizeof(pssParamsHashOnly), &hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA256);
ExpectIntEQ(mgf, WC_MGF1SHA1);
ExpectIntEQ(saltLen, 20);
#endif
hash = WC_HASH_TYPE_NONE;
mgf = 0;
saltLen = 0;
ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsSaltOnly,
(word32)sizeof(pssParamsSaltOnly), &hash, &mgf, &saltLen), 0);
ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA);
ExpectIntEQ(mgf, WC_MGF1SHA1);
ExpectIntEQ(saltLen, 48);
ExpectIntEQ(wc_DecodeRsaPssParams(NULL, 10, &hash, &mgf, &saltLen),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
{
static const byte badTag[] = { 0x01, 0x00 };
ExpectIntEQ(wc_DecodeRsaPssParams(badTag, (word32)sizeof(badTag),
&hash, &mgf, &saltLen), WC_NO_ERR_TRACE(ASN_PARSE_E));
}
#endif
return EXPECT_RESULT();
}
int test_DecodeAltNames_length_underflow(void)
{
EXPECT_DECLS;
#if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_ASN)
static const unsigned char good_san_cert[] = {
0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
0x0d, 0x00, 0x30,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xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
0x09, 0x2a, 0x86, 0x48, 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xa4, 0x0b, 0x40, 0x04,
0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
};
#define SAN_SEQ_LEN_OFFSET 418
DecodedCert cert;
unsigned char bad_san_cert[sizeof(good_san_cert)];
wc_InitDecodedCert(&cert, good_san_cert, (word32)sizeof(good_san_cert),
NULL);
ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0);
wc_FreeDecodedCert(&cert);
XMEMCPY(bad_san_cert, good_san_cert, sizeof(good_san_cert));
bad_san_cert[SAN_SEQ_LEN_OFFSET] = 0x03;
wc_InitDecodedCert(&cert, bad_san_cert, (word32)sizeof(bad_san_cert),
NULL);
ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL),
WC_NO_ERR_TRACE(ASN_PARSE_E));
wc_FreeDecodedCert(&cert);
#endif
return EXPECT_RESULT();
}
int test_wc_DecodeObjectId(void)
{
EXPECT_DECLS;
#if !defined(NO_ASN) && \
(defined(HAVE_OID_DECODING) || defined(WOLFSSL_ASN_PRINT))
{
static const byte oid_sha256rsa[] = {
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b
};
word16 out[MAX_OID_SZ];
word32 outSz;
outSz = MAX_OID_SZ;
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
out, &outSz), 0);
ExpectIntEQ((int)outSz, 7);
ExpectIntEQ(out[0], 1);
ExpectIntEQ(out[1], 2);
ExpectIntEQ(out[2], 840);
ExpectIntEQ(out[3], (word16)113549);
ExpectIntEQ(out[4], 1);
ExpectIntEQ(out[5], 1);
ExpectIntEQ(out[6], 11);
outSz = MAX_OID_SZ;
ExpectIntEQ(DecodeObjectId(NULL, sizeof(oid_sha256rsa), out, &outSz),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
out, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
outSz = 1;
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
out, &outSz),
WC_NO_ERR_TRACE(BUFFER_E));
outSz = 0;
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
out, &outSz),
WC_NO_ERR_TRACE(BUFFER_E));
{
static const byte oid_one_byte[] = { 0x2a };
outSz = 2;
ExpectIntEQ(DecodeObjectId(oid_one_byte, sizeof(oid_one_byte),
out, &outSz), 0);
ExpectIntEQ((int)outSz, 2);
ExpectIntEQ(out[0], 1);
ExpectIntEQ(out[1], 2);
}
outSz = 3;
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
out, &outSz),
WC_NO_ERR_TRACE(BUFFER_E));
}
#endif
return EXPECT_RESULT();
}