WIP more asn1

This commit is contained in:
AJ ONeal 2018-11-18 14:05:05 -07:00
parent 17a06bef18
commit 9adf912e88
1 changed files with 27 additions and 35 deletions

View File

@ -6,7 +6,7 @@ var crypto = require('crypto');
// prime256v1 (ANSI X9.62 named elliptic curve) // prime256v1 (ANSI X9.62 named elliptic curve)
var OBJ_ID_EC = '06 08 2A8648CE3D030107'.replace(/\s+/g, '').toLowerCase(); var OBJ_ID_EC = '06 08 2A8648CE3D030107'.replace(/\s+/g, '').toLowerCase();
function ANY() { function ASN1() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var typ = args.shift(); var typ = args.shift();
var str = args.join('').replace(/\s+/g, ''); var str = args.join('').replace(/\s+/g, '');
@ -33,27 +33,30 @@ function UINT() {
// high-order bit means signed, negative // high-order bit means signed, negative
// we want positive, so we pad with a leading '00' // we want positive, so we pad with a leading '00'
if (0x80 & first) { str = '00' + str; } if (0x80 & first) { str = '00' + str; }
return ANY('02', str); return ASN1('02', str);
} }
function BITSTR() { function BITSTR() {
var str = Array.prototype.slice.call(arguments).join(''); var str = Array.prototype.slice.call(arguments).join('');
var first = parseInt(str.slice(0, 2), 16);
// '00' is a mask of how many bits of the next byte to ignore // '00' is a mask of how many bits of the next byte to ignore
return ANY('03', '00' + str); return ASN1('03', '00' + str);
} }
function SEQ() { function SEQ() {
return ANY('30', Array.prototype.slice.call(arguments).join('')); return ASN1('30', Array.prototype.slice.call(arguments).join(''));
} }
/*
function SET() { function SET() {
return ANY('31', Array.prototype.slice.call(arguments).join('')); return ASN1('31', Array.prototype.slice.call(arguments).join(''));
} }
*/
function OBJID() { function OBJID() {
return ANY('06', Array.prototype.slice.call(arguments).join('')); return ASN1('06', Array.prototype.slice.call(arguments).join(''));
} }
/*
function NULL() { function NULL() {
return '0500'; return '0500';
} }
*/
function fromBase64(b64) { function fromBase64(b64) {
var buf; var buf;
@ -295,42 +298,31 @@ function createCsrBodyEc(domains, xy) {
// P-256 Public Key // P-256 Public Key
// #2 Total 2+25+xy // #2 Total 2+25+xy
, '30 {+25+xy}' // 2 bytes, sequence , ASN1('30'
.replace(/{[^}]+}/, numToHex(2+9+10+3+1+publen)) , ASN1('30'
, '30 13' // 2 bytes, sequence
// 1.2.840.10045.2.1 ecPublicKey // 1.2.840.10045.2.1 ecPublicKey
// (ANSI X9.62 public key type) // (ANSI X9.62 public key type)
, '06 07 2A 86 48 CE 3D 02 01' // 9 bytes, object id , ASN1('06', '2A 86 48 CE 3D 02 01')
// 1.2.840.10045.3.1.7 prime256v1 // 1.2.840.10045.3.1.7 prime256v1
// (ANSI X9.62 named elliptic curve) // (ANSI X9.62 named elliptic curve)
, '06 08 2A 86 48 CE 3D 03 01 07' // 10 bytes, object id , ASN1('06', '2A 86 48 CE 3D 03 01 07')
, '03 {xylen} 00 {xy}' // 3+1+n bytes )
.replace(/{xylen}/, numToHex(publen+2)) , BITSTR(compression + hxy)
.replace(/{xy}/, compression + hxy) )
// Altnames // Altnames
// #3 Total 2+28+n // #3 Total 2+28+n
, 'A0 {+28}' // 2 bytes, ?? [4B] , ASN1('A0'
.replace(/{[^}]+}/, numToHex(2+11+2+2+2+5+2+2+sanlen)) , ASN1('30'
, '30 {+26}' // 2 bytes, sequence
.replace(/{[^}]+}/, numToHex(11+2+2+2+5+2+2+sanlen))
// (extensionRequest (PKCS #9 via CRMF)) // (extensionRequest (PKCS #9 via CRMF))
, '06 09 2A 86 48 86 F7 0D 01 09 0E' // 11 bytes, object id , ASN1('06', '2A 86 48 86 F7 0D 01 09 0E')
, '31 {+13}' // 2 bytes, set , ASN1('31'
.replace(/{[^}]+}/, numToHex(2+2+5+2+2+sanlen)) , ASN1('30'
, '30 {+11}' // 2 bytes, sequence , ASN1('30'
.replace(/{[^}]+}/, numToHex(2+5+2+2+sanlen))
, '30 {+9}' // 2 bytes, sequence
.replace(/{[^}]+}/, numToHex(5+2+2+sanlen))
// (subjectAltName (X.509 extension)) // (subjectAltName (X.509 extension))
, '06 03 55 1D 11' // 5 bytes, object id , ASN1('06', '55 1D 11')
, '04 {+2}' // 2 bytes, octet string , ASN1('04'
.replace(/{[^}]+}/, numToHex(2+sanlen)) , ASN1('30', altnames))))))) ];
, '30 {+n}' // 2 bytes, sequence
.replace(/{[^}]+}/, numToHex(sanlen))
, '{altnames}' // n (elements of sequence)
.replace(/{altnames}/, altnames)
];
body = body.join('').replace(/\s+/g, ''); body = body.join('').replace(/\s+/g, '');
return fromHex(body); return fromHex(body);
} }