UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

138 lines (137 loc) 6.21 kB
import { _UniversalType, _TagClassType } from './../asn1/enumerator'; import { _PdfBasicEncodingElement } from '../asn1/basic-encoding-element'; import { _PdfX509Certificate } from './x509-certificate'; import { _PdfX509CertificateStructure } from './x509-certificate-structure'; /** * Parser utilities for reading X.509 certificates from ASN.1 encoded streams. * * @private */ var _PdfX509CertificateParser = /** @class */ (function () { function _PdfX509CertificateParser() { /** * Cursor index into `_sData` when iterating parsed objects. * */ this._sDataObjectCount = 0; } /** * Read and distinguish between embedded PKCS#7 SignedData and direct certificates. * * @private * @param {Uint8Array} bytes - Input ASN.1 byte stream. * @param {boolean} isCertificateParsing - When true, parse as a leaf certificate. * @returns {_PdfX509Certificate} Parsed X.509 certificate instance. */ _PdfX509CertificateParser.prototype._readDistinguishEncoderCertificate = function (bytes, isCertificateParsing) { var structure = new _PdfX509CertificateStructure(); var stream = new _PdfBasicEncodingElement(); stream._fromBytes(bytes); var seq = stream._getSequence(); var tagNumber = seq[0]._getTagNumber(); if (seq.length > 1 && tagNumber === _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 _PdfBasicEncodingElement && element._tagClass === _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)); }; /** * Retrieve the next certificate from cached sequence data or construct from sequence. * * @private * @param {boolean} [isCertificateParsing] - Whether parsing mode affects extraction. * @returns {_PdfX509Certificate} Parsed X.509 certificate or null. */ _PdfX509CertificateParser.prototype._getCertificate = function (isCertificateParsing) { var structure = new _PdfX509CertificateStructure(); if (this._sData) { if (!isCertificateParsing) { while (this._sDataObjectCount < this._sData.length) { var obj = this._sData[this._sDataObjectCount++]; var tagNumber = obj._getTagNumber(); if (tagNumber === _UniversalType.sequence) { return this._createX509Certificate(structure._getInstance(obj._getSequence())); } } } else { return this._createX509Certificate(structure._getInstance(this._sData)); } } return null; }; /** * Create an `_PdfX509Certificate` wrapper from a parsed certificate structure. * * @private * @param {_PdfX509CertificateStructure} structure - Parsed certificate structure. * @returns {_PdfX509Certificate} The constructed certificate object. */ _PdfX509CertificateParser.prototype._createX509Certificate = function (structure) { return new _PdfX509Certificate(structure); }; /** * Read a certificate from the provided input bytes. Delegates to stream reader. * * @private * @param {Uint8Array} input - ASN.1 encoded input bytes. * @param {boolean} [isCertificateParsing=false] - When true, parse as certificate stream. * @returns {_PdfX509Certificate} The parsed certificate. */ _PdfX509CertificateParser.prototype._readCertificate = function (input, isCertificateParsing) { if (isCertificateParsing === void 0) { isCertificateParsing = false; } return this._readCertificateFromStream(input, isCertificateParsing); }; /** * Read a certificate from a stream buffer, preserving internal parser state. * * @private * @param {Uint8Array} stream - The stream buffer containing ASN.1 data. * @param {boolean} isCertificateParsing - Whether parsing mode should extract certificate only. * @returns {_PdfX509Certificate} The parsed X.509 certificate instance. */ _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; }()); export { _PdfX509CertificateParser };