|
| 1 | +From a7e8464f2a2826820b94cc641ac0aae345641fc6 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Raul Metsma < [email protected]> |
| 3 | +Date: Thu, 26 Jun 2025 17:47:54 +0300 |
| 4 | +Subject: [PATCH] (xmlsec-openssl) add supoprt for RSA PSS key type (#933) |
| 5 | + |
| 6 | +--- |
| 7 | + src/openssl/evp.c | 35 ++++++++++++++++++++++++++++++----- |
| 8 | + src/openssl/kt_rsa.c | 11 ++++++----- |
| 9 | + src/openssl/private.h | 11 ++++++++++- |
| 10 | + src/openssl/x509vfy.c | 1 - |
| 11 | + 4 files changed, 46 insertions(+), 12 deletions(-) |
| 12 | + |
| 13 | +diff --git a/src/openssl/evp.c b/src/openssl/evp.c |
| 14 | +index 97b7b6c2..a3e00006 100644 |
| 15 | +--- a/src/openssl/evp.c |
| 16 | ++++ b/src/openssl/evp.c |
| 17 | +@@ -32,7 +32,6 @@ |
| 18 | + #include <xmlsec/openssl/app.h> |
| 19 | + #include <xmlsec/openssl/crypto.h> |
| 20 | + #include <xmlsec/openssl/evp.h> |
| 21 | +-#include "openssl_compat.h" |
| 22 | + |
| 23 | + #ifdef XMLSEC_OPENSSL_API_300 |
| 24 | + #include <openssl/core_names.h> |
| 25 | +@@ -41,6 +40,8 @@ |
| 26 | + |
| 27 | + #include "../cast_helpers.h" |
| 28 | + #include "../keysdata_helpers.h" |
| 29 | ++#include "openssl_compat.h" |
| 30 | ++#include "private.h" |
| 31 | + |
| 32 | + static int |
| 33 | + xmlSecOpenSSLGetBNValue(const xmlSecBufferPtr buf, BIGNUM **bigNum) { |
| 34 | +@@ -325,6 +326,8 @@ xmlSecOpenSSLEvpKeyAdopt(EVP_PKEY *pKey) { |
| 35 | + switch(EVP_PKEY_base_id(pKey)) { |
| 36 | + #ifndef XMLSEC_NO_RSA |
| 37 | + case EVP_PKEY_RSA: |
| 38 | ++ case EVP_PKEY_RSA2: |
| 39 | ++ case EVP_PKEY_RSA_PSS: |
| 40 | + data = xmlSecKeyDataCreate(xmlSecOpenSSLKeyDataRsaId); |
| 41 | + if(data == NULL) { |
| 42 | + xmlSecInternalError("xmlSecKeyDataCreate(xmlSecOpenSSLKeyDataRsaId)", NULL); |
| 43 | +@@ -3258,6 +3261,26 @@ done: |
| 44 | + |
| 45 | + #ifndef XMLSEC_NO_RSA |
| 46 | + |
| 47 | ++/** |
| 48 | ++ * xmlSecOpenSSLKeyValueRsaCheckKeyType: |
| 49 | ++ * @pKey: the EVP key to check |
| 50 | ++ * |
| 51 | ++ * Returns 0 if @pKey is a valid RSA key type, 1 if it is not, or a negative value if an error occurs. |
| 52 | ++ */ |
| 53 | ++int |
| 54 | ++xmlSecOpenSSLKeyValueRsaCheckKeyType(EVP_PKEY* pKey) { |
| 55 | ++ xmlSecAssert2(pKey != NULL, -1); |
| 56 | ++ |
| 57 | ++ switch(EVP_PKEY_base_id(pKey)) { |
| 58 | ++ case EVP_PKEY_RSA: |
| 59 | ++ case EVP_PKEY_RSA2: |
| 60 | ++ case EVP_PKEY_RSA_PSS: |
| 61 | ++ return(0); |
| 62 | ++ default: |
| 63 | ++ return(1); |
| 64 | ++ } |
| 65 | ++} |
| 66 | ++ |
| 67 | + /* |
| 68 | + * @xmlSecOpenSSLKeyValueRsa: holds the parts of OpenSSL RSA key |
| 69 | + */ |
| 70 | +@@ -3430,7 +3453,7 @@ int |
| 71 | + xmlSecOpenSSLKeyDataRsaAdoptEvp(xmlSecKeyDataPtr data, EVP_PKEY* pKey) { |
| 72 | + xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecOpenSSLKeyDataRsaId), -1); |
| 73 | + xmlSecAssert2(pKey != NULL, -1); |
| 74 | +- xmlSecAssert2(EVP_PKEY_base_id(pKey) == EVP_PKEY_RSA, -1); |
| 75 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1); |
| 76 | + |
| 77 | + return(xmlSecOpenSSLEvpKeyDataAdoptEvp(data, pKey)); |
| 78 | + } |
| 79 | +@@ -3535,9 +3558,11 @@ xmlSecOpenSSLKeyDataRsaGetRsa(xmlSecKeyDataPtr data) { |
| 80 | + xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecOpenSSLKeyDataRsaId), NULL); |
| 81 | + |
| 82 | + pKey = xmlSecOpenSSLKeyDataRsaGetEvp(data); |
| 83 | +- xmlSecAssert2((pKey == NULL) || (EVP_PKEY_base_id(pKey) == EVP_PKEY_RSA), NULL); |
| 84 | +- |
| 85 | +- return((pKey != NULL) ? EVP_PKEY_get0_RSA(pKey) : NULL); |
| 86 | ++ if (pKey == NULL) { |
| 87 | ++ return(NULL); |
| 88 | ++ } |
| 89 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, NULL); |
| 90 | ++ return(EVP_PKEY_get0_RSA(pKey)); |
| 91 | + } |
| 92 | + |
| 93 | + static int |
| 94 | +diff --git a/src/openssl/kt_rsa.c b/src/openssl/kt_rsa.c |
| 95 | +index a1153499..d25c001e 100644 |
| 96 | +--- a/src/openssl/kt_rsa.c |
| 97 | ++++ b/src/openssl/kt_rsa.c |
| 98 | +@@ -34,7 +34,6 @@ |
| 99 | + |
| 100 | + #include <xmlsec/openssl/crypto.h> |
| 101 | + #include <xmlsec/openssl/evp.h> |
| 102 | +-#include "openssl_compat.h" |
| 103 | + |
| 104 | + #ifdef XMLSEC_OPENSSL_API_300 |
| 105 | + #include <openssl/core_names.h> |
| 106 | +@@ -43,6 +42,8 @@ |
| 107 | + |
| 108 | + #include "../cast_helpers.h" |
| 109 | + #include "../transform_helpers.h" |
| 110 | ++#include "openssl_compat.h" |
| 111 | ++#include "private.h" |
| 112 | + |
| 113 | + #ifndef XMLSEC_NO_RSA_PKCS15 |
| 114 | + |
| 115 | +@@ -162,7 +163,7 @@ xmlSecOpenSSLRsaPkcs1ProcessImpl(xmlSecOpenSSLRsaPkcs1CtxPtr ctx, const xmlSecBy |
| 116 | + |
| 117 | + xmlSecAssert2(ctx != NULL, -1); |
| 118 | + xmlSecAssert2(ctx->pKey != NULL, -1); |
| 119 | +- xmlSecAssert2(EVP_PKEY_base_id(ctx->pKey) == EVP_PKEY_RSA, -1); |
| 120 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(ctx->pKey) == 0, -1); |
| 121 | + xmlSecAssert2(inBuf != NULL, -1); |
| 122 | + xmlSecAssert2(inSize > 0, -1); |
| 123 | + xmlSecAssert2(outBuf != NULL, -1); |
| 124 | +@@ -364,7 +365,7 @@ xmlSecOpenSSLRsaPkcs1SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) { |
| 125 | + xmlSecTransformGetName(transform)); |
| 126 | + return(-1); |
| 127 | + } |
| 128 | +- xmlSecAssert2(EVP_PKEY_base_id(pKey) == EVP_PKEY_RSA, -1); |
| 129 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1); |
| 130 | + |
| 131 | + if (transform->operation == xmlSecTransformOperationEncrypt) { |
| 132 | + encrypt = 1; |
| 133 | +@@ -693,7 +694,7 @@ xmlSecOpenSSLRsaOaepProcessImpl(xmlSecOpenSSLRsaOaepCtxPtr ctx, const xmlSecByte |
| 134 | + |
| 135 | + xmlSecAssert2(ctx != NULL, -1); |
| 136 | + xmlSecAssert2(ctx->pKey != NULL, -1); |
| 137 | +- xmlSecAssert2(EVP_PKEY_base_id(ctx->pKey) == EVP_PKEY_RSA, -1); |
| 138 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1); |
| 139 | + xmlSecAssert2(inBuf != NULL, -1); |
| 140 | + xmlSecAssert2(inSize > 0, -1); |
| 141 | + xmlSecAssert2(outBuf != NULL, -1); |
| 142 | +@@ -1223,7 +1224,7 @@ xmlSecOpenSSLRsaOaepSetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) { |
| 143 | + xmlSecTransformGetName(transform)); |
| 144 | + return(-1); |
| 145 | + } |
| 146 | +- xmlSecAssert2(EVP_PKEY_base_id(pKey) == EVP_PKEY_RSA, -1); |
| 147 | ++ xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1); |
| 148 | + |
| 149 | + if (transform->operation == xmlSecTransformOperationEncrypt) { |
| 150 | + encrypt = 1; |
| 151 | +diff --git a/src/openssl/private.h b/src/openssl/private.h |
| 152 | +index 6940f5bd..cc58a0b2 100644 |
| 153 | +--- a/src/openssl/private.h |
| 154 | ++++ b/src/openssl/private.h |
| 155 | +@@ -28,6 +28,16 @@ extern "C" { |
| 156 | + #endif /* __cplusplus */ |
| 157 | + |
| 158 | + |
| 159 | ++/****************************************************************************** |
| 160 | ++ * |
| 161 | ++ * RSA Util functions |
| 162 | ++ * |
| 163 | ++ ******************************************************************************/ |
| 164 | ++#ifndef XMLSEC_NO_RSA |
| 165 | ++ |
| 166 | ++int xmlSecOpenSSLKeyValueRsaCheckKeyType (EVP_PKEY* pKey); |
| 167 | ++ |
| 168 | ++#endif /* XMLSEC_NO_RSA */ |
| 169 | + |
| 170 | + /****************************************************************************** |
| 171 | + * |
| 172 | +@@ -85,7 +95,6 @@ int xmlSecOpenSSLX509Asn1TimeToTime (const ASN1_TIME |
| 173 | + STACK_OF(X509)* xmlSecOpenSSLKeyDataX509GetCerts (xmlSecKeyDataPtr data); |
| 174 | + STACK_OF(X509_CRL)* xmlSecOpenSSLKeyDataX509GetCrls (xmlSecKeyDataPtr data); |
| 175 | + |
| 176 | +- |
| 177 | + #endif /* XMLSEC_NO_X509 */ |
| 178 | + |
| 179 | + #ifdef __cplusplus |
| 180 | +diff --git a/src/openssl/x509vfy.c b/src/openssl/x509vfy.c |
| 181 | +index 322f6661..fdede0fc 100644 |
| 182 | +--- a/src/openssl/x509vfy.c |
| 183 | ++++ b/src/openssl/x509vfy.c |
| 184 | +@@ -34,7 +34,6 @@ |
| 185 | + #include <xmlsec/openssl/crypto.h> |
| 186 | + #include <xmlsec/openssl/evp.h> |
| 187 | + #include <xmlsec/openssl/x509.h> |
| 188 | +-#include "openssl_compat.h" |
| 189 | + |
| 190 | + #include <openssl/evp.h> |
| 191 | + #include <openssl/x509.h> |
| 192 | +-- |
| 193 | +2.46.0 |
| 194 | + |
0 commit comments