all files / core/security/digital-signature/signature/ cryptographic-encoder.js

24.53% Statements 13/53
0% Branches 0/26
20% Functions 2/10
24.53% Lines 13/53
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                                                                                                                                      
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfCryptographicEncoding = (function () {
        function _PdfCryptographicEncoding(cipher) {
            this._cipher = cipher;
        }
        _PdfCryptographicEncoding.prototype._getAlgorithmName = function () {
            return this._cipher._getAlgorithmName() + "/PKCS1Padding";
        };
        _PdfCryptographicEncoding.prototype._getInputBlock = function () {
            return this._isEncryption ? this._cipher._getInputBlock() - 10 : this._cipher._getInputBlock();
        };
        _PdfCryptographicEncoding.prototype._getOutputBlock = function () {
            return this._isEncryption ? this._cipher._getOutputBlock() : this._cipher._getOutputBlock() - 10;
        };
        _PdfCryptographicEncoding.prototype._initialize = function (forEncryption, parameters) {
            this._cipher._initialize(forEncryption, parameters);
            this._isPrivateKey = parameters._isPrivate;
            this._isEncryption = forEncryption;
        };
        _PdfCryptographicEncoding.prototype._processBlock = function (input, inOff, length) {
            return this._isEncryption ?
                this._encodeBlock(input, inOff, length) :
                this._decodeBlock(input, inOff, length);
        };
        _PdfCryptographicEncoding.prototype._encodeBlock = function (input, inOff, inLen) {
            if (inLen > this._getInputBlock()) {
                throw new Error('Input data too large for PKCS#1 padding.');
            }
            var block = new Uint8Array(this._cipher._getInputBlock());
            if (this._isPrivateKey) {
                block[0] = 0x01;
                for (var i = 1; i < block.length - inLen - 1; i++) {
                    block[i] = 0xFF;
                }
            }
            else {
                block[0] = 0x02;
                for (var i = 1; i < block.length - inLen - 1; i++) {
                    var randomByte = void 0;
                    do {
                        randomByte = Math.floor(Math.random() * 255) + 1;
                    } while (randomByte === 0);
                    block[i] = randomByte;
                }
            }
            block[block.length - inLen - 1] = 0x00;
            block.set(input.subarray(inOff, inOff + inLen), block.length - inLen);
            return this._cipher._processBlock(block, 0, block.length);
        };
        _PdfCryptographicEncoding.prototype._decodeBlock = function (input, inOff, inLen) {
            var block = this._cipher._processBlock(input, inOff, inLen);
            if (block.length < this._getOutputBlock()) {
                throw new Error('Data block is truncated.');
            }
            var type = block[0];
            if (type !== 1 && type !== 2) {
                throw new Error("Invalid block type: " + type + ".");
            }
            var separatorIndex = -1;
            for (var i = 1; i < block.length; i++) {
                if (block[i] === 0x00) {
                    separatorIndex = i;
                    break;
                }
                if (type === 1 && block[i] !== 0xFF) {
                    throw new Error('Invalid PKCS#1 padding: bad padding byte.');
                }
            }
            if (separatorIndex === -1 || separatorIndex < 9) {
                throw new Error('Invalid PKCS#1 padding: separator not found or too short.');
            }
            return block.subarray(separatorIndex + 1);
        };
        return _PdfCryptographicEncoding;
    }());
    exports._PdfCryptographicEncoding = _PdfCryptographicEncoding;
});