1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <openssl/dsa.h>
#include <openssl/sha.h>
#include <openssl/objects.h>
#include <openssl/engine.h>
// g++ dlcsign.cpp -lssl -lcrypto -o dlcsign
void readFile(
std::vector<unsigned char> & data,
const char * filename
) {
data.clear();
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) return;
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
data.resize(fileSize);
file.read((char*)&data[0], fileSize);
}
std::string fromHex(const std::string & hexed) {
char h, l;
std::string ret;
int ie = hexed.length();
ie -= (ie % 2 == 0) ? 1 : 2;
for (int i = 0; i <= ie;) {
h = hexed[i++];
if (h >= 'A' && h <= 'F') { h = h - 'A' + 0x0a; } else
if (h >= 'a' && h <= 'f') { h = h - 'a' + 0x0a; } else
if (h >= '0' && h <= '9') { h -= '0'; } else {
h = 0;
}
l = hexed[i++];
if (l >= 'A' && l <= 'F') { l = l - 'A' + 0x0a; } else
if (l >= 'a' && l <= 'f') { l = l - 'a' + 0x0a; } else
if (l >= '0' && l <= '9') { l -= '0'; } else {
l = 0;
}
ret.push_back((h << 4)|l);
}
return ret;
}
std::string toHex(const unsigned char * s, unsigned len, bool lower_case = false) {
char h, l, c = lower_case ? 'a' : 'A';
std::string ret;
for (unsigned i = 0; i < len; ++i) {
h = (0xf0 & s[i]) >> 4;
if (h <= 9) { h += '0'; } else { h = h - 0x0a + c; }
ret.push_back(h);
l = 0x0f & s[i];
if (l <= 9) { l += '0'; } else { l = l - 0x0a + c; }
ret.push_back(l);
}
return ret;
}
bool dlc_verify(
const std::string & IndexFileCRC,
const std::string & IndexFileSig
) {
static DSA * dsa = NULL;
if (!dsa) {
std::vector<unsigned char> pub_key;
readFile(pub_key, "pub.key");
const unsigned char *p = pub_key.data();
d2i_DSA_PUBKEY(&dsa, &p, pub_key.size());
}
if (!dsa) return false;
unsigned char IndexFileCRC_sha1hash[20];
SHA1((unsigned char*)IndexFileCRC.c_str(), IndexFileCRC.size(), IndexFileCRC_sha1hash);
std::string IndexFileSig_bin = fromHex(IndexFileSig);
int rc = DSA_verify(0, IndexFileCRC_sha1hash, sizeof(IndexFileCRC_sha1hash),
(unsigned char*) IndexFileSig_bin.c_str(), IndexFileSig_bin.size(),
dsa);
return rc == 1;
}
bool dlc_sign(
const std::string & IndexFileCRC,
std::string & IndexFileSig
) {
static DSA * dsa = NULL;
if (!dsa) {
std::vector<unsigned char> priv_key;
readFile(priv_key, "priv.key");
const unsigned char *p = priv_key.data();
d2i_DSAPrivateKey(&dsa, &p, priv_key.size());
}
if (!dsa) return false;
unsigned char hash[20];
if (!SHA1((unsigned char*)IndexFileCRC.c_str(), IndexFileCRC.size(), hash)) {
std::cerr << "ERR: SHA1 failed." << std::endl;
} else {
unsigned int sig_len = 0;
unsigned char sig[256];
if (!DSA_sign(NID_sha1, hash, sizeof(hash), sig, &sig_len, dsa)) {
std::cerr << "ERR: DSA_sign failed." << std::endl;
} else {
IndexFileSig = toHex(sig, sig_len);
}
}
}
int main(int argc, char ** argv) {
bool show_usage = true;
bool do_verify = false;
const char * pCRC = NULL;
if (argc > 1) {
pCRC = argv[1];
show_usage = false;
}
const char * pSIGN = NULL;
if (argc > 2) {
pSIGN = argv[2];
do_verify = true;
}
if (show_usage) {
std::cout << "USAGE:\n " << argv[0] << " crc signature\nEXAMPLE:\n"
<< " dlcsign 1148974934 302c021425c3541a544de7d056b0b677810bb54d45b"
"831ab021443163ffd73c4c34f0f2d5c7bc1778711d3a3abd4\n";
return EXIT_SUCCESS;
}
if (do_verify) {
std::cout << (dlc_verify(pCRC, pSIGN) ? "" : "NOT " ) << "verified" << std::endl;
} else {
std::string sign;
if (dlc_sign(pCRC, sign)) {
std::cout << sign << std::endl;
}
}
}
|