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
@@ -191,7 +192,7 @@ func generateAllKeystores(keystoreDir string, p12TruststoreRequired bool, native
191192 }
192193 // Create the CMS Keystore if we have been provided keys and certificates
193194 if haveKeysAndCerts (keysDirectory ) || haveKeysAndCerts (trustDirDefault ) {
194- cmsKeystore .Keystore = keystore .NewCMSKeyStore (filepath . Join (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
195+ cmsKeystore .Keystore = keystore .NewCMSKeyStore (pathutils . CleanPath (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
195196 err = cmsKeystore .Keystore .Create ()
196197 if err != nil {
197198 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create CMS Keystore: %v" , err )
@@ -200,7 +201,7 @@ func generateAllKeystores(keystoreDir string, p12TruststoreRequired bool, native
200201
201202 // Create the PKCS#12 Truststore (if required)
202203 if p12TruststoreRequired {
203- p12Truststore .Keystore = keystore .NewPKCS12KeyStore (filepath . Join (keystoreDir , p12TruststoreName ), p12Truststore .Password )
204+ p12Truststore .Keystore = keystore .NewPKCS12KeyStore (pathutils . CleanPath (keystoreDir , p12TruststoreName ), p12Truststore .Password )
204205 err = p12Truststore .Keystore .Create ()
205206 if err != nil {
206207 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create PKCS#12 Truststore: %v" , err )
@@ -221,7 +222,7 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
221222 if err == nil && len (keyList ) > 0 {
222223 // Process each set of keys - each set should contain files: *.key & *.crt
223224 for _ , keySet := range keyList {
224- keys , _ := os .ReadDir (filepath . Join (keyDir , keySet .Name ()))
225+ keys , _ := os .ReadDir (pathutils . CleanPath (keyDir , keySet .Name ()))
225226
226227 // Ensure the label of the set of keys does not match the name of the PKCS#12 Truststore
227228 if keySet .Name () == p12TruststoreName [0 :len (p12TruststoreName )- len (filepath .Ext (p12TruststoreName ))] {
@@ -263,16 +264,17 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
263264 if err != nil {
264265 return "" , fmt .Errorf ("Failed to encode PKCS#12 Keystore %s: %v" , keySet .Name ()+ ".p12" , err )
265266 }
267+ keystorePath := pathutils .CleanPath (keystoreDir , keySet .Name ()+ ".p12" )
266268 // #nosec G306 - this gives permissions to owner/s group only.
267- err = os .WriteFile (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , file , 0644 )
269+ err = os .WriteFile (keystorePath , file , 0644 )
268270 if err != nil {
269- return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
271+ return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , keystorePath , err )
270272 }
271273
272274 // Import the new PKCS#12 Keystore into the CMS Keystore
273- err = tlsStore .Keystore .Keystore .Import (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , tlsStore .Keystore .Password )
275+ err = tlsStore .Keystore .Keystore .Import (keystorePath , tlsStore .Keystore .Password )
274276 if err != nil {
275- return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
277+ return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , keystorePath , err )
276278 }
277279
278280 // Relabel the certificate in the CMS Keystore
@@ -300,14 +302,15 @@ func processTrustCertificates(tlsStore *TLSStore, trustDir string) error {
300302
301303 // Process each set of keys
302304 for _ , trustSet := range trustList {
303- keys , _ := os .ReadDir (filepath . Join (trustDir , trustSet .Name ()))
305+ keys , _ := os .ReadDir (pathutils . CleanPath (trustDir , trustSet .Name ()))
304306
305307 for _ , key := range keys {
306308 if strings .HasSuffix (key .Name (), ".crt" ) {
309+ trustSetPath := pathutils .CleanPath (trustDir , trustSet .Name (), key .Name ())
307310 // #nosec G304 - filename variable is derived from contents of 'trustDir' which is a defined constant
308- file , err := os .ReadFile (filepath . Join ( trustDir , trustSet . Name (), key . Name ()) )
311+ file , err := os .ReadFile (trustSetPath )
309312 if err != nil {
310- return fmt .Errorf ("Failed to read file %s: %v" , filepath . Join ( trustDir , trustSet . Name (), key . Name ()) , err )
313+ return fmt .Errorf ("Failed to read file %s: %v" , trustSetPath , err )
311314 }
312315
313316 for string (file ) != "" {
@@ -363,15 +366,16 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
363366
364367 for _ , key := range keys {
365368
369+ privateKeyPath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
366370 if strings .HasSuffix (key .Name (), ".key" ) {
367371 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
368- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
372+ file , err := os .ReadFile (privateKeyPath )
369373 if err != nil {
370- return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
374+ return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , privateKeyPath , err )
371375 }
372376 block , _ := pem .Decode (file )
373377 if block == nil {
374- return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
378+ return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , privateKeyPath )
375379 }
376380
377381 // Check if the private key is PKCS1
@@ -380,7 +384,7 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
380384 // Check if the private key is PKCS8
381385 privateKey , err = x509 .ParsePKCS8PrivateKey (block .Bytes )
382386 if err != nil {
383- return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
387+ return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , privateKeyPath , err )
384388 }
385389 }
386390 keyPrefix = key .Name ()[0 : len (key .Name ())- len (filepath .Ext (key .Name ()))]
@@ -398,19 +402,20 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
398402
399403 for _ , key := range keys {
400404
405+ keystorePath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
401406 if strings .HasPrefix (key .Name (), keyPrefix ) && strings .HasSuffix (key .Name (), ".crt" ) {
402407 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
403- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
408+ file , err := os .ReadFile (keystorePath )
404409 if err != nil {
405- return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
410+ return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , keystorePath , err )
406411 }
407412 block , _ := pem .Decode (file )
408413 if block == nil {
409- return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
414+ return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , keystorePath )
410415 }
411416 publicCertificate , err = x509 .ParseCertificate (block .Bytes )
412417 if err != nil {
413- return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
418+ return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , keystorePath , err )
414419 }
415420
416421 // Add to known certificates for the CMS Keystore
@@ -421,9 +426,9 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
421426
422427 } else if strings .HasSuffix (key .Name (), ".crt" ) {
423428 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
424- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
429+ file , err := os .ReadFile (keystorePath )
425430 if err != nil {
426- return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
431+ return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , keystorePath , err )
427432 }
428433
429434 for string (file ) != "" {
@@ -449,7 +454,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
449454
450455 certificate , err := x509 .ParseCertificate (block .Bytes )
451456 if err != nil {
452- return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
457+ return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , keystorePath , err )
453458 }
454459 caCertificate = append (caCertificate , certificate )
455460 }
@@ -496,7 +501,7 @@ func relabelCertificate(newLabel string, cmsKeystore *KeyStoreData) error {
496501// addCertificatesToTruststore adds trust certificates to the PKCS#12 Truststore
497502func addCertificatesToTruststore (p12Truststore * KeyStoreData ) error {
498503
499- temporaryPemFile := filepath . Join ("/tmp" , "trust.pem" )
504+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "trust.pem" )
500505 _ , err := os .Stat (temporaryPemFile )
501506 if err == nil {
502507 err = os .Remove (temporaryPemFile )
@@ -538,7 +543,7 @@ func addCertificatesToTruststore(p12Truststore *KeyStoreData) error {
538543// addCertificatesToCMSKeystore adds trust certificates to the CMS keystore
539544func addCertificatesToCMSKeystore (cmsKeystore * KeyStoreData ) error {
540545
541- temporaryPemFile := filepath . Join ("/tmp" , "cmsTrust.pem" )
546+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "cmsTrust.pem" )
542547 _ , err := os .Stat (temporaryPemFile )
543548 if err == nil {
544549 err = os .Remove (temporaryPemFile )
@@ -644,7 +649,7 @@ func haveKeysAndCerts(keyDir string) bool {
644649 for _ , fileInfo := range fileList {
645650 // Keys and certs will be supplied in an user defined subdirectory.
646651 // Do a listing of the subdirectory and then search for .key and .cert files
647- keys , _ := os .ReadDir (filepath . Join (keyDir , fileInfo .Name ()))
652+ keys , _ := os .ReadDir (pathutils . CleanPath (keyDir , fileInfo .Name ()))
648653 for _ , key := range keys {
649654 if strings .HasSuffix (key .Name (), ".key" ) || strings .HasSuffix (key .Name (), ".crt" ) {
650655 // We found at least one key/crt file.
0 commit comments