options.go 12 KB

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