1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
package attestation
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"time"
"go.step.sm/crypto/tpm"
"github.com/smallstep/go-attestation/attest"
)
type Client struct {
client *http.Client
baseURL *url.URL
}
type Options struct {
rootCAs *x509.CertPool
insecure bool
}
type Option func(o *Options) error
// WithRootsFile can be used to set the trusted roots when
// setting up a TLS connection. An empty filename will
// be ignored.
func WithRootsFile(filename string) Option {
return func(o *Options) error {
if filename == "" {
return nil
}
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed reading %q: %w", filename, err)
}
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(data) {
return fmt.Errorf("failed parsing %q: no certificates found", filename)
}
o.rootCAs = pool
return nil
}
}
// WithInsecure disables TLS server certificate chain checking.
// In general this shouldn't be used, but it can be of use in
// during development and testing.
func WithInsecure() Option {
return func(o *Options) error {
o.insecure = true
return nil
}
}
// NewClient creates a new Client that can be used to perform remote
// attestation.
func NewClient(tpmAttestationCABaseURL string, options ...Option) (*Client, error) {
u, err := url.Parse(tpmAttestationCABaseURL)
if err != nil {
return nil, fmt.Errorf("failed parsing attestation CA base URL: %w", err)
}
opts := &Options{}
for _, o := range options {
if err := o(opts); err != nil {
return nil, fmt.Errorf("failed applying option to attestation client: %w", err)
}
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{
RootCAs: opts.rootCAs,
InsecureSkipVerify: opts.insecure, //nolint:gosec // intentional insecure if provided as option
}
client := &http.Client{
Timeout: 10 * time.Second,
Transport: transport,
}
return &Client{
client: client,
baseURL: u,
}, nil
}
// Attest performs remote attestation using the AK backed by TPM t.
//
// TODO: support multiple EKs again? Currently selection of the EK is left
// to the caller.
func (ac *Client) Attest(ctx context.Context, t *tpm.TPM, ek *tpm.EK, ak *tpm.AK) ([]*x509.Certificate, error) {
// TODO(hs): what about performing attestation for an existing AK identifier and/or cert, but
// with a different Attestation CA? It seems sensible to enroll with that other Attestation CA,
// but it needs capturing some knowledge about the Attestation CA with the AK (cert). Possible to
// derive that from the intermediate and/or root CA and/or fingerprint, somehow? Or the attestation URI?
info, err := t.Info(ctx)
if err != nil {
return nil, fmt.Errorf("failed retrieving info from TPM: %w", err)
}
attestParams, err := ak.AttestationParameters(ctx)
if err != nil {
return nil, fmt.Errorf("failed getting AK attestation parameters: %w", err)
}
attResp, err := ac.attest(ctx, info, ek, attestParams)
if err != nil {
return nil, fmt.Errorf("failed attesting AK: %w", err)
}
encryptedCredentials := tpm.EncryptedCredential{
Credential: attResp.Credential,
Secret: attResp.Secret,
}
// activate the credential with the TPM
secret, err := ak.ActivateCredential(ctx, encryptedCredentials)
if err != nil {
return nil, fmt.Errorf("failed activating credential: %w", err)
}
secretResp, err := ac.secret(ctx, secret)
if err != nil {
return nil, fmt.Errorf("failed validating secret: %w", err)
}
akChain := make([]*x509.Certificate, len(secretResp.CertificateChain))
for i, certBytes := range secretResp.CertificateChain {
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, fmt.Errorf("failed parsing certificate: %w", err)
}
akChain[i] = cert
}
return akChain, nil
}
type tpmInfo struct {
Version attest.TPMVersion `json:"version,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"`
Model string `json:"model,omitempty"`
FirmwareVersion string `json:"firmwareVersion,omitempty"`
}
type attestationParameters struct {
Public []byte `json:"public,omitempty"`
UseTCSDActivationFormat bool `json:"useTCSDActivationFormat,omitempty"`
CreateData []byte `json:"createData,omitempty"`
CreateAttestation []byte `json:"createAttestation,omitempty"`
CreateSignature []byte `json:"createSignature,omitempty"`
}
type attestationRequest struct {
TPMInfo tpmInfo `json:"tpmInfo"`
EKPub []byte `json:"ek,omitempty"`
EKCerts [][]byte `json:"ekCerts,omitempty"`
AKCert []byte `json:"akCert,omitempty"`
AttestParams attestationParameters `json:"params"`
}
type attestationResponse struct {
Credential []byte `json:"credential"`
Secret []byte `json:"secret"` // encrypted secret
}
// attest performs the HTTP POST request to the `/attest` endpoint of the
// Attestation CA.
func (ac *Client) attest(ctx context.Context, info *tpm.Info, ek *tpm.EK, attestParams attest.AttestationParameters) (*attestationResponse, error) {
var ekCerts [][]byte
var ekPub []byte
var err error
// TODO: support multiple EKs again? Currently selection of the EK is left
// to the caller.
if ekCert := ek.Certificate(); ekCert != nil {
ekCerts = append(ekCerts, ekCert.Raw)
}
if ekPub, err = x509.MarshalPKIXPublicKey(ek.Public()); err != nil {
return nil, fmt.Errorf("failed marshaling public key: %w", err)
}
ar := attestationRequest{
TPMInfo: tpmInfo{
Version: attest.TPMVersion20,
Manufacturer: strconv.FormatUint(uint64(info.Manufacturer.ID), 10),
Model: info.VendorInfo,
FirmwareVersion: info.FirmwareVersion.String(),
},
EKCerts: ekCerts,
EKPub: ekPub,
AttestParams: attestationParameters{
Public: attestParams.Public,
UseTCSDActivationFormat: attestParams.UseTCSDActivationFormat,
CreateData: attestParams.CreateData,
CreateAttestation: attestParams.CreateAttestation,
CreateSignature: attestParams.CreateSignature,
},
}
body, err := json.Marshal(ar)
if err != nil {
return nil, fmt.Errorf("failed marshaling attestation request: %w", err)
}
attestURL := ac.baseURL.JoinPath("attest").String()
req, err := newRequest(ctx, http.MethodPost, attestURL, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("failed creating POST http request for %q: %w", attestURL, err)
}
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed performing attestation request with Attestation CA %q: %w", attestURL, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("POST %q failed with HTTP status %q", attestURL, resp.Status)
}
var attResp attestationResponse
if err := json.NewDecoder(resp.Body).Decode(&attResp); err != nil {
return nil, fmt.Errorf("failed decoding attestation response: %w", err)
}
return &attResp, nil
}
type secretRequest struct {
Secret []byte `json:"secret"` // decrypted secret
}
type secretResponse struct {
CertificateChain [][]byte `json:"chain"`
}
// secret performs the HTTP POST request to the `/secret` endpoint of the
// Attestation CA.
func (ac *Client) secret(ctx context.Context, secret []byte) (*secretResponse, error) {
sr := secretRequest{
Secret: secret,
}
body, err := json.Marshal(sr)
if err != nil {
return nil, fmt.Errorf("failed marshaling secret request: %w", err)
}
secretURL := ac.baseURL.JoinPath("secret").String()
req, err := newRequest(ctx, http.MethodPost, secretURL, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("failed creating POST http request for %q: %w", secretURL, err)
}
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed performing secret request with attestation CA %q: %w", secretURL, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("POST %q failed with HTTP status %q", secretURL, resp.Status)
}
var secretResp secretResponse
if err := json.NewDecoder(resp.Body).Decode(&secretResp); err != nil {
return nil, fmt.Errorf("failed decoding secret response: %w", err)
}
return &secretResp, nil
}
func newRequest(ctx context.Context, method, requestURL string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, requestURL, body)
if err != nil {
return nil, err
}
enforceRequestID(req)
setUserAgent(req)
return req, nil
}
|