options.go 11 KB

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