Debian Codegraph @

attest/application_key.go

raw
1 // Copyright 2021 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 package attest
16
17 import (
18 "crypto"
19 "crypto/ecdsa"
20 "crypto/rsa"
21 "fmt"
22 "io"
23 )
24
25 type key interface {
26 close(tpmBase) error
27 marshal() ([]byte, error)
28 certificationParameters() CertificationParameters
29 sign(tpmBase, []byte, crypto.PublicKey, crypto.SignerOpts) ([]byte, error)
30 decrypt(tpmBase, []byte) ([]byte, error)
31 blobs() ([]byte, []byte, error)
32 }
33
34 // Key represents a key which can be used for signing and decrypting
35 // outside-TPM objects.
36 type Key struct {
37 key key
38 pub crypto.PublicKey
39 tpm tpmBase
40 }
41
42 // signer implements crypto.Signer returned by Key.Private().
43 type signer struct {
44 key key
45 pub crypto.PublicKey
46 tpm tpmBase
47 }
48
49 // Sign signs digest with the TPM-stored private signing key.
50 func (s *signer) Sign(r io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
51 return s.key.sign(s.tpm, digest, s.pub, opts)
52 }
53
54 // Public returns the public key corresponding to the private signing key.
55 func (s *signer) Public() crypto.PublicKey {
56 return s.pub
57 }
58
59 // Algorithm indicates an asymmetric algorithm to be used.
60 type Algorithm string
61
62 // Algorithm types supported.
63 const (
64 ECDSA Algorithm = "ECDSA"
65 RSA Algorithm = "RSA"
66
67 // Windows specific ECDSA CNG algorithm identifiers.
68 // NOTE: Using ECDSA will default to P256.
69 // Ref: https://learn.microsoft.com/en-us/windows/win32/SecCNG/cng-algorithm-identifiers
70 P256 Algorithm = "ECDSA_P256"
71 P384 Algorithm = "ECDSA_P384"
72 P521 Algorithm = "ECDSA_P521"
73 )
74
75 // KeyConfig encapsulates parameters for minting keys.
76 type KeyConfig struct {
77 // Algorithm to be used, either RSA or ECDSA.
78 Algorithm Algorithm
79 // Size is used to specify the bit size of the key or elliptic curve. For
80 // example, '256' is used to specify curve P-256.
81 Size int
82 // Parent describes the Storage Root Key that will be used as a parent.
83 // If nil, the default SRK (i.e. RSA with handle 0x81000001) is assumed.
84 // Supported only by TPM 2.0 on Linux.
85 Parent *ParentKeyConfig
86 // QualifyingData is an optional data that will be included into
87 // a TPM-generated signature of the minted key.
88 // It may contain any data chosen by the caller.
89 QualifyingData []byte
90 }
91
92 // defaultConfig is used when no other configuration is specified.
93 var defaultConfig = &KeyConfig{
94 Algorithm: ECDSA,
95 Size: 256,
96 }
97
98 // Size returns the bit size associated with an algorithm.
99 func (a Algorithm) Size() int {
100 switch a {
101 case RSA:
102 return 2048
103 case ECDSA:
104 return 256
105 case P256:
106 return 256
107 case P384:
108 return 384
109 case P521:
110 return 521
111 default:
112 return 0
113 }
114 }
115
116 // Public returns the public key corresponding to the private key.
117 func (k *Key) Public() crypto.PublicKey {
118 return k.pub
119 }
120
121 // Private returns an object allowing to use the TPM-backed private key.
122 // For now it implements only crypto.Signer.
123 func (k *Key) Private(pub crypto.PublicKey) (crypto.PrivateKey, error) {
124 switch pub.(type) {
125 case *rsa.PublicKey:
126 if _, ok := k.pub.(*rsa.PublicKey); !ok {
127 return nil, fmt.Errorf("incompatible public key types: %T != %T", pub, k.pub)
128 }
129 case *ecdsa.PublicKey:
130 if _, ok := k.pub.(*ecdsa.PublicKey); !ok {
131 return nil, fmt.Errorf("incompatible public key types: %T != %T", pub, k.pub)
132 }
133 default:
134 return nil, fmt.Errorf("unsupported public key type: %T", pub)
135 }
136 return &signer{k.key, k.pub, k.tpm}, nil
137 }
138
139 // Close unloads the key from the system.
140 func (k *Key) Close() error {
141 return k.key.close(k.tpm)
142 }
143
144 // Marshal encodes the key in a format that can be loaded with tpm.LoadKey().
145 // This method exists to allow consumers to store the key persistently and load
146 // it as a later time. Users SHOULD NOT attempt to interpret or extract values
147 // from this blob.
148 func (k *Key) Marshal() ([]byte, error) {
149 return k.key.marshal()
150 }
151
152 // CertificationParameters returns information about the key required to
153 // verify key certification.
154 func (k *Key) CertificationParameters() CertificationParameters {
155 return k.key.certificationParameters()
156 }
157
158 // Blobs returns public and private blobs to be used by tpm2.Load().
159 func (k *Key) Blobs() (pub, priv []byte, err error) {
160 return k.key.blobs()
161 }