1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . ExternalSigner = void 0 ;
7+ var pkijs = _interopRequireWildcard ( require ( "pkijs" ) ) ;
8+ var _SignPdfError = require ( "./SignPdfError" ) ;
9+ var _Signer = require ( "./Signer" ) ;
10+ function _getRequireWildcardCache ( nodeInterop ) { if ( typeof WeakMap !== "function" ) return null ; var cacheBabelInterop = new WeakMap ( ) ; var cacheNodeInterop = new WeakMap ( ) ; return ( _getRequireWildcardCache = function ( nodeInterop ) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop ; } ) ( nodeInterop ) ; }
11+ function _interopRequireWildcard ( obj , nodeInterop ) { if ( ! nodeInterop && obj && obj . __esModule ) { return obj ; } if ( obj === null || typeof obj !== "object" && typeof obj !== "function" ) { return { default : obj } ; } var cache = _getRequireWildcardCache ( nodeInterop ) ; if ( cache && cache . has ( obj ) ) { return cache . get ( obj ) ; } var newObj = { } ; var hasPropertyDescriptor = Object . defineProperty && Object . getOwnPropertyDescriptor ; for ( var key in obj ) { if ( key !== "default" && Object . prototype . hasOwnProperty . call ( obj , key ) ) { var desc = hasPropertyDescriptor ? Object . getOwnPropertyDescriptor ( obj , key ) : null ; if ( desc && ( desc . get || desc . set ) ) { Object . defineProperty ( newObj , key , desc ) ; } else { newObj [ key ] = obj [ key ] ; } } } newObj . default = obj ; if ( cache ) { cache . set ( obj , newObj ) ; } return newObj ; }
12+ /* eslint-disable no-unused-vars */
13+
14+ /**
15+ * Abstract ExternalSigner class taking care of creating a suitable signature for a given pdf
16+ * using an external signature provider.
17+ * Subclasses should specify the required signature and hashing algorithms used by the external
18+ * provider (either through the `signAlgorithm` and `hashAlgorithm` attributes, or by overriding
19+ * the `getSignAlgorithm` and `getHashAlgorithm` methods), as well as provide the used signing
20+ * certificate and final signature (by implementing the `getCertificate` and `getSignature`
21+ * methods).
22+ */
23+ class ExternalSigner extends _Signer . Signer {
24+ /**
25+ * Method to retrieve the signature of the given hash (of the given data) from the external
26+ * service. The original data is included in case the external signature provider computes
27+ * the hash automatically before signing.
28+ * To be implemented by subclasses.
29+ * @param {Uint8Array } hash
30+ * @param {Uint8Array } data
31+ * @returns {Promise<Uint8Array> }
32+ */
33+ async getSignature ( hash , data ) {
34+ throw new _SignPdfError . SignPdfError ( `getSignature() is not implemented on ${ this . constructor . name } ` , _SignPdfError . SignPdfError . TYPE_INPUT ) ;
35+ }
36+
37+ /**
38+ * Get a "crypto" extension and override the function used by SignedData.sign to support
39+ * external signing.
40+ * @returns {pkijs.ICryptoEngine }
41+ */
42+ getCrypto ( ) {
43+ const crypto = super . getCrypto ( ) ;
44+ crypto . sign = async ( _algo , _key , data ) => {
45+ // Calculate hash
46+ const hash = await crypto . digest ( {
47+ name : this . hashAlgorithm
48+ } , data ) ;
49+ // And pass it to the external signature provider
50+ const signature = await this . getSignature ( Buffer . from ( hash ) , Buffer . from ( data ) ) ;
51+ return signature ;
52+ } ;
53+ return crypto ;
54+ }
55+
56+ /**
57+ * Obtain a dummy private key to pass the correct signing parameters to the sign function.
58+ * @returns {CryptoKey }
59+ */
60+ async obtainKey ( ) {
61+ // The algorithm parameters cannot be passed directly to the SignedData.sign function, so we
62+ // need to generate a dummy private key with the required parameters and pass that to the
63+ // sign function. The private key is not actually used for signing, as we override the
64+ // crypto.sign function in the getCrypto method.
65+ const algorithmParams = this . crypto . getAlgorithmParameters ( this . signAlgorithm , 'generatekey' ) . algorithm ;
66+ const keypair = await this . crypto . generateKey ( {
67+ name : this . signAlgorithm ,
68+ ...algorithmParams ,
69+ hash : {
70+ name : this . hashAlgorithm
71+ }
72+ } , false , [ 'sign' , 'verify' ] ) ;
73+ return keypair . privateKey ;
74+ }
75+ }
76+ exports . ExternalSigner = ExternalSigner ;
0 commit comments