root.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.rootX509Crt
  24. }
  25. // GetFederation returns all the root certificates in the federation.
  26. func (a *Authority) GetFederation(peer *x509.Certificate) (federation []*x509.Certificate, err error) {
  27. // Check step provisioner extensions
  28. if err := a.authorizeRenewal(peer); err != nil {
  29. return nil, err
  30. }
  31. a.certificates.Range(func(k, v interface{}) bool {
  32. crt, ok := v.(*x509.Certificate)
  33. if !ok {
  34. federation = nil
  35. err = &apiError{errors.Errorf("stored value is not a *x509.Certificate"),
  36. http.StatusInternalServerError, context{}}
  37. return false
  38. }
  39. federation = append(federation, crt)
  40. return true
  41. })
  42. return
  43. }