Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the define a custom tpm device #1044

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions utils/cautils/acmeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,15 +830,26 @@
// TODO: refactor this to be cleaner by passing the TPM and/or key around
// instead of creating a new instance.
if af.tpmSigner != nil {
attestationURI := af.ctx.String("attestation-uri")

Check warning on line 833 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L833

Added line #L833 was not covered by tests
tpmStorageDirectory := af.ctx.String("tpm-storage-directory")
t, err := tpm.New(tpm.WithStore(tpmstorage.NewDirstore(tpmStorageDirectory)))

keyName, attURI, err := parseTPMAttestationURI(attestationURI)

Check warning on line 836 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L835-L836

Added lines #L835 - L836 were not covered by tests
if err != nil {
return nil, fmt.Errorf("failed initializing TPM: %w", err)
return nil, fmt.Errorf("failed parsing --attestation-uri: %w", err)
}

Check warning on line 839 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L838-L839

Added lines #L838 - L839 were not covered by tests

tpmOpts := []tpm.NewTPMOption{
tpm.WithStore(tpmstorage.NewDirstore(tpmStorageDirectory)),

Check warning on line 842 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L841-L842

Added lines #L841 - L842 were not covered by tests
}
keyName, err := parseTPMAttestationURI(af.ctx.String("attestation-uri"))
if device := attURI.Get("device"); device != "" {
tpmOpts = append(tpmOpts, tpm.WithDeviceName(device))
}

Check warning on line 846 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L844-L846

Added lines #L844 - L846 were not covered by tests

t, err := tpm.New(tpmOpts...)

Check warning on line 848 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L848

Added line #L848 was not covered by tests
if err != nil {
return nil, fmt.Errorf("failed parsing --attestation-uri: %w", err)
return nil, fmt.Errorf("failed initializing TPM: %w", err)

Check warning on line 850 in utils/cautils/acmeutils.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/acmeutils.go#L850

Added line #L850 was not covered by tests
}

ctx := tpm.NewContext(context.Background(), t)
key, err := t.GetKey(ctx, keyName)
if err != nil {
Expand Down
48 changes: 29 additions & 19 deletions utils/cautils/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,37 @@
)

func doTPMAttestation(clictx *cli.Context, ac *ca.ACMEClient, ch *acme.Challenge, identifier string, af *acmeFlow) error {
attestationURI := clictx.String("attestation-uri")

Check warning on line 40 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L40

Added line #L40 was not covered by tests
tpmStorageDirectory := clictx.String("tpm-storage-directory")
t, err := tpm.New(tpm.WithStore(tpmstorage.NewDirstore(tpmStorageDirectory)))
tpmAttestationCABaseURL := clictx.String("attestation-ca-url")
tpmAttestationCARootFile := clictx.String("attestation-ca-root")
tpmAttestationCAInsecure := clictx.Bool("attestation-ca-insecure")
insecure := clictx.Bool("insecure")

keyName, attURI, err := parseTPMAttestationURI(attestationURI)

Check warning on line 47 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L42-L47

Added lines #L42 - L47 were not covered by tests
if err != nil {
return fmt.Errorf("failed initializing TPM: %w", err)
return fmt.Errorf("failed parsing --attestation-uri: %w", err)

Check warning on line 49 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L49

Added line #L49 was not covered by tests
}

tpmAttestationCABaseURL := clictx.String("attestation-ca-url")
if tpmAttestationCABaseURL == "" {
return errs.RequiredFlag(clictx, "attestation-ca-url")
tpmAttestationCABaseURL = attURI.Get("attestation-ca-url")
if tpmAttestationCABaseURL == "" {
return errs.RequiredFlag(clictx, "attestation-ca-url")
}

Check warning on line 56 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L53-L56

Added lines #L53 - L56 were not covered by tests
}

tpmAttestationCARootFile := clictx.String("attestation-ca-root")
tpmAttestationCAInsecure := clictx.Bool("attestation-ca-insecure")
tpmOpts := []tpm.NewTPMOption{
tpm.WithStore(tpmstorage.NewDirstore(tpmStorageDirectory)),
}
if device := attURI.Get("device"); device != "" {
tpmOpts = append(tpmOpts, tpm.WithDeviceName(device))
}

Check warning on line 64 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L59-L64

Added lines #L59 - L64 were not covered by tests

t, err := tpm.New(tpmOpts...)
if err != nil {
return fmt.Errorf("failed initializing TPM: %w", err)
}

Check warning on line 69 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L66-L69

Added lines #L66 - L69 were not covered by tests

insecure := clictx.Bool("insecure")
kty, crv, size, err := utils.GetKeyDetailsFromCLI(clictx, insecure, "kty", "curve", "size")
if err != nil {
return fmt.Errorf("failed getting key details: %w", err)
Expand All @@ -78,12 +94,6 @@
return fmt.Errorf("unsupported key type: %q", kty)
}

attestationURI := clictx.String("attestation-uri")
keyName, err := parseTPMAttestationURI(attestationURI)
if err != nil {
return fmt.Errorf("failed parsing --attestation-uri: %w", err)
}

ctx := tpm.NewContext(context.Background(), t)
info, err := t.Info(ctx)
if err != nil {
Expand Down Expand Up @@ -185,23 +195,23 @@
}

// parseTPMAttestationURI parses attestation URIs for `tpmkms`.
func parseTPMAttestationURI(attestationURI string) (string, error) {
func parseTPMAttestationURI(attestationURI string) (string, *uri.URI, error) {

Check warning on line 198 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L198

Added line #L198 was not covered by tests
if attestationURI == "" {
return "", errors.New("attestation URI cannot be empty")
return "", nil, errors.New("attestation URI cannot be empty")

Check warning on line 200 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L200

Added line #L200 was not covered by tests
}
if !strings.HasPrefix(attestationURI, "tpmkms:") {
return "", fmt.Errorf("%q does not start with tpmkms", attestationURI)
return "", nil, fmt.Errorf("%q does not start with tpmkms", attestationURI)

Check warning on line 203 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L203

Added line #L203 was not covered by tests
}
u, err := uri.Parse(attestationURI)
if err != nil {
return "", fmt.Errorf("failed parsing %q: %w", attestationURI, err)
return "", nil, fmt.Errorf("failed parsing %q: %w", attestationURI, err)

Check warning on line 207 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L207

Added line #L207 was not covered by tests
}
var name string
if name = u.Get("name"); name == "" {
return "", fmt.Errorf("failed parsing %q: name is missing", attestationURI)
return "", nil, fmt.Errorf("failed parsing %q: name is missing", attestationURI)

Check warning on line 211 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L211

Added line #L211 was not covered by tests
}
// TODO(hs): more properties for objects created/attested in TPM
return name, nil
return name, u, nil

Check warning on line 214 in utils/cautils/tpm.go

View check run for this annotation

Codecov / codecov/patch

utils/cautils/tpm.go#L214

Added line #L214 was not covered by tests
}

// getAK returns an AK suitable for attesting the identifier that is requested. The
Expand Down