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.
13 _ "crypto/sha512" // need for EC keys
20 // jwsEncodeJSON signs claimset using provided key and a nonce.
21 // The result is serialized in JSON format.
22 // See https://tools.ietf.org/html/rfc7515#section-7.
23 func jwsEncodeJSON(claimset interface{}, key crypto.Signer, nonce string) ([]byte, error) {
24 jwk, err := jwkEncode(key.Public())
28 alg, sha := jwsHasher(key.Public())
29 if alg == "" || !sha.Available() {
30 return nil, ErrUnsupportedKey
32 phead := fmt.Sprintf(`{"alg":%q,"jwk":%s,"nonce":%q}`, alg, jwk, nonce)
33 phead = base64.RawURLEncoding.EncodeToString([]byte(phead))
34 cs, err := json.Marshal(claimset)
38 payload := base64.RawURLEncoding.EncodeToString(cs)
40 hash.Write([]byte(phead + "." + payload))
41 sig, err := jwsSign(key, sha, hash.Sum(nil))
47 Protected string `json:"protected"`
48 Payload string `json:"payload"`
49 Sig string `json:"signature"`
53 Sig: base64.RawURLEncoding.EncodeToString(sig),
55 return json.Marshal(&enc)
58 // jwkEncode encodes public part of an RSA or ECDSA key into a JWK.
59 // The result is also suitable for creating a JWK thumbprint.
60 // https://tools.ietf.org/html/rfc7517
61 func jwkEncode(pub crypto.PublicKey) (string, error) {
62 switch pub := pub.(type) {
64 // https://tools.ietf.org/html/rfc7518#section-6.3.1
66 e := big.NewInt(int64(pub.E))
67 // Field order is important.
68 // See https://tools.ietf.org/html/rfc7638#section-3.3 for details.
69 return fmt.Sprintf(`{"e":"%s","kty":"RSA","n":"%s"}`,
70 base64.RawURLEncoding.EncodeToString(e.Bytes()),
71 base64.RawURLEncoding.EncodeToString(n.Bytes()),
73 case *ecdsa.PublicKey:
74 // https://tools.ietf.org/html/rfc7518#section-6.2.1
75 p := pub.Curve.Params()
82 x = append(make([]byte, n-len(x)), x...)
86 y = append(make([]byte, n-len(y)), y...)
88 // Field order is important.
89 // See https://tools.ietf.org/html/rfc7638#section-3.3 for details.
90 return fmt.Sprintf(`{"crv":"%s","kty":"EC","x":"%s","y":"%s"}`,
92 base64.RawURLEncoding.EncodeToString(x),
93 base64.RawURLEncoding.EncodeToString(y),
96 return "", ErrUnsupportedKey
99 // jwsSign signs the digest using the given key.
100 // The hash is unused for ECDSA keys.
102 // Note: non-stdlib crypto.Signer implementations are expected to return
103 // the signature in the format as specified in RFC7518.
104 // See https://tools.ietf.org/html/rfc7518 for more details.
105 func jwsSign(key crypto.Signer, hash crypto.Hash, digest []byte) ([]byte, error) {
106 if key, ok := key.(*ecdsa.PrivateKey); ok {
107 // The key.Sign method of ecdsa returns ASN1-encoded signature.
108 // So, we use the package Sign function instead
109 // to get R and S values directly and format the result accordingly.
110 r, s, err := ecdsa.Sign(rand.Reader, key, digest)
114 rb, sb := r.Bytes(), s.Bytes()
115 size := key.Params().BitSize / 8
119 sig := make([]byte, size*2)
120 copy(sig[size-len(rb):], rb)
121 copy(sig[size*2-len(sb):], sb)
124 return key.Sign(rand.Reader, digest, hash)
127 // jwsHasher indicates suitable JWS algorithm name and a hash function
128 // to use for signing a digest with the provided key.
129 // It returns ("", 0) if the key is not supported.
130 func jwsHasher(pub crypto.PublicKey) (string, crypto.Hash) {
131 switch pub := pub.(type) {
133 return "RS256", crypto.SHA256
134 case *ecdsa.PublicKey:
135 switch pub.Params().Name {
137 return "ES256", crypto.SHA256
139 return "ES384", crypto.SHA384
141 return "ES512", crypto.SHA512
147 // JWKThumbprint creates a JWK thumbprint out of pub
148 // as specified in https://tools.ietf.org/html/rfc7638.
149 func JWKThumbprint(pub crypto.PublicKey) (string, error) {
150 jwk, err := jwkEncode(pub)
154 b := sha256.Sum256([]byte(jwk))
155 return base64.RawURLEncoding.EncodeToString(b[:]), nil