Repository navigation
feat(libSceLibreSsl3): verify digests natively in EVP_DigestVerifyInit and EVP_DigestVerifyFinal #2319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(libSceLibreSsl3): verify digests natively in EVP_DigestVerifyInit and EVP_DigestVerifyFinal #2319
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -530,6 +530,7 @@ | |
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <initializer_list> | ||
| #include <limits> | ||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
@@ -581,6 +582,10 @@ constexpr int ReasonSaltLengthTooSmall = 164; | |
| constexpr int ReasonInvalidDigestLength = 143; | ||
| constexpr int ReasonInputNotInitialized = 111; | ||
| constexpr int ReasonNoDefaultDigest = 158; | ||
| constexpr int ReasonInvalidDigestType = 138; | ||
| constexpr int EcdsaWithSha1Type = 416; | ||
|
|
||
| bool IsLibreSslOnlyPkeyMethod(int keyType); | ||
|
|
||
| constexpr int Asn1MethodFlags = 4; | ||
| constexpr int DefaultDigestNid = 672; | ||
|
|
@@ -1156,15 +1161,103 @@ const LibreSsl3PkeyMethod RsaPssMethod { | |
| nullptr, | ||
| }; | ||
|
|
||
| struct EcKeyContext { | ||
| const DigestMethod* digest; | ||
| }; | ||
|
|
||
| EcKeyContext* EnsureEcContextData(LibreSsl3PkeyContext* context) { | ||
| if (context->data) return static_cast<EcKeyContext*>(context->data); | ||
| auto* ecContext = static_cast<EcKeyContext*>(calloc_nid_postfix(1, sizeof(EcKeyContext))); | ||
| if (!ecContext) { | ||
| LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, AllocationFailure); | ||
| return nullptr; | ||
| } | ||
| context->data = ecContext; | ||
| return ecContext; | ||
| } | ||
|
|
||
| bool IsAllowedEcDigest(int type) { | ||
| switch (type) { | ||
| case Sha1Type: | ||
| case EcdsaWithSha1Type: | ||
| case Sha224Type: | ||
| case Sha256Type: | ||
| case Sha384Type: | ||
| case Sha512Type: | ||
| case Sha3_224Type: | ||
| case Sha3_256Type: | ||
| case Sha3_384Type: | ||
| case Sha3_512Type: | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| int APS5_VABI EcContextInitialize(LibreSsl3PkeyContext*) { | ||
| return 1; | ||
| } | ||
|
|
||
| void APS5_VABI EcContextClear(LibreSsl3PkeyContext*) {} | ||
| void APS5_VABI EcContextClear(LibreSsl3PkeyContext* context) { | ||
| if (!context->data) return; | ||
| free_nid_postfix(context->data); | ||
| context->data = nullptr; | ||
| } | ||
|
|
||
| int APS5_VABI EcControl(LibreSsl3PkeyContext*, int command, int, void*) { | ||
| if (command == LibreSsl3PkeyControlPeerKey) return 1; | ||
| return -2; | ||
| int APS5_VABI EcContextCopy(LibreSsl3PkeyContext* destination, LibreSsl3PkeyContext* source) { | ||
| if (!source->data) { | ||
| EcContextClear(destination); | ||
| return 1; | ||
| } | ||
| auto* ecContext = EnsureEcContextData(destination); | ||
| if (!ecContext) return 0; | ||
| ecContext->digest = static_cast<const EcKeyContext*>(source->data)->digest; | ||
| return 1; | ||
| } | ||
|
|
||
| int APS5_VABI EcVerify(LibreSsl3PkeyContext* context, const unsigned char* signature, std::size_t signatureLength, | ||
| const unsigned char* content, std::size_t contentLength) { | ||
| const auto* ecContext = static_cast<const EcKeyContext*>(context->data); | ||
| const int type = ecContext && ecContext->digest ? ecContext->digest->type : Sha1Type; | ||
| const auto* key = static_cast<const LibreSsl3EvpPkey*>(context->key); | ||
| auto* ecKey = key ? static_cast<EcKey*>(key->key) : nullptr; | ||
| if (!ecKey) NotImplemented_nid_no_patch("EVP_PKEY_CTX verify without a native EC key"); | ||
| if (signatureLength > static_cast<std::size_t>(std::numeric_limits<int>::max()) || | ||
| contentLength > static_cast<std::size_t>(std::numeric_limits<int>::max())) { | ||
| Reject("EVP_PKEY_verify EC input length exceeds int range"); | ||
| } | ||
| return ECDSA_verify_nid_postfix(type, content, static_cast<int>(contentLength), signature, | ||
| static_cast<int>(signatureLength), ecKey); | ||
| } | ||
|
|
||
| int APS5_VABI EcControl(LibreSsl3PkeyContext* context, int command, int, void* second) { | ||
| switch (command) { | ||
| case LibreSsl3PkeyControlMessageDigest: { | ||
| const auto* digest = static_cast<const DigestMethod*>(second); | ||
| if (!digest) Reject("EVP_PKEY_CTX_ctrl set EC digest without a digest"); | ||
| if (!IsAllowedEcDigest(digest->type)) { | ||
| LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonInvalidDigestType); | ||
| return 0; | ||
| } | ||
| auto* ecContext = EnsureEcContextData(context); | ||
| if (!ecContext) return 0; | ||
| ecContext->digest = digest; | ||
| return 1; | ||
| } | ||
| case LibreSsl3PkeyControlGetMessageDigest: { | ||
| if (!second) Reject("EVP_PKEY_CTX_ctrl get digest without an output"); | ||
| const auto* ecContext = static_cast<const EcKeyContext*>(context->data); | ||
| *static_cast<const DigestMethod**>(second) = ecContext ? ecContext->digest : nullptr; | ||
| return 1; | ||
| } | ||
| case LibreSsl3PkeyControlPeerKey: | ||
| case LibreSsl3PkeyControlDigestInit: | ||
| case LibreSsl3PkeyControlPkcs7Sign: | ||
| case LibreSsl3PkeyControlCmsSign: | ||
| return 1; | ||
| default: | ||
| return -2; | ||
| } | ||
| } | ||
|
|
||
| int APS5_VABI EcDerive(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength) { | ||
|
|
@@ -1214,14 +1307,14 @@ const LibreSsl3PkeyMethod EcMethod { | |
| LibreSsl3PkeyEc, | ||
| 0, | ||
| &EcContextInitialize, | ||
| nullptr, | ||
| &EcContextCopy, | ||
| &EcContextClear, | ||
| nullptr, | ||
| nullptr, | ||
| nullptr, | ||
| nullptr, | ||
| nullptr, | ||
| nullptr, | ||
| &EcVerify, | ||
| nullptr, | ||
| nullptr, | ||
| nullptr, | ||
|
|
@@ -1392,46 +1485,69 @@ int DigestInitialize(LibreSsl3::DigestContext* context, const DigestMethod* type | |
| return type->init(context); | ||
| } | ||
|
|
||
| int DigestSignInitialize(LibreSsl3::DigestContext* ctx, | ||
| LibreSsl3PkeyContext** pctx, const DigestMethod* type, LibreSsl3EvpPkey* pkey) { | ||
| if (!ctx->keyContext) | ||
| const DigestMethod* DefaultDigestForKey(LibreSsl3EvpPkey* pkey, const char* function) { | ||
| if (!pkey) Reject(std::string(function) + " requires a key when digest is null"); | ||
| int nid = 0; | ||
| if (pkey->method) { | ||
| const auto* keyAsn1 = static_cast<const LibreSsl3PkeyAsn1Method*>(pkey->method); | ||
| if (!keyAsn1->keyControl || | ||
| keyAsn1->keyControl(pkey, LibreSsl3Asn1PkeyControlDefaultDigestNid, 0, &nid) <= 0) { | ||
| QueueEvpError(ReasonNoDefaultDigest); | ||
| return nullptr; | ||
| } | ||
| } else if (pkey->type == LibreSsl3PkeyEc) { | ||
| nid = Sha1Type; | ||
| } else { | ||
| QueueEvpError(ReasonNoDefaultDigest); | ||
| return nullptr; | ||
| } | ||
| if (nid == Sha256Type) return reinterpret_cast<const DigestMethod*>(EVP_sha256_nid_postfix()); | ||
| if (nid == Sha1Type) return reinterpret_cast<const DigestMethod*>(EVP_sha1_nid_postfix()); | ||
| NotImplemented_nid_no_patch((std::string(function) + " default digest lookup for non-SHA1/SHA256").c_str()); | ||
| } | ||
|
|
||
| int DoSigVerInitialize(LibreSsl3::DigestContext* ctx, LibreSsl3PkeyContext** pctx, | ||
| const DigestMethod* type, LibreSsl3EvpPkey* pkey, bool verify) { | ||
| const char* const function = verify ? "EVP_DigestVerifyInit" : "EVP_DigestSignInit"; | ||
| if (!ctx) Reject(std::string(function) + " requires a digest context"); | ||
| if (!ctx->keyContext) { | ||
| if (verify && pkey && IsLibreSslOnlyPkeyMethod(pkey->type)) | ||
| NotImplemented_nid_no_patch("EVP_DigestVerifyInit for non-RSA/EC key type"); | ||
| ctx->keyContext = EVP_PKEY_CTX_new_nid_postfix(pkey, nullptr); | ||
| } | ||
| if (!ctx->keyContext) return 0; | ||
|
|
||
| const auto* method = MethodOf(static_cast<LibreSsl3PkeyContext*>(ctx->keyContext)); | ||
| const bool hasSignCtxCustom = (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) != 0; | ||
| if (!hasSignCtxCustom) { | ||
| if (type == nullptr) { | ||
| int nid = 0; | ||
| const auto* keyAsn1 = static_cast<const LibreSsl3PkeyAsn1Method*>(pkey->method); | ||
| if (!keyAsn1 || keyAsn1->keyControl(pkey, LibreSsl3Asn1PkeyControlDefaultDigestNid, 0, | ||
| &nid) <= 0) { | ||
| QueueEvpError(ReasonNoDefaultDigest); | ||
| return 0; | ||
| } | ||
| if (nid == DefaultDigestNid) { | ||
| type = reinterpret_cast<const DigestMethod*>(EVP_sha256_nid_postfix()); | ||
| } else { | ||
| NotImplemented_nid_no_patch("EVP_DigestSignInit default digest lookup for non-SHA256"); | ||
| } | ||
| } | ||
| auto* keyContext = static_cast<LibreSsl3PkeyContext*>(ctx->keyContext); | ||
| const auto* method = MethodOf(keyContext); | ||
| if (!method || FindNativePkeyMethod_nid_no_patch(method->keyType) != method) | ||
| NotImplemented_nid_no_patch((std::string(function) + " with a foreign EVP_PKEY_CTX").c_str()); | ||
|
|
||
| const bool hasSigCtxCustom = (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) != 0; | ||
| if (!hasSigCtxCustom && type == nullptr) { | ||
| type = DefaultDigestForKey(pkey, function); | ||
| if (!type) return 0; | ||
| } | ||
|
|
||
| if (method->signatureContextInitialize) NotImplemented_nid_no_patch("EVP_DigestSignInit with a signctx_init method"); | ||
| if (method->digestSignature) NotImplemented_nid_no_patch("EVP_DigestSignInit with a digestsign method"); | ||
| if (EVP_PKEY_sign_init_nid_postfix(static_cast<LibreSsl3PkeyContext*>(ctx->keyContext)) <= 0) | ||
| return 0; | ||
| if (verify) { | ||
| if (method->digestVerification) | ||
| NotImplemented_nid_no_patch("EVP_DigestVerifyInit with a digestverify method"); | ||
| if (EVP_PKEY_verify_init_nid_postfix(keyContext) <= 0) return 0; | ||
| } else { | ||
| if (method->signatureContextInitialize) | ||
| NotImplemented_nid_no_patch("EVP_DigestSignInit with a signctx_init method"); | ||
| if (method->digestSignature) | ||
| NotImplemented_nid_no_patch("EVP_DigestSignInit with a digestsign method"); | ||
| if (EVP_PKEY_sign_init_nid_postfix(keyContext) <= 0) return 0; | ||
| } | ||
|
|
||
| if (EVP_PKEY_CTX_ctrl_nid_postfix(static_cast<LibreSsl3PkeyContext*>(ctx->keyContext), -1, | ||
| LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlMessageDigest, 0, | ||
| if (EVP_PKEY_CTX_ctrl_nid_postfix(keyContext, -1, LibreSsl3PkeyOperationTypeSignature, | ||
| LibreSsl3PkeyControlMessageDigest, 0, | ||
| static_cast<void*>(const_cast<DigestMethod*>(type))) <= 0) | ||
| return 0; | ||
|
|
||
| if (pctx) *pctx = static_cast<LibreSsl3PkeyContext*>(ctx->keyContext); | ||
| if (pctx) *pctx = keyContext; | ||
|
|
||
| if (!hasSignCtxCustom) { | ||
| if (!DigestInitialize(ctx, type)) return 0; | ||
| } | ||
| if (!hasSigCtxCustom && !DigestInitialize(ctx, type)) return 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make digest initialization retryable after allocation failure. If SHA-1 digest-state allocation fails,
|
||
| return 1; | ||
| } | ||
|
|
||
|
|
@@ -1502,6 +1618,18 @@ int DigestFinal(LibreSsl3::DigestContext* ctx, unsigned char* md, unsigned int* | |
| return ret; | ||
| } | ||
|
|
||
| int FinalizeDigestCopy(LibreSsl3::DigestContext* ctx, unsigned char* digest, unsigned int& digestLength, | ||
| int copyFailureReturn) { | ||
| LibreSsl3::DigestContext copy = {}; | ||
| if (!DigestCopy(©, ctx)) { | ||
| if (copy.data) free_nid_postfix(copy.data); | ||
| return copyFailureReturn; | ||
| } | ||
| const int finalized = DigestFinal(©, digest, &digestLength); | ||
| if (copy.data) free_nid_postfix(copy.data); | ||
| return finalized; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| const LibreSsl3PkeyMethod* FindNativePkeyMethod_nid_no_patch(std::int32_t keyType) { | ||
|
|
@@ -1803,7 +1931,7 @@ int APS5_VABI EVP_DigestSignInit_nid_postfix(LibreSsl3::DigestContext* ctx, | |
| LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, | ||
| LibreSsl3EvpPkey* pkey) { | ||
| if (engine) NotImplemented_nid_no_patch("EVP_DigestSignInit with engine"); | ||
| return DigestSignInitialize(ctx, pctx, type, pkey); | ||
| return DoSigVerInitialize(ctx, pctx, type, pkey, false); | ||
| } | ||
|
|
||
| int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, | ||
|
|
@@ -1819,13 +1947,40 @@ int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, | |
| if (size < 0) return 0; | ||
| return EVP_PKEY_sign_nid_postfix(pctx, nullptr, siglen, nullptr, static_cast<std::size_t>(size)) > 0; | ||
| } | ||
| LibreSsl3::DigestContext copy = {}; | ||
| unsigned char digest[LibreSsl3MaximumDigestSize]; | ||
| unsigned char digest[LibreSsl3MaximumDigestSize] {}; | ||
| unsigned int digestLength = 0; | ||
| auto result = 0; | ||
| if (DigestCopy(©, ctx) && DigestFinal(©, digest, &digestLength)) | ||
| if (FinalizeDigestCopy(ctx, digest, digestLength, 0) > 0) | ||
| result = EVP_PKEY_sign_nid_postfix(pctx, sigret, siglen, digest, digestLength) > 0; | ||
| if (copy.data) free_nid_postfix(copy.data); | ||
| for (unsigned int i = 0; i < digestLength; ++i) digest[i] = 0; | ||
| return result; | ||
| } | ||
|
|
||
| int APS5_VABI EVP_DigestVerifyInit_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, | ||
| LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, | ||
| LibreSsl3EvpPkey* pkey) { | ||
| if (engine) NotImplemented_nid_no_patch("EVP_DigestVerifyInit with engine"); | ||
| return DoSigVerInitialize(ctx, pctx, type, pkey, true); | ||
| } | ||
|
|
||
| int APS5_VABI EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, | ||
| const unsigned char* sig, std::size_t siglen) { | ||
| if (!ctx) Reject("EVP_DigestVerifyFinal requires a digest context"); | ||
| auto* pctx = static_cast<LibreSsl3PkeyContext*>(ctx->keyContext); | ||
| if (!pctx) NotImplemented_nid_no_patch("EVP_DigestVerifyFinal without an EVP_PKEY_CTX"); | ||
| const auto* method = MethodOf(pctx); | ||
| if (!method || FindNativePkeyMethod_nid_no_patch(method->keyType) != method) | ||
| NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a foreign EVP_PKEY_CTX"); | ||
| if (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) | ||
| NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a custom signature context"); | ||
| if (method->digestVerification) | ||
| NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a digestverify method"); | ||
| unsigned char digest[LibreSsl3MaximumDigestSize] {}; | ||
| unsigned int digestLength = 0; | ||
| const int status = FinalizeDigestCopy(ctx, digest, digestLength, -1); | ||
| int result = status; | ||
| if (status > 0) | ||
| result = EVP_PKEY_verify_nid_postfix(pctx, sig, siglen, digest, digestLength); | ||
| for (unsigned int i = 0; i < digestLength; ++i) digest[i] = 0; | ||
| return result; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Publish
pctxonly after digest initialization succeeds.If SHA-1 digest-state allocation fails,
DigestInitializefreeskeyContextand returns0. Line 1548 has already stored that pointer in*pctx, so the output now points to freed storage. Move the assignment after successful initialization. This leaves the output unchanged on failure and still returns the live context on success. (raw.githubusercontent.com)