-
Notifications
You must be signed in to change notification settings - Fork 171
Implement example to generate a fake EK certificate. #94
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
Open
twitchy-jsonp
wants to merge
9
commits into
google:master
Choose a base branch
from
twitchy-jsonp:cert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a540283
Implement example to generate a fake EK certificate.
twitchy-jsonp 6fc8e1d
Correct test name
twitchy-jsonp 4ec831d
Fix year in license header
twitchy-jsonp b734523
Use 'Fake EK' as the default certificate org value
twitchy-jsonp 7a4dc54
Implement NVDefineSpace, NVWriteValue
twitchy-jsonp 590e9ed
Merge remote-tracking branch 'upstream/master' into cert
twitchy-jsonp 3e9dbb0
Use U32Bytes in new methods
twitchy-jsonp c26e619
Finish implementing example nvwrite
twitchy-jsonp 775541b
Refactoring pass 1
twitchy-jsonp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright (c) 2014, Google LLC All rights reserved. | ||
| // | ||
| // 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 main | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/sha1" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/binary" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "math/big" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/google/go-tpm/tpm" | ||
| ) | ||
|
|
||
| var ( | ||
| ownerAuthEnvVar = "TPM_OWNER_AUTH" | ||
|
|
||
| tpmPath = flag.String("tpm", "/dev/tpm0", "The path to the TPM device to use") | ||
| certPath = flag.String("cert", "ek.der", "The path to write the EK out to") | ||
| certOrg = flag.String("cert_org", "Acme Co", "The organization string to use in the EKCert") | ||
twitchy-jsonp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| func generateCertificate(pub *rsa.PublicKey) ([]byte, error) { | ||
| priv, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
| serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| template := x509.Certificate{ | ||
| SerialNumber: serialNumber, | ||
| Subject: pkix.Name{ | ||
| Organization: []string{*certOrg}, | ||
| }, | ||
| NotBefore: time.Now(), | ||
| NotAfter: time.Now().AddDate(1, 0, 0), | ||
| KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
| BasicConstraintsValid: true, | ||
| } | ||
|
|
||
| return x509.CreateCertificate(rand.Reader, &template, &template, pub, priv) | ||
| } | ||
|
|
||
| func writePCCert(f io.Writer, der []byte) error { | ||
| // Write the header as documented in: TCG PC Specific Implementation | ||
| // Specification, section 7.3.2. | ||
| if _, err := f.Write([]byte{0x10, 0x01, 0x00}); err != nil { | ||
| return err | ||
| } | ||
| certLength := make([]byte, 2) | ||
| binary.BigEndian.PutUint16(certLength, uint16(len(der))) | ||
| if _, err := f.Write(certLength); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err := f.Write(der) | ||
| return err | ||
| } | ||
|
|
||
| func main() { | ||
| flag.Parse() | ||
|
|
||
| var ownerAuth [20]byte | ||
| ownerInput := os.Getenv(ownerAuthEnvVar) | ||
| if ownerInput != "" { | ||
| oa := sha1.Sum([]byte(ownerInput)) | ||
| copy(ownerAuth[:], oa[:]) | ||
| } | ||
|
|
||
| rwc, err := tpm.OpenTPM(*tpmPath) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Couldn't open the TPM at %q: %v\n", *tpmPath, err) | ||
| return | ||
| } | ||
|
|
||
| pubEK, err := tpm.OwnerReadPubEK(rwc, ownerAuth) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Couldn't read the endorsement key: %v\n", err) | ||
| return | ||
| } | ||
| pub, err := tpm.DecodePublic(pubEK) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Couldn't decode the endorsement key: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| der, err := generateCertificate(pub.(*rsa.PublicKey)) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Couldn't generate a certificate: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| f, err := os.OpenFile(*certPath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0744) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Could open certificate path %q: %v\n", *certPath, err) | ||
| return | ||
| } | ||
| defer func() { | ||
| if err := f.Close(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to close %q: %v\n", *certPath, err) | ||
| } | ||
| }() | ||
|
|
||
| if err := writePCCert(f, der); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to write certificate: %v\n", err) | ||
| return | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.