options.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "github.com/pkg/errors"
  8. "golang.org/x/crypto/ssh"
  9. "go.step.sm/crypto/kms"
  10. "github.com/smallstep/certificates/authority/admin"
  11. "github.com/smallstep/certificates/authority/config"
  12. "github.com/smallstep/certificates/authority/provisioner"
  13. "github.com/smallstep/certificates/authority/provisioner/androidkey"
  14. "github.com/smallstep/certificates/cas"
  15. casapi "github.com/smallstep/certificates/cas/apiv1"
  16. "github.com/smallstep/certificates/db"
  17. "github.com/smallstep/certificates/est"
  18. "github.com/smallstep/certificates/internal/httptransport"
  19. "github.com/smallstep/certificates/scep"
  20. )
  21. // Option sets options to the Authority.
  22. type Option func(*Authority) error
  23. // WithConfig replaces the current config with the given one. No validation is
  24. // performed in the given value.
  25. func WithConfig(cfg *config.Config) Option {
  26. return func(a *Authority) error {
  27. a.config = cfg
  28. return nil
  29. }
  30. }
  31. // WithConfigFile reads the given filename as a configuration file and replaces
  32. // the current one. No validation is performed in the given configuration.
  33. func WithConfigFile(filename string) Option {
  34. return func(a *Authority) (err error) {
  35. a.config, err = config.LoadConfiguration(filename)
  36. return
  37. }
  38. }
  39. // WithPassword set the password to decrypt the intermediate key as well as the
  40. // ssh host and user keys if they are not overridden by other options.
  41. func WithPassword(password []byte) Option {
  42. return func(a *Authority) (err error) {
  43. a.password = password
  44. return
  45. }
  46. }
  47. // WithSSHHostPassword set the password to decrypt the key used to sign SSH host
  48. // certificates.
  49. func WithSSHHostPassword(password []byte) Option {
  50. return func(a *Authority) (err error) {
  51. a.sshHostPassword = password
  52. return
  53. }
  54. }
  55. // WithSSHUserPassword set the password to decrypt the key used to sign SSH user
  56. // certificates.
  57. func WithSSHUserPassword(password []byte) Option {
  58. return func(a *Authority) (err error) {
  59. a.sshUserPassword = password
  60. return
  61. }
  62. }
  63. // WithIssuerPassword set the password to decrypt the certificate issuer private
  64. // key used in RA mode.
  65. func WithIssuerPassword(password []byte) Option {
  66. return func(a *Authority) (err error) {
  67. a.issuerPassword = password
  68. return
  69. }
  70. }
  71. // WithDatabase sets an already initialized authority database to a new
  72. // authority. This option is intended to be use on graceful reloads.
  73. func WithDatabase(d db.AuthDB) Option {
  74. return func(a *Authority) error {
  75. a.db = d
  76. return nil
  77. }
  78. }
  79. // WithQuietInit disables log output when the authority is initialized.
  80. func WithQuietInit() Option {
  81. return func(a *Authority) error {
  82. a.quietInit = true
  83. return nil
  84. }
  85. }
  86. // WithWebhookClient sets the http.Client to be used for outbound requests.
  87. func WithWebhookClient(c provisioner.HTTPClient) Option {
  88. return func(a *Authority) error {
  89. a.webhookClient = c
  90. return nil
  91. }
  92. }
  93. // WithAndroidKeyCRLChecker sets the [androidkey.CRLChecker] to be
  94. // used to check Android Key Attestation certificate revocation status.
  95. func WithAndroidKeyCRLChecker(c androidkey.CRLChecker) Option {
  96. return func(a *Authority) error {
  97. a.androidKeyCRLChecker = c
  98. return nil
  99. }
  100. }
  101. // Wrapper wraps the set of functions mapping [http.Transport] references to [http.RoundTripper].
  102. type TransportWrapper = httptransport.Wrapper
  103. // WithTransportWrapper sets the transport wrapper of the authority to the provided one or, in case
  104. // that one is nil, to a noop one.
  105. func WithTransportWrapper(tw httptransport.Wrapper) Option {
  106. if tw == nil {
  107. tw = httptransport.NoopWrapper()
  108. }
  109. return func(a *Authority) error {
  110. a.wrapTransport = tw
  111. return nil
  112. }
  113. }
  114. // WithGetIdentityFunc sets a custom function to retrieve the identity from
  115. // an external resource.
  116. func WithGetIdentityFunc(fn func(ctx context.Context, p provisioner.Interface, email string) (*provisioner.Identity, error)) Option {
  117. return func(a *Authority) error {
  118. a.getIdentityFunc = fn
  119. return nil
  120. }
  121. }
  122. // WithAuthorizeRenewFunc sets a custom function that authorizes the renewal of
  123. // an X.509 certificate.
  124. func WithAuthorizeRenewFunc(fn func(ctx context.Context, p *provisioner.Controller, cert *x509.Certificate) error) Option {
  125. return func(a *Authority) error {
  126. a.authorizeRenewFunc = fn
  127. return nil
  128. }
  129. }
  130. // WithAuthorizeSSHRenewFunc sets a custom function that authorizes the renewal
  131. // of a SSH certificate.
  132. func WithAuthorizeSSHRenewFunc(fn func(ctx context.Context, p *provisioner.Controller, cert *ssh.Certificate) error) Option {
  133. return func(a *Authority) error {
  134. a.authorizeSSHRenewFunc = fn
  135. return nil
  136. }
  137. }
  138. // WithSSHBastionFunc sets a custom function to get the bastion for a
  139. // given user-host pair.
  140. func WithSSHBastionFunc(fn func(ctx context.Context, user, host string) (*config.Bastion, error)) Option {
  141. return func(a *Authority) error {
  142. a.sshBastionFunc = fn
  143. return nil
  144. }
  145. }
  146. // WithSSHGetHosts sets a custom function to return a list of step ssh enabled
  147. // hosts.
  148. func WithSSHGetHosts(fn func(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)) Option {
  149. return func(a *Authority) error {
  150. a.sshGetHostsFunc = fn
  151. return nil
  152. }
  153. }
  154. // WithSSHCheckHost sets a custom function to check whether a given host is
  155. // step ssh enabled. The token is used to validate the request, while the roots
  156. // are used to validate the token.
  157. func WithSSHCheckHost(fn func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)) Option {
  158. return func(a *Authority) error {
  159. a.sshCheckHostFunc = fn
  160. return nil
  161. }
  162. }
  163. // WithKeyManager defines the key manager used to get and create keys, and sign
  164. // certificates.
  165. func WithKeyManager(k kms.KeyManager) Option {
  166. return func(a *Authority) error {
  167. a.keyManager = k
  168. return nil
  169. }
  170. }
  171. // WithX509CAService allows the consumer to provide an externally implemented
  172. // API implementation of apiv1.CertificateAuthorityService
  173. func WithX509CAService(svc casapi.CertificateAuthorityService) Option {
  174. return func(a *Authority) error {
  175. a.x509CAService = svc
  176. return nil
  177. }
  178. }
  179. // WithX509Signer defines the signer used to sign X509 certificates.
  180. func WithX509Signer(crt *x509.Certificate, s crypto.Signer) Option {
  181. return WithX509SignerChain([]*x509.Certificate{crt}, s)
  182. }
  183. // WithX509SignerChain defines the signer used to sign X509 certificates. This
  184. // option is similar to WithX509Signer but it supports a chain of intermediates.
  185. func WithX509SignerChain(issuerChain []*x509.Certificate, s crypto.Signer) Option {
  186. return func(a *Authority) error {
  187. srv, err := cas.New(context.Background(), casapi.Options{
  188. Type: casapi.SoftCAS,
  189. Signer: s,
  190. CertificateChain: issuerChain,
  191. })
  192. if err != nil {
  193. return err
  194. }
  195. a.x509CAService = srv
  196. a.intermediateX509Certs = append(a.intermediateX509Certs, issuerChain...)
  197. return nil
  198. }
  199. }
  200. // WithX509SignerFunc defines the function used to get the chain of certificates
  201. // and signer used when we sign X.509 certificates.
  202. func WithX509SignerFunc(fn func() ([]*x509.Certificate, crypto.Signer, error)) Option {
  203. return func(a *Authority) error {
  204. srv, err := cas.New(context.Background(), casapi.Options{
  205. Type: casapi.SoftCAS,
  206. CertificateSigner: fn,
  207. })
  208. if err != nil {
  209. return err
  210. }
  211. a.x509CAService = srv
  212. return nil
  213. }
  214. }
  215. // WithFullSCEPOptions defines the options used for SCEP support.
  216. //
  217. // This feature is EXPERIMENTAL and might change at any time.
  218. func WithFullSCEPOptions(options *scep.Options) Option {
  219. return func(a *Authority) error {
  220. a.scepOptions = options
  221. a.validateSCEP = false
  222. return nil
  223. }
  224. }
  225. // WithFullESTOptions defines the options used for EST support.
  226. //
  227. // This feature is EXPERIMENTAL and might change at any time.
  228. func WithFullESTOptions(options *est.Options) Option {
  229. return func(a *Authority) error {
  230. a.estOptions = options
  231. return nil
  232. }
  233. }
  234. // WithSCEPKeyManager defines the key manager used on SCEP provisioners.
  235. //
  236. // This feature is EXPERIMENTAL and might change at any time.
  237. func WithSCEPKeyManager(skm provisioner.SCEPKeyManager) Option {
  238. return func(a *Authority) error {
  239. a.scepKeyManager = skm
  240. return nil
  241. }
  242. }
  243. // WithSSHUserSigner defines the signer used to sign SSH user certificates.
  244. func WithSSHUserSigner(s crypto.Signer) Option {
  245. return func(a *Authority) error {
  246. signer, err := ssh.NewSignerFromSigner(s)
  247. if err != nil {
  248. return errors.Wrap(err, "error creating ssh user signer")
  249. }
  250. a.sshCAUserCertSignKey = signer
  251. // Append public key to list of user certs
  252. pub := signer.PublicKey()
  253. a.sshCAUserCerts = append(a.sshCAUserCerts, pub)
  254. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, pub)
  255. return nil
  256. }
  257. }
  258. // WithSSHHostSigner defines the signer used to sign SSH host certificates.
  259. func WithSSHHostSigner(s crypto.Signer) Option {
  260. return func(a *Authority) error {
  261. signer, err := ssh.NewSignerFromSigner(s)
  262. if err != nil {
  263. return errors.Wrap(err, "error creating ssh host signer")
  264. }
  265. a.sshCAHostCertSignKey = signer
  266. // Append public key to list of host certs
  267. pub := signer.PublicKey()
  268. a.sshCAHostCerts = append(a.sshCAHostCerts, pub)
  269. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, pub)
  270. return nil
  271. }
  272. }
  273. // WithX509RootCerts is an option that allows to define the list of root
  274. // certificates to use. This option will replace any root certificate defined
  275. // before.
  276. func WithX509RootCerts(rootCerts ...*x509.Certificate) Option {
  277. return func(a *Authority) error {
  278. a.rootX509Certs = rootCerts
  279. return nil
  280. }
  281. }
  282. // WithX509FederatedCerts is an option that allows to define the list of
  283. // federated certificates. This option will replace any federated certificate
  284. // defined before.
  285. func WithX509FederatedCerts(certs ...*x509.Certificate) Option {
  286. return func(a *Authority) error {
  287. a.federatedX509Certs = certs
  288. return nil
  289. }
  290. }
  291. // WithX509IntermediateCerts is an option that allows to define the list of
  292. // intermediate certificates that the CA will be using. This option will replace
  293. // any intermediate certificate defined before.
  294. //
  295. // Note that these certificates will not be bundled with the certificates signed
  296. // by the CA, because the CAS service will take care of that. They should match,
  297. // but that's not guaranteed. These certificates will be mainly used for name
  298. // constraint validation before a certificate is issued.
  299. //
  300. // This option should only be used on specific configurations, for example when
  301. // WithX509SignerFunc is used, as we don't know the list of intermediates in
  302. // advance.
  303. func WithX509IntermediateCerts(intermediateCerts ...*x509.Certificate) Option {
  304. return func(a *Authority) error {
  305. a.intermediateX509Certs = intermediateCerts
  306. return nil
  307. }
  308. }
  309. // WithX509RootBundle is an option that allows to define the list of root
  310. // certificates. This option will replace any root certificate defined before.
  311. func WithX509RootBundle(pemCerts []byte) Option {
  312. return func(a *Authority) error {
  313. certs, err := readCertificateBundle(pemCerts)
  314. if err != nil {
  315. return err
  316. }
  317. a.rootX509Certs = certs
  318. return nil
  319. }
  320. }
  321. // WithX509FederatedBundle is an option that allows to define the list of
  322. // federated certificates. This option will replace any federated certificate
  323. // defined before.
  324. func WithX509FederatedBundle(pemCerts []byte) Option {
  325. return func(a *Authority) error {
  326. certs, err := readCertificateBundle(pemCerts)
  327. if err != nil {
  328. return err
  329. }
  330. a.federatedX509Certs = certs
  331. return nil
  332. }
  333. }
  334. // WithAdminDB is an option to set the database backing the admin APIs.
  335. func WithAdminDB(d admin.DB) Option {
  336. return func(a *Authority) error {
  337. a.adminDB = d
  338. return nil
  339. }
  340. }
  341. // WithProvisioners is an option to set the provisioner collection.
  342. //
  343. // Deprecated: provisioner collections will likely change
  344. func WithProvisioners(ps *provisioner.Collection) Option {
  345. return func(a *Authority) error {
  346. a.provisioners = ps
  347. return nil
  348. }
  349. }
  350. // WithLinkedCAToken is an option to set the authentication token used to enable
  351. // linked ca.
  352. func WithLinkedCAToken(token string) Option {
  353. return func(a *Authority) error {
  354. a.linkedCAToken = token
  355. return nil
  356. }
  357. }
  358. // WithX509Enforcers is an option that allows to define custom certificate
  359. // modifiers that will be processed just before the signing of the certificate.
  360. func WithX509Enforcers(ces ...provisioner.CertificateEnforcer) Option {
  361. return func(a *Authority) error {
  362. a.x509Enforcers = ces
  363. return nil
  364. }
  365. }
  366. // WithSkipInit is an option that allows the constructor to skip initialization
  367. // of the authority.
  368. func WithSkipInit() Option {
  369. return func(a *Authority) error {
  370. a.skipInit = true
  371. return nil
  372. }
  373. }
  374. func readCertificateBundle(pemCerts []byte) ([]*x509.Certificate, error) {
  375. var block *pem.Block
  376. var certs []*x509.Certificate
  377. for len(pemCerts) > 0 {
  378. block, pemCerts = pem.Decode(pemCerts)
  379. if block == nil {
  380. break
  381. }
  382. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  383. continue
  384. }
  385. cert, err := x509.ParseCertificate(block.Bytes)
  386. if err != nil {
  387. return nil, err
  388. }
  389. certs = append(certs, cert)
  390. }
  391. return certs, nil
  392. }
  393. // WithMeter is an option that sets the authority's [Meter] to the provided one.
  394. func WithMeter(m Meter) Option {
  395. if m == nil {
  396. m = noopMeter{}
  397. }
  398. return func(a *Authority) (_ error) {
  399. a.meter = m
  400. return
  401. }
  402. }