2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
2023-01-19 09:57:50 -05:00
|
|
|
*/
|
2022-09-05 03:06:08 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
/*
|
2023-03-29 03:30:13 -04:00
|
|
|
Package variant defines Attestation variants for different CSPs.
|
|
|
|
|
|
|
|
Each variant defines an OID, a string representation, and a function to compare it to other OIDs.
|
|
|
|
|
|
|
|
The OID is used in attested TLS to distinguish the attestation documents.
|
2022-08-31 14:10:49 -04:00
|
|
|
OIDs beginning with 1.3.9900 are reserved and can be used without registration.
|
|
|
|
|
|
|
|
* The 1.3.9900.1 branch is reserved for placeholder values and testing.
|
|
|
|
|
|
|
|
* The 1.3.9900.2 branch is reserved for AWS.
|
|
|
|
|
|
|
|
* The 1.3.9900.3 branch is reserved for GCP.
|
|
|
|
|
|
|
|
* The 1.3.9900.4 branch is reserved for Azure.
|
|
|
|
|
|
|
|
* The 1.3.9900.5 branch is reserved for QEMU.
|
|
|
|
|
|
|
|
Deprecated OIDs should never be reused for different purposes.
|
|
|
|
Instead, new OIDs should be added in the appropriate branch at the next available index.
|
2023-03-29 03:30:13 -04:00
|
|
|
|
|
|
|
String representation should be lowercase and contain only letters, numbers, and hyphens.
|
|
|
|
They should be prefixed with the branch name, e.g. all variants in the 1.3.9900.2 (AWS) branch should start with "aws-".
|
|
|
|
Each variant should have a unique string representation.
|
2022-08-31 14:10:49 -04:00
|
|
|
*/
|
2023-03-29 03:30:13 -04:00
|
|
|
package variant
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/asn1"
|
2023-03-09 05:22:58 -05:00
|
|
|
"fmt"
|
2023-05-17 10:53:56 -04:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
const (
|
|
|
|
dummy = "dummy"
|
|
|
|
awsNitroTPM = "aws-nitro-tpm"
|
|
|
|
gcpSEVES = "gcp-sev-es"
|
|
|
|
azureSEVSNP = "azure-sev-snp"
|
|
|
|
azureTrustedLaunch = "azure-trustedlaunch"
|
|
|
|
qemuVTPM = "qemu-vtpm"
|
2023-03-08 08:13:57 -05:00
|
|
|
qemuTDX = "qemu-tdx"
|
2023-03-29 03:30:13 -04:00
|
|
|
)
|
|
|
|
|
2023-05-17 10:53:56 -04:00
|
|
|
var providerAttestationMapping = map[cloudprovider.Provider][]Variant{
|
|
|
|
cloudprovider.AWS: {AWSNitroTPM{}},
|
|
|
|
cloudprovider.Azure: {AzureSEVSNP{}, AzureTrustedLaunch{}},
|
|
|
|
cloudprovider.GCP: {GCPSEVES{}},
|
|
|
|
cloudprovider.QEMU: {QEMUVTPM{}},
|
|
|
|
cloudprovider.OpenStack: {QEMUVTPM{}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultAttestation returns the default attestation type for the given provider. If not found, it returns the default variant.
|
|
|
|
func GetDefaultAttestation(provider cloudprovider.Provider) Variant {
|
|
|
|
res, ok := providerAttestationMapping[provider]
|
|
|
|
if ok {
|
|
|
|
return res[0]
|
|
|
|
}
|
|
|
|
return Dummy{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAvailableAttestationTypes returns the available attestation types.
|
|
|
|
func GetAvailableAttestationTypes() []Variant {
|
|
|
|
var res []Variant
|
|
|
|
|
|
|
|
// assumes that cloudprovider.Provider is a uint32 to sort the providers and get a consistent order
|
|
|
|
var keys []cloudprovider.Provider
|
|
|
|
for k := range providerAttestationMapping {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
return uint(keys[i]) < uint(keys[j])
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
res = append(res, providerAttestationMapping[k]...)
|
|
|
|
}
|
2023-05-25 12:43:44 -04:00
|
|
|
return RemoveDuplicate(res)
|
2023-05-17 10:53:56 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// Getter returns an ASN.1 Object Identifier.
|
2022-03-22 11:03:15 -04:00
|
|
|
type Getter interface {
|
|
|
|
OID() asn1.ObjectIdentifier
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Variant describes an attestation variant.
|
|
|
|
type Variant interface {
|
|
|
|
Getter
|
|
|
|
String() string
|
|
|
|
Equal(other Getter) bool
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// FromString returns the OID for the given string.
|
2023-03-29 03:30:13 -04:00
|
|
|
func FromString(oid string) (Variant, error) {
|
2023-03-02 04:48:16 -05:00
|
|
|
switch oid {
|
|
|
|
case dummy:
|
|
|
|
return Dummy{}, nil
|
|
|
|
case awsNitroTPM:
|
|
|
|
return AWSNitroTPM{}, nil
|
|
|
|
case gcpSEVES:
|
|
|
|
return GCPSEVES{}, nil
|
|
|
|
case azureSEVSNP:
|
|
|
|
return AzureSEVSNP{}, nil
|
|
|
|
case azureTrustedLaunch:
|
|
|
|
return AzureTrustedLaunch{}, nil
|
|
|
|
case qemuVTPM:
|
|
|
|
return QEMUVTPM{}, nil
|
2023-03-08 08:13:57 -05:00
|
|
|
case qemuTDX:
|
|
|
|
return QEMUTDX{}, nil
|
2023-03-02 04:48:16 -05:00
|
|
|
}
|
2023-03-09 05:22:58 -05:00
|
|
|
return nil, fmt.Errorf("unknown OID: %q", oid)
|
2023-03-02 04:48:16 -05:00
|
|
|
}
|
|
|
|
|
2023-05-17 10:53:56 -04:00
|
|
|
// ValidProvider returns true if the attestation type is valid for the given provider.
|
|
|
|
func ValidProvider(provider cloudprovider.Provider, variant Variant) bool {
|
|
|
|
validTypes, ok := providerAttestationMapping[provider]
|
|
|
|
if ok {
|
|
|
|
for _, aType := range validTypes {
|
|
|
|
if variant.Equal(aType) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dummy OID for testfing.
|
2022-03-22 11:03:15 -04:00
|
|
|
type Dummy struct{}
|
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// OID returns the struct's object identifier.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (Dummy) OID() asn1.ObjectIdentifier {
|
2022-08-31 14:10:49 -04:00
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 1, 1}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (Dummy) String() string {
|
|
|
|
return dummy
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:53:56 -04:00
|
|
|
// Equal returns true if the other variant is also a Default.
|
2023-03-29 03:30:13 -04:00
|
|
|
func (Dummy) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(Dummy{}.OID())
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// AWSNitroTPM holds the AWS nitro TPM OID.
|
|
|
|
type AWSNitroTPM struct{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// OID returns the struct's object identifier.
|
2023-03-02 04:48:16 -05:00
|
|
|
func (AWSNitroTPM) OID() asn1.ObjectIdentifier {
|
2022-08-31 14:10:49 -04:00
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 2, 1}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (AWSNitroTPM) String() string {
|
|
|
|
return awsNitroTPM
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Equal returns true if the other variant is also AWSNitroTPM.
|
|
|
|
func (AWSNitroTPM) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(AWSNitroTPM{}.OID())
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// GCPSEVES holds the GCP SEV-ES OID.
|
|
|
|
type GCPSEVES struct{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// OID returns the struct's object identifier.
|
2023-03-02 04:48:16 -05:00
|
|
|
func (GCPSEVES) OID() asn1.ObjectIdentifier {
|
2022-08-31 14:10:49 -04:00
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 3, 1}
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (GCPSEVES) String() string {
|
|
|
|
return gcpSEVES
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Equal returns true if the other variant is also GCPSEVES.
|
|
|
|
func (GCPSEVES) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(GCPSEVES{}.OID())
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// AzureSEVSNP holds the OID for Azure SNP CVMs.
|
|
|
|
type AzureSEVSNP struct{}
|
2022-08-31 14:10:49 -04:00
|
|
|
|
|
|
|
// OID returns the struct's object identifier.
|
2023-03-02 04:48:16 -05:00
|
|
|
func (AzureSEVSNP) OID() asn1.ObjectIdentifier {
|
2022-08-31 14:10:49 -04:00
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 4, 1}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (AzureSEVSNP) String() string {
|
|
|
|
return azureSEVSNP
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Equal returns true if the other variant is also AzureSEVSNP.
|
|
|
|
func (AzureSEVSNP) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(AzureSEVSNP{}.OID())
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// AzureTrustedLaunch holds the OID for Azure TrustedLaunch VMs.
|
2022-08-31 14:10:49 -04:00
|
|
|
type AzureTrustedLaunch struct{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// OID returns the struct's object identifier.
|
2022-08-31 14:10:49 -04:00
|
|
|
func (AzureTrustedLaunch) OID() asn1.ObjectIdentifier {
|
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 4, 2}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (AzureTrustedLaunch) String() string {
|
|
|
|
return azureTrustedLaunch
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Equal returns true if the other variant is also AzureTrustedLaunch.
|
|
|
|
func (AzureTrustedLaunch) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(AzureTrustedLaunch{}.OID())
|
|
|
|
}
|
|
|
|
|
2023-03-02 04:48:16 -05:00
|
|
|
// QEMUVTPM holds the QEMUVTPM OID.
|
|
|
|
type QEMUVTPM struct{}
|
2022-04-21 10:27:34 -04:00
|
|
|
|
2022-04-28 08:31:01 -04:00
|
|
|
// OID returns the struct's object identifier.
|
2023-03-02 04:48:16 -05:00
|
|
|
func (QEMUVTPM) OID() asn1.ObjectIdentifier {
|
2022-08-31 14:10:49 -04:00
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 5, 1}
|
2022-04-21 10:27:34 -04:00
|
|
|
}
|
2023-03-02 04:48:16 -05:00
|
|
|
|
|
|
|
// String returns the string representation of the OID.
|
|
|
|
func (QEMUVTPM) String() string {
|
|
|
|
return qemuVTPM
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
// Equal returns true if the other variant is also QEMUVTPM.
|
|
|
|
func (QEMUVTPM) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(QEMUVTPM{}.OID())
|
|
|
|
}
|
2023-03-08 08:13:57 -05:00
|
|
|
|
|
|
|
// QEMUTDX holds the QEMU TDX OID.
|
|
|
|
// Placeholder for dev-cloud integration.
|
|
|
|
type QEMUTDX struct{}
|
|
|
|
|
|
|
|
// OID returns the struct's object identifier.
|
|
|
|
// Placeholder for dev-cloud integration.
|
|
|
|
func (QEMUTDX) OID() asn1.ObjectIdentifier {
|
|
|
|
return asn1.ObjectIdentifier{1, 3, 9900, 5, 99}
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:08:03 -04:00
|
|
|
// String returns the string representation of the OID.
|
2023-03-08 08:13:57 -05:00
|
|
|
func (QEMUTDX) String() string {
|
|
|
|
return qemuTDX
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if the other variant is also QEMUTDX.
|
|
|
|
func (QEMUTDX) Equal(other Getter) bool {
|
|
|
|
return other.OID().Equal(QEMUTDX{}.OID())
|
|
|
|
}
|
2023-05-17 10:53:56 -04:00
|
|
|
|
2023-05-25 12:43:44 -04:00
|
|
|
// RemoveDuplicate removes duplicate elements from a slice.
|
|
|
|
func RemoveDuplicate[T comparable](sliceList []T) []T {
|
|
|
|
allKeys := make(map[T]bool)
|
|
|
|
list := []T{}
|
2023-05-17 10:53:56 -04:00
|
|
|
for _, item := range sliceList {
|
|
|
|
if _, value := allKeys[item]; !value {
|
|
|
|
allKeys[item] = true
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|