This adds an install keybox tool to the OEMCrypto unit test directory. It is built when we build the OPK w/linux IPC. This CL also adds some scripts to use this tool when running the OPK Linux TA, and then runs the standard tests. Bug: 295371549 Change-Id: I11e59faa3b24d906f573bcd3f4855e73a4aa5fdf
105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
#include "stdio.h"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
#include <string.h>
|
|
|
|
#include "OEMCryptoCENC.h"
|
|
#include "clock.h"
|
|
#include "file_store.h"
|
|
#include "oemcrypto_types.h"
|
|
#include "string_conversions.h"
|
|
|
|
// Return a printable string from data. If all the characters are printable,
|
|
// then just use the string. Otherwise, convert to hex.
|
|
std::string MaybeHex(const uint8_t* data, size_t length) {
|
|
// Ignore any trailing 0s.
|
|
const size_t actual_length =
|
|
strnlen(reinterpret_cast<const char*>(data), length);
|
|
for (size_t i = actual_length; i < length; i++) {
|
|
// But check that there is nothing nonzero after the first 0.
|
|
if (data[i] != 0) return "0x" + wvutil::HexEncode(data, length);
|
|
}
|
|
// if there are any non-printable characters, just use hex.
|
|
for (size_t i = 0; i < actual_length; i++) {
|
|
if (!isprint(data[i])) return "0x" + wvutil::HexEncode(data, length);
|
|
}
|
|
return std::string(reinterpret_cast<const char*>(data), actual_length);
|
|
}
|
|
|
|
bool install_binary_keybox(const uint8_t* keybox, size_t keybox_length) {
|
|
printf("Installing keybox %s\n",
|
|
wvutil::HexEncode(keybox, keybox_length).c_str());
|
|
OEMCryptoResult status = OEMCrypto_Initialize();
|
|
printf("Init status = %d\n", status);
|
|
if (status != OEMCrypto_SUCCESS) {
|
|
printf("OEMCrypto Initialize failed: %d\n", status);
|
|
return false;
|
|
}
|
|
|
|
status = OEMCrypto_InstallKeyboxOrOEMCert(keybox, keybox_length);
|
|
if (status != OEMCrypto_SUCCESS) {
|
|
printf("OEMCrypto Install keybox failed: %d\n", status);
|
|
return false;
|
|
}
|
|
|
|
uint8_t dev_id[128] = {0};
|
|
size_t dev_id_len = 128;
|
|
status = OEMCrypto_GetDeviceID(dev_id, &dev_id_len);
|
|
if (status != OEMCrypto_SUCCESS) {
|
|
printf("OEMCrypto_GetDeviceID failed: %d\n", status);
|
|
return false;
|
|
}
|
|
printf("Device Id = '%s'\n", MaybeHex(dev_id, dev_id_len).c_str());
|
|
|
|
uint8_t key_data[256];
|
|
size_t key_data_len = sizeof(key_data);
|
|
status = OEMCrypto_GetKeyData(key_data, &key_data_len);
|
|
if (status != OEMCrypto_SUCCESS) {
|
|
printf("OEMCrypto_GetKeyData failed: %d\n", status);
|
|
return false;
|
|
}
|
|
uint32_t* data = reinterpret_cast<uint32_t*>(key_data);
|
|
printf("KeyData system_id = %u = 0x%04X, version=%u\n", htonl(data[1]),
|
|
htonl(data[1]), htonl(data[0]));
|
|
|
|
status = OEMCrypto_Terminate();
|
|
printf("Term status = %d\n", status);
|
|
return true;
|
|
}
|
|
|
|
bool install_xml_keybox(const std::string& xml) {
|
|
// This does not yet work.
|
|
return false;
|
|
}
|
|
|
|
bool install_keybox(const std::string& keybox) {
|
|
// Try to install the keybox as binary. if that doesn't work, try xml.
|
|
if (install_binary_keybox(reinterpret_cast<const uint8_t*>(keybox.c_str()),
|
|
keybox.length())) {
|
|
return true;
|
|
}
|
|
if (install_xml_keybox(keybox)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc != 2) {
|
|
printf("Usage: %s <filename>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
std::ifstream file(argv[1]);
|
|
if (!file) {
|
|
printf("Error opening file %s\n", argv[1]);
|
|
return 1;
|
|
}
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf();
|
|
return install_keybox(buffer.str()) ? 0 : 1;
|
|
}
|