attestation

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

attestation

Package attestation implements ASN.1 encoding/decoding of Android Key attestation extension.

Code example

package main

import (
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"log"

	"github.com/mbreban/attestation"
)

func main() {
	pemBytes := []byte(`
-----BEGIN CERTIFICATE-----
MIICjDCCAjKgAwIBAgIBATAKBggqhkjOPQQDAjA5MQwwCgYDVQQMDANURUUxKTAn
BgNVBAUTIDcwYzI5ODU2MGQ4ZTJlYmJjM2ViZTM5YmQ3NDc4ZDRjMB4XDTcwMDEw
MTAwMDAwMFoXDTQ4MDEwMTAwMDAwMFowHzEdMBsGA1UEAxMUQW5kcm9pZCBLZXlz
dG9yZSBLZXkwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQR+vduzii4Rre8TkK3
12HzdLXxjojSCDXRUg9CISx7QjzBTvpmmsgZ5NCptZ0pX8umerf8H+xrrhL8R3y3
QvUyo4IBQzCCAT8wDgYDVR0PAQH/BAQDAgeAMIIBKwYKKwYBBAHWeQIBEQSCARsw
ggEXAgFkCgEBAgFkCgEBBAZzYW1wbGUEADBYv4U9CAIGAYDUNZXsv4VFSARGMEQx
HjAcBBdhcHAuYXR0ZXN0YXRpb24uYXVkaXRvcgIBLTEiBCCZDgTwhksZ8U+E4OQy
96OT8perEFoiweGxC0QqSmLELDCBpKEIMQYCAQICAQOiAwIBA6MEAgIBAKUFMQMC
AQSqAwIBAb+DdwIFAL+FPgMCAQC/hUBMMEoEIELtG8o1L6vUKPNOj87mJ3b0yyxm
4G+C5aWf9ElSZ7/CAQH/CgEABCC429249di4yi84SAUEJgb66JPihAI1gN+i3flu
dSP7hL+FQQUCAwHUwL+FQgUCAwMV3b+FTgYCBAE0ilm/hU8GAgQBNIpZMAoGCCqG
SM49BAMCA0gAMEUCIQDOefOPPwRmvyae6Yk/E4z0/7VKRyVH6mh+6ZPk84bTBAIg
CDxG2cHci7acvPave6jFDMt5GRpU4WG1SuZnBbEfr1A=
-----END CERTIFICATE-----`)

	block, _ := pem.Decode(pemBytes)
	if block == nil || block.Type != "CERTIFICATE" {
		log.Fatal("failed to decode certificate PEM block")
	}

	cert, err := x509.ParseCertificate(block.Bytes)
	if err != nil {
		log.Fatal(err)
	}

	ext := attestation.GetKeyExtension(cert)
	if ext == nil {
		log.Fatal("key attestation not found")
	}

	keyDesc, err := attestation.ParseExtension(ext.Value)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Got a %T\n", keyDesc)
	fmt.Printf("AttestationVersion: %q\n", keyDesc.AttestationVersion)
	fmt.Printf("AttestationSecurityLevel: %q\n", keyDesc.AttestationSecurityLevel)
	fmt.Printf("KeymasterVersion: %q\n", keyDesc.KeymasterVersion)
	fmt.Printf("KeymasterSecurityLevel: %q\n", keyDesc.KeymasterSecurityLevel)
	fmt.Printf("AttestationChallenge: %x (%s)\n", keyDesc.AttestationChallenge, keyDesc.AttestationChallenge)
	fmt.Printf("UniqueId: %x\n", keyDesc.UniqueId)
	fmt.Printf("SoftwareEnforced: %T\n", keyDesc.SoftwareEnforced)
	fmt.Printf("TeeEnforced: %T\n", keyDesc.TeeEnforced)
}

Installation

go install ./cmd/attest
attest -format der certificate.der.x509

Testing

make test

Resources

Documentation

Index

Constants

