ssh.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package api
  2. import (
  3. "bytes"
  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. }
  15. // SignSSHRequest is the request body of an SSH certificate request.
  16. type SignSSHRequest struct {
  17. PublicKey []byte `json:"publicKey"` //base64 encoded
  18. OTT string `json:"ott"`
  19. CertType string `json:"certType"`
  20. Principals []string `json:"principals"`
  21. ValidAfter TimeDuration `json:"validAfter"`
  22. ValidBefore TimeDuration `json:"validBefore"`
  23. }
  24. // SignSSHResponse is the response object that returns the SSH certificate.
  25. type SignSSHResponse struct {
  26. Certificate SSHCertificate `json:"crt"`
  27. }
  28. // SSHCertificate represents the response SSH certificate.
  29. type SSHCertificate struct {
  30. *ssh.Certificate
  31. }
  32. // MarshalJSON implements the json.Marshaler interface. The certificate is
  33. // quoted string using the PEM encoding.
  34. func (c SSHCertificate) MarshalJSON() ([]byte, error) {
  35. if c.Certificate == nil {
  36. return []byte("null"), nil
  37. }
  38. s := base64.StdEncoding.EncodeToString(c.Certificate.Marshal())
  39. return []byte(`"` + s + `"`), nil
  40. }
  41. // UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
  42. // expected to be a quoted string using the PEM encoding.
  43. func (c *SSHCertificate) UnmarshalJSON(data []byte) error {
  44. var s string
  45. if err := json.Unmarshal(data, &s); err != nil {
  46. return errors.Wrap(err, "error decoding certificate")
  47. }
  48. certData, err := base64.StdEncoding.DecodeString(s)
  49. if err != nil {
  50. return errors.Wrap(err, "error decoding certificate")
  51. }
  52. pub, err := ssh.ParsePublicKey(certData)
  53. if err != nil {
  54. return errors.Wrap(err, "error decoding certificate")
  55. }
  56. cert, ok := pub.(*ssh.Certificate)
  57. if !ok {
  58. return errors.Errorf("error decoding certificate: %T is not an *ssh.Certificate", pub)
  59. }
  60. c.Certificate = cert
  61. return nil
  62. }
  63. // Validate validates the SignSSHRequest.
  64. func (s *SignSSHRequest) Validate() error {
  65. switch {
  66. case s.CertType != "" && s.CertType != provisioner.SSHUserCert && s.CertType != provisioner.SSHHostCert:
  67. return errors.Errorf("unknown certType %s", s.CertType)
  68. case len(s.PublicKey) == 0:
  69. return errors.New("missing or empty publicKey")
  70. case len(s.OTT) == 0:
  71. return errors.New("missing or empty ott")
  72. default:
  73. return nil
  74. }
  75. }
  76. // ParsePublicKey returns the ssh.PublicKey from the request.
  77. func (s *SignSSHRequest) ParsePublicKey() (ssh.PublicKey, error) {
  78. // Validate pub key.
  79. data := make([]byte, base64.StdEncoding.DecodedLen(len(s.PublicKey)))
  80. if _, err := base64.StdEncoding.Decode(data, s.PublicKey); err != nil {
  81. return nil, errors.Wrap(err, "error decoding publicKey")
  82. }
  83. // Trim padding from end of key.
  84. data = bytes.TrimRight(data, "\x00")
  85. publicKey, err := ssh.ParsePublicKey(data)
  86. if err != nil {
  87. return nil, errors.Wrap(err, "error parsing publicKey")
  88. }
  89. return publicKey, nil
  90. }
  91. // SignSSH is an HTTP handler that reads an SignSSHRequest with a one-time-token
  92. // (ott) from the body and creates a new SSH certificate with the information in
  93. // the request.
  94. func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
  95. var body SignSSHRequest
  96. if err := ReadJSON(r.Body, &body); err != nil {
  97. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  98. return
  99. }
  100. logOtt(w, body.OTT)
  101. if err := body.Validate(); err != nil {
  102. WriteError(w, err)
  103. return
  104. }
  105. publicKey, err := body.ParsePublicKey()
  106. if err != nil {
  107. WriteError(w, BadRequest(err))
  108. return
  109. }
  110. opts := provisioner.SSHOptions{
  111. CertType: body.CertType,
  112. Principals: body.Principals,
  113. ValidBefore: body.ValidBefore,
  114. ValidAfter: body.ValidAfter,
  115. }
  116. signOpts, err := h.Authority.AuthorizeSign(body.OTT)
  117. if err != nil {
  118. WriteError(w, Unauthorized(err))
  119. return
  120. }
  121. cert, err := h.Authority.SignSSH(publicKey, opts, signOpts...)
  122. if err != nil {
  123. WriteError(w, Forbidden(err))
  124. return
  125. }
  126. w.WriteHeader(http.StatusCreated)
  127. // logCertificate(w, cert)
  128. JSON(w, &SignSSHResponse{
  129. Certificate: SSHCertificate{cert},
  130. })
  131. }