| 1 |
// Copyright 2024 OpenPubkey |
| 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 |
// SPDX-License-Identifier: Apache-2.0 |
| 16 |
|
| 17 |
package mocks |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"crypto" |
| 22 |
"fmt" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/lestrrat-go/jwx/v3/jwk" |
| 26 |
"github.com/openpubkey/openpubkey/internal/jwx" |
| 27 |
"github.com/openpubkey/openpubkey/jose" |
| 28 |
"github.com/openpubkey/openpubkey/pktoken" |
| 29 |
"github.com/openpubkey/openpubkey/pktoken/clientinstance" |
| 30 |
"github.com/openpubkey/openpubkey/providers" |
| 31 |
"github.com/openpubkey/openpubkey/providers/mocks" |
| 32 |
"github.com/openpubkey/openpubkey/util" |
| 33 |
"github.com/stretchr/testify/require" |
| 34 |
) |
| 35 |
|
| 36 |
func GenerateMockPKToken(t *testing.T, signingKey crypto.Signer, alg jose.KeyAlgorithm) (*pktoken.PKToken, error) { |
| 37 |
options := &MockPKTokenOpts{ |
| 38 |
GQSign: false, |
| 39 |
CommitType: providers.CommitTypesEnum.NONCE_CLAIM, |
| 40 |
CorrectCicHash: true, |
| 41 |
CorrectCicSig: true, |
| 42 |
} |
| 43 |
pkt, _, err := GenerateMockPKTokenWithOpts(t, signingKey, alg, mocks.DefaultIDTokenTemplate(), options) |
| 44 |
return pkt, err |
| 45 |
} |
| 46 |
|
| 47 |
func GenerateMockPKTokenGQ(t *testing.T, signingKey crypto.Signer, alg jose.KeyAlgorithm) (*pktoken.PKToken, error) { |
| 48 |
options := &MockPKTokenOpts{ |
| 49 |
GQSign: true, |
| 50 |
CommitType: providers.CommitTypesEnum.NONCE_CLAIM, |
| 51 |
CorrectCicHash: true, |
| 52 |
CorrectCicSig: true, |
| 53 |
} |
| 54 |
pkt, _, err := GenerateMockPKTokenWithOpts(t, signingKey, alg, mocks.DefaultIDTokenTemplate(), options) |
| 55 |
return pkt, err |
| 56 |
} |
| 57 |
|
| 58 |
type MockPKTokenOpts struct { |
| 59 |
GQSign bool |
| 60 |
CommitType providers.CommitType |
| 61 |
GQOnly bool |
| 62 |
CorrectCicHash bool |
| 63 |
CorrectCicSig bool |
| 64 |
} |
| 65 |
|
| 66 |
func GenerateMockPKTokenWithOpts(t *testing.T, signingKey crypto.Signer, alg jose.KeyAlgorithm, |
| 67 |
idtTemplate mocks.IDTokenTemplate, options *MockPKTokenOpts) (*pktoken.PKToken, *mocks.MockProviderBackend, error) { |
| 68 |
|
| 69 |
jwkKey, err := jwk.PublicKeyOf(signingKey) |
| 70 |
if err != nil { |
| 71 |
return nil, nil, err |
| 72 |
} |
| 73 |
err = jwkKey.Set(jwk.AlgorithmKey, alg) |
| 74 |
if err != nil { |
| 75 |
return nil, nil, err |
| 76 |
} |
| 77 |
|
| 78 |
cic, err := clientinstance.NewClaims(jwkKey, map[string]any{}) |
| 79 |
require.NoError(t, err) |
| 80 |
|
| 81 |
// Set gqOnly to gqCommitment since gqCommitment requires gqOnly |
| 82 |
gqOnly := options.CommitType.GQCommitment |
| 83 |
|
| 84 |
providerOpts := providers.MockProviderOpts{ |
| 85 |
GQSign: options.GQSign, |
| 86 |
CommitType: options.CommitType, |
| 87 |
NumKeys: 2, |
| 88 |
VerifierOpts: providers.ProviderVerifierOpts{ |
| 89 |
SkipClientIDCheck: false, |
| 90 |
GQOnly: gqOnly, |
| 91 |
CommitType: options.CommitType, |
| 92 |
ClientID: "mockClient-ID", |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
op, backend, _, err := providers.NewMockProvider(providerOpts) |
| 97 |
require.NoError(t, err) |
| 98 |
opSignKey, keyID, _ := backend.RandomSigningKey() |
| 99 |
idtTemplate.KeyID = keyID |
| 100 |
idtTemplate.SigningKey = opSignKey |
| 101 |
|
| 102 |
switch options.CommitType { |
| 103 |
case providers.CommitTypesEnum.NONCE_CLAIM: |
| 104 |
idtTemplate.CommitFunc = mocks.AddNonceCommit |
| 105 |
case providers.CommitTypesEnum.AUD_CLAIM: |
| 106 |
idtTemplate.CommitFunc = mocks.AddAudCommit |
| 107 |
case providers.CommitTypesEnum.GQ_BOUND: |
| 108 |
default: |
| 109 |
return nil, nil, fmt.Errorf("unknown CommitType: %v", options.CommitType) |
| 110 |
} |
| 111 |
|
| 112 |
backend.SetIDTokenTemplate(&idtTemplate) |
| 113 |
|
| 114 |
tokens, err := op.RequestTokens(context.Background(), cic) |
| 115 |
if err != nil { |
| 116 |
return nil, nil, err |
| 117 |
} |
| 118 |
idToken := tokens.IDToken |
| 119 |
|
| 120 |
// Return a PK Token where the CIC which doesn't match the commitment |
| 121 |
if !options.CorrectCicHash { |
| 122 |
// overwrite the cic with a new cic with a different hash |
| 123 |
cic, err = clientinstance.NewClaims(jwkKey, map[string]any{"cause": "differentCicHash"}) |
| 124 |
if err != nil { |
| 125 |
return nil, nil, err |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Return a PK Token where the CIC that is signed by the wrong key |
| 130 |
if !options.CorrectCicSig { |
| 131 |
// overwrite the signkey with a new key |
| 132 |
signingKey, err = util.GenKeyPair(alg) |
| 133 |
require.NoError(t, err) |
| 134 |
|
| 135 |
jwkKey, err = jwk.PublicKeyOf(signingKey) |
| 136 |
if err != nil { |
| 137 |
return nil, nil, err |
| 138 |
} |
| 139 |
err = jwkKey.Set(jwk.AlgorithmKey, alg) |
| 140 |
if err != nil { |
| 141 |
return nil, nil, err |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Sign mock id token payload with cic headers |
| 146 |
jwkAlg, ok := jwkKey.Algorithm() |
| 147 |
if !ok { |
| 148 |
return nil, nil, fmt.Errorf("failed to get algorithm from jwk key") |
| 149 |
} |
| 150 |
cicToken, err := cic.Sign(signingKey, jwx.ToJoseAlgorithm(jwkAlg), idToken) |
| 151 |
if err != nil { |
| 152 |
return nil, nil, err |
| 153 |
} |
| 154 |
|
| 155 |
// Combine two tokens into a PK Token |
| 156 |
pkt, err := pktoken.New(idToken, cicToken) |
| 157 |
if err != nil { |
| 158 |
return nil, nil, err |
| 159 |
} |
| 160 |
return pkt, backend, nil |
| 161 |
} |