Debian Codegraph @

examples/x509/ca/ca_test.go

raw
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 ca
18
19 import (
20 "context"
21 "crypto/ecdsa"
22 "crypto/x509"
23 "encoding/json"
24 "encoding/pem"
25 "testing"
26
27 "github.com/lestrrat-go/jwx/v3/jwa"
28 "github.com/lestrrat-go/jwx/v3/jws"
29 "github.com/stretchr/testify/require"
30
31 "github.com/openpubkey/openpubkey/client"
32 "github.com/openpubkey/openpubkey/jose"
33 "github.com/openpubkey/openpubkey/providers"
34 "github.com/openpubkey/openpubkey/util"
35 )
36
37 func TestCACertCreation(t *testing.T) {
38 providerOpts := providers.DefaultMockProviderOpts()
39 op, _, _, err := providers.NewMockProvider(providerOpts)
40 require.NoError(t, err)
41
42 certAuth, err := New(op)
43 require.NoError(t, err)
44
45 err = certAuth.KeyGen(jwa.ES256().String())
46 require.NoError(t, err)
47
48 userAlg := jose.ES256
49 userSigningKey, err := util.GenKeyPair(userAlg)
50 require.NoError(t, err)
51
52 client, err := client.New(op, client.WithSigner(userSigningKey, userAlg))
53 require.NoError(t, err)
54
55 pkt, err := client.Auth(context.Background())
56 require.NoError(t, err)
57
58 pktJson, err := json.Marshal(pkt)
59 require.NoError(t, err)
60
61 pemSubCert, err := certAuth.PktToSignedX509(pktJson)
62 require.NoError(t, err)
63 decodeBlock, _ := pem.Decode(pemSubCert)
64
65 cc, err := x509.ParseCertificate(decodeBlock.Bytes)
66 require.NoError(t, err)
67
68 certPubkey := cc.PublicKey.(*ecdsa.PublicKey)
69
70 _, err = jws.Verify(pkt.CicToken, jws.WithKey(jwa.ES256(), certPubkey))
71 require.NoError(t, err)
72
73 err = certAuth.VerifyPktCert(pemSubCert)
74 require.NoError(t, err)
75
76 }