| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package jose |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/elliptic" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
type KeyAlgorithm string |
| 27 |
|
| 28 |
|
| 29 |
type SignatureAlgorithm string |
| 30 |
|
| 31 |
|
| 32 |
type ContentEncryption string |
| 33 |
|
| 34 |
|
| 35 |
type CompressionAlgorithm string |
| 36 |
|
| 37 |
var ( |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ErrCryptoFailure = errors.New("square/go-jose: error in cryptographic primitive") |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ErrUnsupportedAlgorithm = errors.New("square/go-jose: unknown/unsupported algorithm") |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
ErrUnsupportedKeyType = errors.New("square/go-jose: unsupported key type/format") |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
ErrNotSupported = errors.New("square/go-jose: compact serialization not supported for object") |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
ErrUnprotectedNonce = errors.New("square/go-jose: Nonce parameter included in unprotected header") |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
const ( |
| 66 |
RSA1_5 = KeyAlgorithm("RSA1_5") |
| 67 |
RSA_OAEP = KeyAlgorithm("RSA-OAEP") |
| 68 |
RSA_OAEP_256 = KeyAlgorithm("RSA-OAEP-256") |
| 69 |
A128KW = KeyAlgorithm("A128KW") |
| 70 |
A192KW = KeyAlgorithm("A192KW") |
| 71 |
A256KW = KeyAlgorithm("A256KW") |
| 72 |
DIRECT = KeyAlgorithm("dir") |
| 73 |
ECDH_ES = KeyAlgorithm("ECDH-ES") |
| 74 |
ECDH_ES_A128KW = KeyAlgorithm("ECDH-ES+A128KW") |
| 75 |
ECDH_ES_A192KW = KeyAlgorithm("ECDH-ES+A192KW") |
| 76 |
ECDH_ES_A256KW = KeyAlgorithm("ECDH-ES+A256KW") |
| 77 |
A128GCMKW = KeyAlgorithm("A128GCMKW") |
| 78 |
A192GCMKW = KeyAlgorithm("A192GCMKW") |
| 79 |
A256GCMKW = KeyAlgorithm("A256GCMKW") |
| 80 |
PBES2_HS256_A128KW = KeyAlgorithm("PBES2-HS256+A128KW") |
| 81 |
PBES2_HS384_A192KW = KeyAlgorithm("PBES2-HS384+A192KW") |
| 82 |
PBES2_HS512_A256KW = KeyAlgorithm("PBES2-HS512+A256KW") |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
const ( |
| 87 |
HS256 = SignatureAlgorithm("HS256") |
| 88 |
HS384 = SignatureAlgorithm("HS384") |
| 89 |
HS512 = SignatureAlgorithm("HS512") |
| 90 |
RS256 = SignatureAlgorithm("RS256") |
| 91 |
RS384 = SignatureAlgorithm("RS384") |
| 92 |
RS512 = SignatureAlgorithm("RS512") |
| 93 |
ES256 = SignatureAlgorithm("ES256") |
| 94 |
ES384 = SignatureAlgorithm("ES384") |
| 95 |
ES512 = SignatureAlgorithm("ES512") |
| 96 |
PS256 = SignatureAlgorithm("PS256") |
| 97 |
PS384 = SignatureAlgorithm("PS384") |
| 98 |
PS512 = SignatureAlgorithm("PS512") |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
const ( |
| 103 |
A128CBC_HS256 = ContentEncryption("A128CBC-HS256") |
| 104 |
A192CBC_HS384 = ContentEncryption("A192CBC-HS384") |
| 105 |
A256CBC_HS512 = ContentEncryption("A256CBC-HS512") |
| 106 |
A128GCM = ContentEncryption("A128GCM") |
| 107 |
A192GCM = ContentEncryption("A192GCM") |
| 108 |
A256GCM = ContentEncryption("A256GCM") |
| 109 |
) |
| 110 |
|
| 111 |
|
| 112 |
const ( |
| 113 |
NONE = CompressionAlgorithm("") |
| 114 |
DEFLATE = CompressionAlgorithm("DEF") |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
type rawHeader struct { |
| 119 |
Alg string `json:"alg,omitempty"` |
| 120 |
Enc ContentEncryption `json:"enc,omitempty"` |
| 121 |
Zip CompressionAlgorithm `json:"zip,omitempty"` |
| 122 |
Crit []string `json:"crit,omitempty"` |
| 123 |
Apu *byteBuffer `json:"apu,omitempty"` |
| 124 |
Apv *byteBuffer `json:"apv,omitempty"` |
| 125 |
Epk *JsonWebKey `json:"epk,omitempty"` |
| 126 |
Iv *byteBuffer `json:"iv,omitempty"` |
| 127 |
Tag *byteBuffer `json:"tag,omitempty"` |
| 128 |
Jwk *JsonWebKey `json:"jwk,omitempty"` |
| 129 |
Kid string `json:"kid,omitempty"` |
| 130 |
Nonce string `json:"nonce,omitempty"` |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
type JoseHeader struct { |
| 135 |
KeyID string |
| 136 |
JsonWebKey *JsonWebKey |
| 137 |
Algorithm string |
| 138 |
Nonce string |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (parsed rawHeader) sanitized() JoseHeader { |
| 143 |
return JoseHeader{ |
| 144 |
KeyID: parsed.Kid, |
| 145 |
JsonWebKey: parsed.Jwk, |
| 146 |
Algorithm: parsed.Alg, |
| 147 |
Nonce: parsed.Nonce, |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func (dst *rawHeader) merge(src *rawHeader) { |
| 153 |
if src == nil { |
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
if dst.Alg == "" { |
| 158 |
dst.Alg = src.Alg |
| 159 |
} |
| 160 |
if dst.Enc == "" { |
| 161 |
dst.Enc = src.Enc |
| 162 |
} |
| 163 |
if dst.Zip == "" { |
| 164 |
dst.Zip = src.Zip |
| 165 |
} |
| 166 |
if dst.Crit == nil { |
| 167 |
dst.Crit = src.Crit |
| 168 |
} |
| 169 |
if dst.Crit == nil { |
| 170 |
dst.Crit = src.Crit |
| 171 |
} |
| 172 |
if dst.Apu == nil { |
| 173 |
dst.Apu = src.Apu |
| 174 |
} |
| 175 |
if dst.Apv == nil { |
| 176 |
dst.Apv = src.Apv |
| 177 |
} |
| 178 |
if dst.Epk == nil { |
| 179 |
dst.Epk = src.Epk |
| 180 |
} |
| 181 |
if dst.Iv == nil { |
| 182 |
dst.Iv = src.Iv |
| 183 |
} |
| 184 |
if dst.Tag == nil { |
| 185 |
dst.Tag = src.Tag |
| 186 |
} |
| 187 |
if dst.Kid == "" { |
| 188 |
dst.Kid = src.Kid |
| 189 |
} |
| 190 |
if dst.Jwk == nil { |
| 191 |
dst.Jwk = src.Jwk |
| 192 |
} |
| 193 |
if dst.Nonce == "" { |
| 194 |
dst.Nonce = src.Nonce |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
func curveName(crv elliptic.Curve) (string, error) { |
| 200 |
switch crv { |
| 201 |
case elliptic.P256(): |
| 202 |
return "P-256", nil |
| 203 |
case elliptic.P384(): |
| 204 |
return "P-384", nil |
| 205 |
case elliptic.P521(): |
| 206 |
return "P-521", nil |
| 207 |
default: |
| 208 |
return "", fmt.Errorf("square/go-jose: unsupported/unknown elliptic curve") |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func curveSize(crv elliptic.Curve) int { |
| 214 |
bits := crv.Params().BitSize |
| 215 |
|
| 216 |
div := bits / 8 |
| 217 |
mod := bits % 8 |
| 218 |
|
| 219 |
if mod == 0 { |
| 220 |
return div |
| 221 |
} |
| 222 |
|
| 223 |
return div + 1 |
| 224 |
} |