jws_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. "crypto"
  7. "crypto/ecdsa"
  8. "crypto/elliptic"
  9. "crypto/rsa"
  10. "crypto/sha256"
  11. "crypto/x509"
  12. "encoding/base64"
  13. "encoding/json"
  14. "encoding/pem"
  15. "fmt"
  16. "io"
  17. "math/big"
  18. "testing"
  19. )
  20. // The following shell command alias is used in the comments
  21. // throughout this file:
  22. // alias b64raw="base64 -w0 | tr -d '=' | tr '/+' '_-'"
  23. const (
  24. // Modulus in raw base64:
  25. // 4xgZ3eRPkwoRvy7qeRUbmMDe0V-xH9eWLdu0iheeLlrmD2mqWXfP9IeSKApbn34
  26. // g8TuAS9g5zhq8ELQ3kmjr-KV86GAMgI6VAcGlq3QrzpTCf_30Ab7-zawrfRaFON
  27. // a1HwEzPY1KHnGVkxJc85gNkwYI9SY2RHXtvln3zs5wITNrdosqEXeaIkVYBEhbh
  28. // Nu54pp3kxo6TuWLi9e6pXeWetEwmlBwtWZlPoib2j3TxLBksKZfoyFyek380mHg
  29. // JAumQ_I2fjj98_97mk3ihOY4AgVdCDj1z_GCoZkG5Rq7nbCGyosyKWyDX00Zs-n
  30. // NqVhoLeIvXC4nnWdJMZ6rogxyQQ
  31. testKeyPEM = `
  32. -----BEGIN RSA PRIVATE KEY-----
  33. MIIEowIBAAKCAQEA4xgZ3eRPkwoRvy7qeRUbmMDe0V+xH9eWLdu0iheeLlrmD2mq
  34. WXfP9IeSKApbn34g8TuAS9g5zhq8ELQ3kmjr+KV86GAMgI6VAcGlq3QrzpTCf/30
  35. Ab7+zawrfRaFONa1HwEzPY1KHnGVkxJc85gNkwYI9SY2RHXtvln3zs5wITNrdosq
  36. EXeaIkVYBEhbhNu54pp3kxo6TuWLi9e6pXeWetEwmlBwtWZlPoib2j3TxLBksKZf
  37. oyFyek380mHgJAumQ/I2fjj98/97mk3ihOY4AgVdCDj1z/GCoZkG5Rq7nbCGyosy
  38. KWyDX00Zs+nNqVhoLeIvXC4nnWdJMZ6rogxyQQIDAQABAoIBACIEZTOI1Kao9nmV
  39. 9IeIsuaR1Y61b9neOF/MLmIVIZu+AAJFCMB4Iw11FV6sFodwpEyeZhx2WkpWVN+H
  40. r19eGiLX3zsL0DOdqBJoSIHDWCCMxgnYJ6nvS0nRxX3qVrBp8R2g12Ub+gNPbmFm
  41. ecf/eeERIVxfifd9VsyRu34eDEvcmKFuLYbElFcPh62xE3x12UZvV/sN7gXbawpP
  42. G+w255vbE5MoaKdnnO83cTFlcHvhn24M/78qP7Te5OAeelr1R89kYxQLpuGe4fbS
  43. zc6E3ym5Td6urDetGGrSY1Eu10/8sMusX+KNWkm+RsBRbkyKq72ks/qKpOxOa+c6
  44. 9gm+Y8ECgYEA/iNUyg1ubRdH11p82l8KHtFC1DPE0V1gSZsX29TpM5jS4qv46K+s
  45. 8Ym1zmrORM8x+cynfPx1VQZQ34EYeCMIX212ryJ+zDATl4NE0I4muMvSiH9vx6Xc
  46. 7FmhNnaYzPsBL5Tm9nmtQuP09YEn8poiOJFiDs/4olnD5ogA5O4THGkCgYEA5MIL
  47. qWYBUuqbEWLRtMruUtpASclrBqNNsJEsMGbeqBJmoMxdHeSZckbLOrqm7GlMyNRJ
  48. Ne/5uWRGSzaMYuGmwsPpERzqEvYFnSrpjW5YtXZ+JtxFXNVfm9Z1gLLgvGpOUCIU
  49. RbpoDckDe1vgUuk3y5+DjZihs+rqIJ45XzXTzBkCgYBWuf3segruJZy5rEKhTv+o
  50. JqeUvRn0jNYYKFpLBeyTVBrbie6GkbUGNIWbrK05pC+c3K9nosvzuRUOQQL1tJbd
  51. 4gA3oiD9U4bMFNr+BRTHyZ7OQBcIXdz3t1qhuHVKtnngIAN1p25uPlbRFUNpshnt
  52. jgeVoHlsBhApcs5DUc+pyQKBgDzeHPg/+g4z+nrPznjKnktRY1W+0El93kgi+J0Q
  53. YiJacxBKEGTJ1MKBb8X6sDurcRDm22wMpGfd9I5Cv2v4GsUsF7HD/cx5xdih+G73
  54. c4clNj/k0Ff5Nm1izPUno4C+0IOl7br39IPmfpSuR6wH/h6iHQDqIeybjxyKvT1G
  55. N0rRAoGBAKGD+4ZI/E1MoJ5CXB8cDDMHagbE3cq/DtmYzE2v1DFpQYu5I4PCm5c7
  56. EQeIP6dZtv8IMgtGIb91QX9pXvP0aznzQKwYIA8nZgoENCPfiMTPiEDT9e/0lObO
  57. 9XWsXpbSTsRPj0sv1rB+UzBJ0PgjK4q2zOF0sNo7b1+6nlM3BWPx
  58. -----END RSA PRIVATE KEY-----
  59. `
  60. // This thumbprint is for the testKey defined above.
  61. testKeyThumbprint = "6nicxzh6WETQlrvdchkz-U3e3DOQZ4heJKU63rfqMqQ"
  62. // openssl ecparam -name secp256k1 -genkey -noout
  63. testKeyECPEM = `
  64. -----BEGIN EC PRIVATE KEY-----
  65. MHcCAQEEIK07hGLr0RwyUdYJ8wbIiBS55CjnkMD23DWr+ccnypWLoAoGCCqGSM49
  66. AwEHoUQDQgAE5lhEug5xK4xBDZ2nAbaxLtaLiv85bxJ7ePd1dkO23HThqIrvawF5
  67. QAaS/RNouybCiRhRjI3EaxLkQwgrCw0gqQ==
  68. -----END EC PRIVATE KEY-----
  69. `
  70. // openssl ecparam -name secp384r1 -genkey -noout
  71. testKeyEC384PEM = `
  72. -----BEGIN EC PRIVATE KEY-----
  73. MIGkAgEBBDAQ4lNtXRORWr1bgKR1CGysr9AJ9SyEk4jiVnlUWWUChmSNL+i9SLSD
  74. Oe/naPqXJ6CgBwYFK4EEACKhZANiAAQzKtj+Ms0vHoTX5dzv3/L5YMXOWuI5UKRj
  75. JigpahYCqXD2BA1j0E/2xt5vlPf+gm0PL+UHSQsCokGnIGuaHCsJAp3ry0gHQEke
  76. WYXapUUFdvaK1R2/2hn5O+eiQM8YzCg=
  77. -----END EC PRIVATE KEY-----
  78. `
  79. // openssl ecparam -name secp521r1 -genkey -noout
  80. testKeyEC512PEM = `
  81. -----BEGIN EC PRIVATE KEY-----
  82. MIHcAgEBBEIBSNZKFcWzXzB/aJClAb305ibalKgtDA7+70eEkdPt28/3LZMM935Z
  83. KqYHh/COcxuu3Kt8azRAUz3gyr4zZKhlKUSgBwYFK4EEACOhgYkDgYYABAHUNKbx
  84. 7JwC7H6pa2sV0tERWhHhB3JmW+OP6SUgMWryvIKajlx73eS24dy4QPGrWO9/ABsD
  85. FqcRSkNVTXnIv6+0mAF25knqIBIg5Q8M9BnOu9GGAchcwt3O7RDHmqewnJJDrbjd
  86. GGnm6rb+NnWR9DIopM0nKNkToWoF/hzopxu4Ae/GsQ==
  87. -----END EC PRIVATE KEY-----
  88. `
  89. // 1. openssl ec -in key.pem -noout -text
  90. // 2. remove first byte, 04 (the header); the rest is X and Y
  91. // 3. convert each with: echo <val> | xxd -r -p | b64raw
  92. testKeyECPubX = "5lhEug5xK4xBDZ2nAbaxLtaLiv85bxJ7ePd1dkO23HQ"
  93. testKeyECPubY = "4aiK72sBeUAGkv0TaLsmwokYUYyNxGsS5EMIKwsNIKk"
  94. testKeyEC384PubX = "MyrY_jLNLx6E1-Xc79_y-WDFzlriOVCkYyYoKWoWAqlw9gQNY9BP9sbeb5T3_oJt"
  95. testKeyEC384PubY = "Dy_lB0kLAqJBpyBrmhwrCQKd68tIB0BJHlmF2qVFBXb2itUdv9oZ-TvnokDPGMwo"
  96. testKeyEC512PubX = "AdQ0pvHsnALsfqlraxXS0RFaEeEHcmZb44_pJSAxavK8gpqOXHvd5Lbh3LhA8atY738AGwMWpxFKQ1VNeci_r7SY"
  97. testKeyEC512PubY = "AXbmSeogEiDlDwz0Gc670YYByFzC3c7tEMeap7CckkOtuN0Yaebqtv42dZH0MiikzSco2ROhagX-HOinG7gB78ax"
  98. // echo -n '{"crv":"P-256","kty":"EC","x":"<testKeyECPubX>","y":"<testKeyECPubY>"}' | \
  99. // openssl dgst -binary -sha256 | b64raw
  100. testKeyECThumbprint = "zedj-Bd1Zshp8KLePv2MB-lJ_Hagp7wAwdkA0NUTniU"
  101. )
  102. var (
  103. testKey *rsa.PrivateKey
  104. testKeyEC *ecdsa.PrivateKey
  105. testKeyEC384 *ecdsa.PrivateKey
  106. testKeyEC512 *ecdsa.PrivateKey
  107. )
  108. func init() {
  109. testKey = parseRSA(testKeyPEM, "testKeyPEM")
  110. testKeyEC = parseEC(testKeyECPEM, "testKeyECPEM")
  111. testKeyEC384 = parseEC(testKeyEC384PEM, "testKeyEC384PEM")
  112. testKeyEC512 = parseEC(testKeyEC512PEM, "testKeyEC512PEM")
  113. }
  114. func decodePEM(s, name string) []byte {
  115. d, _ := pem.Decode([]byte(s))
  116. if d == nil {
  117. panic("no block found in " + name)
  118. }
  119. return d.Bytes
  120. }
  121. func parseRSA(s, name string) *rsa.PrivateKey {
  122. b := decodePEM(s, name)
  123. k, err := x509.ParsePKCS1PrivateKey(b)
  124. if err != nil {
  125. panic(fmt.Sprintf("%s: %v", name, err))
  126. }
  127. return k
  128. }
  129. func parseEC(s, name string) *ecdsa.PrivateKey {
  130. b := decodePEM(s, name)
  131. k, err := x509.ParseECPrivateKey(b)
  132. if err != nil {
  133. panic(fmt.Sprintf("%s: %v", name, err))
  134. }
  135. return k
  136. }
  137. func TestJWSEncodeJSON(t *testing.T) {
  138. claims := struct{ Msg string }{"Hello JWS"}
  139. // JWS signed with testKey and "nonce" as the nonce value
  140. // JSON-serialized JWS fields are split for easier testing
  141. const (
  142. // {"alg":"RS256","jwk":{"e":"AQAB","kty":"RSA","n":"..."},"nonce":"nonce","url":"url"}
  143. protected = "eyJhbGciOiJSUzI1NiIsImp3ayI6eyJlIjoiQVFBQiIsImt0eSI6" +
  144. "IlJTQSIsIm4iOiI0eGdaM2VSUGt3b1J2eTdxZVJVYm1NRGUwVi14" +
  145. "SDllV0xkdTBpaGVlTGxybUQybXFXWGZQOUllU0tBcGJuMzRnOFR1" +
  146. "QVM5ZzV6aHE4RUxRM2ttanItS1Y4NkdBTWdJNlZBY0dscTNRcnpw" +
  147. "VENmXzMwQWI3LXphd3JmUmFGT05hMUh3RXpQWTFLSG5HVmt4SmM4" +
  148. "NWdOa3dZSTlTWTJSSFh0dmxuM3pzNXdJVE5yZG9zcUVYZWFJa1ZZ" +
  149. "QkVoYmhOdTU0cHAza3hvNlR1V0xpOWU2cFhlV2V0RXdtbEJ3dFda" +
  150. "bFBvaWIyajNUeExCa3NLWmZveUZ5ZWszODBtSGdKQXVtUV9JMmZq" +
  151. "ajk4Xzk3bWszaWhPWTRBZ1ZkQ0RqMXpfR0NvWmtHNVJxN25iQ0d5" +
  152. "b3N5S1d5RFgwMFpzLW5OcVZob0xlSXZYQzRubldkSk1aNnJvZ3h5" +
  153. "UVEifSwibm9uY2UiOiJub25jZSIsInVybCI6InVybCJ9"
  154. // {"Msg":"Hello JWS"}
  155. payload = "eyJNc2ciOiJIZWxsbyBKV1MifQ"
  156. // printf '<protected>.<payload>' | openssl dgst -binary -sha256 -sign testKey | b64raw
  157. signature = "YFyl_xz1E7TR-3E1bIuASTr424EgCvBHjt25WUFC2VaDjXYV0Rj_" +
  158. "Hd3dJ_2IRqBrXDZZ2n4ZeA_4mm3QFwmwyeDwe2sWElhb82lCZ8iX" +
  159. "uFnjeOmSOjx-nWwPa5ibCXzLq13zZ-OBV1Z4oN_TuailQeRoSfA3" +
  160. "nO8gG52mv1x2OMQ5MAFtt8jcngBLzts4AyhI6mBJ2w7Yaj3ZCriq" +
  161. "DWA3GLFvvHdW1Ba9Z01wtGT2CuZI7DUk_6Qj1b3BkBGcoKur5C9i" +
  162. "bUJtCkABwBMvBQNyD3MmXsrRFRTgvVlyU_yMaucYm7nmzEr_2PaQ" +
  163. "50rFt_9qOfJ4sfbLtG1Wwae57BQx1g"
  164. )
  165. b, err := jwsEncodeJSON(claims, testKey, noKeyID, "nonce", "url")
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. var jws struct{ Protected, Payload, Signature string }
  170. if err := json.Unmarshal(b, &jws); err != nil {
  171. t.Fatal(err)
  172. }
  173. if jws.Protected != protected {
  174. t.Errorf("protected:\n%s\nwant:\n%s", jws.Protected, protected)
  175. }
  176. if jws.Payload != payload {
  177. t.Errorf("payload:\n%s\nwant:\n%s", jws.Payload, payload)
  178. }
  179. if jws.Signature != signature {
  180. t.Errorf("signature:\n%s\nwant:\n%s", jws.Signature, signature)
  181. }
  182. }
  183. func TestJWSEncodeKID(t *testing.T) {
  184. kid := keyID("https://example.org/account/1")
  185. claims := struct{ Msg string }{"Hello JWS"}
  186. // JWS signed with testKeyEC
  187. const (
  188. // {"alg":"ES256","kid":"https://example.org/account/1","nonce":"nonce","url":"url"}
  189. protected = "eyJhbGciOiJFUzI1NiIsImtpZCI6Imh0dHBzOi8vZXhhbXBsZS5" +
  190. "vcmcvYWNjb3VudC8xIiwibm9uY2UiOiJub25jZSIsInVybCI6InVybCJ9"
  191. // {"Msg":"Hello JWS"}
  192. payload = "eyJNc2ciOiJIZWxsbyBKV1MifQ"
  193. )
  194. b, err := jwsEncodeJSON(claims, testKeyEC, kid, "nonce", "url")
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. var jws struct{ Protected, Payload, Signature string }
  199. if err := json.Unmarshal(b, &jws); err != nil {
  200. t.Fatal(err)
  201. }
  202. if jws.Protected != protected {
  203. t.Errorf("protected:\n%s\nwant:\n%s", jws.Protected, protected)
  204. }
  205. if jws.Payload != payload {
  206. t.Errorf("payload:\n%s\nwant:\n%s", jws.Payload, payload)
  207. }
  208. sig, err := base64.RawURLEncoding.DecodeString(jws.Signature)
  209. if err != nil {
  210. t.Fatalf("jws.Signature: %v", err)
  211. }
  212. r, s := big.NewInt(0), big.NewInt(0)
  213. r.SetBytes(sig[:len(sig)/2])
  214. s.SetBytes(sig[len(sig)/2:])
  215. h := sha256.Sum256([]byte(protected + "." + payload))
  216. if !ecdsa.Verify(testKeyEC.Public().(*ecdsa.PublicKey), h[:], r, s) {
  217. t.Error("invalid signature")
  218. }
  219. }
  220. func TestJWSEncodeJSONEC(t *testing.T) {
  221. tt := []struct {
  222. key *ecdsa.PrivateKey
  223. x, y string
  224. alg, crv string
  225. }{
  226. {testKeyEC, testKeyECPubX, testKeyECPubY, "ES256", "P-256"},
  227. {testKeyEC384, testKeyEC384PubX, testKeyEC384PubY, "ES384", "P-384"},
  228. {testKeyEC512, testKeyEC512PubX, testKeyEC512PubY, "ES512", "P-521"},
  229. }
  230. for i, test := range tt {
  231. claims := struct{ Msg string }{"Hello JWS"}
  232. b, err := jwsEncodeJSON(claims, test.key, noKeyID, "nonce", "url")
  233. if err != nil {
  234. t.Errorf("%d: %v", i, err)
  235. continue
  236. }
  237. var jws struct{ Protected, Payload, Signature string }
  238. if err := json.Unmarshal(b, &jws); err != nil {
  239. t.Errorf("%d: %v", i, err)
  240. continue
  241. }
  242. b, err = base64.RawURLEncoding.DecodeString(jws.Protected)
  243. if err != nil {
  244. t.Errorf("%d: jws.Protected: %v", i, err)
  245. }
  246. var head struct {
  247. Alg string
  248. Nonce string
  249. URL string `json:"url"`
  250. KID string `json:"kid"`
  251. JWK struct {
  252. Crv string
  253. Kty string
  254. X string
  255. Y string
  256. } `json:"jwk"`
  257. }
  258. if err := json.Unmarshal(b, &head); err != nil {
  259. t.Errorf("%d: jws.Protected: %v", i, err)
  260. }
  261. if head.Alg != test.alg {
  262. t.Errorf("%d: head.Alg = %q; want %q", i, head.Alg, test.alg)
  263. }
  264. if head.Nonce != "nonce" {
  265. t.Errorf("%d: head.Nonce = %q; want nonce", i, head.Nonce)
  266. }
  267. if head.URL != "url" {
  268. t.Errorf("%d: head.URL = %q; want 'url'", i, head.URL)
  269. }
  270. if head.KID != "" {
  271. // We used noKeyID in jwsEncodeJSON: expect no kid value.
  272. t.Errorf("%d: head.KID = %q; want empty", i, head.KID)
  273. }
  274. if head.JWK.Crv != test.crv {
  275. t.Errorf("%d: head.JWK.Crv = %q; want %q", i, head.JWK.Crv, test.crv)
  276. }
  277. if head.JWK.Kty != "EC" {
  278. t.Errorf("%d: head.JWK.Kty = %q; want EC", i, head.JWK.Kty)
  279. }
  280. if head.JWK.X != test.x {
  281. t.Errorf("%d: head.JWK.X = %q; want %q", i, head.JWK.X, test.x)
  282. }
  283. if head.JWK.Y != test.y {
  284. t.Errorf("%d: head.JWK.Y = %q; want %q", i, head.JWK.Y, test.y)
  285. }
  286. }
  287. }
  288. type customTestSigner struct {
  289. sig []byte
  290. pub crypto.PublicKey
  291. }
  292. func (s *customTestSigner) Public() crypto.PublicKey { return s.pub }
  293. func (s *customTestSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
  294. return s.sig, nil
  295. }
  296. func TestJWSEncodeJSONCustom(t *testing.T) {
  297. claims := struct{ Msg string }{"hello"}
  298. const (
  299. // printf '{"Msg":"hello"}' | b64raw
  300. payload = "eyJNc2ciOiJoZWxsbyJ9"
  301. // printf 'testsig' | b64raw
  302. testsig = "dGVzdHNpZw"
  303. // printf '{"alg":"ES256","jwk":{"crv":"P-256","kty":"EC","x":<testKeyECPubY>,"y":<testKeyECPubY>},"nonce":"nonce","url":"url"}' | b64raw
  304. es256phead = "eyJhbGciOiJFUzI1NiIsImp3ayI6eyJjcnYiOiJQLTI1NiIsImt0" +
  305. "eSI6IkVDIiwieCI6IjVsaEV1ZzV4SzR4QkRaMm5BYmF4THRhTGl2" +
  306. "ODVieEo3ZVBkMWRrTzIzSFEiLCJ5IjoiNGFpSzcyc0JlVUFHa3Yw" +
  307. "VGFMc213b2tZVVl5TnhHc1M1RU1JS3dzTklLayJ9LCJub25jZSI6" +
  308. "Im5vbmNlIiwidXJsIjoidXJsIn0"
  309. // {"alg":"RS256","jwk":{"e":"AQAB","kty":"RSA","n":"..."},"nonce":"nonce","url":"url"}
  310. rs256phead = "eyJhbGciOiJSUzI1NiIsImp3ayI6eyJlIjoiQVFBQiIsImt0eSI6" +
  311. "IlJTQSIsIm4iOiI0eGdaM2VSUGt3b1J2eTdxZVJVYm1NRGUwVi14" +
  312. "SDllV0xkdTBpaGVlTGxybUQybXFXWGZQOUllU0tBcGJuMzRnOFR1" +
  313. "QVM5ZzV6aHE4RUxRM2ttanItS1Y4NkdBTWdJNlZBY0dscTNRcnpw" +
  314. "VENmXzMwQWI3LXphd3JmUmFGT05hMUh3RXpQWTFLSG5HVmt4SmM4" +
  315. "NWdOa3dZSTlTWTJSSFh0dmxuM3pzNXdJVE5yZG9zcUVYZWFJa1ZZ" +
  316. "QkVoYmhOdTU0cHAza3hvNlR1V0xpOWU2cFhlV2V0RXdtbEJ3dFda" +
  317. "bFBvaWIyajNUeExCa3NLWmZveUZ5ZWszODBtSGdKQXVtUV9JMmZq" +
  318. "ajk4Xzk3bWszaWhPWTRBZ1ZkQ0RqMXpfR0NvWmtHNVJxN25iQ0d5" +
  319. "b3N5S1d5RFgwMFpzLW5OcVZob0xlSXZYQzRubldkSk1aNnJvZ3h5" +
  320. "UVEifSwibm9uY2UiOiJub25jZSIsInVybCI6InVybCJ9"
  321. )
  322. tt := []struct {
  323. alg, phead string
  324. pub crypto.PublicKey
  325. }{
  326. {"ES256", es256phead, testKeyEC.Public()},
  327. {"RS256", rs256phead, testKey.Public()},
  328. }
  329. for _, tc := range tt {
  330. tc := tc
  331. t.Run(tc.alg, func(t *testing.T) {
  332. signer := &customTestSigner{
  333. sig: []byte("testsig"),
  334. pub: tc.pub,
  335. }
  336. b, err := jwsEncodeJSON(claims, signer, noKeyID, "nonce", "url")
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. var j struct{ Protected, Payload, Signature string }
  341. if err := json.Unmarshal(b, &j); err != nil {
  342. t.Fatal(err)
  343. }
  344. if j.Protected != tc.phead {
  345. t.Errorf("j.Protected = %q\nwant %q", j.Protected, tc.phead)
  346. }
  347. if j.Payload != payload {
  348. t.Errorf("j.Payload = %q\nwant %q", j.Payload, payload)
  349. }
  350. if j.Signature != testsig {
  351. t.Errorf("j.Signature = %q\nwant %q", j.Signature, testsig)
  352. }
  353. })
  354. }
  355. }
  356. func TestJWKThumbprintRSA(t *testing.T) {
  357. // Key example from RFC 7638
  358. const base64N = "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAt" +
  359. "VT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn6" +
  360. "4tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FD" +
  361. "W2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n9" +
  362. "1CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINH" +
  363. "aQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw"
  364. const base64E = "AQAB"
  365. const expected = "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs"
  366. b, err := base64.RawURLEncoding.DecodeString(base64N)
  367. if err != nil {
  368. t.Fatalf("Error parsing example key N: %v", err)
  369. }
  370. n := new(big.Int).SetBytes(b)
  371. b, err = base64.RawURLEncoding.DecodeString(base64E)
  372. if err != nil {
  373. t.Fatalf("Error parsing example key E: %v", err)
  374. }
  375. e := new(big.Int).SetBytes(b)
  376. pub := &rsa.PublicKey{N: n, E: int(e.Uint64())}
  377. th, err := JWKThumbprint(pub)
  378. if err != nil {
  379. t.Error(err)
  380. }
  381. if th != expected {
  382. t.Errorf("thumbprint = %q; want %q", th, expected)
  383. }
  384. }
  385. func TestJWKThumbprintEC(t *testing.T) {
  386. // Key example from RFC 7520
  387. // expected was computed with
  388. // printf '{"crv":"P-521","kty":"EC","x":"<base64X>","y":"<base64Y>"}' | \
  389. // openssl dgst -binary -sha256 | b64raw
  390. const (
  391. base64X = "AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkT" +
  392. "KqjqvjyekWF-7ytDyRXYgCF5cj0Kt"
  393. base64Y = "AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUda" +
  394. "QkAgDPrwQrJmbnX9cwlGfP-HqHZR1"
  395. expected = "dHri3SADZkrush5HU_50AoRhcKFryN-PI6jPBtPL55M"
  396. )
  397. b, err := base64.RawURLEncoding.DecodeString(base64X)
  398. if err != nil {
  399. t.Fatalf("Error parsing example key X: %v", err)
  400. }
  401. x := new(big.Int).SetBytes(b)
  402. b, err = base64.RawURLEncoding.DecodeString(base64Y)
  403. if err != nil {
  404. t.Fatalf("Error parsing example key Y: %v", err)
  405. }
  406. y := new(big.Int).SetBytes(b)
  407. pub := &ecdsa.PublicKey{Curve: elliptic.P521(), X: x, Y: y}
  408. th, err := JWKThumbprint(pub)
  409. if err != nil {
  410. t.Error(err)
  411. }
  412. if th != expected {
  413. t.Errorf("thumbprint = %q; want %q", th, expected)
  414. }
  415. }
  416. func TestJWKThumbprintErrUnsupportedKey(t *testing.T) {
  417. _, err := JWKThumbprint(struct{}{})
  418. if err != ErrUnsupportedKey {
  419. t.Errorf("err = %q; want %q", err, ErrUnsupportedKey)
  420. }
  421. }