View Source
const (
	KAKeymasterVersion2  AttestationVersion = iota + 1 // Keymaster version 2.0
	KAKeymasterVersion3                                // Keymaster version 3.0
	KAKeymasterVersion4                                // Keymaster version 4.0
	KAKeymasterVersion41                               // Keymaster version 4.1
	KAKeyMintVersion1    = 100                         // KeyMint version 1.0
	KAKeyMintVersion2    = 200                         // KeyMint version 2.0
	KAKeyMintVersion3    = 300                         // KeyMint version 3.0
)
View Source
const (
	KeymasterVersion0  KeymasterVersion = iota // Keymaster version 0.2 or 0.3
	KeymasterVersion1                          // Keymaster version 1.0
	KeymasterVersion2                          // Keymaster version 2.0
	KeymasterVersion3                          // Keymaster version 3.0
	KeymasterVersion4                          // Keymaster version 4.0
	KeymasterVersion41 = 41                    // Keymaster version 4.1
	KeyMintVersion1    = 100                   // KeyMint version 1.0
	KeyMintVersion2    = 200                   // KeyMint version 2.0
	KeyMintVersion3    = 300                   // KeyMint version 3.0
)
View Source
const (
	Verified   = iota // Indicates a full chain of trust, which includes the bootloader, the boot partition, and all verified partitions.
	SelfSigned        // Indicates that the device-embedded certificate has verified the device's boot partition and that the signature is valid.
	Unverified        // Indicates that the user can modify the device freely. Therefore, the user is responsible for verifying the device's integrity.
	Failed            // Indicates that the device has failed verification. The attestation certificate should never use this value for VerifiedBootState.
)

VerifiedBootState is the state of verified boot.

