@@ -7,6 +7,22 @@ const assert = require('assert');
77const crypto = require ( 'crypto' ) ;
88const { hasOpenSSL } = require ( '../common/crypto' ) ;
99
10+ // Error code for a key-type mismatch during ECDH/EdDH derivation. The
11+ // underlying OpenSSL error code varies by version (and in OpenSSL 4.0, by
12+ // platform: some builds report a generic internal error instead
13+ // of a typed key-type mismatch).
14+ // TODO(panva): report the OpenSSL 4.0 generic internal error behavior
15+ // upstream and tighten this check once fixed.
16+ let keyTypeMismatchCode ;
17+ if ( hasOpenSSL ( 4 , 0 ) ) {
18+ keyTypeMismatchCode =
19+ / ^ E R R _ O S S L _ E V P _ ( O P E R A T I O N _ N O T _ S U P P O R T E D _ F O R _ T H I S _ K E Y T Y P E | I N T E R N A L _ E R R O R ) $ / ;
20+ } else if ( hasOpenSSL ( 3 ) ) {
21+ keyTypeMismatchCode = 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' ;
22+ } else {
23+ keyTypeMismatchCode = 'ERR_OSSL_EVP_DIFFERENT_KEY_TYPES' ;
24+ }
25+
1026assert . throws ( ( ) => crypto . diffieHellman ( ) , {
1127 name : 'TypeError' ,
1228 code : 'ERR_INVALID_ARG_TYPE' ,
@@ -397,9 +413,7 @@ test(crypto.generateKeyPairSync('x25519'),
397413 privateKey : crypto . generateKeyPairSync ( 'x448' ) . privateKey ,
398414 publicKey : crypto . generateKeyPairSync ( 'x25519' ) . publicKey ,
399415 } ;
400- testDHError ( options , { code : hasOpenSSL ( 3 ) ?
401- 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' :
402- 'ERR_OSSL_EVP_DIFFERENT_KEY_TYPES' } ) ;
416+ testDHError ( options , { code : keyTypeMismatchCode } ) ;
403417}
404418
405419// Test all key encoding formats
@@ -541,23 +555,21 @@ for (const { privateKey: alicePriv, publicKey: bobPub } of [
541555 testDHError ( {
542556 privateKey : privKey ( ec256 . privateKey ) ,
543557 publicKey : pubKey ( x25519 . publicKey ) ,
544- } , { code : hasOpenSSL ( 3 ) ?
545- 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' :
546- 'ERR_OSSL_EVP_DIFFERENT_KEY_TYPES' } ) ;
558+ } , { code : keyTypeMismatchCode } ) ;
547559
548560 // Unsupported key type (ed25519)
549561 testDHError ( {
550562 privateKey : privKey ( ed25519 . privateKey ) ,
551563 publicKey : pubKey ( ed25519 . publicKey ) ,
552- } , { code : 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' } ) ;
564+ } , { code : hasOpenSSL ( 4 , 0 ) ?
565+ / ^ E R R _ O S S L _ E V P _ ( O P E R A T I O N _ N O T _ S U P P O R T E D _ F O R _ T H I S _ K E Y T Y P E | I N T E R N A L _ E R R O R ) $ / :
566+ 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' } ) ;
553567
554568 // Incompatible key types (x448 + x25519)
555569 testDHError ( {
556570 privateKey : privKey ( x448 . privateKey ) ,
557571 publicKey : pubKey ( x25519 . publicKey ) ,
558- } , { code : hasOpenSSL ( 3 ) ?
559- 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' :
560- 'ERR_OSSL_EVP_DIFFERENT_KEY_TYPES' } ) ;
572+ } , { code : keyTypeMismatchCode } ) ;
561573
562574 // Zero x25519 public key
563575 testDHError ( {
0 commit comments