| 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 | package jwtscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 20 | |
| 21 | "google.golang.org/protobuf/protoscip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/" |
| 22 | "github.com/tink-crypto/tink-go/v2/core/registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/" |
| 23 | jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/ "github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_protoscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/" |
| 24 | ) |
| 25 | |
| 26 | const testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType. = "type.googleapis.com/google.crypto.tink.JwtEcdsaPublicKey" |
| 27 | |
| 28 | func TestECDSAVerifierNotImplementedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/TestECDSAVerifierNotImplemented().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 29 | km, err := registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 30 | if err != nil { |
| 31 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("registry.GetKeyManager(%q) err = %v, want nil", testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType., err) |
| 32 | } |
| 33 | if _, err := km.NewKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#NewKey.(nil); err == nil { |
| 34 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("km.NewKey() err = nil, want error") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestECDSAVerifierDoesSupportscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/TestECDSAVerifierDoesSupport().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 39 | km, err := registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 40 | if err != nil { |
| 41 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("registry.GetKeyManager(%q) err = %v, want nil", testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType., err) |
| 42 | } |
| 43 | if !km.DoesSupportscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#DoesSupport.(testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) { |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("km.DoesSupport(%q) = false, want true", testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 45 | } |
| 46 | if km.DoesSupportscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#DoesSupport.("not.the.actual.key.type") { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("km.DoesSupport('not.the.actual.key.type') = true, want false") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestECDSAVerifierTypeURLscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/TestECDSAVerifierTypeURL().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 52 | km, err := registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 53 | if err != nil { |
| 54 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("registry.GetKeyManager(%q) err = %v, want nil", testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType., err) |
| 55 | } |
| 56 | if km.TypeURLscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#TypeURL.() != testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType. { |
| 57 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("km.TypeURL() = %q, want %q", km.TypeURLscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#TypeURL.(), testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func createECDSAPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/createECDSAPublicKey().(algorithm jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaAlgorithmscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaAlgorithm#, kid *string, version uint32) (*jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#, error) { |
| 62 | // Public key from: https://datatracker.ietf.org/doc/html/rfc7515#appendix-A.3 |
| 63 | x, err := base64Decodescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/base64Decode().("f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU") |
| 64 | if err != nil { |
| 65 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("base64 decoding x coordinate of public key: %v", err) |
| 66 | } |
| 67 | y, err := base64Decodescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/base64Decode().("x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0") |
| 68 | if err != nil { |
| 69 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("base64 decoding y coordinate of public key: %v", err) |
| 70 | } |
| 71 | var customKID *jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaPublicKey_CustomKidscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey_CustomKid# = nil |
| 72 | if kid != nil { |
| 73 | customKID = &jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaPublicKey_CustomKidscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey_CustomKid#{Valuescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey_CustomKid#Value.: *kid} |
| 74 | } |
| 75 | return &jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#{ |
| 76 | Versionscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#Version.: version, |
| 77 | Algorithmscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#Algorithm.: algorithm, |
| 78 | Xscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#X.: x, |
| 79 | Yscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#Y.: y, |
| 80 | CustomKidscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaPublicKey#CustomKid.: customKID, |
| 81 | }, nil |
| 82 | } |
| 83 | |
| 84 | func createECDSASerializedPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/createECDSASerializedPublicKey().(algorithm jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaAlgorithmscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaAlgorithm#, kid *string, version uint32) ([]byte, error) { |
| 85 | pubKey, err := createECDSAPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/createECDSAPublicKey().(algorithm, kid, version) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | return protoscip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/.Marshalscip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/Marshal().(pubKey) |
| 90 | } |
| 91 | |
| 92 | func TestECDSAVerifierPrimitiveAlwaysFailsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/TestECDSAVerifierPrimitiveAlwaysFails().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 93 | km, err := registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType.) |
| 94 | if err != nil { |
| 95 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("registry.GetKeyManager(%q) err = %v, want nil", testECDSAVerifierKeyTypescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/testECDSAVerifierKeyType., err) |
| 96 | } |
| 97 | serializedPubKey, err := createECDSASerializedPublicKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/createECDSASerializedPublicKey().(jepbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/.JwtEcdsaAlgorithm_ES256scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/jwt_ecdsa_go_proto`/JwtEcdsaAlgorithm_ES256., refStringscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/jwt`/refString().("1234"), 0 /*=version*/) |
| 98 | if err != nil { |
| 99 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 100 | } |
| 101 | if _, err := km.Primitivescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#Primitive.(serializedPubKey); err == nil { |
| 102 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("km.Primitive() err = %v, want nil", err) |
| 103 | } |
| 104 | } |