tls_keystore.go 876 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package dsig
  2. import (
  3. "crypto/rsa"
  4. "crypto/tls"
  5. "fmt"
  6. )
  7. //Well-known errors
  8. var (
  9. ErrNonRSAKey = fmt.Errorf("Private key was not RSA")
  10. ErrMissingCertificates = fmt.Errorf("No public certificates provided")
  11. )
  12. //TLSCertKeyStore wraps the stdlib tls.Certificate to return its contained key
  13. //and certs.
  14. type TLSCertKeyStore tls.Certificate
  15. //GetKeyPair implements X509KeyStore using the underlying tls.Certificate
  16. func (d TLSCertKeyStore) GetKeyPair() (*rsa.PrivateKey, []byte, error) {
  17. pk, ok := d.PrivateKey.(*rsa.PrivateKey)
  18. if !ok {
  19. return nil, nil, ErrNonRSAKey
  20. }
  21. if len(d.Certificate) < 1 {
  22. return nil, nil, ErrMissingCertificates
  23. }
  24. crt := d.Certificate[0]
  25. return pk, crt, nil
  26. }
  27. //GetChain impliments X509ChainStore using the underlying tls.Certificate
  28. func (d TLSCertKeyStore) GetChain() ([][]byte, error) {
  29. return d.Certificate, nil
  30. }