@@ -26,19 +26,35 @@ const { isArrayBufferView } = require('internal/util/types');
26
26
const tls = require ( 'tls' ) ;
27
27
const {
28
28
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED ,
29
- ERR_INVALID_ARG_TYPE
29
+ ERR_INVALID_ARG_TYPE ,
30
+ ERR_TLS_INVALID_PROTOCOL_VERSION ,
31
+ ERR_TLS_PROTOCOL_VERSION_CONFLICT ,
30
32
} = require ( 'internal/errors' ) . codes ;
31
33
32
- const { SSL_OP_CIPHER_SERVER_PREFERENCE } = process . binding ( 'constants' ) . crypto ;
34
+ const {
35
+ SSL_OP_CIPHER_SERVER_PREFERENCE ,
36
+ TLS1_VERSION ,
37
+ TLS1_1_VERSION ,
38
+ TLS1_2_VERSION ,
39
+ } = process . binding ( 'constants' ) . crypto ;
33
40
34
41
// Lazily loaded
35
42
var crypto = null ;
36
43
37
- const { SecureContext : NativeSecureContext } = internalBinding ( 'crypto' ) ;
44
+ function toV ( which , v , def ) {
45
+ if ( v == null ) v = def ;
46
+ if ( v === 'TLSv1' ) return TLS1_VERSION ;
47
+ if ( v === 'TLSv1.1' ) return TLS1_1_VERSION ;
48
+ if ( v === 'TLSv1.2' ) return TLS1_2_VERSION ;
49
+ throw new ERR_TLS_INVALID_PROTOCOL_VERSION ( v , which ) ;
50
+ }
38
51
39
- function SecureContext ( secureProtocol , secureOptions , context ) {
52
+ const { SecureContext : NativeSecureContext } = internalBinding ( 'crypto' ) ;
53
+ function SecureContext ( secureProtocol , secureOptions , context ,
54
+ minVersion , maxVersion ) {
40
55
if ( ! ( this instanceof SecureContext ) ) {
41
- return new SecureContext ( secureProtocol , secureOptions , context ) ;
56
+ return new SecureContext ( secureProtocol , secureOptions , context ,
57
+ minVersion , maxVersion ) ;
42
58
}
43
59
44
60
if ( context ) {
@@ -47,10 +63,15 @@ function SecureContext(secureProtocol, secureOptions, context) {
47
63
this . context = new NativeSecureContext ( ) ;
48
64
49
65
if ( secureProtocol ) {
50
- this . context . init ( secureProtocol ) ;
51
- } else {
52
- this . context . init ( ) ;
66
+ if ( minVersion != null )
67
+ throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT ( minVersion , secureProtocol ) ;
68
+ if ( maxVersion != null )
69
+ throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT ( maxVersion , secureProtocol ) ;
53
70
}
71
+
72
+ this . context . init ( secureProtocol ,
73
+ toV ( 'minimum' , minVersion , tls . DEFAULT_MIN_VERSION ) ,
74
+ toV ( 'maximum' , maxVersion , tls . DEFAULT_MAX_VERSION ) ) ;
54
75
}
55
76
56
77
if ( secureOptions ) this . context . setOptions ( secureOptions ) ;
@@ -76,7 +97,8 @@ exports.createSecureContext = function createSecureContext(options, context) {
76
97
if ( options . honorCipherOrder )
77
98
secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE ;
78
99
79
- const c = new SecureContext ( options . secureProtocol , secureOptions , context ) ;
100
+ const c = new SecureContext ( options . secureProtocol , secureOptions , context ,
101
+ options . minVersion , options . maxVersion ) ;
80
102
var i ;
81
103
var val ;
82
104
0 commit comments