mirror of
https://github.com/therootcompany/x509.js.git
synced 2025-03-14 04:20:49 +00:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
c089e05908 | |||
b051cb96b0 |
@ -8,8 +8,8 @@ and [Keypairs.js](https://git.rootprojects.org/root/keypairs.js)
|
|||||||
Lightweight, Zero-Dependency, x509 encoder and decoder for Node.js and Browsers
|
Lightweight, Zero-Dependency, x509 encoder and decoder for Node.js and Browsers
|
||||||
|
|
||||||
| 1.6k gzipped
|
| 1.6k gzipped
|
||||||
| 6.9k minified
|
| 6.8k minified
|
||||||
| 9.9k pretty
|
| 9.7k pretty
|
||||||
|
|
|
|
||||||
|
|
||||||
This provides a set ASN.1 / x509 schemas for DER encoding and decoding
|
This provides a set ASN.1 / x509 schemas for DER encoding and decoding
|
||||||
|
61
dist/x509.all.js
vendored
61
dist/x509.all.js
vendored
@ -536,7 +536,7 @@ X509.parsePkcs1 = function parseRsaPkcs1(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,24 +664,14 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
/*
|
|
||||||
console.log(
|
|
||||||
JSON.stringify(ASN1.parse({ der: asn1, verbose: true, json: false }), null, 2)
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
2 === asn1.children.length &&
|
2 === asn1.children.length &&
|
||||||
0x03 === asn1.children[1].type &&
|
0x03 === asn1.children[1].type // && 2 === asn1.children[1].children.length
|
||||||
0x30 === asn1.children[1].value[0]
|
|
||||||
) {
|
) {
|
||||||
asn1 = ASN1.parse({
|
asn1 = asn1.children[1].children[0];
|
||||||
der: asn1.children[1].value,
|
|
||||||
verbose: true,
|
|
||||||
json: false
|
|
||||||
});
|
|
||||||
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value);
|
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value);
|
||||||
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value);
|
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value);
|
||||||
jwk.kty = 'RSA';
|
jwk.kty = 'RSA';
|
||||||
@ -706,6 +696,7 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
'not an RSA PKCS#8 public or private key (wrong format)'
|
'not an RSA PKCS#8 public or private key (wrong format)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jwk;
|
return jwk;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -713,7 +704,6 @@ X509.parseSpki = function(buf, jwk) {
|
|||||||
try {
|
try {
|
||||||
return X509.parseRsaPkcs8(buf, jwk);
|
return X509.parseRsaPkcs8(buf, jwk);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//console.error(e);
|
|
||||||
return X509.parseEcSpki(buf, jwk);
|
return X509.parseEcSpki(buf, jwk);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -750,6 +740,47 @@ X509.parseEcSpki = function(u8, jwk) {
|
|||||||
|
|
||||||
X509.parsePkix = X509.parseSpki;
|
X509.parsePkix = X509.parseSpki;
|
||||||
|
|
||||||
|
// TODO look for ECDSA as well
|
||||||
|
X509._parseRsa = function(asn1) {
|
||||||
|
// accepting der for compatability with other usages
|
||||||
|
|
||||||
|
if (asn1.byteLength) {
|
||||||
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
var meta = { kty: 'RSA', format: 'pkcs1', public: true };
|
||||||
|
//meta.asn1 = ASN1.parse(u8);
|
||||||
|
|
||||||
|
if (
|
||||||
|
asn1.children.every(function(el) {
|
||||||
|
return 0x02 === el.type;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
if (2 === asn1.children.length) {
|
||||||
|
// rsa pkcs1 public
|
||||||
|
//return meta;
|
||||||
|
} else if (asn1.children.length >= 9) {
|
||||||
|
// the standard allows for "otherPrimeInfos", hence at least 9
|
||||||
|
meta.public = false;
|
||||||
|
// rsa pkcs1 private
|
||||||
|
//return meta;
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'not an RSA PKCS#1 public or private key (wrong number of ints)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
meta.format = 'pkcs8';
|
||||||
|
}
|
||||||
|
|
||||||
|
var jwk = { kty: 'RSA', n: null, e: null };
|
||||||
|
if ('pkcs1' === meta.format) {
|
||||||
|
return X509.parsePkcs1(asn1, jwk);
|
||||||
|
} else {
|
||||||
|
return X509.parsePkcs8(asn1, jwk);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// 1.2.840.10045.3.1.7
|
// 1.2.840.10045.3.1.7
|
||||||
// prime256v1 (ANSI X9.62 named elliptic curve)
|
// prime256v1 (ANSI X9.62 named elliptic curve)
|
||||||
|
2
dist/x509.all.min.js
vendored
2
dist/x509.all.min.js
vendored
File diff suppressed because one or more lines are too long
61
dist/x509.js
vendored
61
dist/x509.js
vendored
@ -18,7 +18,7 @@ X509.parsePkcs1 = function parseRsaPkcs1(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,24 +146,14 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
/*
|
|
||||||
console.log(
|
|
||||||
JSON.stringify(ASN1.parse({ der: asn1, verbose: true, json: false }), null, 2)
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
2 === asn1.children.length &&
|
2 === asn1.children.length &&
|
||||||
0x03 === asn1.children[1].type &&
|
0x03 === asn1.children[1].type // && 2 === asn1.children[1].children.length
|
||||||
0x30 === asn1.children[1].value[0]
|
|
||||||
) {
|
) {
|
||||||
asn1 = ASN1.parse({
|
asn1 = asn1.children[1].children[0];
|
||||||
der: asn1.children[1].value,
|
|
||||||
verbose: true,
|
|
||||||
json: false
|
|
||||||
});
|
|
||||||
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value);
|
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value);
|
||||||
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value);
|
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value);
|
||||||
jwk.kty = 'RSA';
|
jwk.kty = 'RSA';
|
||||||
@ -188,6 +178,7 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
'not an RSA PKCS#8 public or private key (wrong format)'
|
'not an RSA PKCS#8 public or private key (wrong format)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jwk;
|
return jwk;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -195,7 +186,6 @@ X509.parseSpki = function(buf, jwk) {
|
|||||||
try {
|
try {
|
||||||
return X509.parseRsaPkcs8(buf, jwk);
|
return X509.parseRsaPkcs8(buf, jwk);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//console.error(e);
|
|
||||||
return X509.parseEcSpki(buf, jwk);
|
return X509.parseEcSpki(buf, jwk);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -232,6 +222,47 @@ X509.parseEcSpki = function(u8, jwk) {
|
|||||||
|
|
||||||
X509.parsePkix = X509.parseSpki;
|
X509.parsePkix = X509.parseSpki;
|
||||||
|
|
||||||
|
// TODO look for ECDSA as well
|
||||||
|
X509._parseRsa = function(asn1) {
|
||||||
|
// accepting der for compatability with other usages
|
||||||
|
|
||||||
|
if (asn1.byteLength) {
|
||||||
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
var meta = { kty: 'RSA', format: 'pkcs1', public: true };
|
||||||
|
//meta.asn1 = ASN1.parse(u8);
|
||||||
|
|
||||||
|
if (
|
||||||
|
asn1.children.every(function(el) {
|
||||||
|
return 0x02 === el.type;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
if (2 === asn1.children.length) {
|
||||||
|
// rsa pkcs1 public
|
||||||
|
//return meta;
|
||||||
|
} else if (asn1.children.length >= 9) {
|
||||||
|
// the standard allows for "otherPrimeInfos", hence at least 9
|
||||||
|
meta.public = false;
|
||||||
|
// rsa pkcs1 private
|
||||||
|
//return meta;
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'not an RSA PKCS#1 public or private key (wrong number of ints)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
meta.format = 'pkcs8';
|
||||||
|
}
|
||||||
|
|
||||||
|
var jwk = { kty: 'RSA', n: null, e: null };
|
||||||
|
if ('pkcs1' === meta.format) {
|
||||||
|
return X509.parsePkcs1(asn1, jwk);
|
||||||
|
} else {
|
||||||
|
return X509.parsePkcs8(asn1, jwk);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// 1.2.840.10045.3.1.7
|
// 1.2.840.10045.3.1.7
|
||||||
// prime256v1 (ANSI X9.62 named elliptic curve)
|
// prime256v1 (ANSI X9.62 named elliptic curve)
|
||||||
|
2
dist/x509.min.js
vendored
2
dist/x509.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@root/x509",
|
"name": "@root/x509",
|
||||||
"version": "0.7.0",
|
"version": "0.7.2",
|
||||||
"description": "VanillaJS, Lightweight, Zero-Dependency, X509 schema encoder and decoder.",
|
"description": "VanillaJS, Lightweight, Zero-Dependency, X509 schema encoder and decoder.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"browser": {
|
"browser": {
|
||||||
|
46
parsers.js
46
parsers.js
@ -17,7 +17,7 @@ X509.parsePkcs1 = function parseRsaPkcs1(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might be a buffer
|
// might be a buffer
|
||||||
if (!Array.isArray(asn1)) {
|
if (asn1.byteLength) {
|
||||||
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
@ -177,6 +177,7 @@ X509.parseRsaPkcs8 = function parseRsaPkcs8(asn1, jwk) {
|
|||||||
'not an RSA PKCS#8 public or private key (wrong format)'
|
'not an RSA PKCS#8 public or private key (wrong format)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jwk;
|
return jwk;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -219,3 +220,44 @@ X509.parseEcSpki = function(u8, jwk) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
X509.parsePkix = X509.parseSpki;
|
X509.parsePkix = X509.parseSpki;
|
||||||
|
|
||||||
|
// TODO look for ECDSA as well
|
||||||
|
X509._parseRsa = function(asn1) {
|
||||||
|
// accepting der for compatability with other usages
|
||||||
|
|
||||||
|
if (asn1.byteLength) {
|
||||||
|
asn1 = ASN1.parse({ der: asn1, verbose: true, json: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
var meta = { kty: 'RSA', format: 'pkcs1', public: true };
|
||||||
|
//meta.asn1 = ASN1.parse(u8);
|
||||||
|
|
||||||
|
if (
|
||||||
|
asn1.children.every(function(el) {
|
||||||
|
return 0x02 === el.type;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
if (2 === asn1.children.length) {
|
||||||
|
// rsa pkcs1 public
|
||||||
|
//return meta;
|
||||||
|
} else if (asn1.children.length >= 9) {
|
||||||
|
// the standard allows for "otherPrimeInfos", hence at least 9
|
||||||
|
meta.public = false;
|
||||||
|
// rsa pkcs1 private
|
||||||
|
//return meta;
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'not an RSA PKCS#1 public or private key (wrong number of ints)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
meta.format = 'pkcs8';
|
||||||
|
}
|
||||||
|
|
||||||
|
var jwk = { kty: 'RSA', n: null, e: null };
|
||||||
|
if ('pkcs1' === meta.format) {
|
||||||
|
return X509.parsePkcs1(asn1, jwk);
|
||||||
|
} else {
|
||||||
|
return X509.parsePkcs8(asn1, jwk);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user