/* Derived from LibreSSL 4.0.0 (openbsd ce488b78e0bd994780569df718596b9776ff681a) src/lib/libcrypto/ec/ec_pmeth.c (ec_pmeth.c,v 1.22), * src/lib/libcrypto/ec/ec_ameth.c (ec_ameth.c,v 1.69), * src/lib/libcrypto/evp/pmeth_fn.c (pmeth_fn.c,v 1.11) * and src/lib/libcrypto/hmac/hm_ameth.c (hm_ameth.c,v 1.20) */ /* $OpenBSD: ec_pmeth.c,v 1.22 2024/08/26 22:01:28 op Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: ec_ameth.c,v 1.69 2024/08/29 16:58:19 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: rsa_pmeth.c,v 1.41 2024/08/26 22:01:28 op Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: pmeth_lib.c,v 1.41 2024/07/09 17:02:29 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: pmeth_fn.c,v 1.11 2024/04/12 09:41:39 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: m_sigver.c,v 1.27 2024/04/09 13:52:41 beck Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ /* ==================================================================== * Copyright (c) 2006,2007 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* $OpenBSD: p_sign.c,v 1.22 2024/04/09 13:52:41 beck Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* $OpenBSD: p_verify.c,v 1.21 2024/04/09 13:52:41 beck Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* $OpenBSD: hm_ameth.c,v 1.20 2024/01/04 17:01:26 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2007. */ /* ==================================================================== * Copyright (c) 2007 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #include "prx/libSceLibreSsl3/include/PkeyContext.hpp" #include #include #include #include #include #include #include #include #include #include "prx/libSceLibreSsl/include/Error.hpp" #include "prx/libSceLibreSsl3/include/Api.hpp" #include "prx/libSceLibreSsl3/include/PkeyMethod.hpp" #include "prx/libSceLibreSsl3/include/PkeyState.hpp" #include "prx/libSceLibreSsl3/include/RsaCrypto.hpp" #include "prx/libSceLibreSsl/include/Ec.hpp" #include "prx/libc/include/Api.hpp" #include "prx/libc/include/General.hpp" #include #include namespace { using LibreSsl3::DigestMethod; constexpr int EvpLibrary = 6; constexpr int RsaLibrary = 4; constexpr int EcLibrary = 16; constexpr int AnyFunction = 0xfff; constexpr int AllocationFailure = 65; constexpr int ReasonCommandNotSupported = 147; constexpr int ReasonNoOperationSet = 149; constexpr int ReasonInvalidOperation = 148; constexpr int ReasonOperationNotSupported = 150; constexpr int ReasonOperationNotInitialized = 151; constexpr int ReasonBufferTooSmall = 155; constexpr int ReasonUnsupportedAlgorithm = 156; constexpr int ReasonDifferentKeyTypes = 101; constexpr int ReasonDifferentParameters = 153; constexpr int ReasonMissingParameters = 103; constexpr int ReasonNoKeySet = 154; constexpr int ReasonKeysNotSet = 140; constexpr int ReasonKeySizeTooSmall = 120; constexpr int ReasonBadExponentValue = 101; constexpr int ReasonInvalidPaddingMode = 141; constexpr int ReasonIllegalPaddingMode = 144; constexpr int ReasonDigestNotAllowed = 145; constexpr int ReasonInvalidSaltLength = 146; constexpr int ReasonKeyTypeOperationNotSupported = 148; constexpr int ReasonInvalidMaskDigest = 156; constexpr int ReasonInvalidDigest = 157; constexpr int ReasonSaltLengthTooSmall = 164; constexpr int ReasonInvalidDigestLength = 143; constexpr int ReasonInputNotInitialized = 111; constexpr int ReasonNoDefaultDigest = 158; constexpr int Asn1MethodFlags = 4; constexpr int DefaultDigestNid = 672; constexpr int DefaultKeyGenerationBits = 2048; constexpr int NoSaltLengthLimit = -1; constexpr std::uint64_t DigestCleanedFlag = 2; constexpr std::uint64_t DigestReuseFlag = 4; constexpr std::uint64_t DigestKeepKeyContextFlag = 0x400; constexpr std::uint64_t DigestNoInitFlag = 0x100; constexpr int Md4Type = 257; constexpr int Md5Type = 4; constexpr int Md5Sha1Type = 114; constexpr int Ripemd160Type = 117; constexpr int Sha1Type = 64; constexpr int Sha224Type = 675; constexpr int Sha256Type = 672; constexpr int Sha384Type = 673; constexpr int Sha512Type = 674; constexpr int Sha512_224Type = 1029; constexpr int Sha512_256Type = 1030; constexpr int Sha3_224Type = 1031; constexpr int Sha3_256Type = 1032; constexpr int Sha3_384Type = 1033; constexpr int Sha3_512Type = 1034; constexpr int MaxDigestSize = 64; void QueueEvpError(int reason) { LibreSslPutError_nid_no_patch(EvpLibrary, AnyFunction, reason); } void QueueRsaError(int reason) { LibreSslPutError_nid_no_patch(RsaLibrary, AnyFunction, reason); } [[noreturn]] void Reject(const std::string& message) { throw std::invalid_argument("libSceLibreSsl3 EVP_PKEY_CTX: " + message); } const LibreSsl3PkeyMethod* MethodOf(const LibreSsl3PkeyContext* context) { return static_cast(context->method); } LibreSsl3RsaKeyContext* RsaContextData(const LibreSsl3PkeyContext* context) { auto* keyContext = static_cast(context->data); if (!keyContext) NotImplemented_nid_no_patch("EVP_PKEY_CTX operation without RSA context data"); return keyContext; } LibreSsl3Rsa* RsaKeyOf(const LibreSsl3PkeyContext* context) { const auto* key = static_cast(context->key); auto* rsa = key ? static_cast(key->key) : nullptr; if (!rsa) NotImplemented_nid_no_patch("EVP_PKEY_CTX operation without a native RSA key"); return rsa; } bool CheckPaddingDigest(const DigestMethod* digest, int padding) { if (!digest) return true; if (padding == LibreSsl3RsaPaddingNone) { QueueRsaError(ReasonInvalidPaddingMode); return false; } if (padding == LibreSsl3RsaPaddingX931) NotImplemented_nid_no_patch("check_padding_md for X9.31 padding"); switch (digest->type) { case Md4Type: case Md5Type: case Md5Sha1Type: case Ripemd160Type: case Sha1Type: case Sha224Type: case Sha256Type: case Sha384Type: case Sha512Type: case Sha512_224Type: case Sha512_256Type: case Sha3_224Type: case Sha3_256Type: case Sha3_384Type: case Sha3_512Type: return true; default: QueueRsaError(ReasonInvalidDigest); return false; } } bool SetupTemporaryBuffer(LibreSsl3RsaKeyContext* keyContext, const LibreSsl3Rsa* rsa) { if (keyContext->temporaryBuffer) return true; const int size = LibreSsl3::RsaByteCount_nid_no_patch(rsa); if (size <= 0) return false; auto* buffer = static_cast(calloc_nid_postfix(1, static_cast(size))); if (!buffer) { QueueRsaError(AllocationFailure); return false; } keyContext->temporaryBuffer = buffer; return true; } std::optional CheckAutomaticLength(LibreSsl3PkeyContext* context, const LibreSsl3PkeyMethod* method, const unsigned char* output, std::size_t& outputLength) { if ((method->flags & LibreSsl3PkeyMethodFlagAutomaticArgumentLength) == 0) return std::nullopt; const auto keySize = static_cast(EVP_PKEY_size_nid_postfix(static_cast(context->key))); if (!output) { outputLength = keySize; return 1; } if (outputLength < keySize) { QueueEvpError(ReasonBufferTooSmall); return 0; } return std::nullopt; } template int WithOutputLength(std::size_t* outputLength, const char* function, TOperation operation) { if (!outputLength) Reject(std::string(function) + " without an output length"); std::size_t length = 0; int saved = errno; const bool readable = GuestCopyIn_nid_no_patch(&length, outputLength, sizeof(length)); errno = saved; if (!readable) Reject(std::string(function) + " with an unreadable output length"); const std::size_t incoming = length; const int result = operation(length); if (length == incoming) return result; saved = errno; const bool writable = GuestCopyOut_nid_no_patch(outputLength, &length, sizeof(length)); errno = saved; if (!writable) Reject(std::string(function) + " with an unwritable output length"); return result; } int APS5_VABI RsaSign(LibreSsl3PkeyContext* context, unsigned char* signature, std::size_t* signatureLength, const unsigned char* content, std::size_t contentLength) { auto* keyContext = RsaContextData(context); auto* rsa = RsaKeyOf(context); if (keyContext->digest) { if (contentLength != static_cast(keyContext->digest->digestSize)) { QueueRsaError(ReasonInvalidDigestLength); return -1; } if (keyContext->padding == LibreSsl3RsaPaddingPkcs1) { unsigned int sltmp = 0; const int ret = LibreSsl3::RsaSign_nid_no_patch(keyContext->digest->type, content, static_cast(contentLength), signature, &sltmp, rsa); if (ret <= 0) return ret; *signatureLength = sltmp; return 1; } else if (keyContext->padding == LibreSsl3RsaPaddingPss) { if (!SetupTemporaryBuffer(keyContext, rsa)) return -1; const auto* maskDigest = keyContext->maskDigest ? keyContext->maskDigest : keyContext->digest; if (!LibreSsl3::RsaPaddingAddPkcs1PssMgf1_nid_no_patch(rsa, keyContext->temporaryBuffer, content, keyContext->digest, maskDigest, keyContext->saltLength)) return -1; const int ret = LibreSsl3::RsaPrivateEncrypt_nid_no_patch(LibreSsl3::RsaByteCount_nid_no_patch(rsa), keyContext->temporaryBuffer, signature, rsa, LibreSsl3RsaPaddingNone); if (ret < 0) return ret; *signatureLength = ret; return 1; } else if (keyContext->padding == LibreSsl3RsaPaddingX931) { NotImplemented_nid_no_patch("RSA X9.31 signing"); } else { return -1; } } else { const int ret = LibreSsl3::RsaPrivateEncrypt_nid_no_patch(static_cast(contentLength), content, signature, rsa, keyContext->padding); if (ret < 0) return ret; *signatureLength = ret; return 1; } } int APS5_VABI PkeyRsaVerify(LibreSsl3PkeyContext* context, const unsigned char* signature, std::size_t signatureLength, const unsigned char* content, std::size_t contentLength) { auto* keyContext = RsaContextData(context); auto* rsa = RsaKeyOf(context); std::size_t rslen = 0; if (keyContext->digest) { if (keyContext->padding == LibreSsl3RsaPaddingPkcs1) { return LibreSsl3::RsaVerify_nid_no_patch(keyContext->digest->type, content, static_cast(contentLength), signature, static_cast(signatureLength), rsa); } else if (contentLength != static_cast(keyContext->digest->digestSize)) { QueueRsaError(ReasonInvalidDigestLength); return -1; } else if (keyContext->padding == LibreSsl3RsaPaddingPss) { if (!SetupTemporaryBuffer(keyContext, rsa)) return -1; const int ret = LibreSsl3::RsaPublicDecrypt_nid_no_patch(static_cast(signatureLength), signature, keyContext->temporaryBuffer, rsa, LibreSsl3RsaPaddingNone); if (ret <= 0) return 0; const auto* maskDigest = keyContext->maskDigest ? keyContext->maskDigest : keyContext->digest; const int verified = LibreSsl3::RsaVerifyPkcs1PssMgf1_nid_no_patch(rsa, content, keyContext->digest, maskDigest, keyContext->temporaryBuffer, keyContext->saltLength); if (verified <= 0) return 0; return 1; } else if (keyContext->padding == LibreSsl3RsaPaddingX931) { NotImplemented_nid_no_patch("RSA X9.31 verification"); } else { return -1; } } else { if (!SetupTemporaryBuffer(keyContext, rsa)) return -1; const int ret = LibreSsl3::RsaPublicDecrypt_nid_no_patch(static_cast(signatureLength), signature, keyContext->temporaryBuffer, rsa, keyContext->padding); if (ret <= 0) return 0; rslen = ret; } if (rslen != contentLength) return 0; if (CRYPTO_memcmp_nid_postfix(content, keyContext->temporaryBuffer, rslen)) return 0; return 1; } int APS5_VABI RsaPssInitialize(LibreSsl3PkeyContext* context) { if (MethodOf(context)->keyType != LibreSsl3PkeyRsaPss) return 0; if (RsaKeyOf(context)->pss) NotImplemented_nid_no_patch("EVP_PKEY_CTX signature with RSA-PSS key restrictions"); return 1; } int APS5_VABI RsaContextInitialize(LibreSsl3PkeyContext* context) { auto* keyContext = static_cast(calloc_nid_postfix(1, sizeof(LibreSsl3RsaKeyContext))); if (!keyContext) return 0; keyContext->keyBits = DefaultKeyGenerationBits; keyContext->padding = MethodOf(context)->keyType == LibreSsl3PkeyRsaPss ? LibreSsl3RsaPaddingPss : LibreSsl3RsaPaddingPkcs1; keyContext->saltLength = LibreSsl3RsaPssSaltLengthAuto; keyContext->minimumSaltLength = NoSaltLengthLimit; context->data = keyContext; context->generationInformation = keyContext->generationTemporary; context->generationInformationCount = 2; return 1; } int APS5_VABI PkeyRsaContextCopy(LibreSsl3PkeyContext* destination, LibreSsl3PkeyContext* source) { if (RsaContextInitialize(destination) <= 0) return 0; const auto* from = RsaContextData(source); auto* to = RsaContextData(destination); to->keyBits = from->keyBits; if (from->publicExponent) { to->publicExponent = LibreSsl3::DuplicateRsaBignum_nid_no_patch(from->publicExponent); if (!to->publicExponent) return 0; } to->padding = from->padding; to->digest = from->digest; to->maskDigest = from->maskDigest; if (from->oaepLabel) { to->oaepLabel = static_cast(calloc_nid_postfix(1, from->oaepLabelLength)); if (!to->oaepLabel) return 0; std::memcpy(to->oaepLabel, from->oaepLabel, from->oaepLabelLength); to->oaepLabelLength = from->oaepLabelLength; } return 1; } void APS5_VABI RsaContextClear(LibreSsl3PkeyContext* context) { auto* keyContext = static_cast(context->data); if (!keyContext) return; if (keyContext->publicExponent) BN_clear_free_nid_postfix(keyContext->publicExponent); if (keyContext->temporaryBuffer) free_nid_postfix(keyContext->temporaryBuffer); if (keyContext->oaepLabel) free_nid_postfix(keyContext->oaepLabel); free_nid_postfix(keyContext); context->data = nullptr; } int APS5_VABI RsaEncryption(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength, const unsigned char* input, std::size_t inputLength) { auto* keyContext = RsaContextData(context); auto* rsa = RsaKeyOf(context); if (keyContext->padding == LibreSsl3RsaPaddingOaep) { const int keyLength = LibreSsl3::RsaByteCount_nid_no_patch(rsa); if (!SetupTemporaryBuffer(keyContext, rsa)) return -1; if (!LibreSsl3::RsaPaddingAddPkcs1OaepMgf1_nid_no_patch(keyContext->temporaryBuffer, keyLength, input, static_cast(inputLength), keyContext->oaepLabel, static_cast(keyContext->oaepLabelLength), keyContext->digest, keyContext->maskDigest)) return -1; const int result = LibreSsl3::RsaPublicEncrypt_nid_no_patch(keyLength, keyContext->temporaryBuffer, output, rsa, LibreSsl3RsaPaddingNone); if (result < 0) return result; *outputLength = static_cast(result); return 1; } const int result = LibreSsl3::RsaPublicEncrypt_nid_no_patch(static_cast(inputLength), input, output, rsa, keyContext->padding); if (result < 0) return result; *outputLength = static_cast(result); return 1; } int APS5_VABI RsaDecryption(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength, const unsigned char* input, std::size_t inputLength) { auto* keyContext = RsaContextData(context); auto* rsa = RsaKeyOf(context); if (keyContext->padding == LibreSsl3RsaPaddingOaep) { if (!SetupTemporaryBuffer(keyContext, rsa)) return -1; const int decrypted = LibreSsl3::RsaPrivateDecrypt_nid_no_patch(static_cast(inputLength), input, keyContext->temporaryBuffer, rsa, LibreSsl3RsaPaddingNone); if (decrypted <= 0) return decrypted; const int recovered = LibreSsl3::RsaPaddingCheckPkcs1OaepMgf1_nid_no_patch(output, decrypted, keyContext->temporaryBuffer, decrypted, decrypted, keyContext->oaepLabel, static_cast(keyContext->oaepLabelLength), keyContext->digest, keyContext->maskDigest); if (recovered < 0) return recovered; *outputLength = static_cast(recovered); return 1; } const int recovered = LibreSsl3::RsaPrivateDecrypt_nid_no_patch(static_cast(inputLength), input, output, rsa, keyContext->padding); if (recovered < 0) return recovered; *outputLength = static_cast(recovered); return 1; } int APS5_VABI PkeyRsaControl(LibreSsl3PkeyContext* context, int type, int first, void* second) { auto* keyContext = RsaContextData(context); const auto* method = MethodOf(context); switch (type) { case LibreSsl3PkeyControlRsaPadding: if (first >= LibreSsl3RsaPaddingPkcs1 && first <= LibreSsl3RsaPaddingPss) { if (!CheckPaddingDigest(keyContext->digest, first)) return 0; if (first == LibreSsl3RsaPaddingPss) { if ((context->operation & (LibreSsl3PkeyOperationSign | LibreSsl3PkeyOperationVerify)) == 0) goto badPadding; if (!keyContext->digest) keyContext->digest = EVP_sha1_nid_postfix(); } else if (method->keyType == LibreSsl3PkeyRsaPss) { goto badPadding; } if (first == LibreSsl3RsaPaddingOaep) { if ((context->operation & LibreSsl3PkeyOperationTypeCrypt) == 0) goto badPadding; if (!keyContext->digest) keyContext->digest = EVP_sha1_nid_postfix(); } keyContext->padding = first; return 1; } badPadding: QueueRsaError(ReasonIllegalPaddingMode); return -2; case LibreSsl3PkeyControlGetRsaPadding: if (!second) Reject("EVP_PKEY_CTX_ctrl get RSA padding without an output"); *static_cast(second) = keyContext->padding; return 1; case LibreSsl3PkeyControlRsaPssSaltLength: case LibreSsl3PkeyControlGetRsaPssSaltLength: if (keyContext->padding != LibreSsl3RsaPaddingPss) { QueueRsaError(ReasonInvalidSaltLength); return -2; } if (type == LibreSsl3PkeyControlGetRsaPssSaltLength) { if (!second) Reject("EVP_PKEY_CTX_ctrl get RSA PSS salt length without an output"); *static_cast(second) = keyContext->saltLength; } else { if (first < LibreSsl3RsaPssSaltLengthMaximum) return -2; if (keyContext->minimumSaltLength != NoSaltLengthLimit) { if (first == LibreSsl3RsaPssSaltLengthAuto && context->operation == LibreSsl3PkeyOperationVerify) { QueueRsaError(ReasonInvalidSaltLength); return -2; } const int digestSize = keyContext->digest ? keyContext->digest->digestSize : 0; if ((first == LibreSsl3RsaPssSaltLengthDigest && keyContext->minimumSaltLength > digestSize) || (first >= 0 && first < keyContext->minimumSaltLength)) { QueueRsaError(ReasonSaltLengthTooSmall); return 0; } } keyContext->saltLength = first; } return 1; case LibreSsl3PkeyControlRsaKeyGenerationBits: if (first < LibreSsl3RsaMinimumModulusBits) { QueueRsaError(ReasonKeySizeTooSmall); return -2; } keyContext->keyBits = first; return 1; case LibreSsl3PkeyControlRsaKeyGenerationPublicExponent: { auto* exponent = static_cast(second); const bool odd = exponent != nullptr && exponent->top > 0 && exponent->data != nullptr && (exponent->data[0] & 1U) != 0; const bool one = exponent != nullptr && exponent->top == 1 && exponent->data != nullptr && exponent->data[0] == 1; if (!odd || one) { QueueRsaError(ReasonBadExponentValue); return -2; } if (keyContext->publicExponent) BN_clear_free_nid_postfix(keyContext->publicExponent); keyContext->publicExponent = exponent; return 1; } case LibreSsl3PkeyControlRsaOaepDigest: case LibreSsl3PkeyControlGetRsaOaepDigest: if (keyContext->padding != LibreSsl3RsaPaddingOaep) { QueueRsaError(ReasonInvalidPaddingMode); return -2; } if (type == LibreSsl3PkeyControlGetRsaOaepDigest) { if (!second) Reject("EVP_PKEY_CTX_ctrl get OAEP digest without an output"); *static_cast(second) = keyContext->digest; } else { keyContext->digest = static_cast(second); } return 1; case LibreSsl3PkeyControlMessageDigest: if (!CheckPaddingDigest(static_cast(second), keyContext->padding)) return 0; if (keyContext->minimumSaltLength != NoSaltLengthLimit) { const int restricted = keyContext->digest ? keyContext->digest->type : -1; const int requested = second ? static_cast(second)->type : -1; if (restricted == requested) return 1; QueueRsaError(ReasonDigestNotAllowed); return 0; } keyContext->digest = static_cast(second); return 1; case LibreSsl3PkeyControlGetMessageDigest: if (!second) Reject("EVP_PKEY_CTX_ctrl get digest without an output"); *static_cast(second) = keyContext->digest; return 1; case LibreSsl3PkeyControlRsaMaskDigest: case LibreSsl3PkeyControlGetRsaMaskDigest: if (keyContext->padding != LibreSsl3RsaPaddingPss && keyContext->padding != LibreSsl3RsaPaddingOaep) { QueueRsaError(ReasonInvalidMaskDigest); return -2; } if (type == LibreSsl3PkeyControlGetRsaMaskDigest) { if (!second) Reject("EVP_PKEY_CTX_ctrl get MGF1 digest without an output"); *static_cast(second) = keyContext->maskDigest ? keyContext->maskDigest : keyContext->digest; } else { if (keyContext->minimumSaltLength != NoSaltLengthLimit) { const int restricted = keyContext->maskDigest ? keyContext->maskDigest->type : -1; const int requested = second ? static_cast(second)->type : -1; if (restricted == requested) return 1; QueueRsaError(ReasonInvalidMaskDigest); return 0; } keyContext->maskDigest = static_cast(second); } return 1; case LibreSsl3PkeyControlRsaOaepLabel: if (keyContext->padding != LibreSsl3RsaPaddingOaep) { QueueRsaError(ReasonInvalidPaddingMode); return -2; } if (keyContext->oaepLabel) free_nid_postfix(keyContext->oaepLabel); if (second && first > 0) { keyContext->oaepLabel = static_cast(second); keyContext->oaepLabelLength = static_cast(first); } else { keyContext->oaepLabel = nullptr; keyContext->oaepLabelLength = 0; } return 1; case LibreSsl3PkeyControlGetRsaOaepLabel: if (keyContext->padding != LibreSsl3RsaPaddingOaep) { QueueRsaError(ReasonInvalidPaddingMode); return -2; } if (!second) Reject("EVP_PKEY_CTX_ctrl get OAEP label without an output"); *static_cast(second) = keyContext->oaepLabel; return static_cast(keyContext->oaepLabelLength); case LibreSsl3PkeyControlDigestInit: case LibreSsl3PkeyControlPkcs7Sign: case LibreSsl3PkeyControlCmsSign: return 1; case LibreSsl3PkeyControlPkcs7Encrypt: case LibreSsl3PkeyControlPkcs7Decrypt: case LibreSsl3PkeyControlCmsDecrypt: case LibreSsl3PkeyControlCmsEncrypt: if (method->keyType != LibreSsl3PkeyRsaPss) return 1; QueueRsaError(ReasonKeyTypeOperationNotSupported); return -2; case LibreSsl3PkeyControlPeerKey: QueueRsaError(ReasonKeyTypeOperationNotSupported); return -2; default: return -2; } } int APS5_VABI RsaKeySize(const LibreSsl3EvpPkey* key) { return LibreSsl3::RsaByteCount_nid_no_patch(static_cast(key->key)); } int APS5_VABI RsaKeyBits(const LibreSsl3EvpPkey* key) { return LibreSsl3::RsaBitCount_nid_no_patch(static_cast(key->key)); } int APS5_VABI RsaKeySecurityBits(const LibreSsl3EvpPkey* key) { return LibreSsl3::RsaSecurityBits_nid_no_patch(static_cast(key->key)); } int APS5_VABI RsaPublicKeyCompare(const LibreSsl3EvpPkey* first, const LibreSsl3EvpPkey* second) { return LibreSsl3::RsaPublicCompare_nid_no_patch(static_cast(first->key), static_cast(second->key)); } void APS5_VABI RsaKeyFree(LibreSsl3EvpPkey* key) { LibreSsl3::FreeRsa_nid_no_patch(static_cast(key->key)); } int APS5_VABI RsaKeyControl(LibreSsl3EvpPkey* key, int operation, long first, void* second) { (void) first; if (operation != LibreSsl3Asn1PkeyControlDefaultDigestNid) return -2; const auto* rsa = static_cast(key->key); if (!rsa) NotImplemented_nid_no_patch("EVP_PKEY_asn1 method control without a native RSA key"); if (rsa->pss) NotImplemented_nid_no_patch("EVP_PKEY_asn1 method control of a key with PSS restrictions"); if (!second) Reject("EVP_PKEY_asn1 method control without a digest identifier"); *static_cast(second) = DefaultDigestNid; return 1; } const LibreSsl3PkeyMethod RsaMethod { LibreSsl3PkeyRsa, LibreSsl3PkeyMethodFlagAutomaticArgumentLength, &RsaContextInitialize, &PkeyRsaContextCopy, &RsaContextClear, nullptr, nullptr, nullptr, &RsaSign, nullptr, &PkeyRsaVerify, nullptr, nullptr, nullptr, &RsaEncryption, &RsaDecryption, nullptr, nullptr, &PkeyRsaControl, nullptr, nullptr, nullptr, }; const LibreSsl3PkeyMethod RsaPssMethod { LibreSsl3PkeyRsaPss, LibreSsl3PkeyMethodFlagAutomaticArgumentLength, &RsaContextInitialize, &PkeyRsaContextCopy, &RsaContextClear, nullptr, nullptr, &RsaPssInitialize, &RsaSign, &RsaPssInitialize, &PkeyRsaVerify, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &PkeyRsaControl, nullptr, nullptr, nullptr, }; int APS5_VABI EcContextInitialize(LibreSsl3PkeyContext*) { return 1; } void APS5_VABI EcContextClear(LibreSsl3PkeyContext*) {} int APS5_VABI EcControl(LibreSsl3PkeyContext*, int command, int, void*) { if (command == LibreSsl3PkeyControlPeerKey) return 1; return -2; } int APS5_VABI EcDerive(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength) { auto* key = static_cast(context->key); auto* peer = static_cast(context->peerKey); if (!key || !peer) { LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonKeysNotSet); return 0; } auto* ecKey = static_cast(key->key); auto* peerEcKey = static_cast(peer->key); if (!ecKey || !peerEcKey || !ecKey->group || !peerEcKey->group) { LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonKeysNotSet); return 0; } if (!output) { if (!outputLength) return 0; const int degree = EC_GROUP_get_degree_nid_postfix(ecKey->group); if (degree <= 0) return 0; *outputLength = static_cast((degree + 7) / 8); return 1; } if (!outputLength) return 0; if (!peerEcKey->publicKey) { LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonKeysNotSet); return 0; } if (!ecKey->privateKey) { LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonKeysNotSet); return 0; } const int degree = EC_GROUP_get_degree_nid_postfix(ecKey->group); if (degree <= 0) return 0; const auto fieldSize = static_cast((degree + 7) / 8); std::array secret {}; if (fieldSize > secret.size()) return 0; const int result = ECDH_compute_key_nid_postfix(secret.data(), fieldSize, peerEcKey->publicKey, ecKey, nullptr); if (result <= 0) return 0; const auto copied = std::min(*outputLength, static_cast(result)); std::copy_n(secret.data(), copied, output); *outputLength = copied; std::fill(secret.begin(), secret.end(), 0); return 1; } const LibreSsl3PkeyMethod EcMethod { LibreSsl3PkeyEc, 0, &EcContextInitialize, nullptr, &EcContextClear, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &EcContextInitialize, &EcDerive, &EcControl, nullptr, nullptr, nullptr, }; const LibreSsl3PkeyAsn1Method RsaAsn1Method { &RsaAsn1Method, LibreSsl3PkeyRsa, Asn1MethodFlags, "RSA", "OpenSSL RSA method", nullptr, nullptr, &RsaPublicKeyCompare, nullptr, nullptr, nullptr, nullptr, &RsaKeySize, &RsaKeyBits, &RsaKeySecurityBits, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &RsaKeyFree, &RsaKeyControl, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, }; const LibreSsl3PkeyAsn1Method RsaPssAsn1Method { &RsaPssAsn1Method, LibreSsl3PkeyRsaPss, Asn1MethodFlags, "RSA-PSS", "OpenSSL RSA-PSS method", nullptr, nullptr, &RsaPublicKeyCompare, nullptr, nullptr, nullptr, nullptr, &RsaKeySize, &RsaKeyBits, &RsaKeySecurityBits, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &RsaKeyFree, &RsaKeyControl, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, }; int APS5_VABI HmacCompare_nid_no_patch(const LibreSsl3EvpPkey* first, const LibreSsl3EvpPkey* second) { const auto* firstOctets = static_cast(first->key); const auto* secondOctets = static_cast(second->key); return ASN1_OCTET_STRING_cmp_nid_postfix(firstOctets, secondOctets) == 0; } int APS5_VABI HmacSize_nid_no_patch(const LibreSsl3EvpPkey*) { return MaxDigestSize; } void APS5_VABI HmacFree_nid_no_patch(LibreSsl3EvpPkey* key) { ASN1_OCTET_STRING_free_nid_postfix(static_cast(key->key)); } const LibreSsl3PkeyAsn1Method HmacAsn1Method { &HmacAsn1Method, LibreSsl3PkeyHmac, 0, "HMAC", "OpenSSL HMAC method", nullptr, nullptr, &HmacCompare_nid_no_patch, nullptr, nullptr, nullptr, nullptr, &HmacSize_nid_no_patch, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &HmacFree_nid_no_patch, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, }; int DigestInitialize(LibreSsl3::DigestContext* context, const DigestMethod* type) { context->flags &= ~DigestCleanedFlag; if (context->digest != type) { if (context->digest && context->digest->contextSize && context->data && !(context->flags & DigestReuseFlag)) { if (context->digest->contextSize > 0) { for (int i = 0; i < context->digest->contextSize; ++i) static_cast(context->data)[i] = 0; } free_nid_postfix(context->data); context->data = nullptr; } context->digest = type; if (!(context->flags & DigestNoInitFlag) && type->contextSize) { context->update = type->update; context->data = calloc_nid_postfix(1, static_cast(type->contextSize)); if (!context->data) { EVP_PKEY_CTX_free_nid_postfix(static_cast(context->keyContext)); context->keyContext = nullptr; QueueEvpError(AllocationFailure); return 0; } } } if (context->keyContext) { const int r = EVP_PKEY_CTX_ctrl_nid_postfix(static_cast(context->keyContext), -1, LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlDigestInit, 0, context); if (r <= 0 && r != -2) return 0; } if (context->flags & DigestNoInitFlag) return 1; return type->init(context); } int DigestSignInitialize(LibreSsl3::DigestContext* ctx, LibreSsl3PkeyContext** pctx, const DigestMethod* type, LibreSsl3EvpPkey* pkey) { if (!ctx->keyContext) ctx->keyContext = EVP_PKEY_CTX_new_nid_postfix(pkey, nullptr); if (!ctx->keyContext) return 0; const auto* method = MethodOf(static_cast(ctx->keyContext)); const bool hasSignCtxCustom = (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) != 0; if (!hasSignCtxCustom) { if (type == nullptr) { int nid = 0; const auto* keyAsn1 = static_cast(pkey->method); if (!keyAsn1 || keyAsn1->keyControl(pkey, LibreSsl3Asn1PkeyControlDefaultDigestNid, 0, &nid) <= 0) { QueueEvpError(ReasonNoDefaultDigest); return 0; } if (nid == DefaultDigestNid) { type = reinterpret_cast(EVP_sha256_nid_postfix()); } else { NotImplemented_nid_no_patch("EVP_DigestSignInit default digest lookup for non-SHA256"); } } } 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(ctx->keyContext)) <= 0) return 0; if (EVP_PKEY_CTX_ctrl_nid_postfix(static_cast(ctx->keyContext), -1, LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlMessageDigest, 0, static_cast(const_cast(type))) <= 0) return 0; if (pctx) *pctx = static_cast(ctx->keyContext); if (!hasSignCtxCustom) { if (!DigestInitialize(ctx, type)) return 0; } return 1; } int DigestCopy(LibreSsl3::DigestContext* out, const LibreSsl3::DigestContext* in) { if (!in || !in->digest) { QueueEvpError(ReasonInputNotInitialized); return 0; } unsigned char* tmp_buf; if (out->digest == in->digest) { tmp_buf = static_cast(out->data); out->flags |= DigestReuseFlag; } else { tmp_buf = nullptr; } if (out->digest && out->digest->cleanup && !(out->flags & DigestCleanedFlag)) out->digest->cleanup(out); if (out->digest && out->digest->contextSize && out->data && !(out->flags & DigestReuseFlag)) { if (out->digest->contextSize > 0) { for (int i = 0; i < out->digest->contextSize; ++i) static_cast(out->data)[i] = 0; } free_nid_postfix(out->data); } std::memcpy(out, in, sizeof(*in)); out->data = nullptr; out->keyContext = nullptr; out->flags &= ~DigestKeepKeyContextFlag; if (in->data && out->digest->contextSize) { if (tmp_buf) { out->data = tmp_buf; } else { out->data = calloc_nid_postfix(1, static_cast(out->digest->contextSize)); if (!out->data) { QueueEvpError(AllocationFailure); return 0; } } std::memcpy(out->data, in->data, static_cast(out->digest->contextSize)); } out->update = in->update; if (out->digest->copy) { return out->digest->copy(out, in); } return 1; } int DigestFinal(LibreSsl3::DigestContext* ctx, unsigned char* md, unsigned int* mdlen) { if (!ctx->digest) return 0; if (!ctx->digest->finalize) return 0; const int ret = ctx->digest->finalize(ctx, md); if (mdlen) *mdlen = ctx->digest->digestSize; if (ctx->digest->cleanup) { ctx->digest->cleanup(ctx); ctx->flags |= DigestCleanedFlag; } if (ctx->data && ctx->digest->contextSize) { for (int i = 0; i < ctx->digest->contextSize; ++i) static_cast(ctx->data)[i] = 0; } return ret; } } // namespace const LibreSsl3PkeyMethod* FindNativePkeyMethod_nid_no_patch(std::int32_t keyType) { if (keyType == LibreSsl3PkeyRsa) return &RsaMethod; if (keyType == LibreSsl3PkeyRsaPss) return &RsaPssMethod; if (keyType == LibreSsl3PkeyEc) return &EcMethod; return nullptr; } const LibreSsl3PkeyAsn1Method* FindNativePkeyAsn1Method_nid_no_patch(std::int32_t keyType) { if (keyType == LibreSsl3PkeyRsa) return &RsaAsn1Method; if (keyType == LibreSsl3PkeyRsaPss) return &RsaPssAsn1Method; if (keyType == LibreSsl3PkeyHmac) return &HmacAsn1Method; return nullptr; } bool IsNativePkeyMethod_nid_no_patch(const void* method) { return method == &RsaAsn1Method || method == &RsaPssAsn1Method || method == &HmacAsn1Method; } namespace { constexpr std::int32_t PkeyCmac = 894; constexpr std::int32_t PkeyDh = 28; constexpr std::int32_t PkeyDsa = 116; constexpr std::int32_t PkeyEd25519 = 952; constexpr std::int32_t PkeyHkdf = 1022; constexpr std::int32_t PkeyTls1Prf = 1055; constexpr std::int32_t PkeyX25519 = 950; bool IsLibreSslOnlyPkeyMethod(int keyType) { for (const auto supported : {PkeyCmac, PkeyDh, PkeyDsa, PkeyEd25519, PkeyHkdf, LibreSsl3PkeyHmac, PkeyTls1Prf, PkeyX25519}) { if (keyType == supported) return true; } return false; } LibreSsl3PkeyContext* NewPkeyContext(LibreSsl3EvpPkey* key, const LibreSsl3PkeyMethod* method) { auto* context = static_cast(calloc_nid_postfix(1, sizeof(LibreSsl3PkeyContext))); if (!context) { QueueEvpError(AllocationFailure); return nullptr; } context->method = method; context->operation = LibreSsl3PkeyOperationUndefined; if ((context->key = key) != nullptr) EVP_PKEY_up_ref_nid_postfix(key); if (method->initialize != nullptr && method->initialize(context) <= 0) { EVP_PKEY_CTX_free_nid_postfix(context); return nullptr; } return context; } } // namespace extern "C" { LibreSsl3PkeyContext* APS5_VABI EVP_PKEY_CTX_new_nid_postfix(LibreSsl3EvpPkey* key, void* engine) { (void) engine; if (!key || (!key->method && key->type != LibreSsl3PkeyEc)) return nullptr; const auto* asn1Method = static_cast(key->method); const auto* method = key->type == LibreSsl3PkeyEc ? &EcMethod : FindNativePkeyMethod_nid_no_patch(asn1Method->keyType); if (!method) { QueueEvpError(ReasonUnsupportedAlgorithm); return nullptr; } return NewPkeyContext(key, method); } LibreSsl3PkeyContext* APS5_VABI EVP_PKEY_CTX_new_id_nid_postfix(int keyType, void* engine) { (void) engine; if (keyType == -1) return nullptr; const auto* method = FindNativePkeyMethod_nid_no_patch(keyType); if (!method) { if (IsLibreSslOnlyPkeyMethod(keyType)) NotImplemented_nid_no_patch(__func__); QueueEvpError(ReasonUnsupportedAlgorithm); return nullptr; } return NewPkeyContext(nullptr, method); } int APS5_VABI EVP_PKEY_derive_init_nid_postfix(LibreSsl3PkeyContext* context) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->derivation) { QueueEvpError(ReasonOperationNotSupported); return -2; } context->operation = LibreSsl3PkeyOperationDerive; if (!method->derivationInitialize) return 1; const int result = method->derivationInitialize(context); if (result <= 0) context->operation = LibreSsl3PkeyOperationUndefined; return result; } int APS5_VABI EVP_PKEY_derive_set_peer_nid_postfix(LibreSsl3PkeyContext* context, LibreSsl3EvpPkey* peer) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || (!method->derivation && !method->encryption && !method->decryption) || !method->control) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationDerive && context->operation != LibreSsl3PkeyOperationEncrypt && context->operation != LibreSsl3PkeyOperationDecrypt) { QueueEvpError(ReasonOperationNotInitialized); return -1; } int result = method->control(context, LibreSsl3PkeyControlPeerKey, 0, peer); if (result <= 0) return result; if (!context->key) { QueueEvpError(ReasonNoKeySet); return -1; } if (!peer || static_cast(context->key)->type != peer->type) { QueueEvpError(ReasonDifferentKeyTypes); return -1; } if (peer->type == LibreSsl3PkeyEc) { const auto* ownKey = static_cast(static_cast(context->key)->key); const auto* peerKey = static_cast(peer->key); if (ownKey && peerKey && ownKey->group && peerKey->group && ownKey->group->curve.nid != peerKey->group->curve.nid) { QueueEvpError(ReasonDifferentParameters); return -1; } } EVP_PKEY_free_nid_disambig1_nid_postfix(static_cast(context->peerKey)); context->peerKey = peer; EVP_PKEY_up_ref_nid_postfix(peer); return 1; } int APS5_VABI EVP_PKEY_derive_nid_postfix(LibreSsl3PkeyContext* context, unsigned char* key, std::size_t* keyLength) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->derivation) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationDerive) { QueueEvpError(ReasonOperationNotInitialized); return -1; } return method->derivation(context, key, keyLength); } void APS5_VABI EVP_PKEY_CTX_free_nid_postfix(LibreSsl3PkeyContext* context) { if (!context) return; const auto* method = MethodOf(context); if (method != nullptr && method->clear != nullptr) method->clear(context); EVP_PKEY_free_nid_disambig1_nid_postfix(static_cast(context->key)); EVP_PKEY_free_nid_disambig1_nid_postfix(static_cast(context->peerKey)); free_nid_postfix(context); } int APS5_VABI EVP_PKEY_CTX_ctrl_nid_postfix(LibreSsl3PkeyContext* context, int keyType, int operationType, int command, int first, void* second) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->control) { QueueEvpError(ReasonCommandNotSupported); return -2; } if (keyType != -1 && method->keyType != keyType) return -1; if (context->operation == LibreSsl3PkeyOperationUndefined) { QueueEvpError(ReasonNoOperationSet); return -1; } if (operationType != -1 && (context->operation & operationType) == 0) { QueueEvpError(ReasonInvalidOperation); return -1; } const int result = method->control(context, command, first, second); if (result == -2) QueueEvpError(ReasonCommandNotSupported); return result; } int APS5_VABI EVP_PKEY_encrypt_init_nid_postfix(LibreSsl3PkeyContext* context) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->encryption) { QueueEvpError(ReasonOperationNotSupported); return -2; } context->operation = LibreSsl3PkeyOperationEncrypt; return 1; } int APS5_VABI EVP_PKEY_encrypt_nid_postfix(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength, const unsigned char* input, std::size_t inputLength) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->encryption) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationEncrypt) { QueueEvpError(ReasonOperationNotInitialized); return -1; } return WithOutputLength(outputLength, "EVP_PKEY_encrypt", [&](std::size_t& length) { if (const auto result = CheckAutomaticLength(context, method, output, length)) return *result; return method->encryption(context, output, &length, input, inputLength); }); } int APS5_VABI EVP_PKEY_decrypt_init_nid_postfix(LibreSsl3PkeyContext* context) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->decryption) { QueueEvpError(ReasonOperationNotSupported); return -2; } context->operation = LibreSsl3PkeyOperationDecrypt; return 1; } int APS5_VABI EVP_PKEY_decrypt_nid_postfix(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength, const unsigned char* input, std::size_t inputLength) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->decryption) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationDecrypt) { QueueEvpError(ReasonOperationNotInitialized); return -1; } return WithOutputLength(outputLength, "EVP_PKEY_decrypt", [&](std::size_t& length) { if (const auto result = CheckAutomaticLength(context, method, output, length)) return *result; return method->decryption(context, output, &length, input, inputLength); }); } int APS5_VABI EVP_PKEY_sign_init_nid_postfix(LibreSsl3PkeyContext* context) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->signature) { QueueEvpError(ReasonOperationNotSupported); return -2; } context->operation = LibreSsl3PkeyOperationSign; if (!method->signatureInitialize) return 1; const int ret = method->signatureInitialize(context); if (ret <= 0) context->operation = LibreSsl3PkeyOperationUndefined; return ret; } int APS5_VABI EVP_PKEY_sign_nid_postfix(LibreSsl3PkeyContext* context, unsigned char* signature, std::size_t* signatureLength, const unsigned char* content, std::size_t contentLength) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->signature) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationSign) { QueueEvpError(ReasonOperationNotInitialized); return -1; } return WithOutputLength(signatureLength, "EVP_PKEY_sign", [&](std::size_t& length) { if (const auto result = CheckAutomaticLength(context, method, signature, length)) return *result; return method->signature(context, signature, &length, content, contentLength); }); } int APS5_VABI EVP_PKEY_verify_init_nid_postfix(LibreSsl3PkeyContext* context) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->verification) { QueueEvpError(ReasonOperationNotSupported); return -2; } context->operation = LibreSsl3PkeyOperationVerify; if (!method->verificationInitialize) return 1; const int ret = method->verificationInitialize(context); if (ret <= 0) context->operation = LibreSsl3PkeyOperationUndefined; return ret; } int APS5_VABI EVP_PKEY_verify_nid_postfix(LibreSsl3PkeyContext* context, const unsigned char* signature, std::size_t signatureLength, const unsigned char* content, std::size_t contentLength) { const auto* method = context ? MethodOf(context) : nullptr; if (!context || !method || !method->verification) { QueueEvpError(ReasonOperationNotSupported); return -2; } if (context->operation != LibreSsl3PkeyOperationVerify) { QueueEvpError(ReasonOperationNotInitialized); return -1; } return method->verification(context, signature, signatureLength, content, contentLength); } int APS5_VABI RSA_pkey_ctx_ctrl_nid_postfix(LibreSsl3PkeyContext* context, int operationType, int command, int first, void* second) { const auto* method = context ? MethodOf(context) : nullptr; if (context != nullptr && method != nullptr && method->keyType != LibreSsl3PkeyRsa && method->keyType != LibreSsl3PkeyRsaPss) return -1; return EVP_PKEY_CTX_ctrl_nid_postfix(context, -1, operationType, command, first, second); } 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); } int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, unsigned char* sigret, std::size_t* siglen) { auto* pctx = static_cast(ctx->keyContext); if (!pctx) NotImplemented_nid_no_patch("EVP_DigestSignFinal without an EVP_PKEY_CTX"); const auto* method = MethodOf(pctx); if (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) NotImplemented_nid_no_patch("EVP_DigestSignFinal with a custom signature context"); if (method->signatureContext) NotImplemented_nid_no_patch("EVP_DigestSignFinal with a signctx method"); if (sigret == nullptr) { const auto size = ctx->digest->digestSize; if (size < 0) return 0; return EVP_PKEY_sign_nid_postfix(pctx, nullptr, siglen, nullptr, static_cast(size)) > 0; } LibreSsl3::DigestContext copy = {}; unsigned char digest[LibreSsl3MaximumDigestSize]; unsigned int digestLength = 0; auto result = 0; if (DigestCopy(©, ctx) && DigestFinal(©, digest, &digestLength)) 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_SignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, unsigned char* sigret, unsigned int* siglen, LibreSsl3EvpPkey* pkey) { unsigned char digest[LibreSsl3MaximumDigestSize]; unsigned int digestLength = 0; LibreSsl3::DigestContext copy = {}; LibreSsl3PkeyContext* pkctx = nullptr; std::size_t sltmp = 0; unsigned int written = 0; int ret = 0; const unsigned int zero = 0; if (!GuestCopyOut_nid_no_patch(siglen, &zero, sizeof(zero))) return 0; if (!DigestCopy(©, ctx)) goto err; if (!DigestFinal(©, digest, &digestLength)) goto err; sltmp = EVP_PKEY_size_nid_postfix(pkey); pkctx = EVP_PKEY_CTX_new_nid_postfix(pkey, nullptr); if (!pkctx) goto err; if (EVP_PKEY_sign_init_nid_postfix(pkctx) <= 0) goto err; if (EVP_PKEY_CTX_ctrl_nid_postfix(pkctx, -1, LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlMessageDigest, 0, static_cast(const_cast(ctx->digest))) <= 0) goto err; if (EVP_PKEY_sign_nid_postfix(pkctx, sigret, &sltmp, digest, digestLength) <= 0) goto err; written = static_cast(sltmp); if (!GuestCopyOut_nid_no_patch(siglen, &written, sizeof(written))) goto err; ret = 1; err: if (copy.data) free_nid_postfix(copy.data); EVP_PKEY_CTX_free_nid_postfix(pkctx); return ret; } int APS5_VABI EVP_VerifyFinal_nid_postfix(LibreSsl3::DigestContext* ctx, const unsigned char* sigbuf, unsigned int siglen, LibreSsl3EvpPkey* pkey) { unsigned char digest[LibreSsl3MaximumDigestSize]; unsigned int digestLength = 0; LibreSsl3::DigestContext copy = {}; LibreSsl3PkeyContext* pkctx = nullptr; int ret = 0; if (!DigestCopy(©, ctx)) goto err; if (!DigestFinal(©, digest, &digestLength)) goto err; ret = -1; pkctx = EVP_PKEY_CTX_new_nid_postfix(pkey, nullptr); if (!pkctx) goto err; if (EVP_PKEY_verify_init_nid_postfix(pkctx) <= 0) goto err; if (EVP_PKEY_CTX_ctrl_nid_postfix(pkctx, -1, LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlMessageDigest, 0, static_cast(const_cast(ctx->digest))) <= 0) goto err; ret = EVP_PKEY_verify_nid_postfix(pkctx, sigbuf, siglen, digest, digestLength); err: if (copy.data) free_nid_postfix(copy.data); EVP_PKEY_CTX_free_nid_postfix(pkctx); return ret; } }