@ -37,6 +37,9 @@
# include <openssl/pem.h>
# include <openssl/pem.h>
# include <openssl/rsa.h>
# include <openssl/rsa.h>
# include <openssl/x509.h>
# include <openssl/x509.h>
# if OPENSSL_VERSION_NUMBER >= 0x30000000L
# include <openssl/param_build.h>
# endif
# include "absl/status/status.h"
# include "absl/status/status.h"
# include "absl/status/statusor.h"
# include "absl/status/statusor.h"
@ -523,7 +526,13 @@ static int RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) {
# endif // OPENSSL_VERSION_NUMBER < 0x10100000L
# endif // OPENSSL_VERSION_NUMBER < 0x10100000L
static EVP_PKEY * pkey_from_jwk ( const Json & json , const char * kty ) {
static EVP_PKEY * pkey_from_jwk ( const Json & json , const char * kty ) {
# if OPENSSL_VERSION_NUMBER < 0x30000000L
RSA * rsa = nullptr ;
RSA * rsa = nullptr ;
# else
EVP_PKEY_CTX * ctx = nullptr ;
OSSL_PARAM * params = NULL ;
OSSL_PARAM_BLD * bld = OSSL_PARAM_BLD_new ( ) ;
# endif
EVP_PKEY * result = nullptr ;
EVP_PKEY * result = nullptr ;
BIGNUM * tmp_n = nullptr ;
BIGNUM * tmp_n = nullptr ;
BIGNUM * tmp_e = nullptr ;
BIGNUM * tmp_e = nullptr ;
@ -535,11 +544,13 @@ static EVP_PKEY* pkey_from_jwk(const Json& json, const char* kty) {
gpr_log ( GPR_ERROR , " Unsupported key type %s. " , kty ) ;
gpr_log ( GPR_ERROR , " Unsupported key type %s. " , kty ) ;
goto end ;
goto end ;
}
}
# if OPENSSL_VERSION_NUMBER < 0x30000000L
rsa = RSA_new ( ) ;
rsa = RSA_new ( ) ;
if ( rsa = = nullptr ) {
if ( rsa = = nullptr ) {
gpr_log ( GPR_ERROR , " Could not create rsa key. " ) ;
gpr_log ( GPR_ERROR , " Could not create rsa key. " ) ;
goto end ;
goto end ;
}
}
# endif
it = json . object ( ) . find ( " n " ) ;
it = json . object ( ) . find ( " n " ) ;
if ( it = = json . object ( ) . end ( ) ) {
if ( it = = json . object ( ) . end ( ) ) {
gpr_log ( GPR_ERROR , " Missing RSA public key field. " ) ;
gpr_log ( GPR_ERROR , " Missing RSA public key field. " ) ;
@ -554,6 +565,7 @@ static EVP_PKEY* pkey_from_jwk(const Json& json, const char* kty) {
}
}
tmp_e = bignum_from_base64 ( validate_string_field ( it - > second , " e " ) ) ;
tmp_e = bignum_from_base64 ( validate_string_field ( it - > second , " e " ) ) ;
if ( tmp_e = = nullptr ) goto end ;
if ( tmp_e = = nullptr ) goto end ;
# if OPENSSL_VERSION_NUMBER < 0x30000000L
if ( ! RSA_set0_key ( rsa , tmp_n , tmp_e , nullptr ) ) {
if ( ! RSA_set0_key ( rsa , tmp_n , tmp_e , nullptr ) ) {
gpr_log ( GPR_ERROR , " Cannot set RSA key from inputs. " ) ;
gpr_log ( GPR_ERROR , " Cannot set RSA key from inputs. " ) ;
goto end ;
goto end ;
@ -563,9 +575,38 @@ static EVP_PKEY* pkey_from_jwk(const Json& json, const char* kty) {
tmp_e = nullptr ;
tmp_e = nullptr ;
result = EVP_PKEY_new ( ) ;
result = EVP_PKEY_new ( ) ;
EVP_PKEY_set1_RSA ( result , rsa ) ; // uprefs rsa.
EVP_PKEY_set1_RSA ( result , rsa ) ; // uprefs rsa.
# else
if ( ! OSSL_PARAM_BLD_push_BN ( bld , " n " , tmp_n ) | |
! OSSL_PARAM_BLD_push_BN ( bld , " e " , tmp_e ) | |
( params = OSSL_PARAM_BLD_to_param ( bld ) ) = = NULL ) {
gpr_log ( GPR_ERROR , " Could not create OSSL_PARAM " ) ;
goto end ;
}
ctx = EVP_PKEY_CTX_new_from_name ( nullptr , " RSA " , nullptr ) ;
if ( ctx = = nullptr ) {
gpr_log ( GPR_ERROR , " Could not create rsa key. " ) ;
goto end ;
}
if ( EVP_PKEY_fromdata_init ( ctx ) < = 0 ) {
gpr_log ( GPR_ERROR , " Could not create rsa key. " ) ;
goto end ;
}
if ( EVP_PKEY_fromdata ( ctx , & result , EVP_PKEY_KEYPAIR , params ) < = 0 ) {
gpr_log ( GPR_ERROR , " Cannot set RSA key from inputs. " ) ;
goto end ;
}
# endif
end :
end :
# if OPENSSL_VERSION_NUMBER < 0x30000000L
RSA_free ( rsa ) ;
RSA_free ( rsa ) ;
# else
EVP_PKEY_CTX_free ( ctx ) ;
OSSL_PARAM_free ( params ) ;
OSSL_PARAM_BLD_free ( bld ) ;
# endif
BN_free ( tmp_n ) ;
BN_free ( tmp_n ) ;
BN_free ( tmp_e ) ;
BN_free ( tmp_e ) ;
return result ;
return result ;
@ -642,6 +683,7 @@ static int verify_jwt_signature(EVP_PKEY* key, const char* alg,
if ( EVP_DigestVerifyFinal ( md_ctx , GRPC_SLICE_START_PTR ( signature ) ,
if ( EVP_DigestVerifyFinal ( md_ctx , GRPC_SLICE_START_PTR ( signature ) ,
GRPC_SLICE_LENGTH ( signature ) ) ! = 1 ) {
GRPC_SLICE_LENGTH ( signature ) ) ! = 1 ) {
gpr_log ( GPR_ERROR , " JWT signature verification failed. " ) ;
gpr_log ( GPR_ERROR , " JWT signature verification failed. " ) ;
goto end ;
goto end ;
}
}
result = 1 ;
result = 1 ;