ssh.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package api
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "net/http"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "golang.org/x/crypto/ssh"
  10. )
  11. // SSHAuthority is the interface implemented by a SSH CA authority.
  12. type SSHAuthority interface {
  13. SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
  14. SignSSHAddUser(key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error)
  15. }
  16. // SignSSHRequest is the request body of an SSH certificate request.
  17. type SignSSHRequest struct {
  18. PublicKey []byte `json:"publicKey"` //base64 encoded
  19. OTT string `json:"ott"`
  20. CertType string `json:"certType,omitempty"`
  21. Principals []string `json:"principals,omitempty"`
  22. ValidAfter TimeDuration `json:"validAfter,omitempty"`
  23. ValidBefore TimeDuration `json:"validBefore,omitempty"`
  24. AddUserPublicKey []byte `json:"addUserPublicKey,omitempty"`
  25. }
  26. // SignSSHResponse is the response object that returns the SSH certificate.
  27. type SignSSHResponse struct {
  28. Certificate SSHCertificate `json:"crt"`
  29. AddUserCertificate *SSHCertificate `json:"addUserCrt,omitempty"`
  30. }
  31. // SSHCertificate represents the response SSH certificate.
  32. type SSHCertificate struct {
  33. *ssh.Certificate `json:"omitempty"`
  34. }
  35. // MarshalJSON implements the json.Marshaler interface. The certificate is
  36. // quoted string using the PEM encoding.
  37. func (c SSHCertificate) MarshalJSON() ([]byte, error) {
  38. if c.Certificate == nil {
  39. return []byte("null"), nil
  40. }
  41. s := base64.StdEncoding.EncodeToString(c.Certificate.Marshal())
  42. return []byte(`"` + s + `"`), nil
  43. }
  44. // UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
  45. // expected to be a quoted string using the PEM encoding.
  46. func (c *SSHCertificate) UnmarshalJSON(data []byte) error {
  47. var s string
  48. if err := json.Unmarshal(data, &s); err != nil {
  49. return errors.Wrap(err, "error decoding certificate")
  50. }
  51. if s == "" {
  52. c.Certificate = nil
  53. return nil
  54. }
  55. certData, err := base64.StdEncoding.DecodeString(s)
  56. if err != nil {
  57. return errors.Wrap(err, "error decoding certificate")
  58. }
  59. pub, err := ssh.ParsePublicKey(certData)
  60. if err != nil {
  61. return errors.Wrap(err, "error decoding certificate")
  62. }
  63. cert, ok := pub.(*ssh.Certificate)
  64. if !ok {
  65. return errors.Errorf("error decoding certificate: %T is not an *ssh.Certificate", pub)
  66. }
  67. c.Certificate = cert
  68. return nil
  69. }
  70. // Validate validates the SignSSHRequest.
  71. func (s *SignSSHRequest) Validate() error {
  72. switch {
  73. case s.CertType != "" && s.CertType != provisioner.SSHUserCert && s.CertType != provisioner.SSHHostCert:
  74. return errors.Errorf("unknown certType %s", s.CertType)
  75. case len(s.PublicKey) == 0:
  76. return errors.New("missing or empty publicKey")
  77. case len(s.OTT) == 0:
  78. return errors.New("missing or empty ott")
  79. default:
  80. return nil
  81. }
  82. }
  83. // SignSSH is an HTTP handler that reads an SignSSHRequest with a one-time-token
  84. // (ott) from the body and creates a new SSH certificate with the information in
  85. // the request.
  86. func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
  87. var body SignSSHRequest
  88. if err := ReadJSON(r.Body, &body); err != nil {
  89. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  90. return
  91. }
  92. logOtt(w, body.OTT)
  93. if err := body.Validate(); err != nil {
  94. WriteError(w, BadRequest(err))
  95. return
  96. }
  97. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  98. if err != nil {
  99. WriteError(w, BadRequest(errors.Wrap(err, "error parsing publicKey")))
  100. return
  101. }
  102. var addUserPublicKey ssh.PublicKey
  103. if body.AddUserPublicKey != nil {
  104. addUserPublicKey, err = ssh.ParsePublicKey(body.AddUserPublicKey)
  105. if err != nil {
  106. WriteError(w, BadRequest(errors.Wrap(err, "error parsing addUserPublicKey")))
  107. return
  108. }
  109. }
  110. opts := provisioner.SSHOptions{
  111. CertType: body.CertType,
  112. Principals: body.Principals,
  113. ValidBefore: body.ValidBefore,
  114. ValidAfter: body.ValidAfter,
  115. }
  116. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignSSHMethod)
  117. signOpts, err := h.Authority.Authorize(ctx, body.OTT)
  118. if err != nil {
  119. WriteError(w, Unauthorized(err))
  120. return
  121. }
  122. cert, err := h.Authority.SignSSH(publicKey, opts, signOpts...)
  123. if err != nil {
  124. WriteError(w, Forbidden(err))
  125. return
  126. }
  127. var addUserCertificate *SSHCertificate
  128. if addUserPublicKey != nil && cert.CertType == ssh.UserCert && len(cert.ValidPrincipals) == 1 {
  129. addUserCert, err := h.Authority.SignSSHAddUser(addUserPublicKey, cert)
  130. if err != nil {
  131. WriteError(w, Forbidden(err))
  132. return
  133. }
  134. addUserCertificate = &SSHCertificate{addUserCert}
  135. }
  136. w.WriteHeader(http.StatusCreated)
  137. JSON(w, &SignSSHResponse{
  138. Certificate: SSHCertificate{cert},
  139. AddUserCertificate: addUserCertificate,
  140. })
  141. }