options.go 13 KB

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