root.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.InternalServer("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(_, v interface{}) bool {
  41. crt, ok := v.(*x509.Certificate)
  42. if !ok {
  43. federation = nil
  44. err = errs.InternalServer("stored value is not a *x509.Certificate")
  45. return false
  46. }
  47. federation = append(federation, crt)
  48. return true
  49. })
  50. return
  51. }
  52. // GetIntermediateCertificate return the intermediate certificate that issues
  53. // the leaf certificates in the CA.
  54. //
  55. // This method can return nil if the CA is configured with a Certificate
  56. // Authority Service (CAS) that does not implement the
  57. // CertificateAuthorityGetter interface.
  58. func (a *Authority) GetIntermediateCertificate() *x509.Certificate {
  59. if len(a.intermediateX509Certs) > 0 {
  60. return a.intermediateX509Certs[0]
  61. }
  62. return nil
  63. }
  64. // GetIntermediateCertificates returns a list of all intermediate certificates
  65. // configured. The first certificate in the list will be the issuer certificate.
  66. //
  67. // This method can return an empty list or nil if the CA is configured with a
  68. // Certificate Authority Service (CAS) that does not implement the
  69. // CertificateAuthorityGetter interface.
  70. func (a *Authority) GetIntermediateCertificates() []*x509.Certificate {
  71. return a.intermediateX509Certs
  72. }