• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const {
9  generateKeyPair,
10} = require('crypto');
11const {
12  assertApproximateSize,
13  testSignVerify,
14  spkiExp,
15} = require('../common/crypto');
16
17// Test async DSA key generation.
18{
19  const privateKeyEncoding = {
20    type: 'pkcs8',
21    format: 'der'
22  };
23
24  generateKeyPair('dsa', {
25    modulusLength: common.hasOpenSSL3 ? 2048 : 512,
26    divisorLength: 256,
27    publicKeyEncoding: {
28      type: 'spki',
29      format: 'pem'
30    },
31    privateKeyEncoding: {
32      cipher: 'aes-128-cbc',
33      passphrase: 'secret',
34      ...privateKeyEncoding
35    }
36  }, common.mustSucceed((publicKey, privateKeyDER) => {
37    assert.strictEqual(typeof publicKey, 'string');
38    assert.match(publicKey, spkiExp);
39    // The private key is DER-encoded.
40    assert(Buffer.isBuffer(privateKeyDER));
41
42    assertApproximateSize(publicKey, common.hasOpenSSL3 ? 1194 : 440);
43    assertApproximateSize(privateKeyDER, common.hasOpenSSL3 ? 721 : 336);
44
45    // Since the private key is encrypted, signing shouldn't work anymore.
46    assert.throws(() => {
47      return testSignVerify(publicKey, {
48        key: privateKeyDER,
49        ...privateKeyEncoding
50      });
51    }, {
52      name: 'TypeError',
53      code: 'ERR_MISSING_PASSPHRASE',
54      message: 'Passphrase required for encrypted key'
55    });
56
57    // Signing should work with the correct password.
58    testSignVerify(publicKey, {
59      key: privateKeyDER,
60      ...privateKeyEncoding,
61      passphrase: 'secret'
62    });
63  }));
64}
65