selfsigned
Advanced tools
+1
| 22 |
+21
-3
@@ -1,3 +0,21 @@ | ||
| import { pki } from 'node-forge' | ||
| declare enum ASN1Class { | ||
| UNIVERSAL = 0x00, | ||
| APPLICATION = 0x40, | ||
| CONTEXT_SPECIFIC = 0x80, | ||
| PRIVATE = 0xc0, | ||
| } | ||
| interface CertificateFieldOptions { | ||
| name?: string | undefined; | ||
| type?: string | undefined; | ||
| shortName?: string | undefined; | ||
| } | ||
| interface CertificateField extends CertificateFieldOptions { | ||
| valueConstructed?: boolean | undefined; | ||
| valueTagClass?: ASN1Class | undefined; | ||
| value?: any[] | string | undefined; | ||
| extensions?: any[] | undefined; | ||
| } | ||
| declare interface SelfsignedOptions { | ||
@@ -60,3 +78,3 @@ /** | ||
| declare function generate( | ||
| attrs?: pki.CertificateField[], | ||
| attrs?: CertificateField[], | ||
| opts?: SelfsignedOptions | ||
@@ -66,3 +84,3 @@ ): GenerateResult | ||
| declare function generate( | ||
| attrs?: pki.CertificateField[], | ||
| attrs?: CertificateField[], | ||
| opts?: SelfsignedOptions, | ||
@@ -69,0 +87,0 @@ /** Optional callback, if not provided the generation is synchronous */ |
+114
-78
@@ -1,2 +0,2 @@ | ||
| var forge = require('node-forge'); | ||
| var forge = require("node-forge"); | ||
@@ -7,5 +7,5 @@ // a hexString is considered negative if it's most significant bit is 1 | ||
| // http://www.ietf.org/rfc/rfc5280.txt | ||
| function toPositiveHex(hexString){ | ||
| function toPositiveHex(hexString) { | ||
| var mostSiginficativeHexAsInt = parseInt(hexString[0], 16); | ||
| if (mostSiginficativeHexAsInt < 8){ | ||
| if (mostSiginficativeHexAsInt < 8) { | ||
| return hexString; | ||
@@ -20,3 +20,3 @@ } | ||
| switch (key) { | ||
| case 'sha256': | ||
| case "sha256": | ||
| return forge.md.sha256.create(); | ||
@@ -30,3 +30,3 @@ default: | ||
| * | ||
| * @param {forge.pki.CertificateField[]} attrs Attributes used for subject and issuer. | ||
| * @param {CertificateField[]} attrs Attributes used for subject and issuer. | ||
| * @param {object} options | ||
@@ -44,6 +44,6 @@ * @param {number} [options.days=365] the number of days before expiration | ||
| exports.generate = function generate(attrs, options, done) { | ||
| if (typeof attrs === 'function') { | ||
| if (typeof attrs === "function") { | ||
| done = attrs; | ||
| attrs = undefined; | ||
| } else if (typeof options === 'function') { | ||
| } else if (typeof options === "function") { | ||
| done = options; | ||
@@ -58,3 +58,5 @@ options = {}; | ||
| cert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9))); // the serial number can be decimal or hex (if preceded by 0x) | ||
| cert.serialNumber = toPositiveHex( | ||
| forge.util.bytesToHex(forge.random.getBytesSync(9)) | ||
| ); // the serial number can be decimal or hex (if preceded by 0x) | ||
@@ -67,21 +69,28 @@ cert.validity.notBefore = options.notBeforeDate || new Date(); | ||
| attrs = attrs || [{ | ||
| name: 'commonName', | ||
| value: 'example.org' | ||
| }, { | ||
| name: 'countryName', | ||
| value: 'US' | ||
| }, { | ||
| shortName: 'ST', | ||
| value: 'Virginia' | ||
| }, { | ||
| name: 'localityName', | ||
| value: 'Blacksburg' | ||
| }, { | ||
| name: 'organizationName', | ||
| value: 'Test' | ||
| }, { | ||
| shortName: 'OU', | ||
| value: 'Test' | ||
| }]; | ||
| attrs = attrs || [ | ||
| { | ||
| name: "commonName", | ||
| value: "example.org", | ||
| }, | ||
| { | ||
| name: "countryName", | ||
| value: "US", | ||
| }, | ||
| { | ||
| shortName: "ST", | ||
| value: "Virginia", | ||
| }, | ||
| { | ||
| name: "localityName", | ||
| value: "Blacksburg", | ||
| }, | ||
| { | ||
| name: "organizationName", | ||
| value: "Test", | ||
| }, | ||
| { | ||
| shortName: "OU", | ||
| value: "Test", | ||
| }, | ||
| ]; | ||
@@ -93,19 +102,27 @@ cert.setSubject(attrs); | ||
| cert.setExtensions(options.extensions || [{ | ||
| name: 'basicConstraints', | ||
| cA: true | ||
| }, { | ||
| name: 'keyUsage', | ||
| keyCertSign: true, | ||
| digitalSignature: true, | ||
| nonRepudiation: true, | ||
| keyEncipherment: true, | ||
| dataEncipherment: true | ||
| }, { | ||
| name: 'subjectAltName', | ||
| altNames: [{ | ||
| type: 6, // URI | ||
| value: 'http://example.org/webid#me' | ||
| }] | ||
| }]); | ||
| cert.setExtensions( | ||
| options.extensions || [ | ||
| { | ||
| name: "basicConstraints", | ||
| cA: true, | ||
| }, | ||
| { | ||
| name: "keyUsage", | ||
| keyCertSign: true, | ||
| digitalSignature: true, | ||
| nonRepudiation: true, | ||
| keyEncipherment: true, | ||
| dataEncipherment: true, | ||
| }, | ||
| { | ||
| name: "subjectAltName", | ||
| altNames: [ | ||
| { | ||
| type: 6, // URI | ||
| value: "http://example.org/webid#me", | ||
| }, | ||
| ], | ||
| }, | ||
| ] | ||
| ); | ||
@@ -115,13 +132,13 @@ cert.sign(keyPair.privateKey, getAlgorithm(options && options.algorithm)); | ||
| const fingerprint = forge.md.sha1 | ||
| .create() | ||
| .update(forge.asn1.toDer(forge.pki.certificateToAsn1(cert)).getBytes()) | ||
| .digest() | ||
| .toHex() | ||
| .match(/.{2}/g) | ||
| .join(':'); | ||
| .create() | ||
| .update(forge.asn1.toDer(forge.pki.certificateToAsn1(cert)).getBytes()) | ||
| .digest() | ||
| .toHex() | ||
| .match(/.{2}/g) | ||
| .join(":"); | ||
| var pem = { | ||
| private: forge.pki.privateKeyToPem(keyPair.privateKey), | ||
| public: forge.pki.publicKeyToPem(keyPair.publicKey), | ||
| cert: forge.pki.certificateToPem(cert), | ||
| private: forge.pki.privateKeyToPem(keyPair.privateKey), | ||
| public: forge.pki.publicKeyToPem(keyPair.publicKey), | ||
| cert: forge.pki.certificateToPem(cert), | ||
| fingerprint: fingerprint, | ||
@@ -137,17 +154,26 @@ }; | ||
| if (options && options.clientCertificate) { | ||
| var clientkeys = forge.pki.rsa.generateKeyPair(options.clientCertificateKeySize || 1024); | ||
| var clientkeys = forge.pki.rsa.generateKeyPair( | ||
| options.clientCertificateKeySize || 1024 | ||
| ); | ||
| var clientcert = forge.pki.createCertificate(); | ||
| clientcert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9))); | ||
| clientcert.serialNumber = toPositiveHex( | ||
| forge.util.bytesToHex(forge.random.getBytesSync(9)) | ||
| ); | ||
| clientcert.validity.notBefore = new Date(); | ||
| clientcert.validity.notAfter = new Date(); | ||
| clientcert.validity.notAfter.setFullYear(clientcert.validity.notBefore.getFullYear() + 1); | ||
| clientcert.validity.notAfter.setFullYear( | ||
| clientcert.validity.notBefore.getFullYear() + 1 | ||
| ); | ||
| var clientAttrs = JSON.parse(JSON.stringify(attrs)); | ||
| for(var i = 0; i < clientAttrs.length; i++) { | ||
| if(clientAttrs[i].name === 'commonName') { | ||
| if( options.clientCertificateCN ) | ||
| clientAttrs[i] = { name: 'commonName', value: options.clientCertificateCN }; | ||
| for (var i = 0; i < clientAttrs.length; i++) { | ||
| if (clientAttrs[i].name === "commonName") { | ||
| if (options.clientCertificateCN) | ||
| clientAttrs[i] = { | ||
| name: "commonName", | ||
| value: options.clientCertificateCN, | ||
| }; | ||
| else | ||
| clientAttrs[i] = { name: 'commonName', value: 'John Doe jdoe123' }; | ||
| clientAttrs[i] = { name: "commonName", value: "John Doe jdoe123" }; | ||
| } | ||
@@ -181,11 +207,13 @@ } | ||
| try { | ||
| forge.pki.verifyCertificateChain(caStore, [cert], | ||
| forge.pki.verifyCertificateChain( | ||
| caStore, | ||
| [cert], | ||
| function (vfd, depth, chain) { | ||
| if (vfd !== true) { | ||
| throw new Error('Certificate could not be verified.'); | ||
| throw new Error("Certificate could not be verified."); | ||
| } | ||
| return true; | ||
| }); | ||
| } | ||
| catch(ex) { | ||
| } | ||
| ); | ||
| } catch (ex) { | ||
| throw new Error(ex); | ||
@@ -199,20 +227,28 @@ } | ||
| if (done) { // async scenario | ||
| return forge.pki.rsa.generateKeyPair({ bits: keySize }, function (err, keyPair) { | ||
| if (err) { return done(err); } | ||
| if (done) { | ||
| // async scenario | ||
| return forge.pki.rsa.generateKeyPair( | ||
| { bits: keySize }, | ||
| function (err, keyPair) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| try { | ||
| return done(null, generatePem(keyPair)); | ||
| } catch (ex) { | ||
| return done(ex); | ||
| try { | ||
| return done(null, generatePem(keyPair)); | ||
| } catch (ex) { | ||
| return done(ex); | ||
| } | ||
| } | ||
| }); | ||
| ); | ||
| } | ||
| var keyPair = options.keyPair ? { | ||
| privateKey: forge.pki.privateKeyFromPem(options.keyPair.privateKey), | ||
| publicKey: forge.pki.publicKeyFromPem(options.keyPair.publicKey) | ||
| } : forge.pki.rsa.generateKeyPair(keySize); | ||
| var keyPair = options.keyPair | ||
| ? { | ||
| privateKey: forge.pki.privateKeyFromPem(options.keyPair.privateKey), | ||
| publicKey: forge.pki.publicKeyFromPem(options.keyPair.publicKey), | ||
| } | ||
| : forge.pki.rsa.generateKeyPair(keySize); | ||
| return generatePem(keyPair); | ||
| }; |
+2
-2
| { | ||
| "name": "selfsigned", | ||
| "version": "2.4.1", | ||
| "version": "3.0.0", | ||
| "description": "Generate self signed certificates private and public keys", | ||
@@ -35,6 +35,6 @@ "main": "index.js", | ||
| "dependencies": { | ||
| "@types/node-forge": "^1.3.0", | ||
| "node-forge": "^1" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node-forge": "^1.3.0", | ||
| "chai": "^4.3.4", | ||
@@ -41,0 +41,0 @@ "mocha": "^9.1.1" |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
26084
2.88%1
-50%8
14.29%419
14.17%3
50%- Removed
- Removed
- Removed
- Removed