63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var ASN1 = require('./asn1-packer.js');
|
||
|
var Enc = require('./encoding.js');
|
||
|
var x509 = module.exports;
|
||
|
|
||
|
// 1.2.840.10045.3.1.7
|
||
|
// prime256v1 (ANSI X9.62 named elliptic curve)
|
||
|
var OBJ_ID_EC = '06 08 2A8648CE3D030107'.replace(/\s+/g, '').toLowerCase();
|
||
|
// 1.3.132.0.34
|
||
|
// secp384r1 (SECG (Certicom) named elliptic curve)
|
||
|
var OBJ_ID_EC_384 = '06 05 2B81040022'.replace(/\s+/g, '').toLowerCase();
|
||
|
// 1.2.840.10045.2.1
|
||
|
// ecPublicKey (ANSI X9.62 public key type)
|
||
|
var OBJ_ID_EC_PUB = '06 07 2A8648CE3D0201'.replace(/\s+/g, '').toLowerCase();
|
||
|
|
||
|
x509.packSec1 = function (jwk) {
|
||
|
var d = Enc.base64ToHex(jwk.d);
|
||
|
var x = Enc.base64ToHex(jwk.x);
|
||
|
var y = Enc.base64ToHex(jwk.y);
|
||
|
var objId = ('P-256' === jwk.crv) ? OBJ_ID_EC : OBJ_ID_EC_384;
|
||
|
return Enc.hexToUint8(
|
||
|
ASN1('30'
|
||
|
, ASN1.UInt('01')
|
||
|
, ASN1('04', d)
|
||
|
, ASN1('A0', objId)
|
||
|
, ASN1('A1', ASN1.BitStr('04' + x + y)))
|
||
|
);
|
||
|
};
|
||
|
x509.packPkcs8 = function (jwk) {
|
||
|
var d = Enc.base64ToHex(jwk.d);
|
||
|
var x = Enc.base64ToHex(jwk.x);
|
||
|
var y = Enc.base64ToHex(jwk.y);
|
||
|
var objId = ('P-256' === jwk.crv) ? OBJ_ID_EC : OBJ_ID_EC_384;
|
||
|
return Enc.hexToUint8(
|
||
|
ASN1('30'
|
||
|
, ASN1.UInt('00')
|
||
|
, ASN1('30'
|
||
|
, OBJ_ID_EC_PUB
|
||
|
, objId
|
||
|
)
|
||
|
, ASN1('04'
|
||
|
, ASN1('30'
|
||
|
, ASN1.UInt('01')
|
||
|
, ASN1('04', d)
|
||
|
, ASN1('A1', ASN1.BitStr('04' + x + y)))))
|
||
|
);
|
||
|
};
|
||
|
x509.packSpki = function (jwk) {
|
||
|
var x = Enc.base64ToHex(jwk.x);
|
||
|
var y = Enc.base64ToHex(jwk.y);
|
||
|
var objId = ('P-256' === jwk.crv) ? OBJ_ID_EC : OBJ_ID_EC_384;
|
||
|
return Enc.hexToUint8(
|
||
|
ASN1('30'
|
||
|
, ASN1('30'
|
||
|
, OBJ_ID_EC_PUB
|
||
|
, objId
|
||
|
)
|
||
|
, ASN1.BitStr('04' + x + y))
|
||
|
);
|
||
|
};
|
||
|
x509.packPkix = x509.packSpki;
|