Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 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 | #include <cstdio> |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "base/command_line.h" |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 21 | #include "base/files/file_util.h" |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 22 | #include "keymaster/authorization_set.h" |
| 23 | #include "keystore/keystore_client_impl.h" |
| 24 | |
| 25 | using base::CommandLine; |
| 26 | using keymaster::AuthorizationSet; |
| 27 | using keymaster::AuthorizationSetBuilder; |
| 28 | using keystore::KeystoreClient; |
| 29 | |
| 30 | namespace { |
| 31 | |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 32 | struct TestCase { |
| 33 | std::string name; |
| 34 | bool required_for_brillo_pts; |
| 35 | AuthorizationSet parameters; |
| 36 | }; |
| 37 | |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 38 | void PrintUsageAndExit() { |
| 39 | printf("Usage: keystore_client_v2 <command> [options]\n"); |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 40 | printf("Commands: brillo-platform-test [--prefix=<test_name_prefix>]\n" |
| 41 | " list-brillo-tests\n" |
| 42 | " add-entropy --input=<entropy>\n" |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 43 | " generate --name=<key_name>\n" |
| 44 | " get-chars --name=<key_name>\n" |
| 45 | " export --name=<key_name>\n" |
| 46 | " delete --name=<key_name>\n" |
| 47 | " delete-all\n" |
| 48 | " exists --name=<key_name>\n" |
| 49 | " list [--prefix=<key_name_prefix>]\n" |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 50 | " sign-verify --name=<key_name>\n" |
| 51 | " [en|de]crypt --name=<key_name> --in=<file> --out=<file>\n"); |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 52 | exit(1); |
| 53 | } |
| 54 | |
| 55 | std::unique_ptr<KeystoreClient> CreateKeystoreInstance() { |
| 56 | return std::unique_ptr<KeystoreClient>(new keystore::KeystoreClientImpl); |
| 57 | } |
| 58 | |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 59 | const char* StringifyTag(keymaster_tag_t tag) { |
| 60 | switch (tag) { |
| 61 | case KM_TAG_INVALID: |
| 62 | return "KM_TAG_INVALID"; |
| 63 | case KM_TAG_PURPOSE: |
| 64 | return "KM_TAG_PURPOSE"; |
| 65 | case KM_TAG_ALGORITHM: |
| 66 | return "KM_TAG_ALGORITHM"; |
| 67 | case KM_TAG_KEY_SIZE: |
| 68 | return "KM_TAG_KEY_SIZE"; |
| 69 | case KM_TAG_BLOCK_MODE: |
| 70 | return "KM_TAG_BLOCK_MODE"; |
| 71 | case KM_TAG_DIGEST: |
| 72 | return "KM_TAG_DIGEST"; |
| 73 | case KM_TAG_PADDING: |
| 74 | return "KM_TAG_PADDING"; |
| 75 | case KM_TAG_CALLER_NONCE: |
| 76 | return "KM_TAG_CALLER_NONCE"; |
| 77 | case KM_TAG_MIN_MAC_LENGTH: |
| 78 | return "KM_TAG_MIN_MAC_LENGTH"; |
| 79 | case KM_TAG_RSA_PUBLIC_EXPONENT: |
| 80 | return "KM_TAG_RSA_PUBLIC_EXPONENT"; |
| 81 | case KM_TAG_BLOB_USAGE_REQUIREMENTS: |
| 82 | return "KM_TAG_BLOB_USAGE_REQUIREMENTS"; |
| 83 | case KM_TAG_BOOTLOADER_ONLY: |
| 84 | return "KM_TAG_BOOTLOADER_ONLY"; |
| 85 | case KM_TAG_ACTIVE_DATETIME: |
| 86 | return "KM_TAG_ACTIVE_DATETIME"; |
| 87 | case KM_TAG_ORIGINATION_EXPIRE_DATETIME: |
| 88 | return "KM_TAG_ORIGINATION_EXPIRE_DATETIME"; |
| 89 | case KM_TAG_USAGE_EXPIRE_DATETIME: |
| 90 | return "KM_TAG_USAGE_EXPIRE_DATETIME"; |
| 91 | case KM_TAG_MIN_SECONDS_BETWEEN_OPS: |
| 92 | return "KM_TAG_MIN_SECONDS_BETWEEN_OPS"; |
| 93 | case KM_TAG_MAX_USES_PER_BOOT: |
| 94 | return "KM_TAG_MAX_USES_PER_BOOT"; |
| 95 | case KM_TAG_ALL_USERS: |
| 96 | return "KM_TAG_ALL_USERS"; |
| 97 | case KM_TAG_USER_ID: |
| 98 | return "KM_TAG_USER_ID"; |
| 99 | case KM_TAG_USER_SECURE_ID: |
| 100 | return "KM_TAG_USER_SECURE_ID"; |
| 101 | case KM_TAG_NO_AUTH_REQUIRED: |
| 102 | return "KM_TAG_NO_AUTH_REQUIRED"; |
| 103 | case KM_TAG_USER_AUTH_TYPE: |
| 104 | return "KM_TAG_USER_AUTH_TYPE"; |
| 105 | case KM_TAG_AUTH_TIMEOUT: |
| 106 | return "KM_TAG_AUTH_TIMEOUT"; |
| 107 | case KM_TAG_ALL_APPLICATIONS: |
| 108 | return "KM_TAG_ALL_APPLICATIONS"; |
| 109 | case KM_TAG_APPLICATION_ID: |
| 110 | return "KM_TAG_APPLICATION_ID"; |
| 111 | case KM_TAG_APPLICATION_DATA: |
| 112 | return "KM_TAG_APPLICATION_DATA"; |
| 113 | case KM_TAG_CREATION_DATETIME: |
| 114 | return "KM_TAG_CREATION_DATETIME"; |
| 115 | case KM_TAG_ORIGIN: |
| 116 | return "KM_TAG_ORIGIN"; |
| 117 | case KM_TAG_ROLLBACK_RESISTANT: |
| 118 | return "KM_TAG_ROLLBACK_RESISTANT"; |
| 119 | case KM_TAG_ROOT_OF_TRUST: |
| 120 | return "KM_TAG_ROOT_OF_TRUST"; |
| 121 | case KM_TAG_ASSOCIATED_DATA: |
| 122 | return "KM_TAG_ASSOCIATED_DATA"; |
| 123 | case KM_TAG_NONCE: |
| 124 | return "KM_TAG_NONCE"; |
| 125 | case KM_TAG_AUTH_TOKEN: |
| 126 | return "KM_TAG_AUTH_TOKEN"; |
| 127 | case KM_TAG_MAC_LENGTH: |
| 128 | return "KM_TAG_MAC_LENGTH"; |
Shawn Willden | 0ba9f6e | 2015-11-23 08:56:33 -0700 | [diff] [blame] | 129 | case KM_TAG_KDF: |
| 130 | return "KM_TAG_KDF"; |
| 131 | case KM_TAG_EC_CURVE: |
| 132 | return "KM_TAG_EC_CURVE"; |
| 133 | case KM_TAG_ECIES_SINGLE_HASH_MODE: |
| 134 | return "KM_TAG_ECIES_SINGLE_HASH_MODE"; |
Shawn Willden | 9d9318e | 2016-01-05 22:51:21 -0700 | [diff] [blame] | 135 | case KM_TAG_OS_VERSION: |
| 136 | return "KM_TAG_OS_VERSION"; |
| 137 | case KM_TAG_OS_PATCHLEVEL: |
| 138 | return "KM_TAG_OS_PATCHLEVEL"; |
| 139 | case KM_TAG_EXPORTABLE: |
| 140 | return "KM_TAG_EXPORTABLE"; |
| 141 | case KM_TAG_UNIQUE_ID: |
| 142 | return "KM_TAG_UNIQUE_ID"; |
| 143 | case KM_TAG_INCLUDE_UNIQUE_ID: |
| 144 | return "KM_TAG_INCLUDE_UNIQUE_ID"; |
| 145 | case KM_TAG_RESET_SINCE_ID_ROTATION: |
| 146 | return "KM_TAG_RESET_SINCE_ID_ROTATION"; |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 147 | } |
| 148 | return "<Unknown>"; |
| 149 | } |
| 150 | |
| 151 | void PrintTags(const AuthorizationSet& parameters) { |
| 152 | const keymaster_key_param_t* iter = nullptr; |
| 153 | for (iter = parameters.begin(); iter != parameters.end(); ++iter) { |
| 154 | printf(" %s\n", StringifyTag(iter->tag)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void PrintKeyCharacteristics(const AuthorizationSet& hardware_enforced_characteristics, |
| 159 | const AuthorizationSet& software_enforced_characteristics) { |
| 160 | printf("Hardware:\n"); |
| 161 | PrintTags(hardware_enforced_characteristics); |
| 162 | printf("Software:\n"); |
| 163 | PrintTags(software_enforced_characteristics); |
| 164 | } |
| 165 | |
| 166 | bool TestKey(const std::string& name, bool required, const AuthorizationSet& parameters) { |
| 167 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 168 | AuthorizationSet hardware_enforced_characteristics; |
| 169 | AuthorizationSet software_enforced_characteristics; |
| 170 | int32_t result = keystore->generateKey("tmp", parameters, &hardware_enforced_characteristics, |
| 171 | &software_enforced_characteristics); |
| 172 | if (result != KM_ERROR_OK) { |
| 173 | LOG(ERROR) << "Failed to generate key: " << result; |
| 174 | printf("%s Result: ABORT\n", name.c_str()); |
| 175 | return false; |
| 176 | } |
| 177 | result = keystore->deleteKey("tmp"); |
| 178 | if (result != KM_ERROR_OK) { |
| 179 | LOG(ERROR) << "Failed to delete key: " << result; |
| 180 | printf("%s Result: ABORT\n", name.c_str()); |
| 181 | return false; |
| 182 | } |
| 183 | printf("===============================================================\n"); |
| 184 | printf("%s Key Characteristics:\n", name.c_str()); |
| 185 | PrintKeyCharacteristics(hardware_enforced_characteristics, software_enforced_characteristics); |
| 186 | bool hardware_backed = (hardware_enforced_characteristics.size() > 0); |
| 187 | if (software_enforced_characteristics.GetTagCount(KM_TAG_PURPOSE) > 0 || |
| 188 | software_enforced_characteristics.GetTagCount(KM_TAG_ALGORITHM) > 0 || |
| 189 | software_enforced_characteristics.GetTagCount(KM_TAG_KEY_SIZE) > 0 || |
| 190 | software_enforced_characteristics.GetTagCount(KM_TAG_RSA_PUBLIC_EXPONENT) > 0 || |
| 191 | software_enforced_characteristics.GetTagCount(KM_TAG_DIGEST) > 0 || |
| 192 | software_enforced_characteristics.GetTagCount(KM_TAG_PADDING) > 0 || |
| 193 | software_enforced_characteristics.GetTagCount(KM_TAG_BLOCK_MODE) > 0) { |
| 194 | VLOG(1) << "Hardware-backed key but required characteristics enforced in software."; |
| 195 | hardware_backed = false; |
| 196 | } |
| 197 | const char kBoldRedFail[] = "\033[1;31mFAIL\033[0m"; |
| 198 | const char kBoldGreenPass[] = "\033[1;32mPASS\033[0m"; |
| 199 | const char kBoldYellowWarn[] = "\033[1;33mWARN\033[0m"; |
| 200 | printf("[%s] %s\n", |
| 201 | hardware_backed ? kBoldGreenPass : (required ? kBoldRedFail : kBoldYellowWarn), |
| 202 | name.c_str()); |
| 203 | |
| 204 | return (hardware_backed || !required); |
| 205 | } |
| 206 | |
| 207 | AuthorizationSet GetRSASignParameters(uint32_t key_size, bool sha256_only) { |
| 208 | AuthorizationSetBuilder parameters; |
| 209 | parameters.RsaSigningKey(key_size, 65537) |
| 210 | .Digest(KM_DIGEST_SHA_2_256) |
| 211 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN) |
| 212 | .Padding(KM_PAD_RSA_PSS) |
| 213 | .Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 214 | if (!sha256_only) { |
| 215 | parameters.Digest(KM_DIGEST_SHA_2_224) |
| 216 | .Digest(KM_DIGEST_SHA_2_384) |
| 217 | .Digest(KM_DIGEST_SHA_2_512); |
| 218 | } |
| 219 | return parameters.build(); |
| 220 | } |
| 221 | |
| 222 | AuthorizationSet GetRSAEncryptParameters(uint32_t key_size) { |
| 223 | AuthorizationSetBuilder parameters; |
| 224 | parameters.RsaEncryptionKey(key_size, 65537) |
| 225 | .Padding(KM_PAD_RSA_PKCS1_1_5_ENCRYPT) |
| 226 | .Padding(KM_PAD_RSA_OAEP) |
| 227 | .Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 228 | return parameters.build(); |
| 229 | } |
| 230 | |
| 231 | AuthorizationSet GetECDSAParameters(uint32_t key_size, bool sha256_only) { |
| 232 | AuthorizationSetBuilder parameters; |
| 233 | parameters.EcdsaSigningKey(key_size) |
| 234 | .Digest(KM_DIGEST_SHA_2_256) |
| 235 | .Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 236 | if (!sha256_only) { |
| 237 | parameters.Digest(KM_DIGEST_SHA_2_224) |
| 238 | .Digest(KM_DIGEST_SHA_2_384) |
| 239 | .Digest(KM_DIGEST_SHA_2_512); |
| 240 | } |
| 241 | return parameters.build(); |
| 242 | } |
| 243 | |
| 244 | AuthorizationSet GetAESParameters(uint32_t key_size, bool with_gcm_mode) { |
| 245 | AuthorizationSetBuilder parameters; |
| 246 | parameters.AesEncryptionKey(key_size).Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 247 | if (with_gcm_mode) { |
| 248 | parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM) |
| 249 | .Authorization(keymaster::TAG_MIN_MAC_LENGTH, 128); |
| 250 | } else { |
| 251 | parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_ECB); |
| 252 | parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_CBC); |
| 253 | parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_CTR); |
| 254 | } |
| 255 | return parameters.build(); |
| 256 | } |
| 257 | |
| 258 | AuthorizationSet GetHMACParameters(uint32_t key_size, keymaster_digest_t digest) { |
| 259 | AuthorizationSetBuilder parameters; |
| 260 | parameters.HmacKey(key_size) |
| 261 | .Digest(digest) |
| 262 | .Authorization(keymaster::TAG_MIN_MAC_LENGTH, 224) |
| 263 | .Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 264 | return parameters.build(); |
| 265 | } |
| 266 | |
| 267 | std::vector<TestCase> GetTestCases() { |
| 268 | TestCase test_cases[] = { |
| 269 | {"RSA-2048 Sign", true, GetRSASignParameters(2048, true)}, |
| 270 | {"RSA-2048 Sign (more digests)", false, GetRSASignParameters(2048, false)}, |
| 271 | {"RSA-3072 Sign", false, GetRSASignParameters(3072, false)}, |
| 272 | {"RSA-4096 Sign", false, GetRSASignParameters(4096, false)}, |
| 273 | {"RSA-2048 Encrypt", true, GetRSAEncryptParameters(2048)}, |
| 274 | {"RSA-3072 Encrypt", false, GetRSAEncryptParameters(3072)}, |
| 275 | {"RSA-4096 Encrypt", false, GetRSAEncryptParameters(4096)}, |
| 276 | {"ECDSA-P256 Sign", true, GetECDSAParameters(256, true)}, |
| 277 | {"ECDSA-P256 Sign (more digests)", false, GetECDSAParameters(256, false)}, |
| 278 | {"ECDSA-P224 Sign", false, GetECDSAParameters(224, false)}, |
| 279 | {"ECDSA-P384 Sign", false, GetECDSAParameters(384, false)}, |
| 280 | {"ECDSA-P521 Sign", false, GetECDSAParameters(521, false)}, |
| 281 | {"AES-128", true, GetAESParameters(128, false)}, |
| 282 | {"AES-256", true, GetAESParameters(256, false)}, |
| 283 | {"AES-128-GCM", false, GetAESParameters(128, true)}, |
| 284 | {"AES-256-GCM", false, GetAESParameters(256, true)}, |
| 285 | {"HMAC-SHA256-16", true, GetHMACParameters(16, KM_DIGEST_SHA_2_256)}, |
| 286 | {"HMAC-SHA256-32", true, GetHMACParameters(32, KM_DIGEST_SHA_2_256)}, |
| 287 | {"HMAC-SHA256-64", false, GetHMACParameters(64, KM_DIGEST_SHA_2_256)}, |
| 288 | {"HMAC-SHA224-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_224)}, |
| 289 | {"HMAC-SHA384-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_384)}, |
| 290 | {"HMAC-SHA512-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_512)}, |
| 291 | }; |
| 292 | return std::vector<TestCase>(&test_cases[0], &test_cases[arraysize(test_cases)]); |
| 293 | } |
| 294 | |
| 295 | int BrilloPlatformTest(const std::string& prefix) { |
| 296 | int test_count = 0; |
| 297 | int fail_count = 0; |
| 298 | std::vector<TestCase> test_cases = GetTestCases(); |
| 299 | for (const auto& test_case : test_cases) { |
| 300 | if (!prefix.empty() && test_case.name.find(prefix) != 0) { |
| 301 | continue; |
| 302 | } |
| 303 | ++test_count; |
| 304 | if (!TestKey(test_case.name, test_case.required_for_brillo_pts, test_case.parameters)) { |
| 305 | VLOG(1) << "Test failed: " << test_case.name; |
| 306 | ++fail_count; |
| 307 | } |
| 308 | } |
| 309 | return fail_count; |
| 310 | } |
| 311 | |
| 312 | int ListTestCases() { |
| 313 | const char kBoldGreenRequired[] = "\033[1;32mREQUIRED\033[0m"; |
| 314 | const char kBoldYellowRecommended[] = "\033[1;33mRECOMMENDED\033[0m"; |
| 315 | std::vector<TestCase> test_cases = GetTestCases(); |
| 316 | for (const auto& test_case : test_cases) { |
| 317 | printf("%s : %s\n", test_case.name.c_str(), |
| 318 | test_case.required_for_brillo_pts ? kBoldGreenRequired : kBoldYellowRecommended); |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 323 | std::string ReadFile(const std::string& filename) { |
| 324 | std::string content; |
| 325 | base::FilePath path(filename); |
| 326 | if (!base::ReadFileToString(path, &content)) { |
| 327 | printf("Failed to read file: %s\n", filename.c_str()); |
| 328 | exit(1); |
| 329 | } |
| 330 | return content; |
| 331 | } |
| 332 | |
| 333 | void WriteFile(const std::string& filename, const std::string& content) { |
| 334 | base::FilePath path(filename); |
| 335 | int size = content.size(); |
| 336 | if (base::WriteFile(path, content.data(), size) != size) { |
| 337 | printf("Failed to write file: %s\n", filename.c_str()); |
| 338 | exit(1); |
| 339 | } |
| 340 | } |
| 341 | |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 342 | int AddEntropy(const std::string& input) { |
| 343 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 344 | int32_t result = keystore->addRandomNumberGeneratorEntropy(input); |
| 345 | printf("AddEntropy: %d\n", result); |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | int GenerateKey(const std::string& name) { |
| 350 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 351 | AuthorizationSetBuilder params; |
| 352 | params.RsaSigningKey(2048, 65537) |
| 353 | .Digest(KM_DIGEST_SHA_2_224) |
| 354 | .Digest(KM_DIGEST_SHA_2_256) |
| 355 | .Digest(KM_DIGEST_SHA_2_384) |
| 356 | .Digest(KM_DIGEST_SHA_2_512) |
| 357 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN) |
| 358 | .Padding(KM_PAD_RSA_PSS) |
| 359 | .Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 360 | AuthorizationSet hardware_enforced_characteristics; |
| 361 | AuthorizationSet software_enforced_characteristics; |
| 362 | int32_t result = keystore->generateKey(name, params.build(), &hardware_enforced_characteristics, |
| 363 | &software_enforced_characteristics); |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 364 | printf("GenerateKey: %d\n", result); |
| 365 | if (result == KM_ERROR_OK) { |
| 366 | PrintKeyCharacteristics(hardware_enforced_characteristics, |
| 367 | software_enforced_characteristics); |
| 368 | } |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 369 | return result; |
| 370 | } |
| 371 | |
| 372 | int GetCharacteristics(const std::string& name) { |
| 373 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 374 | AuthorizationSet hardware_enforced_characteristics; |
| 375 | AuthorizationSet software_enforced_characteristics; |
| 376 | int32_t result = keystore->getKeyCharacteristics(name, &hardware_enforced_characteristics, |
| 377 | &software_enforced_characteristics); |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 378 | printf("GetCharacteristics: %d\n", result); |
| 379 | if (result == KM_ERROR_OK) { |
| 380 | PrintKeyCharacteristics(hardware_enforced_characteristics, |
| 381 | software_enforced_characteristics); |
| 382 | } |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 383 | return result; |
| 384 | } |
| 385 | |
| 386 | int ExportKey(const std::string& name) { |
| 387 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 388 | std::string data; |
| 389 | int32_t result = keystore->exportKey(KM_KEY_FORMAT_X509, name, &data); |
| 390 | printf("ExportKey: %d (%zu)\n", result, data.size()); |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | int DeleteKey(const std::string& name) { |
| 395 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 396 | int32_t result = keystore->deleteKey(name); |
| 397 | printf("DeleteKey: %d\n", result); |
| 398 | return result; |
| 399 | } |
| 400 | |
| 401 | int DeleteAllKeys() { |
| 402 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 403 | int32_t result = keystore->deleteAllKeys(); |
| 404 | printf("DeleteAllKeys: %d\n", result); |
| 405 | return result; |
| 406 | } |
| 407 | |
| 408 | int DoesKeyExist(const std::string& name) { |
| 409 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 410 | printf("DoesKeyExist: %s\n", keystore->doesKeyExist(name) ? "yes" : "no"); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | int List(const std::string& prefix) { |
| 415 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 416 | std::vector<std::string> key_list; |
| 417 | if (!keystore->listKeys(prefix, &key_list)) { |
| 418 | printf("ListKeys failed.\n"); |
| 419 | return 1; |
| 420 | } |
| 421 | printf("Keys:\n"); |
| 422 | for (const auto& key_name : key_list) { |
| 423 | printf(" %s\n", key_name.c_str()); |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int SignAndVerify(const std::string& name) { |
| 429 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 430 | AuthorizationSetBuilder sign_params; |
| 431 | sign_params.Padding(KM_PAD_RSA_PKCS1_1_5_SIGN); |
| 432 | sign_params.Digest(KM_DIGEST_SHA_2_256); |
| 433 | AuthorizationSet output_params; |
| 434 | keymaster_operation_handle_t handle; |
| 435 | int32_t result = keystore->beginOperation(KM_PURPOSE_SIGN, name, sign_params.build(), |
| 436 | &output_params, &handle); |
| 437 | if (result != KM_ERROR_OK) { |
| 438 | printf("Sign: BeginOperation failed: %d\n", result); |
| 439 | return result; |
| 440 | } |
| 441 | AuthorizationSet empty_params; |
| 442 | size_t num_input_bytes_consumed; |
| 443 | std::string output_data; |
| 444 | result = keystore->updateOperation(handle, empty_params, "data_to_sign", |
| 445 | &num_input_bytes_consumed, &output_params, &output_data); |
| 446 | if (result != KM_ERROR_OK) { |
| 447 | printf("Sign: UpdateOperation failed: %d\n", result); |
| 448 | return result; |
| 449 | } |
| 450 | result = keystore->finishOperation(handle, empty_params, std::string() /*signature_to_verify*/, |
| 451 | &output_params, &output_data); |
| 452 | if (result != KM_ERROR_OK) { |
| 453 | printf("Sign: FinishOperation failed: %d\n", result); |
| 454 | return result; |
| 455 | } |
| 456 | printf("Sign: %zu bytes.\n", output_data.size()); |
| 457 | // We have a signature, now verify it. |
| 458 | std::string signature_to_verify = output_data; |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 459 | output_data.clear(); |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 460 | result = keystore->beginOperation(KM_PURPOSE_VERIFY, name, sign_params.build(), &output_params, |
| 461 | &handle); |
| 462 | if (result != KM_ERROR_OK) { |
| 463 | printf("Verify: BeginOperation failed: %d\n", result); |
| 464 | return result; |
| 465 | } |
| 466 | result = keystore->updateOperation(handle, empty_params, "data_to_sign", |
| 467 | &num_input_bytes_consumed, &output_params, &output_data); |
| 468 | if (result != KM_ERROR_OK) { |
| 469 | printf("Verify: UpdateOperation failed: %d\n", result); |
| 470 | return result; |
| 471 | } |
| 472 | result = keystore->finishOperation(handle, empty_params, signature_to_verify, &output_params, |
| 473 | &output_data); |
| 474 | if (result == KM_ERROR_VERIFICATION_FAILED) { |
| 475 | printf("Verify: Failed to verify signature.\n"); |
| 476 | return result; |
| 477 | } |
| 478 | if (result != KM_ERROR_OK) { |
| 479 | printf("Verify: FinishOperation failed: %d\n", result); |
| 480 | return result; |
| 481 | } |
| 482 | printf("Verify: OK\n"); |
| 483 | return 0; |
| 484 | } |
| 485 | |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 486 | int Encrypt(const std::string& key_name, const std::string& input_filename, |
| 487 | const std::string& output_filename) { |
| 488 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 489 | std::string input = ReadFile(input_filename); |
| 490 | std::string output; |
| 491 | if (!keystore->encryptWithAuthentication(key_name, input, &output)) { |
| 492 | printf("EncryptWithAuthentication failed.\n"); |
| 493 | return 1; |
| 494 | } |
| 495 | WriteFile(output_filename, output); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | int Decrypt(const std::string& key_name, const std::string& input_filename, |
| 500 | const std::string& output_filename) { |
| 501 | std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance(); |
| 502 | std::string input = ReadFile(input_filename); |
| 503 | std::string output; |
| 504 | if (!keystore->decryptWithAuthentication(key_name, input, &output)) { |
| 505 | printf("DecryptWithAuthentication failed.\n"); |
| 506 | return 1; |
| 507 | } |
| 508 | WriteFile(output_filename, output); |
| 509 | return 0; |
| 510 | } |
| 511 | |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 512 | } // namespace |
| 513 | |
| 514 | int main(int argc, char** argv) { |
| 515 | CommandLine::Init(argc, argv); |
| 516 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 517 | CommandLine::StringVector args = command_line->GetArgs(); |
| 518 | if (args.empty()) { |
| 519 | PrintUsageAndExit(); |
| 520 | } |
Darren Krahn | a9474ab | 2015-11-03 14:38:53 -0800 | [diff] [blame] | 521 | if (args[0] == "brillo-platform-test") { |
| 522 | return BrilloPlatformTest(command_line->GetSwitchValueASCII("prefix")); |
| 523 | } else if (args[0] == "list-brillo-tests") { |
| 524 | return ListTestCases(); |
| 525 | } else if (args[0] == "add-entropy") { |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 526 | return AddEntropy(command_line->GetSwitchValueASCII("input")); |
| 527 | } else if (args[0] == "generate") { |
| 528 | return GenerateKey(command_line->GetSwitchValueASCII("name")); |
| 529 | } else if (args[0] == "get-chars") { |
| 530 | return GetCharacteristics(command_line->GetSwitchValueASCII("name")); |
| 531 | } else if (args[0] == "export") { |
| 532 | return ExportKey(command_line->GetSwitchValueASCII("name")); |
| 533 | } else if (args[0] == "delete") { |
| 534 | return DeleteKey(command_line->GetSwitchValueASCII("name")); |
| 535 | } else if (args[0] == "delete-all") { |
| 536 | return DeleteAllKeys(); |
| 537 | } else if (args[0] == "exists") { |
| 538 | return DoesKeyExist(command_line->GetSwitchValueASCII("name")); |
| 539 | } else if (args[0] == "list") { |
| 540 | return List(command_line->GetSwitchValueASCII("prefix")); |
| 541 | } else if (args[0] == "sign-verify") { |
| 542 | return SignAndVerify(command_line->GetSwitchValueASCII("name")); |
Darren Krahn | 251cb28 | 2015-09-28 08:51:18 -0700 | [diff] [blame] | 543 | } else if (args[0] == "encrypt") { |
| 544 | return Encrypt(command_line->GetSwitchValueASCII("name"), |
| 545 | command_line->GetSwitchValueASCII("in"), |
| 546 | command_line->GetSwitchValueASCII("out")); |
| 547 | } else if (args[0] == "decrypt") { |
| 548 | return Decrypt(command_line->GetSwitchValueASCII("name"), |
| 549 | command_line->GetSwitchValueASCII("in"), |
| 550 | command_line->GetSwitchValueASCII("out")); |
Darren Krahn | 69a3dbc | 2015-09-22 16:21:04 -0700 | [diff] [blame] | 551 | } else { |
| 552 | PrintUsageAndExit(); |
| 553 | } |
| 554 | return 0; |
| 555 | } |