Debian Codegraph @

verifier/truststore/truststore.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package truststore reads certificates in a trust store
15 package truststore
16
17 import (
18 "bytes"
19 "context"
20 "crypto/x509"
21 "errors"
22 "fmt"
23 "io/fs"
24 "os"
25 "path/filepath"
26
27 corex509 "github.com/notaryproject/notation-core-go/x509"
28 "github.com/notaryproject/notation-go/dir"
29 "github.com/notaryproject/notation-go/internal/file"
30 "github.com/notaryproject/notation-go/internal/slices"
31 )
32
33 // Type is an enum for trust store types supported such as
34 // "ca" and "signingAuthority"
35 type Type string
36
37 const (
38 TypeCA Type = "ca"
39 TypeSigningAuthority Type = "signingAuthority"
40 TypeTSA Type = "tsa"
41 )
42
43 var (
44 Types = []Type{
45 TypeCA,
46 TypeSigningAuthority,
47 TypeTSA,
48 }
49 )
50
51 // X509TrustStore provide list and get behaviors for the trust store
52 type X509TrustStore interface {
53 // GetCertificates returns certificates under storeType/namedStore
54 GetCertificates(ctx context.Context, storeType Type, namedStore string) ([]*x509.Certificate, error)
55 }
56
57 // NewX509TrustStore generates a new X509TrustStore
58 func NewX509TrustStore(trustStorefs dir.SysFS) X509TrustStore {
59 return &x509TrustStore{trustStorefs}
60 }
61
62 // x509TrustStore implements X509TrustStore
63 type x509TrustStore struct {
64 trustStorefs dir.SysFS
65 }
66
67 // GetCertificates returns certificates under storeType/namedStore
68 func (trustStore *x509TrustStore) GetCertificates(ctx context.Context, storeType Type, namedStore string) ([]*x509.Certificate, error) {
69 if !isValidStoreType(storeType) {
70 return nil, TrustStoreError{Msg: fmt.Sprintf("unsupported trust store type: %s", storeType)}
71 }
72 if !file.IsValidFileName(namedStore) {
73 return nil, TrustStoreError{Msg: fmt.Sprintf("trust store name needs to follow [a-zA-Z0-9_.-]+ format, %s is invalid", namedStore)}
74 }
75 path, err := trustStore.trustStorefs.SysPath(dir.X509TrustStoreDir(string(storeType), namedStore))
76 if err != nil {
77 return nil, TrustStoreError{InnerError: err, Msg: fmt.Sprintf("failed to get path of trust store %s of type %s", namedStore, storeType)}
78 }
79 // throw error if path is not a directory or is a symlink or does not exist.
80 fileInfo, err := os.Lstat(path)
81 if err != nil {
82 if os.IsNotExist(err) {
83 return nil, TrustStoreError{InnerError: err, Msg: fmt.Sprintf("the trust store %q of type %q does not exist", namedStore, storeType)}
84 }
85 return nil, TrustStoreError{InnerError: err, Msg: fmt.Sprintf("failed to access the trust store %q of type %q", namedStore, storeType)}
86 }
87 mode := fileInfo.Mode()
88 if !mode.IsDir() || mode&fs.ModeSymlink != 0 {
89 return nil, TrustStoreError{Msg: fmt.Sprintf("the trust store %s of type %s with path %s is not a regular directory (symlinks are not supported)", namedStore, storeType, path)}
90 }
91 files, err := os.ReadDir(path)
92 if err != nil {
93 return nil, TrustStoreError{InnerError: err, Msg: fmt.Sprintf("failed to access the trust store %q of type %q", namedStore, storeType)}
94 }
95
96 var certificates []*x509.Certificate
97 for _, file := range files {
98 certFileName := file.Name()
99 joinedPath := filepath.Join(path, certFileName)
100 if file.IsDir() || file.Type()&fs.ModeSymlink != 0 {
101 return nil, CertificateError{Msg: fmt.Sprintf("trusted certificate %s in trust store %s of type %s is not a regular file (directories or symlinks are not supported)", certFileName, namedStore, storeType)}
102 }
103 certs, err := corex509.ReadCertificateFile(joinedPath)
104 if err != nil {
105 return nil, CertificateError{InnerError: err, Msg: fmt.Sprintf("failed to read the trusted certificate %s in trust store %s of type %s", certFileName, namedStore, storeType)}
106 }
107 if err := ValidateCertificates(certs); err != nil {
108 return nil, CertificateError{InnerError: err, Msg: fmt.Sprintf("failed to validate the trusted certificate %s in trust store %s of type %s", certFileName, namedStore, storeType)}
109 }
110 // we require TSA certificates in trust store to be root CA certificates
111 if storeType == TypeTSA {
112 for _, cert := range certs {
113 if err := isRootCACertificate(cert); err != nil {
114 return nil, CertificateError{InnerError: err, Msg: fmt.Sprintf("trusted certificate %s in trust store %s of type %s is invalid: %v", certFileName, namedStore, storeType, err.Error())}
115 }
116 }
117 }
118 certificates = append(certificates, certs...)
119 }
120 if len(certificates) < 1 {
121 return nil, CertificateError{InnerError: fs.ErrNotExist, Msg: fmt.Sprintf("no x509 certificates were found in trust store %q of type %q", namedStore, storeType)}
122 }
123 return certificates, nil
124 }
125
126 // ValidateCertificates ensures certificates from trust store are
127 // CA certificates or self-signed.
128 func ValidateCertificates(certs []*x509.Certificate) error {
129 if len(certs) < 1 {
130 return errors.New("input certs cannot be empty")
131 }
132 for _, cert := range certs {
133 if !cert.IsCA {
134 if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
135 return fmt.Errorf(
136 "certificate with subject %q is not a CA certificate or self-signed signing certificate",
137 cert.Subject,
138 )
139 }
140 }
141 }
142 return nil
143 }
144
145 // isValidStoreType checks if storeType is supported
146 func isValidStoreType(storeType Type) bool {
147 return slices.Contains(Types, storeType)
148 }
149
150 // isRootCACertificate returns nil if cert is a root CA certificate
151 func isRootCACertificate(cert *x509.Certificate) error {
152 if err := cert.CheckSignatureFrom(cert); err != nil {
153 return fmt.Errorf("certificate with subject %q is not a root CA certificate: %w", cert.Subject, err)
154 }
155 if !bytes.Equal(cert.RawSubject, cert.RawIssuer) {
156 return fmt.Errorf("certificate with subject %q is not a root CA certificate: issuer (%s) and subject (%s) are not the same", cert.Subject, cert.Issuer, cert.Subject)
157 }
158 return nil
159 }