jwk.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*-
  2. * Copyright 2014 Square Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package jose
  17. import (
  18. "crypto"
  19. "crypto/ecdsa"
  20. "crypto/elliptic"
  21. "crypto/rsa"
  22. "crypto/x509"
  23. "encoding/base64"
  24. "errors"
  25. "fmt"
  26. "math/big"
  27. "reflect"
  28. "strings"
  29. "golang.org/x/crypto/ed25519"
  30. "gopkg.in/square/go-jose.v2/json"
  31. )
  32. // rawJSONWebKey represents a public or private key in JWK format, used for parsing/serializing.
  33. type rawJSONWebKey struct {
  34. Use string `json:"use,omitempty"`
  35. Kty string `json:"kty,omitempty"`
  36. Kid string `json:"kid,omitempty"`
  37. Crv string `json:"crv,omitempty"`
  38. Alg string `json:"alg,omitempty"`
  39. K *byteBuffer `json:"k,omitempty"`
  40. X *byteBuffer `json:"x,omitempty"`
  41. Y *byteBuffer `json:"y,omitempty"`
  42. N *byteBuffer `json:"n,omitempty"`
  43. E *byteBuffer `json:"e,omitempty"`
  44. // -- Following fields are only used for private keys --
  45. // RSA uses D, P and Q, while ECDSA uses only D. Fields Dp, Dq, and Qi are
  46. // completely optional. Therefore for RSA/ECDSA, D != nil is a contract that
  47. // we have a private key whereas D == nil means we have only a public key.
  48. D *byteBuffer `json:"d,omitempty"`
  49. P *byteBuffer `json:"p,omitempty"`
  50. Q *byteBuffer `json:"q,omitempty"`
  51. Dp *byteBuffer `json:"dp,omitempty"`
  52. Dq *byteBuffer `json:"dq,omitempty"`
  53. Qi *byteBuffer `json:"qi,omitempty"`
  54. // Certificates
  55. X5c []string `json:"x5c,omitempty"`
  56. }
  57. // JSONWebKey represents a public or private key in JWK format.
  58. type JSONWebKey struct {
  59. Key interface{}
  60. Certificates []*x509.Certificate
  61. KeyID string
  62. Algorithm string
  63. Use string
  64. }
  65. // MarshalJSON serializes the given key to its JSON representation.
  66. func (k JSONWebKey) MarshalJSON() ([]byte, error) {
  67. var raw *rawJSONWebKey
  68. var err error
  69. switch key := k.Key.(type) {
  70. case ed25519.PublicKey:
  71. raw = fromEdPublicKey(key)
  72. case *ecdsa.PublicKey:
  73. raw, err = fromEcPublicKey(key)
  74. case *rsa.PublicKey:
  75. raw = fromRsaPublicKey(key)
  76. case ed25519.PrivateKey:
  77. raw, err = fromEdPrivateKey(key)
  78. case *ecdsa.PrivateKey:
  79. raw, err = fromEcPrivateKey(key)
  80. case *rsa.PrivateKey:
  81. raw, err = fromRsaPrivateKey(key)
  82. case []byte:
  83. raw, err = fromSymmetricKey(key)
  84. default:
  85. return nil, fmt.Errorf("square/go-jose: unknown key type '%s'", reflect.TypeOf(key))
  86. }
  87. if err != nil {
  88. return nil, err
  89. }
  90. raw.Kid = k.KeyID
  91. raw.Alg = k.Algorithm
  92. raw.Use = k.Use
  93. for _, cert := range k.Certificates {
  94. raw.X5c = append(raw.X5c, base64.StdEncoding.EncodeToString(cert.Raw))
  95. }
  96. return json.Marshal(raw)
  97. }
  98. // UnmarshalJSON reads a key from its JSON representation.
  99. func (k *JSONWebKey) UnmarshalJSON(data []byte) (err error) {
  100. var raw rawJSONWebKey
  101. err = json.Unmarshal(data, &raw)
  102. if err != nil {
  103. return err
  104. }
  105. var key interface{}
  106. switch raw.Kty {
  107. case "EC":
  108. if raw.D != nil {
  109. key, err = raw.ecPrivateKey()
  110. } else {
  111. key, err = raw.ecPublicKey()
  112. }
  113. case "RSA":
  114. if raw.D != nil {
  115. key, err = raw.rsaPrivateKey()
  116. } else {
  117. key, err = raw.rsaPublicKey()
  118. }
  119. case "oct":
  120. key, err = raw.symmetricKey()
  121. case "OKP":
  122. if raw.Crv == "Ed25519" && raw.X != nil {
  123. if raw.D != nil {
  124. key, err = raw.edPrivateKey()
  125. } else {
  126. key, err = raw.edPublicKey()
  127. }
  128. } else {
  129. err = fmt.Errorf("square/go-jose: unknown curve %s'", raw.Crv)
  130. }
  131. default:
  132. err = fmt.Errorf("square/go-jose: unknown json web key type '%s'", raw.Kty)
  133. }
  134. if err == nil {
  135. *k = JSONWebKey{Key: key, KeyID: raw.Kid, Algorithm: raw.Alg, Use: raw.Use}
  136. k.Certificates, err = parseCertificateChain(raw.X5c)
  137. if err != nil {
  138. return fmt.Errorf("failed to unmarshal x5c field: %s", err)
  139. }
  140. }
  141. return
  142. }
  143. // JSONWebKeySet represents a JWK Set object.
  144. type JSONWebKeySet struct {
  145. Keys []JSONWebKey `json:"keys"`
  146. }
  147. // Key convenience method returns keys by key ID. Specification states
  148. // that a JWK Set "SHOULD" use distinct key IDs, but allows for some
  149. // cases where they are not distinct. Hence method returns a slice
  150. // of JSONWebKeys.
  151. func (s *JSONWebKeySet) Key(kid string) []JSONWebKey {
  152. var keys []JSONWebKey
  153. for _, key := range s.Keys {
  154. if key.KeyID == kid {
  155. keys = append(keys, key)
  156. }
  157. }
  158. return keys
  159. }
  160. const rsaThumbprintTemplate = `{"e":"%s","kty":"RSA","n":"%s"}`
  161. const ecThumbprintTemplate = `{"crv":"%s","kty":"EC","x":"%s","y":"%s"}`
  162. const edThumbprintTemplate = `{"crv":"%s","kty":"OKP",x":"%s"}`
  163. func ecThumbprintInput(curve elliptic.Curve, x, y *big.Int) (string, error) {
  164. coordLength := curveSize(curve)
  165. crv, err := curveName(curve)
  166. if err != nil {
  167. return "", err
  168. }
  169. if len(x.Bytes()) > coordLength || len(y.Bytes()) > coordLength {
  170. return "", errors.New("square/go-jose: invalid elliptic key (too large)")
  171. }
  172. return fmt.Sprintf(ecThumbprintTemplate, crv,
  173. newFixedSizeBuffer(x.Bytes(), coordLength).base64(),
  174. newFixedSizeBuffer(y.Bytes(), coordLength).base64()), nil
  175. }
  176. func rsaThumbprintInput(n *big.Int, e int) (string, error) {
  177. return fmt.Sprintf(rsaThumbprintTemplate,
  178. newBufferFromInt(uint64(e)).base64(),
  179. newBuffer(n.Bytes()).base64()), nil
  180. }
  181. func edThumbprintInput(ed ed25519.PublicKey) (string, error) {
  182. crv := "Ed25519"
  183. if len(ed) > 32 {
  184. return "", errors.New("square/go-jose: invalid elliptic key (too large)")
  185. }
  186. return fmt.Sprintf(edThumbprintTemplate, crv,
  187. newFixedSizeBuffer(ed, 32).base64()), nil
  188. }
  189. // Thumbprint computes the JWK Thumbprint of a key using the
  190. // indicated hash algorithm.
  191. func (k *JSONWebKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
  192. var input string
  193. var err error
  194. switch key := k.Key.(type) {
  195. case ed25519.PublicKey:
  196. input, err = edThumbprintInput(key)
  197. case *ecdsa.PublicKey:
  198. input, err = ecThumbprintInput(key.Curve, key.X, key.Y)
  199. case *ecdsa.PrivateKey:
  200. input, err = ecThumbprintInput(key.Curve, key.X, key.Y)
  201. case *rsa.PublicKey:
  202. input, err = rsaThumbprintInput(key.N, key.E)
  203. case *rsa.PrivateKey:
  204. input, err = rsaThumbprintInput(key.N, key.E)
  205. case ed25519.PrivateKey:
  206. input, err = edThumbprintInput(ed25519.PublicKey(key[32:]))
  207. default:
  208. return nil, fmt.Errorf("square/go-jose: unknown key type '%s'", reflect.TypeOf(key))
  209. }
  210. if err != nil {
  211. return nil, err
  212. }
  213. h := hash.New()
  214. h.Write([]byte(input))
  215. return h.Sum(nil), nil
  216. }
  217. // IsPublic returns true if the JWK represents a public key (not symmetric, not private).
  218. func (k *JSONWebKey) IsPublic() bool {
  219. switch k.Key.(type) {
  220. case *ecdsa.PublicKey, *rsa.PublicKey, ed25519.PublicKey:
  221. return true
  222. default:
  223. return false
  224. }
  225. }
  226. // Public creates JSONWebKey with corresponding publik key if JWK represents asymmetric private key.
  227. func (k *JSONWebKey) Public() JSONWebKey {
  228. if k.IsPublic() {
  229. return *k
  230. }
  231. ret := *k
  232. switch key := k.Key.(type) {
  233. case *ecdsa.PrivateKey:
  234. ret.Key = key.Public()
  235. case *rsa.PrivateKey:
  236. ret.Key = key.Public()
  237. case ed25519.PrivateKey:
  238. ret.Key = key.Public()
  239. default:
  240. return JSONWebKey{} // returning invalid key
  241. }
  242. return ret
  243. }
  244. // Valid checks that the key contains the expected parameters.
  245. func (k *JSONWebKey) Valid() bool {
  246. if k.Key == nil {
  247. return false
  248. }
  249. switch key := k.Key.(type) {
  250. case *ecdsa.PublicKey:
  251. if key.Curve == nil || key.X == nil || key.Y == nil {
  252. return false
  253. }
  254. case *ecdsa.PrivateKey:
  255. if key.Curve == nil || key.X == nil || key.Y == nil || key.D == nil {
  256. return false
  257. }
  258. case *rsa.PublicKey:
  259. if key.N == nil || key.E == 0 {
  260. return false
  261. }
  262. case *rsa.PrivateKey:
  263. if key.N == nil || key.E == 0 || key.D == nil || len(key.Primes) < 2 {
  264. return false
  265. }
  266. case ed25519.PublicKey:
  267. if len(key) != 32 {
  268. return false
  269. }
  270. case ed25519.PrivateKey:
  271. if len(key) != 64 {
  272. return false
  273. }
  274. default:
  275. return false
  276. }
  277. return true
  278. }
  279. func (key rawJSONWebKey) rsaPublicKey() (*rsa.PublicKey, error) {
  280. if key.N == nil || key.E == nil {
  281. return nil, fmt.Errorf("square/go-jose: invalid RSA key, missing n/e values")
  282. }
  283. return &rsa.PublicKey{
  284. N: key.N.bigInt(),
  285. E: key.E.toInt(),
  286. }, nil
  287. }
  288. func fromEdPublicKey(pub ed25519.PublicKey) *rawJSONWebKey {
  289. return &rawJSONWebKey{
  290. Kty: "OKP",
  291. Crv: "Ed25519",
  292. X: newBuffer(pub),
  293. }
  294. }
  295. func fromRsaPublicKey(pub *rsa.PublicKey) *rawJSONWebKey {
  296. return &rawJSONWebKey{
  297. Kty: "RSA",
  298. N: newBuffer(pub.N.Bytes()),
  299. E: newBufferFromInt(uint64(pub.E)),
  300. }
  301. }
  302. func (key rawJSONWebKey) ecPublicKey() (*ecdsa.PublicKey, error) {
  303. var curve elliptic.Curve
  304. switch key.Crv {
  305. case "P-256":
  306. curve = elliptic.P256()
  307. case "P-384":
  308. curve = elliptic.P384()
  309. case "P-521":
  310. curve = elliptic.P521()
  311. default:
  312. return nil, fmt.Errorf("square/go-jose: unsupported elliptic curve '%s'", key.Crv)
  313. }
  314. if key.X == nil || key.Y == nil {
  315. return nil, errors.New("square/go-jose: invalid EC key, missing x/y values")
  316. }
  317. // The length of this octet string MUST be the full size of a coordinate for
  318. // the curve specified in the "crv" parameter.
  319. // https://tools.ietf.org/html/rfc7518#section-6.2.1.2
  320. if curveSize(curve) != len(key.X.data) {
  321. return nil, fmt.Errorf("square/go-jose: invalid EC public key, wrong length for x")
  322. }
  323. if curveSize(curve) != len(key.Y.data) {
  324. return nil, fmt.Errorf("square/go-jose: invalid EC public key, wrong length for y")
  325. }
  326. x := key.X.bigInt()
  327. y := key.Y.bigInt()
  328. if !curve.IsOnCurve(x, y) {
  329. return nil, errors.New("square/go-jose: invalid EC key, X/Y are not on declared curve")
  330. }
  331. return &ecdsa.PublicKey{
  332. Curve: curve,
  333. X: x,
  334. Y: y,
  335. }, nil
  336. }
  337. func fromEcPublicKey(pub *ecdsa.PublicKey) (*rawJSONWebKey, error) {
  338. if pub == nil || pub.X == nil || pub.Y == nil {
  339. return nil, fmt.Errorf("square/go-jose: invalid EC key (nil, or X/Y missing)")
  340. }
  341. name, err := curveName(pub.Curve)
  342. if err != nil {
  343. return nil, err
  344. }
  345. size := curveSize(pub.Curve)
  346. xBytes := pub.X.Bytes()
  347. yBytes := pub.Y.Bytes()
  348. if len(xBytes) > size || len(yBytes) > size {
  349. return nil, fmt.Errorf("square/go-jose: invalid EC key (X/Y too large)")
  350. }
  351. key := &rawJSONWebKey{
  352. Kty: "EC",
  353. Crv: name,
  354. X: newFixedSizeBuffer(xBytes, size),
  355. Y: newFixedSizeBuffer(yBytes, size),
  356. }
  357. return key, nil
  358. }
  359. func (key rawJSONWebKey) edPrivateKey() (ed25519.PrivateKey, error) {
  360. var missing []string
  361. switch {
  362. case key.D == nil:
  363. missing = append(missing, "D")
  364. case key.X == nil:
  365. missing = append(missing, "X")
  366. }
  367. if len(missing) > 0 {
  368. return nil, fmt.Errorf("square/go-jose: invalid Ed25519 private key, missing %s value(s)", strings.Join(missing, ", "))
  369. }
  370. privateKey := make([]byte, ed25519.PrivateKeySize)
  371. copy(privateKey[0:32], key.D.bytes())
  372. copy(privateKey[32:], key.X.bytes())
  373. rv := ed25519.PrivateKey(privateKey)
  374. return rv, nil
  375. }
  376. func (key rawJSONWebKey) edPublicKey() (ed25519.PublicKey, error) {
  377. if key.X == nil {
  378. return nil, fmt.Errorf("square/go-jose: invalid Ed key, missing x value")
  379. }
  380. publicKey := make([]byte, ed25519.PublicKeySize)
  381. copy(publicKey[0:32], key.X.bytes())
  382. rv := ed25519.PublicKey(publicKey)
  383. return rv, nil
  384. }
  385. func (key rawJSONWebKey) rsaPrivateKey() (*rsa.PrivateKey, error) {
  386. var missing []string
  387. switch {
  388. case key.N == nil:
  389. missing = append(missing, "N")
  390. case key.E == nil:
  391. missing = append(missing, "E")
  392. case key.D == nil:
  393. missing = append(missing, "D")
  394. case key.P == nil:
  395. missing = append(missing, "P")
  396. case key.Q == nil:
  397. missing = append(missing, "Q")
  398. }
  399. if len(missing) > 0 {
  400. return nil, fmt.Errorf("square/go-jose: invalid RSA private key, missing %s value(s)", strings.Join(missing, ", "))
  401. }
  402. rv := &rsa.PrivateKey{
  403. PublicKey: rsa.PublicKey{
  404. N: key.N.bigInt(),
  405. E: key.E.toInt(),
  406. },
  407. D: key.D.bigInt(),
  408. Primes: []*big.Int{
  409. key.P.bigInt(),
  410. key.Q.bigInt(),
  411. },
  412. }
  413. if key.Dp != nil {
  414. rv.Precomputed.Dp = key.Dp.bigInt()
  415. }
  416. if key.Dq != nil {
  417. rv.Precomputed.Dq = key.Dq.bigInt()
  418. }
  419. if key.Qi != nil {
  420. rv.Precomputed.Qinv = key.Qi.bigInt()
  421. }
  422. err := rv.Validate()
  423. return rv, err
  424. }
  425. func fromEdPrivateKey(ed ed25519.PrivateKey) (*rawJSONWebKey, error) {
  426. raw := fromEdPublicKey(ed25519.PublicKey(ed[32:]))
  427. raw.D = newBuffer(ed[0:32])
  428. return raw, nil
  429. }
  430. func fromRsaPrivateKey(rsa *rsa.PrivateKey) (*rawJSONWebKey, error) {
  431. if len(rsa.Primes) != 2 {
  432. return nil, ErrUnsupportedKeyType
  433. }
  434. raw := fromRsaPublicKey(&rsa.PublicKey)
  435. raw.D = newBuffer(rsa.D.Bytes())
  436. raw.P = newBuffer(rsa.Primes[0].Bytes())
  437. raw.Q = newBuffer(rsa.Primes[1].Bytes())
  438. if rsa.Precomputed.Dp != nil {
  439. raw.Dp = newBuffer(rsa.Precomputed.Dp.Bytes())
  440. }
  441. if rsa.Precomputed.Dq != nil {
  442. raw.Dq = newBuffer(rsa.Precomputed.Dq.Bytes())
  443. }
  444. if rsa.Precomputed.Qinv != nil {
  445. raw.Qi = newBuffer(rsa.Precomputed.Qinv.Bytes())
  446. }
  447. return raw, nil
  448. }
  449. func (key rawJSONWebKey) ecPrivateKey() (*ecdsa.PrivateKey, error) {
  450. var curve elliptic.Curve
  451. switch key.Crv {
  452. case "P-256":
  453. curve = elliptic.P256()
  454. case "P-384":
  455. curve = elliptic.P384()
  456. case "P-521":
  457. curve = elliptic.P521()
  458. default:
  459. return nil, fmt.Errorf("square/go-jose: unsupported elliptic curve '%s'", key.Crv)
  460. }
  461. if key.X == nil || key.Y == nil || key.D == nil {
  462. return nil, fmt.Errorf("square/go-jose: invalid EC private key, missing x/y/d values")
  463. }
  464. // The length of this octet string MUST be the full size of a coordinate for
  465. // the curve specified in the "crv" parameter.
  466. // https://tools.ietf.org/html/rfc7518#section-6.2.1.2
  467. if curveSize(curve) != len(key.X.data) {
  468. return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for x")
  469. }
  470. if curveSize(curve) != len(key.Y.data) {
  471. return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for y")
  472. }
  473. // https://tools.ietf.org/html/rfc7518#section-6.2.2.1
  474. if dSize(curve) != len(key.D.data) {
  475. return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for d")
  476. }
  477. x := key.X.bigInt()
  478. y := key.Y.bigInt()
  479. if !curve.IsOnCurve(x, y) {
  480. return nil, errors.New("square/go-jose: invalid EC key, X/Y are not on declared curve")
  481. }
  482. return &ecdsa.PrivateKey{
  483. PublicKey: ecdsa.PublicKey{
  484. Curve: curve,
  485. X: x,
  486. Y: y,
  487. },
  488. D: key.D.bigInt(),
  489. }, nil
  490. }
  491. func fromEcPrivateKey(ec *ecdsa.PrivateKey) (*rawJSONWebKey, error) {
  492. raw, err := fromEcPublicKey(&ec.PublicKey)
  493. if err != nil {
  494. return nil, err
  495. }
  496. if ec.D == nil {
  497. return nil, fmt.Errorf("square/go-jose: invalid EC private key")
  498. }
  499. raw.D = newFixedSizeBuffer(ec.D.Bytes(), dSize(ec.PublicKey.Curve))
  500. return raw, nil
  501. }
  502. // dSize returns the size in octets for the "d" member of an elliptic curve
  503. // private key.
  504. // The length of this octet string MUST be ceiling(log-base-2(n)/8)
  505. // octets (where n is the order of the curve).
  506. // https://tools.ietf.org/html/rfc7518#section-6.2.2.1
  507. func dSize(curve elliptic.Curve) int {
  508. order := curve.Params().P
  509. bitLen := order.BitLen()
  510. size := bitLen / 8
  511. if bitLen%8 != 0 {
  512. size = size + 1
  513. }
  514. return size
  515. }
  516. func fromSymmetricKey(key []byte) (*rawJSONWebKey, error) {
  517. return &rawJSONWebKey{
  518. Kty: "oct",
  519. K: newBuffer(key),
  520. }, nil
  521. }
  522. func (key rawJSONWebKey) symmetricKey() ([]byte, error) {
  523. if key.K == nil {
  524. return nil, fmt.Errorf("square/go-jose: invalid OCT (symmetric) key, missing k value")
  525. }
  526. return key.K.bytes(), nil
  527. }