Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 260x 260x 260x 260x 260x 260x 19x 260x 780x 260x 260x 260x 92x 92x 92x 92x 92x 92x 92x 92x 7x 92x 64x 64x 64x 64x 24x 24x 24x 24x 24x 24x 179x 179x 6x 6x 179x 73x 73x 73x 24x 24x 49x 49x 73x 73x 488x 73x 168x 168x 56x 56x 56x 112x 112x 112x 168x | import * as asn1js from "asn1js";
import { getParametersValue } from "pvutils";
import AlgorithmIdentifier from "./AlgorithmIdentifier.js";
import Attribute from "./Attribute.js";
import ECPrivateKey from "./ECPrivateKey.js";
import RSAPrivateKey from "./RSAPrivateKey.js";
//**************************************************************************************
/**
* Class from RFC5208
*/
export default class PrivateKeyInfo
{
//**********************************************************************************
/**
* Constructor for PrivateKeyInfo class
* @param {Object} [parameters={}]
* @property {Object} [schema] asn1js parsed value
*/
constructor(parameters = {})
{
//region Internal properties of the object
/**
* @type {number}
* @description version
*/
this.version = getParametersValue(parameters, "version", PrivateKeyInfo.defaultValues("version"));
/**
* @type {AlgorithmIdentifier}
* @description privateKeyAlgorithm
*/
this.privateKeyAlgorithm = getParametersValue(parameters, "privateKeyAlgorithm", PrivateKeyInfo.defaultValues("privateKeyAlgorithm"));
/**
* @type {OctetString}
* @description privateKey
*/
this.privateKey = getParametersValue(parameters, "privateKey", PrivateKeyInfo.defaultValues("privateKey"));
Iif("attributes" in parameters)
/**
* @type {Array.<Attribute>}
* @description attributes
*/
this.attributes = getParametersValue(parameters, "attributes", PrivateKeyInfo.defaultValues("attributes"));
Iif("parsedKey" in parameters)
/**
* @type {ECPrivateKey|RSAPrivateKey}
* @description Parsed public key value
*/
this.parsedKey = getParametersValue(parameters, "parsedKey", PrivateKeyInfo.defaultValues("parsedKey"));
//endregion
//region If input argument array contains "schema" for this object
if("schema" in parameters)
this.fromSchema(parameters.schema);
//endregion
//region If input argument array contains "json" for this object
Iif("json" in parameters)
this.fromJSON(parameters.json);
//endregion
}
//**********************************************************************************
/**
* Return default values for all class members
* @param {string} memberName String name for a class member
*/
static defaultValues(memberName)
{
switch(memberName)
{
case "version":
return 0;
case "privateKeyAlgorithm":
return new AlgorithmIdentifier();
case "privateKey":
return new asn1js.OctetString();
case "attributes":
return [];
default:
throw new Error(`Invalid member name for PrivateKeyInfo class: ${memberName}`);
}
}
//**********************************************************************************
/**
* Return value of asn1js schema for current class
* @param {Object} parameters Input parameters for the schema
* @returns {Object} asn1js schema object
*/
static schema(parameters = {})
{
//PrivateKeyInfo ::= SEQUENCE {
// version Version,
// privateKeyAlgorithm AlgorithmIdentifier {{PrivateKeyAlgorithms}},
// privateKey PrivateKey,
// attributes [0] Attributes OPTIONAL }
//
//Version ::= INTEGER {v1(0)} (v1,...)
//
//PrivateKey ::= OCTET STRING
//
//Attributes ::= SET OF Attribute
/**
* @type {Object}
* @property {string} [blockName]
* @property {string} [version]
* @property {string} [privateKeyAlgorithm]
* @property {string} [privateKey]
* @property {string} [attributes]
*/
const names = getParametersValue(parameters, "names", {});
return (new asn1js.Sequence({
name: (names.blockName || ""),
value: [
new asn1js.Integer({ name: (names.version || "") }),
AlgorithmIdentifier.schema(names.privateKeyAlgorithm || {}),
new asn1js.OctetString({ name: (names.privateKey || "") }),
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [
new asn1js.Repeated({
name: (names.attributes || ""),
value: Attribute.schema()
})
]
})
]
}));
}
//**********************************************************************************
/**
* Convert parsed asn1js object into current class
* @param {!Object} schema
*/
fromSchema(schema)
{
//region Check the schema is valid
const asn1 = asn1js.compareSchema(schema,
schema,
PrivateKeyInfo.schema({
names: {
version: "version",
privateKeyAlgorithm: {
names: {
blockName: "privateKeyAlgorithm"
}
},
privateKey: "privateKey",
attributes: "attributes"
}
})
);
Iif(asn1.verified === false)
throw new Error("Object's schema was not verified against input data for PKCS8");
//endregion
//region Get internal properties from parsed schema
this.version = asn1.result.version.valueBlock.valueDec;
this.privateKeyAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.privateKeyAlgorithm });
this.privateKey = asn1.result.privateKey;
if("attributes" in asn1.result)
this.attributes = Array.from(asn1.result.attributes, element => new Attribute({ schema: element }));
switch(this.privateKeyAlgorithm.algorithmId)
{
case "1.2.840.113549.1.1.1": // RSA
{
const privateKeyASN1 = asn1js.fromBER(this.privateKey.valueBlock.valueHex);
Eif(privateKeyASN1.offset !== (-1))
this.parsedKey = new RSAPrivateKey({ schema: privateKeyASN1.result });
}
break;
case "1.2.840.10045.2.1": // ECDSA
Eif("algorithmParams" in this.privateKeyAlgorithm)
{
Eif(this.privateKeyAlgorithm.algorithmParams instanceof asn1js.ObjectIdentifier)
{
const privateKeyASN1 = asn1js.fromBER(this.privateKey.valueBlock.valueHex);
Eif(privateKeyASN1.offset !== (-1))
{
this.parsedKey = new ECPrivateKey({
namedCurve: this.privateKeyAlgorithm.algorithmParams.valueBlock.toString(),
schema: privateKeyASN1.result
});
}
}
}
break;
default:
}
//endregion
}
//**********************************************************************************
/**
* Convert current object to asn1js object and set correct values
* @returns {Object} asn1js object
*/
toSchema()
{
//region Create array for output sequence
const outputArray = [
new asn1js.Integer({ value: this.version }),
this.privateKeyAlgorithm.toSchema(),
this.privateKey
];
if("attributes" in this)
{
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: Array.from(this.attributes, element => element.toSchema())
}));
}
//endregion
//region Construct and return new ASN.1 schema for this object
return (new asn1js.Sequence({
value: outputArray
}));
//endregion
}
//**********************************************************************************
/**
* Convertion for the class to JSON object
* @returns {Object}
*/
toJSON()
{
//region Return common value in case we do not have enough info fo making JWK
Iif(("parsedKey" in this) === false)
{
const object = {
version: this.version,
privateKeyAlgorithm: this.privateKeyAlgorithm.toJSON(),
privateKey: this.privateKey.toJSON()
};
if("attributes" in this)
object.attributes = Array.from(this.attributes, element => element.toJSON());
return object;
}
//endregion
//region Making JWK
const jwk = {};
switch(this.privateKeyAlgorithm.algorithmId)
{
case "1.2.840.10045.2.1": // ECDSA
jwk.kty = "EC";
break;
case "1.2.840.113549.1.1.1": // RSA
jwk.kty = "RSA";
break;
default:
}
const publicKeyJWK = this.parsedKey.toJSON();
for(const key of Object.keys(publicKeyJWK))
jwk[key] = publicKeyJWK[key];
return jwk;
//endregion
}
//**********************************************************************************
/**
* Convert JSON value into current object
* @param {Object} json
*/
fromJSON(json)
{
Eif("kty" in json)
{
switch(json.kty.toUpperCase())
{
case "EC":
this.parsedKey = new ECPrivateKey({ json });
this.privateKeyAlgorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new asn1js.ObjectIdentifier({ value: this.parsedKey.namedCurve })
});
break;
case "RSA":
this.parsedKey = new RSAPrivateKey({ json });
this.privateKeyAlgorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.113549.1.1.1",
algorithmParams: new asn1js.Null()
});
break;
default:
throw new Error(`Invalid value for \"kty\" parameter: ${json.kty}`);
}
this.privateKey = new asn1js.OctetString({ valueHex: this.parsedKey.toSchema().toBER(false) });
}
}
//**********************************************************************************
}
//**************************************************************************************
|