Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
http://tools.ietf.org/html/rfc2315#section-9.1
SignedData ::= SEQUENCE {
version Version,
digestAlgorithms DigestAlgorithmIdentifiers,
contentInfo ContentInfo,
certificates
[0] IMPLICIT ExtendedCertificatesAndCertificates
OPTIONAL,
crls
[1] IMPLICIT CertificateRevocationLists OPTIONAL,
signerInfos SignerInfos }
*/
var SignedData = asn1.define('SignedData', function() {
this.seq().obj(
this.key('version').int(),
this.key('digestAlgorithms').use(DigestAlgorithmIdentifiers),
this.key('contentInfo').use(ContentInfo),
this.key('certificate').optional().implicit(0).use(Certificates),
this.key('crls').optional().implicit(1).set(), // NOT PARSED
this.key('signerInfos').use(SignerInfos)
);
});
var RecipientKeyIdentifier = asn1.define('RecipientKeyIdentifier', function() {
this.seq().obj(
this.key('subjectKeyIdentifier').octstr(),
this.key('date').use(rfc3280.Time).optional(),
this.key('other').optional().any()
import { ec as EC } from 'elliptic'
// @ts-ignore
import * as asn1 from 'asn1.js'
const BN = require('bn.js')
/**
* Use types for the `bn.js` lib, e.g. `@types/bn.js`
*/
type BNjs = any
const ECPrivateKeyASN = asn1.define('ECPrivateKey', function () {
// @ts-ignore
const self = this as any
self.seq().obj(
self.key('version').int(),
self.key('privateKey').octstr(),
self.key('parameters').explicit(0).objid().optional(),
self.key('publicKey').explicit(1).bitstr().optional()
)
})
const SubjectPublicKeyInfoASN = asn1.define('SubjectPublicKeyInfo', function () {
// @ts-ignore
const self = this as any
self.seq().obj(
self.key('algorithm').seq().obj(
self.key("id").objid(),
// TODO(indutny): validate that version is v3
this.key('extensions').optional().explicit(3).use(Extensions)
);
});
exports.TBSCertificate = TBSCertificate;
var Version = asn1.define('Version', function() {
this.int({
0: 'v1',
1: 'v2',
2: 'v3'
});
});
exports.Version = Version;
var CertificateSerialNumber = asn1.define('CertificateSerialNumber',
function() {
this.int();
});
exports.CertificateSerialNumber = CertificateSerialNumber;
var Validity = asn1.define('Validity', function() {
this.seq().obj(
this.key('notBefore').use(Time),
this.key('notAfter').use(Time)
);
});
exports.Validity = Validity;
var Time = asn1.define('Time', function() {
this.choice({
utcTime: this.utctime(),
q = tmp.data;
offset = tmp.offset;
tmp = readNext(buffer, offset);
g = tmp.data;
offset = tmp.offset;
tmp = readNext(buffer, offset);
y = tmp.data;
} catch (e) {
console.log(e.stack);
throw new Error('Invalid ssh key: ' + key);
}
// DER is a subset of BER
der = new asn1.BerWriter();
der.startSequence();
der.startSequence();
der.writeOID('1.2.840.10040.4.1');
der.startSequence();
writeInt(der, p);
writeInt(der, q);
writeInt(der, g);
der.endSequence();
der.endSequence();
der.startSequence(0x03); // bit string
der.writeByte(0x00);
var ncert = chain[i + 1];
// The root cert, check if it's trusted:
if (!ncert || name) {
if (!name) {
return false;
}
chain.length = 0;
return true;
}
var nder = ncert.toString('hex');
var npem = KJUR.asn1.ASN1Util.getPEMStringFromHex(nder, 'CERTIFICATE');
// Get Next Certificate:
var ndata = new Buffer(nder, 'hex');
var nc = rfc5280.Certificate.decode(ndata, 'der');
var npubKey;
// Get Public Key from next certificate (via KJUR because it's a mess):
if (sigHashAlg !== 'none') {
var js = new KJUR.crypto.Signature({
alg: sigHashAlg + 'withRSA',
prov: 'cryptojs/jsrsa'
});
js.init(npem);
npubKey = js.pubKey;
}
// Get Signature Value from current certificate:
var data = new Buffer(der, 'hex');
var c = rfc5280.Certificate.decode(data, 'der');
var sig = c.signature.data;
var nc = rfc5280.Certificate.decode(ndata, 'der');
var npubKey;
// Get Public Key from next certificate (via KJUR because it's a mess):
if (sigHashAlg !== 'none') {
var js = new KJUR.crypto.Signature({
alg: sigHashAlg + 'withRSA',
prov: 'cryptojs/jsrsa'
});
js.init(npem);
npubKey = js.pubKey;
}
// Get Signature Value from current certificate:
var data = new Buffer(der, 'hex');
var c = rfc5280.Certificate.decode(data, 'der');
var sig = c.signature.data;
// Check Validity of Certificates
var validityVerified = PaymentProtocol.validateCertTime(c, nc);
// Check the Issuer matches the Subject of the next certificate:
var issuerVerified = PaymentProtocol.validateCertIssuer(c, nc);
var sigVerified;
// Verify current Certificate signature
if (sigHashAlg !== 'none') {
var jsrsaSig = new KJUR.crypto.Signature({
alg: sigHashAlg + 'withRSA',
prov: 'cryptojs/jsrsa'
});
keyType = 'dss';
if (keyType === 'ec' && semver.lt(process.version, '5.2.0')) {
return new Error(
'EC private keys are not supported in this version of node'
);
}
if (!RE_HEADER_OPENSSH.test(data[1])) {
// unencrypted, no headers
var privData = new Buffer(data.slice(1, -1).join(''), 'base64');
if (keyType !== 'ec') {
ret.fulltype = 'ssh-' + keyType;
} else {
// ECDSA
var asnReader = new Ber.Reader(privData);
asnReader.readSequence();
asnReader.readInt();
asnReader.readString(Ber.OctetString, true);
asnReader.readByte(); // Skip "complex" context type byte
var offset = asnReader.readLength(); // Skip context length
if (offset !== null) {
asnReader._offset = offset;
switch (asnReader.readOID()) {
case '1.2.840.10045.3.1.7':
// prime256v1/secp256r1
ret.fulltype = 'ecdsa-sha2-nistp256';
break;
case '1.3.132.0.34':
// secp384r1
ret.fulltype = 'ecdsa-sha2-nistp384';
break;
return new Error(
'EC private keys are not supported in this version of node'
);
}
if (!RE_HEADER_OPENSSH.test(data[1])) {
// unencrypted, no headers
var privData = new Buffer(data.slice(1, -1).join(''), 'base64');
if (keyType !== 'ec') {
ret.fulltype = 'ssh-' + keyType;
} else {
// ECDSA
var asnReader = new Ber.Reader(privData);
asnReader.readSequence();
asnReader.readInt();
asnReader.readString(Ber.OctetString, true);
asnReader.readByte(); // Skip "complex" context type byte
var offset = asnReader.readLength(); // Skip context length
if (offset !== null) {
asnReader._offset = offset;
switch (asnReader.readOID()) {
case '1.2.840.10045.3.1.7':
// prime256v1/secp256r1
ret.fulltype = 'ecdsa-sha2-nistp256';
break;
case '1.3.132.0.34':
// secp384r1
ret.fulltype = 'ecdsa-sha2-nistp384';
break;
case '1.3.132.0.35':
// secp521r1
ret.fulltype = 'ecdsa-sha2-nistp521';
const keys = cacheKey.split("#");
const issuerNameHash = Buffer.from(keys[0], 'base64');
const issuerKeyHash = Buffer.from(keys[1], 'base64');
const serialNumber = new bn(keys[2], 10);
const certID = {
hashAlgorithm: {
// algorithm: [ 2, 16, 840, 1, 101, 3, 4, 2, 1 ] // sha256
algorithm: [1, 3, 14, 3, 2, 26] // sha1
},
issuerNameHash: issuerNameHash,
issuerKeyHash: issuerKeyHash,
serialNumber: serialNumber
};
const certIDDer = rfc2560.CertID.encode(certID, 'der');
return certIDDer.toString("BASE64");
};
exports.decodeKey = decodeKey;
});
text = text.slice(1, -1).join('');
return new Buffer(text.replace(/[^\w\d\+\/=]+/g, ''), 'base64');
}
var ECDSAPublicKey = asn.define('ECDSAPublicKey', function () {
this.seq().obj(
/*this.key('ECpoint').octstr()
/*
// TODO Figure out this format/sequence
this.key('x').int(),
this.key('y').int()*/
)
})
var AlgorithmIdentifier = asn.define('AlgorithmIdentifier', function () {
this.seq().obj(
this.key('algorithm').objid(),
this.key('parameters').optional().any()
)
})
var PublicKeyInfo = asn.define('PublicKeyInfo', function () {
this.seq().obj(
this.key('algorithm').use(AlgorithmIdentifier),
this.key('subjectPublicKey').bitstr()
)
})
var Version = asn.define('Version', function () {
this.int({
0: 'two-prime',