@@ -34,8 +34,6 @@ import {
3434 CertExtendedKeyUsageOIDToName ,
3535 CertKeyAlgorithm ,
3636 CertKeyUsage ,
37- CertSignatureAlgorithm ,
38- CertSignatureType ,
3937 CertStatus ,
4038 TAltNameMapping
4139} from "../../certificate/certificate-types" ;
@@ -127,6 +125,22 @@ export const internalCertificateAuthorityServiceFactory = ({
127125 kmsService,
128126 permissionService
129127} : TInternalCertificateAuthorityServiceFactoryDep ) => {
128+ const $checkSignature = ( caKeyAlg : string , requestedKeyType : string , signatureAlgorithm ?: string ) => {
129+ const isRsaCa = caKeyAlg . startsWith ( "RSA" ) ;
130+ const isEcdsaCa = caKeyAlg . startsWith ( "EC" ) || caKeyAlg . startsWith ( "ECDSA" ) ;
131+
132+ // eslint-disable-next-line no-nested-ternary
133+ const caSupports = isRsaCa ? "RSA" : isEcdsaCa ? "ECDSA" : "unknown" ;
134+
135+ const isRequestValid = ( requestedKeyType === "RSA" && isRsaCa ) || ( requestedKeyType === "ECDSA" && isEcdsaCa ) ;
136+
137+ if ( ! isRequestValid ) {
138+ throw new BadRequestError ( {
139+ message : `Requested signature algorithm ${ signatureAlgorithm } is not compatible with CA key algorithm ${ caKeyAlg } . CA can only sign with ${ caSupports } -based signature algorithms.`
140+ } ) ;
141+ }
142+ } ;
143+
130144 const createCa = async ( {
131145 type,
132146 friendlyName,
@@ -1302,26 +1316,7 @@ export const internalCertificateAuthorityServiceFactory = ({
13021316 const leafKeys = await crypto . nativeCrypto . subtle . generateKey ( keyGenAlg , true , [ "sign" , "verify" ] ) ;
13031317
13041318 if ( signatureAlgorithm ) {
1305- const caKeyAlgorithm = ca . internalCa . keyAlgorithm ;
1306- const requestedKeyType = signatureAlgorithm . split ( "-" ) [ 0 ] ;
1307-
1308- const isRsaCa = caKeyAlgorithm . startsWith ( CertKeyAlgorithm . RSA_2048 . split ( "_" ) [ 0 ] ) ;
1309- const isEcdsaCa = caKeyAlgorithm . startsWith ( CertKeyAlgorithm . ECDSA_P256 . split ( "_" ) [ 0 ] ) ;
1310-
1311- if (
1312- ( requestedKeyType === CertSignatureAlgorithm . RSA_SHA256 . split ( "-" ) [ 0 ] && ! isRsaCa ) ||
1313- ( requestedKeyType === CertSignatureAlgorithm . ECDSA_SHA256 . split ( "-" ) [ 0 ] && ! isEcdsaCa )
1314- ) {
1315- // eslint-disable-next-line no-nested-ternary
1316- const supportedType = isRsaCa
1317- ? CertSignatureAlgorithm . RSA_SHA256 . split ( "-" ) [ 0 ]
1318- : isEcdsaCa
1319- ? CertSignatureAlgorithm . ECDSA_SHA256 . split ( "-" ) [ 0 ]
1320- : "unknown" ;
1321- throw new BadRequestError ( {
1322- message : `Requested signature algorithm ${ signatureAlgorithm } is not compatible with CA key algorithm ${ caKeyAlgorithm } . CA can only sign with ${ supportedType } -based signature algorithms.`
1323- } ) ;
1324- }
1319+ $checkSignature ( ca . internalCa . keyAlgorithm , signatureAlgorithm . split ( "-" ) [ 0 ] , signatureAlgorithm ) ;
13251320 }
13261321
13271322 // Determine signing algorithm for certificate signing
@@ -1690,22 +1685,7 @@ export const internalCertificateAuthorityServiceFactory = ({
16901685 }
16911686
16921687 if ( signatureAlgorithm ) {
1693- const caKeyAlgorithm = ca . internalCa . keyAlgorithm ;
1694- const requestedKeyType = signatureAlgorithm . split ( "-" ) [ 0 ] ; // Get the first part (RSA, ECDSA)
1695-
1696- const isRsaCa = caKeyAlgorithm . startsWith ( CertSignatureType . RSA ) ;
1697- const isEcdsaCa = caKeyAlgorithm . startsWith ( CertSignatureType . ECDSA ) ;
1698-
1699- if (
1700- ( requestedKeyType === CertSignatureType . RSA && ! isRsaCa ) ||
1701- ( requestedKeyType === CertSignatureType . ECDSA && ! isEcdsaCa )
1702- ) {
1703- // eslint-disable-next-line no-nested-ternary
1704- const supportedType = isRsaCa ? CertSignatureType . RSA : isEcdsaCa ? CertSignatureType . ECDSA : "unknown" ;
1705- throw new BadRequestError ( {
1706- message : `Requested signature algorithm ${ signatureAlgorithm } is not compatible with CA key algorithm ${ caKeyAlgorithm } . CA can only sign with ${ supportedType } -based signature algorithms.`
1707- } ) ;
1708- }
1688+ $checkSignature ( ca . internalCa . keyAlgorithm , signatureAlgorithm . split ( "-" ) [ 0 ] , signatureAlgorithm ) ;
17091689 }
17101690
17111691 const effectiveKeyAlgorithm = ( keyAlgorithm || ca . internalCa . keyAlgorithm ) as CertKeyAlgorithm ;
0 commit comments