2020-08-11 11:29:50 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package certs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRootCAs - returns all the root CAs into certPool
|
|
|
|
// at the input certsCADir
|
|
|
|
func GetRootCAs(certsCAsDir string) (*x509.CertPool, error) {
|
2020-12-02 05:23:51 -05:00
|
|
|
rootCAs, _ := loadSystemRoots()
|
2020-08-11 11:29:50 -04:00
|
|
|
if rootCAs == nil {
|
2020-12-02 05:23:51 -05:00
|
|
|
// In some systems system cert pool is not supported
|
|
|
|
// or no certificates are present on the
|
2020-08-11 11:29:50 -04:00
|
|
|
// system - so we create a new cert pool.
|
|
|
|
rootCAs = x509.NewCertPool()
|
|
|
|
}
|
|
|
|
|
|
|
|
fis, err := ioutil.ReadDir(certsCAsDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) || os.IsPermission(err) {
|
|
|
|
// Return success if CA's directory is missing or permission denied.
|
2020-12-16 21:24:12 -05:00
|
|
|
return rootCAs, nil
|
2020-08-11 11:29:50 -04:00
|
|
|
}
|
|
|
|
return rootCAs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load all custom CA files.
|
|
|
|
for _, fi := range fis {
|
|
|
|
caCert, err := ioutil.ReadFile(path.Join(certsCAsDir, fi.Name()))
|
2020-12-16 21:24:12 -05:00
|
|
|
if err == nil {
|
|
|
|
rootCAs.AppendCertsFromPEM(caCert)
|
2020-08-11 11:29:50 -04:00
|
|
|
}
|
2020-12-16 21:24:12 -05:00
|
|
|
// ignore files which are not readable.
|
2020-08-11 11:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rootCAs, nil
|
|
|
|
}
|