| 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 |
"bytes" |
| 21 |
"context" |
| 22 |
"crypto/ecdsa" |
| 23 |
"crypto/elliptic" |
| 24 |
"crypto/rand" |
| 25 |
"crypto/x509" |
| 26 |
"crypto/x509/pkix" |
| 27 |
"encoding/json" |
| 28 |
"encoding/pem" |
| 29 |
"fmt" |
| 30 |
"math/big" |
| 31 |
"time" |
| 32 |
|
| 33 |
"github.com/lestrrat-go/jwx/v3/jwa" |
| 34 |
|
| 35 |
"github.com/openpubkey/openpubkey/cert" |
| 36 |
"github.com/openpubkey/openpubkey/client" |
| 37 |
"github.com/openpubkey/openpubkey/jose" |
| 38 |
"github.com/openpubkey/openpubkey/pktoken" |
| 39 |
"github.com/openpubkey/openpubkey/verifier" |
| 40 |
) |
| 41 |
|
| 42 |
type Ca struct { |
| 43 |
pksk *ecdsa.PrivateKey |
| 44 |
Alg jose.KeyAlgorithm |
| 45 |
// CaCertBytes []byte |
| 46 |
RootCertPem []byte |
| 47 |
op client.OpenIdProvider |
| 48 |
} |
| 49 |
|
| 50 |
func New(op client.OpenIdProvider) (*Ca, error) { |
| 51 |
ca := Ca{ |
| 52 |
op: op, |
| 53 |
} |
| 54 |
alg := jwa.ES256().String() |
| 55 |
err := ca.KeyGen(alg) |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
|
| 60 |
return &ca, nil |
| 61 |
} |
| 62 |
|
| 63 |
func (a *Ca) KeyGen(alg string) error { |
| 64 |
keyAlg, err := jwa.KeyAlgorithmFrom(alg) |
| 65 |
if err != nil { |
| 66 |
return fmt.Errorf("failed to parse key algorithm from %s: %w", alg, err) |
| 67 |
} |
| 68 |
a.Alg = keyAlg.String() |
| 69 |
|
| 70 |
pksk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
a.pksk = pksk |
| 75 |
|
| 76 |
caTemplate := &x509.Certificate{ |
| 77 |
SerialNumber: big.NewInt(2019), |
| 78 |
Subject: pkix.Name{ |
| 79 |
Organization: []string{"Openpubkey-test-ca-cert"}, |
| 80 |
Country: []string{"International"}, |
| 81 |
Province: []string{""}, |
| 82 |
Locality: []string{""}, |
| 83 |
StreetAddress: []string{"255 Test St."}, |
| 84 |
PostalCode: []string{""}, |
| 85 |
}, |
| 86 |
NotBefore: time.Now(), |
| 87 |
NotAfter: time.Now().AddDate(10, 0, 0), |
| 88 |
IsCA: true, |
| 89 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageCodeSigning}, |
| 90 |
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, |
| 91 |
BasicConstraintsValid: true, |
| 92 |
} |
| 93 |
|
| 94 |
caBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, &a.pksk.PublicKey, a.pksk) |
| 95 |
if err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
|
| 99 |
caPEM := new(bytes.Buffer) |
| 100 |
pem.Encode(caPEM, &pem.Block{ |
| 101 |
Type: "CERTIFICATE", |
| 102 |
Bytes: caBytes, |
| 103 |
}) |
| 104 |
a.RootCertPem = caPEM.Bytes() |
| 105 |
|
| 106 |
return nil |
| 107 |
} |
| 108 |
|
| 109 |
func (a *Ca) CheckPKToken(pktJson []byte) (*pktoken.PKToken, error) { |
| 110 |
pkt := new(pktoken.PKToken) |
| 111 |
if err := json.Unmarshal(pktJson, pkt); err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
verifier, err := verifier.New(a.op) |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
if err := verifier.VerifyPKToken(context.TODO(), pkt); err != nil { |
| 120 |
return nil, fmt.Errorf("failed to verify PK token: %w", err) |
| 121 |
} |
| 122 |
|
| 123 |
return pkt, nil |
| 124 |
} |
| 125 |
|
| 126 |
func (a *Ca) PktToSignedX509(pktJson []byte) ([]byte, error) { |
| 127 |
pkt, err := a.CheckPKToken(pktJson) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
|
| 132 |
pktUpk, err := ExtractRawPubkey(pkt) |
| 133 |
if err != nil { |
| 134 |
return nil, err |
| 135 |
} |
| 136 |
|
| 137 |
subTemplate, err := cert.PktToX509Template(pkt) |
| 138 |
if err != nil { |
| 139 |
return nil, err |
| 140 |
} |
| 141 |
|
| 142 |
rootCert, _ := pem.Decode(a.RootCertPem) |
| 143 |
if rootCert == nil { |
| 144 |
return nil, fmt.Errorf("failed to parse certificate PEM") |
| 145 |
} |
| 146 |
caTemplate, err := x509.ParseCertificate(rootCert.Bytes) |
| 147 |
if err != nil { |
| 148 |
return nil, err |
| 149 |
} |
| 150 |
|
| 151 |
subCertBytes, err := x509.CreateCertificate(rand.Reader, subTemplate, caTemplate, pktUpk, a.pksk) |
| 152 |
if err != nil { |
| 153 |
return nil, err |
| 154 |
} |
| 155 |
|
| 156 |
subCert, err := x509.ParseCertificate(subCertBytes) |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
|
| 161 |
var pemSubCert bytes.Buffer |
| 162 |
err = pem.Encode(&pemSubCert, &pem.Block{Type: "CERTIFICATE", Bytes: subCert.Raw}) |
| 163 |
if err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
return pemSubCert.Bytes(), nil |
| 167 |
} |
| 168 |
|
| 169 |
// VerifyPktCert checks that the X509 cert is signed by the CA and that |
| 170 |
// the PK Token in the cert matches the public key in the cert. |
| 171 |
func (a *Ca) VerifyPktCert(issuedCertPEM []byte) error { |
| 172 |
|
| 173 |
roots := x509.NewCertPool() |
| 174 |
ok := roots.AppendCertsFromPEM([]byte(a.RootCertPem)) |
| 175 |
if !ok { |
| 176 |
return fmt.Errorf("failed to parse root certificate") |
| 177 |
} |
| 178 |
|
| 179 |
block, _ := pem.Decode([]byte(issuedCertPEM)) |
| 180 |
if block == nil { |
| 181 |
return fmt.Errorf("failed to parse certificate PEM") |
| 182 |
} |
| 183 |
cert, err := x509.ParseCertificate(block.Bytes) |
| 184 |
if err != nil { |
| 185 |
return fmt.Errorf("failed to parse certificate PEM: %w", err) |
| 186 |
} |
| 187 |
_, err = cert.Verify(x509.VerifyOptions{ |
| 188 |
Roots: roots, |
| 189 |
Intermediates: x509.NewCertPool(), |
| 190 |
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, |
| 191 |
}) |
| 192 |
if err != nil { |
| 193 |
return fmt.Errorf("failed to verify certificate: %w", err) |
| 194 |
} |
| 195 |
|
| 196 |
pktJson := cert.SubjectKeyId |
| 197 |
pkt := new(pktoken.PKToken) |
| 198 |
if err := json.Unmarshal(pktJson, pkt); err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
pktUpk, err := ExtractRawPubkey(pkt) |
| 202 |
if err != nil { |
| 203 |
return err |
| 204 |
} |
| 205 |
|
| 206 |
certPublickey := cert.PublicKey.(*ecdsa.PublicKey) |
| 207 |
if !certPublickey.Equal(pktUpk) { |
| 208 |
return fmt.Errorf("public key in cert does not match PK Token's public key") |
| 209 |
} |
| 210 |
|
| 211 |
certPublicKeyBytes, err := x509.MarshalPKIXPublicKey(certPublickey) |
| 212 |
if err := json.Unmarshal(pktJson, pkt); err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
if string(cert.RawSubjectPublicKeyInfo) != string(certPublicKeyBytes) { |
| 216 |
return fmt.Errorf("certificate raw subject public key info does not match ephemeral public key") |
| 217 |
} |
| 218 |
|
| 219 |
// Verification succeeds |
| 220 |
return nil |
| 221 |
} |
| 222 |
|
| 223 |
func ExtractRawPubkey(pkt *pktoken.PKToken) (interface{}, error) { |
| 224 |
cic, err := pkt.GetCicValues() |
| 225 |
if err != nil { |
| 226 |
return nil, err |
| 227 |
} |
| 228 |
return cic.PublicKey(), nil |
| 229 |
} |