remote_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package remote
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "fmt"
  7. "math/big"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "testing"
  12. "time"
  13. apiinfo "github.com/cloudflare/cfssl/api/info"
  14. apisign "github.com/cloudflare/cfssl/api/signhandler"
  15. "github.com/cloudflare/cfssl/config"
  16. "github.com/cloudflare/cfssl/helpers"
  17. "github.com/cloudflare/cfssl/helpers/testsuite"
  18. "github.com/cloudflare/cfssl/info"
  19. "github.com/cloudflare/cfssl/signer"
  20. "github.com/cloudflare/cfssl/signer/local"
  21. )
  22. const (
  23. testCaFile = "testdata/ca.pem"
  24. testCaKeyFile = "testdata/ca_key.pem"
  25. testServerFile = "testdata/server.pem"
  26. testServerKeyFile = "testdata/server-key.pem"
  27. testClientFile = "testdata/client.pem"
  28. testClientKeyFile = "testdata/client-key.pem"
  29. )
  30. var validMinimalRemoteConfig = `
  31. {
  32. "signing": {
  33. "default": {
  34. "remote": "localhost"
  35. }
  36. },
  37. "remotes": {
  38. "localhost": "http://127.0.0.1:80"
  39. }
  40. }`
  41. var validMinimalAuthRemoteConfig = `
  42. {
  43. "signing": {
  44. "default": {
  45. "auth_key": "sample",
  46. "remote": "localhost"
  47. }
  48. },
  49. "auth_keys": {
  50. "sample": {
  51. "type":"standard",
  52. "key":"0123456789ABCDEF0123456789ABCDEF"
  53. }
  54. },
  55. "remotes": {
  56. "localhost": "http://127.0.0.1:80"
  57. }
  58. }`
  59. func TestNewSigner(t *testing.T) {
  60. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  61. _, err := NewSigner(remoteConfig.Signing)
  62. if err != nil {
  63. t.Fatal("fail to init remote signer:", err)
  64. }
  65. }
  66. func TestNewAuthSigner(t *testing.T) {
  67. remoteAuthConfig := testsuite.NewConfig(t, []byte(validMinimalAuthRemoteConfig))
  68. _, err := NewSigner(remoteAuthConfig.Signing)
  69. if err != nil {
  70. t.Fatal("fail to init remote signer:", err)
  71. }
  72. }
  73. func TestRemoteInfo(t *testing.T) {
  74. remoteServer := newTestInfoServer(t, false, nil)
  75. defer closeTestServer(t, remoteServer)
  76. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  77. // override with test server address
  78. remoteConfig.Signing.OverrideRemotes(remoteServer.URL)
  79. verifyRemoteInfo(t, remoteConfig)
  80. }
  81. func TestRemoteTLSInfo(t *testing.T) {
  82. remoteTLSInfo(t, false)
  83. }
  84. func TestRemoteMutualTLSInfo(t *testing.T) {
  85. remoteTLSInfo(t, true)
  86. }
  87. func remoteTLSInfo(t *testing.T, isMutual bool) {
  88. t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
  89. certPool, err := helpers.LoadPEMCertPool(testCaFile)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. var clientCA *x509.CertPool
  94. if isMutual {
  95. clientCA = certPool
  96. }
  97. remoteServer := newTestInfoServer(t, true, clientCA)
  98. defer closeTestServer(t, remoteServer)
  99. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  100. // override with full server URL to get https in protocol"
  101. t.Log("remote is:", remoteServer.URL)
  102. remoteConfig.Signing.OverrideRemotes(remoteServer.URL)
  103. remoteConfig.Signing.SetRemoteCAs(certPool)
  104. if isMutual {
  105. remoteConfig.Signing.SetClientCertKeyPairFromFile(testClientFile, testClientKeyFile)
  106. }
  107. verifyRemoteInfo(t, remoteConfig)
  108. }
  109. func verifyRemoteInfo(t *testing.T, remoteConfig *config.Config) {
  110. s := newRemoteSigner(t, remoteConfig.Signing)
  111. req := info.Req{}
  112. resp, err := s.Info(req)
  113. if err != nil {
  114. t.Fatal("remote info failed:", err)
  115. }
  116. caBytes, err := os.ReadFile(testCaFile)
  117. caBytes = bytes.TrimSpace(caBytes)
  118. if err != nil {
  119. t.Fatal("fail to read test CA cert:", err)
  120. }
  121. if bytes.Compare(caBytes, []byte(resp.Certificate)) != 0 {
  122. t.Fatal("Get a different CA cert through info api.", len(resp.Certificate), len(caBytes))
  123. }
  124. }
  125. func TestRemoteSign(t *testing.T) {
  126. remoteServer := newTestSignServer(t, false, nil)
  127. defer closeTestServer(t, remoteServer)
  128. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  129. // override with test server address
  130. remoteConfig.Signing.OverrideRemotes(remoteServer.URL)
  131. verifyRemoteSign(t, remoteConfig)
  132. }
  133. func TestRemoteTLSSign(t *testing.T) {
  134. remoteTLSSign(t, false)
  135. }
  136. func TestRemoteMutualTLSSign(t *testing.T) {
  137. remoteTLSSign(t, true)
  138. }
  139. func remoteTLSSign(t *testing.T, isMutual bool) {
  140. t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
  141. certPool, err := helpers.LoadPEMCertPool(testCaFile)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. var clientCA *x509.CertPool
  146. if isMutual {
  147. clientCA = certPool
  148. }
  149. remoteServer := newTestSignServer(t, true, clientCA)
  150. defer closeTestServer(t, remoteServer)
  151. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  152. // override with full server URL to get https in protocol"
  153. remoteConfig.Signing.OverrideRemotes(remoteServer.URL)
  154. remoteConfig.Signing.SetRemoteCAs(certPool)
  155. if isMutual {
  156. remoteConfig.Signing.SetClientCertKeyPairFromFile(testClientFile, testClientKeyFile)
  157. }
  158. verifyRemoteSign(t, remoteConfig)
  159. }
  160. func verifyRemoteSign(t *testing.T, remoteConfig *config.Config) {
  161. s := newRemoteSigner(t, remoteConfig.Signing)
  162. hosts := []string{"cloudflare.com"}
  163. for _, test := range testsuite.CSRTests {
  164. csr, err := os.ReadFile(test.File)
  165. if err != nil {
  166. t.Fatal("CSR loading error:", err)
  167. }
  168. testSerial := big.NewInt(0x7007F)
  169. certBytes, err := s.Sign(signer.SignRequest{
  170. Hosts: hosts,
  171. Request: string(csr),
  172. Serial: testSerial,
  173. })
  174. if test.ErrorCallback != nil {
  175. test.ErrorCallback(t, err)
  176. } else {
  177. if err != nil {
  178. t.Fatalf("Expected no error. Got %s. Param %s %d", err.Error(), test.KeyAlgo, test.KeyLen)
  179. }
  180. cert, err := helpers.ParseCertificatePEM(certBytes)
  181. if err != nil {
  182. t.Fatal("Fail to parse returned certificate:", err)
  183. }
  184. sn := fmt.Sprintf("%X", cert.SerialNumber)
  185. if sn != "7007F" {
  186. t.Fatal("Serial Number was incorrect:", sn)
  187. }
  188. }
  189. }
  190. }
  191. func TestRemoteSignBadServerAndOverride(t *testing.T) {
  192. remoteServer := newTestSignServer(t, false, nil)
  193. defer closeTestServer(t, remoteServer)
  194. // remoteConfig contains port 80 that no test server will listen on
  195. remoteConfig := testsuite.NewConfig(t, []byte(validMinimalRemoteConfig))
  196. s := newRemoteSigner(t, remoteConfig.Signing)
  197. hosts := []string{"cloudflare.com"}
  198. csr, err := os.ReadFile("../local/testdata/rsa2048.csr")
  199. if err != nil {
  200. t.Fatal("CSR loading error:", err)
  201. }
  202. _, err = s.Sign(signer.SignRequest{Hosts: hosts, Request: string(csr)})
  203. if err == nil {
  204. t.Fatal("Should return error")
  205. }
  206. remoteConfig.Signing.OverrideRemotes(remoteServer.URL)
  207. s.SetPolicy(remoteConfig.Signing)
  208. certBytes, err := s.Sign(signer.SignRequest{
  209. Hosts: hosts,
  210. Request: string(csr),
  211. Serial: big.NewInt(1),
  212. })
  213. if err != nil {
  214. t.Fatalf("Expected no error. Got %s.", err.Error())
  215. }
  216. _, err = helpers.ParseCertificatePEM(certBytes)
  217. if err != nil {
  218. t.Fatal("Fail to parse returned certificate:", err)
  219. }
  220. }
  221. // helper functions
  222. func newRemoteSigner(t *testing.T, policy *config.Signing) *Signer {
  223. s, err := NewSigner(policy)
  224. if err != nil {
  225. t.Fatal("fail to init remote signer:", err)
  226. }
  227. return s
  228. }
  229. func newTestSignHandler(t *testing.T) (h http.Handler) {
  230. h, err := newHandler(t, testCaFile, testCaKeyFile, "sign")
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. return
  235. }
  236. func newTestInfoHandler(t *testing.T) (h http.Handler) {
  237. h, err := newHandler(t, testCaFile, testCaKeyFile, "info")
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. return
  242. }
  243. func newTestServer(t *testing.T, path string, handler http.Handler, isTLS bool, certPool *x509.CertPool) *httptest.Server {
  244. mux := http.NewServeMux()
  245. mux.Handle(path, handler)
  246. ts := httptest.NewUnstartedServer(mux)
  247. if isTLS {
  248. cert, err := tls.LoadX509KeyPair(testServerFile, testServerKeyFile)
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. clientCertRequired := tls.NoClientCert
  253. if certPool != nil {
  254. clientCertRequired = tls.RequireAndVerifyClientCert
  255. }
  256. ts.TLS = &tls.Config{
  257. Certificates: []tls.Certificate{cert},
  258. ClientCAs: certPool,
  259. ClientAuth: clientCertRequired,
  260. }
  261. ts.TLS.BuildNameToCertificate()
  262. ts.StartTLS()
  263. } else {
  264. ts.Start()
  265. }
  266. return ts
  267. }
  268. func newTestSignServer(t *testing.T, isTLS bool, certPool *x509.CertPool) *httptest.Server {
  269. ts := newTestServer(t, "/api/v1/cfssl/sign", newTestSignHandler(t), isTLS, certPool)
  270. t.Log(ts.URL)
  271. return ts
  272. }
  273. func newTestInfoServer(t *testing.T, isTLS bool, certPool *x509.CertPool) *httptest.Server {
  274. ts := newTestServer(t, "/api/v1/cfssl/info", newTestInfoHandler(t), isTLS, certPool)
  275. t.Log(ts.URL)
  276. return ts
  277. }
  278. func closeTestServer(t *testing.T, ts *httptest.Server) {
  279. t.Log("Finalizing test server.")
  280. ts.Close()
  281. }
  282. // newHandler generates a new sign handler (or info handler) using the certificate
  283. // authority private key and certficate to sign certificates.
  284. func newHandler(t *testing.T, caFile, caKeyFile, op string) (http.Handler, error) {
  285. var expiry = 1 * time.Minute
  286. var CAConfig = &config.Config{
  287. Signing: &config.Signing{
  288. Profiles: map[string]*config.SigningProfile{
  289. "signature": {
  290. Usage: []string{"digital signature"},
  291. Expiry: expiry,
  292. },
  293. },
  294. Default: &config.SigningProfile{
  295. Usage: []string{"cert sign", "crl sign"},
  296. ExpiryString: "43800h",
  297. Expiry: expiry,
  298. CAConstraint: config.CAConstraint{IsCA: true},
  299. ClientProvidesSerialNumbers: true,
  300. },
  301. },
  302. }
  303. s, err := local.NewSignerFromFile(testCaFile, testCaKeyFile, CAConfig.Signing)
  304. if err != nil {
  305. t.Fatal(err)
  306. }
  307. if op == "sign" {
  308. return apisign.NewHandlerFromSigner(s)
  309. } else if op == "info" {
  310. return apiinfo.NewHandler(s)
  311. }
  312. t.Fatal("Bad op code")
  313. return nil, nil
  314. }