root.go 2.0 KB

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