ssh.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package authority
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "net/http"
  6. "strings"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/cli/crypto/randutil"
  10. "golang.org/x/crypto/ssh"
  11. )
  12. const (
  13. // SSHAddUserPrincipal is the principal that will run the add user command.
  14. // Defaults to "provisioner" but it can be changed in the configuration.
  15. SSHAddUserPrincipal = "provisioner"
  16. // SSHAddUserCommand is the default command to run to add a new user.
  17. // Defaults to "sudo useradd -m <principal>; nc -q0 localhost 22" but it can be changed in the
  18. // configuration. The string "<principal>" will be replace by the new
  19. // principal to add.
  20. SSHAddUserCommand = "sudo useradd -m <principal>; nc -q0 localhost 22"
  21. )
  22. // SignSSH creates a signed SSH certificate with the given public key and options.
  23. func (a *Authority) SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  24. var mods []provisioner.SSHCertificateModifier
  25. var validators []provisioner.SSHCertificateValidator
  26. for _, op := range signOpts {
  27. switch o := op.(type) {
  28. // modify the ssh.Certificate
  29. case provisioner.SSHCertificateModifier:
  30. mods = append(mods, o)
  31. // modify the ssh.Certificate given the SSHOptions
  32. case provisioner.SSHCertificateOptionModifier:
  33. mods = append(mods, o.Option(opts))
  34. // validate the ssh.Certificate
  35. case provisioner.SSHCertificateValidator:
  36. validators = append(validators, o)
  37. // validate the given SSHOptions
  38. case provisioner.SSHCertificateOptionsValidator:
  39. if err := o.Valid(opts); err != nil {
  40. return nil, &apiError{err: err, code: http.StatusForbidden}
  41. }
  42. default:
  43. return nil, &apiError{
  44. err: errors.Errorf("signSSH: invalid extra option type %T", o),
  45. code: http.StatusInternalServerError,
  46. }
  47. }
  48. }
  49. nonce, err := randutil.ASCII(32)
  50. if err != nil {
  51. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  52. }
  53. var serial uint64
  54. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  55. return nil, &apiError{
  56. err: errors.Wrap(err, "signSSH: error reading random number"),
  57. code: http.StatusInternalServerError,
  58. }
  59. }
  60. // Build base certificate with the key and some random values
  61. cert := &ssh.Certificate{
  62. Nonce: []byte(nonce),
  63. Key: key,
  64. Serial: serial,
  65. }
  66. // Use opts to modify the certificate
  67. if err := opts.Modify(cert); err != nil {
  68. return nil, &apiError{err: err, code: http.StatusForbidden}
  69. }
  70. // Use provisioner modifiers
  71. for _, m := range mods {
  72. if err := m.Modify(cert); err != nil {
  73. return nil, &apiError{err: err, code: http.StatusForbidden}
  74. }
  75. }
  76. // Get signer from authority keys
  77. var signer ssh.Signer
  78. switch cert.CertType {
  79. case ssh.UserCert:
  80. if a.sshCAUserCertSignKey == nil {
  81. return nil, &apiError{
  82. err: errors.New("signSSH: user certificate signing is not enabled"),
  83. code: http.StatusNotImplemented,
  84. }
  85. }
  86. if signer, err = ssh.NewSignerFromSigner(a.sshCAUserCertSignKey); err != nil {
  87. return nil, &apiError{
  88. err: errors.Wrap(err, "signSSH: error creating signer"),
  89. code: http.StatusInternalServerError,
  90. }
  91. }
  92. case ssh.HostCert:
  93. if a.sshCAHostCertSignKey == nil {
  94. return nil, &apiError{
  95. err: errors.New("signSSH: host certificate signing is not enabled"),
  96. code: http.StatusNotImplemented,
  97. }
  98. }
  99. if signer, err = ssh.NewSignerFromSigner(a.sshCAHostCertSignKey); err != nil {
  100. return nil, &apiError{
  101. err: errors.Wrap(err, "signSSH: error creating signer"),
  102. code: http.StatusInternalServerError,
  103. }
  104. }
  105. default:
  106. return nil, &apiError{
  107. err: errors.Errorf("signSSH: unexpected ssh certificate type: %d", cert.CertType),
  108. code: http.StatusInternalServerError,
  109. }
  110. }
  111. cert.SignatureKey = signer.PublicKey()
  112. // Get bytes for signing trailing the signature length.
  113. data := cert.Marshal()
  114. data = data[:len(data)-4]
  115. // Sign the certificate
  116. sig, err := signer.Sign(rand.Reader, data)
  117. if err != nil {
  118. return nil, &apiError{
  119. err: errors.Wrap(err, "signSSH: error signing certificate"),
  120. code: http.StatusInternalServerError,
  121. }
  122. }
  123. cert.Signature = sig
  124. // User provisioners validators
  125. for _, v := range validators {
  126. if err := v.Valid(cert); err != nil {
  127. return nil, &apiError{err: err, code: http.StatusForbidden}
  128. }
  129. }
  130. return cert, nil
  131. }
  132. // SignSSHAddUser signs a certificate that provisions a new user in a server.
  133. func (a *Authority) SignSSHAddUser(key ssh.PublicKey, subject *ssh.Certificate) (*ssh.Certificate, error) {
  134. if a.sshCAUserCertSignKey == nil {
  135. return nil, &apiError{
  136. err: errors.New("signSSHAddUser: user certificate signing is not enabled"),
  137. code: http.StatusNotImplemented,
  138. }
  139. }
  140. if subject.CertType != ssh.UserCert {
  141. return nil, &apiError{
  142. err: errors.New("signSSHProxy: certificate is not a user certificate"),
  143. code: http.StatusForbidden,
  144. }
  145. }
  146. if len(subject.ValidPrincipals) != 1 {
  147. return nil, &apiError{
  148. err: errors.New("signSSHProxy: certificate does not have only one principal"),
  149. code: http.StatusForbidden,
  150. }
  151. }
  152. nonce, err := randutil.ASCII(32)
  153. if err != nil {
  154. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  155. }
  156. var serial uint64
  157. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  158. return nil, &apiError{
  159. err: errors.Wrap(err, "signSSHProxy: error reading random number"),
  160. code: http.StatusInternalServerError,
  161. }
  162. }
  163. signer, err := ssh.NewSignerFromSigner(a.sshCAUserCertSignKey)
  164. if err != nil {
  165. return nil, &apiError{
  166. err: errors.Wrap(err, "signSSHProxy: error creating signer"),
  167. code: http.StatusInternalServerError,
  168. }
  169. }
  170. principal := subject.ValidPrincipals[0]
  171. addUserPrincipal := a.getAddUserPrincipal()
  172. cert := &ssh.Certificate{
  173. Nonce: []byte(nonce),
  174. Key: key,
  175. Serial: serial,
  176. CertType: ssh.UserCert,
  177. KeyId: principal + "-" + addUserPrincipal,
  178. ValidPrincipals: []string{addUserPrincipal},
  179. ValidAfter: subject.ValidAfter,
  180. ValidBefore: subject.ValidBefore,
  181. Permissions: ssh.Permissions{
  182. CriticalOptions: map[string]string{
  183. "force-command": a.getAddUserCommand(principal),
  184. },
  185. },
  186. SignatureKey: signer.PublicKey(),
  187. }
  188. // Get bytes for signing trailing the signature length.
  189. data := cert.Marshal()
  190. data = data[:len(data)-4]
  191. // Sign the certificate
  192. sig, err := signer.Sign(rand.Reader, data)
  193. if err != nil {
  194. return nil, err
  195. }
  196. cert.Signature = sig
  197. return cert, nil
  198. }
  199. func (a *Authority) getAddUserPrincipal() (cmd string) {
  200. if a.config.SSH.AddUserPrincipal == "" {
  201. return SSHAddUserPrincipal
  202. }
  203. return a.config.SSH.AddUserPrincipal
  204. }
  205. func (a *Authority) getAddUserCommand(principal string) string {
  206. var cmd string
  207. if a.config.SSH.AddUserCommand == "" {
  208. cmd = SSHAddUserCommand
  209. } else {
  210. cmd = a.config.SSH.AddUserCommand
  211. }
  212. return strings.Replace(cmd, "<principal>", principal, -1)
  213. }