diff --git a/libwvdrmengine/oemcrypto/test/install_keybox_tool.cpp b/libwvdrmengine/oemcrypto/test/install_keybox_tool.cpp new file mode 100644 index 00000000..265cb11b --- /dev/null +++ b/libwvdrmengine/oemcrypto/test/install_keybox_tool.cpp @@ -0,0 +1,104 @@ +#include "stdio.h" + +#include +#include +#include +#include + +#include + +#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(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(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(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(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 \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; +}