Browse Source

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: https://github.com/nodejs/node/pull/5611
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
process-exit-stdio-flushing
James M Snell 9 years ago
parent
commit
f429fe1b88
  1. 15
      doc/api/crypto.markdown
  2. 9
      lib/_tls_legacy.js
  3. 2
      lib/_tls_wrap.js
  4. 22
      lib/crypto.js
  5. 2
      lib/https.js
  6. 6
      lib/internal/util.js
  7. 2
      lib/tls.js

15
doc/api/crypto.markdown

@ -19,6 +19,21 @@ console.log(hash);
// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e // c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
``` ```
## Determining if crypto support is unavailable
It is possible for Node.js to be built without including support for the
`crypto` module. In such cases, calling `require('crypto')` will result in an
error being thrown.
```js
var crypto;
try {
crypto = require('crypto');
} catch (err) {
console.log('crypto support is disabled!');
}
```
## Class: Certificate ## Class: Certificate
SPKAC is a Certificate Signing Request mechanism originally implemented by SPKAC is a Certificate Signing Request mechanism originally implemented by

9
lib/_tls_legacy.js

@ -1,5 +1,7 @@
'use strict'; 'use strict';
require('internal/util').assertCrypto(exports);
const assert = require('assert'); const assert = require('assert');
const EventEmitter = require('events'); const EventEmitter = require('events');
const stream = require('stream'); const stream = require('stream');
@ -9,12 +11,7 @@ const common = require('_tls_common');
const debug = util.debuglog('tls-legacy'); const debug = util.debuglog('tls-legacy');
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const Timer = process.binding('timer_wrap').Timer; const Timer = process.binding('timer_wrap').Timer;
var Connection = null; const Connection = process.binding('crypto').Connection;
try {
Connection = process.binding('crypto').Connection;
} catch (e) {
throw new Error('Node.js is not compiled with openssl crypto support');
}
function SlabBuffer() { function SlabBuffer() {
this.create(); this.create();

2
lib/_tls_wrap.js

@ -1,5 +1,7 @@
'use strict'; 'use strict';
require('internal/util').assertCrypto(exports);
const assert = require('assert'); const assert = require('assert');
const crypto = require('crypto'); const crypto = require('crypto');
const net = require('net'); const net = require('net');

22
lib/crypto.js

@ -3,25 +3,23 @@
'use strict'; 'use strict';
const internalUtil = require('internal/util');
internalUtil.assertCrypto(exports);
exports.DEFAULT_ENCODING = 'buffer'; exports.DEFAULT_ENCODING = 'buffer';
try { const binding = process.binding('crypto');
var binding = process.binding('crypto'); const randomBytes = binding.randomBytes;
var randomBytes = binding.randomBytes; const getCiphers = binding.getCiphers;
var getCiphers = binding.getCiphers; const getHashes = binding.getHashes;
var getHashes = binding.getHashes; const getCurves = binding.getCurves;
var getCurves = binding.getCurves; const getFipsCrypto = binding.getFipsCrypto;
var getFipsCrypto = binding.getFipsCrypto; const setFipsCrypto = binding.setFipsCrypto;
var setFipsCrypto = binding.setFipsCrypto;
} catch (e) {
throw new Error('Node.js is not compiled with openssl crypto support');
}
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const constants = require('constants'); const constants = require('constants');
const stream = require('stream'); const stream = require('stream');
const util = require('util'); const util = require('util');
const internalUtil = require('internal/util');
const LazyTransform = require('internal/streams/lazy_transform'); const LazyTransform = require('internal/streams/lazy_transform');
const DH_GENERATOR = 2; const DH_GENERATOR = 2;

2
lib/https.js

@ -1,5 +1,7 @@
'use strict'; 'use strict';
require('internal/util').assertCrypto(exports);
const tls = require('tls'); const tls = require('tls');
const url = require('url'); const url = require('url');
const http = require('http'); const http = require('http');

6
lib/internal/util.js

@ -96,3 +96,9 @@ exports.isError = function isError(e) {
exports.objectToString = function objectToString(o) { exports.objectToString = function objectToString(o) {
return Object.prototype.toString.call(o); return Object.prototype.toString.call(o);
}; };
const noCrypto = !process.versions.openssl;
exports.assertCrypto = function(exports) {
if (noCrypto)
throw new Error('Node.js is not compiled with openssl crypto support');
};

2
lib/tls.js

@ -1,5 +1,7 @@
'use strict'; 'use strict';
require('internal/util').assertCrypto(exports);
const net = require('net'); const net = require('net');
const url = require('url'); const url = require('url');
const binding = process.binding('crypto'); const binding = process.binding('crypto');

Loading…
Cancel
Save