remote.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package remote
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "github.com/cloudflare/cfssl/api/client"
  8. "github.com/cloudflare/cfssl/certdb"
  9. "github.com/cloudflare/cfssl/config"
  10. cferr "github.com/cloudflare/cfssl/errors"
  11. "github.com/cloudflare/cfssl/helpers"
  12. "github.com/cloudflare/cfssl/info"
  13. "github.com/cloudflare/cfssl/signer"
  14. )
  15. // A Signer represents a CFSSL instance running as signing server.
  16. // fulfills the signer.Signer interface
  17. type Signer struct {
  18. policy *config.Signing
  19. reqModifier func(*http.Request, []byte)
  20. }
  21. // NewSigner creates a new remote Signer directly from a
  22. // signing policy.
  23. func NewSigner(policy *config.Signing) (*Signer, error) {
  24. if policy != nil {
  25. if !policy.Valid() {
  26. return nil, cferr.New(cferr.PolicyError,
  27. cferr.InvalidPolicy)
  28. }
  29. return &Signer{policy: policy}, nil
  30. }
  31. return nil, cferr.New(cferr.PolicyError,
  32. cferr.InvalidPolicy)
  33. }
  34. // Sign sends a signature request to the remote CFSSL server,
  35. // receiving a signed certificate or an error in response. The hostname,
  36. // csr, and profileName are used as with a local signing operation, and
  37. // the label is used to select a signing root in a multi-root CA.
  38. func (s *Signer) Sign(req signer.SignRequest) (cert []byte, err error) {
  39. resp, err := s.remoteOp(req, req.Profile, "sign")
  40. if err != nil {
  41. return
  42. }
  43. if cert, ok := resp.([]byte); ok {
  44. return cert, nil
  45. }
  46. return
  47. }
  48. // Info sends an info request to the remote CFSSL server, receiving an
  49. // Resp struct or an error in response.
  50. func (s *Signer) Info(req info.Req) (resp *info.Resp, err error) {
  51. respInterface, err := s.remoteOp(req, req.Profile, "info")
  52. if err != nil {
  53. return
  54. }
  55. if resp, ok := respInterface.(*info.Resp); ok {
  56. return resp, nil
  57. }
  58. return
  59. }
  60. // Helper function to perform a remote sign or info request.
  61. func (s *Signer) remoteOp(req interface{}, profile, target string) (resp interface{}, err error) {
  62. jsonData, err := json.Marshal(req)
  63. if err != nil {
  64. return nil, cferr.Wrap(cferr.APIClientError, cferr.JSONError, err)
  65. }
  66. p, err := signer.Profile(s, profile)
  67. if err != nil {
  68. return
  69. }
  70. server := client.NewServerTLS(p.RemoteServer, helpers.CreateTLSConfig(p.RemoteCAs, p.ClientCert))
  71. if server == nil {
  72. return nil, cferr.Wrap(cferr.PolicyError, cferr.InvalidRequest,
  73. errors.New("failed to connect to remote"))
  74. }
  75. server.SetReqModifier(s.reqModifier)
  76. // There's no auth provider for the "info" method
  77. if target == "info" {
  78. resp, err = server.Info(jsonData)
  79. } else if p.RemoteProvider != nil {
  80. resp, err = server.AuthSign(jsonData, nil, p.RemoteProvider)
  81. } else {
  82. resp, err = server.Sign(jsonData)
  83. }
  84. if err != nil {
  85. return nil, err
  86. }
  87. return
  88. }
  89. // SigAlgo returns the RSA signer's signature algorithm.
  90. func (s *Signer) SigAlgo() x509.SignatureAlgorithm {
  91. // TODO: implement this as a remote info call
  92. return x509.UnknownSignatureAlgorithm
  93. }
  94. // SetPolicy sets the signer's signature policy.
  95. func (s *Signer) SetPolicy(policy *config.Signing) {
  96. s.policy = policy
  97. }
  98. // SetDBAccessor sets the signers' cert db accessor, currently noop.
  99. func (s *Signer) SetDBAccessor(dba certdb.Accessor) {
  100. // noop
  101. }
  102. // GetDBAccessor returns the signers' cert db accessor, currently noop.
  103. func (s *Signer) GetDBAccessor() certdb.Accessor {
  104. return nil
  105. }
  106. // SetReqModifier sets the function to call to modify the HTTP request prior to sending it
  107. func (s *Signer) SetReqModifier(mod func(*http.Request, []byte)) {
  108. s.reqModifier = mod
  109. }
  110. // Policy returns the signer's policy.
  111. func (s *Signer) Policy() *config.Signing {
  112. return s.policy
  113. }