options.go 9.2 KB

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