all files / core/security/digital-signature/x509/ x509-certificate-parser.js

17.46% Statements 11/63
0% Branches 0/35
25% Functions 2/8
17.74% Lines 11/62
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                                                                                                                                                                
define(["require", "exports", "./../asn1/enumerator", "../asn1/basic-encoding-element", "./x509-certificate", "./x509-certificate-structure"], function (require, exports, enumerator_1, basic_encoding_element_1, x509_certificate_1, x509_certificate_structure_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfX509CertificateParser = (function () {
        function _PdfX509CertificateParser() {
            this._sDataObjectCount = 0;
        }
        _PdfX509CertificateParser.prototype._readDistinguishEncoderCertificate = function (bytes, isCertificateParsing) {
            var structure = new x509_certificate_structure_1._PdfX509CertificateStructure();
            var stream = new basic_encoding_element_1._PdfBasicEncodingElement();
            stream._fromBytes(bytes);
            var seq = stream._getSequence();
            var tagNumber = seq[0]._getTagNumber();
            if (seq.length > 1 && tagNumber === enumerator_1._UniversalType.objectIdentifier) {
                var oid = seq[0]._getObjectIdentifier();
                var dotDelimitedNotation = oid._getDotDelimitedNotation();
                if (dotDelimitedNotation === '1.2.840.113549.1.7.2') {
                    if (seq.length >= 2) {
                        var inner = seq[1]._getInner();
                        var innerSequence = inner._getSequence();
                        var signedSequence = innerSequence;
                        for (var _i = 0, signedSequence_1 = signedSequence; _i < signedSequence_1.length; _i++) {
                            var element = signedSequence_1[_i];
                            if (element instanceof basic_encoding_element_1._PdfBasicEncodingElement && element._tagClass === enumerator_1._TagClassType.context
                                && element._getTagNumber() === 0) {
                                var inner_1 = element._getInner(isCertificateParsing);
                                var innerSet = void 0;
                                if (isCertificateParsing) {
                                    innerSet = inner_1._getSequence();
                                }
                                else {
                                    innerSet = inner_1._getAbstractSetValue();
                                }
                                this._sData = innerSet;
                                break;
                            }
                        }
                    }
                    return this._getCertificate(isCertificateParsing);
                }
            }
            return this._createX509Certificate(structure._getInstance(seq));
        };
        _PdfX509CertificateParser.prototype._getCertificate = function (isCertificateParsing) {
            var structure = new x509_certificate_structure_1._PdfX509CertificateStructure();
            if (this._sData) {
                if (!isCertificateParsing) {
                    while (this._sDataObjectCount < this._sData.length) {
                        var obj = this._sData[this._sDataObjectCount++];
                        var tagNumber = obj._getTagNumber();
                        if (tagNumber === enumerator_1._UniversalType.sequence) {
                            return this._createX509Certificate(structure._getInstance(obj._getSequence()));
                        }
                    }
                }
                else {
                    return this._createX509Certificate(structure._getInstance(this._sData));
                }
            }
            return null;
        };
        _PdfX509CertificateParser.prototype._createX509Certificate = function (structure) {
            return new x509_certificate_1._PdfX509Certificate(structure);
        };
        _PdfX509CertificateParser.prototype._readCertificate = function (input, isCertificateParsing) {
            if (isCertificateParsing === void 0) { isCertificateParsing = false; }
            return this._readCertificateFromStream(input, isCertificateParsing);
        };
        _PdfX509CertificateParser.prototype._readCertificateFromStream = function (stream, isCertificateParsing) {
            if (!stream || stream.length === 0) {
                throw new Error('Input stream is empty or null');
            }
            if (this._currentStream !== stream) {
                this._currentStream = stream;
                this._sData = null;
                this._sDataObjectCount = 0;
            }
            if (this._sData && this._sDataObjectCount < this._sData.length) {
                return this._getCertificate();
            }
            var tag = stream[0];
            if (tag < 0) {
                return null;
            }
            return this._readDistinguishEncoderCertificate(stream, isCertificateParsing);
        };
        return _PdfX509CertificateParser;
    }());
    exports._PdfX509CertificateParser = _PdfX509CertificateParser;
});