| 1 |
// Copyright (c) 2014, Google LLC All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of 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, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package tpm |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/google/go-tpm/tpmutil" |
| 22 |
) |
| 23 |
|
| 24 |
// Supported TPM commands. |
| 25 |
const ( |
| 26 |
tagPCRInfoLong uint16 = 0x06 |
| 27 |
tagNVAttributes uint16 = 0x0017 |
| 28 |
tagNVDataPublic uint16 = 0x0018 |
| 29 |
tagRQUCommand uint16 = 0x00C1 |
| 30 |
tagRQUAuth1Command uint16 = 0x00C2 |
| 31 |
tagRQUAuth2Command uint16 = 0x00C3 |
| 32 |
tagRSPCommand uint16 = 0x00C4 |
| 33 |
tagRSPAuth1Command uint16 = 0x00C5 |
| 34 |
tagRSPAuth2Command uint16 = 0x00C6 |
| 35 |
) |
| 36 |
|
| 37 |
// Supported TPM operations. |
| 38 |
const ( |
| 39 |
ordOIAP uint32 = 0x0000000A |
| 40 |
ordOSAP uint32 = 0x0000000B |
| 41 |
ordTakeOwnership uint32 = 0x0000000D |
| 42 |
ordExtend uint32 = 0x00000014 |
| 43 |
ordPCRRead uint32 = 0x00000015 |
| 44 |
ordQuote uint32 = 0x00000016 |
| 45 |
ordSeal uint32 = 0x00000017 |
| 46 |
ordUnseal uint32 = 0x00000018 |
| 47 |
ordCreateWrapKey uint32 = 0x0000001F |
| 48 |
ordGetPubKey uint32 = 0x00000021 |
| 49 |
ordCreateMigrationBlob uint32 = 0x00000028 |
| 50 |
ordAuthorizeMigrationKey uint32 = 0x0000002b |
| 51 |
ordSign uint32 = 0x0000003C |
| 52 |
ordQuote2 uint32 = 0x0000003E |
| 53 |
ordResetLockValue uint32 = 0x00000040 |
| 54 |
ordLoadKey2 uint32 = 0x00000041 |
| 55 |
ordGetRandom uint32 = 0x00000046 |
| 56 |
ordOwnerClear uint32 = 0x0000005B |
| 57 |
ordForceClear uint32 = 0x0000005D |
| 58 |
ordGetCapability uint32 = 0x00000065 |
| 59 |
ordCreateEndorsementKeyPair uint32 = 0x00000078 |
| 60 |
ordMakeIdentity uint32 = 0x00000079 |
| 61 |
ordActivateIdentity uint32 = 0x0000007A |
| 62 |
ordReadPubEK uint32 = 0x0000007C |
| 63 |
ordOwnerReadInternalPub uint32 = 0x00000081 |
| 64 |
ordStartup uint32 = 0x00000099 |
| 65 |
ordFlushSpecific uint32 = 0x000000BA |
| 66 |
ordNVDefineSpace uint32 = 0x000000CC |
| 67 |
ordPcrReset uint32 = 0x000000C8 |
| 68 |
ordNVWriteValue uint32 = 0x000000CD |
| 69 |
ordNVWriteValueAuth uint32 = 0x000000CE |
| 70 |
ordNVReadValue uint32 = 0x000000CF |
| 71 |
ordNVReadValueAuth uint32 = 0x000000D0 |
| 72 |
) |
| 73 |
|
| 74 |
// Capability types. |
| 75 |
const ( |
| 76 |
CapAlg uint32 = 0x00000002 |
| 77 |
CapProperty uint32 = 0x00000005 |
| 78 |
CapFlag uint32 = 0x00000004 |
| 79 |
CapNVList uint32 = 0x0000000D |
| 80 |
CapNVIndex uint32 = 0x00000011 |
| 81 |
CapHandle uint32 = 0x00000014 |
| 82 |
CapVersion uint32 = 0x0000001A |
| 83 |
) |
| 84 |
|
| 85 |
// SubCapabilities |
| 86 |
const ( |
| 87 |
SubCapPropManufacturer uint32 = 0x00000103 |
| 88 |
SubCapFlagPermanent uint32 = 0x00000108 |
| 89 |
) |
| 90 |
|
| 91 |
// Permission type |
| 92 |
type Permission uint32 |
| 93 |
|
| 94 |
// NV Permissions and Operations |
| 95 |
// Note: Permissions are summable |
| 96 |
const ( |
| 97 |
NVPerPPWrite Permission = 0x00000001 |
| 98 |
NVPerOwnerWrite Permission = 0x00000002 |
| 99 |
NVPerAuthWrite Permission = 0x00000004 |
| 100 |
NVPerWriteAll Permission = 0x00000800 |
| 101 |
// Warning: The Value 0x00001000 is |
| 102 |
// defined in the spec as |
| 103 |
// TPM_NV_PER_WRITEDEFINE, but it is |
| 104 |
// not included directly in this |
| 105 |
// code because it locks the given |
| 106 |
// NV Index permanently if used |
| 107 |
// incorrectly. This operation can't |
| 108 |
// be undone in any way. Do not use |
| 109 |
// this value unless you know what |
| 110 |
// you're doing! |
| 111 |
NVPerWriteSTClear Permission = 0x00002000 |
| 112 |
NVPerGlobalLock Permission = 0x00004000 |
| 113 |
NVPerPPRead Permission = 0x00008000 |
| 114 |
NVPerOwnerRead Permission = 0x00100000 |
| 115 |
NVPerAuthRead Permission = 0x00200000 |
| 116 |
NVPerReadSTClear Permission = 0x80000000 |
| 117 |
) |
| 118 |
|
| 119 |
// permMap : Map of TPM_NV_Permissions to its strings for convenience |
| 120 |
var permMap = map[Permission]string{ |
| 121 |
NVPerPPWrite: "PPWrite", |
| 122 |
NVPerOwnerWrite: "OwnerWrite", |
| 123 |
NVPerAuthWrite: "AuthWrite", |
| 124 |
NVPerWriteAll: "WriteAll", |
| 125 |
NVPerWriteSTClear: " WriteSTClear", |
| 126 |
NVPerGlobalLock: "GlobalLock", |
| 127 |
NVPerPPRead: "PPRead", |
| 128 |
NVPerOwnerRead: "OwnerRead", |
| 129 |
NVPerAuthRead: "AuthRead", |
| 130 |
NVPerReadSTClear: "ReadSTClear", |
| 131 |
} |
| 132 |
|
| 133 |
// String returns a textual representation of the set of permissions |
| 134 |
func (p Permission) String() string { |
| 135 |
var retString strings.Builder |
| 136 |
for iterator, item := range permMap { |
| 137 |
if (p & iterator) != 0 { |
| 138 |
retString.WriteString(item + " + ") |
| 139 |
} |
| 140 |
} |
| 141 |
if retString.String() == "" { |
| 142 |
return "Permission/s not found" |
| 143 |
} |
| 144 |
return strings.TrimSuffix(retString.String(), " + ") |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
// Entity types. The LSB gives the entity type, and the MSB (currently fixed to |
| 149 |
// 0x00) gives the ADIP type. ADIP type 0x00 is XOR. |
| 150 |
const ( |
| 151 |
_ uint16 = iota |
| 152 |
etKeyHandle |
| 153 |
etOwner |
| 154 |
etData |
| 155 |
etSRK |
| 156 |
etKey |
| 157 |
etRevoke |
| 158 |
) |
| 159 |
|
| 160 |
// Resource types. |
| 161 |
const ( |
| 162 |
_ uint32 = iota |
| 163 |
rtKey |
| 164 |
rtAuth |
| 165 |
rtHash |
| 166 |
rtTrans |
| 167 |
) |
| 168 |
|
| 169 |
// Locality type |
| 170 |
type Locality byte |
| 171 |
|
| 172 |
// Values of locality |
| 173 |
// Note: Localities are summable |
| 174 |
const ( |
| 175 |
LocZero Locality = 1 << iota |
| 176 |
LocOne |
| 177 |
LocTwo |
| 178 |
LocThree |
| 179 |
LocFour |
| 180 |
) |
| 181 |
|
| 182 |
// LocaMap maps Locality values to strings for convenience |
| 183 |
var locaMap = map[Locality]string{ |
| 184 |
LocZero: "Locality 0", |
| 185 |
LocOne: "Locality 1", |
| 186 |
LocTwo: "Locality 2", |
| 187 |
LocThree: "Locality 3", |
| 188 |
LocFour: "Locality 4", |
| 189 |
} |
| 190 |
|
| 191 |
// // String returns a textual representation of the set of Localities |
| 192 |
func (l Locality) String() string { |
| 193 |
var retString strings.Builder |
| 194 |
for iterator, item := range locaMap { |
| 195 |
if l&iterator != 0 { |
| 196 |
retString.WriteString(item + " + ") |
| 197 |
} |
| 198 |
} |
| 199 |
if retString.String() == "" { |
| 200 |
return fmt.Sprintf("locality %d", int(l)) |
| 201 |
} |
| 202 |
return strings.TrimSuffix(retString.String(), " + ") |
| 203 |
} |
| 204 |
|
| 205 |
// Entity values. |
| 206 |
const ( |
| 207 |
khSRK tpmutil.Handle = 0x40000000 |
| 208 |
khOwner tpmutil.Handle = 0x40000001 |
| 209 |
khRevokeTrust tpmutil.Handle = 0x40000002 |
| 210 |
khEK tpmutil.Handle = 0x40000006 |
| 211 |
) |
| 212 |
|
| 213 |
// Protocol IDs. |
| 214 |
const ( |
| 215 |
_ uint16 = iota |
| 216 |
pidOIAP |
| 217 |
pidOSAP |
| 218 |
pidADIP |
| 219 |
pidADCP |
| 220 |
pidOwner |
| 221 |
pidDSAP |
| 222 |
pidTransport |
| 223 |
) |
| 224 |
|
| 225 |
// Algorithm type for more convenient handling. |
| 226 |
// see Algorithm ID for possible values. |
| 227 |
type Algorithm uint32 |
| 228 |
|
| 229 |
// Algorithm ID values. |
| 230 |
const ( |
| 231 |
_ Algorithm = iota |
| 232 |
AlgRSA |
| 233 |
_ // was DES |
| 234 |
_ // was 3DES in EDE mode |
| 235 |
AlgSHA |
| 236 |
AlgHMAC |
| 237 |
AlgAES128 |
| 238 |
AlgMGF1 |
| 239 |
AlgAES192 |
| 240 |
AlgAES256 |
| 241 |
AlgXOR |
| 242 |
) |
| 243 |
|
| 244 |
// AlgMap Map of Algorithms to Strings for nicer output and comparisons, etc. |
| 245 |
var AlgMap = map[Algorithm]string{ |
| 246 |
AlgRSA: "RSA", |
| 247 |
AlgSHA: "SHA1", |
| 248 |
AlgHMAC: "HMAC", |
| 249 |
AlgAES128: "AER128", |
| 250 |
AlgMGF1: "MFG1", |
| 251 |
AlgAES192: "AES192", |
| 252 |
AlgAES256: "AES256", |
| 253 |
} |
| 254 |
|
| 255 |
func (a Algorithm) String() string { |
| 256 |
n, ok := AlgMap[a] |
| 257 |
if !ok { |
| 258 |
return "unknown_algorithm" |
| 259 |
} |
| 260 |
return n |
| 261 |
} |
| 262 |
|
| 263 |
// Encryption schemes. The values esNone and the two that contain the string |
| 264 |
// "RSA" are only valid under AlgRSA. The other two are symmetric encryption |
| 265 |
// schemes. |
| 266 |
const ( |
| 267 |
_ uint16 = iota |
| 268 |
esNone |
| 269 |
esRSAEsPKCSv15 |
| 270 |
esRSAEsOAEPSHA1MGF1 |
| 271 |
esSymCTR |
| 272 |
esSymOFB |
| 273 |
esSymCBCPKCS5 = 0xff // esSymCBCPKCS5 was taken from go-tspi |
| 274 |
) |
| 275 |
|
| 276 |
// Signature schemes. These are only valid under AlgRSA. |
| 277 |
const ( |
| 278 |
_ uint16 = iota |
| 279 |
ssNone |
| 280 |
ssRSASaPKCS1v15SHA1 |
| 281 |
ssRSASaPKCS1v15DER |
| 282 |
ssRSASaPKCS1v15INFO |
| 283 |
) |
| 284 |
|
| 285 |
// KeyUsage types for TPM_KEY (the key type). |
| 286 |
const ( |
| 287 |
keySigning uint16 = 0x0010 |
| 288 |
keyStorage uint16 = 0x0011 |
| 289 |
keyIdentity uint16 = 0x0012 |
| 290 |
keyAuthChange uint16 = 0x0013 |
| 291 |
keyBind uint16 = 0x0014 |
| 292 |
keyLegacy uint16 = 0x0015 |
| 293 |
keyMigrate uint16 = 0x0016 |
| 294 |
) |
| 295 |
|
| 296 |
const ( |
| 297 |
authNever byte = 0x00 |
| 298 |
authAlways byte = 0x01 |
| 299 |
authPrivUseOnly byte = 0x03 |
| 300 |
) |
| 301 |
|
| 302 |
// KeyFlags represents TPM_KEY_FLAGS. |
| 303 |
type KeyFlags uint32 |
| 304 |
|
| 305 |
const ( |
| 306 |
keyRedirection KeyFlags = 0x00000001 |
| 307 |
keyMigratable KeyFlags = 0x00000002 |
| 308 |
keyIsVolatile KeyFlags = 0x00000004 |
| 309 |
keyPcrIgnoredOnRead KeyFlags = 0x00000008 |
| 310 |
keyMigrateAuthority KeyFlags = 0x00000010 |
| 311 |
) |
| 312 |
|
| 313 |
// MigrationScheme represents TPM_MIGRATE_SCHEME. |
| 314 |
type MigrationScheme uint16 |
| 315 |
|
| 316 |
const ( |
| 317 |
msMigrate MigrationScheme = 0x0001 |
| 318 |
msRewrap MigrationScheme = 0x0002 |
| 319 |
msMaint MigrationScheme = 0x0003 |
| 320 |
msRestrictMigrate MigrationScheme = 0x0004 |
| 321 |
msRestrictApprove MigrationScheme = 0x0005 |
| 322 |
) |
| 323 |
|
| 324 |
// fixedQuote is the fixed constant string used in quoteInfo. |
| 325 |
var fixedQuote = [4]byte{byte('Q'), byte('U'), byte('O'), byte('T')} |
| 326 |
|
| 327 |
// quoteVersion is the fixed version string for quoteInfo. |
| 328 |
const quoteVersion uint32 = 0x01010000 |
| 329 |
|
| 330 |
// oaepLabel is the label used for OEAP encryption in esRSAEsOAEPSHA1MGF1 |
| 331 |
var oaepLabel = []byte{byte('T'), byte('C'), byte('P'), byte('A')} |