ssh.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }
  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. // SignSSH is an HTTP handler that reads an SignSSHRequest with a one-time-token
  77. // (ott) from the body and creates a new SSH certificate with the information in
  78. // the request.
  79. func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
  80. var body SignSSHRequest
  81. if err := ReadJSON(r.Body, &body); err != nil {
  82. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  83. return
  84. }
  85. logOtt(w, body.OTT)
  86. if err := body.Validate(); err != nil {
  87. WriteError(w, err)
  88. return
  89. }
  90. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  91. if err != nil {
  92. WriteError(w, BadRequest(errors.Wrap(err, "error parsing publicKey")))
  93. return
  94. }
  95. opts := provisioner.SSHOptions{
  96. CertType: body.CertType,
  97. Principals: body.Principals,
  98. ValidBefore: body.ValidBefore,
  99. ValidAfter: body.ValidAfter,
  100. }
  101. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignSSHMethod)
  102. signOpts, err := h.Authority.Authorize(ctx, body.OTT)
  103. if err != nil {
  104. WriteError(w, Unauthorized(err))
  105. return
  106. }
  107. cert, err := h.Authority.SignSSH(publicKey, opts, signOpts...)
  108. if err != nil {
  109. WriteError(w, Forbidden(err))
  110. return
  111. }
  112. w.WriteHeader(http.StatusCreated)
  113. // logCertificate(w, cert)
  114. JSON(w, &SignSSHResponse{
  115. Certificate: SSHCertificate{cert},
  116. })
  117. }