options.go 11 KB

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