acmeClient_test.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. package ca
  2. import (
  3. "crypto/x509"
  4. "encoding/base64"
  5. "encoding/json"
  6. "encoding/pem"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/assert"
  14. "github.com/smallstep/certificates/acme"
  15. acmeAPI "github.com/smallstep/certificates/acme/api"
  16. "github.com/smallstep/certificates/api"
  17. "go.step.sm/crypto/jose"
  18. "go.step.sm/crypto/pemutil"
  19. )
  20. func TestNewACMEClient(t *testing.T) {
  21. type test struct {
  22. ops []ClientOption
  23. r1, r2 interface{}
  24. rc1, rc2 int
  25. err error
  26. }
  27. srv := httptest.NewServer(nil)
  28. defer srv.Close()
  29. dir := acmeAPI.Directory{
  30. NewNonce: srv.URL + "/foo",
  31. NewAccount: srv.URL + "/bar",
  32. NewOrder: srv.URL + "/baz",
  33. RevokeCert: srv.URL + "/zip",
  34. KeyChange: srv.URL + "/blorp",
  35. }
  36. acc := acme.Account{
  37. Contact: []string{"max", "mariano"},
  38. Status: "valid",
  39. OrdersURL: "orders-url",
  40. }
  41. tests := map[string]func(t *testing.T) test{
  42. "fail/client-option-error": func(t *testing.T) test {
  43. return test{
  44. ops: []ClientOption{
  45. func(o *clientOptions) error {
  46. return errors.New("force")
  47. },
  48. },
  49. err: errors.New("force"),
  50. }
  51. },
  52. "fail/get-directory": func(t *testing.T) test {
  53. return test{
  54. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  55. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  56. rc1: 400,
  57. err: errors.New("The request message was malformed"),
  58. }
  59. },
  60. "fail/bad-directory": func(t *testing.T) test {
  61. return test{
  62. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  63. r1: "foo",
  64. rc1: 200,
  65. err: errors.New("error reading http://127.0.0.1"),
  66. }
  67. },
  68. "fail/error-post-newAccount": func(t *testing.T) test {
  69. return test{
  70. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  71. r1: dir,
  72. rc1: 200,
  73. r2: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  74. rc2: 400,
  75. err: errors.New("Account does not exist"),
  76. }
  77. },
  78. "fail/error-bad-account": func(t *testing.T) test {
  79. return test{
  80. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  81. r1: dir,
  82. rc1: 200,
  83. r2: "foo",
  84. rc2: 200,
  85. err: errors.New("error reading http://127.0.0.1"),
  86. }
  87. },
  88. "ok": func(t *testing.T) test {
  89. return test{
  90. ops: []ClientOption{WithTransport(http.DefaultTransport)},
  91. r1: dir,
  92. rc1: 200,
  93. r2: acc,
  94. rc2: 200,
  95. }
  96. },
  97. }
  98. accLocation := "linkitylink"
  99. for name, run := range tests {
  100. t.Run(name, func(t *testing.T) {
  101. tc := run(t)
  102. i := 0
  103. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  104. switch {
  105. case i == 0:
  106. api.JSONStatus(w, tc.r1, tc.rc1)
  107. i++
  108. case i == 1:
  109. w.Header().Set("Replay-Nonce", "abc123")
  110. api.JSONStatus(w, []byte{}, 200)
  111. i++
  112. default:
  113. w.Header().Set("Location", accLocation)
  114. api.JSONStatus(w, tc.r2, tc.rc2)
  115. }
  116. })
  117. if client, err := NewACMEClient(srv.URL, []string{"max", "mariano"}, tc.ops...); err != nil {
  118. if assert.NotNil(t, tc.err) {
  119. assert.HasPrefix(t, err.Error(), tc.err.Error())
  120. }
  121. } else {
  122. if assert.Nil(t, tc.err) {
  123. assert.Equals(t, *client.dir, dir)
  124. assert.NotNil(t, client.Key)
  125. assert.NotNil(t, client.acc)
  126. assert.Equals(t, client.kid, accLocation)
  127. }
  128. }
  129. })
  130. }
  131. }
  132. func TestACMEClient_GetDirectory(t *testing.T) {
  133. c := &ACMEClient{
  134. dir: &acmeAPI.Directory{
  135. NewNonce: "/foo",
  136. NewAccount: "/bar",
  137. NewOrder: "/baz",
  138. RevokeCert: "/zip",
  139. KeyChange: "/blorp",
  140. },
  141. }
  142. dir, err := c.GetDirectory()
  143. assert.FatalError(t, err)
  144. assert.Equals(t, c.dir, dir)
  145. }
  146. func TestACMEClient_GetNonce(t *testing.T) {
  147. type test struct {
  148. r1 interface{}
  149. rc1 int
  150. err error
  151. }
  152. srv := httptest.NewServer(nil)
  153. defer srv.Close()
  154. dir := acmeAPI.Directory{
  155. NewNonce: srv.URL + "/foo",
  156. }
  157. // Retrieve transport from options.
  158. o := new(clientOptions)
  159. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  160. tr, err := o.getTransport(srv.URL)
  161. assert.FatalError(t, err)
  162. ac := &ACMEClient{
  163. client: &http.Client{
  164. Transport: tr,
  165. },
  166. dirLoc: srv.URL,
  167. dir: &dir,
  168. }
  169. tests := map[string]func(t *testing.T) test{
  170. "fail/GET-nonce": func(t *testing.T) test {
  171. return test{
  172. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  173. rc1: 400,
  174. err: errors.New("The request message was malformed"),
  175. }
  176. },
  177. "ok": func(t *testing.T) test {
  178. return test{
  179. r1: []byte{},
  180. rc1: 200,
  181. }
  182. },
  183. }
  184. expectedNonce := "abc123"
  185. for name, run := range tests {
  186. t.Run(name, func(t *testing.T) {
  187. tc := run(t)
  188. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  189. w.Header().Set("Replay-Nonce", expectedNonce)
  190. api.JSONStatus(w, tc.r1, tc.rc1)
  191. })
  192. if nonce, err := ac.GetNonce(); err != nil {
  193. if assert.NotNil(t, tc.err) {
  194. assert.HasPrefix(t, err.Error(), tc.err.Error())
  195. }
  196. } else {
  197. if assert.Nil(t, tc.err) {
  198. assert.Equals(t, expectedNonce, nonce)
  199. }
  200. }
  201. })
  202. }
  203. }
  204. func TestACMEClient_post(t *testing.T) {
  205. type test struct {
  206. payload []byte
  207. Key *jose.JSONWebKey
  208. ops []withHeaderOption
  209. r1, r2 interface{}
  210. rc1, rc2 int
  211. jwkInJWS bool
  212. client *ACMEClient
  213. err error
  214. }
  215. srv := httptest.NewServer(nil)
  216. defer srv.Close()
  217. dir := acmeAPI.Directory{
  218. NewNonce: srv.URL + "/foo",
  219. }
  220. // Retrieve transport from options.
  221. o := new(clientOptions)
  222. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  223. tr, err := o.getTransport(srv.URL)
  224. assert.FatalError(t, err)
  225. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  226. assert.FatalError(t, err)
  227. acc := acme.Account{
  228. Contact: []string{"max", "mariano"},
  229. Status: "valid",
  230. OrdersURL: "orders-url",
  231. }
  232. ac := &ACMEClient{
  233. client: &http.Client{
  234. Transport: tr,
  235. },
  236. dirLoc: srv.URL,
  237. dir: &dir,
  238. Key: jwk,
  239. kid: "foobar",
  240. }
  241. tests := map[string]func(t *testing.T) test{
  242. "fail/account-not-configured": func(t *testing.T) test {
  243. return test{
  244. client: &ACMEClient{},
  245. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  246. rc1: 400,
  247. err: errors.New("acme client not configured with account"),
  248. }
  249. },
  250. "fail/GET-nonce": func(t *testing.T) test {
  251. return test{
  252. client: ac,
  253. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  254. rc1: 400,
  255. err: errors.New("The request message was malformed"),
  256. }
  257. },
  258. "ok/jwk": func(t *testing.T) test {
  259. return test{
  260. client: ac,
  261. r1: []byte{},
  262. rc1: 200,
  263. r2: acc,
  264. rc2: 200,
  265. ops: []withHeaderOption{withJWK(ac)},
  266. jwkInJWS: true,
  267. }
  268. },
  269. "ok/kid": func(t *testing.T) test {
  270. return test{
  271. client: ac,
  272. r1: []byte{},
  273. rc1: 200,
  274. r2: acc,
  275. rc2: 200,
  276. ops: []withHeaderOption{withKid(ac)},
  277. }
  278. },
  279. }
  280. expectedNonce := "abc123"
  281. url := srv.URL + "/foo"
  282. for name, run := range tests {
  283. t.Run(name, func(t *testing.T) {
  284. tc := run(t)
  285. i := 0
  286. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  287. w.Header().Set("Replay-Nonce", expectedNonce)
  288. if i == 0 {
  289. api.JSONStatus(w, tc.r1, tc.rc1)
  290. i++
  291. return
  292. }
  293. // validate jws request protected headers and body
  294. body, err := ioutil.ReadAll(req.Body)
  295. assert.FatalError(t, err)
  296. jws, err := jose.ParseJWS(string(body))
  297. assert.FatalError(t, err)
  298. hdr := jws.Signatures[0].Protected
  299. assert.Equals(t, hdr.Nonce, expectedNonce)
  300. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  301. assert.Fatal(t, ok)
  302. assert.Equals(t, jwsURL, url)
  303. if tc.jwkInJWS {
  304. assert.Equals(t, hdr.JSONWebKey.KeyID, ac.Key.KeyID)
  305. } else {
  306. assert.Equals(t, hdr.KeyID, ac.kid)
  307. }
  308. api.JSONStatus(w, tc.r2, tc.rc2)
  309. })
  310. if resp, err := tc.client.post(tc.payload, url, tc.ops...); err != nil {
  311. if assert.NotNil(t, tc.err) {
  312. assert.HasPrefix(t, err.Error(), tc.err.Error())
  313. }
  314. } else {
  315. if assert.Nil(t, tc.err) {
  316. var res acme.Account
  317. assert.FatalError(t, readJSON(resp.Body, &res))
  318. assert.Equals(t, res, acc)
  319. }
  320. }
  321. })
  322. }
  323. }
  324. func TestACMEClient_NewOrder(t *testing.T) {
  325. type test struct {
  326. ops []withHeaderOption
  327. r1, r2 interface{}
  328. rc1, rc2 int
  329. err error
  330. }
  331. srv := httptest.NewServer(nil)
  332. defer srv.Close()
  333. dir := acmeAPI.Directory{
  334. NewNonce: srv.URL + "/foo",
  335. NewOrder: srv.URL + "/bar",
  336. }
  337. // Retrieve transport from options.
  338. o := new(clientOptions)
  339. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  340. tr, err := o.getTransport(srv.URL)
  341. assert.FatalError(t, err)
  342. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  343. assert.FatalError(t, err)
  344. now := time.Now().UTC().Round(time.Second)
  345. nor := acmeAPI.NewOrderRequest{
  346. Identifiers: []acme.Identifier{
  347. {Type: "dns", Value: "example.com"},
  348. {Type: "dns", Value: "acme.example.com"},
  349. },
  350. NotBefore: now,
  351. NotAfter: now.Add(time.Minute),
  352. }
  353. norb, err := json.Marshal(nor)
  354. assert.FatalError(t, err)
  355. ord := acme.Order{
  356. Status: "valid",
  357. ExpiresAt: now, // "soon"
  358. FinalizeURL: "finalize-url",
  359. }
  360. ac := &ACMEClient{
  361. client: &http.Client{
  362. Transport: tr,
  363. },
  364. dirLoc: srv.URL,
  365. dir: &dir,
  366. Key: jwk,
  367. kid: "foobar",
  368. }
  369. tests := map[string]func(t *testing.T) test{
  370. "fail/client-post": func(t *testing.T) test {
  371. return test{
  372. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  373. rc1: 400,
  374. err: errors.New("The request message was malformed"),
  375. }
  376. },
  377. "fail/newOrder-error": func(t *testing.T) test {
  378. return test{
  379. r1: []byte{},
  380. rc1: 200,
  381. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  382. rc2: 400,
  383. ops: []withHeaderOption{withKid(ac)},
  384. err: errors.New("The request message was malformed"),
  385. }
  386. },
  387. "fail/bad-order": func(t *testing.T) test {
  388. return test{
  389. r1: []byte{},
  390. rc1: 200,
  391. r2: "foo",
  392. rc2: 200,
  393. ops: []withHeaderOption{withKid(ac)},
  394. err: errors.New("error reading http://127.0.0.1"),
  395. }
  396. },
  397. "ok": func(t *testing.T) test {
  398. return test{
  399. r1: []byte{},
  400. rc1: 200,
  401. r2: ord,
  402. rc2: 200,
  403. ops: []withHeaderOption{withKid(ac)},
  404. }
  405. },
  406. }
  407. expectedNonce := "abc123"
  408. for name, run := range tests {
  409. t.Run(name, func(t *testing.T) {
  410. tc := run(t)
  411. i := 0
  412. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  413. w.Header().Set("Replay-Nonce", expectedNonce)
  414. if i == 0 {
  415. api.JSONStatus(w, tc.r1, tc.rc1)
  416. i++
  417. return
  418. }
  419. // validate jws request protected headers and body
  420. body, err := ioutil.ReadAll(req.Body)
  421. assert.FatalError(t, err)
  422. jws, err := jose.ParseJWS(string(body))
  423. assert.FatalError(t, err)
  424. hdr := jws.Signatures[0].Protected
  425. assert.Equals(t, hdr.Nonce, expectedNonce)
  426. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  427. assert.Fatal(t, ok)
  428. assert.Equals(t, jwsURL, dir.NewOrder)
  429. assert.Equals(t, hdr.KeyID, ac.kid)
  430. payload, err := jws.Verify(ac.Key.Public())
  431. assert.FatalError(t, err)
  432. assert.Equals(t, payload, norb)
  433. api.JSONStatus(w, tc.r2, tc.rc2)
  434. })
  435. if res, err := ac.NewOrder(norb); err != nil {
  436. if assert.NotNil(t, tc.err) {
  437. assert.HasPrefix(t, err.Error(), tc.err.Error())
  438. }
  439. } else {
  440. if assert.Nil(t, tc.err) {
  441. assert.Equals(t, *res, ord)
  442. }
  443. }
  444. })
  445. }
  446. }
  447. func TestACMEClient_GetOrder(t *testing.T) {
  448. type test struct {
  449. r1, r2 interface{}
  450. rc1, rc2 int
  451. err error
  452. }
  453. srv := httptest.NewServer(nil)
  454. defer srv.Close()
  455. dir := acmeAPI.Directory{
  456. NewNonce: srv.URL + "/foo",
  457. }
  458. // Retrieve transport from options.
  459. o := new(clientOptions)
  460. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  461. tr, err := o.getTransport(srv.URL)
  462. assert.FatalError(t, err)
  463. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  464. assert.FatalError(t, err)
  465. ord := acme.Order{
  466. Status: "valid",
  467. ExpiresAt: time.Now().UTC().Round(time.Second), // "soon"
  468. FinalizeURL: "finalize-url",
  469. }
  470. ac := &ACMEClient{
  471. client: &http.Client{
  472. Transport: tr,
  473. },
  474. dirLoc: srv.URL,
  475. dir: &dir,
  476. Key: jwk,
  477. kid: "foobar",
  478. }
  479. tests := map[string]func(t *testing.T) test{
  480. "fail/client-post": func(t *testing.T) test {
  481. return test{
  482. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  483. rc1: 400,
  484. err: errors.New("The request message was malformed"),
  485. }
  486. },
  487. "fail/getOrder-error": func(t *testing.T) test {
  488. return test{
  489. r1: []byte{},
  490. rc1: 200,
  491. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  492. rc2: 400,
  493. err: errors.New("The request message was malformed"),
  494. }
  495. },
  496. "fail/bad-order": func(t *testing.T) test {
  497. return test{
  498. r1: []byte{},
  499. rc1: 200,
  500. r2: "foo",
  501. rc2: 200,
  502. err: errors.New("error reading http://127.0.0.1"),
  503. }
  504. },
  505. "ok": func(t *testing.T) test {
  506. return test{
  507. r1: []byte{},
  508. rc1: 200,
  509. r2: ord,
  510. rc2: 200,
  511. }
  512. },
  513. }
  514. expectedNonce := "abc123"
  515. url := srv.URL + "/hullaballoo"
  516. for name, run := range tests {
  517. t.Run(name, func(t *testing.T) {
  518. tc := run(t)
  519. i := 0
  520. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  521. w.Header().Set("Replay-Nonce", expectedNonce)
  522. if i == 0 {
  523. api.JSONStatus(w, tc.r1, tc.rc1)
  524. i++
  525. return
  526. }
  527. // validate jws request protected headers and body
  528. body, err := ioutil.ReadAll(req.Body)
  529. assert.FatalError(t, err)
  530. jws, err := jose.ParseJWS(string(body))
  531. assert.FatalError(t, err)
  532. hdr := jws.Signatures[0].Protected
  533. assert.Equals(t, hdr.Nonce, expectedNonce)
  534. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  535. assert.Fatal(t, ok)
  536. assert.Equals(t, jwsURL, url)
  537. assert.Equals(t, hdr.KeyID, ac.kid)
  538. payload, err := jws.Verify(ac.Key.Public())
  539. assert.FatalError(t, err)
  540. assert.Equals(t, len(payload), 0)
  541. api.JSONStatus(w, tc.r2, tc.rc2)
  542. })
  543. if res, err := ac.GetOrder(url); err != nil {
  544. if assert.NotNil(t, tc.err) {
  545. assert.HasPrefix(t, err.Error(), tc.err.Error())
  546. }
  547. } else {
  548. if assert.Nil(t, tc.err) {
  549. assert.Equals(t, *res, ord)
  550. }
  551. }
  552. })
  553. }
  554. }
  555. func TestACMEClient_GetAuthz(t *testing.T) {
  556. type test struct {
  557. r1, r2 interface{}
  558. rc1, rc2 int
  559. err error
  560. }
  561. srv := httptest.NewServer(nil)
  562. defer srv.Close()
  563. dir := acmeAPI.Directory{
  564. NewNonce: srv.URL + "/foo",
  565. }
  566. // Retrieve transport from options.
  567. o := new(clientOptions)
  568. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  569. tr, err := o.getTransport(srv.URL)
  570. assert.FatalError(t, err)
  571. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  572. assert.FatalError(t, err)
  573. az := acme.Authorization{
  574. Status: "valid",
  575. ExpiresAt: time.Now().UTC().Round(time.Second),
  576. Identifier: acme.Identifier{Type: "dns", Value: "example.com"},
  577. }
  578. ac := &ACMEClient{
  579. client: &http.Client{
  580. Transport: tr,
  581. },
  582. dirLoc: srv.URL,
  583. dir: &dir,
  584. Key: jwk,
  585. kid: "foobar",
  586. }
  587. tests := map[string]func(t *testing.T) test{
  588. "fail/client-post": func(t *testing.T) test {
  589. return test{
  590. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  591. rc1: 400,
  592. err: errors.New("The request message was malformed"),
  593. }
  594. },
  595. "fail/getChallenge-error": func(t *testing.T) test {
  596. return test{
  597. r1: []byte{},
  598. rc1: 200,
  599. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  600. rc2: 400,
  601. err: errors.New("The request message was malformed"),
  602. }
  603. },
  604. "fail/bad-challenge": func(t *testing.T) test {
  605. return test{
  606. r1: []byte{},
  607. rc1: 200,
  608. r2: "foo",
  609. rc2: 200,
  610. err: errors.New("error reading http://127.0.0.1"),
  611. }
  612. },
  613. "ok": func(t *testing.T) test {
  614. return test{
  615. r1: []byte{},
  616. rc1: 200,
  617. r2: az,
  618. rc2: 200,
  619. }
  620. },
  621. }
  622. expectedNonce := "abc123"
  623. url := srv.URL + "/hullaballoo"
  624. for name, run := range tests {
  625. t.Run(name, func(t *testing.T) {
  626. tc := run(t)
  627. i := 0
  628. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  629. w.Header().Set("Replay-Nonce", expectedNonce)
  630. if i == 0 {
  631. api.JSONStatus(w, tc.r1, tc.rc1)
  632. i++
  633. return
  634. }
  635. // validate jws request protected headers and body
  636. body, err := ioutil.ReadAll(req.Body)
  637. assert.FatalError(t, err)
  638. jws, err := jose.ParseJWS(string(body))
  639. assert.FatalError(t, err)
  640. hdr := jws.Signatures[0].Protected
  641. assert.Equals(t, hdr.Nonce, expectedNonce)
  642. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  643. assert.Fatal(t, ok)
  644. assert.Equals(t, jwsURL, url)
  645. assert.Equals(t, hdr.KeyID, ac.kid)
  646. payload, err := jws.Verify(ac.Key.Public())
  647. assert.FatalError(t, err)
  648. assert.Equals(t, len(payload), 0)
  649. api.JSONStatus(w, tc.r2, tc.rc2)
  650. })
  651. if res, err := ac.GetAuthz(url); err != nil {
  652. if assert.NotNil(t, tc.err) {
  653. assert.HasPrefix(t, err.Error(), tc.err.Error())
  654. }
  655. } else {
  656. if assert.Nil(t, tc.err) {
  657. assert.Equals(t, *res, az)
  658. }
  659. }
  660. })
  661. }
  662. }
  663. func TestACMEClient_GetChallenge(t *testing.T) {
  664. type test struct {
  665. r1, r2 interface{}
  666. rc1, rc2 int
  667. err error
  668. }
  669. srv := httptest.NewServer(nil)
  670. defer srv.Close()
  671. dir := acmeAPI.Directory{
  672. NewNonce: srv.URL + "/foo",
  673. }
  674. // Retrieve transport from options.
  675. o := new(clientOptions)
  676. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  677. tr, err := o.getTransport(srv.URL)
  678. assert.FatalError(t, err)
  679. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  680. assert.FatalError(t, err)
  681. ch := acme.Challenge{
  682. Type: "http-01",
  683. Status: "valid",
  684. Token: "foo",
  685. }
  686. ac := &ACMEClient{
  687. client: &http.Client{
  688. Transport: tr,
  689. },
  690. dirLoc: srv.URL,
  691. dir: &dir,
  692. Key: jwk,
  693. kid: "foobar",
  694. }
  695. tests := map[string]func(t *testing.T) test{
  696. "fail/client-post": func(t *testing.T) test {
  697. return test{
  698. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  699. rc1: 400,
  700. err: errors.New("The request message was malformed"),
  701. }
  702. },
  703. "fail/getChallenge-error": func(t *testing.T) test {
  704. return test{
  705. r1: []byte{},
  706. rc1: 200,
  707. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  708. rc2: 400,
  709. err: errors.New("The request message was malformed"),
  710. }
  711. },
  712. "fail/bad-challenge": func(t *testing.T) test {
  713. return test{
  714. r1: []byte{},
  715. rc1: 200,
  716. r2: "foo",
  717. rc2: 200,
  718. err: errors.New("error reading http://127.0.0.1"),
  719. }
  720. },
  721. "ok": func(t *testing.T) test {
  722. return test{
  723. r1: []byte{},
  724. rc1: 200,
  725. r2: ch,
  726. rc2: 200,
  727. }
  728. },
  729. }
  730. expectedNonce := "abc123"
  731. url := srv.URL + "/hullaballoo"
  732. for name, run := range tests {
  733. t.Run(name, func(t *testing.T) {
  734. tc := run(t)
  735. i := 0
  736. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  737. w.Header().Set("Replay-Nonce", expectedNonce)
  738. if i == 0 {
  739. api.JSONStatus(w, tc.r1, tc.rc1)
  740. i++
  741. return
  742. }
  743. // validate jws request protected headers and body
  744. body, err := ioutil.ReadAll(req.Body)
  745. assert.FatalError(t, err)
  746. jws, err := jose.ParseJWS(string(body))
  747. assert.FatalError(t, err)
  748. hdr := jws.Signatures[0].Protected
  749. assert.Equals(t, hdr.Nonce, expectedNonce)
  750. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  751. assert.Fatal(t, ok)
  752. assert.Equals(t, jwsURL, url)
  753. assert.Equals(t, hdr.KeyID, ac.kid)
  754. payload, err := jws.Verify(ac.Key.Public())
  755. assert.FatalError(t, err)
  756. assert.Equals(t, len(payload), 0)
  757. api.JSONStatus(w, tc.r2, tc.rc2)
  758. })
  759. if res, err := ac.GetChallenge(url); err != nil {
  760. if assert.NotNil(t, tc.err) {
  761. assert.HasPrefix(t, err.Error(), tc.err.Error())
  762. }
  763. } else {
  764. if assert.Nil(t, tc.err) {
  765. assert.Equals(t, *res, ch)
  766. }
  767. }
  768. })
  769. }
  770. }
  771. func TestACMEClient_ValidateChallenge(t *testing.T) {
  772. type test struct {
  773. r1, r2 interface{}
  774. rc1, rc2 int
  775. err error
  776. }
  777. srv := httptest.NewServer(nil)
  778. defer srv.Close()
  779. dir := acmeAPI.Directory{
  780. NewNonce: srv.URL + "/foo",
  781. }
  782. // Retrieve transport from options.
  783. o := new(clientOptions)
  784. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  785. tr, err := o.getTransport(srv.URL)
  786. assert.FatalError(t, err)
  787. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  788. assert.FatalError(t, err)
  789. ch := acme.Challenge{
  790. Type: "http-01",
  791. Status: "valid",
  792. Token: "foo",
  793. }
  794. ac := &ACMEClient{
  795. client: &http.Client{
  796. Transport: tr,
  797. },
  798. dirLoc: srv.URL,
  799. dir: &dir,
  800. Key: jwk,
  801. kid: "foobar",
  802. }
  803. tests := map[string]func(t *testing.T) test{
  804. "fail/client-post": func(t *testing.T) test {
  805. return test{
  806. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  807. rc1: 400,
  808. err: errors.New("The request message was malformed"),
  809. }
  810. },
  811. "fail/getChallenge-error": func(t *testing.T) test {
  812. return test{
  813. r1: []byte{},
  814. rc1: 200,
  815. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  816. rc2: 400,
  817. err: errors.New("The request message was malformed"),
  818. }
  819. },
  820. "fail/bad-challenge": func(t *testing.T) test {
  821. return test{
  822. r1: []byte{},
  823. rc1: 200,
  824. r2: "foo",
  825. rc2: 200,
  826. err: errors.New("error reading http://127.0.0.1"),
  827. }
  828. },
  829. "ok": func(t *testing.T) test {
  830. return test{
  831. r1: []byte{},
  832. rc1: 200,
  833. r2: ch,
  834. rc2: 200,
  835. }
  836. },
  837. }
  838. expectedNonce := "abc123"
  839. url := srv.URL + "/hullaballoo"
  840. for name, run := range tests {
  841. t.Run(name, func(t *testing.T) {
  842. tc := run(t)
  843. i := 0
  844. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  845. w.Header().Set("Replay-Nonce", expectedNonce)
  846. if i == 0 {
  847. api.JSONStatus(w, tc.r1, tc.rc1)
  848. i++
  849. return
  850. }
  851. // validate jws request protected headers and body
  852. body, err := ioutil.ReadAll(req.Body)
  853. assert.FatalError(t, err)
  854. jws, err := jose.ParseJWS(string(body))
  855. assert.FatalError(t, err)
  856. hdr := jws.Signatures[0].Protected
  857. assert.Equals(t, hdr.Nonce, expectedNonce)
  858. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  859. assert.Fatal(t, ok)
  860. assert.Equals(t, jwsURL, url)
  861. assert.Equals(t, hdr.KeyID, ac.kid)
  862. payload, err := jws.Verify(ac.Key.Public())
  863. assert.FatalError(t, err)
  864. assert.Equals(t, payload, []byte("{}"))
  865. api.JSONStatus(w, tc.r2, tc.rc2)
  866. })
  867. if err := ac.ValidateChallenge(url); err != nil {
  868. if assert.NotNil(t, tc.err) {
  869. assert.HasPrefix(t, err.Error(), tc.err.Error())
  870. }
  871. }
  872. })
  873. }
  874. }
  875. func TestACMEClient_FinalizeOrder(t *testing.T) {
  876. type test struct {
  877. r1, r2 interface{}
  878. rc1, rc2 int
  879. err error
  880. }
  881. srv := httptest.NewServer(nil)
  882. defer srv.Close()
  883. dir := acmeAPI.Directory{
  884. NewNonce: srv.URL + "/foo",
  885. }
  886. // Retrieve transport from options.
  887. o := new(clientOptions)
  888. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  889. tr, err := o.getTransport(srv.URL)
  890. assert.FatalError(t, err)
  891. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  892. assert.FatalError(t, err)
  893. ord := acme.Order{
  894. Status: "valid",
  895. ExpiresAt: time.Now(), // "soon"
  896. FinalizeURL: "finalize-url",
  897. CertificateURL: "cert-url",
  898. }
  899. _csr, err := pemutil.Read("../authority/testdata/certs/foo.csr")
  900. assert.FatalError(t, err)
  901. csr, ok := _csr.(*x509.CertificateRequest)
  902. assert.Fatal(t, ok)
  903. fr := acmeAPI.FinalizeRequest{CSR: base64.RawURLEncoding.EncodeToString(csr.Raw)}
  904. frb, err := json.Marshal(fr)
  905. assert.FatalError(t, err)
  906. ac := &ACMEClient{
  907. client: &http.Client{
  908. Transport: tr,
  909. },
  910. dirLoc: srv.URL,
  911. dir: &dir,
  912. Key: jwk,
  913. kid: "foobar",
  914. }
  915. tests := map[string]func(t *testing.T) test{
  916. "fail/client-post": func(t *testing.T) test {
  917. return test{
  918. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  919. rc1: 400,
  920. err: errors.New("The request message was malformed"),
  921. }
  922. },
  923. "fail/finalizeOrder-error": func(t *testing.T) test {
  924. return test{
  925. r1: []byte{},
  926. rc1: 200,
  927. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  928. rc2: 400,
  929. err: errors.New("The request message was malformed"),
  930. }
  931. },
  932. "fail/bad-order": func(t *testing.T) test {
  933. return test{
  934. r1: []byte{},
  935. rc1: 200,
  936. r2: "foo",
  937. rc2: 200,
  938. err: errors.New("error reading http://127.0.0.1"),
  939. }
  940. },
  941. "ok": func(t *testing.T) test {
  942. return test{
  943. r1: []byte{},
  944. rc1: 200,
  945. r2: ord,
  946. rc2: 200,
  947. }
  948. },
  949. }
  950. expectedNonce := "abc123"
  951. url := srv.URL + "/hullaballoo"
  952. for name, run := range tests {
  953. t.Run(name, func(t *testing.T) {
  954. tc := run(t)
  955. i := 0
  956. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  957. w.Header().Set("Replay-Nonce", expectedNonce)
  958. if i == 0 {
  959. api.JSONStatus(w, tc.r1, tc.rc1)
  960. i++
  961. return
  962. }
  963. // validate jws request protected headers and body
  964. body, err := ioutil.ReadAll(req.Body)
  965. assert.FatalError(t, err)
  966. jws, err := jose.ParseJWS(string(body))
  967. assert.FatalError(t, err)
  968. hdr := jws.Signatures[0].Protected
  969. assert.Equals(t, hdr.Nonce, expectedNonce)
  970. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  971. assert.Fatal(t, ok)
  972. assert.Equals(t, jwsURL, url)
  973. assert.Equals(t, hdr.KeyID, ac.kid)
  974. payload, err := jws.Verify(ac.Key.Public())
  975. assert.FatalError(t, err)
  976. assert.Equals(t, payload, frb)
  977. api.JSONStatus(w, tc.r2, tc.rc2)
  978. })
  979. if err := ac.FinalizeOrder(url, csr); err != nil {
  980. if assert.NotNil(t, tc.err) {
  981. assert.HasPrefix(t, err.Error(), tc.err.Error())
  982. }
  983. }
  984. })
  985. }
  986. }
  987. func TestACMEClient_GetAccountOrders(t *testing.T) {
  988. type test struct {
  989. r1, r2 interface{}
  990. rc1, rc2 int
  991. err error
  992. client *ACMEClient
  993. }
  994. srv := httptest.NewServer(nil)
  995. defer srv.Close()
  996. dir := acmeAPI.Directory{
  997. NewNonce: srv.URL + "/foo",
  998. }
  999. // Retrieve transport from options.
  1000. o := new(clientOptions)
  1001. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  1002. tr, err := o.getTransport(srv.URL)
  1003. assert.FatalError(t, err)
  1004. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  1005. assert.FatalError(t, err)
  1006. orders := []string{"foo", "bar", "baz"}
  1007. ac := &ACMEClient{
  1008. client: &http.Client{
  1009. Transport: tr,
  1010. },
  1011. dirLoc: srv.URL,
  1012. dir: &dir,
  1013. Key: jwk,
  1014. kid: "foobar",
  1015. acc: &acme.Account{
  1016. Contact: []string{"max", "mariano"},
  1017. Status: "valid",
  1018. OrdersURL: srv.URL + "/orders-url",
  1019. },
  1020. }
  1021. tests := map[string]func(t *testing.T) test{
  1022. "fail/account-not-configured": func(t *testing.T) test {
  1023. return test{
  1024. client: &ACMEClient{},
  1025. err: errors.New("acme client not configured with account"),
  1026. }
  1027. },
  1028. "fail/client-post": func(t *testing.T) test {
  1029. return test{
  1030. client: ac,
  1031. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1032. rc1: 400,
  1033. err: errors.New("The request message was malformed"),
  1034. }
  1035. },
  1036. "fail/getAccountOrders-error": func(t *testing.T) test {
  1037. return test{
  1038. client: ac,
  1039. r1: []byte{},
  1040. rc1: 200,
  1041. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1042. rc2: 400,
  1043. err: errors.New("The request message was malformed"),
  1044. }
  1045. },
  1046. "fail/bad-accountOrders": func(t *testing.T) test {
  1047. return test{
  1048. client: ac,
  1049. r1: []byte{},
  1050. rc1: 200,
  1051. r2: "foo",
  1052. rc2: 200,
  1053. err: errors.New("error reading http://127.0.0.1"),
  1054. }
  1055. },
  1056. "ok": func(t *testing.T) test {
  1057. return test{
  1058. client: ac,
  1059. r1: []byte{},
  1060. rc1: 200,
  1061. r2: orders,
  1062. rc2: 200,
  1063. }
  1064. },
  1065. }
  1066. expectedNonce := "abc123"
  1067. for name, run := range tests {
  1068. t.Run(name, func(t *testing.T) {
  1069. tc := run(t)
  1070. i := 0
  1071. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  1072. w.Header().Set("Replay-Nonce", expectedNonce)
  1073. if i == 0 {
  1074. api.JSONStatus(w, tc.r1, tc.rc1)
  1075. i++
  1076. return
  1077. }
  1078. // validate jws request protected headers and body
  1079. body, err := ioutil.ReadAll(req.Body)
  1080. assert.FatalError(t, err)
  1081. jws, err := jose.ParseJWS(string(body))
  1082. assert.FatalError(t, err)
  1083. hdr := jws.Signatures[0].Protected
  1084. assert.Equals(t, hdr.Nonce, expectedNonce)
  1085. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  1086. assert.Fatal(t, ok)
  1087. assert.Equals(t, jwsURL, ac.acc.OrdersURL)
  1088. assert.Equals(t, hdr.KeyID, ac.kid)
  1089. payload, err := jws.Verify(ac.Key.Public())
  1090. assert.FatalError(t, err)
  1091. assert.Equals(t, len(payload), 0)
  1092. api.JSONStatus(w, tc.r2, tc.rc2)
  1093. })
  1094. if res, err := tc.client.GetAccountOrders(); err != nil {
  1095. if assert.NotNil(t, tc.err) {
  1096. assert.HasPrefix(t, err.Error(), tc.err.Error())
  1097. }
  1098. } else {
  1099. if assert.Nil(t, tc.err) {
  1100. assert.Equals(t, res, orders)
  1101. }
  1102. }
  1103. })
  1104. }
  1105. }
  1106. func TestACMEClient_GetCertificate(t *testing.T) {
  1107. type test struct {
  1108. r1, r2 interface{}
  1109. certBytes []byte
  1110. rc1, rc2 int
  1111. err error
  1112. }
  1113. srv := httptest.NewServer(nil)
  1114. defer srv.Close()
  1115. dir := acmeAPI.Directory{
  1116. NewNonce: srv.URL + "/foo",
  1117. }
  1118. // Retrieve transport from options.
  1119. o := new(clientOptions)
  1120. assert.FatalError(t, o.apply([]ClientOption{WithTransport(http.DefaultTransport)}))
  1121. tr, err := o.getTransport(srv.URL)
  1122. assert.FatalError(t, err)
  1123. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  1124. assert.FatalError(t, err)
  1125. leaf, err := pemutil.ReadCertificate("../authority/testdata/certs/foo.crt")
  1126. assert.FatalError(t, err)
  1127. leafb := pem.EncodeToMemory(&pem.Block{
  1128. Type: "Certificate",
  1129. Bytes: leaf.Raw,
  1130. })
  1131. certBytes := append(leafb, leafb...)
  1132. certBytes = append(certBytes, leafb...)
  1133. ac := &ACMEClient{
  1134. client: &http.Client{
  1135. Transport: tr,
  1136. },
  1137. dirLoc: srv.URL,
  1138. dir: &dir,
  1139. Key: jwk,
  1140. kid: "foobar",
  1141. acc: &acme.Account{
  1142. Contact: []string{"max", "mariano"},
  1143. Status: "valid",
  1144. OrdersURL: srv.URL + "/orders-url",
  1145. },
  1146. }
  1147. tests := map[string]func(t *testing.T) test{
  1148. "fail/client-post": func(t *testing.T) test {
  1149. return test{
  1150. r1: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1151. rc1: 400,
  1152. err: errors.New("The request message was malformed"),
  1153. }
  1154. },
  1155. "fail/getAccountOrders-error": func(t *testing.T) test {
  1156. return test{
  1157. r1: []byte{},
  1158. rc1: 200,
  1159. r2: acme.NewError(acme.ErrorMalformedType, "malformed request"),
  1160. rc2: 400,
  1161. err: errors.New("The request message was malformed"),
  1162. }
  1163. },
  1164. "fail/bad-certificate": func(t *testing.T) test {
  1165. return test{
  1166. r1: []byte{},
  1167. rc1: 200,
  1168. r2: "foo",
  1169. rc2: 200,
  1170. err: errors.New("failed to parse any certificates from response"),
  1171. }
  1172. },
  1173. "ok": func(t *testing.T) test {
  1174. return test{
  1175. r1: []byte{},
  1176. rc1: 200,
  1177. certBytes: certBytes,
  1178. }
  1179. },
  1180. }
  1181. expectedNonce := "abc123"
  1182. url := srv.URL + "/cert/foo"
  1183. for name, run := range tests {
  1184. t.Run(name, func(t *testing.T) {
  1185. tc := run(t)
  1186. i := 0
  1187. srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  1188. w.Header().Set("Replay-Nonce", expectedNonce)
  1189. if i == 0 {
  1190. api.JSONStatus(w, tc.r1, tc.rc1)
  1191. i++
  1192. return
  1193. }
  1194. // validate jws request protected headers and body
  1195. body, err := ioutil.ReadAll(req.Body)
  1196. assert.FatalError(t, err)
  1197. jws, err := jose.ParseJWS(string(body))
  1198. assert.FatalError(t, err)
  1199. hdr := jws.Signatures[0].Protected
  1200. assert.Equals(t, hdr.Nonce, expectedNonce)
  1201. jwsURL, ok := hdr.ExtraHeaders["url"].(string)
  1202. assert.Fatal(t, ok)
  1203. assert.Equals(t, jwsURL, url)
  1204. assert.Equals(t, hdr.KeyID, ac.kid)
  1205. payload, err := jws.Verify(ac.Key.Public())
  1206. assert.FatalError(t, err)
  1207. assert.Equals(t, len(payload), 0)
  1208. if tc.certBytes != nil {
  1209. w.Write(tc.certBytes)
  1210. } else {
  1211. api.JSONStatus(w, tc.r2, tc.rc2)
  1212. }
  1213. })
  1214. if crt, chain, err := ac.GetCertificate(url); err != nil {
  1215. if assert.NotNil(t, tc.err) {
  1216. assert.HasPrefix(t, err.Error(), tc.err.Error())
  1217. }
  1218. } else {
  1219. if assert.Nil(t, tc.err) {
  1220. assert.Equals(t, crt, leaf)
  1221. assert.Equals(t, chain, []*x509.Certificate{leaf, leaf})
  1222. }
  1223. }
  1224. })
  1225. }
  1226. }