View Source
const (
	TagPurpose                     = 1   // Corresponds to the Tag::PURPOSE authorization tag, which uses a tag ID value of 1.
	TagAlgorithm                   = 2   // Corresponds to the Tag::ALGORITHM authorization tag, which uses a tag ID value of 2. // In an attestation AuthorizationList object, the algorithm value is always RSA or EC.
	TagKeySize                     = 3   // Corresponds to the Tag::KEY_SIZE authorization tag, which uses a tag ID value of 3.
	TagDigest                      = 5   // Corresponds to the Tag::DIGEST authorization tag, which uses a tag ID value of 5.
	TagPadding                     = 6   // Corresponds to the Tag::PADDING authorization tag, which uses a tag ID value of 6.
	TagEcCurve                     = 10  // Corresponds to the Tag::EC_CURVE authorization tag, which uses a tag ID value of 10. // The set of parameters used to generate an elliptic curve (EC) key pair, which uses ECDSA for signing and verification, within the Android system keystore.
	TagRsaPublicExponent           = 200 // Corresponds to the Tag::RSA_PUBLIC_EXPONENT authorization tag, which uses a tag ID value of 200.
	TagMgfDigest                   = 203 // Present only in key attestation version >= 100. // Corresponds to the Tag::RSA_OAEP_MGF_DIGEST KeyMint authorization tag, which uses a tag ID value of 203.
	TagRollbackResistance          = 303 // Present only in key attestation version >= 3. // Corresponds to the Tag::ROLLBACK_RESISTANT authorization tag, which uses a tag ID value of 303.
	TagEarlyBootOnly               = 305 // Present only in key attestation version >= 4. // Corresponds to the Tag::EARLY_BOOT_ONLY authorization tag, which uses a tag ID value of 305.
	TagActiveDateTime              = 400 // Corresponds to the Tag::ACTIVE_DATETIME authorization tag, which uses a tag ID value of 400.
	TagOriginationExpireDateTime   = 401 // Corresponds to the Tag::ORIGINATION_EXPIRE_DATETIME Keymaster authorization tag, which uses a tag ID value of 401.
	TagUsageExpireDateTime         = 402 // Corresponds to the Tag::USAGE_EXPIRE_DATETIME authorization tag, which uses a tag ID value of 402.
	TagUsageCountLimit             = 405 // Corresponds to the Tag::USAGE_COUNT_LIMIT authorization tag, which uses a tag ID value of 405.
	TagNoAuthRequired              = 503 // Corresponds to the Tag::NO_AUTH_REQUIRED authorization tag, which uses a tag ID value of 503.
	TagUserAuthType                = 504 // Corresponds to the Tag::USER_AUTH_TYPE authorization tag, which uses a tag ID value of 504.
	TagAuthTimeout                 = 505 // Corresponds to the Tag::AUTH_TIMEOUT authorization tag, which uses a tag ID value of 505.
	TagAllowWhileOnBody            = 506 // Corresponds to the Tag::ALLOW_WHILE_ON_BODY authorization tag, which uses a tag ID value of 506. // Allows the key to be used after its authentication timeout period if the user is still wearing the device on their body. Note that a secure on-body sensor determines whether the device is being worn on the user's body.
	TagTrustedUserPresenceRequired = 507 // Present only in key attestation version >= 3. // Corresponds to the Tag::TRUSTED_USER_PRESENCE_REQUIRED authorization tag, which uses a tag ID value of 507. // Specifies that this key is usable only if the user has provided proof of physical presence. Several examples include the following: //     For a StrongBox key, a hardware button hardwired to a pin on the StrongBox device. //     For a TEE key, fingerprint authentication provides proof of presence as long as the TEE has exclusive control of the scanner and performs the fingerprint matching process.
	TagTrustedConfirmationRequired = 508 // Present only in key attestation version >= 3. // Corresponds to the Tag::TRUSTED_CONFIRMATION_REQUIRED authorization tag, which uses a tag ID value of 508. // Specifies that the key is usable only if the user provides confirmation of the data to be signed using an approval token. For more information about how to obtain user confirmation, see Android Protected Confirmation. // Note: This tag is only applicable to keys that use the SIGN purpose.
	TagUnlockedDeviceRequired      = 509 // Present only in key attestation version >= 3. // Corresponds to the Tag::UNLOCKED_DEVICE_REQUIRED authorization tag, which uses a tag ID value of 509.
	TagAllApplications             = 600 // Corresponds to the Tag::ALL_APPLICATIONS authorization tag, which uses a tag ID value of 600. // Indicates whether all apps on a device can access the key pair.
	TagApplicationId               = 601 // Corresponds to the Tag::APPLICATION_ID authorization tag, which uses a tag ID value of 601.
	TagCreationDateTime            = 701 // Corresponds to the Tag::CREATION_DATETIME authorization tag, which uses a tag ID value of 701.
	TagOrigin                      = 702 // Corresponds to the Tag::ORIGIN authorization tag, which uses a tag ID value of 702.
	TagRollbackResistant           = 703 // Present only in key attestation versions 1 and 2. // Corresponds to the Tag::ROLLBACK_RESISTANT authorization tag, which uses a tag ID value of 703.
	TagRootOfTrust                 = 704 // Corresponds to the Tag::ROOT_OF_TRUST authorization tag, which uses a tag ID value of 704. // For more details, see the section describing the RootOfTrust data structure.
	TagOsVersion                   = 705 // Corresponds to the Tag::OS_VERSION authorization tag, which uses a tag ID value of 705. // The version of the Android operating system associated with the Keymaster, specified as a six-digit integer. For example, version 8.1.0 is represented as 080100. // Only Keymaster version 1.0 or higher includes this value in the authorization list.
	TagOsPatchLevel                = 706 // Corresponds to the Tag::PATCHLEVEL authorization tag, which uses a tag ID value of 706. // The month and year associated with the security patch that is being used within the Keymaster, specified as a six-digit integer. For example, the August 2018 patch is represented as 201808. // Only Keymaster version 1.0 or higher includes this value in the authorization list.
	TagAttestationApplicationId    = 709 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_APPLICATION_ID Keymaster authorization tag, which uses a tag ID value of 709. // For more details, see the section describing the AttestationApplicationId data structure.
	TagAttestationIdBrand          = 710 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_BRAND Keymaster tag, which uses a tag ID value of 710.
	TagAttestationIdDevice         = 711 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_DEVICE Keymaster tag, which uses a tag ID value of 711.
	TagAttestationIdProduct        = 712 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_PRODUCT Keymaster tag, which uses a tag ID value of 712.
	TagAttestationIdSerial         = 713 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_SERIAL Keymaster tag, which uses a tag ID value of 713.
	TagAttestationIdImei           = 714 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_IMEI authorization tag, which uses a tag ID value of 714.
	TagAttestationIdMeid           = 715 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_MEID authorization tag, which uses a tag ID value of 715.
	TagAttestationIdManufacturer   = 716 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_MANUFACTURER authorization tag, which uses a tag ID value of 716.
	TagAttestationIdModel          = 717 // Present only in key attestation versions >= 2. // Corresponds to the Tag::ATTESTATION_ID_MODEL authorization tag, which uses a tag ID value of 717.
	TagVendorPatchLevel            = 718 // Present only in key attestation versions >= 3. // Corresponds to the Tag::VENDOR_PATCHLEVEL authorization tag, which uses a tag ID value of 718. // Specifies the vendor image security patch level that must be installed on the device for this key to be used. The value appears in the form YYYYMMDD, representing the date of the vendor security patch. For example, if a key were generated on an Android device with the vendor's August 1, 2018 security patch installed, this value would be 20180801.
	TagBootPatchLevel              = 719 // Present only in key attestation versions >= 3. // Corresponds to the Tag::BOOT_PATCHLEVEL authorization tag, which uses a tag ID value of 719. // Specifies the kernel image security patch level that must be installed on the device for this key to be used. The value appears in the form YYYYMMDD, representing the date of the system security patch. For example, if a key were generated on an Android device with the system's August 5, 2018 security patch installed, this value would be 20180805.
	TagDeviceUniqueAttestation     = 720 // Present only in key attestation versions >= 4. // Corresponds to the Tag::DEVICE_UNIQUE_ATTESTATION authorization tag, which uses a tag ID value of 720.
)
View Source
const (
	TagVerifiedBootKey   = iota // A secure hash of the key that verifies the system image. It is recommended that you use the SHA-256 algorithm for this hash.
	TagDeviceLocked             // True if the device's bootloader is locked, which enables Verified Boot checking and prevents an unsigned device image from being flashed onto the device. For more information about this feature, see the Verifying Boot documentation.
	TagVerifiedBootState        // The boot state of the device, according to the Verified Boot feature.
	TagVerifiedBootHash         // A digest of all data protected by Verified Boot.
)

