options.go 12 KB

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