authority.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/hex"
  8. "log"
  9. "net/http"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/pkg/errors"
  14. "github.com/smallstep/certificates/authority/admin"
  15. adminDBNosql "github.com/smallstep/certificates/authority/admin/db/nosql"
  16. "github.com/smallstep/certificates/authority/administrator"
  17. "github.com/smallstep/certificates/authority/config"
  18. "github.com/smallstep/certificates/authority/provisioner"
  19. "github.com/smallstep/certificates/cas"
  20. casapi "github.com/smallstep/certificates/cas/apiv1"
  21. "github.com/smallstep/certificates/db"
  22. "github.com/smallstep/certificates/kms"
  23. kmsapi "github.com/smallstep/certificates/kms/apiv1"
  24. "github.com/smallstep/certificates/kms/sshagentkms"
  25. "github.com/smallstep/certificates/scep"
  26. "github.com/smallstep/certificates/templates"
  27. "github.com/smallstep/nosql"
  28. "go.step.sm/crypto/pemutil"
  29. "go.step.sm/linkedca"
  30. "golang.org/x/crypto/ssh"
  31. )
  32. // Authority implements the Certificate Authority internal interface.
  33. type Authority struct {
  34. config *config.Config
  35. keyManager kms.KeyManager
  36. provisioners *provisioner.Collection
  37. admins *administrator.Collection
  38. db db.AuthDB
  39. adminDB admin.DB
  40. templates *templates.Templates
  41. linkedCAToken string
  42. // X509 CA
  43. password []byte
  44. issuerPassword []byte
  45. x509CAService cas.CertificateAuthorityService
  46. rootX509Certs []*x509.Certificate
  47. rootX509CertPool *x509.CertPool
  48. federatedX509Certs []*x509.Certificate
  49. certificates *sync.Map
  50. x509Enforcers []provisioner.CertificateEnforcer
  51. // SCEP CA
  52. scepService *scep.Service
  53. // SSH CA
  54. sshHostPassword []byte
  55. sshUserPassword []byte
  56. sshCAUserCertSignKey ssh.Signer
  57. sshCAHostCertSignKey ssh.Signer
  58. sshCAUserCerts []ssh.PublicKey
  59. sshCAHostCerts []ssh.PublicKey
  60. sshCAUserFederatedCerts []ssh.PublicKey
  61. sshCAHostFederatedCerts []ssh.PublicKey
  62. // Do not re-initialize
  63. initOnce bool
  64. startTime time.Time
  65. // Custom functions
  66. sshBastionFunc func(ctx context.Context, user, hostname string) (*config.Bastion, error)
  67. sshCheckHostFunc func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)
  68. sshGetHostsFunc func(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)
  69. getIdentityFunc provisioner.GetIdentityFunc
  70. authorizeRenewFunc provisioner.AuthorizeRenewFunc
  71. authorizeSSHRenewFunc provisioner.AuthorizeSSHRenewFunc
  72. adminMutex sync.RWMutex
  73. }
  74. type Info struct {
  75. StartTime time.Time
  76. RootX509Certs []*x509.Certificate
  77. SSHCAUserPublicKey []byte
  78. SSHCAHostPublicKey []byte
  79. DNSNames []string
  80. }
  81. // New creates and initiates a new Authority type.
  82. func New(cfg *config.Config, opts ...Option) (*Authority, error) {
  83. err := cfg.Validate()
  84. if err != nil {
  85. return nil, err
  86. }
  87. var a = &Authority{
  88. config: cfg,
  89. certificates: new(sync.Map),
  90. }
  91. // Apply options.
  92. for _, fn := range opts {
  93. if err := fn(a); err != nil {
  94. return nil, err
  95. }
  96. }
  97. // Initialize authority from options or configuration.
  98. if err := a.init(); err != nil {
  99. return nil, err
  100. }
  101. return a, nil
  102. }
  103. // NewEmbedded initializes an authority that can be embedded in a different
  104. // project without the limitations of the config.
  105. func NewEmbedded(opts ...Option) (*Authority, error) {
  106. a := &Authority{
  107. config: &config.Config{},
  108. certificates: new(sync.Map),
  109. }
  110. // Apply options.
  111. for _, fn := range opts {
  112. if err := fn(a); err != nil {
  113. return nil, err
  114. }
  115. }
  116. // Validate required options
  117. switch {
  118. case a.config == nil:
  119. return nil, errors.New("cannot create an authority without a configuration")
  120. case len(a.rootX509Certs) == 0 && a.config.Root.HasEmpties():
  121. return nil, errors.New("cannot create an authority without a root certificate")
  122. case a.x509CAService == nil && a.config.IntermediateCert == "":
  123. return nil, errors.New("cannot create an authority without an issuer certificate")
  124. case a.x509CAService == nil && a.config.IntermediateKey == "":
  125. return nil, errors.New("cannot create an authority without an issuer signer")
  126. }
  127. // Initialize config required fields.
  128. a.config.Init()
  129. // Initialize authority from options or configuration.
  130. if err := a.init(); err != nil {
  131. return nil, err
  132. }
  133. return a, nil
  134. }
  135. type authorityKey struct{}
  136. // NewContext adds the given authority to the context.
  137. func NewContext(ctx context.Context, a *Authority) context.Context {
  138. return context.WithValue(ctx, authorityKey{}, a)
  139. }
  140. // FromContext returns the current authority from the given context.
  141. func FromContext(ctx context.Context) (a *Authority, ok bool) {
  142. a, ok = ctx.Value(authorityKey{}).(*Authority)
  143. return
  144. }
  145. // Middleware adds the current authority to the request context.
  146. func (a *Authority) Middleware(next http.Handler) http.Handler {
  147. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  148. ctx := NewContext(r.Context(), a)
  149. next.ServeHTTP(w, r.WithContext(ctx))
  150. })
  151. }
  152. // reloadAdminResources reloads admins and provisioners from the DB.
  153. func (a *Authority) reloadAdminResources(ctx context.Context) error {
  154. var (
  155. provList provisioner.List
  156. adminList []*linkedca.Admin
  157. )
  158. if a.config.AuthorityConfig.EnableAdmin {
  159. provs, err := a.adminDB.GetProvisioners(ctx)
  160. if err != nil {
  161. return admin.WrapErrorISE(err, "error getting provisioners to initialize authority")
  162. }
  163. provList, err = provisionerListToCertificates(provs)
  164. if err != nil {
  165. return admin.WrapErrorISE(err, "error converting provisioner list to certificates")
  166. }
  167. adminList, err = a.adminDB.GetAdmins(ctx)
  168. if err != nil {
  169. return admin.WrapErrorISE(err, "error getting admins to initialize authority")
  170. }
  171. } else {
  172. provList = a.config.AuthorityConfig.Provisioners
  173. adminList = a.config.AuthorityConfig.Admins
  174. }
  175. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  176. if err != nil {
  177. return admin.WrapErrorISE(err, "error generating provisioner config")
  178. }
  179. // Create provisioner collection.
  180. provClxn := provisioner.NewCollection(provisionerConfig.Audiences)
  181. for _, p := range provList {
  182. if err := p.Init(provisionerConfig); err != nil {
  183. return err
  184. }
  185. if err := provClxn.Store(p); err != nil {
  186. return err
  187. }
  188. }
  189. // Create admin collection.
  190. adminClxn := administrator.NewCollection(provClxn)
  191. for _, adm := range adminList {
  192. p, ok := provClxn.Load(adm.ProvisionerId)
  193. if !ok {
  194. return admin.NewErrorISE("provisioner %s not found when loading admin %s",
  195. adm.ProvisionerId, adm.Id)
  196. }
  197. if err := adminClxn.Store(adm, p); err != nil {
  198. return err
  199. }
  200. }
  201. a.config.AuthorityConfig.Provisioners = provList
  202. a.provisioners = provClxn
  203. a.config.AuthorityConfig.Admins = adminList
  204. a.admins = adminClxn
  205. return nil
  206. }
  207. // init performs validation and initializes the fields of an Authority struct.
  208. func (a *Authority) init() error {
  209. // Check if handler has already been validated/initialized.
  210. if a.initOnce {
  211. return nil
  212. }
  213. var err error
  214. // Set password if they are not set.
  215. var configPassword []byte
  216. if a.config.Password != "" {
  217. configPassword = []byte(a.config.Password)
  218. }
  219. if configPassword != nil && a.password == nil {
  220. a.password = configPassword
  221. }
  222. if a.sshHostPassword == nil {
  223. a.sshHostPassword = a.password
  224. }
  225. if a.sshUserPassword == nil {
  226. a.sshUserPassword = a.password
  227. }
  228. // Automatically enable admin for all linked cas.
  229. if a.linkedCAToken != "" {
  230. a.config.AuthorityConfig.EnableAdmin = true
  231. }
  232. // Initialize step-ca Database if it's not already initialized with WithDB.
  233. // If a.config.DB is nil then a simple, barebones in memory DB will be used.
  234. if a.db == nil {
  235. if a.db, err = db.New(a.config.DB); err != nil {
  236. return err
  237. }
  238. }
  239. // Initialize key manager if it has not been set in the options.
  240. if a.keyManager == nil {
  241. var options kmsapi.Options
  242. if a.config.KMS != nil {
  243. options = *a.config.KMS
  244. }
  245. a.keyManager, err = kms.New(context.Background(), options)
  246. if err != nil {
  247. return err
  248. }
  249. }
  250. // Initialize linkedca client if necessary. On a linked RA, the issuer
  251. // configuration might come from majordomo.
  252. var linkedcaClient *linkedCaClient
  253. if a.config.AuthorityConfig.EnableAdmin && a.linkedCAToken != "" && a.adminDB == nil {
  254. linkedcaClient, err = newLinkedCAClient(a.linkedCAToken)
  255. if err != nil {
  256. return err
  257. }
  258. // If authorityId is configured make sure it matches the one in the token
  259. if id := a.config.AuthorityConfig.AuthorityID; id != "" && !strings.EqualFold(id, linkedcaClient.authorityID) {
  260. return errors.New("error initializing linkedca: token authority and configured authority do not match")
  261. }
  262. linkedcaClient.Run()
  263. }
  264. // Initialize the X.509 CA Service if it has not been set in the options.
  265. if a.x509CAService == nil {
  266. var options casapi.Options
  267. if a.config.AuthorityConfig.Options != nil {
  268. options = *a.config.AuthorityConfig.Options
  269. }
  270. // Configure linked RA
  271. if linkedcaClient != nil && options.CertificateAuthority == "" {
  272. conf, err := linkedcaClient.GetConfiguration(context.Background())
  273. if err != nil {
  274. return err
  275. }
  276. if conf.RaConfig != nil {
  277. options.CertificateAuthority = conf.RaConfig.CaUrl
  278. options.CertificateAuthorityFingerprint = conf.RaConfig.Fingerprint
  279. options.CertificateIssuer = &casapi.CertificateIssuer{
  280. Type: conf.RaConfig.Provisioner.Type.String(),
  281. Provisioner: conf.RaConfig.Provisioner.Name,
  282. }
  283. }
  284. }
  285. // Set the issuer password if passed in the flags.
  286. if options.CertificateIssuer != nil && a.issuerPassword != nil {
  287. options.CertificateIssuer.Password = string(a.issuerPassword)
  288. }
  289. // Read intermediate and create X509 signer for default CAS.
  290. if options.Is(casapi.SoftCAS) {
  291. options.CertificateChain, err = pemutil.ReadCertificateBundle(a.config.IntermediateCert)
  292. if err != nil {
  293. return err
  294. }
  295. options.Signer, err = a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  296. SigningKey: a.config.IntermediateKey,
  297. Password: []byte(a.password),
  298. })
  299. if err != nil {
  300. return err
  301. }
  302. }
  303. a.x509CAService, err = cas.New(context.Background(), options)
  304. if err != nil {
  305. return err
  306. }
  307. // Get root certificate from CAS.
  308. if srv, ok := a.x509CAService.(casapi.CertificateAuthorityGetter); ok {
  309. resp, err := srv.GetCertificateAuthority(&casapi.GetCertificateAuthorityRequest{
  310. Name: options.CertificateAuthority,
  311. })
  312. if err != nil {
  313. return err
  314. }
  315. a.rootX509Certs = append(a.rootX509Certs, resp.RootCertificate)
  316. }
  317. }
  318. // Read root certificates and store them in the certificates map.
  319. if len(a.rootX509Certs) == 0 {
  320. a.rootX509Certs = make([]*x509.Certificate, len(a.config.Root))
  321. for i, path := range a.config.Root {
  322. crt, err := pemutil.ReadCertificate(path)
  323. if err != nil {
  324. return err
  325. }
  326. a.rootX509Certs[i] = crt
  327. }
  328. }
  329. for _, crt := range a.rootX509Certs {
  330. sum := sha256.Sum256(crt.Raw)
  331. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  332. }
  333. a.rootX509CertPool = x509.NewCertPool()
  334. for _, cert := range a.rootX509Certs {
  335. a.rootX509CertPool.AddCert(cert)
  336. }
  337. // Read federated certificates and store them in the certificates map.
  338. if len(a.federatedX509Certs) == 0 {
  339. a.federatedX509Certs = make([]*x509.Certificate, len(a.config.FederatedRoots))
  340. for i, path := range a.config.FederatedRoots {
  341. crt, err := pemutil.ReadCertificate(path)
  342. if err != nil {
  343. return err
  344. }
  345. a.federatedX509Certs[i] = crt
  346. }
  347. }
  348. for _, crt := range a.federatedX509Certs {
  349. sum := sha256.Sum256(crt.Raw)
  350. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  351. }
  352. // Decrypt and load SSH keys
  353. var tmplVars templates.Step
  354. if a.config.SSH != nil {
  355. if a.config.SSH.HostKey != "" {
  356. signer, err := a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  357. SigningKey: a.config.SSH.HostKey,
  358. Password: []byte(a.sshHostPassword),
  359. })
  360. if err != nil {
  361. return err
  362. }
  363. // If our signer is from sshagentkms, just unwrap it instead of
  364. // wrapping it in another layer, and this prevents crypto from
  365. // erroring out with: ssh: unsupported key type *agent.Key
  366. switch s := signer.(type) {
  367. case *sshagentkms.WrappedSSHSigner:
  368. a.sshCAHostCertSignKey = s.Sshsigner
  369. case crypto.Signer:
  370. a.sshCAHostCertSignKey, err = ssh.NewSignerFromSigner(s)
  371. default:
  372. return errors.Errorf("unsupported signer type %T", signer)
  373. }
  374. if err != nil {
  375. return errors.Wrap(err, "error creating ssh signer")
  376. }
  377. // Append public key to list of host certs
  378. a.sshCAHostCerts = append(a.sshCAHostCerts, a.sshCAHostCertSignKey.PublicKey())
  379. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, a.sshCAHostCertSignKey.PublicKey())
  380. }
  381. if a.config.SSH.UserKey != "" {
  382. signer, err := a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  383. SigningKey: a.config.SSH.UserKey,
  384. Password: []byte(a.sshUserPassword),
  385. })
  386. if err != nil {
  387. return err
  388. }
  389. // If our signer is from sshagentkms, just unwrap it instead of
  390. // wrapping it in another layer, and this prevents crypto from
  391. // erroring out with: ssh: unsupported key type *agent.Key
  392. switch s := signer.(type) {
  393. case *sshagentkms.WrappedSSHSigner:
  394. a.sshCAUserCertSignKey = s.Sshsigner
  395. case crypto.Signer:
  396. a.sshCAUserCertSignKey, err = ssh.NewSignerFromSigner(s)
  397. default:
  398. return errors.Errorf("unsupported signer type %T", signer)
  399. }
  400. if err != nil {
  401. return errors.Wrap(err, "error creating ssh signer")
  402. }
  403. // Append public key to list of user certs
  404. a.sshCAUserCerts = append(a.sshCAUserCerts, a.sshCAUserCertSignKey.PublicKey())
  405. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, a.sshCAUserCertSignKey.PublicKey())
  406. }
  407. // Append other public keys and add them to the template variables.
  408. for _, key := range a.config.SSH.Keys {
  409. publicKey := key.PublicKey()
  410. switch key.Type {
  411. case provisioner.SSHHostCert:
  412. if key.Federated {
  413. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, publicKey)
  414. } else {
  415. a.sshCAHostCerts = append(a.sshCAHostCerts, publicKey)
  416. }
  417. case provisioner.SSHUserCert:
  418. if key.Federated {
  419. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, publicKey)
  420. } else {
  421. a.sshCAUserCerts = append(a.sshCAUserCerts, publicKey)
  422. }
  423. default:
  424. return errors.Errorf("unsupported type %s", key.Type)
  425. }
  426. }
  427. }
  428. // Configure template variables. On the template variables HostFederatedKeys
  429. // and UserFederatedKeys we will skip the actual CA that will be available
  430. // in HostKey and UserKey.
  431. //
  432. // We cannot do it in the previous blocks because this configuration can be
  433. // injected using options.
  434. if a.sshCAHostCertSignKey != nil {
  435. tmplVars.SSH.HostKey = a.sshCAHostCertSignKey.PublicKey()
  436. tmplVars.SSH.HostFederatedKeys = append(tmplVars.SSH.HostFederatedKeys, a.sshCAHostFederatedCerts[1:]...)
  437. } else {
  438. tmplVars.SSH.HostFederatedKeys = append(tmplVars.SSH.HostFederatedKeys, a.sshCAHostFederatedCerts...)
  439. }
  440. if a.sshCAUserCertSignKey != nil {
  441. tmplVars.SSH.UserKey = a.sshCAUserCertSignKey.PublicKey()
  442. tmplVars.SSH.UserFederatedKeys = append(tmplVars.SSH.UserFederatedKeys, a.sshCAUserFederatedCerts[1:]...)
  443. } else {
  444. tmplVars.SSH.UserFederatedKeys = append(tmplVars.SSH.UserFederatedKeys, a.sshCAUserFederatedCerts...)
  445. }
  446. // Check if a KMS with decryption capability is required and available
  447. if a.requiresDecrypter() {
  448. if _, ok := a.keyManager.(kmsapi.Decrypter); !ok {
  449. return errors.New("keymanager doesn't provide crypto.Decrypter")
  450. }
  451. }
  452. // TODO: decide if this is a good approach for providing the SCEP functionality
  453. // It currently mirrors the logic for the x509CAService
  454. if a.requiresSCEPService() && a.scepService == nil {
  455. var options scep.Options
  456. // Read intermediate and create X509 signer and decrypter for default CAS.
  457. options.CertificateChain, err = pemutil.ReadCertificateBundle(a.config.IntermediateCert)
  458. if err != nil {
  459. return err
  460. }
  461. options.CertificateChain = append(options.CertificateChain, a.rootX509Certs...)
  462. options.Signer, err = a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  463. SigningKey: a.config.IntermediateKey,
  464. Password: []byte(a.password),
  465. })
  466. if err != nil {
  467. return err
  468. }
  469. if km, ok := a.keyManager.(kmsapi.Decrypter); ok {
  470. options.Decrypter, err = km.CreateDecrypter(&kmsapi.CreateDecrypterRequest{
  471. DecryptionKey: a.config.IntermediateKey,
  472. Password: []byte(a.password),
  473. })
  474. if err != nil {
  475. return err
  476. }
  477. }
  478. a.scepService, err = scep.NewService(context.Background(), options)
  479. if err != nil {
  480. return err
  481. }
  482. // TODO: mimick the x509CAService GetCertificateAuthority here too?
  483. }
  484. if a.config.AuthorityConfig.EnableAdmin {
  485. // Initialize step-ca Admin Database if it's not already initialized using
  486. // WithAdminDB.
  487. if a.adminDB == nil {
  488. if linkedcaClient != nil {
  489. a.adminDB = linkedcaClient
  490. } else {
  491. a.adminDB, err = adminDBNosql.New(a.db.(nosql.DB), admin.DefaultAuthorityID)
  492. if err != nil {
  493. return err
  494. }
  495. }
  496. }
  497. provs, err := a.adminDB.GetProvisioners(context.Background())
  498. if err != nil {
  499. return admin.WrapErrorISE(err, "error loading provisioners to initialize authority")
  500. }
  501. if len(provs) == 0 && !strings.EqualFold(a.config.AuthorityConfig.DeploymentType, "linked") {
  502. // Create First Provisioner
  503. prov, err := CreateFirstProvisioner(context.Background(), a.adminDB, string(a.password))
  504. if err != nil {
  505. return admin.WrapErrorISE(err, "error creating first provisioner")
  506. }
  507. // Create first admin
  508. if err := a.adminDB.CreateAdmin(context.Background(), &linkedca.Admin{
  509. ProvisionerId: prov.Id,
  510. Subject: "step",
  511. Type: linkedca.Admin_SUPER_ADMIN,
  512. }); err != nil {
  513. return admin.WrapErrorISE(err, "error creating first admin")
  514. }
  515. }
  516. }
  517. // Load Provisioners and Admins
  518. if err := a.reloadAdminResources(context.Background()); err != nil {
  519. return err
  520. }
  521. // Configure templates, currently only ssh templates are supported.
  522. if a.sshCAHostCertSignKey != nil || a.sshCAUserCertSignKey != nil {
  523. a.templates = a.config.Templates
  524. if a.templates == nil {
  525. a.templates = templates.DefaultTemplates()
  526. }
  527. if a.templates.Data == nil {
  528. a.templates.Data = make(map[string]interface{})
  529. }
  530. a.templates.Data["Step"] = tmplVars
  531. }
  532. // JWT numeric dates are seconds.
  533. a.startTime = time.Now().Truncate(time.Second)
  534. // Set flag indicating that initialization has been completed, and should
  535. // not be repeated.
  536. a.initOnce = true
  537. return nil
  538. }
  539. // GetDatabase returns the authority database. If the configuration does not
  540. // define a database, GetDatabase will return a db.SimpleDB instance.
  541. func (a *Authority) GetDatabase() db.AuthDB {
  542. return a.db
  543. }
  544. // GetAdminDatabase returns the admin database, if one exists.
  545. func (a *Authority) GetAdminDatabase() admin.DB {
  546. return a.adminDB
  547. }
  548. func (a *Authority) GetInfo() Info {
  549. ai := Info{
  550. StartTime: a.startTime,
  551. RootX509Certs: a.rootX509Certs,
  552. DNSNames: a.config.DNSNames,
  553. }
  554. if a.sshCAUserCertSignKey != nil {
  555. ai.SSHCAUserPublicKey = ssh.MarshalAuthorizedKey(a.sshCAUserCertSignKey.PublicKey())
  556. }
  557. if a.sshCAHostCertSignKey != nil {
  558. ai.SSHCAHostPublicKey = ssh.MarshalAuthorizedKey(a.sshCAHostCertSignKey.PublicKey())
  559. }
  560. return ai
  561. }
  562. // IsAdminAPIEnabled returns a boolean indicating whether the Admin API has
  563. // been enabled.
  564. func (a *Authority) IsAdminAPIEnabled() bool {
  565. return a.config.AuthorityConfig.EnableAdmin
  566. }
  567. // Shutdown safely shuts down any clients, databases, etc. held by the Authority.
  568. func (a *Authority) Shutdown() error {
  569. if err := a.keyManager.Close(); err != nil {
  570. log.Printf("error closing the key manager: %v", err)
  571. }
  572. return a.db.Shutdown()
  573. }
  574. // CloseForReload closes internal services, to allow a safe reload.
  575. func (a *Authority) CloseForReload() {
  576. if err := a.keyManager.Close(); err != nil {
  577. log.Printf("error closing the key manager: %v", err)
  578. }
  579. if client, ok := a.adminDB.(*linkedCaClient); ok {
  580. client.Stop()
  581. }
  582. }
  583. // IsRevoked returns whether or not a certificate has been
  584. // revoked before.
  585. func (a *Authority) IsRevoked(sn string) (bool, error) {
  586. // Check the passive revocation table.
  587. if lca, ok := a.adminDB.(interface {
  588. IsRevoked(string) (bool, error)
  589. }); ok {
  590. return lca.IsRevoked(sn)
  591. }
  592. return a.db.IsRevoked(sn)
  593. }
  594. // requiresDecrypter returns whether the Authority
  595. // requires a KMS that provides a crypto.Decrypter
  596. // Currently this is only required when SCEP is
  597. // enabled.
  598. func (a *Authority) requiresDecrypter() bool {
  599. return a.requiresSCEPService()
  600. }
  601. // requiresSCEPService iterates over the configured provisioners
  602. // and determines if one of them is a SCEP provisioner.
  603. func (a *Authority) requiresSCEPService() bool {
  604. for _, p := range a.config.AuthorityConfig.Provisioners {
  605. if p.GetType() == provisioner.TypeSCEP {
  606. return true
  607. }
  608. }
  609. return false
  610. }
  611. // GetSCEPService returns the configured SCEP Service
  612. // TODO: this function is intended to exist temporarily
  613. // in order to make SCEP work more easily. It can be
  614. // made more correct by using the right interfaces/abstractions
  615. // after it works as expected.
  616. func (a *Authority) GetSCEPService() *scep.Service {
  617. return a.scepService
  618. }