Variables

View Source
var OIDKeyAttestationExtension = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 1, 17}

OIDKeyAttestationExtension is the key attestation extension.

Functions

func CreateExtension

func CreateExtension(template *KeyDescription) (*pkix.Extension, error)

CreateExtension creates a new Attestation extension based on a template.

func CreateKeyDescription

func CreateKeyDescription(template *KeyDescription) ([]byte, error)

CreateExtension creates a new KeyDescription based on a template.

func GetKeyExtension

func GetKeyExtension(crt *x509.Certificate) *pkix.Extension

GetKeyExtension returns the Key Attestation Extension.

Types

type Algorithm

type Algorithm uint
const (
	AlgoRSA  Algorithm = 1
	AlgoEC   Algorithm = 3
	AlgoAES  Algorithm = 32
	AlgoHMAC Algorithm = 128
)

func (Algorithm) String

func (a Algorithm) String() string

type AttestationApplicationId

type AttestationApplicationId struct {
	PackageInfos     []*AttestationPackageInfo
	SignatureDigests [][]byte
}

AttestationApplicationId reflects the Android platform's belief as to which apps are allowed to use the secret key material under attestation. The ID can comprise multiple packages if and only if multiple packages share the same UID.

type AttestationPackageInfo

type AttestationPackageInfo struct {
	PackageName string
	Version     int
}

AttestationPackageInfo reflects a package's name and version number.

type AttestationVersion

type AttestationVersion uint

AttestationVersion is the version of attestation schema.

func (AttestationVersion) String

func (v AttestationVersion) String() string

String returns the string representation.

type AuthorizationList

type AuthorizationList struct {
	Raw                         []byte
	Purpose                     []KeyPurpose
	Algorithm                   *Algorithm
	KeySize                     *int
	Digest                      []Digest
	Padding                     []PaddingMode
	EcCurve                     *EcCurve
	RsaPublicExponent           *int64
	RollbackResistance          bool
	ActiveDateTime              *int64
	OriginationExpireDateTime   *int
	UsageExpireDateTime         *int64
	NoAuthRequired              bool
	UserAuthType                *HardwareAuthenticatorType
	AuthTimeout                 *int32
	AllowWhileOnBody            bool
	TrustedUserPresenceRequired bool
	TrustedConfirmationRequired bool
	UnlockedDeviceRequired      bool
	AllApplications             bool
	ApplicationId               []byte
	CreationDateTime            *int
	Origin                      *KeyOrigin
	RollbackResistant           bool
	RootOfTrust                 *RootOfTrust
	OsVersion                   *int
	OsPatchLevel                *int
	AttestationApplicationId    *AttestationApplicationId
	AttestationIdBrand          []byte
	AttestationIdDevice         []byte
	AttestationIdProduct        []byte
	AttestationIdSerial         []byte
	AttestationIdImei           []byte
	AttestationIdMeid           []byte
	AttestationIdManufacturer   []byte
	AttestationIdModel          []byte
	VendorPatchLevel            *int
	BootPatchLevel              *int
}

AuthorizationList reflects the key pair's properties as defined in the Keymaster or KeyMint hardware abstraction layer.

type BlockMode

type BlockMode uint

BlockMode specifies the block cipher mode(s) with which the key may be used. This tag is only relevant to AES keys.

const (
	BlockModeECB BlockMode = iota
	BlockModeCBC
	BlockModeCTR
	BlockModeGCM BlockMode = 32
)

func (BlockMode) String

func (m BlockMode) String() string

type Digest

type Digest uint

Digest specifies the digest algorithms that may be used with the key to perform signing and verification operations. This tag is relevant to RSA, ECDSA and HMAC keys.

const (
	DigestNONE Digest = iota
	DigestMD5
	DigestSHA1
	DigestSHA_2_224
	DigestSHA_2_256
	DigestSHA_2_384
	DigestSHA_2_512
)

func (Digest) String

func (d Digest) String() string

