|
| 1 | +/* |
| 2 | +© Copyright IBM Corporation 2025 |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metricstest |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/rand" |
| 21 | + "crypto/rsa" |
| 22 | + "crypto/x509" |
| 23 | + "crypto/x509/pkix" |
| 24 | + "encoding/pem" |
| 25 | + "fmt" |
| 26 | + "math/big" |
| 27 | + mathrand "math/rand" |
| 28 | + "os" |
| 29 | + "path" |
| 30 | + "time" |
| 31 | +) |
| 32 | + |
| 33 | +func GenerateTestKeys(numServerKeys int, serverCommonNames ...string) (*x509.Certificate, []*x509.Certificate, []*rsa.PrivateKey, error) { |
| 34 | + caKey, caCert, err := makeTestCA() |
| 35 | + if err != nil { |
| 36 | + return nil, nil, nil, err |
| 37 | + } |
| 38 | + |
| 39 | + srvCerts := make([]*x509.Certificate, 0, numServerKeys) |
| 40 | + srvKeys := make([]*rsa.PrivateKey, 0, numServerKeys) |
| 41 | + |
| 42 | + if len(serverCommonNames) == 0 { |
| 43 | + serverCommonNames = []string{fmt.Sprintf("test-cert-%d", numServerKeys)} |
| 44 | + } |
| 45 | + |
| 46 | + for i := 0; i < numServerKeys; i++ { |
| 47 | + srvKey, srvCert, err := makeTestCert(caCert, caKey, serverCommonNames...) |
| 48 | + if err != nil { |
| 49 | + return nil, nil, nil, err |
| 50 | + } |
| 51 | + srvCerts = append(srvCerts, srvCert) |
| 52 | + srvKeys = append(srvKeys, srvKey) |
| 53 | + } |
| 54 | + |
| 55 | + return caCert, srvCerts, srvKeys, nil |
| 56 | +} |
| 57 | + |
| 58 | +func MakeCACertPool(caCert *x509.Certificate) *x509.CertPool { |
| 59 | + caPool := x509.NewCertPool() |
| 60 | + caPool.AppendCertsFromPEM(pem.EncodeToMemory(&pem.Block{ |
| 61 | + Type: "CERTIFICATE", |
| 62 | + Bytes: caCert.Raw, |
| 63 | + })) |
| 64 | + return caPool |
| 65 | +} |
| 66 | + |
| 67 | +func WriteCertsToDir(caCert, srvCert *x509.Certificate, srvKey *rsa.PrivateKey, certDir string, combineCert bool) error { |
| 68 | + var caCertPEM, srvCertPEM, srvKeyPEM []byte |
| 69 | + caCertFile := path.Join(certDir, "ca.crt") |
| 70 | + srvCertFile := path.Join(certDir, "tls.crt") |
| 71 | + srvKeyFile := path.Join(certDir, "tls.key") |
| 72 | + |
| 73 | + if caCert != nil { |
| 74 | + caCertPEM = pem.EncodeToMemory(&pem.Block{ |
| 75 | + Type: "CERTIFICATE", |
| 76 | + Bytes: caCert.Raw, |
| 77 | + }) |
| 78 | + } else { |
| 79 | + _ = os.Remove(caCertFile) |
| 80 | + } |
| 81 | + |
| 82 | + if srvCert != nil { |
| 83 | + srvCertPEM = pem.EncodeToMemory(&pem.Block{ |
| 84 | + Type: "CERTIFICATE", |
| 85 | + Bytes: srvCert.Raw, |
| 86 | + }) |
| 87 | + } else { |
| 88 | + _ = os.Remove(srvCertFile) |
| 89 | + } |
| 90 | + if srvKey != nil { |
| 91 | + srvKeyPEM = pem.EncodeToMemory(&pem.Block{ |
| 92 | + Type: "RSA PRIVATE KEY", |
| 93 | + Bytes: x509.MarshalPKCS1PrivateKey(srvKey), |
| 94 | + }) |
| 95 | + } else { |
| 96 | + _ = os.Remove(srvKeyFile) |
| 97 | + } |
| 98 | + |
| 99 | + if combineCert { |
| 100 | + _ = os.Remove(caCertFile) |
| 101 | + combined := make([]byte, 0, len(caCertPEM)+len(srvCertPEM)+1) |
| 102 | + combined = append(combined, srvCertPEM...) |
| 103 | + if len(combined) > 0 && combined[len(combined)-1] != '\n' { |
| 104 | + combined = append(combined, '\n') |
| 105 | + } |
| 106 | + combined = append(combined, caCertPEM...) |
| 107 | + err := os.WriteFile(srvCertFile, combined, 0644) // #nosec G306 -- Test-only Certificate needs to be readable by multiple users |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + } else { |
| 112 | + err := os.WriteFile(caCertFile, caCertPEM, 0644) // #nosec G306 -- Test-only Certificate needs to be readable by multiple users |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + err = os.WriteFile(srvCertFile, srvCertPEM, 0644) // #nosec G306 -- Test-only Certificate needs to be readable by multiple users |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } |
| 121 | + err := os.WriteFile(srvKeyFile, srvKeyPEM, 0644) // #nosec G306 -- Test-only Certificate needs to be readable by multiple users |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func makeTestCA() (*rsa.PrivateKey, *x509.Certificate, error) { |
| 129 | + ca := &x509.Certificate{ |
| 130 | + // #nosec G404 -- Non crypto rand acceptable for serial number |
| 131 | + SerialNumber: big.NewInt(mathrand.Int63()), |
| 132 | + Subject: pkix.Name{ |
| 133 | + Organization: []string{"IBM"}, |
| 134 | + StreetAddress: []string{"1 New Orchard Road"}, |
| 135 | + Locality: []string{"Armonk"}, |
| 136 | + Province: []string{"New York"}, |
| 137 | + PostalCode: []string{"10504"}, |
| 138 | + Country: []string{"US"}, |
| 139 | + }, |
| 140 | + NotBefore: time.Now(), |
| 141 | + NotAfter: time.Now().AddDate(0, 0, 1), |
| 142 | + IsCA: true, |
| 143 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, |
| 144 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 145 | + BasicConstraintsValid: true, |
| 146 | + } |
| 147 | + caPrivKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 148 | + if err != nil { |
| 149 | + return nil, nil, err |
| 150 | + } |
| 151 | + ca.PublicKey = &caPrivKey.PublicKey |
| 152 | + caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey) |
| 153 | + if err != nil { |
| 154 | + return nil, nil, err |
| 155 | + } |
| 156 | + ca.Raw = caBytes |
| 157 | + return caPrivKey, ca, nil |
| 158 | +} |
| 159 | + |
| 160 | +func makeTestCert(caCert *x509.Certificate, caKey *rsa.PrivateKey, serverCommonNames ...string) (*rsa.PrivateKey, *x509.Certificate, error) { |
| 161 | + cert := &x509.Certificate{ |
| 162 | + // #nosec G404 -- Noncrypto rand acceptable for serial number |
| 163 | + SerialNumber: big.NewInt(mathrand.Int63()), |
| 164 | + Subject: pkix.Name{ |
| 165 | + Organization: []string{"IBM"}, |
| 166 | + StreetAddress: []string{"1 New Orchard Road"}, |
| 167 | + Locality: []string{"Armonk"}, |
| 168 | + Province: []string{"New York"}, |
| 169 | + PostalCode: []string{"10504"}, |
| 170 | + Country: []string{"US"}, |
| 171 | + CommonName: serverCommonNames[0], |
| 172 | + }, |
| 173 | + DNSNames: serverCommonNames, |
| 174 | + NotBefore: time.Now(), |
| 175 | + NotAfter: time.Now().AddDate(0, 0, 1), |
| 176 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, |
| 177 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 178 | + } |
| 179 | + certPrivKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 180 | + if err != nil { |
| 181 | + return nil, nil, err |
| 182 | + } |
| 183 | + cert.PublicKey = &certPrivKey.PublicKey |
| 184 | + certBytes, err := x509.CreateCertificate(rand.Reader, cert, caCert, &certPrivKey.PublicKey, caKey) |
| 185 | + if err != nil { |
| 186 | + return nil, nil, err |
| 187 | + } |
| 188 | + cert.Raw = certBytes |
| 189 | + return certPrivKey, cert, nil |
| 190 | +} |
0 commit comments