options.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/admin"
  9. "github.com/smallstep/certificates/authority/cache"
  10. "github.com/smallstep/certificates/authority/config"
  11. "github.com/smallstep/certificates/authority/provisioner"
  12. "github.com/smallstep/certificates/cas"
  13. casapi "github.com/smallstep/certificates/cas/apiv1"
  14. "github.com/smallstep/certificates/db"
  15. "github.com/smallstep/certificates/kms"
  16. "golang.org/x/crypto/ssh"
  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. // WithGetIdentityFunc sets a custom function to retrieve the identity from
  77. // an external resource.
  78. func WithGetIdentityFunc(fn func(ctx context.Context, p provisioner.Interface, email string) (*provisioner.Identity, error)) Option {
  79. return func(a *Authority) error {
  80. a.getIdentityFunc = fn
  81. return nil
  82. }
  83. }
  84. // WithAuthorizeRenewFunc sets a custom function that authorizes the renewal of
  85. // an X.509 certificate.
  86. func WithAuthorizeRenewFunc(fn func(ctx context.Context, p *provisioner.Controller, cert *x509.Certificate) error) Option {
  87. return func(a *Authority) error {
  88. a.authorizeRenewFunc = fn
  89. return nil
  90. }
  91. }
  92. // WithAuthorizeSSHRenewFunc sets a custom function that authorizes the renewal
  93. // of a SSH certificate.
  94. func WithAuthorizeSSHRenewFunc(fn func(ctx context.Context, p *provisioner.Controller, cert *ssh.Certificate) error) Option {
  95. return func(a *Authority) error {
  96. a.authorizeSSHRenewFunc = fn
  97. return nil
  98. }
  99. }
  100. // WithSSHBastionFunc sets a custom function to get the bastion for a
  101. // given user-host pair.
  102. func WithSSHBastionFunc(fn func(ctx context.Context, user, host string) (*config.Bastion, error)) Option {
  103. return func(a *Authority) error {
  104. a.sshBastionFunc = fn
  105. return nil
  106. }
  107. }
  108. // WithSSHGetHosts sets a custom function to return a list of step ssh enabled
  109. // hosts.
  110. func WithSSHGetHosts(fn func(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)) Option {
  111. return func(a *Authority) error {
  112. a.sshGetHostsFunc = fn
  113. return nil
  114. }
  115. }
  116. // WithSSHCheckHost sets a custom function to check whether a given host is
  117. // step ssh enabled. The token is used to validate the request, while the roots
  118. // are used to validate the token.
  119. func WithSSHCheckHost(fn func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)) Option {
  120. return func(a *Authority) error {
  121. a.sshCheckHostFunc = fn
  122. return nil
  123. }
  124. }
  125. // WithKeyManager defines the key manager used to get and create keys, and sign
  126. // certificates.
  127. func WithKeyManager(k kms.KeyManager) Option {
  128. return func(a *Authority) error {
  129. a.keyManager = k
  130. return nil
  131. }
  132. }
  133. // WithX509Signer defines the signer used to sign X509 certificates.
  134. func WithX509Signer(crt *x509.Certificate, s crypto.Signer) Option {
  135. return func(a *Authority) error {
  136. srv, err := cas.New(context.Background(), casapi.Options{
  137. Type: casapi.SoftCAS,
  138. Signer: s,
  139. CertificateChain: []*x509.Certificate{crt},
  140. })
  141. if err != nil {
  142. return err
  143. }
  144. a.x509CAService = srv
  145. return nil
  146. }
  147. }
  148. // WithX509SignerFunc defines the function used to get the chain of certificates
  149. // and signer used when we sign X.509 certificates.
  150. func WithX509SignerFunc(fn func() ([]*x509.Certificate, crypto.Signer, error)) Option {
  151. return func(a *Authority) error {
  152. srv, err := cas.New(context.Background(), casapi.Options{
  153. Type: casapi.SoftCAS,
  154. CertificateSigner: fn,
  155. })
  156. if err != nil {
  157. return err
  158. }
  159. a.x509CAService = srv
  160. return nil
  161. }
  162. }
  163. // WithSSHUserSigner defines the signer used to sign SSH user certificates.
  164. func WithSSHUserSigner(s crypto.Signer) Option {
  165. return func(a *Authority) error {
  166. signer, err := ssh.NewSignerFromSigner(s)
  167. if err != nil {
  168. return errors.Wrap(err, "error creating ssh user signer")
  169. }
  170. a.sshCAUserCertSignKey = signer
  171. // Append public key to list of user certs
  172. pub := signer.PublicKey()
  173. a.sshCAUserCerts = append(a.sshCAUserCerts, pub)
  174. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, pub)
  175. return nil
  176. }
  177. }
  178. // WithSSHHostSigner defines the signer used to sign SSH host certificates.
  179. func WithSSHHostSigner(s crypto.Signer) Option {
  180. return func(a *Authority) error {
  181. signer, err := ssh.NewSignerFromSigner(s)
  182. if err != nil {
  183. return errors.Wrap(err, "error creating ssh host signer")
  184. }
  185. a.sshCAHostCertSignKey = signer
  186. // Append public key to list of host certs
  187. pub := signer.PublicKey()
  188. a.sshCAHostCerts = append(a.sshCAHostCerts, pub)
  189. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, pub)
  190. return nil
  191. }
  192. }
  193. // WithX509RootCerts is an option that allows to define the list of root
  194. // certificates to use. This option will replace any root certificate defined
  195. // before.
  196. func WithX509RootCerts(rootCerts ...*x509.Certificate) Option {
  197. return func(a *Authority) error {
  198. a.rootX509Certs = rootCerts
  199. return nil
  200. }
  201. }
  202. // WithX509FederatedCerts is an option that allows to define the list of
  203. // federated certificates. This option will replace any federated certificate
  204. // defined before.
  205. func WithX509FederatedCerts(certs ...*x509.Certificate) Option {
  206. return func(a *Authority) error {
  207. a.federatedX509Certs = certs
  208. return nil
  209. }
  210. }
  211. // WithX509RootBundle is an option that allows to define the list of root
  212. // certificates. This option will replace any root certificate defined before.
  213. func WithX509RootBundle(pemCerts []byte) Option {
  214. return func(a *Authority) error {
  215. certs, err := readCertificateBundle(pemCerts)
  216. if err != nil {
  217. return err
  218. }
  219. a.rootX509Certs = certs
  220. return nil
  221. }
  222. }
  223. // WithX509FederatedBundle is an option that allows to define the list of
  224. // federated certificates. This option will replace any federated certificate
  225. // defined before.
  226. func WithX509FederatedBundle(pemCerts []byte) Option {
  227. return func(a *Authority) error {
  228. certs, err := readCertificateBundle(pemCerts)
  229. if err != nil {
  230. return err
  231. }
  232. a.federatedX509Certs = certs
  233. return nil
  234. }
  235. }
  236. // WithAdminDB is an option to set the database backing the admin APIs.
  237. func WithAdminDB(d admin.DB) Option {
  238. return func(a *Authority) error {
  239. a.adminDB = d
  240. return nil
  241. }
  242. }
  243. // WithLinkedCAToken is an option to set the authentication token used to enable
  244. // linked ca.
  245. func WithLinkedCAToken(token string) Option {
  246. return func(a *Authority) error {
  247. a.linkedCAToken = token
  248. return nil
  249. }
  250. }
  251. // WithX509Enforcers is an option that allows to define custom certificate
  252. // modifiers that will be processed just before the signing of the certificate.
  253. func WithX509Enforcers(ces ...provisioner.CertificateEnforcer) Option {
  254. return func(a *Authority) error {
  255. a.x509Enforcers = ces
  256. return nil
  257. }
  258. }
  259. // WithCachePool is an options that allows to define a custom cache pool.
  260. func WithCachePool(pool cache.Pool) Option {
  261. return func(a *Authority) error {
  262. a.cachePool = pool
  263. return nil
  264. }
  265. }
  266. func readCertificateBundle(pemCerts []byte) ([]*x509.Certificate, error) {
  267. var block *pem.Block
  268. var certs []*x509.Certificate
  269. for len(pemCerts) > 0 {
  270. block, pemCerts = pem.Decode(pemCerts)
  271. if block == nil {
  272. break
  273. }
  274. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  275. continue
  276. }
  277. cert, err := x509.ParseCertificate(block.Bytes)
  278. if err != nil {
  279. return nil, err
  280. }
  281. certs = append(certs, cert)
  282. }
  283. return certs, nil
  284. }