UNPKG

@syncfusion/ej2-pdf

Version:

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

166 lines (165 loc) 6.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { _ConstructionType } from '../asn1/enumerator'; import { _PdfBasicEncodingElement } from '../asn1/basic-encoding-element'; import { _PdfUniqueEncodingElement } from '../asn1/unique-encoding-element'; import { _PdfObjectIdentifier } from '../asn1/identifier-mapping'; import { _isBasicEncodingElement } from '../asn1/utils'; import { _PdfRonCipherParameter } from './x509-cipher-handler'; import { _PdfX509ExtensionBase, _PdfX509Extensions } from './x509-extensions'; /** * Wrapper around X.509 certificate data and helpers to extract keys and extensions. * * @private */ var _PdfX509Certificate = /** @class */ (function (_super) { __extends(_PdfX509Certificate, _super); function _PdfX509Certificate(certificate) { var _this = _super.call(this) || this; /** * Key usage bits parsed from the certificate extensions. * * @private * @type {boolean[]} */ _this._keyUsage = []; _this._structure = certificate; var keyUsageOid = new _PdfObjectIdentifier()._fromString('2.5.29.15'); var keyUsageExt = _this._getExtension(keyUsageOid); if (keyUsageExt) { var rawBytes = keyUsageExt._getValue(); var asn1Element = _isBasicEncodingElement(rawBytes) ? new _PdfBasicEncodingElement() : new _PdfUniqueEncodingElement(); asn1Element._fromBytes(rawBytes); var bitStringElement = asn1Element._construction === _ConstructionType.constructed ? asn1Element._getInner() : asn1Element; var bitBytes = bitStringElement._getValue(); var unusedBits = bitBytes[0]; var bits_1 = bitBytes.slice(1); var length_1 = (bits_1.length * 8) - unusedBits; _this._keyUsage = Array.from({ length: Math.max(9, length_1) }, function (value, i) { return (bits_1[Math.floor(i / 8)] & (0x80 >> (i % 8))) !== 0; }); } else { _this._keyUsage = null; } return _this; } /** * Retrieve the certificate extensions wrapper when present. * * @private * @returns {_PdfX509Extensions} Certificate extensions container. */ _PdfX509Certificate.prototype._getExtensions = function () { var signed = this._structure._getSignedCertificate(); return signed._getVersion() === 3 ? signed._extensions : new _PdfX509Extensions(); }; /** * Extract and return the public key parameters for this certificate. * * @private * @returns {_PdfCipherParameter} The parsed public key parameters. */ _PdfX509Certificate.prototype._getPublicKey = function () { var signed = this._structure._getSignedCertificate(); return this._createKey(signed._publicKeyInformation); }; /** * Create a cipher parameter object from SubjectPublicKeyInfo. * * @private * @param {_PdfPublicKeyInformation} publicKeyInfo - Parsed public key information. * @returns {_PdfCipherParameter} The created cipher parameter instance. */ _PdfX509Certificate.prototype._createKey = function (publicKeyInfo) { var algOID = publicKeyInfo._algorithms._objectID.toString(); var publicKeyElement = new _PdfUniqueEncodingElement(); publicKeyElement._fromBytes(publicKeyInfo._publicKey._getBytes()); this._publicKeyBytes = publicKeyInfo._publicKey._data; if (algOID === '1.2.840.113549.1.1.1') { return this._parsePublicKey(publicKeyElement); } throw new Error('Unsupported Algorithm'); }; /** * Parse an ASN.1 public key element (e.g., RSA modulus/exponent) into parameters. * * @private * @param {_PdfAbstractSyntaxElement} publicKey - ASN.1 element containing public key data. * @returns {_PdfCipherParameter} The parsed cipher parameters. */ _PdfX509Certificate.prototype._parsePublicKey = function (publicKey) { var seq = publicKey._getSequence(); if (!seq || seq.length < 2) { throw new Error('Invalid RSA public key structure'); } var modulus = seq[0]._getValue(); var exponent = seq[1]._getValue(); return new _PdfRonCipherParameter(true, modulus, exponent); }; /** * Return the DER bytes for the tbsCertificate (to-be-signed certificate). * * @private * @returns {Uint8Array} DER-encoded tbsCertificate bytes. */ _PdfX509Certificate.prototype._getTobeSignedCertificate = function () { var signed = this._structure._getSignedCertificate(); return signed._getDistinguishEncoded(); }; /** * Return the DER encoding for the entire certificate structure. * * @private * @returns {Uint8Array} DER-encoded certificate bytes. */ _PdfX509Certificate.prototype._getEncoded = function () { var asn1Element = this._structure._getDerEncoded(); return asn1Element; }; /** * Alias for `_getEncoded` returning the encoded certificate bytes. * * @private * @returns {Uint8Array} DER-encoded certificate bytes. */ _PdfX509Certificate.prototype._getEncodedString = function () { return this._getEncoded(); }; return _PdfX509Certificate; }(_PdfX509ExtensionBase)); export { _PdfX509Certificate }; /** * Simple container for a certificate and cached hash state. * * @private */ var _PdfX509Certificates = /** @class */ (function () { function _PdfX509Certificates(certificates) { /** * True when `_hashValue` has been computed and stored. * * @private * @type {boolean} */ this._hashValueSet = false; this._certificate = certificates; } return _PdfX509Certificates; }()); export { _PdfX509Certificates };