Skip to content

Commit add7428

Browse files
committed
TLS, SM2: fixes to get SM handshakes working
Pass around the algorithm id from the private key so that the WOLFSSL or WOLFSSL_CTX get the correct key format set. Use different verification context when self-signed certificate with SM2 and SM3 signature but public key OID is ECC.
1 parent 1ddc552 commit add7428

5 files changed

Lines changed: 50 additions & 39 deletions

File tree

src/ssl_load.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@
116116
* @param [in, out] info Info for encryption.
117117
* @param [in] heap Dynamic memory allocation hint.
118118
* @param [out] der Holds DER encoded data.
119+
* @param [out] algId Algorithm identifier for private keys.
119120
* @return 0 on success.
120121
* @return NOT_COMPILED_IN when format is PEM and PEM not supported.
121122
* @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding.
122123
* @return MEMORY_E when dynamic memory allocation fails.
123124
*/
124125
static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
125-
int type, EncryptedInfo* info, void* heap, DerBuffer** der)
126+
int type, EncryptedInfo* info, void* heap, DerBuffer** der, int* algId)
126127
{
127128
int ret;
128129

@@ -131,7 +132,7 @@ static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
131132
/* Data in buffer has PEM format - extract DER data. */
132133
if (format == WOLFSSL_FILETYPE_PEM) {
133134
#ifdef WOLFSSL_PEM_TO_DER
134-
ret = PemToDer(buff, len, type, der, heap, info, NULL);
135+
ret = PemToDer(buff, len, type, der, heap, info, algId);
135136
if (ret != 0) {
136137
FreeDer(der);
137138
}
@@ -341,7 +342,7 @@ static int ProcessUserChain(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
341342

342343
/* Get a certificate as DER. */
343344
ret = DataToDerBuffer(buff + consumed, (word32)(sz - consumed),
344-
format, type, info, heap, &part);
345+
format, type, info, heap, &part, NULL);
345346
if (ret == 0) {
346347
/* Process the user certificate. */
347348
ret = ProcessUserCert(ctx->cm, &part, type, verify,
@@ -604,6 +605,12 @@ static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
604605
idx = 0;
605606
ret = wc_EccPublicKeyDecode(der->buffer, &idx, key, der->length);
606607
}
608+
#endif
609+
#ifdef WOLFSSL_SM2
610+
if (*keyFormat == SM2k) {
611+
ret = wc_ecc_set_curve(key, WOLFSSL_SM2_KEY_BITS / 8,
612+
ECC_SM2P256V1);
613+
}
607614
#endif
608615
if (ret == 0) {
609616
/* Get the minimum ECC key size from SSL or SSL context object. */
@@ -1317,52 +1324,53 @@ static void ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
13171324
* @param [in] heap Dynamic memory allocation hint.
13181325
* @param [in] type Type of data:
13191326
* PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
1327+
* @param [in] algId Algorithm id of key.
13201328
* @return 0 on success.
13211329
* @return WOLFSSL_BAD_FILE when not able to decode.
13221330
*/
13231331
static int ProcessBufferPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
1324-
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type)
1332+
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type,
1333+
int algId)
13251334
{
13261335
int ret;
1327-
int keyFormat = 0;
13281336
#if (defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)) || \
13291337
defined(HAVE_PKCS8)
1330-
word32 algId = 0;
1338+
word32 p8AlgId = 0;
13311339
#endif
13321340

13331341
(void)info;
13341342
(void)format;
13351343

13361344
#ifdef HAVE_PKCS8
13371345
/* Try and remove PKCS8 header and get algorithm id. */
1338-
ret = ToTraditional_ex(der->buffer, der->length, &algId);
1346+
ret = ToTraditional_ex(der->buffer, der->length, &p8AlgId);
13391347
if (ret > 0) {
13401348
/* Header stripped inline. */
13411349
der->length = ret;
1342-
keyFormat = algId;
1350+
algId = p8AlgId;
13431351
}
13441352
#endif
13451353

13461354
/* Put the data into the SSL or SSL context object. */
13471355
ProcessBufferPrivKeyHandleDer(ctx, ssl, &der, type);
13481356
/* Try to decode the DER data. */
1349-
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
1357+
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);
13501358

13511359
#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
13521360
/* If private key type PKCS8 header wasn't already removed (algId == 0). */
1353-
if (((ret != 0) || (keyFormat == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
1361+
if (((ret != 0) || (algId == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
13541362
(info->passwd_cb != NULL) && (algId == 0)) {
13551363
/* Try to decrypt DER data as a PKCS#8 private key. */
13561364
ret = ProcessBufferPrivPkcs8Dec(info, der, heap);
13571365
if (ret >= 0) {
13581366
/* Try to decode decrypted data. */
1359-
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
1367+
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);
13601368
}
13611369
}
13621370
#endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
13631371

1364-
/* Check if we were able to determine key format. */
1365-
if ((ret == 0) && (keyFormat == 0)) {
1372+
/* Check if we were able to determine algorithm id. */
1373+
if ((ret == 0) && (algId == 0)) {
13661374
#ifdef OPENSSL_EXTRA
13671375
/* Decryption password is probably wrong. */
13681376
if (info->passwd_cb) {
@@ -2265,6 +2273,7 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
22652273
#else
22662274
EncryptedInfo info[1];
22672275
#endif
2276+
int algId = 0;
22682277

22692278
WOLFSSL_ENTER("ProcessBuffer");
22702279

@@ -2306,7 +2315,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
23062315
#endif
23072316

23082317
/* Get the DER data for a private key or certificate. */
2309-
ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der);
2318+
ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der,
2319+
&algId);
23102320
if (used != NULL) {
23112321
/* Update to amount used/consumed. */
23122322
*used = info->consumed;
@@ -2321,7 +2331,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
23212331

23222332
if ((ret == 0) && IS_PRIVKEY_TYPE(type)) {
23232333
/* Process the private key. */
2324-
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type);
2334+
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type,
2335+
algId);
23252336
#ifdef WOLFSSL_SMALL_STACK
23262337
/* Info no longer needed - keep max memory usage down. */
23272338
XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);

wolfcrypt/benchmark/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5536,7 +5536,7 @@ void bench_sm4_gcm(void)
55365536
#endif
55375537

55385538
#ifdef WOLFSSL_SM4_CCM
5539-
void bench_sm4_ccm()
5539+
void bench_sm4_ccm(void)
55405540
{
55415541
wc_Sm4 enc;
55425542
double start;

wolfcrypt/src/asn.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23790,13 +23790,19 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
2379023790
if (cert->ca) {
2379123791
if (verify == VERIFY || verify == VERIFY_OCSP ||
2379223792
verify == VERIFY_SKIP_DATE) {
23793+
word32 keyOID = cert->ca->keyOID;
23794+
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
23795+
if (cert->selfSigned && (cert->signatureOID == CTC_SM3wSM2)) {
23796+
keyOID = SM2k;
23797+
}
23798+
#endif
2379323799
/* try to confirm/verify signature */
2379423800
if ((ret = ConfirmSignature(&cert->sigCtx,
2379523801
cert->source + cert->certBegin,
2379623802
cert->sigIndex - cert->certBegin,
2379723803
cert->ca->publicKey, cert->ca->pubKeySize,
23798-
cert->ca->keyOID, cert->signature,
23799-
cert->sigLength, cert->signatureOID,
23804+
keyOID, cert->signature, cert->sigLength,
23805+
cert->signatureOID,
2380023806
#ifdef WC_RSA_PSS
2380123807
cert->source + cert->sigParamsIndex,
2380223808
cert->sigParamsLength,

wolfcrypt/src/port/arm/armv8-aes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
15321532
"USHR v7.2d, v7.2d, #56 \n"
15331533

15341534
"# AAD \n"
1535+
"CBZ %[a], 20f \n"
15351536
"CBZ %w[aSz], 20f \n"
15361537
"MOV w12, %w[aSz] \n"
15371538

@@ -1702,6 +1703,7 @@ void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
17021703

17031704
"20: \n"
17041705
"# Cipher Text \n"
1706+
"CBZ %[c], 120f \n"
17051707
"CBZ %w[cSz], 120f \n"
17061708
"MOV w12, %w[cSz] \n"
17071709

wolfcrypt/test/test.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29740,21 +29740,19 @@ static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
2974029740
#ifdef WOLFSSL_SM2
2974129741
#ifdef HAVE_ECC_VERIFY
2974229742
#if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
29743-
#ifdef WOLFSSL_SM2
29744-
#ifdef HAVE_OID_ENCODING
29745-
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
29746-
#define CODED_SM2P256V1_SZ 6
29747-
#else
29748-
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
29749-
#define CODED_SM2P256V1_SZ 10
29750-
#endif
29751-
#ifndef WOLFSSL_ECC_CURVE_STATIC
29752-
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
29753-
#else
29754-
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
29755-
#endif
29756-
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
29757-
#endif /* WOLFSSL_SM2 */
29743+
#ifdef HAVE_OID_ENCODING
29744+
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
29745+
#define CODED_SM2P256V1_SZ 6
29746+
#else
29747+
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
29748+
#define CODED_SM2P256V1_SZ 10
29749+
#endif
29750+
#ifndef WOLFSSL_ECC_CURVE_STATIC
29751+
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
29752+
#else
29753+
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
29754+
#endif
29755+
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
2975829756
#define ECC_SM2P256V1_TEST 102
2975929757
static int test_sm2_verify_caseA2(void)
2976029758
{
@@ -29931,9 +29929,7 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
2993129929
WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
2993229930
WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
2993329931
int i;
29934-
#ifdef HAVE_ECC_VERIFY
2993529932
int verify;
29936-
#endif /* HAVE_ECC_VERIFY */
2993729933
#endif /* HAVE_ECC_SIGN */
2993829934
int ret;
2993929935
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@@ -30129,7 +30125,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3012930125
if (ret != 0)
3013030126
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
3013130127

30132-
#ifdef HAVE_ECC_VERIFY
3013330128
for (i = 0; i < testVerifyCount; i++) {
3013430129
verify = 0;
3013530130
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
@@ -30139,7 +30134,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3013930134
if (verify != 1)
3014030135
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
3014130136
}
30142-
#endif /* HAVE_ECC_VERIFY */
3014330137
#endif /* ECC_SHAMIR */
3014430138

3014530139
/* test DSA sign hash with sequence (0,1,2,3,4,...) */
@@ -30152,7 +30146,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3015230146
if (ret != 0)
3015330147
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
3015430148

30155-
#ifdef HAVE_ECC_VERIFY
3015630149
for (i = 0; i < testVerifyCount; i++) {
3015730150
verify = 0;
3015830151
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
@@ -30162,7 +30155,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3016230155
if (verify != 1)
3016330156
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
3016430157
}
30165-
#endif /* HAVE_ECC_VERIFY */
3016630158
#endif /* HAVE_ECC_SIGN */
3016730159
#endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
3016830160

0 commit comments

Comments
 (0)