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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | 1980x 1980x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 1763x 23257x 2076x 2497x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 2076x 1980x 1980x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 1763x 2x 1763x 4x 1763x 7363x 1763x 1763x 421x 421x 421x 421x 421x 421x 421x 421x 421x 421x 421x 421x 421x 421x 652x 421x 274x 274x 21x 21x 253x 274x 74x 74x 74x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 74x 74x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 700x 700x 700x 700x 700x 468x 232x 232x 700x 700x 700x 700x 700x 700x 700x 8x 692x 700x 700x 700x 700x 692x 700x 8x 8x 8x 8x 8x 8x 8x 8x 8x 700x 700x 700x 700x 700x 700x 700x 700x 700x 700x 8x 8x 700x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 700x 700x | import * as asn1js from "asn1js";
import { getParametersValue, bufferToHexCodes } from "pvutils";
import {
getCrypto,
getEngine,
getHashAlgorithm,
getAlgorithmByOID,
createECDSASignatureFromCMS,
getAlgorithmParameters
} from "./common.js";
import AlgorithmIdentifier from "./AlgorithmIdentifier.js";
import RelativeDistinguishedNames from "./RelativeDistinguishedNames.js";
import Time from "./Time.js";
import PublicKeyInfo from "./PublicKeyInfo.js";
import Extension from "./Extension.js";
import Extensions from "./Extensions.js";
import RSASSAPSSParams from "./RSASSAPSSParams.js";
//**************************************************************************************
function tbsCertificate(parameters = {})
{
//TBSCertificate ::= SEQUENCE {
// version [0] EXPLICIT Version DEFAULT v1,
// serialNumber CertificateSerialNumber,
// signature AlgorithmIdentifier,
// issuer Name,
// validity Validity,
// subject Name,
// subjectPublicKeyInfo SubjectPublicKeyInfo,
// issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
// -- If present, version MUST be v2 or v3
// subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
// -- If present, version MUST be v2 or v3
// extensions [3] EXPLICIT Extensions OPTIONAL
// -- If present, version MUST be v3
//}
/**
* @type {Object}
* @property {string} [blockName]
* @property {string} [tbsCertificateVersion]
* @property {string} [tbsCertificateSerialNumber]
* @property {string} [signature]
* @property {string} [issuer]
* @property {string} [tbsCertificateValidity]
* @property {string} [notBefore]
* @property {string} [notAfter]
* @property {string} [subject]
* @property {string} [subjectPublicKeyInfo]
* @property {string} [tbsCertificateIssuerUniqueID]
* @property {string} [tbsCertificateSubjectUniqueID]
* @property {string} [extensions]
*/
const names = getParametersValue(parameters, "names", {});
return (new asn1js.Sequence({
name: (names.blockName || "tbsCertificate"),
value: [
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [
new asn1js.Integer({ name: (names.tbsCertificateVersion || "tbsCertificate.version") }) // EXPLICIT integer value
]
}),
new asn1js.Integer({ name: (names.tbsCertificateSerialNumber || "tbsCertificate.serialNumber") }),
AlgorithmIdentifier.schema(names.signature || {
names: {
blockName: "tbsCertificate.signature"
}
}),
RelativeDistinguishedNames.schema(names.issuer || {
names: {
blockName: "tbsCertificate.issuer"
}
}),
new asn1js.Sequence({
name: (names.tbsCertificateValidity || "tbsCertificate.validity"),
value: [
Time.schema(names.notBefore || {
names: {
utcTimeName: "tbsCertificate.notBefore",
generalTimeName: "tbsCertificate.notBefore"
}
}),
Time.schema(names.notAfter || {
names: {
utcTimeName: "tbsCertificate.notAfter",
generalTimeName: "tbsCertificate.notAfter"
}
})
]
}),
RelativeDistinguishedNames.schema(names.subject || {
names: {
blockName: "tbsCertificate.subject"
}
}),
PublicKeyInfo.schema(names.subjectPublicKeyInfo || {
names: {
blockName: "tbsCertificate.subjectPublicKeyInfo"
}
}),
new asn1js.Primitive({
name: (names.tbsCertificateIssuerUniqueID || "tbsCertificate.issuerUniqueID"),
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
}
}), // IMPLICIT bistring value
new asn1js.Primitive({
name: (names.tbsCertificateSubjectUniqueID || "tbsCertificate.subjectUniqueID"),
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 2 // [2]
}
}), // IMPLICIT bistring value
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 3 // [3]
},
value: [Extensions.schema(names.extensions || {
names: {
blockName: "tbsCertificate.extensions"
}
})]
}) // EXPLICIT SEQUENCE value
]
}));
}
//**************************************************************************************
/**
* Class from RFC5280
*/
export default class Certificate
{
//**********************************************************************************
/**
* Constructor for Certificate class
* @param {Object} [parameters={}]
* @property {Object} [schema] asn1js parsed value
*/
constructor(parameters = {})
{
//region Internal properties of the object
/**
* @type {ArrayBuffer}
* @description tbs
*/
this.tbs = getParametersValue(parameters, "tbs", Certificate.defaultValues("tbs"));
/**
* @type {number}
* @description version
*/
this.version = getParametersValue(parameters, "version", Certificate.defaultValues("version"));
/**
* @type {Integer}
* @description serialNumber
*/
this.serialNumber = getParametersValue(parameters, "serialNumber", Certificate.defaultValues("serialNumber"));
/**
* @type {AlgorithmIdentifier}
* @description signature
*/
this.signature = getParametersValue(parameters, "signature", Certificate.defaultValues("signature"));
/**
* @type {RelativeDistinguishedNames}
* @description issuer
*/
this.issuer = getParametersValue(parameters, "issuer", Certificate.defaultValues("issuer"));
/**
* @type {Time}
* @description notBefore
*/
this.notBefore = getParametersValue(parameters, "notBefore", Certificate.defaultValues("notBefore"));
/**
* @type {Time}
* @description notAfter
*/
this.notAfter = getParametersValue(parameters, "notAfter", Certificate.defaultValues("notAfter"));
/**
* @type {RelativeDistinguishedNames}
* @description subject
*/
this.subject = getParametersValue(parameters, "subject", Certificate.defaultValues("subject"));
/**
* @type {PublicKeyInfo}
* @description subjectPublicKeyInfo
*/
this.subjectPublicKeyInfo = getParametersValue(parameters, "subjectPublicKeyInfo", Certificate.defaultValues("subjectPublicKeyInfo"));
Iif("issuerUniqueID" in parameters)
/**
* @type {ArrayBuffer}
* @description issuerUniqueID
*/
this.issuerUniqueID = getParametersValue(parameters, "issuerUniqueID", Certificate.defaultValues("issuerUniqueID"));
Iif("subjectUniqueID" in parameters)
/**
* @type {ArrayBuffer}
* @description subjectUniqueID
*/
this.subjectUniqueID = getParametersValue(parameters, "subjectUniqueID", Certificate.defaultValues("subjectUniqueID"));
Iif("extensions" in parameters)
/**
* @type {Array}
* @description extensions
*/
this.extensions = getParametersValue(parameters, "extensions", Certificate.defaultValues("extensions"));
/**
* @type {AlgorithmIdentifier}
* @description signatureAlgorithm
*/
this.signatureAlgorithm = getParametersValue(parameters, "signatureAlgorithm", Certificate.defaultValues("signatureAlgorithm"));
/**
* @type {BitString}
* @description signatureValue
*/
this.signatureValue = getParametersValue(parameters, "signatureValue", Certificate.defaultValues("signatureValue"));
//endregion
//region If input argument array contains "schema" for this object
if("schema" in parameters)
this.fromSchema(parameters.schema);
//endregion
}
//**********************************************************************************
/**
* Return default values for all class members
* @param {string} memberName String name for a class member
*/
static defaultValues(memberName)
{
switch(memberName)
{
case "tbs":
return new ArrayBuffer(0);
case "version":
return 0;
case "serialNumber":
return new asn1js.Integer();
case "signature":
return new AlgorithmIdentifier();
case "issuer":
return new RelativeDistinguishedNames();
case "notBefore":
return new Time();
case "notAfter":
return new Time();
case "subject":
return new RelativeDistinguishedNames();
case "subjectPublicKeyInfo":
return new PublicKeyInfo();
case "issuerUniqueID":
return new ArrayBuffer(0);
case "subjectUniqueID":
return new ArrayBuffer(0);
case "extensions":
return [];
case "signatureAlgorithm":
return new AlgorithmIdentifier();
case "signatureValue":
return new asn1js.BitString();
default:
throw new Error(`Invalid member name for Certificate 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 = {})
{
//Certificate ::= SEQUENCE {
// tbsCertificate TBSCertificate,
// signatureAlgorithm AlgorithmIdentifier,
// signatureValue BIT STRING }
/**
* @type {Object}
* @property {string} [blockName]
* @property {string} [tbsCertificate]
* @property {string} [signatureAlgorithm]
* @property {string} [signatureValue]
*/
const names = getParametersValue(parameters, "names", {});
return (new asn1js.Sequence({
name: (names.blockName || ""),
value: [
tbsCertificate(names.tbsCertificate),
AlgorithmIdentifier.schema(names.signatureAlgorithm || {
names: {
blockName: "signatureAlgorithm"
}
}),
new asn1js.BitString({ name: (names.signatureValue || "signatureValue") })
]
}));
}
//**********************************************************************************
/**
* Convert parsed asn1js object into current class
* @param {!Object} schema
*/
fromSchema(schema)
{
//region Check the schema is valid
const asn1 = asn1js.compareSchema(schema,
schema,
Certificate.schema({
names: {
tbsCertificate: {
names: {
extensions: {
names: {
extensions: "tbsCertificate.extensions"
}
}
}
}
}
})
);
Iif(asn1.verified === false)
throw new Error("Object's schema was not verified against input data for CERT");
//endregion
//region Get internal properties from parsed schema
this.tbs = asn1.result.tbsCertificate.valueBeforeDecode;
Eif("tbsCertificate.version" in asn1.result)
this.version = asn1.result["tbsCertificate.version"].valueBlock.valueDec;
this.serialNumber = asn1.result["tbsCertificate.serialNumber"];
this.signature = new AlgorithmIdentifier({ schema: asn1.result["tbsCertificate.signature"] });
this.issuer = new RelativeDistinguishedNames({ schema: asn1.result["tbsCertificate.issuer"] });
this.notBefore = new Time({ schema: asn1.result["tbsCertificate.notBefore"] });
this.notAfter = new Time({ schema: asn1.result["tbsCertificate.notAfter"] });
this.subject = new RelativeDistinguishedNames({ schema: asn1.result["tbsCertificate.subject"] });
this.subjectPublicKeyInfo = new PublicKeyInfo({ schema: asn1.result["tbsCertificate.subjectPublicKeyInfo"] });
if("tbsCertificate.issuerUniqueID" in asn1.result)
this.issuerUniqueID = asn1.result["tbsCertificate.issuerUniqueID"].valueBlock.valueHex;
if("tbsCertificate.subjectUniqueID" in asn1.result)
this.issuerUniqueID = asn1.result["tbsCertificate.subjectUniqueID"].valueBlock.valueHex;
Eif("tbsCertificate.extensions" in asn1.result)
this.extensions = Array.from(asn1.result["tbsCertificate.extensions"], element => new Extension({ schema: element }));
this.signatureAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.signatureAlgorithm });
this.signatureValue = asn1.result.signatureValue;
//endregion
}
//**********************************************************************************
/**
* Create ASN.1 schema for existing values of TBS part for the certificate
*/
encodeTBS()
{
//region Create array for output sequence
const outputArray = [];
Eif(("version" in this) && (this.version !== Certificate.defaultValues("version")))
{
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [
new asn1js.Integer({ value: this.version }) // EXPLICIT integer value
]
}));
}
outputArray.push(this.serialNumber);
outputArray.push(this.signature.toSchema());
outputArray.push(this.issuer.toSchema());
outputArray.push(new asn1js.Sequence({
value: [
this.notBefore.toSchema(),
this.notAfter.toSchema()
]
}));
outputArray.push(this.subject.toSchema());
outputArray.push(this.subjectPublicKeyInfo.toSchema());
Iif("issuerUniqueID" in this)
{
outputArray.push(new asn1js.Primitive({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
},
valueHex: this.issuerUniqueID
}));
}
Iif("subjectUniqueID" in this)
{
outputArray.push(new asn1js.Primitive({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 2 // [2]
},
valueHex: this.subjectUniqueID
}));
}
Iif("subjectUniqueID" in this)
{
outputArray.push(new asn1js.Primitive({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 3 // [3]
},
value: [this.extensions.toSchema()]
}));
}
Eif("extensions" in this)
{
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 3 // [3]
},
value: [new asn1js.Sequence({
value: Array.from(this.extensions, element => element.toSchema())
})]
}));
}
//endregion
//region Create and return output sequence
return (new asn1js.Sequence({
value: outputArray
}));
//endregion
}
//**********************************************************************************
/**
* Convert current object to asn1js object and set correct values
* @returns {Object} asn1js object
*/
toSchema(encodeFlag = false)
{
let tbsSchema = {};
//region Decode stored TBS value
if(encodeFlag === false)
{
Iif(this.tbs.length === 0) // No stored certificate TBS part
return Certificate.schema().value[0];
tbsSchema = asn1js.fromBER(this.tbs).result;
}
//endregion
//region Create TBS schema via assembling from TBS parts
else
tbsSchema = this.encodeTBS();
//endregion
//region Construct and return new ASN.1 schema for this object
return (new asn1js.Sequence({
value: [
tbsSchema,
this.signatureAlgorithm.toSchema(),
this.signatureValue
]
}));
//endregion
}
//**********************************************************************************
/**
* Convertion for the class to JSON object
* @returns {Object}
*/
toJSON()
{
const object = {
tbs: bufferToHexCodes(this.tbs, 0, this.tbs.byteLength),
serialNumber: this.serialNumber.toJSON(),
signature: this.signature.toJSON(),
issuer: this.issuer.toJSON(),
notBefore: this.notBefore.toJSON(),
notAfter: this.notAfter.toJSON(),
subject: this.subject.toJSON(),
subjectPublicKeyInfo: this.subjectPublicKeyInfo.toJSON(),
signatureAlgorithm: this.signatureAlgorithm.toJSON(),
signatureValue: this.signatureValue.toJSON()
};
if(("version" in this) && (this.version !== Certificate.defaultValues("version")))
object.version = this.version;
if("issuerUniqueID" in this)
object.issuerUniqueID = bufferToHexCodes(this.issuerUniqueID, 0, this.issuerUniqueID.byteLength);
if("subjectUniqueID" in this)
object.subjectUniqueID = bufferToHexCodes(this.subjectUniqueID, 0, this.subjectUniqueID.byteLength);
if("extensions" in this)
object.extensions = Array.from(this.extensions, element => element.toJSON());
return object;
}
//**********************************************************************************
/**
* Importing public key for current certificate
*/
getPublicKey(parameters = null)
{
//region Get a "crypto" extension
const crypto = getCrypto();
Iif(typeof crypto === "undefined")
return Promise.reject("Unable to create WebCrypto object");
//endregion
//region Find correct algorithm for imported public key
if(parameters === null)
{
//region Initial variables
parameters = {};
//endregion
//region Find signer's hashing algorithm
const shaAlgorithm = getHashAlgorithm(this.signatureAlgorithm);
Iif(shaAlgorithm === "")
return Promise.reject(`Unsupported signature algorithm: ${this.signatureAlgorithm.algorithmId}`);
//endregion
//region Get information about public key algorithm and default parameters for import
const algorithmObject = getAlgorithmByOID(this.subjectPublicKeyInfo.algorithm.algorithmId);
Iif(("name" in algorithmObject) === false)
return Promise.reject(`Unsupported public key algorithm: ${this.subjectPublicKeyInfo.algorithm.algorithmId}`);
parameters.algorithm = getAlgorithmParameters(algorithmObject.name, "importkey");
Eif("hash" in parameters.algorithm.algorithm)
parameters.algorithm.algorithm.hash.name = shaAlgorithm;
//region Special case for ECDSA
Iif(algorithmObject.name === "ECDSA")
{
//region Get information about named curve
let algorithmParamsChecked = false;
if(("algorithmParams" in this.subjectPublicKeyInfo.algorithm) === true)
{
if("idBlock" in this.subjectPublicKeyInfo.algorithm.algorithmParams)
{
if((this.subjectPublicKeyInfo.algorithm.algorithmParams.idBlock.tagClass === 1) && (this.subjectPublicKeyInfo.algorithm.algorithmParams.idBlock.tagNumber === 6))
algorithmParamsChecked = true;
}
}
if(algorithmParamsChecked === false)
return Promise.reject("Incorrect type for ECDSA public key parameters");
const curveObject = getAlgorithmByOID(this.subjectPublicKeyInfo.algorithm.algorithmParams.valueBlock.toString());
if(("name" in curveObject) === false)
return Promise.reject(`Unsupported named curve algorithm: ${this.subjectPublicKeyInfo.algorithm.algorithmParams.valueBlock.toString()}`);
//endregion
parameters.algorithm.algorithm.namedCurve = curveObject.name;
}
//endregion
//endregion
}
//endregion
//region Get neccessary values from internal fields for current certificate
const publicKeyInfoSchema = this.subjectPublicKeyInfo.toSchema();
const publicKeyInfoBuffer = publicKeyInfoSchema.toBER(false);
const publicKeyInfoView = new Uint8Array(publicKeyInfoBuffer);
//endregion
return crypto.importKey("spki", publicKeyInfoView, parameters.algorithm.algorithm, true, parameters.algorithm.usages);
}
//**********************************************************************************
/**
* Get SHA-1 hash value for subject public key
*/
getKeyHash()
{
//region Get a "crypto" extension
const crypto = getCrypto();
if(typeof crypto === "undefined")
return Promise.reject("Unable to create WebCrypto object");
//endregion
return crypto.digest({ name: "sha-1" }, new Uint8Array(this.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex));
}
//**********************************************************************************
/**
* Make a signature for current value from TBS section
* @param {Object} privateKey Private key for "subjectPublicKeyInfo" structure
* @param {string} [hashAlgorithm="SHA-1"] Hashing algorithm
*/
sign(privateKey, hashAlgorithm = "SHA-1")
{
//region Initial checking
//region Check private key
Iif(typeof privateKey === "undefined")
return Promise.reject("Need to provide a private key for signing");
//endregion
//endregion
//region Initial variables
let sequence = Promise.resolve();
let parameters;
const engine = getEngine();
//endregion
//region Get a "default parameters" for current algorithm and set correct signature algorithm
sequence = sequence.then(() => engine.subtle.getSignatureParameters(privateKey, hashAlgorithm));
sequence = sequence.then(result =>
{
parameters = result.parameters;
this.signature = result.signatureAlgorithm;
this.signatureAlgorithm = result.signatureAlgorithm;
});
//endregion
//region Create TBS data for signing
sequence = sequence.then(() =>
{
this.tbs = this.encodeTBS().toBER(false);
});
//endregion
//region Signing TBS data on provided private key
sequence = sequence.then(() => engine.subtle.signWithPrivateKey(this.tbs, privateKey, parameters));
sequence = sequence.then(result =>
{
this.signatureValue = new asn1js.BitString({ valueHex: result });
});
//endregion
return sequence;
}
//**********************************************************************************
verify(issuerCertificate = null)
{
//region Global variables
let sequence = Promise.resolve();
let subjectPublicKeyInfo = {};
const signature = this.signatureValue;
const tbs = this.tbs;
//endregion
//region Set correct "subjectPublicKeyInfo" value
if(issuerCertificate !== null)
subjectPublicKeyInfo = issuerCertificate.subjectPublicKeyInfo;
else
{
Eif(this.issuer.isEqual(this.subject)) // Self-signed certificate
subjectPublicKeyInfo = this.subjectPublicKeyInfo;
}
Iif((subjectPublicKeyInfo instanceof PublicKeyInfo) === false)
return Promise.reject("Please provide issuer certificate as a parameter");
//endregion
//region Get a "crypto" extension
const crypto = getCrypto();
Iif(typeof crypto === "undefined")
return Promise.reject("Unable to create WebCrypto object");
//endregion
//region Find signer's hashing algorithm
const shaAlgorithm = getHashAlgorithm(this.signatureAlgorithm);
Iif(shaAlgorithm === "")
return Promise.reject(`Unsupported signature algorithm: ${this.signatureAlgorithm.algorithmId}`);
//endregion
//region Importing public key
sequence = sequence.then(() =>
{
//region Get information about public key algorithm and default parameters for import
let algorithmId;
if(this.signatureAlgorithm.algorithmId === "1.2.840.113549.1.1.10")
algorithmId = this.signatureAlgorithm.algorithmId;
else
algorithmId = subjectPublicKeyInfo.algorithm.algorithmId;
const algorithmObject = getAlgorithmByOID(algorithmId);
Iif(("name" in algorithmObject) === false)
return Promise.reject(`Unsupported public key algorithm: ${algorithmId}`);
const algorithm = getAlgorithmParameters(algorithmObject.name, "importkey");
if("hash" in algorithm.algorithm)
algorithm.algorithm.hash.name = shaAlgorithm;
//region Special case for ECDSA
if(algorithmObject.name === "ECDSA")
{
//region Get information about named curve
let algorithmParamsChecked = false;
Eif(("algorithmParams" in subjectPublicKeyInfo.algorithm) === true)
{
Eif("idBlock" in subjectPublicKeyInfo.algorithm.algorithmParams)
{
Eif((subjectPublicKeyInfo.algorithm.algorithmParams.idBlock.tagClass === 1) && (subjectPublicKeyInfo.algorithm.algorithmParams.idBlock.tagNumber === 6))
algorithmParamsChecked = true;
}
}
Iif(algorithmParamsChecked === false)
return Promise.reject("Incorrect type for ECDSA public key parameters");
const curveObject = getAlgorithmByOID(subjectPublicKeyInfo.algorithm.algorithmParams.valueBlock.toString());
Iif(("name" in curveObject) === false)
return Promise.reject(`Unsupported named curve algorithm: ${subjectPublicKeyInfo.algorithm.algorithmParams.valueBlock.toString()}`);
//endregion
algorithm.algorithm.namedCurve = curveObject.name;
}
//endregion
//endregion
const publicKeyInfoSchema = subjectPublicKeyInfo.toSchema();
const publicKeyInfoBuffer = publicKeyInfoSchema.toBER(false);
const publicKeyInfoView = new Uint8Array(publicKeyInfoBuffer);
return crypto.importKey("spki", publicKeyInfoView, algorithm.algorithm, true, algorithm.usages);
});
//endregion
//region Verify signature for the certificate
sequence = sequence.then(publicKey =>
{
//region Get default algorithm parameters for verification
const algorithm = getAlgorithmParameters(publicKey.algorithm.name, "verify");
Eif("hash" in algorithm.algorithm)
algorithm.algorithm.hash.name = shaAlgorithm;
//endregion
//region Special case for ECDSA signatures
let signatureValue = signature.valueBlock.valueHex;
if(publicKey.algorithm.name === "ECDSA")
{
const asn1 = asn1js.fromBER(signatureValue);
signatureValue = createECDSASignatureFromCMS(asn1.result);
}
//endregion
//region Special case for RSA-PSS
if(publicKey.algorithm.name === "RSA-PSS")
{
let pssParameters;
try
{
pssParameters = new RSASSAPSSParams({ schema: this.signatureAlgorithm.algorithmParams });
}
catch(ex)
{
return Promise.reject(ex);
}
Eif("saltLength" in pssParameters)
algorithm.algorithm.saltLength = pssParameters.saltLength;
else
algorithm.algorithm.saltLength = 20;
let hashAlgo = "SHA-1";
Eif("hashAlgorithm" in pssParameters)
{
const hashAlgorithm = getAlgorithmByOID(pssParameters.hashAlgorithm.algorithmId);
Iif(("name" in hashAlgorithm) === false)
return Promise.reject(`Unrecognized hash algorithm: ${pssParameters.hashAlgorithm.algorithmId}`);
hashAlgo = hashAlgorithm.name;
}
algorithm.algorithm.hash.name = hashAlgo;
}
//endregion
return crypto.verify(algorithm.algorithm,
publicKey,
new Uint8Array(signatureValue),
new Uint8Array(tbs)
);
});
//endregion
return sequence;
}
//**********************************************************************************
}
//**************************************************************************************
|