type EcCurve

type EcCurve uint

EcCurve specifies the EC curves.

const (
	CurveP224 EcCurve = iota
	CurveP256
	CurveP384
	CurveP521
)

func (EcCurve) String

func (c EcCurve) String() string

type HardwareAuthenticatorType

type HardwareAuthenticatorType uint

HardwareAuthenticatorType specifies the types of user authenticators that may be used to authorize this key.

const (
	HwAuthTypeNone     HardwareAuthenticatorType = iota
	HwAuthTypePassword HardwareAuthenticatorType = 1 << iota
	HwAuthTypeFingerprint
	HwAuthTypeAny HardwareAuthenticatorType = HardwareAuthenticatorType(^uint32(0))
)

func (HardwareAuthenticatorType) String

func (t HardwareAuthenticatorType) String() string

type KeyBlobUsageRequirements

type KeyBlobUsageRequirements uint

KeyBlobUsageRequirements specifies the necessary system environment conditions for the generated key to be used.

const (
	KBURequirementsStandalone KeyBlobUsageRequirements = iota
	KBURequirementsRequiresFileSystem
)

func (KeyBlobUsageRequirements) String

func (r KeyBlobUsageRequirements) String() string

type KeyDescription

type KeyDescription struct {
	Raw                      []byte
	AttestationVersion       AttestationVersion
	AttestationSecurityLevel SecurityLevel
	KeymasterVersion         KeymasterVersion
	KeymasterSecurityLevel   SecurityLevel
	AttestationChallenge     []byte
	UniqueId                 []byte
	SoftwareEnforced         AuthorizationList
	TeeEnforced              AuthorizationList
}

KeyDescription reflects the attestation extension content.

This sequence of values presents general information about the key pair being verified through key attestation and provides easy access to additional details.

func ParseExtension

func ParseExtension(derBytes []byte) (*KeyDescription, error)

ParseExtension parses a single KeyDescription from the given ASN.1 DER data.

type KeyOrigin

type KeyOrigin uint

KeyOrigin specifies where the key was created, if known.

const (
	KeyOriginGenerated KeyOrigin = iota
	KeyOriginDerived
	KeyOriginImported
	KeyOriginUnknown
)

func (KeyOrigin) String

func (o KeyOrigin) String() string

type KeyPurpose

type KeyPurpose uint

KeyPurpose specifies the set of purposes for which the key may be used.

const (
	PurposeEncrypt KeyPurpose = iota
	PurposeDecrypt
	PurposeSign
	PurposeVerify
	PurposeDeriveKey
	PurposeWrapKey
)

func (KeyPurpose) String

func (p KeyPurpose) String() string

type KeymasterVersion

type KeymasterVersion uint

KeymasterVersion is the version of the Keymaster or KeyMint hardware abstraction layer.

func (KeymasterVersion) String

func (v KeymasterVersion) String() string

String returns the string representation.

type PaddingMode

type PaddingMode uint

PaddingMode specifies the padding modes that may be used with the key.

const (
	PaddingNone PaddingMode = iota
	PaddingRSA_OAEP
	PaddingRSA_PSS
	PaddingRSA_PKCS1_1_5_ENCRYPT
	PaddingRSA_PKCS1_1_5_SIGN
	PaddingPKCS7 PaddingMode = 64
)

func (PaddingMode) String

func (m PaddingMode) String() string

type RootOfTrust

type RootOfTrust struct {
	Raw               []byte
	VerifiedBootKey   []byte
	DeviceLocked      bool
	VerifiedBootState VerifiedBootState
	VerifiedBootHash  []byte
}

RootOfTrust reflects information on Android secure boot.

type SecurityLevel

type SecurityLevel uint

SecurityLevel reflects the ASN.1 data structure for SecurityLevel.

This data structure indicates the extent to which a software feature, such as a key pair, is protected based on its location within the device.

SecurityLevel ::= ENUMERATED {
    Software                   (0),
    TrustedEnvironment         (1),
    StrongBox                  (2),
}
const (
	Software SecurityLevel = iota
	TrustedEnvironment
	StrongBox
)

The security level of the attestation.

func (SecurityLevel) String

func (l SecurityLevel) String() string

String returns the string representation.

type VerifiedBootState

type VerifiedBootState uint

VerifiedBootState reflects the ASN.1 data structure for VerifiedBootState.

VerifiedBootState ::= ENUMERATED {
	Verified                   (0),
	SelfSigned                 (1),
	Unverified                 (2),
	Failed                     (3),
}

func (VerifiedBootState) String

func (s VerifiedBootState) String() string

String returns the string representation.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL