All files / examples/NodePKCS12Example NodeEngine.js

63.04% Statements 162/257
42.21% Branches 65/154
94.44% Functions 17/18
63.04% Lines 162/257

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                            1x             1x         1x         1x                     38x 38x         23x               4x                             4x                 3x       4x                                     8x                                                       35x   35x     2x 2x   4x 4x               4x 4x   10x 10x   10x     2x 2x   8x 8x                 10x   1x 1x   4x 4x                   35x         5x         5x     1x                             1x               1x                 1x   2x                             2x               2x                 2x                                                                                                                                               2x                             2x               2x                   2x                                                     5x                     10x       10x     10x     10x     10x     10x     10x     10x     10x 4x   10x 10x     10x     10x     10x           10x         10x     2x 2x 2x   2x           2x           2x 2x             2x 2x         2x   2x                       2x   2x       2x         8x 8x 8x   8x 8x 8x   8x                     8x 8x         8x   8x                     8x             8x   8x           8x                     13x 13x 13x             13x     13x     13x     13x     8x   2x 2x 2x   3x 3x 3x             13x   13x 6x     7x 10x         13x     5x   5x 5x     5x 5x           8x   8x             8x   8x             8x 8x     8x 8x   8x   8x   8x   8x 8x     8x       8x 8x                       5x     5x     5x     5x     5x     5x                 5x     1x 1x   4x 4x                       5x 5x             7x     7x     7x     7x     7x     7x     7x             7x     3x 3x   4x 4x                       7x 7x            
import * as asn1js from "asn1js";
import { utilConcatBuf } from "pvutils";
import AlgorithmIdentifier from "../../src/AlgorithmIdentifier";
import EncryptedContentInfo from "../../src/EncryptedContentInfo";
import PBKDF2Params from "../../src/PBKDF2Params";
import PBES2Params from "../../src/PBES2Params";
import CryptoEngine from "../../src/CryptoEngine";
import * as nodeSpecificCrypto from "./NodeEngineNodeSpecific";
//**************************************************************************************
export default class NodeEngine extends CryptoEngine
{
	//**********************************************************************************
	constructor(parameters)
	{
		super(parameters);
		
		//region Internal properties of the object
		/**
		 * @type {Object}
		 * @description Usually here we are expecting "window.crypto" or an equivalent from custom "crypto engine"
		 */
		this.crypto = nodeSpecificCrypto;
		/**
		 * @type {Object}
		 * @description Usually here we are expecting "window.crypto.subtle" or an equivalent from custom "crypto engine"
		 */
		this.subtle = this;
		/**
		 * @type {string}
		 * @description Name of the "crypto engine"
		 */
		this.name = "nodeCryptoEngine";
		//endregion
	}
	//**********************************************************************************
	/**
	 * Initialize input Uint8Array by random values (with help from current "crypto engine")
	 * @param {!Uint8Array} view
	 * @returns {*}
	 */
	getRandomValues(view)
	{
		view.set(nodeSpecificCrypto.getRandomValues(view.length));
		return view;
	}
	//**********************************************************************************
	getAlgorithmByOID(oid)
	{
		switch(oid)
		{
			case "1.2.840.113549.3.2":
				return {
					name: "RC2-40-CBC",
					length: 5
				};
			case "1.2.840.113549.3.7":
				return {
					name: "DES-EDE3-CBC",
					length: 24
				};
			case "2.16.840.1.101.3.4.1.2":
				return {
					name: "AES-128-CBC",
					length: 16
				};
			case "2.16.840.1.101.3.4.1.22":
				return {
					name: "AES-192-CBC",
					length: 24
				};
			case "2.16.840.1.101.3.4.1.42":
				return {
					name: "AES-256-CBC",
					length: 32
				};
			case "1.2.840.113549.1.5.12":
				return {
					name: "PBKDF2"
				};
			case "1.3.14.3.2.26":
				return {
					name: "SHA-1"
				};
			case "2.16.840.1.101.3.4.2.1":
				return {
					name: "SHA-256"
				};
			case "2.16.840.1.101.3.4.2.2":
				return {
					name: "SHA-384"
				};
			case "2.16.840.1.101.3.4.2.3":
				return {
					name: "SHA-512"
				};
			case "1.2.840.113549.2.7":
				return {
					name: "HMAC",
					hash: {
						name: "SHA-1"
					}
				};
			case "1.2.840.113549.2.9":
				return {
					name: "HMAC",
					hash: {
						name: "SHA-256"
					}
				};
			case "1.2.840.113549.2.10":
				return {
					name: "HMAC",
					hash: {
						name: "SHA-384"
					}
				};
			case "1.2.840.113549.2.11":
				return {
					name: "HMAC",
					hash: {
						name: "SHA-512"
					}
				};
			default:
		}
		
		return {};
	}
	//**********************************************************************************
	getOIDByAlgorithm(algorithm)
	{
		let result = "";
		
		switch(algorithm.name.toUpperCase())
		{
			case "RC2-40-CBC":
				result = "1.2.840.113549.3.2";
				break;
			case "DES-EDE3-CBC":
				result = "1.2.840.113549.3.7";
				break;
			case "AES-128-CBC":
				result = "2.16.840.1.101.3.4.1.2";
				break;
			case "AES-192-CBC":
				result = "2.16.840.1.101.3.4.1.22";
				break;
			case "AES-256-CBC":
				result = "2.16.840.1.101.3.4.1.42";
				break;
			case "PBKDF2":
				result = "1.2.840.113549.1.5.12";
				break;
			case "HMAC":
				switch(algorithm.hash.name.toUpperCase())
				{
					case "SHA-1":
						result = "1.2.840.113549.2.7";
						break;
					case "SHA-256":
						result = "1.2.840.113549.2.9";
						break;
					case "SHA-384":
						result = "1.2.840.113549.2.10";
						break;
					case "SHA-512":
						result = "1.2.840.113549.2.11";
						break;
					default:
				}
				break;
			case "SHA-1":
				result = "1.3.14.3.2.26";
				break;
			case "SHA-256":
				result = "2.16.840.1.101.3.4.2.1";
				break;
			case "SHA-384":
				result = "2.16.840.1.101.3.4.2.2";
				break;
			case "SHA-512":
				result = "2.16.840.1.101.3.4.2.3";
				break;
			default:
		}
		
		return result;
	}
	//**********************************************************************************
	getAlgorithmParameters(algorithmName, operation)
	{
		let result = {
			algorithm: {},
			usages: []
		};
		
		switch(algorithmName.toUpperCase())
		{
			case "RC2-40-CBC":
				switch(operation.toLowerCase())
				{
					case "importkey":
					case "exportkey":
					case "generatekey":
						result = {
							algorithm: {
								name: "RC2-40-CBC",
								length: 5
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					case "decrypt":
					case "encrypt":
						result = {
							algorithm: {
								name: "RC2-40-CBC",
								iv: this.getRandomValues(new Uint8Array(8)), // For "decrypt" the value should be replaced with value got on "encrypt" step
								length: 5
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "RC2-40-CBC"
							},
							usages: []
						};
				}
				break;
			case "DES-EDE3-CBC":
				switch(operation.toLowerCase())
				{
					case "importkey":
					case "exportkey":
					case "generatekey":
						result = {
							algorithm: {
								name: "DES-EDE3-CBC",
								length: 24
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					case "decrypt":
					case "encrypt":
						result = {
							algorithm: {
								name: "DES-EDE3-CBC",
								iv: this.getRandomValues(new Uint8Array(8)), // For "decrypt" the value should be replaced with value got on "encrypt" step
								length: 24
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "DES-EDE3-CBC"
							},
							usages: []
						};
				}
				break;
			case "AES-128-CBC":
				switch(operation.toLowerCase())
				{
					case "importkey":
					case "exportkey":
					case "generatekey":
						result = {
							algorithm: {
								name: "AES-128-CBC",
								length: 16
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					case "decrypt":
					case "encrypt":
						result = {
							algorithm: {
								name: "AES-128-CBC",
								iv: this.getRandomValues(new Uint8Array(16)), // For "decrypt" the value should be replaced with value got on "encrypt" step
								length: 16
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "AES-128-CBC",
								length: 16
							},
							usages: []
						};
				}
				break;
			case "AES-192-CBC":
				switch(operation.toLowerCase())
				{
					case "importkey":
					case "exportkey":
					case "generatekey":
						result = {
							algorithm: {
								name: "AES-192-CBC",
								length: 24
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					case "decrypt":
					case "encrypt":
						result = {
							algorithm: {
								name: "AES-192-CBC",
								iv: this.getRandomValues(new Uint8Array(16)), // For "decrypt" the value should be replaced with value got on "encrypt" step
								length: 24
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "AES-192-CBC",
								length: 24
							},
							usages: []
						};
				}
				break;
			case "AES-256-CBC":
				switch(operation.toLowerCase())
				{
					case "importkey":
					case "exportkey":
					case "generatekey":
						result = {
							algorithm: {
								name: "AES-256-CBC",
								length: 32
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					case "decrypt":
					case "encrypt":
						result = {
							algorithm: {
								name: "AES-256-CBC",
								iv: this.getRandomValues(new Uint8Array(16)), // For "decrypt" the value should be replaced with value got on "encrypt" step
								length: 32
							},
							usages: ["encrypt", "decrypt", "wrapKey", "unwrapKey"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "AES-256-CBC",
								length: 32
							},
							usages: []
						};
				}
				break;
			case "PBKDF2":
				switch(operation.toLowerCase())
				{
					case "derivekey":
						result = {
							algorithm: {
								name: "PBKDF2",
								hash: { name: "SHA-256" },
								salt: new Uint8Array([]),
								iterations: 10000
							},
							usages: ["encrypt", "decrypt"]
						};
						break;
					default:
						return {
							algorithm: {
								name: "PBKDF2"
							},
							usages: []
						};
				}
				break;
			default:
		}
		
		return result;
	}
	//**********************************************************************************
	/**
	 * Specialized function encrypting "EncryptedContentInfo" object using parameters
	 * @param {Object} parameters
	 * @returns {Promise}
	 */
	encryptEncryptedContentInfo(parameters)
	{
		//region Initial variables
		let sequence = Promise.resolve();
		//endregion
		
		//region Check for input parameters
		Iif((parameters instanceof Object) === false)
			return Promise.reject("Parameters must have type \"Object\"");
		
		Iif(("password" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"password\"");
		
		Iif(("contentEncryptionAlgorithm" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"contentEncryptionAlgorithm\"");
		
		Iif(("hmacHashAlgorithm" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"hmacHashAlgorithm\"");
		
		Iif(("iterationCount" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"iterationCount\"");
		
		Iif(("contentToEncrypt" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"contentToEncrypt\"");
		
		Iif(("contentType" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"contentType\"");
		
		if(("pbeSchema" in parameters) === false)
			parameters.pbeSchema = "PBES2";
 
		const contentEncryptionOID = this.getOIDByAlgorithm(parameters.contentEncryptionAlgorithm);
		Iif(contentEncryptionOID === "")
			return Promise.reject("Wrong \"contentEncryptionAlgorithm\" value");
		
		const pbkdf2OID = this.getOIDByAlgorithm({
			name: "PBKDF2"
		});
		Iif(pbkdf2OID === "")
			return Promise.reject("Can not find OID for PBKDF2");
		
		const hmacOID = this.getOIDByAlgorithm({
			name: "HMAC",
			hash: {
				name: parameters.hmacHashAlgorithm
			}
		});
		Iif(hmacOID === "")
			return Promise.reject(`Incorrect value for \"hmacHashAlgorithm\": ${parameters.hmacHashAlgorithm}`);
		//endregion
		
		//region Special case for PBES1
		if(parameters.pbeSchema.toUpperCase() !== "PBES2")  // Assume we have PBES1 here
		{
			//region Initial variables
			const saltBuffer = new ArrayBuffer(20);
			const saltView = new Uint8Array(saltBuffer);
			this.getRandomValues(saltView);
			
			const ivLength = 8; // (in bytes) For current algorithms (3DES and RC2) IV length has the same value
			//endregion
 
			//region Check we have correct encryption algorithm
			let pbeAlgorithm;
			
			switch(parameters.contentEncryptionAlgorithm.name.toUpperCase())
			{
				case "DES-EDE3-CBC":
					pbeAlgorithm = "1.2.840.113549.1.12.1.3"; // pbeWithSHAAnd3-KeyTripleDES-CBC
					break;
				case "RC2-40-CBC":
					pbeAlgorithm = "1.2.840.113549.1.12.1.6"; // pbeWithSHAAnd40BitRC2-CBC
					break;
				default:
					return Promise.reject("For PBES1 encryption algorithm could be only DES-EDE3-CBC or RC2-40-CBC");
			}
			//endregion
			
			//region Encrypt data using PBKDF1 as a source for key
			sequence = sequence.then(() =>
				nodeSpecificCrypto.encryptUsingPBKDF1Password(parameters.contentEncryptionAlgorithm.name, parameters.contentEncryptionAlgorithm.length, ivLength, parameters.password, saltBuffer, parameters.iterationCount, parameters.contentToEncrypt)
			);
			//endregion
			
			//region Store all parameters in EncryptedData object
			sequence = sequence.then(result =>
			{
				const encryptedContentInfo = new EncryptedContentInfo({
					contentType: parameters.contentType,
					contentEncryptionAlgorithm: new AlgorithmIdentifier({
						algorithmId: pbeAlgorithm,
						algorithmParams: new asn1js.Sequence({
							value: [
								new asn1js.OctetString({ valueHex: saltBuffer }),
								new asn1js.Integer({ value: parameters.iterationCount })
							]
						})
					})
				});
				encryptedContentInfo.encryptedContent = new asn1js.OctetString({ valueHex: result });
				
				return encryptedContentInfo;
			});
			//endregion
			
			return sequence;
		}
		//endregion
		
		//region Initial variables
		const ivBuffer = new ArrayBuffer(parameters.contentEncryptionAlgorithm.iv.length);
		const ivView = new Uint8Array(ivBuffer);
		this.getRandomValues(ivView);
		
		const saltBuffer = new ArrayBuffer(8);
		const saltView = new Uint8Array(saltBuffer);
		this.getRandomValues(saltView);
		
		const pbkdf2Params = new PBKDF2Params({
			salt: new asn1js.OctetString({ valueHex: saltBuffer }),
			iterationCount: parameters.iterationCount,
			prf: new AlgorithmIdentifier({
				algorithmId: hmacOID,
				algorithmParams: new asn1js.Null()
			})
		});
		//endregion
		
		//region Encrypt data using PBKDF2 as a source for key
		sequence = sequence.then(() =>
			nodeSpecificCrypto.encryptUsingPBKDF2Password(parameters.contentEncryptionAlgorithm.name, parameters.contentEncryptionAlgorithm.length, parameters.password, saltBuffer, parameters.iterationCount, parameters.hmacHashAlgorithm, ivBuffer, parameters.contentToEncrypt)
		);
		//endregion
		
		//region Store all parameters in EncryptedData object
		sequence = sequence.then(result =>
		{
			const pbes2Parameters = new PBES2Params({
				keyDerivationFunc: new AlgorithmIdentifier({
					algorithmId: pbkdf2OID,
					algorithmParams: pbkdf2Params.toSchema()
				}),
				encryptionScheme: new AlgorithmIdentifier({
					algorithmId: contentEncryptionOID,
					algorithmParams: new asn1js.OctetString({ valueHex: ivBuffer })
				})
			});
			
			const encryptedContentInfo = new EncryptedContentInfo({
				contentType: parameters.contentType,
				contentEncryptionAlgorithm: new AlgorithmIdentifier({
					algorithmId: "1.2.840.113549.1.5.13", // pkcs5PBES2
					algorithmParams: pbes2Parameters.toSchema()
				})
			});
			encryptedContentInfo.encryptedContent = new asn1js.OctetString({ valueHex: result });
			
			return encryptedContentInfo;
		}, error =>
			Promise.reject(error)
		);
		//endregion
		
		return sequence;
	}
	//**********************************************************************************
	/**
	 * Decrypt data stored in "EncryptedContentInfo" object using parameters
	 * @param parameters
	 * @return {Promise}
	 */
	decryptEncryptedContentInfo(parameters)
	{
		//region Initial variables
		let pbes1EncryptionAlgorithm = "";
		let pbes1EncryptionAlgorithmLength = 0;
		let pbes1EncryptionIVLength = 8;
 
		let pbes2Parameters;
		let pbkdf2Params;
		//endregion
		
		//region Check for input parameters
		Iif((parameters instanceof Object) === false)
			return Promise.reject("Parameters must have type \"Object\"");
		
		Iif(("password" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"password\"");
		
		Iif(("encryptedContentInfo" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"encryptedContentInfo\"");
		
		switch(parameters.encryptedContentInfo.contentEncryptionAlgorithm.algorithmId)
		{
			case "1.2.840.113549.1.5.13": // pkcs5PBES2
				break;
			case "1.2.840.113549.1.12.1.3": // pbeWithSHAAnd3-KeyTripleDES-CBC
				pbes1EncryptionAlgorithm = "DES-EDE3-CBC";
				pbes1EncryptionAlgorithmLength = 24;
				break;
			case "1.2.840.113549.1.12.1.6": // pbeWithSHAAnd40BitRC2-CBC
				pbes1EncryptionAlgorithm = "RC2-40-CBC";
				pbes1EncryptionAlgorithmLength = 5;
				break;
			default:
				return Promise.reject(`Unknown \"contentEncryptionAlgorithm\": ${parameters.encryptedContentInfo.contentEncryptionAlgorithm.algorithmId}`);
		}
		//endregion
		
		//region Create correct data block for decryption
		let dataBuffer = new ArrayBuffer(0);
		
		if(parameters.encryptedContentInfo.encryptedContent.idBlock.isConstructed === false)
			dataBuffer = parameters.encryptedContentInfo.encryptedContent.valueBlock.valueHex;
		else
		{
			for(const content of parameters.encryptedContentInfo.encryptedContent.valueBlock.value)
				dataBuffer = utilConcatBuf(dataBuffer, content.valueBlock.valueHex);
		}
		//endregion
 
		//region Check if we have PBES1
		if(pbes1EncryptionAlgorithm.length)
		{
			//region Description
			const pbesParameters = parameters.encryptedContentInfo.contentEncryptionAlgorithm.algorithmParams;
			
			const saltBuffer = pbesParameters.valueBlock.value[0].valueBlock.valueHex;
			const iterationCount = pbesParameters.valueBlock.value[1].valueBlock.valueDec;
			//endregion
			
			return Promise.resolve().then(() =>
				nodeSpecificCrypto.decryptUsingPBKDF1Password(pbes1EncryptionAlgorithm, pbes1EncryptionAlgorithmLength, pbes1EncryptionIVLength, parameters.password, saltBuffer, iterationCount, dataBuffer)
			);
		}
		//endregion
		
		//region Initial variables
		try
		{
			pbes2Parameters = new PBES2Params({ schema: parameters.encryptedContentInfo.contentEncryptionAlgorithm.algorithmParams });
		}
		catch(ex)
		{
			return Promise.reject("Incorrectly encoded \"pbes2Parameters\"");
		}
		
		try
		{
			pbkdf2Params = new PBKDF2Params({ schema: pbes2Parameters.keyDerivationFunc.algorithmParams });
		}
		catch(ex)
		{
			return Promise.reject("Incorrectly encoded \"pbkdf2Params\"");
		}
		
		const contentEncryptionAlgorithm = this.getAlgorithmByOID(pbes2Parameters.encryptionScheme.algorithmId);
		Iif(("name" in contentEncryptionAlgorithm) === false)
			return Promise.reject(`Incorrect OID for \"contentEncryptionAlgorithm\": ${pbes2Parameters.encryptionScheme.algorithmId}`);
		
		const ivBuffer = pbes2Parameters.encryptionScheme.algorithmParams.valueBlock.valueHex;
		const saltBuffer = pbkdf2Params.salt.valueBlock.valueHex;
		
		const iterationCount = pbkdf2Params.iterationCount;
		
		let hmacHashAlgorithm = "SHA-1";
		
		Eif("prf" in pbkdf2Params)
		{
			const algorithm = this.getAlgorithmByOID(pbkdf2Params.prf.algorithmId);
			Iif(("name" in algorithm) === false)
				return Promise.reject("Incorrect OID for HMAC hash algorithm");
			
			hmacHashAlgorithm = algorithm.hash.name;
		}
		//endregion
		
		return Promise.resolve().then(() =>
			nodeSpecificCrypto.decryptUsingPBKDF2Password(contentEncryptionAlgorithm.name, contentEncryptionAlgorithm.length, parameters.password, saltBuffer, iterationCount, hmacHashAlgorithm, ivBuffer, dataBuffer)
		);
	}
	//**********************************************************************************
	/**
	 * Stamping (signing) data using algorithm simular to HMAC
	 * @param {Object} parameters
	 * @return {Promise.<T>|Promise}
	 */
	stampDataWithPassword(parameters)
	{
		//region Check for input parameters
		Iif((parameters instanceof Object) === false)
			return Promise.reject("Parameters must have type \"Object\"");
		
		Iif(("password" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"password\"");
		
		Iif(("hashAlgorithm" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"hashAlgorithm\"");
		
		Iif(("salt" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"iterationCount\"");
		
		Iif(("iterationCount" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"iterationCount\"");
		
		Iif(("contentToStamp" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"contentToStamp\"");
		//endregion
		
		//region Initial variables
		let length;
		//endregion
		
		//region Choose correct length for HMAC key
		switch(parameters.hashAlgorithm.toLowerCase())
		{
			case "sha-1":
				length = 160;
				break;
			case "sha-256":
				length = 256;
				break;
			case "sha-384":
				length = 384;
				break;
			case "sha-512":
				length = 512;
				break;
			default:
				return Promise.reject(`Incorrect \"parameters.hashAlgorithm\" parameter: ${parameters.hashAlgorithm}`);
		}
		//endregion
 
		return Promise.resolve().then(() =>
			nodeSpecificCrypto.stampDataWithPassword(parameters.hashAlgorithm, length, parameters.password, parameters.salt, parameters.iterationCount, parameters.contentToStamp)
		);
	}
	//**********************************************************************************
	verifyDataStampedWithPassword(parameters)
	{
		//region Check for input parameters
		Iif((parameters instanceof Object) === false)
			return Promise.reject("Parameters must have type \"Object\"");
		
		Iif(("password" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"password\"");
		
		Iif(("hashAlgorithm" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"hashAlgorithm\"");
		
		Iif(("salt" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"iterationCount\"");
		
		Iif(("iterationCount" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"salt\"");
		
		Iif(("contentToVerify" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"contentToVerify\"");
		
		Iif(("signatureToVerify" in parameters) === false)
			return Promise.reject("Absent mandatory parameter \"signatureToVerify\"");
		//endregion
		
		//region Choose correct length for HMAC key
		let length;
		
		switch(parameters.hashAlgorithm.toLowerCase())
		{
			case "sha-1":
				length = 160;
				break;
			case "sha-256":
				length = 256;
				break;
			case "sha-384":
				length = 384;
				break;
			case "sha-512":
				length = 512;
				break;
			default:
				return Promise.reject(`Incorrect \"parameters.hashAlgorithm\" parameter: ${parameters.hashAlgorithm}`);
		}
		//endregion
		
		return Promise.resolve().then(() =>
			nodeSpecificCrypto.verifyDataStampedWithPassword(parameters.hashAlgorithm, length, parameters.password, parameters.salt, parameters.iterationCount, parameters.contentToVerify, parameters.signatureToVerify)
		);
	}
	//**********************************************************************************
}
//**************************************************************************************