2022-02-16 09:53:10 +00:00
|
|
|
/*
|
|
|
|
** Copyright 2019 Bloomberg Finance L.P.
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package tpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/certificate-transparency-go/x509"
|
|
|
|
"github.com/google/go-attestation/attest"
|
2022-02-17 09:10:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-02-16 09:53:10 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 09:10:04 +00:00
|
|
|
type attestationData struct {
|
2022-02-16 09:53:10 +00:00
|
|
|
EK []byte
|
|
|
|
AK *attest.AttestationParameters
|
|
|
|
}
|
|
|
|
|
2022-02-17 09:10:04 +00:00
|
|
|
// Challenge represent the struct returned from the ws server,
|
|
|
|
// used to resolve the TPM challenge.
|
2022-02-16 09:53:10 +00:00
|
|
|
type Challenge struct {
|
|
|
|
EC *attest.EncryptedCredential
|
|
|
|
}
|
|
|
|
|
2022-02-17 09:10:04 +00:00
|
|
|
// ChallengeResponse represent the struct returned to the ws server
|
|
|
|
// as a challenge response.
|
2022-02-16 09:53:10 +00:00
|
|
|
type ChallengeResponse struct {
|
|
|
|
Secret []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPubHash(ek *attest.EK) (string, error) {
|
|
|
|
data, err := pubBytes(ek)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pubHash := sha256.Sum256(data)
|
|
|
|
hashEncoded := fmt.Sprintf("%x", pubHash)
|
|
|
|
return hashEncoded, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 09:10:04 +00:00
|
|
|
func encodeEK(ek *attest.EK) ([]byte, error) {
|
2022-02-16 09:53:10 +00:00
|
|
|
if ek.Certificate != nil {
|
|
|
|
return pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: ek.Certificate.Raw,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := pubBytes(ek)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "PUBLIC KEY",
|
|
|
|
Bytes: data,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pubBytes(ek *attest.EK) ([]byte, error) {
|
|
|
|
data, err := x509.MarshalPKIXPublicKey(ek.Public)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error marshaling ec public key: %v", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
2022-02-17 09:10:04 +00:00
|
|
|
|
|
|
|
// DecodeEK decodes EK pem bytes to attest.EK
|
|
|
|
func DecodeEK(pemBytes []byte) (*attest.EK, error) {
|
|
|
|
block, _ := pem.Decode(pemBytes)
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
return nil, errors.New("invalid pemBytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch block.Type {
|
|
|
|
case "CERTIFICATE":
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing certificate: %v", err)
|
|
|
|
}
|
|
|
|
return &attest.EK{
|
|
|
|
Certificate: cert,
|
|
|
|
Public: cert.PublicKey,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case "PUBLIC KEY":
|
|
|
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing ecdsa public key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &attest.EK{
|
|
|
|
Public: pub,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("invalid pem type: %s", block.Type)
|
|
|
|
}
|