acme_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package acme
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "crypto/tls"
  10. "crypto/x509"
  11. "crypto/x509/pkix"
  12. "encoding/base64"
  13. "encoding/json"
  14. "fmt"
  15. "io/ioutil"
  16. "math/big"
  17. "net/http"
  18. "net/http/httptest"
  19. "reflect"
  20. "sort"
  21. "strings"
  22. "testing"
  23. "time"
  24. "golang.org/x/net/context"
  25. )
  26. // Decodes a JWS-encoded request and unmarshals the decoded JSON into a provided
  27. // interface.
  28. func decodeJWSRequest(t *testing.T, v interface{}, r *http.Request) {
  29. // Decode request
  30. var req struct{ Payload string }
  31. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  32. t.Fatal(err)
  33. }
  34. payload, err := base64.RawURLEncoding.DecodeString(req.Payload)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. err = json.Unmarshal(payload, v)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. }
  43. func TestDiscover(t *testing.T) {
  44. const (
  45. reg = "https://example.com/acme/new-reg"
  46. authz = "https://example.com/acme/new-authz"
  47. cert = "https://example.com/acme/new-cert"
  48. revoke = "https://example.com/acme/revoke-cert"
  49. )
  50. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. w.Header().Set("content-type", "application/json")
  52. fmt.Fprintf(w, `{
  53. "new-reg": %q,
  54. "new-authz": %q,
  55. "new-cert": %q,
  56. "revoke-cert": %q
  57. }`, reg, authz, cert, revoke)
  58. }))
  59. defer ts.Close()
  60. c := Client{DirectoryURL: ts.URL}
  61. dir, err := c.Discover(context.Background())
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if dir.RegURL != reg {
  66. t.Errorf("dir.RegURL = %q; want %q", dir.RegURL, reg)
  67. }
  68. if dir.AuthzURL != authz {
  69. t.Errorf("dir.AuthzURL = %q; want %q", dir.AuthzURL, authz)
  70. }
  71. if dir.CertURL != cert {
  72. t.Errorf("dir.CertURL = %q; want %q", dir.CertURL, cert)
  73. }
  74. if dir.RevokeURL != revoke {
  75. t.Errorf("dir.RevokeURL = %q; want %q", dir.RevokeURL, revoke)
  76. }
  77. }
  78. func TestRegister(t *testing.T) {
  79. contacts := []string{"mailto:admin@example.com"}
  80. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  81. if r.Method == "HEAD" {
  82. w.Header().Set("replay-nonce", "test-nonce")
  83. return
  84. }
  85. if r.Method != "POST" {
  86. t.Errorf("r.Method = %q; want POST", r.Method)
  87. }
  88. var j struct {
  89. Resource string
  90. Contact []string
  91. Agreement string
  92. }
  93. decodeJWSRequest(t, &j, r)
  94. // Test request
  95. if j.Resource != "new-reg" {
  96. t.Errorf("j.Resource = %q; want new-reg", j.Resource)
  97. }
  98. if !reflect.DeepEqual(j.Contact, contacts) {
  99. t.Errorf("j.Contact = %v; want %v", j.Contact, contacts)
  100. }
  101. w.Header().Set("Location", "https://ca.tld/acme/reg/1")
  102. w.Header().Set("Link", `<https://ca.tld/acme/new-authz>;rel="next"`)
  103. w.Header().Add("Link", `<https://ca.tld/acme/recover-reg>;rel="recover"`)
  104. w.Header().Add("Link", `<https://ca.tld/acme/terms>;rel="terms-of-service"`)
  105. w.WriteHeader(http.StatusCreated)
  106. b, _ := json.Marshal(contacts)
  107. fmt.Fprintf(w, `{"contact": %s}`, b)
  108. }))
  109. defer ts.Close()
  110. prompt := func(url string) bool {
  111. const terms = "https://ca.tld/acme/terms"
  112. if url != terms {
  113. t.Errorf("prompt url = %q; want %q", url, terms)
  114. }
  115. return false
  116. }
  117. c := Client{Key: testKeyEC, dir: &Directory{RegURL: ts.URL}}
  118. a := &Account{Contact: contacts}
  119. var err error
  120. if a, err = c.Register(context.Background(), a, prompt); err != nil {
  121. t.Fatal(err)
  122. }
  123. if a.URI != "https://ca.tld/acme/reg/1" {
  124. t.Errorf("a.URI = %q; want https://ca.tld/acme/reg/1", a.URI)
  125. }
  126. if a.Authz != "https://ca.tld/acme/new-authz" {
  127. t.Errorf("a.Authz = %q; want https://ca.tld/acme/new-authz", a.Authz)
  128. }
  129. if a.CurrentTerms != "https://ca.tld/acme/terms" {
  130. t.Errorf("a.CurrentTerms = %q; want https://ca.tld/acme/terms", a.CurrentTerms)
  131. }
  132. if !reflect.DeepEqual(a.Contact, contacts) {
  133. t.Errorf("a.Contact = %v; want %v", a.Contact, contacts)
  134. }
  135. }
  136. func TestUpdateReg(t *testing.T) {
  137. const terms = "https://ca.tld/acme/terms"
  138. contacts := []string{"mailto:admin@example.com"}
  139. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  140. if r.Method == "HEAD" {
  141. w.Header().Set("replay-nonce", "test-nonce")
  142. return
  143. }
  144. if r.Method != "POST" {
  145. t.Errorf("r.Method = %q; want POST", r.Method)
  146. }
  147. var j struct {
  148. Resource string
  149. Contact []string
  150. Agreement string
  151. }
  152. decodeJWSRequest(t, &j, r)
  153. // Test request
  154. if j.Resource != "reg" {
  155. t.Errorf("j.Resource = %q; want reg", j.Resource)
  156. }
  157. if j.Agreement != terms {
  158. t.Errorf("j.Agreement = %q; want %q", j.Agreement, terms)
  159. }
  160. if !reflect.DeepEqual(j.Contact, contacts) {
  161. t.Errorf("j.Contact = %v; want %v", j.Contact, contacts)
  162. }
  163. w.Header().Set("Link", `<https://ca.tld/acme/new-authz>;rel="next"`)
  164. w.Header().Add("Link", `<https://ca.tld/acme/recover-reg>;rel="recover"`)
  165. w.Header().Add("Link", fmt.Sprintf(`<%s>;rel="terms-of-service"`, terms))
  166. w.WriteHeader(http.StatusOK)
  167. b, _ := json.Marshal(contacts)
  168. fmt.Fprintf(w, `{"contact":%s, "agreement":%q}`, b, terms)
  169. }))
  170. defer ts.Close()
  171. c := Client{Key: testKeyEC}
  172. a := &Account{URI: ts.URL, Contact: contacts, AgreedTerms: terms}
  173. var err error
  174. if a, err = c.UpdateReg(context.Background(), a); err != nil {
  175. t.Fatal(err)
  176. }
  177. if a.Authz != "https://ca.tld/acme/new-authz" {
  178. t.Errorf("a.Authz = %q; want https://ca.tld/acme/new-authz", a.Authz)
  179. }
  180. if a.AgreedTerms != terms {
  181. t.Errorf("a.AgreedTerms = %q; want %q", a.AgreedTerms, terms)
  182. }
  183. if a.CurrentTerms != terms {
  184. t.Errorf("a.CurrentTerms = %q; want %q", a.CurrentTerms, terms)
  185. }
  186. if a.URI != ts.URL {
  187. t.Errorf("a.URI = %q; want %q", a.URI, ts.URL)
  188. }
  189. }
  190. func TestGetReg(t *testing.T) {
  191. const terms = "https://ca.tld/acme/terms"
  192. const newTerms = "https://ca.tld/acme/new-terms"
  193. contacts := []string{"mailto:admin@example.com"}
  194. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  195. if r.Method == "HEAD" {
  196. w.Header().Set("replay-nonce", "test-nonce")
  197. return
  198. }
  199. if r.Method != "POST" {
  200. t.Errorf("r.Method = %q; want POST", r.Method)
  201. }
  202. var j struct {
  203. Resource string
  204. Contact []string
  205. Agreement string
  206. }
  207. decodeJWSRequest(t, &j, r)
  208. // Test request
  209. if j.Resource != "reg" {
  210. t.Errorf("j.Resource = %q; want reg", j.Resource)
  211. }
  212. if len(j.Contact) != 0 {
  213. t.Errorf("j.Contact = %v", j.Contact)
  214. }
  215. if j.Agreement != "" {
  216. t.Errorf("j.Agreement = %q", j.Agreement)
  217. }
  218. w.Header().Set("Link", `<https://ca.tld/acme/new-authz>;rel="next"`)
  219. w.Header().Add("Link", `<https://ca.tld/acme/recover-reg>;rel="recover"`)
  220. w.Header().Add("Link", fmt.Sprintf(`<%s>;rel="terms-of-service"`, newTerms))
  221. w.WriteHeader(http.StatusOK)
  222. b, _ := json.Marshal(contacts)
  223. fmt.Fprintf(w, `{"contact":%s, "agreement":%q}`, b, terms)
  224. }))
  225. defer ts.Close()
  226. c := Client{Key: testKeyEC}
  227. a, err := c.GetReg(context.Background(), ts.URL)
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. if a.Authz != "https://ca.tld/acme/new-authz" {
  232. t.Errorf("a.AuthzURL = %q; want https://ca.tld/acme/new-authz", a.Authz)
  233. }
  234. if a.AgreedTerms != terms {
  235. t.Errorf("a.AgreedTerms = %q; want %q", a.AgreedTerms, terms)
  236. }
  237. if a.CurrentTerms != newTerms {
  238. t.Errorf("a.CurrentTerms = %q; want %q", a.CurrentTerms, newTerms)
  239. }
  240. if a.URI != ts.URL {
  241. t.Errorf("a.URI = %q; want %q", a.URI, ts.URL)
  242. }
  243. }
  244. func TestAuthorize(t *testing.T) {
  245. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  246. if r.Method == "HEAD" {
  247. w.Header().Set("replay-nonce", "test-nonce")
  248. return
  249. }
  250. if r.Method != "POST" {
  251. t.Errorf("r.Method = %q; want POST", r.Method)
  252. }
  253. var j struct {
  254. Resource string
  255. Identifier struct {
  256. Type string
  257. Value string
  258. }
  259. }
  260. decodeJWSRequest(t, &j, r)
  261. // Test request
  262. if j.Resource != "new-authz" {
  263. t.Errorf("j.Resource = %q; want new-authz", j.Resource)
  264. }
  265. if j.Identifier.Type != "dns" {
  266. t.Errorf("j.Identifier.Type = %q; want dns", j.Identifier.Type)
  267. }
  268. if j.Identifier.Value != "example.com" {
  269. t.Errorf("j.Identifier.Value = %q; want example.com", j.Identifier.Value)
  270. }
  271. w.Header().Set("Location", "https://ca.tld/acme/auth/1")
  272. w.WriteHeader(http.StatusCreated)
  273. fmt.Fprintf(w, `{
  274. "identifier": {"type":"dns","value":"example.com"},
  275. "status":"pending",
  276. "challenges":[
  277. {
  278. "type":"http-01",
  279. "status":"pending",
  280. "uri":"https://ca.tld/acme/challenge/publickey/id1",
  281. "token":"token1"
  282. },
  283. {
  284. "type":"tls-sni-01",
  285. "status":"pending",
  286. "uri":"https://ca.tld/acme/challenge/publickey/id2",
  287. "token":"token2"
  288. }
  289. ],
  290. "combinations":[[0],[1]]}`)
  291. }))
  292. defer ts.Close()
  293. cl := Client{Key: testKeyEC, dir: &Directory{AuthzURL: ts.URL}}
  294. auth, err := cl.Authorize(context.Background(), "example.com")
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. if auth.URI != "https://ca.tld/acme/auth/1" {
  299. t.Errorf("URI = %q; want https://ca.tld/acme/auth/1", auth.URI)
  300. }
  301. if auth.Status != "pending" {
  302. t.Errorf("Status = %q; want pending", auth.Status)
  303. }
  304. if auth.Identifier.Type != "dns" {
  305. t.Errorf("Identifier.Type = %q; want dns", auth.Identifier.Type)
  306. }
  307. if auth.Identifier.Value != "example.com" {
  308. t.Errorf("Identifier.Value = %q; want example.com", auth.Identifier.Value)
  309. }
  310. if n := len(auth.Challenges); n != 2 {
  311. t.Fatalf("len(auth.Challenges) = %d; want 2", n)
  312. }
  313. c := auth.Challenges[0]
  314. if c.Type != "http-01" {
  315. t.Errorf("c.Type = %q; want http-01", c.Type)
  316. }
  317. if c.URI != "https://ca.tld/acme/challenge/publickey/id1" {
  318. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id1", c.URI)
  319. }
  320. if c.Token != "token1" {
  321. t.Errorf("c.Token = %q; want token1", c.Type)
  322. }
  323. c = auth.Challenges[1]
  324. if c.Type != "tls-sni-01" {
  325. t.Errorf("c.Type = %q; want tls-sni-01", c.Type)
  326. }
  327. if c.URI != "https://ca.tld/acme/challenge/publickey/id2" {
  328. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id2", c.URI)
  329. }
  330. if c.Token != "token2" {
  331. t.Errorf("c.Token = %q; want token2", c.Type)
  332. }
  333. combs := [][]int{{0}, {1}}
  334. if !reflect.DeepEqual(auth.Combinations, combs) {
  335. t.Errorf("auth.Combinations: %+v\nwant: %+v\n", auth.Combinations, combs)
  336. }
  337. }
  338. func TestAuthorizeValid(t *testing.T) {
  339. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  340. if r.Method == "HEAD" {
  341. w.Header().Set("replay-nonce", "nonce")
  342. return
  343. }
  344. w.WriteHeader(http.StatusCreated)
  345. w.Write([]byte(`{"status":"valid"}`))
  346. }))
  347. defer ts.Close()
  348. client := Client{Key: testKey, dir: &Directory{AuthzURL: ts.URL}}
  349. _, err := client.Authorize(context.Background(), "example.com")
  350. if err != nil {
  351. t.Errorf("err = %v", err)
  352. }
  353. }
  354. func TestGetAuthorization(t *testing.T) {
  355. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  356. if r.Method != "GET" {
  357. t.Errorf("r.Method = %q; want GET", r.Method)
  358. }
  359. w.WriteHeader(http.StatusOK)
  360. fmt.Fprintf(w, `{
  361. "identifier": {"type":"dns","value":"example.com"},
  362. "status":"pending",
  363. "challenges":[
  364. {
  365. "type":"http-01",
  366. "status":"pending",
  367. "uri":"https://ca.tld/acme/challenge/publickey/id1",
  368. "token":"token1"
  369. },
  370. {
  371. "type":"tls-sni-01",
  372. "status":"pending",
  373. "uri":"https://ca.tld/acme/challenge/publickey/id2",
  374. "token":"token2"
  375. }
  376. ],
  377. "combinations":[[0],[1]]}`)
  378. }))
  379. defer ts.Close()
  380. cl := Client{Key: testKeyEC}
  381. auth, err := cl.GetAuthorization(context.Background(), ts.URL)
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. if auth.Status != "pending" {
  386. t.Errorf("Status = %q; want pending", auth.Status)
  387. }
  388. if auth.Identifier.Type != "dns" {
  389. t.Errorf("Identifier.Type = %q; want dns", auth.Identifier.Type)
  390. }
  391. if auth.Identifier.Value != "example.com" {
  392. t.Errorf("Identifier.Value = %q; want example.com", auth.Identifier.Value)
  393. }
  394. if n := len(auth.Challenges); n != 2 {
  395. t.Fatalf("len(set.Challenges) = %d; want 2", n)
  396. }
  397. c := auth.Challenges[0]
  398. if c.Type != "http-01" {
  399. t.Errorf("c.Type = %q; want http-01", c.Type)
  400. }
  401. if c.URI != "https://ca.tld/acme/challenge/publickey/id1" {
  402. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id1", c.URI)
  403. }
  404. if c.Token != "token1" {
  405. t.Errorf("c.Token = %q; want token1", c.Type)
  406. }
  407. c = auth.Challenges[1]
  408. if c.Type != "tls-sni-01" {
  409. t.Errorf("c.Type = %q; want tls-sni-01", c.Type)
  410. }
  411. if c.URI != "https://ca.tld/acme/challenge/publickey/id2" {
  412. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id2", c.URI)
  413. }
  414. if c.Token != "token2" {
  415. t.Errorf("c.Token = %q; want token2", c.Type)
  416. }
  417. combs := [][]int{{0}, {1}}
  418. if !reflect.DeepEqual(auth.Combinations, combs) {
  419. t.Errorf("auth.Combinations: %+v\nwant: %+v\n", auth.Combinations, combs)
  420. }
  421. }
  422. func TestWaitAuthorization(t *testing.T) {
  423. var count int
  424. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  425. count++
  426. w.Header().Set("retry-after", "0")
  427. if count > 1 {
  428. fmt.Fprintf(w, `{"status":"valid"}`)
  429. return
  430. }
  431. fmt.Fprintf(w, `{"status":"pending"}`)
  432. }))
  433. defer ts.Close()
  434. type res struct {
  435. authz *Authorization
  436. err error
  437. }
  438. done := make(chan res)
  439. defer close(done)
  440. go func() {
  441. var client Client
  442. a, err := client.WaitAuthorization(context.Background(), ts.URL)
  443. done <- res{a, err}
  444. }()
  445. select {
  446. case <-time.After(5 * time.Second):
  447. t.Fatal("WaitAuthz took too long to return")
  448. case res := <-done:
  449. if res.err != nil {
  450. t.Fatalf("res.err = %v", res.err)
  451. }
  452. if res.authz == nil {
  453. t.Fatal("res.authz is nil")
  454. }
  455. }
  456. }
  457. func TestWaitAuthorizationInvalid(t *testing.T) {
  458. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  459. fmt.Fprintf(w, `{"status":"invalid"}`)
  460. }))
  461. defer ts.Close()
  462. res := make(chan error)
  463. defer close(res)
  464. go func() {
  465. var client Client
  466. _, err := client.WaitAuthorization(context.Background(), ts.URL)
  467. res <- err
  468. }()
  469. select {
  470. case <-time.After(3 * time.Second):
  471. t.Fatal("WaitAuthz took too long to return")
  472. case err := <-res:
  473. if err == nil {
  474. t.Error("err is nil")
  475. }
  476. }
  477. }
  478. func TestWaitAuthorizationCancel(t *testing.T) {
  479. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  480. w.Header().Set("retry-after", "60")
  481. fmt.Fprintf(w, `{"status":"pending"}`)
  482. }))
  483. defer ts.Close()
  484. res := make(chan error)
  485. defer close(res)
  486. go func() {
  487. var client Client
  488. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  489. defer cancel()
  490. _, err := client.WaitAuthorization(ctx, ts.URL)
  491. res <- err
  492. }()
  493. select {
  494. case <-time.After(time.Second):
  495. t.Fatal("WaitAuthz took too long to return")
  496. case err := <-res:
  497. if err == nil {
  498. t.Error("err is nil")
  499. }
  500. }
  501. }
  502. func TestPollChallenge(t *testing.T) {
  503. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  504. if r.Method != "GET" {
  505. t.Errorf("r.Method = %q; want GET", r.Method)
  506. }
  507. w.WriteHeader(http.StatusOK)
  508. fmt.Fprintf(w, `{
  509. "type":"http-01",
  510. "status":"pending",
  511. "uri":"https://ca.tld/acme/challenge/publickey/id1",
  512. "token":"token1"}`)
  513. }))
  514. defer ts.Close()
  515. cl := Client{Key: testKeyEC}
  516. chall, err := cl.GetChallenge(context.Background(), ts.URL)
  517. if err != nil {
  518. t.Fatal(err)
  519. }
  520. if chall.Status != "pending" {
  521. t.Errorf("Status = %q; want pending", chall.Status)
  522. }
  523. if chall.Type != "http-01" {
  524. t.Errorf("c.Type = %q; want http-01", chall.Type)
  525. }
  526. if chall.URI != "https://ca.tld/acme/challenge/publickey/id1" {
  527. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id1", chall.URI)
  528. }
  529. if chall.Token != "token1" {
  530. t.Errorf("c.Token = %q; want token1", chall.Type)
  531. }
  532. }
  533. func TestAcceptChallenge(t *testing.T) {
  534. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  535. if r.Method == "HEAD" {
  536. w.Header().Set("replay-nonce", "test-nonce")
  537. return
  538. }
  539. if r.Method != "POST" {
  540. t.Errorf("r.Method = %q; want POST", r.Method)
  541. }
  542. var j struct {
  543. Resource string
  544. Type string
  545. Auth string `json:"keyAuthorization"`
  546. }
  547. decodeJWSRequest(t, &j, r)
  548. // Test request
  549. if j.Resource != "challenge" {
  550. t.Errorf(`resource = %q; want "challenge"`, j.Resource)
  551. }
  552. if j.Type != "http-01" {
  553. t.Errorf(`type = %q; want "http-01"`, j.Type)
  554. }
  555. keyAuth := "token1." + testKeyECThumbprint
  556. if j.Auth != keyAuth {
  557. t.Errorf(`keyAuthorization = %q; want %q`, j.Auth, keyAuth)
  558. }
  559. // Respond to request
  560. w.WriteHeader(http.StatusAccepted)
  561. fmt.Fprintf(w, `{
  562. "type":"http-01",
  563. "status":"pending",
  564. "uri":"https://ca.tld/acme/challenge/publickey/id1",
  565. "token":"token1",
  566. "keyAuthorization":%q
  567. }`, keyAuth)
  568. }))
  569. defer ts.Close()
  570. cl := Client{Key: testKeyEC}
  571. c, err := cl.Accept(context.Background(), &Challenge{
  572. URI: ts.URL,
  573. Token: "token1",
  574. Type: "http-01",
  575. })
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. if c.Type != "http-01" {
  580. t.Errorf("c.Type = %q; want http-01", c.Type)
  581. }
  582. if c.URI != "https://ca.tld/acme/challenge/publickey/id1" {
  583. t.Errorf("c.URI = %q; want https://ca.tld/acme/challenge/publickey/id1", c.URI)
  584. }
  585. if c.Token != "token1" {
  586. t.Errorf("c.Token = %q; want token1", c.Type)
  587. }
  588. }
  589. func TestNewCert(t *testing.T) {
  590. notBefore := time.Now()
  591. notAfter := notBefore.AddDate(0, 2, 0)
  592. timeNow = func() time.Time { return notBefore }
  593. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  594. if r.Method == "HEAD" {
  595. w.Header().Set("replay-nonce", "test-nonce")
  596. return
  597. }
  598. if r.Method != "POST" {
  599. t.Errorf("r.Method = %q; want POST", r.Method)
  600. }
  601. var j struct {
  602. Resource string `json:"resource"`
  603. CSR string `json:"csr"`
  604. NotBefore string `json:"notBefore,omitempty"`
  605. NotAfter string `json:"notAfter,omitempty"`
  606. }
  607. decodeJWSRequest(t, &j, r)
  608. // Test request
  609. if j.Resource != "new-cert" {
  610. t.Errorf(`resource = %q; want "new-cert"`, j.Resource)
  611. }
  612. if j.NotBefore != notBefore.Format(time.RFC3339) {
  613. t.Errorf(`notBefore = %q; wanted %q`, j.NotBefore, notBefore.Format(time.RFC3339))
  614. }
  615. if j.NotAfter != notAfter.Format(time.RFC3339) {
  616. t.Errorf(`notAfter = %q; wanted %q`, j.NotAfter, notAfter.Format(time.RFC3339))
  617. }
  618. // Respond to request
  619. template := x509.Certificate{
  620. SerialNumber: big.NewInt(int64(1)),
  621. Subject: pkix.Name{
  622. Organization: []string{"goacme"},
  623. },
  624. NotBefore: notBefore,
  625. NotAfter: notAfter,
  626. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
  627. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  628. BasicConstraintsValid: true,
  629. }
  630. sampleCert, err := x509.CreateCertificate(rand.Reader, &template, &template, &testKeyEC.PublicKey, testKeyEC)
  631. if err != nil {
  632. t.Fatalf("Error creating certificate: %v", err)
  633. }
  634. w.Header().Set("Location", "https://ca.tld/acme/cert/1")
  635. w.WriteHeader(http.StatusCreated)
  636. w.Write(sampleCert)
  637. }))
  638. defer ts.Close()
  639. csr := x509.CertificateRequest{
  640. Version: 0,
  641. Subject: pkix.Name{
  642. CommonName: "example.com",
  643. Organization: []string{"goacme"},
  644. },
  645. }
  646. csrb, err := x509.CreateCertificateRequest(rand.Reader, &csr, testKeyEC)
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. c := Client{Key: testKeyEC, dir: &Directory{CertURL: ts.URL}}
  651. cert, certURL, err := c.CreateCert(context.Background(), csrb, notAfter.Sub(notBefore), false)
  652. if err != nil {
  653. t.Fatal(err)
  654. }
  655. if cert == nil {
  656. t.Errorf("cert is nil")
  657. }
  658. if certURL != "https://ca.tld/acme/cert/1" {
  659. t.Errorf("certURL = %q; want https://ca.tld/acme/cert/1", certURL)
  660. }
  661. }
  662. func TestFetchCert(t *testing.T) {
  663. var count byte
  664. var ts *httptest.Server
  665. ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  666. count++
  667. if count < 3 {
  668. up := fmt.Sprintf("<%s>;rel=up", ts.URL)
  669. w.Header().Set("link", up)
  670. }
  671. w.Write([]byte{count})
  672. }))
  673. defer ts.Close()
  674. res, err := (&Client{}).FetchCert(context.Background(), ts.URL, true)
  675. if err != nil {
  676. t.Fatalf("FetchCert: %v", err)
  677. }
  678. cert := [][]byte{{1}, {2}, {3}}
  679. if !reflect.DeepEqual(res, cert) {
  680. t.Errorf("res = %v; want %v", res, cert)
  681. }
  682. }
  683. func TestFetchCertRetry(t *testing.T) {
  684. var count int
  685. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  686. if count < 1 {
  687. w.Header().Set("retry-after", "0")
  688. w.WriteHeader(http.StatusAccepted)
  689. count++
  690. return
  691. }
  692. w.Write([]byte{1})
  693. }))
  694. defer ts.Close()
  695. res, err := (&Client{}).FetchCert(context.Background(), ts.URL, false)
  696. if err != nil {
  697. t.Fatalf("FetchCert: %v", err)
  698. }
  699. cert := [][]byte{{1}}
  700. if !reflect.DeepEqual(res, cert) {
  701. t.Errorf("res = %v; want %v", res, cert)
  702. }
  703. }
  704. func TestFetchCertCancel(t *testing.T) {
  705. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  706. w.Header().Set("retry-after", "0")
  707. w.WriteHeader(http.StatusAccepted)
  708. }))
  709. defer ts.Close()
  710. ctx, cancel := context.WithCancel(context.Background())
  711. done := make(chan struct{})
  712. var err error
  713. go func() {
  714. _, err = (&Client{}).FetchCert(ctx, ts.URL, false)
  715. close(done)
  716. }()
  717. cancel()
  718. <-done
  719. if err != context.Canceled {
  720. t.Errorf("err = %v; want %v", err, context.Canceled)
  721. }
  722. }
  723. func TestFetchCertDepth(t *testing.T) {
  724. var count byte
  725. var ts *httptest.Server
  726. ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  727. count++
  728. if count > maxChainLen+1 {
  729. t.Errorf("count = %d; want at most %d", count, maxChainLen+1)
  730. w.WriteHeader(http.StatusInternalServerError)
  731. }
  732. w.Header().Set("link", fmt.Sprintf("<%s>;rel=up", ts.URL))
  733. w.Write([]byte{count})
  734. }))
  735. defer ts.Close()
  736. _, err := (&Client{}).FetchCert(context.Background(), ts.URL, true)
  737. if err == nil {
  738. t.Errorf("err is nil")
  739. }
  740. }
  741. func TestFetchCertBreadth(t *testing.T) {
  742. var ts *httptest.Server
  743. ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  744. for i := 0; i < maxChainLen+1; i++ {
  745. w.Header().Add("link", fmt.Sprintf("<%s>;rel=up", ts.URL))
  746. }
  747. w.Write([]byte{1})
  748. }))
  749. defer ts.Close()
  750. _, err := (&Client{}).FetchCert(context.Background(), ts.URL, true)
  751. if err == nil {
  752. t.Errorf("err is nil")
  753. }
  754. }
  755. func TestFetchCertSize(t *testing.T) {
  756. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  757. b := bytes.Repeat([]byte{1}, maxCertSize+1)
  758. w.Write(b)
  759. }))
  760. defer ts.Close()
  761. _, err := (&Client{}).FetchCert(context.Background(), ts.URL, false)
  762. if err == nil {
  763. t.Errorf("err is nil")
  764. }
  765. }
  766. func TestRevokeCert(t *testing.T) {
  767. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  768. if r.Method == "HEAD" {
  769. w.Header().Set("replay-nonce", "nonce")
  770. return
  771. }
  772. var req struct {
  773. Resource string
  774. Certificate string
  775. Reason int
  776. }
  777. decodeJWSRequest(t, &req, r)
  778. if req.Resource != "revoke-cert" {
  779. t.Errorf("req.Resource = %q; want revoke-cert", req.Resource)
  780. }
  781. if req.Reason != 1 {
  782. t.Errorf("req.Reason = %d; want 1", req.Reason)
  783. }
  784. // echo -n cert | base64 | tr -d '=' | tr '/+' '_-'
  785. cert := "Y2VydA"
  786. if req.Certificate != cert {
  787. t.Errorf("req.Certificate = %q; want %q", req.Certificate, cert)
  788. }
  789. }))
  790. defer ts.Close()
  791. client := &Client{
  792. Key: testKeyEC,
  793. dir: &Directory{RevokeURL: ts.URL},
  794. }
  795. ctx := context.Background()
  796. if err := client.RevokeCert(ctx, nil, []byte("cert"), CRLReasonKeyCompromise); err != nil {
  797. t.Fatal(err)
  798. }
  799. }
  800. func TestFetchNonce(t *testing.T) {
  801. tests := []struct {
  802. code int
  803. nonce string
  804. }{
  805. {http.StatusOK, "nonce1"},
  806. {http.StatusBadRequest, "nonce2"},
  807. {http.StatusOK, ""},
  808. }
  809. var i int
  810. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  811. if r.Method != "HEAD" {
  812. t.Errorf("%d: r.Method = %q; want HEAD", i, r.Method)
  813. }
  814. w.Header().Set("replay-nonce", tests[i].nonce)
  815. w.WriteHeader(tests[i].code)
  816. }))
  817. defer ts.Close()
  818. for ; i < len(tests); i++ {
  819. test := tests[i]
  820. n, err := fetchNonce(context.Background(), http.DefaultClient, ts.URL)
  821. if n != test.nonce {
  822. t.Errorf("%d: n=%q; want %q", i, n, test.nonce)
  823. }
  824. switch {
  825. case err == nil && test.nonce == "":
  826. t.Errorf("%d: n=%q, err=%v; want non-nil error", i, n, err)
  827. case err != nil && test.nonce != "":
  828. t.Errorf("%d: n=%q, err=%v; want %q", i, n, err, test.nonce)
  829. }
  830. }
  831. }
  832. func TestLinkHeader(t *testing.T) {
  833. h := http.Header{"Link": {
  834. `<https://example.com/acme/new-authz>;rel="next"`,
  835. `<https://example.com/acme/recover-reg>; rel=recover`,
  836. `<https://example.com/acme/terms>; foo=bar; rel="terms-of-service"`,
  837. `<dup>;rel="next"`,
  838. }}
  839. tests := []struct {
  840. rel string
  841. out []string
  842. }{
  843. {"next", []string{"https://example.com/acme/new-authz", "dup"}},
  844. {"recover", []string{"https://example.com/acme/recover-reg"}},
  845. {"terms-of-service", []string{"https://example.com/acme/terms"}},
  846. {"empty", nil},
  847. }
  848. for i, test := range tests {
  849. if v := linkHeader(h, test.rel); !reflect.DeepEqual(v, test.out) {
  850. t.Errorf("%d: linkHeader(%q): %v; want %v", i, test.rel, v, test.out)
  851. }
  852. }
  853. }
  854. func TestErrorResponse(t *testing.T) {
  855. s := `{
  856. "status": 400,
  857. "type": "urn:acme:error:xxx",
  858. "detail": "text"
  859. }`
  860. res := &http.Response{
  861. StatusCode: 400,
  862. Status: "400 Bad Request",
  863. Body: ioutil.NopCloser(strings.NewReader(s)),
  864. Header: http.Header{"X-Foo": {"bar"}},
  865. }
  866. err := responseError(res)
  867. v, ok := err.(*Error)
  868. if !ok {
  869. t.Fatalf("err = %+v (%T); want *Error type", err, err)
  870. }
  871. if v.StatusCode != 400 {
  872. t.Errorf("v.StatusCode = %v; want 400", v.StatusCode)
  873. }
  874. if v.ProblemType != "urn:acme:error:xxx" {
  875. t.Errorf("v.ProblemType = %q; want urn:acme:error:xxx", v.ProblemType)
  876. }
  877. if v.Detail != "text" {
  878. t.Errorf("v.Detail = %q; want text", v.Detail)
  879. }
  880. if !reflect.DeepEqual(v.Header, res.Header) {
  881. t.Errorf("v.Header = %+v; want %+v", v.Header, res.Header)
  882. }
  883. }
  884. func TestTLSSNI01ChallengeCert(t *testing.T) {
  885. const (
  886. token = "evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA"
  887. // echo -n <token.testKeyECThumbprint> | shasum -a 256
  888. san = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.acme.invalid"
  889. )
  890. client := &Client{Key: testKeyEC}
  891. tlscert, name, err := client.TLSSNI01ChallengeCert(token)
  892. if err != nil {
  893. t.Fatal(err)
  894. }
  895. if n := len(tlscert.Certificate); n != 1 {
  896. t.Fatalf("len(tlscert.Certificate) = %d; want 1", n)
  897. }
  898. cert, err := x509.ParseCertificate(tlscert.Certificate[0])
  899. if err != nil {
  900. t.Fatal(err)
  901. }
  902. if len(cert.DNSNames) != 1 || cert.DNSNames[0] != san {
  903. t.Fatalf("cert.DNSNames = %v; want %q", cert.DNSNames, san)
  904. }
  905. if cert.DNSNames[0] != name {
  906. t.Errorf("cert.DNSNames[0] != name: %q vs %q", cert.DNSNames[0], name)
  907. }
  908. }
  909. func TestTLSSNI02ChallengeCert(t *testing.T) {
  910. const (
  911. token = "evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA"
  912. // echo -n evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA | shasum -a 256
  913. sanA = "7ea0aaa69214e71e02cebb18bb867736.09b730209baabf60e43d4999979ff139.token.acme.invalid"
  914. // echo -n <token.testKeyECThumbprint> | shasum -a 256
  915. sanB = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.ka.acme.invalid"
  916. )
  917. client := &Client{Key: testKeyEC}
  918. tlscert, name, err := client.TLSSNI02ChallengeCert(token)
  919. if err != nil {
  920. t.Fatal(err)
  921. }
  922. if n := len(tlscert.Certificate); n != 1 {
  923. t.Fatalf("len(tlscert.Certificate) = %d; want 1", n)
  924. }
  925. cert, err := x509.ParseCertificate(tlscert.Certificate[0])
  926. if err != nil {
  927. t.Fatal(err)
  928. }
  929. names := []string{sanA, sanB}
  930. if !reflect.DeepEqual(cert.DNSNames, names) {
  931. t.Fatalf("cert.DNSNames = %v;\nwant %v", cert.DNSNames, names)
  932. }
  933. sort.Strings(cert.DNSNames)
  934. i := sort.SearchStrings(cert.DNSNames, name)
  935. if i >= len(cert.DNSNames) || cert.DNSNames[i] != name {
  936. t.Errorf("%v doesn't have %q", cert.DNSNames, name)
  937. }
  938. }
  939. func TestTLSChallengeCertRSA(t *testing.T) {
  940. key, err := rsa.GenerateKey(rand.Reader, 512)
  941. if err != nil {
  942. t.Fatal(err)
  943. }
  944. client := &Client{Key: testKeyEC}
  945. cert1, _, err := client.TLSSNI01ChallengeCert("token", WithKey(key))
  946. if err != nil {
  947. t.Fatal(err)
  948. }
  949. cert2, _, err := client.TLSSNI02ChallengeCert("token", WithKey(key))
  950. if err != nil {
  951. t.Fatal(err)
  952. }
  953. for i, tlscert := range []tls.Certificate{cert1, cert2} {
  954. // verify generated cert private key
  955. tlskey, ok := tlscert.PrivateKey.(*rsa.PrivateKey)
  956. if !ok {
  957. t.Errorf("%d: tlscert.PrivateKey is %T; want *rsa.PrivateKey", i, tlscert.PrivateKey)
  958. continue
  959. }
  960. if tlskey.D.Cmp(key.D) != 0 {
  961. t.Errorf("%d: tlskey.D = %v; want %v", i, tlskey.D, key.D)
  962. }
  963. // verify generated cert public key
  964. x509Cert, err := x509.ParseCertificate(tlscert.Certificate[0])
  965. if err != nil {
  966. t.Errorf("%d: %v", i, err)
  967. continue
  968. }
  969. tlspub, ok := x509Cert.PublicKey.(*rsa.PublicKey)
  970. if !ok {
  971. t.Errorf("%d: x509Cert.PublicKey is %T; want *rsa.PublicKey", i, x509Cert.PublicKey)
  972. continue
  973. }
  974. if tlspub.N.Cmp(key.N) != 0 {
  975. t.Errorf("%d: tlspub.N = %v; want %v", i, tlspub.N, key.N)
  976. }
  977. }
  978. }
  979. func TestHTTP01Challenge(t *testing.T) {
  980. const (
  981. token = "xxx"
  982. // thumbprint is precomputed for testKeyEC in jws_test.go
  983. value = token + "." + testKeyECThumbprint
  984. urlpath = "/.well-known/acme-challenge/" + token
  985. )
  986. client := &Client{Key: testKeyEC}
  987. val, err := client.HTTP01ChallengeResponse(token)
  988. if err != nil {
  989. t.Fatal(err)
  990. }
  991. if val != value {
  992. t.Errorf("val = %q; want %q", val, value)
  993. }
  994. if path := client.HTTP01ChallengePath(token); path != urlpath {
  995. t.Errorf("path = %q; want %q", path, urlpath)
  996. }
  997. }
  998. func TestDNS01ChallengeRecord(t *testing.T) {
  999. // echo -n xxx.<testKeyECThumbprint> | \
  1000. // openssl dgst -binary -sha256 | \
  1001. // base64 | tr -d '=' | tr '/+' '_-'
  1002. const value = "8DERMexQ5VcdJ_prpPiA0mVdp7imgbCgjsG4SqqNMIo"
  1003. client := &Client{Key: testKeyEC}
  1004. val, err := client.DNS01ChallengeRecord("xxx")
  1005. if err != nil {
  1006. t.Fatal(err)
  1007. }
  1008. if val != value {
  1009. t.Errorf("val = %q; want %q", val, value)
  1010. }
  1011. }
  1012. func TestBackoff(t *testing.T) {
  1013. tt := []struct{ min, max time.Duration }{
  1014. {time.Second, 2 * time.Second},
  1015. {2 * time.Second, 3 * time.Second},
  1016. {4 * time.Second, 5 * time.Second},
  1017. {8 * time.Second, 9 * time.Second},
  1018. }
  1019. for i, test := range tt {
  1020. d := backoff(i, time.Minute)
  1021. if d < test.min || test.max < d {
  1022. t.Errorf("%d: d = %v; want between %v and %v", i, d, test.min, test.max)
  1023. }
  1024. }
  1025. min, max := time.Second, 2*time.Second
  1026. if d := backoff(-1, time.Minute); d < min || max < d {
  1027. t.Errorf("d = %v; want between %v and %v", d, min, max)
  1028. }
  1029. bound := 10 * time.Second
  1030. if d := backoff(100, bound); d != bound {
  1031. t.Errorf("d = %v; want %v", d, bound)
  1032. }
  1033. }