|
|
551b01 |
diff -urN tpm2-pkcs11-1.5.0/src/lib/ssl_util.c tpm2-pkcs11-1.5.0-fix/src/lib/ssl_util.c
|
|
|
551b01 |
--- tpm2-pkcs11-1.5.0/src/lib/ssl_util.c 2020-11-03 17:36:45.000000000 -0700
|
|
|
551b01 |
+++ tpm2-pkcs11-1.5.0-fix/src/lib/ssl_util.c 2021-05-26 10:17:23.723128758 -0700
|
|
|
551b01 |
@@ -438,82 +438,29 @@
|
|
|
551b01 |
return rv;
|
|
|
551b01 |
}
|
|
|
551b01 |
|
|
|
551b01 |
-static CK_RV create_ecdsa_sig(CK_BYTE_PTR sig, CK_ULONG siglen, ECDSA_SIG **outsig) {
|
|
|
551b01 |
-
|
|
|
551b01 |
- if (siglen & 1) {
|
|
|
551b01 |
- LOGE("Expected ECDSA signature length to be even, got : %lu",
|
|
|
551b01 |
- siglen);
|
|
|
551b01 |
- return CKR_SIGNATURE_LEN_RANGE;
|
|
|
551b01 |
- }
|
|
|
551b01 |
-
|
|
|
551b01 |
- size_t len = siglen >> 1;
|
|
|
551b01 |
-
|
|
|
551b01 |
- unsigned char *rbuf = sig;
|
|
|
551b01 |
- unsigned char *sbuf = &sig[len];
|
|
|
551b01 |
-
|
|
|
551b01 |
- BIGNUM *r = BN_bin2bn(rbuf, len, NULL);
|
|
|
551b01 |
- if (!r) {
|
|
|
551b01 |
- LOGE("Could not make bignum for r");
|
|
|
551b01 |
- return CKR_GENERAL_ERROR;
|
|
|
551b01 |
- }
|
|
|
551b01 |
-
|
|
|
551b01 |
- BIGNUM *s = BN_bin2bn(sbuf, len, NULL);
|
|
|
551b01 |
- if (!s) {
|
|
|
551b01 |
- LOGE("Could not make bignum for s");
|
|
|
551b01 |
- BN_free(r);
|
|
|
551b01 |
- return CKR_GENERAL_ERROR;
|
|
|
551b01 |
- }
|
|
|
551b01 |
-
|
|
|
551b01 |
- ECDSA_SIG *ossl_sig = ECDSA_SIG_new();
|
|
|
551b01 |
- if (!ossl_sig) {
|
|
|
551b01 |
- LOGE("oom");
|
|
|
551b01 |
- return CKR_HOST_MEMORY;
|
|
|
551b01 |
- }
|
|
|
551b01 |
-
|
|
|
551b01 |
- int rc = ECDSA_SIG_set0(ossl_sig, r, s);
|
|
|
551b01 |
- if (!rc) {
|
|
|
551b01 |
- LOGE("Could not call ECDSA_SIG_set0");
|
|
|
551b01 |
- ECDSA_SIG_free(ossl_sig);
|
|
|
551b01 |
- return CKR_GENERAL_ERROR;
|
|
|
551b01 |
- }
|
|
|
551b01 |
-
|
|
|
551b01 |
- *outsig = ossl_sig;
|
|
|
551b01 |
-
|
|
|
551b01 |
- return CKR_OK;
|
|
|
551b01 |
-}
|
|
|
551b01 |
-
|
|
|
551b01 |
static CK_RV do_sig_verify_ec(EVP_PKEY *pkey,
|
|
|
551b01 |
CK_BYTE_PTR digest, CK_ULONG digest_len,
|
|
|
551b01 |
CK_BYTE_PTR signature, CK_ULONG signature_len) {
|
|
|
551b01 |
|
|
|
551b01 |
- EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
|
|
|
551b01 |
- if (!eckey) {
|
|
|
551b01 |
- LOGE("Expected EC Key");
|
|
|
551b01 |
- return CKR_GENERAL_ERROR;
|
|
|
551b01 |
- }
|
|
|
551b01 |
+ int rc = CKR_OK;
|
|
|
551b01 |
+ EVP_PKEY_CTX *pctx = NULL;
|
|
|
551b01 |
|
|
|
551b01 |
- /*
|
|
|
551b01 |
- * OpenSSL expects ASN1 framed signatures, PKCS11 does flate
|
|
|
551b01 |
- * R + S signatures, so convert it to ASN1 framing.
|
|
|
551b01 |
- * See:
|
|
|
551b01 |
- * https://github.com/tpm2-software/tpm2-pkcs11/issues/277
|
|
|
551b01 |
- * For details.
|
|
|
551b01 |
- */
|
|
|
551b01 |
- ECDSA_SIG *ossl_sig = NULL;
|
|
|
551b01 |
- CK_RV rv = create_ecdsa_sig(signature, signature_len, &ossl_sig);
|
|
|
551b01 |
- if (rv != CKR_OK) {
|
|
|
551b01 |
- return rv;
|
|
|
551b01 |
+ if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) {
|
|
|
551b01 |
+ rc = CKR_HOST_MEMORY;
|
|
|
551b01 |
+ goto fail;
|
|
|
551b01 |
}
|
|
|
551b01 |
|
|
|
551b01 |
- int rc = ECDSA_do_verify(digest, digest_len, ossl_sig, eckey);
|
|
|
551b01 |
- if (rc < 0) {
|
|
|
551b01 |
- ECDSA_SIG_free(ossl_sig);
|
|
|
551b01 |
- SSL_UTIL_LOGE("ECDSA_do_verify failed");
|
|
|
551b01 |
- return CKR_GENERAL_ERROR;
|
|
|
551b01 |
+ if (EVP_PKEY_verify_init(pctx) != 1 ||
|
|
|
551b01 |
+ EVP_PKEY_verify(pctx, signature, signature_len,
|
|
|
551b01 |
+ digest, digest_len) != 1) {
|
|
|
551b01 |
+ rc = CKR_SIGNATURE_INVALID;
|
|
|
551b01 |
+ goto fail;
|
|
|
551b01 |
}
|
|
|
551b01 |
- ECDSA_SIG_free(ossl_sig);
|
|
|
551b01 |
|
|
|
551b01 |
- return rc == 1 ? CKR_OK : CKR_SIGNATURE_INVALID;
|
|
|
551b01 |
+fail:
|
|
|
551b01 |
+ if (pctx)
|
|
|
551b01 |
+ EVP_PKEY_CTX_free(pctx);
|
|
|
551b01 |
+ return rc;
|
|
|
551b01 |
}
|
|
|
551b01 |
|
|
|
551b01 |
CK_RV ssl_util_sig_verify(EVP_PKEY *pkey,
|