authority.go 20 KB

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