ssh.go 6.9 KB

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