@@ -9,6 +9,13 @@ var ApiException = require('./ApiException');
9
9
var Logger = require ( '../logging/Logger' ) ;
10
10
var Utility = require ( './Utility' ) ;
11
11
12
+ function loadP12FileToAsn1 ( filePath ) {
13
+ var p12Buffer = fs . readFileSync ( filePath ) ;
14
+ var p12Der = forge . util . binary . raw . encode ( new Uint8Array ( p12Buffer ) ) ;
15
+ var p12Asn1 = forge . asn1 . fromDer ( p12Der ) ;
16
+ return p12Asn1 ;
17
+ }
18
+
12
19
13
20
/**
14
21
* This module is doing Caching.
@@ -19,7 +26,7 @@ exports.fetchCachedCertificate = function (merchantConfig, logger) {
19
26
var cachedCertificateFromP12File = cache . get ( "certificateFromP12File" ) ;
20
27
var cachedLastModifiedTimeStamp = cache . get ( "certificateLastModifideTimeStamp" ) ;
21
28
22
- var filePath = path . resolve ( path . join ( merchantConfig . getKeysDirectory ( ) , merchantConfig . getKeyFileName ( ) + '.p12' ) ) ;
29
+ var filePath = merchantConfig . getP12FilePath ( ) ;
23
30
if ( fs . existsSync ( filePath ) ) {
24
31
const stats = fs . statSync ( filePath ) ;
25
32
const currentFileLastModifiedTime = stats . mtime ;
@@ -48,9 +55,7 @@ exports.fetchCachedCertificate = function (merchantConfig, logger) {
48
55
//Function to read the file and put values to new cache
49
56
function getCertificate ( keyPass , filePath , fileLastModifiedTime , logger ) {
50
57
try {
51
- var p12Buffer = fs . readFileSync ( filePath ) ;
52
- var p12Der = forge . util . binary . raw . encode ( new Uint8Array ( p12Buffer ) ) ;
53
- var p12Asn1 = forge . asn1 . fromDer ( p12Der ) ;
58
+ var p12Asn1 = loadP12FileToAsn1 ( filePath ) ;
54
59
var certificate = forge . pkcs12 . pkcs12FromAsn1 ( p12Asn1 , false , keyPass ) ;
55
60
cache . put ( "certificateFromP12File" , certificate ) ;
56
61
cache . put ( "certificateLastModifideTimeStamp" , fileLastModifiedTime ) ;
@@ -90,13 +95,7 @@ exports.getRequestMLECertFromCache = function(merchantConfig) {
90
95
cacheKey = merchantId + Constants . MLE_CACHE_IDENTIFIER_FOR_CONFIG_CERT ;
91
96
mleCertPath = merchantConfig . getMleForRequestPublicCertPath ( ) ;
92
97
} else if ( Constants . JWT === merchantConfig . getAuthenticationType ( ) . toLowerCase ( ) ) {
93
- mleCertPath = path . resolve ( path . join ( merchantConfig . getKeysDirectory ( ) , merchantConfig . getKeyFileName ( ) + '.p12' ) ) ;
94
- try {
95
- fs . accessSync ( mleCertPath , fs . constants . R_OK ) ;
96
- } catch ( err ) {
97
- logger . warn ( "MLE certificate file not found or not readable: " + mleCertPath ) ;
98
- return null ;
99
- }
98
+ mleCertPath = merchantConfig . getP12FilePath ( ) ;
100
99
cacheKey = merchantId + Constants . MLE_CACHE_IDENTIFIER_FOR_P12_CERT ;
101
100
} else {
102
101
logger . debug ( "The certificate to use for MLE for requests is not provided in the merchant configuration. Please ensure that the certificate path is provided." ) ;
@@ -138,10 +137,8 @@ function setupMLECache(merchantConfig, cacheKey, mleCertPath) {
138
137
function loadCertificateFromP12 ( merchantConfig , mleCertPath ) {
139
138
const logger = Logger . getLogger ( merchantConfig , 'Cache' ) ;
140
139
try {
141
- // Read the P12 file as before
142
- var p12Buffer = fs . readFileSync ( mleCertPath ) ;
143
- var p12Der = forge . util . binary . raw . encode ( new Uint8Array ( p12Buffer ) ) ;
144
- var p12Asn1 = forge . asn1 . fromDer ( p12Der ) ;
140
+ // Read the P12 file and convert to ASN1
141
+ var p12Asn1 = loadP12FileToAsn1 ( mleCertPath ) ;
145
142
var p12Cert = forge . pkcs12 . pkcs12FromAsn1 ( p12Asn1 , false , merchantConfig . getKeyPass ( ) ) ;
146
143
147
144
// Extract the certificate from the P12 container
0 commit comments