open-vault/helper/certutil/certutil_test.go
Jeff Mitchell 390f769d1a Add unit tests for certutil, and fix a whitespace stripping issue.
Commit contents (C)2015 Akamai Technologies, Inc. <opensource@akamai.com>
2015-06-19 16:06:56 -04:00

345 lines
12 KiB
Go

package certutil
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/fatih/structs"
"github.com/hashicorp/vault/api"
)
// Tests converting back and forth between a CertBundle and a ParsedCertBundle.
//
// Also tests the GetSubjKeyID, GetOctalFormatted, and
// ParsedCertBundle.getSigner functions.
func TestCertBundleConversion(t *testing.T) {
cbuts := []*CertBundle{
refreshRSACertBundle(),
refreshECCertBundle(),
}
for _, cbut := range cbuts {
pcbut, err := cbut.ToParsedCertBundle()
if err != nil {
t.Fatalf("Error converting to parsed cert bundle: %s", err)
}
err = compareCertBundleToParsedCertBundle(cbut, pcbut)
if err != nil {
t.Fatalf(err.Error())
}
}
}
func TestTLSConfig(t *testing.T) {
cbut := refreshRSACertBundle()
pcbut, err := cbut.ToParsedCertBundle()
if err != nil {
t.Fatalf("Error getting parsed cert bundle: %s", err)
}
usages := []TLSUsage{
TLSUnknown,
TLSClient,
TLSServer,
TLSClient | TLSServer,
}
for _, usage := range usages {
tlsConfig, err := pcbut.GetTLSConfig(usage)
if err != nil {
t.Fatalf("Error getting tls config: %s", err)
}
if tlsConfig == nil {
t.Fatalf("Got nil tls.Config")
}
if len(tlsConfig.Certificates) != 1 {
t.Fatalf("Unexpected length in config.Certificates")
}
// Length should be 2, since we passed in a CA
if len(tlsConfig.Certificates[0].Certificate) != 2 {
t.Fatalf("Did not find both certificates in config.Certificates.Certificate")
}
if tlsConfig.Certificates[0].Leaf != pcbut.Certificate {
t.Fatalf("Leaf certificate does not match parsed bundle's certificate")
}
if tlsConfig.Certificates[0].PrivateKey != pcbut.PrivateKey {
t.Fatalf("Config's private key does not match parsed bundle's private key")
}
switch usage {
case TLSServer | TLSClient:
if len(tlsConfig.ClientCAs.Subjects()) != 1 || bytes.Compare(tlsConfig.ClientCAs.Subjects()[0], pcbut.IssuingCA.RawSubject) != 0 {
t.Fatalf("CA certificate not in client cert pool as expected")
}
if len(tlsConfig.RootCAs.Subjects()) != 1 || bytes.Compare(tlsConfig.RootCAs.Subjects()[0], pcbut.IssuingCA.RawSubject) != 0 {
t.Fatalf("CA certificate not in root cert pool as expected")
}
case TLSServer:
if len(tlsConfig.ClientCAs.Subjects()) != 1 || bytes.Compare(tlsConfig.ClientCAs.Subjects()[0], pcbut.IssuingCA.RawSubject) != 0 {
t.Fatalf("CA certificate not in client cert pool as expected")
}
if tlsConfig.RootCAs != nil {
t.Fatalf("Found root pools in config object when not expected")
}
case TLSClient:
if len(tlsConfig.RootCAs.Subjects()) != 1 || bytes.Compare(tlsConfig.RootCAs.Subjects()[0], pcbut.IssuingCA.RawSubject) != 0 {
t.Fatalf("CA certificate not in root cert pool as expected")
}
if tlsConfig.ClientCAs != nil {
t.Fatalf("Found root pools in config object when not expected")
}
default:
if tlsConfig.RootCAs != nil || tlsConfig.ClientCAs != nil {
t.Fatalf("Found root pools in config object when not expected")
}
}
}
}
func TestCertBundleParsing(t *testing.T) {
jsonBundle := refreshRSACertBundle()
jsonString, err := json.Marshal(jsonBundle)
if err != nil {
t.Fatalf("Error marshaling testing certbundle to JSON: %s", err)
}
pcbut, err := ParsePKIJSON(jsonString)
if err != nil {
t.Fatalf("Error during JSON bundle handling: %s", err)
}
err = compareCertBundleToParsedCertBundle(jsonBundle, pcbut)
if err != nil {
t.Fatalf(err.Error())
}
secret := &api.Secret{
Data: structs.New(jsonBundle).Map(),
}
pcbut, err = ParsePKIMap(secret.Data)
if err != nil {
t.Fatalf("Error during JSON bundle handling: %s", err)
}
err = compareCertBundleToParsedCertBundle(jsonBundle, pcbut)
if err != nil {
t.Fatalf(err.Error())
}
pemBundle := strings.Join([]string{
jsonBundle.Certificate,
jsonBundle.IssuingCA,
jsonBundle.PrivateKey,
}, "\n")
pcbut, err = ParsePEMBundle(pemBundle)
if err != nil {
t.Fatalf("Error during JSON bundle handling: %s", err)
}
err = compareCertBundleToParsedCertBundle(jsonBundle, pcbut)
if err != nil {
t.Fatalf(err.Error())
}
}
func compareCertBundleToParsedCertBundle(cbut *CertBundle, pcbut *ParsedCertBundle) error {
if cbut == nil {
return fmt.Errorf("Got nil bundle")
}
if pcbut == nil {
return fmt.Errorf("Got nil parsed bundle")
}
switch {
case pcbut.Certificate == nil:
return fmt.Errorf("Parsed bundle has nil certificate")
case pcbut.PrivateKey == nil:
return fmt.Errorf("Parsed bundle has nil private key")
case pcbut.IssuingCA == nil:
return fmt.Errorf("Parsed bundle has nil issuing CA")
}
switch cbut.PrivateKey {
case privRSAKeyPem:
if pcbut.PrivateKeyType != RSAPrivateKey {
return fmt.Errorf("Parsed bundle has wrong private key type")
}
case privECKeyPem:
if pcbut.PrivateKeyType != ECPrivateKey {
return fmt.Errorf("Parsed bundle has wrong private key type")
}
default:
return fmt.Errorf("Parsed bundle has unknown private key type")
}
subjKeyID, err := GetSubjKeyID(pcbut.PrivateKey)
if err != nil {
return fmt.Errorf("Error when getting subject key id: %s", err)
}
if bytes.Compare(subjKeyID, pcbut.Certificate.SubjectKeyId) != 0 {
return fmt.Errorf("Parsed bundle private key does not match subject key id")
}
cb, err := pcbut.ToCertBundle()
if err != nil {
return fmt.Errorf("Thrown error during parsed bundle conversion: %s\n\nInput was: %#v", err, *pcbut)
}
switch {
case len(cb.Certificate) == 0:
return fmt.Errorf("Bundle has nil certificate")
case len(cb.PrivateKey) == 0:
return fmt.Errorf("Bundle has nil private key")
case len(cb.IssuingCA) == 0:
return fmt.Errorf("Bundle has nil issuing CA")
}
switch cb.PrivateKeyType {
case "rsa":
if pcbut.PrivateKeyType != RSAPrivateKey {
return fmt.Errorf("Bundle has wrong private key type")
}
if cb.PrivateKey != privRSAKeyPem {
return fmt.Errorf("Bundle private key does not match")
}
case "ec":
if pcbut.PrivateKeyType != ECPrivateKey {
return fmt.Errorf("Bundle has wrong private key type")
}
if cb.PrivateKey != privECKeyPem {
return fmt.Errorf("Bundle private key does not match")
}
default:
return fmt.Errorf("Bundle has unknown private key type")
}
if cb.SerialNumber != GetOctalFormatted(pcbut.Certificate.SerialNumber.Bytes(), ":") {
return fmt.Errorf("Bundle serial number does not match")
}
return nil
}
func refreshRSACertBundle() *CertBundle {
return &CertBundle{
Certificate: certRSAPem,
PrivateKey: privRSAKeyPem,
IssuingCA: issuingCaPem,
}
}
func refreshECCertBundle() *CertBundle {
return &CertBundle{
Certificate: certECPem,
PrivateKey: privECKeyPem,
IssuingCA: issuingCaPem,
}
}
const (
privRSAKeyPem = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAt3ZJUaztCRiVg87P0y8T7QMNFQi61BCSIKepxXXWc7zi5JJS
MfQAstXJEqBYiShsSpYm6soiT6hX074t7wQAHGS3+u7qNogWpmTAUTUnNIM+QCxH
2Nc/kzYxaWajupVzgGvLeiqU3d4tIUk/ZkftvWJryr2hZc8zEN3C4pGS/2F+RQ+z
Ov+BpAI1BdbQGhF7m92vn6KS/iWsqmwHG9oChgvWeBHjWUI8qGauBc+it4S5RxfN
8JJIBXUIZtbaqFZzgjv8kUDyqoQGvkY/4Ce1K0bFJsM7wmMPv+5QscIBF4KWgN0k
TSMPPXfnn/QIAfhaYQkT9MjwGr6+B3SODNGCTQIDAQABAoIBAHtSwhprGbNhmS/P
F5ioLsbFpEedZKkksnXM/qxDd/K45/Qp/6KgmM+eMdmZe6pHR/QjVunBEqtlSBSH
5KykjcaIVbwSWdJqTH9xfm2YQ1BjYLcWjP1QQ+YbKb/mRO0phUiwLUlj0koKDWAw
srN4anFB9Z+FNTcQvwz5ZQWUQbH0neQtWO1nDvLsScgu1kchoEzJEJaFOQ1+HfGe
WxD766fZyqZQi5+cLrhOqHOGSlO+IFVe0hguiEHFr9LEPTXXkZtOR4wTf7j1Us8s
1KQ/jv01sx9S7HEbZJurzIjS23OywEUdJd1EsIE2lJV2QUwSiAsPYZOSQZlgOGzP
VRKVkGkCg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