1// Copyright 2022 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15//////////////////////////////////////////////////////////////////////////////// 16 17package jwt 18 19import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "google.golang.org/protobuf/proto" 24 "github.com/google/tink/go/core/registry" 25 jepb "github.com/google/tink/go/proto/jwt_ecdsa_go_proto" 26 tinkpb "github.com/google/tink/go/proto/tink_go_proto" 27) 28 29const ( 30 testECDSASignerKeyType = "type.googleapis.com/google.crypto.tink.JwtEcdsaPrivateKey" 31 testECDSASignerVersion = 0 32) 33 34func TestECDSASignerDoesSupport(t *testing.T) { 35 km, err := registry.GetKeyManager(testECDSASignerKeyType) 36 if err != nil { 37 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 38 } 39 if !km.DoesSupport(testECDSASignerKeyType) { 40 t.Errorf("km.DoesSupport(%q) = false, want true", testECDSASignerKeyType) 41 } 42 if km.DoesSupport("not.the.actual.key.type") { 43 t.Errorf("km.DoesSupport('not.the.actual.key.type') = true, want false") 44 } 45} 46 47func TestECDSASignerTypeURL(t *testing.T) { 48 km, err := registry.GetKeyManager(testECDSASignerKeyType) 49 if err != nil { 50 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 51 } 52 if km.TypeURL() != testECDSASignerKeyType { 53 t.Errorf("km.TypeURL() = %q, want %q", km.TypeURL(), testECDSASignerKeyType) 54 } 55} 56 57func TestECDSASignerNewKeyWithEmptyKeyFormatFails(t *testing.T) { 58 km, err := registry.GetKeyManager(testECDSASignerKeyType) 59 if err != nil { 60 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 61 } 62 if _, err := km.NewKey(nil); err == nil { 63 t.Errorf("km.NewKey(nil) err = nil, want error") 64 } 65} 66 67func createECDSASerializedKeyFormat(algorithm jepb.JwtEcdsaAlgorithm, version uint32) ([]byte, error) { 68 kf := &jepb.JwtEcdsaKeyFormat{ 69 Version: version, 70 Algorithm: algorithm, 71 } 72 return proto.Marshal(kf) 73} 74 75func TestECDSASignerNewKeyWithInvalidAlgorithmFails(t *testing.T) { 76 km, err := registry.GetKeyManager(testECDSASignerKeyType) 77 if err != nil { 78 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 79 } 80 keyFormat, err := createECDSASerializedKeyFormat(jepb.JwtEcdsaAlgorithm_ES_UNKNOWN, testECDSASignerVersion) 81 if err != nil { 82 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 83 } 84 if _, err := km.NewKey(keyFormat); err == nil { 85 t.Errorf("km.NewKey(keyFormat) err = nil, want error") 86 } 87} 88 89func TestECDSASignerNewKeyGeneratesValidKey(t *testing.T) { 90 type testCase struct { 91 tag string 92 algorithm jepb.JwtEcdsaAlgorithm 93 } 94 for _, tc := range []testCase{ 95 { 96 tag: "ES256", 97 algorithm: jepb.JwtEcdsaAlgorithm_ES256, 98 }, 99 { 100 tag: "ES384", 101 algorithm: jepb.JwtEcdsaAlgorithm_ES384, 102 }, 103 { 104 tag: "ES521", 105 algorithm: jepb.JwtEcdsaAlgorithm_ES512, 106 }, 107 } { 108 t.Run(tc.tag, func(t *testing.T) { 109 km, err := registry.GetKeyManager(testECDSASignerKeyType) 110 if err != nil { 111 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 112 } 113 keyFormat, err := createECDSASerializedKeyFormat(tc.algorithm, testECDSASignerVersion) 114 if err != nil { 115 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 116 } 117 k, err := km.NewKey(keyFormat) 118 if err != nil { 119 t.Errorf("km.NewKey(keyFormat) err = %v, want nil", err) 120 } 121 key, ok := k.(*jepb.JwtEcdsaPrivateKey) 122 if !ok { 123 t.Errorf("key is not of type: *jepb.JwtEcdsaPrivateKey") 124 } 125 pubKey := key.GetPublicKey() 126 if pubKey == nil { 127 t.Errorf("pubKey = nil, want *jebp.JwtEcdsaPublicKey{}") 128 } 129 if pubKey.GetAlgorithm() != tc.algorithm { 130 t.Errorf("pubKey.GetAlgorithm() = %q, want %q", pubKey.GetAlgorithm(), tc.algorithm) 131 } 132 if pubKey.GetVersion() != testECDSASignerVersion { 133 t.Errorf("pubKey.GetVersion() = %d, want %d", pubKey.GetVersion(), testECDSASignerVersion) 134 } 135 }) 136 } 137} 138 139func TestECDSASignerNewKeyGeneratesDifferentKeys(t *testing.T) { 140 km, err := registry.GetKeyManager(testECDSASignerKeyType) 141 if err != nil { 142 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 143 } 144 keyFormat, err := createECDSASerializedKeyFormat(jepb.JwtEcdsaAlgorithm_ES256, testECDSASignerVersion) 145 if err != nil { 146 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 147 } 148 k1, err := km.NewKey(keyFormat) 149 if err != nil { 150 t.Errorf("km.NewKey(keyFormat) err = %v, want nil", err) 151 } 152 key1, ok := k1.(*jepb.JwtEcdsaPrivateKey) 153 if !ok { 154 t.Errorf("key1 is not of type: *jepb.JwtEcdsaPrivateKey") 155 } 156 k2, err := km.NewKey(keyFormat) 157 if err != nil { 158 t.Errorf("km.NewKey(keyFormat) err = %v, want nil", err) 159 } 160 key2, ok := k2.(*jepb.JwtEcdsaPrivateKey) 161 if !ok { 162 t.Errorf("key2 is not of type: *jepb.JwtEcdsaPrivateKey") 163 } 164 if cmp.Equal(key1.GetKeyValue(), key2.GetKeyValue()) { 165 t.Errorf("keys should have different values") 166 } 167} 168 169func TestECDSASignerNewKeyDataWithEmptyKeyFormatFails(t *testing.T) { 170 km, err := registry.GetKeyManager(testECDSASignerKeyType) 171 if err != nil { 172 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 173 } 174 if _, err := km.NewKeyData(nil); err == nil { 175 t.Errorf("km.NewKeyData(nil) err = nil, want error") 176 } 177} 178 179func TestECDSASignerNewKeyDataWithInvalidAlgorithmFails(t *testing.T) { 180 km, err := registry.GetKeyManager(testECDSASignerKeyType) 181 if err != nil { 182 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 183 } 184 keyFormat, err := createECDSASerializedKeyFormat(jepb.JwtEcdsaAlgorithm_ES_UNKNOWN, testECDSASignerVersion) 185 if err != nil { 186 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 187 } 188 if _, err := km.NewKeyData(keyFormat); err != errECDSAInvalidAlgorithm { 189 t.Errorf("km.NewKeyData() err = %v, want %v", err, errECDSAInvalidAlgorithm) 190 } 191} 192 193func TestECDSASignerNewKeyDataGeneratesValidKeyData(t *testing.T) { 194 km, err := registry.GetKeyManager(testECDSASignerKeyType) 195 if err != nil { 196 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 197 } 198 keyFormat, err := createECDSASerializedKeyFormat(jepb.JwtEcdsaAlgorithm_ES256, testECDSASignerVersion) 199 if err != nil { 200 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 201 } 202 keyData, err := km.NewKeyData(keyFormat) 203 if err != nil { 204 t.Errorf("km.NewKeyData(keyFormat) err = %v, want nil", err) 205 } 206 if keyData.GetTypeUrl() != testECDSASignerKeyType { 207 t.Errorf("keyData.GetTypeUrl() = %q, want %q", keyData.GetTypeUrl(), testECDSASignerKeyType) 208 } 209 if keyData.GetKeyMaterialType() != tinkpb.KeyData_ASYMMETRIC_PRIVATE { 210 t.Errorf("keyData.GetKeyMaterialType() = %q, want %q", keyData.GetKeyMaterialType(), tinkpb.KeyData_ASYMMETRIC_PRIVATE) 211 } 212} 213 214func TestECDSASignerPublicKeyDataWithEmptyKeyFormatFails(t *testing.T) { 215 km, err := registry.GetKeyManager(testECDSASignerKeyType) 216 if err != nil { 217 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 218 } 219 pkm, ok := km.(registry.PrivateKeyManager) 220 if !ok { 221 t.Fatalf("key manager is not of type registry.PrivateKeyManager") 222 } 223 if _, err := pkm.PublicKeyData(nil); err == nil { 224 t.Errorf("km.PublicKeyData(nil) err = nil, want error") 225 } 226} 227 228func createECDSAKey() (*jepb.JwtEcdsaPrivateKey, error) { 229 // Private key from: https://datatracker.ietf.org/doc/html/rfc7515#appendix-A.3 230 k, err := base64Decode("jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI") 231 if err != nil { 232 return nil, err 233 } 234 pubKey, err := createECDSAPublicKey(jepb.JwtEcdsaAlgorithm_ES256, nil /*=kid*/, testECDSASignerVersion) 235 if err != nil { 236 return nil, err 237 } 238 return &jepb.JwtEcdsaPrivateKey{ 239 Version: testECDSASignerVersion, 240 PublicKey: pubKey, 241 KeyValue: k, 242 }, nil 243} 244 245func createSerializedECDSAKey() ([]byte, error) { 246 key, err := createECDSAKey() 247 if err != nil { 248 return nil, err 249 } 250 return proto.Marshal(key) 251} 252 253func TestECDSASignerPublicKeyDataGeneratesValidKeyData(t *testing.T) { 254 km, err := registry.GetKeyManager(testECDSASignerKeyType) 255 if err != nil { 256 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 257 } 258 pkm, ok := km.(registry.PrivateKeyManager) 259 if !ok { 260 t.Fatalf("key manager is not of type registry.PrivateKeyManager") 261 } 262 key, err := createSerializedECDSAKey() 263 if err != nil { 264 t.Fatalf("createECDSASerializedKeyFormat() err = %v, want nil", err) 265 } 266 pubKeyData, err := pkm.PublicKeyData(key) 267 if err != nil { 268 t.Fatalf("km.PublicKeyData() err = %v, want nil", err) 269 } 270 if pubKeyData.GetKeyMaterialType() != tinkpb.KeyData_ASYMMETRIC_PUBLIC { 271 t.Fatalf("km.PublicKeyData() = %q, want %q", pubKeyData.GetKeyMaterialType(), tinkpb.KeyData_ASYMMETRIC_PUBLIC) 272 } 273 if pubKeyData.GetTypeUrl() != testECDSAVerifierKeyType { 274 t.Errorf("keyData.GetTypeUrl() = %q, want %q", pubKeyData.GetTypeUrl(), testECDSAVerifierKeyType) 275 } 276} 277 278func TestECDSASignerPrimitiveWithEmptyKeyFails(t *testing.T) { 279 km, err := registry.GetKeyManager(testECDSASignerKeyType) 280 if err != nil { 281 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 282 } 283 if _, err := km.Primitive(nil); err == nil { 284 t.Errorf("km.Primitive(nil) err = nil, want error") 285 } 286} 287 288func TestECDSASignerPrimitiveWithInvalidKeyVersionFails(t *testing.T) { 289 km, err := registry.GetKeyManager(testECDSASignerKeyType) 290 if err != nil { 291 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 292 } 293 k, err := createECDSAKey() 294 if err != nil { 295 t.Fatalf("createECDSAKey() err = %v, want nil", err) 296 } 297 k.Version = testECDSASignerVersion + 1 298 serializedKey, err := proto.Marshal(k) 299 if err != nil { 300 t.Fatalf("proto.Marshal(k) err = %v, want nil", err) 301 } 302 if _, err := km.Primitive(serializedKey); err == nil { 303 t.Errorf("km.Primitive() err = nil, want error") 304 } 305} 306 307func TestECDSASignerPrimitiveWithoutPublicKeyFails(t *testing.T) { 308 km, err := registry.GetKeyManager(testECDSASignerKeyType) 309 if err != nil { 310 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 311 } 312 k, err := createECDSAKey() 313 if err != nil { 314 t.Fatalf("createECDSAKey() err = %v, want nil", err) 315 } 316 k.PublicKey = nil 317 serializedKey, err := proto.Marshal(k) 318 if err != nil { 319 t.Fatalf("proto.Marshal(k) err = %v, want nil", err) 320 } 321 if _, err := km.Primitive(serializedKey); err == nil { 322 t.Errorf("km.Primitive() err = nil, want error") 323 } 324} 325 326func TestECDSASignerPrimitiveWithInvalidAlgorithmFails(t *testing.T) { 327 km, err := registry.GetKeyManager(testECDSASignerKeyType) 328 if err != nil { 329 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testECDSASignerKeyType, err) 330 } 331 k, err := createECDSAKey() 332 if err != nil { 333 t.Fatalf("createECDSAKey() err = %v, want nil", err) 334 } 335 k.GetPublicKey().Algorithm = jepb.JwtEcdsaAlgorithm_ES_UNKNOWN 336 serializedKey, err := proto.Marshal(k) 337 if err != nil { 338 t.Fatalf("proto.Marshal(k) err = %v, want nil", err) 339 } 340 if _, err := km.Primitive(serializedKey); err == nil { 341 t.Errorf("km.Primitive(nil) err = nil, want error") 342 } 343} 344 345func TestECDSASignerPrimitiveSignAndVerifyToken(t *testing.T) { 346 rawJWT, err := NewRawJWT(&RawJWTOptions{WithoutExpiration: true}) 347 if err != nil { 348 t.Fatalf("NewRawJWT() err = %v, want nil", err) 349 } 350 validator, err := NewValidator(&ValidatorOpts{AllowMissingExpiration: true}) 351 if err != nil { 352 t.Fatalf("NewValidator() err = %v, want nil", err) 353 } 354 355 km, err := registry.GetKeyManager(testECDSASignerKeyType) 356 if err != nil { 357 t.Errorf("registry.GetKeyManager(%q): %v", jwtECDSASignerTypeURL, err) 358 } 359 k, err := createECDSAKey() 360 if err != nil { 361 t.Fatal(err) 362 } 363 serializedKey, err := proto.Marshal(k) 364 if err != nil { 365 t.Fatalf("proto.Marshal(k) err = %v, want nil", err) 366 } 367 s, err := km.Primitive(serializedKey) 368 if err != nil { 369 t.Fatalf("km.Primitive() err = %v, want error", err) 370 } 371 signer, ok := s.(*signerWithKID) 372 if !ok { 373 t.Fatalf("s.(*signerWithKID) = %T, want *signerWithKID", s) 374 } 375 compact, err := signer.SignAndEncodeWithKID(rawJWT, nil) 376 if err != nil { 377 t.Errorf("signer.SignAndEncodeWithKID() err = %v, want nil", err) 378 } 379 380 vkm, err := registry.GetKeyManager(testECDSAVerifierKeyType) 381 if err != nil { 382 t.Errorf("registry.GetKeyManager(%q): %v", jwtECDSAVerifierTypeURL, err) 383 } 384 serializedPubKey, err := proto.Marshal(k.GetPublicKey()) 385 if err != nil { 386 t.Fatalf("proto.Marshal(k.GetPublicKey()) err = %v, want nil", err) 387 } 388 v, err := vkm.Primitive(serializedPubKey) 389 if err != nil { 390 t.Fatalf("vkm.Primitive() err = %v, want error", err) 391 } 392 verifier, ok := v.(*verifierWithKID) 393 if !ok { 394 t.Fatalf("v.(*verifierWithKID) = %T, want *verifierWithKID", v) 395 } 396 if _, err := verifier.VerifyAndDecodeWithKID(compact, validator, nil); err != nil { 397 t.Errorf("verifier.VerifyAndDecodeWithKID() err = %v, want nil", err) 398 } 399 // Shouldn't contain KID header at all 400 if _, err := verifier.VerifyAndDecodeWithKID(compact, validator, refString("")); err == nil { 401 t.Errorf("verifier.VerifyAndDecodeWithKID() err = nil, want error") 402 } 403} 404 405func TestECDSASignerPrimitiveSignAndVerifyTokenWithCustomKID(t *testing.T) { 406 rawJWT, err := NewRawJWT(&RawJWTOptions{WithoutExpiration: true}) 407 if err != nil { 408 t.Fatalf("NewRawJWT() err = %v, want nil", err) 409 } 410 validator, err := NewValidator(&ValidatorOpts{AllowMissingExpiration: true}) 411 if err != nil { 412 t.Fatalf("NewValidator() err = %v, want nil", err) 413 } 414 415 km, err := registry.GetKeyManager(testECDSASignerKeyType) 416 if err != nil { 417 t.Errorf("registry.GetKeyManager(%q): %v", jwtECDSASignerTypeURL, err) 418 } 419 k, err := createECDSAKey() 420 if err != nil { 421 t.Fatal(err) 422 } 423 k.GetPublicKey().CustomKid = &jepb.JwtEcdsaPublicKey_CustomKid{ 424 Value: "1234", 425 } 426 serializedKey, err := proto.Marshal(k) 427 if err != nil { 428 t.Fatalf("proto.Marshal(k) err = %v, want nil", err) 429 } 430 s, err := km.Primitive(serializedKey) 431 if err != nil { 432 t.Fatalf("km.Primitive() err = %v, want error", err) 433 } 434 signer, ok := s.(*signerWithKID) 435 if !ok { 436 t.Fatalf("s.(*signerWithKID) = %T, want *signerWithKID", s) 437 } 438 compact, err := signer.SignAndEncodeWithKID(rawJWT, nil) 439 if err != nil { 440 t.Errorf("signer.SignAndEncodeWithKID(kid = nil) err = %v, want nil", err) 441 } 442 if _, err := signer.SignAndEncodeWithKID(rawJWT, refString("1234")); err == nil { 443 t.Errorf("signer.SignAndEncodeWithKID(kid = 1234) err = nil, want error") 444 } 445 446 vkm, err := registry.GetKeyManager(testECDSAVerifierKeyType) 447 if err != nil { 448 t.Errorf("registry.GetKeyManager(%q): %v", jwtECDSAVerifierTypeURL, err) 449 } 450 k.GetPublicKey().CustomKid = nil 451 serializedPubKey, err := proto.Marshal(k.GetPublicKey()) 452 if err != nil { 453 t.Fatalf("proto.Marshal(k.GetPublicKey()) err = %v, want nil", err) 454 } 455 v, err := vkm.Primitive(serializedPubKey) 456 if err != nil { 457 t.Fatalf("vkm.Primitive() err = %v, want error", err) 458 } 459 verifier, ok := v.(*verifierWithKID) 460 if !ok { 461 t.Fatalf("v.(*verifierWithKID) = %T, want *verifierWithKID", v) 462 } 463 if _, err := verifier.VerifyAndDecodeWithKID(compact, validator, refString("1234")); err != nil { 464 t.Errorf("verifier.VerifyAndDecodeWithKID(kid = '1234') err = %v, want nil", err) 465 } 466 // wrong KID verification fail 467 if _, err := verifier.VerifyAndDecodeWithKID(compact, validator, refString("1235")); err == nil { 468 t.Errorf("verifier.VerifyAndDecodeWithKID(kid = '1235') err = nil, want error") 469 } 470} 471