// Generate the packet BUF *WpcGeneratePacket(PACK *pack, X *cert, K *key) { UCHAR hash[SHA1_SIZE]; BUF *pack_data; BUF *cert_data = NULL; BUF *sign_data = NULL; BUF *b; // Validate arguments if (pack == NULL) { return NULL; } pack_data = PackToBuf(pack); HashSha1(hash, pack_data->Buf, pack_data->Size); if (cert != NULL && key != NULL) { UCHAR sign[128]; cert_data = XToBuf(cert, false); RsaSign(sign, hash, sizeof(hash), key); sign_data = NewBuf(); WriteBuf(sign_data, sign, sizeof(sign)); SeekBuf(sign_data, 0, 0); } b = NewBuf(); WpcAddDataEntryBin(b, "PACK", pack_data->Buf, pack_data->Size); WpcAddDataEntryBin(b, "HASH", hash, sizeof(hash)); if (cert_data != NULL) { WpcAddDataEntryBin(b, "CERT", cert_data->Buf, cert_data->Size); WpcAddDataEntryBin(b, "SIGN", sign_data->Buf, sign_data->Size); } FreeBuf(pack_data); FreeBuf(cert_data); FreeBuf(sign_data); SeekBuf(b, 0, 0); return b; }
// Test main procedure void TestSecMain(SECURE *sec) { char *test_str = CEDAR_PRODUCT_STR " VPN"; K *public_key, *private_key; // Validate arguments if (sec == NULL) { return; } Print("test_str: \"%s\"\n", test_str); Print("Writing Data...\n"); if (WriteSecData(sec, true, "test_str", test_str, StrLen(test_str)) == false) { Print("WriteSecData() Failed.\n"); } else { char data[MAX_SIZE]; Zero(data, sizeof(data)); Print("Reading Data...\n"); if (ReadSecData(sec, "test_str", data, sizeof(data)) == false) { Print("ReadSecData() Failed.\n"); } else { Print("test_str: \"%s\"\n", data); } Print("Deleting Data...\n"); DeleteSecData(sec, "test_str"); } Print("Generating Key...\n"); if (RsaGen(&private_key, &public_key, 1024) == false) { Print("RsaGen() Failed.\n"); } else { X *cert; NAME *name; X_SERIAL *serial; UINT num = 0x11220000; Print("Creating Cert...\n"); serial = NewXSerial(&num, sizeof(UINT)); name = NewName(L"Test", L"Test", L"Test", L"JP", L"Test", L"Test"); cert = NewRootX(public_key, private_key, name, 365, NULL); FreeXSerial(serial); if (cert == NULL) { Print("NewRootX() Failed.\n"); } else { Print("Writing Cert...\n"); DeleteSecData(sec, "test_cer"); if (WriteSecCert(sec, true, "test_cer", cert) == false) { Print("WriteSecCert() Failed.\n"); } else { X *x; Print("Reading Cert...\n"); x = ReadSecCert(sec, "test_cer"); if (x == NULL) { Print("ReadSecCert() Failed.\n"); } else { Print("Checking two Certs... "); if (CompareX(x, cert) == false) { Print("[FAILED]\n"); } else { Print("Ok.\n"); } FreeX(x); } if (cert != NULL) { X *x; XToFile(cert, "cert_tmp.cer", true); x = FileToX("cert_tmp.cer"); if (CompareX(x, cert) == false) { Print("[FAILED]\n"); } else { Print("Ok.\n"); Print("Writing Private Key...\n"); DeleteSecKey(sec, "test_key"); if (WriteSecKey(sec, false, "test_key", private_key) == false) { Print("WriteSecKey() Failed.\n"); } else { UCHAR sign_cpu[128]; UCHAR sign_sec[128]; K *pub = GetKFromX(cert); Print("Ok.\n"); Print("Signing Data by CPU...\n"); if (RsaSign(sign_cpu, test_str, StrLen(test_str), private_key) == false) { Print("RsaSign() Failed.\n"); } else { Print("Ok.\n"); Print("sign_cpu: "); PrintBin(sign_cpu, sizeof(sign_cpu)); Print("Signing Data by %s..\n", sec->Dev->DeviceName); if (SignSec(sec, "test_key", sign_sec, test_str, StrLen(test_str)) == false) { Print("SignSec() Failed.\n"); } else { Print("Ok.\n"); Print("sign_sec: "); PrintBin(sign_sec, sizeof(sign_sec)); Print("Compare..."); if (Cmp(sign_sec, sign_cpu, sizeof(sign_cpu)) == 0) { Print("Ok.\n"); Print("Verify..."); if (RsaVerify(test_str, StrLen(test_str), sign_sec, pub) == false) { Print("[FAILED]\n"); } else { Print("Ok.\n"); } } else { Print("[DIFFIRENT]\n"); } } } Print("Deleting test_key...\n"); // DeleteSecKey(sec, "test_key"); FreeK(pub); } } FreeX(x); } } Print("Deleting Cert..\n"); // DeleteSecCert(sec, "test_cer"); FreeX(cert); } FreeName(name); FreeK(private_key); FreeK(public_key); } }