11/*
2- © Copyright IBM Corporation 2019, 2023
2+ © Copyright IBM Corporation 2019, 2024
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import (
3333
3434 "github.com/ibm-messaging/mq-container/internal/keystore"
3535 "github.com/ibm-messaging/mq-container/internal/mqtemplate"
36+ "github.com/ibm-messaging/mq-container/internal/pathutils"
3637 "github.com/ibm-messaging/mq-container/pkg/logger"
3738)
3839
@@ -200,7 +201,7 @@ func generateAllKeystores(keystoreDir string, p12TruststoreRequired bool, native
200201 }
201202 // Create the CMS Keystore if we have been provided keys and certificates
202203 if haveKeysAndCerts (keysDirectory ) || haveKeysAndCerts (trustDirDefault ) {
203- cmsKeystore .Keystore = keystore .NewCMSKeyStore (filepath . Join (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
204+ cmsKeystore .Keystore = keystore .NewCMSKeyStore (pathutils . CleanPath (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
204205 err = cmsKeystore .Keystore .Create ()
205206 if err != nil {
206207 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create CMS Keystore: %v" , err )
@@ -209,7 +210,7 @@ func generateAllKeystores(keystoreDir string, p12TruststoreRequired bool, native
209210
210211 // Create the PKCS#12 Truststore (if required)
211212 if p12TruststoreRequired {
212- p12Truststore .Keystore = keystore .NewPKCS12KeyStore (filepath . Join (keystoreDir , p12TruststoreName ), p12Truststore .Password )
213+ p12Truststore .Keystore = keystore .NewPKCS12KeyStore (pathutils . CleanPath (keystoreDir , p12TruststoreName ), p12Truststore .Password )
213214 err = p12Truststore .Keystore .Create ()
214215 if err != nil {
215216 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create PKCS#12 Truststore: %v" , err )
@@ -230,7 +231,7 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
230231 if err == nil && len (keyList ) > 0 {
231232 // Process each set of keys - each set should contain files: *.key & *.crt
232233 for _ , keySet := range keyList {
233- keys , _ := os .ReadDir (filepath . Join (keyDir , keySet .Name ()))
234+ keys , _ := os .ReadDir (pathutils . CleanPath (keyDir , keySet .Name ()))
234235
235236 // Ensure the label of the set of keys does not match the name of the PKCS#12 Truststore
236237 if keySet .Name () == p12TruststoreName [0 :len (p12TruststoreName )- len (filepath .Ext (p12TruststoreName ))] {
@@ -266,16 +267,17 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
266267 if err != nil {
267268 return "" , fmt .Errorf ("Failed to encode PKCS#12 Keystore %s: %v" , keySet .Name ()+ ".p12" , err )
268269 }
270+ keystorePath := pathutils .CleanPath (keystoreDir , keySet .Name ()+ ".p12" )
269271 // #nosec G306 - this gives permissions to owner/s group only.
270- err = os .WriteFile (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , file , 0644 )
272+ err = os .WriteFile (keystorePath , file , 0644 )
271273 if err != nil {
272- return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
274+ return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , keystorePath , err )
273275 }
274276
275277 // Import the new PKCS#12 Keystore into the CMS Keystore
276- err = tlsStore .Keystore .Keystore .Import (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , tlsStore .Keystore .Password )
278+ err = tlsStore .Keystore .Keystore .Import (keystorePath , tlsStore .Keystore .Password )
277279 if err != nil {
278- return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
280+ return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , keystorePath , err )
279281 }
280282
281283 // Relabel the certificate in the CMS Keystore
@@ -303,14 +305,15 @@ func processTrustCertificates(tlsStore *TLSStore, trustDir string) error {
303305
304306 // Process each set of keys
305307 for _ , trustSet := range trustList {
306- keys , _ := os .ReadDir (filepath . Join (trustDir , trustSet .Name ()))
308+ keys , _ := os .ReadDir (pathutils . CleanPath (trustDir , trustSet .Name ()))
307309
308310 for _ , key := range keys {
309311 if strings .HasSuffix (key .Name (), ".crt" ) {
312+ trustSetPath := pathutils .CleanPath (trustDir , trustSet .Name (), key .Name ())
310313 // #nosec G304 - filename variable is derived from contents of 'trustDir' which is a defined constant
311- file , err := os .ReadFile (filepath . Join ( trustDir , trustSet . Name (), key . Name ()) )
314+ file , err := os .ReadFile (trustSetPath )
312315 if err != nil {
313- return fmt .Errorf ("Failed to read file %s: %v" , filepath . Join ( trustDir , trustSet . Name (), key . Name ()) , err )
316+ return fmt .Errorf ("Failed to read file %s: %v" , trustSetPath , err )
314317 }
315318
316319 for string (file ) != "" {
@@ -366,15 +369,16 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
366369
367370 for _ , key := range keys {
368371
372+ privateKeyPath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
369373 if strings .HasSuffix (key .Name (), ".key" ) {
370374 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
371- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
375+ file , err := os .ReadFile (privateKeyPath )
372376 if err != nil {
373- return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
377+ return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , privateKeyPath , err )
374378 }
375379 block , _ := pem .Decode (file )
376380 if block == nil {
377- return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
381+ return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , privateKeyPath )
378382 }
379383
380384 // Check if the private key is PKCS1
@@ -383,7 +387,7 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
383387 // Check if the private key is PKCS8
384388 privateKey , err = x509 .ParsePKCS8PrivateKey (block .Bytes )
385389 if err != nil {
386- return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
390+ return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , privateKeyPath , err )
387391 }
388392 }
389393 keyPrefix = key .Name ()[0 : len (key .Name ())- len (filepath .Ext (key .Name ()))]
@@ -401,19 +405,20 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
401405
402406 for _ , key := range keys {
403407
408+ keystorePath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
404409 if strings .HasPrefix (key .Name (), keyPrefix ) && strings .HasSuffix (key .Name (), ".crt" ) {
405410 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
406- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
411+ file , err := os .ReadFile (keystorePath )
407412 if err != nil {
408- return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
413+ return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , keystorePath , err )
409414 }
410415 block , _ := pem .Decode (file )
411416 if block == nil {
412- return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
417+ return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , keystorePath )
413418 }
414419 publicCertificate , err = x509 .ParseCertificate (block .Bytes )
415420 if err != nil {
416- return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
421+ return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , keystorePath , err )
417422 }
418423
419424 // Add to known certificates for the CMS Keystore
@@ -424,9 +429,9 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
424429
425430 } else if strings .HasSuffix (key .Name (), ".crt" ) {
426431 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
427- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
432+ file , err := os .ReadFile (keystorePath )
428433 if err != nil {
429- return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
434+ return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , keystorePath , err )
430435 }
431436
432437 for string (file ) != "" {
@@ -452,7 +457,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
452457
453458 certificate , err := x509 .ParseCertificate (block .Bytes )
454459 if err != nil {
455- return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
460+ return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , keystorePath , err )
456461 }
457462 caCertificate = append (caCertificate , certificate )
458463 }
@@ -499,7 +504,7 @@ func relabelCertificate(newLabel string, cmsKeystore *KeyStoreData) error {
499504// addCertificatesToTruststore adds trust certificates to the PKCS#12 Truststore
500505func addCertificatesToTruststore (p12Truststore * KeyStoreData ) error {
501506
502- temporaryPemFile := filepath . Join ("/tmp" , "trust.pem" )
507+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "trust.pem" )
503508 _ , err := os .Stat (temporaryPemFile )
504509 if err == nil {
505510 err = os .Remove (temporaryPemFile )
@@ -541,7 +546,7 @@ func addCertificatesToTruststore(p12Truststore *KeyStoreData) error {
541546// addCertificatesToCMSKeystore adds trust certificates to the CMS keystore
542547func addCertificatesToCMSKeystore (cmsKeystore * KeyStoreData ) error {
543548
544- temporaryPemFile := filepath . Join ("/tmp" , "cmsTrust.pem" )
549+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "cmsTrust.pem" )
545550 _ , err := os .Stat (temporaryPemFile )
546551 if err == nil {
547552 err = os .Remove (temporaryPemFile )
@@ -647,7 +652,7 @@ func haveKeysAndCerts(keyDir string) bool {
647652 for _ , fileInfo := range fileList {
648653 // Keys and certs will be supplied in an user defined subdirectory.
649654 // Do a listing of the subdirectory and then search for .key and .cert files
650- keys , _ := os .ReadDir (filepath . Join (keyDir , fileInfo .Name ()))
655+ keys , _ := os .ReadDir (pathutils . CleanPath (keyDir , fileInfo .Name ()))
651656 for _ , key := range keys {
652657 if strings .HasSuffix (key .Name (), ".key" ) || strings .HasSuffix (key .Name (), ".crt" ) {
653658 // We found at least one key/crt file.
0 commit comments