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

6.7% Statements 29/433
0% Branches 0/79
6.45% Functions 2/31
6.73% Lines 29/431
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
define(["require", "exports", "./../asn1/enumerator", "./../../../enumerator", "../asn1/unique-encoding-element", "../asn1/basic-encoding-element", "../asn1/identifier-mapping", "./pdf-digest-algorithms", "./pdf-object-identifiers"], function (require, exports, enumerator_1, enumerator_2, unique_encoding_element_1, basic_encoding_element_1, identifier_mapping_1, pdf_digest_algorithms_1, pdf_object_identifiers_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfCryptographicMessageSyntaxSigner = (function () {
        function _PdfCryptographicMessageSyntaxSigner(privateKey, certChain, hashAlgorithm, hasRsaData) {
            this._digestAlgorithm = new pdf_digest_algorithms_1._PdfMessageDigestAlgorithms();
            if (privateKey instanceof Uint8Array) {
                this._initializeCmsSigner(privateKey);
            }
            else {
                this._digestAlgorithm = new pdf_digest_algorithms_1._PdfMessageDigestAlgorithms();
                this._digestAlgorithmObjectIdentifier = this._digestAlgorithm._getAllowedDigests(hashAlgorithm);
                if (!this._digestAlgorithmObjectIdentifier) {
                    throw new Error("Unknown hash algorithm: " + hashAlgorithm);
                }
                this._version = 1;
                this._signerVersion = 1;
                this._digestObjectIdentifier = new Map();
                this._digestObjectIdentifier.set(this._digestAlgorithmObjectIdentifier, null);
                if (certChain) {
                    this._certificates = certChain.slice();
                    this._signatureCertificate = this._certificates[0];
                }
                else {
                    this._certificates = [];
                    this._signatureCertificate = null;
                }
                if (privateKey) {
                    if (this._isRsaKey(privateKey)) {
                        var identifier = new pdf_object_identifiers_1._PdfDigitalIdentifiers();
                        this._encryptionAlgorithmObjectIdentifier = identifier._rsaEncryption;
                    }
                    else {
                        throw new Error('Unknown key algorithm');
                    }
                }
                if (hasRsaData) {
                    this._rsaData = new Uint8Array(0);
                }
            }
        }
        _PdfCryptographicMessageSyntaxSigner.prototype._initializeCmsSigner = function (bytes) {
            var stream = new basic_encoding_element_1._PdfBasicEncodingElement();
            stream._fromBytes(bytes);
            var sequence = stream._getSequence();
            var oid = sequence[0]._getObjectIdentifier();
            var dotDelimitedNotation = oid._getDotDelimitedNotation();
            if (dotDelimitedNotation === '1.2.840.113549.1.7.2') {
                var inner = sequence[1]._getInner();
                var innerSequence = inner._getSequence();
                var signerInfosSet = innerSequence[4]._getSequence();
                var signerInformationSeq = signerInfosSet[0]._getSequence();
                var digestAlgorithmSeq = signerInformationSeq[2]._getSequence();
                var digestAlgorithmOidBytes = digestAlgorithmSeq[0]._getValue();
                var identifier = new identifier_mapping_1._PdfObjectIdentifier()._fromBytes(digestAlgorithmOidBytes);
                this._digestAlgorithmObjectIdentifier = identifier.toString();
            }
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._isRsaKey = function (key) {
            return 'modulus' in key && 'exponent' in key;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._getHashAlgorithm = function () {
            if (!this._hashAlgorithm) {
                this._hashAlgorithm = this._digestAlgorithm._getDigest(this._digestAlgorithmObjectIdentifier);
            }
            return this._hashAlgorithm;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._getDigestAlgorithm = function () {
            return this._digestAlgorithm;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._getSequenceDataSet = function (secondDigest, ocsp, crlBytes, sigtype) {
            var attributeElements = [];
            var contentTypeOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
            contentTypeOid._tagClass = enumerator_1._TagClassType.universal;
            contentTypeOid._construction = enumerator_1._ConstructionType.primitive;
            contentTypeOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
            contentTypeOid._setValue(this._encodeObjectIdentifier('1.2.840.113549.1.9.3'));
            var pkcs7DataOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
            pkcs7DataOid._tagClass = enumerator_1._TagClassType.universal;
            pkcs7DataOid._construction = enumerator_1._ConstructionType.primitive;
            pkcs7DataOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
            pkcs7DataOid._setValue(this._encodeObjectIdentifier('1.2.840.113549.1.7.1'));
            var contentTypeSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            contentTypeSet._tagClass = enumerator_1._TagClassType.universal;
            contentTypeSet._construction = enumerator_1._ConstructionType.constructed;
            contentTypeSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            contentTypeSet._setValue(this._encodeSequence([pkcs7DataOid]));
            var contentTypeSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
            contentTypeSeq._tagClass = enumerator_1._TagClassType.universal;
            contentTypeSeq._construction = enumerator_1._ConstructionType.constructed;
            contentTypeSeq._setTagNumber(enumerator_1._UniversalType.sequence);
            contentTypeSeq._setValue(this._encodeSequence([contentTypeOid, contentTypeSet]));
            attributeElements.push(contentTypeSeq);
            var messageDigestOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
            messageDigestOid._tagClass = enumerator_1._TagClassType.universal;
            messageDigestOid._construction = enumerator_1._ConstructionType.primitive;
            messageDigestOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
            messageDigestOid._setValue(this._encodeObjectIdentifier('1.2.840.113549.1.9.4'));
            var digestOctet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            digestOctet._tagClass = enumerator_1._TagClassType.universal;
            digestOctet._construction = enumerator_1._ConstructionType.primitive;
            digestOctet._setTagNumber(enumerator_1._UniversalType.octetString);
            digestOctet._setValue(secondDigest);
            var digestSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            digestSet._tagClass = enumerator_1._TagClassType.universal;
            digestSet._construction = enumerator_1._ConstructionType.constructed;
            digestSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            digestSet._setValue(this._encodeSequence([digestOctet]));
            var messageDigestSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
            messageDigestSeq._tagClass = enumerator_1._TagClassType.universal;
            messageDigestSeq._construction = enumerator_1._ConstructionType.constructed;
            messageDigestSeq._setTagNumber(enumerator_1._UniversalType.sequence);
            messageDigestSeq._setValue(this._encodeSequence([messageDigestOid, digestSet]));
            attributeElements.push(messageDigestSeq);
            if (sigtype === enumerator_2.CryptographicStandard.cades && this._signatureCertificate) {
                var certHash = this._hashCertificate(this._signatureCertificate);
                var certHashOctet = new unique_encoding_element_1._PdfUniqueEncodingElement();
                certHashOctet._tagClass = enumerator_1._TagClassType.universal;
                certHashOctet._construction = enumerator_1._ConstructionType.primitive;
                certHashOctet._setTagNumber(enumerator_1._UniversalType.octetString);
                certHashOctet._setValue(certHash);
                var signingCertOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
                signingCertOid._tagClass = enumerator_1._TagClassType.universal;
                signingCertOid._construction = enumerator_1._ConstructionType.primitive;
                signingCertOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
                signingCertOid._setValue(this._encodeObjectIdentifier('1.2.840.113549.1.9.16.2.47'));
                var sha256String = new pdf_digest_algorithms_1._PdfMessageDigestAlgorithms()._secureHash256;
                var isSha256 = this._digestAlgorithmObjectIdentifier === this._digestAlgorithm._getAllowedDigests(sha256String);
                var signingCertAttr = void 0;
                if (isSha256) {
                    var essCertIdV2 = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    essCertIdV2._tagClass = enumerator_1._TagClassType.universal;
                    essCertIdV2._construction = enumerator_1._ConstructionType.constructed;
                    essCertIdV2._setTagNumber(enumerator_1._UniversalType.sequence);
                    essCertIdV2._setValue(this._encodeSequence([certHashOctet]));
                    var certsSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    certsSeq._tagClass = enumerator_1._TagClassType.universal;
                    certsSeq._construction = enumerator_1._ConstructionType.constructed;
                    certsSeq._setTagNumber(enumerator_1._UniversalType.sequence);
                    certsSeq._setValue(this._encodeSequence([essCertIdV2]));
                    var signingCertV2 = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertV2._tagClass = enumerator_1._TagClassType.universal;
                    signingCertV2._construction = enumerator_1._ConstructionType.constructed;
                    signingCertV2._setTagNumber(enumerator_1._UniversalType.sequence);
                    signingCertV2._setValue(this._encodeSequence([certsSeq]));
                    var signingCertSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertSet._tagClass = enumerator_1._TagClassType.universal;
                    signingCertSet._construction = enumerator_1._ConstructionType.constructed;
                    signingCertSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
                    signingCertSet._setValue(this._encodeSequence([signingCertV2]));
                    signingCertAttr = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertAttr._tagClass = enumerator_1._TagClassType.universal;
                    signingCertAttr._construction = enumerator_1._ConstructionType.constructed;
                    signingCertAttr._setTagNumber(enumerator_1._UniversalType.sequence);
                    signingCertAttr._setValue(this._encodeSequence([signingCertOid, signingCertSet]));
                }
                else {
                    var hashAlgOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    hashAlgOid._tagClass = enumerator_1._TagClassType.universal;
                    hashAlgOid._construction = enumerator_1._ConstructionType.primitive;
                    hashAlgOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
                    hashAlgOid._setValue(this._encodeObjectIdentifier(this._digestAlgorithmObjectIdentifier));
                    var algSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    algSeq._tagClass = enumerator_1._TagClassType.universal;
                    algSeq._construction = enumerator_1._ConstructionType.constructed;
                    algSeq._setTagNumber(enumerator_1._UniversalType.sequence);
                    algSeq._setValue(this._encodeSequence([hashAlgOid]));
                    var essCertIdV2 = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    essCertIdV2._tagClass = enumerator_1._TagClassType.universal;
                    essCertIdV2._construction = enumerator_1._ConstructionType.constructed;
                    essCertIdV2._setTagNumber(enumerator_1._UniversalType.sequence);
                    essCertIdV2._setValue(this._encodeSequence([algSeq, certHashOctet]));
                    var certsSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    certsSeq._tagClass = enumerator_1._TagClassType.universal;
                    certsSeq._construction = enumerator_1._ConstructionType.constructed;
                    certsSeq._setTagNumber(enumerator_1._UniversalType.sequence);
                    certsSeq._setValue(this._encodeSequence([essCertIdV2]));
                    var signingCertV2 = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertV2._tagClass = enumerator_1._TagClassType.universal;
                    signingCertV2._construction = enumerator_1._ConstructionType.constructed;
                    signingCertV2._setTagNumber(enumerator_1._UniversalType.sequence);
                    signingCertV2._setValue(this._encodeSequence([certsSeq]));
                    var signingCertSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertSet._tagClass = enumerator_1._TagClassType.universal;
                    signingCertSet._construction = enumerator_1._ConstructionType.constructed;
                    signingCertSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
                    signingCertSet._setValue(this._encodeSequence([signingCertV2]));
                    signingCertAttr = new unique_encoding_element_1._PdfUniqueEncodingElement();
                    signingCertAttr._tagClass = enumerator_1._TagClassType.universal;
                    signingCertAttr._construction = enumerator_1._ConstructionType.constructed;
                    signingCertAttr._setTagNumber(enumerator_1._UniversalType.sequence);
                    signingCertAttr._setValue(this._encodeSequence([signingCertOid, signingCertSet]));
                }
                attributeElements.push(signingCertAttr);
            }
            var attributeSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            attributeSet._tagClass = enumerator_1._TagClassType.universal;
            attributeSet._construction = enumerator_1._ConstructionType.constructed;
            attributeSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            attributeSet._setValue(this._encodeSequence(attributeElements));
            return this._encodeToUniqueElement(attributeSet);
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createPrimitiveOid = function (oid) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.universal;
            element._construction = enumerator_1._ConstructionType.primitive;
            element._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
            element._setValue(this._encodeObjectIdentifier(oid));
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createPrimitiveOctet = function (value) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.universal;
            element._construction = enumerator_1._ConstructionType.primitive;
            element._setTagNumber(enumerator_1._UniversalType.octetString);
            element._setValue(value);
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createConstructed = function (tag, elements) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.universal;
            element._construction = enumerator_1._ConstructionType.constructed;
            element._setTagNumber(tag);
            element._setValue(this._encodeSequence(elements));
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._encodeObjectIdentifier = function (oidString) {
            var parts = oidString.split('.').map(Number);
            var bytes = [];
            bytes.push(parts[0] * 40 + parts[1]);
            for (var i = 2; i < parts.length; i++) {
                var value = parts[i];
                if (value < 128) {
                    bytes.push(value);
                }
                else {
                    var temp = [];
                    while (value > 0) {
                        temp.unshift(value & 0x7F);
                        value >>>= 7;
                    }
                    for (var j = 0; j < temp.length - 1; j++) {
                        temp[j] |= 0x80;
                    }
                    bytes.push.apply(bytes, temp);
                }
            }
            return new Uint8Array(bytes);
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._encodeSequence = function (elements) {
            var content = [];
            for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) {
                var element = elements_1[_i];
                var encoded = this._encodeToUniqueElement(element);
                content.push.apply(content, Array.from(encoded));
            }
            return new Uint8Array(content);
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._encodeToUniqueElement = function (element) {
            var result = [];
            var tag = element._getTagNumber();
            if (element._construction === enumerator_1._ConstructionType.constructed) {
                tag |= 0x20;
            }
            if (element._tagClass === enumerator_1._TagClassType.context) {
                tag |= 0x80;
            }
            result.push(tag);
            var contentLength = element._getValue() ? element._getValue().length : 0;
            if (contentLength < 128) {
                result.push(contentLength);
            }
            else {
                var lengthBytes = this._encodeLength(contentLength);
                result.push(0x80 | lengthBytes.length);
                result.push.apply(result, lengthBytes);
            }
            if (element._getValue()) {
                result.push.apply(result, Array.from(element._getValue()));
            }
            return new Uint8Array(result);
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._encodeLength = function (length) {
            var bytes = [];
            while (length > 0) {
                bytes.unshift(length & 0xFF);
                length >>>= 8;
            }
            return bytes;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._hashCertificate = function (certificate) {
            var certBytes = certificate._getEncoded();
            var hasher = this._digestAlgorithm._getMessageDigest(this._getHashAlgorithm());
            return hasher._hash(certBytes, 0, certBytes.length);
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._setSignedData = function (digest, rsaData, digestEncryptionAlgorithm) {
            this._signedData = digest;
            this._signedRsaData = rsaData;
            if (digestEncryptionAlgorithm) {
                switch (digestEncryptionAlgorithm) {
                    case 'RSA':
                        this._encryptionAlgorithmObjectIdentifier = new pdf_object_identifiers_1._PdfDigitalIdentifiers()._rsaEncryption;
                        break;
                    case 'DSA':
                        this._encryptionAlgorithmObjectIdentifier = new pdf_object_identifiers_1._PdfDigitalIdentifiers()._dsaSignature;
                        break;
                    case 'ECDSA':
                        this._encryptionAlgorithmObjectIdentifier = new pdf_object_identifiers_1._PdfDigitalIdentifiers()._ecPublicKey;
                        break;
                    default:
                        throw new Error("Invalid algorithm: " + digestEncryptionAlgorithm);
                }
            }
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._encodeCertificateSet = function (certificates) {
            var totalLen = certificates.reduce(function (sum, arr) { return sum + arr.length; }, 0);
            var lengthBytes;
            if (totalLen < 128) {
                lengthBytes = [totalLen];
            }
            else if (totalLen < 256) {
                lengthBytes = [0x81, totalLen];
            }
            else {
                lengthBytes = [0x82, totalLen >> 8 & 0xff, totalLen & 0xff];
            }
            var out = new Uint8Array(1 + lengthBytes.length + totalLen);
            out[0] = 0xa0;
            out.set(lengthBytes, 1);
            var pos = 1 + lengthBytes.length;
            for (var _i = 0, certificates_1 = certificates; _i < certificates_1.length; _i++) {
                var cert = certificates_1[_i];
                out.set(cert, pos);
                pos += cert.length;
            }
            return out;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createPrimitive = function (tag, value) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.universal;
            element._construction = enumerator_1._ConstructionType.primitive;
            element._setTagNumber(tag);
            element._setValue(value);
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createAsn1Constructed = function (tag, elements) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.universal;
            element._construction = enumerator_1._ConstructionType.constructed;
            element._setTagNumber(tag);
            element._setSequence(elements);
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._createContextConstructed = function (tag, elements) {
            var element = new unique_encoding_element_1._PdfUniqueEncodingElement();
            element._tagClass = enumerator_1._TagClassType.context;
            element._construction = enumerator_1._ConstructionType.constructed;
            element._setTagNumber(tag);
            if (Array.isArray(elements)) {
                element._setSequence(elements);
            }
            else {
                element._setValue(elements);
            }
            return element;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._sign = function (secondDigest, timeStampResponse, revocation, bytes, sigtype, hashAlgorithm) {
            var _this = this;
            if (this._signedData) {
                this._digest = this._signedData;
                if (this._rsaData) {
                    this._rsaData = this._signedRsaData;
                }
            }
            var digestAlgorithms = [];
            this._digestObjectIdentifier.forEach(function (_, oid) {
                var oidEl = _this._createPrimitive(enumerator_1._UniversalType.objectIdentifier, _this._encodeObjectIdentifier(oid));
                var nullEl = _this._createPrimitive(enumerator_1._UniversalType.nullValue, new Uint8Array(0));
                digestAlgorithms.push(_this._createAsn1Constructed(enumerator_1._UniversalType.sequence, [oidEl, nullEl]));
            });
            var contentInfoElements = [
                this._createPrimitive(enumerator_1._UniversalType.objectIdentifier, this._encodeObjectIdentifier(new pdf_object_identifiers_1._PdfDigitalIdentifiers()._cryptographicData))
            ];
            if (this._rsaData && this._rsaData.length > 0) {
                var octet = this._createPrimitive(enumerator_1._UniversalType.octetString, this._rsaData);
                contentInfoElements.push(this._createContextConstructed(0, [octet]));
            }
            var contentInfoSeq = this._createAsn1Constructed(enumerator_1._UniversalType.sequence, contentInfoElements);
            var certificateElements = this._certificates
                .filter(function (cert) { return cert; })
                .map(function (cert) {
                var el = new unique_encoding_element_1._PdfUniqueEncodingElement();
                el._fromBytes(cert._getEncodedString());
                return el;
            });
            var signerInfoElements = [
                this._createPrimitive(enumerator_1._UniversalType.integer, new Uint8Array([this._signerVersion]))
            ];
            if (this._signatureCertificate) {
                var issuerAndSerialElements = [];
                var tbsCertBytes = this._signatureCertificate._getTobeSignedCertificate();
                var issuerElement = this._getIssuer(tbsCertBytes);
                if (issuerElement) {
                    issuerAndSerialElements.push(issuerElement);
                }
                var serialValue = void 0;
                var singed = this._signatureCertificate._structure._getSignedCertificate();
                if (this._signatureCertificate._structure && singed && singed._serialNumber) {
                    serialValue = singed._serialNumber;
                }
                else {
                    serialValue = new Uint8Array([1]);
                }
                var serialElement = this._createPrimitive(enumerator_1._UniversalType.integer, serialValue);
                issuerAndSerialElements.push(serialElement);
                signerInfoElements.push(this._createAsn1Constructed(enumerator_1._UniversalType.sequence, issuerAndSerialElements));
            }
            var digestAlgSeq = this._createAsn1Constructed(enumerator_1._UniversalType.sequence, [
                this._createPrimitive(enumerator_1._UniversalType.objectIdentifier, this._encodeObjectIdentifier(this._digestAlgorithmObjectIdentifier)),
                this._createPrimitive(enumerator_1._UniversalType.nullValue, new Uint8Array(0))
            ]);
            signerInfoElements.push(digestAlgSeq);
            if (secondDigest) {
                var authenticatedAttrs = this._getSequenceDataSet(secondDigest, revocation, bytes, sigtype);
                signerInfoElements.push(this._createContextConstructed(0, authenticatedAttrs));
            }
            var sigAlgSeq = this._createAsn1Constructed(enumerator_1._UniversalType.sequence, [
                this._createPrimitive(enumerator_1._UniversalType.objectIdentifier, this._encodeObjectIdentifier(this._encryptionAlgorithmObjectIdentifier)),
                this._createPrimitive(enumerator_1._UniversalType.nullValue, new Uint8Array(0))
            ]);
            signerInfoElements.push(sigAlgSeq);
            signerInfoElements.push(this._createPrimitive(enumerator_1._UniversalType.octetString, this._digest || new Uint8Array(0)));
            var signerInfoSeq = this._createAsn1Constructed(enumerator_1._UniversalType.sequence, signerInfoElements);
            var bodyElements = this._buildSignedDataBodyElements(this._version, digestAlgorithms, contentInfoSeq, certificateElements, signerInfoSeq);
            var signedDataBytes = this._concatAbstractSyntaxSequence(bodyElements);
            var encodedIdentifier = this._encodeObjectIdentifier(new pdf_object_identifiers_1._PdfDigitalIdentifiers()._cryptographicSignedData);
            var pkcs7Oid = this._createPrimitive(enumerator_1._UniversalType.objectIdentifier, encodedIdentifier);
            var signedDataContext = this._createContextConstructed(0, signedDataBytes);
            var pkcs7TopSeq = this._createAsn1Constructed(enumerator_1._UniversalType.sequence, [pkcs7Oid, signedDataContext]);
            return pkcs7TopSeq._toBytes();
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._buildSignedDataBodyElements = function (version, digestAlgorithms, contentInfoSeq, certificateElements, signerInfoSeq) {
            var bodyElements = [];
            bodyElements.push(this._createPrimitive(enumerator_1._UniversalType.integer, new Uint8Array([version])));
            var digestAlgSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            digestAlgSet._tagClass = enumerator_1._TagClassType.universal;
            digestAlgSet._construction = enumerator_1._ConstructionType.constructed;
            digestAlgSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            digestAlgSet._setAbstractSetValue(digestAlgorithms);
            bodyElements.push(digestAlgSet);
            bodyElements.push(contentInfoSeq);
            if (certificateElements.length > 0) {
                var certSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
                certSet._tagClass = enumerator_1._TagClassType.context;
                certSet._construction = enumerator_1._ConstructionType.constructed;
                certSet._setTagNumber(0);
                certSet._setAbstractSetValue(certificateElements);
                bodyElements.push(certSet);
            }
            var signerInfoSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            signerInfoSet._tagClass = enumerator_1._TagClassType.universal;
            signerInfoSet._construction = enumerator_1._ConstructionType.constructed;
            signerInfoSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            signerInfoSet._setAbstractSetValue([signerInfoSeq]);
            bodyElements.push(signerInfoSet);
            return bodyElements;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._concatAbstractSyntaxSequence = function (elements) {
            var parts = [];
            for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) {
                var el = elements_2[_i];
                if (el instanceof Uint8Array) {
                    parts.push(el);
                }
                else if (el instanceof unique_encoding_element_1._PdfUniqueEncodingElement) {
                    parts.push(el._toBytes());
                }
                else {
                    throw new Error('Element for PKCS#7 serialization must be distinguished element or Uint8Array');
                }
            }
            var totalLen = parts.reduce(function (sum, part) { return sum + part.length; }, 0);
            var lenBytes;
            if (totalLen < 128) {
                lenBytes = [totalLen];
            }
            else if (totalLen < 256) {
                lenBytes = [0x81, totalLen];
            }
            else {
                lenBytes = [0x82, totalLen >> 8, totalLen & 0xff];
            }
            var out = new Uint8Array(1 + lenBytes.length + totalLen);
            out[0] = 0x30;
            out.set(lenBytes, 1);
            var pos = 1 + lenBytes.length;
            for (var _a = 0, parts_1 = parts; _a < parts_1.length; _a++) {
                var p = parts_1[_a];
                out.set(p, pos);
                pos += p.length;
            }
            return out;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._getIssuer = function (tbsCertBytes) {
            var tbsElement = new unique_encoding_element_1._PdfUniqueEncodingElement();
            tbsElement._fromBytes(tbsCertBytes);
            var elements = tbsElement._getSequence();
            var issuerElement = elements[3];
            return issuerElement;
        };
        _PdfCryptographicMessageSyntaxSigner.prototype._getTimestampAttributes = function (timeStampResponse) {
            var timestampOid = new unique_encoding_element_1._PdfUniqueEncodingElement();
            timestampOid._tagClass = enumerator_1._TagClassType.universal;
            timestampOid._construction = enumerator_1._ConstructionType.primitive;
            timestampOid._setTagNumber(enumerator_1._UniversalType.objectIdentifier);
            timestampOid._setValue(this._encodeObjectIdentifier('1.2.840.113549.1.9.16.2.14'));
            var timestampValue = new unique_encoding_element_1._PdfUniqueEncodingElement();
            timestampValue._tagClass = enumerator_1._TagClassType.universal;
            timestampValue._construction = enumerator_1._ConstructionType.constructed;
            timestampValue._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            timestampValue._setValue(timeStampResponse);
            var timestampSeq = new unique_encoding_element_1._PdfUniqueEncodingElement();
            timestampSeq._tagClass = enumerator_1._TagClassType.universal;
            timestampSeq._construction = enumerator_1._ConstructionType.constructed;
            timestampSeq._setTagNumber(enumerator_1._UniversalType.sequence);
            timestampSeq._setValue(this._encodeSequence([timestampOid, timestampValue]));
            var timestampSet = new unique_encoding_element_1._PdfUniqueEncodingElement();
            timestampSet._tagClass = enumerator_1._TagClassType.universal;
            timestampSet._construction = enumerator_1._ConstructionType.constructed;
            timestampSet._setTagNumber(enumerator_1._UniversalType.abstractSyntaxSet);
            timestampSet._setValue(this._encodeToUniqueElement(timestampSeq));
            return this._encodeToUniqueElement(timestampSet);
        };
        return _PdfCryptographicMessageSyntaxSigner;
    }());
    exports._PdfCryptographicMessageSyntaxSigner = _PdfCryptographicMessageSyntaxSigner;
});