Skip to content

Commit f429fe1

Browse files
committed
crypto: fail early when loading crypto without openssl
Fail early in require('crypto'), require('tls'), require('https'), etc when crypto is not available (rather than depending on an internal try/catch). Add documentation for detecting when crypto is not available. PR-URL: #5611 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 0b3936b commit f429fe1

File tree

7 files changed

+40
-18
lines changed

7 files changed

+40
-18
lines changed

doc/api/crypto.markdown

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ console.log(hash);
1919
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
2020
```
2121

22+
## Determining if crypto support is unavailable
23+
24+
It is possible for Node.js to be built without including support for the
25+
`crypto` module. In such cases, calling `require('crypto')` will result in an
26+
error being thrown.
27+
28+
```js
29+
var crypto;
30+
try {
31+
crypto = require('crypto');
32+
} catch (err) {
33+
console.log('crypto support is disabled!');
34+
}
35+
```
36+
2237
## Class: Certificate
2338

2439
SPKAC is a Certificate Signing Request mechanism originally implemented by

lib/_tls_legacy.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
require('internal/util').assertCrypto(exports);
4+
35
const assert = require('assert');
46
const EventEmitter = require('events');
57
const stream = require('stream');
@@ -9,12 +11,7 @@ const common = require('_tls_common');
911
const debug = util.debuglog('tls-legacy');
1012
const Buffer = require('buffer').Buffer;
1113
const Timer = process.binding('timer_wrap').Timer;
12-
var Connection = null;
13-
try {
14-
Connection = process.binding('crypto').Connection;
15-
} catch (e) {
16-
throw new Error('Node.js is not compiled with openssl crypto support');
17-
}
14+
const Connection = process.binding('crypto').Connection;
1815

1916
function SlabBuffer() {
2017
this.create();

lib/_tls_wrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
require('internal/util').assertCrypto(exports);
4+
35
const assert = require('assert');
46
const crypto = require('crypto');
57
const net = require('net');

lib/crypto.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33

44
'use strict';
55

6+
const internalUtil = require('internal/util');
7+
internalUtil.assertCrypto(exports);
8+
69
exports.DEFAULT_ENCODING = 'buffer';
710

8-
try {
9-
var binding = process.binding('crypto');
10-
var randomBytes = binding.randomBytes;
11-
var getCiphers = binding.getCiphers;
12-
var getHashes = binding.getHashes;
13-
var getCurves = binding.getCurves;
14-
var getFipsCrypto = binding.getFipsCrypto;
15-
var setFipsCrypto = binding.setFipsCrypto;
16-
} catch (e) {
17-
throw new Error('Node.js is not compiled with openssl crypto support');
18-
}
11+
const binding = process.binding('crypto');
12+
const randomBytes = binding.randomBytes;
13+
const getCiphers = binding.getCiphers;
14+
const getHashes = binding.getHashes;
15+
const getCurves = binding.getCurves;
16+
const getFipsCrypto = binding.getFipsCrypto;
17+
const setFipsCrypto = binding.setFipsCrypto;
1918

2019
const Buffer = require('buffer').Buffer;
2120
const constants = require('constants');
2221
const stream = require('stream');
2322
const util = require('util');
24-
const internalUtil = require('internal/util');
2523
const LazyTransform = require('internal/streams/lazy_transform');
2624

2725
const DH_GENERATOR = 2;

lib/https.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
require('internal/util').assertCrypto(exports);
4+
35
const tls = require('tls');
46
const url = require('url');
57
const http = require('http');

lib/internal/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ exports.isError = function isError(e) {
9696
exports.objectToString = function objectToString(o) {
9797
return Object.prototype.toString.call(o);
9898
};
99+
100+
const noCrypto = !process.versions.openssl;
101+
exports.assertCrypto = function(exports) {
102+
if (noCrypto)
103+
throw new Error('Node.js is not compiled with openssl crypto support');
104+
};

lib/tls.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
require('internal/util').assertCrypto(exports);
4+
35
const net = require('net');
46
const url = require('url');
57
const binding = process.binding('crypto');

0 commit comments

Comments
 (0)