Skip to content

Commit

Permalink
Compare two certificates chains where one is expired
Browse files Browse the repository at this point in the history
Signed-off-by: Takuya Kosugiyama <[email protected]>
  • Loading branch information
itkq committed Jun 10, 2020
1 parent 2059d86 commit d45159b
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions prober/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,14 @@ func TestTCPConnectionWithTLSAndVerifiedCertificateChain(t *testing.T) {
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// From here prepare two certificate chains where one is expired

rootPrivatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(fmt.Sprintf("Error creating rsa key: %s", err))
}

// Prepare certificates to simulate a situation where
// the root certificate in the chain sent by the server
// expired but a verified certificate chain can be found
// because a new one has installed on the local machine.

rootCertExpiry := time.Now().AddDate(0, 0, 1)
rootCertExpiry := time.Now().AddDate(0, 0, 2)
rootCertTmpl := generateCertificateTemplate(rootCertExpiry, false)
rootCertTmpl.IsCA = true
_, rootCertPem := generateSelfSignedCertificateWithPrivateKey(rootCertTmpl, rootPrivatekey)
Expand All @@ -236,19 +233,16 @@ func TestTCPConnectionWithTLSAndVerifiedCertificateChain(t *testing.T) {
expiredRootCertTmpl.IsCA = true
expiredRootCert, expiredRootCertPem := generateSelfSignedCertificateWithPrivateKey(expiredRootCertTmpl, rootPrivatekey)

intermediateCertTmpl := generateCertificateTemplate(rootCertExpiry, false)
intermediateCertTmpl.IsCA = true
intermediateCert, intermediateCertPem, intermediateKey := generateSignedCertificate(intermediateCertTmpl, expiredRootCert, rootPrivatekey)

leafCertImpl := generateCertificateTemplate(rootCertExpiry, false)
_, leafCertPem, leafKey := generateSignedCertificate(leafCertImpl, intermediateCert, intermediateKey)
serverCertExpiry := time.Now().AddDate(0, 0, 1)
serverCertTmpl := generateCertificateTemplate(serverCertExpiry, false)
_, serverCertPem, serverKey := generateSignedCertificate(serverCertTmpl, expiredRootCert, rootPrivatekey)

// CAFile must be passed via filesystem, use a tempfile.
tmpCaFile, err := ioutil.TempFile("", "cafile.pem")
if err != nil {
t.Fatalf(fmt.Sprintf("Error creating CA tempfile: %s", err))
}
if _, err := tmpCaFile.Write(rootCertPem); err != nil {
if _, err := tmpCaFile.Write(bytes.Join([][]byte{rootCertPem, expiredRootCertPem}, []byte("\n"))); err != nil {
t.Fatalf(fmt.Sprintf("Error writing CA tempfile: %s", err))
}
if err := tmpCaFile.Close(); err != nil {
Expand All @@ -266,14 +260,9 @@ func TestTCPConnectionWithTLSAndVerifiedCertificateChain(t *testing.T) {
}
defer conn.Close()

expiredRootKeyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rootPrivatekey)})
intermediateKeyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(intermediateKey)})
leafKeyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(leafKey)})

certPem := bytes.Join([][]byte{leafCertPem, intermediateCertPem, expiredRootCertPem}, []byte("\n"))
keyPem := bytes.Join([][]byte{leafKeyPem, intermediateKeyPem, expiredRootKeyPem}, []byte("\n"))
serverKeyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(serverKey)})

keypair, err := tls.X509KeyPair(certPem, keyPem)
keypair, err := tls.X509KeyPair(serverCertPem, serverKeyPem)
if err != nil {
panic(fmt.Sprintf("Failed to decode TLS testing keypair: %s\n", err))
}
Expand Down Expand Up @@ -326,8 +315,8 @@ func TestTCPConnectionWithTLSAndVerifiedCertificateChain(t *testing.T) {

// Check values
expectedResults := map[string]float64{
"probe_ssl_earliest_cert_expiry": float64(oldRootCertExpiry.Unix()),
"probe_ssl_last_chain_expiry_timestamp_seconds": float64(rootCertExpiry.Unix()),
"probe_ssl_earliest_cert_expiry": float64(serverCertExpiry.Unix()),
"probe_ssl_last_chain_expiry_timestamp_seconds": float64(serverCertExpiry.Unix()),
"probe_tls_version_info": 1,
}
checkRegistryResults(expectedResults, mfs, t)
Expand Down

0 comments on commit d45159b

Please sign in to comment.