root.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "github.com/smallstep/certificates/errs"
  5. )
  6. // Root returns the certificate corresponding to the given SHA sum argument.
  7. func (a *Authority) Root(sum string) (*x509.Certificate, error) {
  8. val, ok := a.certificates.Load(sum)
  9. if !ok {
  10. return nil, errs.NotFound("certificate with fingerprint %s was not found", sum)
  11. }
  12. crt, ok := val.(*x509.Certificate)
  13. if !ok {
  14. return nil, errs.Internal("stored value is not a *x509.Certificate")
  15. }
  16. return crt, nil
  17. }
  18. // GetRootCertificate returns the server root certificate.
  19. func (a *Authority) GetRootCertificate() *x509.Certificate {
  20. return a.rootX509Certs[0]
  21. }
  22. // GetRootCertificates returns the server root certificates.
  23. //
  24. // In the Authority interface we also have a similar method, GetRoots, at the
  25. // moment the functionality of these two methods are almost identical, but this
  26. // method is intended to be used internally by CA HTTP server to load the roots
  27. // that will be set in the tls.Config while GetRoots will be used by the
  28. // Authority interface and might have extra checks in the future.
  29. func (a *Authority) GetRootCertificates() []*x509.Certificate {
  30. return a.rootX509Certs
  31. }
  32. // GetRoots returns all the root certificates for this CA.
  33. // This method implements the Authority interface.
  34. func (a *Authority) GetRoots() ([]*x509.Certificate, error) {
  35. return a.rootX509Certs, nil
  36. }
  37. // GetFederation returns all the root certificates in the federation.
  38. // This method implements the Authority interface.
  39. func (a *Authority) GetFederation() (federation []*x509.Certificate, err error) {
  40. a.certificates.Range(func(k, v interface{}) bool {
  41. crt, ok := v.(*x509.Certificate)
  42. if !ok {
  43. federation = nil
  44. err = errs.Internal("stored value is not a *x509.Certificate")
  45. return false
  46. }
  47. federation = append(federation, crt)
  48. return true
  49. })
  50. return
  51. }