11const forge = require ( 'node-forge' ) ;
2- const fs = require ( 'fs' ) ;
32const utils = require ( '../utils/utils' ) ;
43const nodeCrypto = require ( 'crypto' ) ;
54
@@ -19,7 +18,7 @@ function JweCrypto(config) {
1918
2019 this . privateKey = getPrivateKey ( config ) ;
2120
22- this . publicKeyFingerprint = config . publicKeyFingerprint || computePublicFingerprint . call ( this , config ) ;
21+ this . publicKeyFingerprint = config . publicKeyFingerprint || computePublicFingerprint ( config , this . encryptionCertificate ) ;
2322
2423 this . encryptedValueFieldName = config . encryptedValueFieldName ;
2524
@@ -40,7 +39,7 @@ function JweCrypto(config) {
4039 "enc" : "A256GCM"
4140 } ;
4241
43- const encodedJweHeader = Buffer . from ( JSON . stringify ( jweHeader ) ) . toString ( ' base64url') ;
42+ const encodedJweHeader = Buffer . from ( utils . toEncodedString ( JSON . stringify ( jweHeader ) , 'binary' , ' base64url') ) ;
4443
4544 const secretKey = nodeCrypto . randomBytes ( 32 ) ;
4645 const secretKeyBuffer = Buffer . from ( secretKey , 'binary' ) ;
@@ -62,10 +61,10 @@ function JweCrypto(config) {
6261 cipherText += cipher . final ( 'base64' ) ;
6362 const authTag = cipher . getAuthTag ( ) . toString ( "base64" ) ;
6463
65- const encodedEncryptedSecretKey = Buffer . from ( encryptedSecretKey , 'binary' ) . toString ( 'base64url' ) ;
66- const encodedIv = Buffer . from ( iv , 'binary' ) . toString ( 'base64url' ) ;
67- const encodedEncryptedText = Buffer . from ( cipherText , "base64" ) . toString ( 'base64url' ) ;
68- const encodedAuthTag = Buffer . from ( authTag , "base64" ) . toString ( 'base64url' ) ;
64+ const encodedEncryptedSecretKey = utils . toEncodedString ( encryptedSecretKey , 'binary' , 'base64url' ) ;
65+ const encodedIv = utils . toEncodedString ( iv , 'binary' , 'base64url' ) ;
66+ const encodedEncryptedText = utils . toEncodedString ( cipherText , "base64" , 'base64url' ) ;
67+ const encodedAuthTag = utils . toEncodedString ( authTag , "base64" , 'base64url' ) ;
6968
7069 const encryptedData = serialize ( encodedJweHeader , encodedEncryptedSecretKey , encodedIv , encodedEncryptedText , encodedAuthTag ) ;
7170 return { [ this . encryptedValueFieldName ] : encryptedData } ;
@@ -83,18 +82,16 @@ function JweCrypto(config) {
8382 }
8483
8584 const jweTokenParts = deSerialize ( encryptedData ) ;
86- const jweHeader = Buffer . from ( jweTokenParts [ 0 ] , 'base64url' ) . toString ( 'utf8' ) ;
87- const encryptedSecretKey = Buffer . from ( jweTokenParts [ 1 ] , 'base64url' ) ;
88- const iv = Buffer . from ( jweTokenParts [ 2 ] , 'base64url' ) ;
89- const encryptedText = Buffer . from ( jweTokenParts [ 3 ] , 'base64url' ) ;
90- const authTag = Buffer . from ( jweTokenParts [ 4 ] , 'base64url' ) ;
91-
92- // const node10 = utils.isNode10()
85+ const jweHeader = Buffer . from ( jweTokenParts [ 0 ] , 'base64' ) . toString ( 'utf8' ) ;
86+ const encryptedSecretKey = Buffer . from ( jweTokenParts [ 1 ] , 'base64' ) ;
87+ const iv = Buffer . from ( jweTokenParts [ 2 ] , 'base64' ) ;
88+ const encryptedText = Buffer . from ( jweTokenParts [ 3 ] , 'base64' ) ;
89+ const authTag = Buffer . from ( jweTokenParts [ 4 ] , 'base64' ) ;
9390
9491 let secretKey = nodeCrypto . privateDecrypt (
9592 {
9693 key : this . privateKey ,
97- padding : nodeCrypto . constants . RSA_PKCS1_OAEP_PADDING ,
94+ padding : nodeCrypto . constants . RSA_NO_PADDING ,
9895 oaepHash : "sha256"
9996 } ,
10097 Buffer . from ( encryptedSecretKey , 'binary' )
@@ -143,11 +140,12 @@ function deSerialize(jweToken) {
143140 * @private
144141 */
145142function readPublicCertificate ( publicCertificatePath ) {
146- const certificateContent = fs . readFileSync ( publicCertificatePath , 'binary' ) ;
147- if ( ! certificateContent || certificateContent . length <= 1 ) {
148- throw new Error ( 'Public certificate content is not valid' ) ;
143+ const publicCertificate = utils . readPublicCertificate ( publicCertificatePath ) ;
144+ if ( publicCertificate ) {
145+ return forge . pki . certificateToPem ( publicCertificate ) ;
146+ } else {
147+ return null ;
149148 }
150- return certificateContent ;
151149}
152150
153151/**
@@ -166,52 +164,14 @@ function getPrivateKey(config) {
166164 * @private
167165 * @param config Configuration object
168166 */
169- function computePublicFingerprint ( config ) {
170- let fingerprint = null ;
171- if ( config && config . publicKeyFingerprintType ) {
172- switch ( config . publicKeyFingerprintType ) {
173- case "certificate" :
174- fingerprint = publicCertificateFingerprint . call ( this , this . encryptionCertificate ) ;
175- break ;
176- case "publicKey" :
177- fingerprint = publicKeyFingerprint . call ( this , this . encryptionCertificate ) ;
178- break ;
179- }
180- }
181- return fingerprint ;
182- }
183-
184- /**
185- * Get SHA-256 certificate fingerprint from a X509 certificate
186- *
187- * @private
188- * @param {string } publicCertificate PEM
189- */
190- function publicCertificateFingerprint ( publicCertificate ) {
191- if ( ! publicCertificate || publicCertificate . length <= 1 ) {
192- throw new Error ( 'Public certificate content is not valid' ) ;
193- }
194- let hexFingerprint = new nodeCrypto . X509Certificate ( publicCertificate ) . fingerprint256 ;
195- hexFingerprint = hexFingerprint . replaceAll ( ":" , "" ) ;
196- return Buffer . from ( hexFingerprint , 'hex' ) . toString ( 'base64' ) ;
197- }
198-
199- /**
200- * Get SHA-256 public Key fingerprint from a X509 certificate
201- *
202- * @private
203- * @param {string } publicCertificate PEM
204- */
205- function publicKeyFingerprint ( publicKey ) {
206- if ( ! publicKey || publicKey . length <= 1 ) {
207- throw new Error ( 'Public certificate content is not valid' ) ;
167+ function computePublicFingerprint ( config , encryptionCertificate ) {
168+ if ( config && encryptionCertificate ) {
169+ return utils . computePublicFingerprint ( config , forge . pki . certificateFromPem ( encryptionCertificate ) , 'base64' ) ;
170+ } else {
171+ return null ;
208172 }
209- let hexFingerprint = new nodeCrypto . X509Certificate ( publicKey ) . fingerprint256 ;
210- hexFingerprint = hexFingerprint . replaceAll ( ":" , "" ) ;
211- return Buffer . from ( hexFingerprint , 'hex' ) . toString ( 'base64' ) ;
212173}
213174
214-
215175/**
216176 * Check if the passed configuration is valid
217177 * @throws {Error } if the config is not valid
0 commit comments