@@ -26,19 +26,35 @@ const { isArrayBufferView } = require('internal/util/types');
2626const tls = require ( 'tls' ) ;
2727const {
2828 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 ,
3032} = require ( 'internal/errors' ) . codes ;
3133
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 ;
3340
3441// Lazily loaded
3542var crypto = null ;
3643
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+ }
3851
39- function SecureContext ( secureProtocol , secureOptions , context ) {
52+ const { SecureContext : NativeSecureContext } = internalBinding ( 'crypto' ) ;
53+ function SecureContext ( secureProtocol , secureOptions , context ,
54+ minVersion , maxVersion ) {
4055 if ( ! ( this instanceof SecureContext ) ) {
41- return new SecureContext ( secureProtocol , secureOptions , context ) ;
56+ return new SecureContext ( secureProtocol , secureOptions , context ,
57+ minVersion , maxVersion ) ;
4258 }
4359
4460 if ( context ) {
@@ -47,10 +63,15 @@ function SecureContext(secureProtocol, secureOptions, context) {
4763 this . context = new NativeSecureContext ( ) ;
4864
4965 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 ) ;
5370 }
71+
72+ this . context . init ( secureProtocol ,
73+ toV ( 'minimum' , minVersion , tls . DEFAULT_MIN_VERSION ) ,
74+ toV ( 'maximum' , maxVersion , tls . DEFAULT_MAX_VERSION ) ) ;
5475 }
5576
5677 if ( secureOptions ) this . context . setOptions ( secureOptions ) ;
@@ -76,7 +97,8 @@ exports.createSecureContext = function createSecureContext(options, context) {
7697 if ( options . honorCipherOrder )
7798 secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE ;
7899
79- const c = new SecureContext ( options . secureProtocol , secureOptions , context ) ;
100+ const c = new SecureContext ( options . secureProtocol , secureOptions , context ,
101+ options . minVersion , options . maxVersion ) ;
80102 var i ;
81103 var val ;
82104
0 commit comments