Files

421 lines
13 KiB
C++
Raw Permalink Normal View History

// doglic — 自签授权验签(头文件版,零外部依赖)
//
// 与 tools/dogkeygen(DogProtect 仓库)签出的序列号完全兼容:
// serial = base64( (00 02 || 非零填充 || 00 || payload) ^ d mod n )
// payload = chunk..., 末尾 0xFF + sha1(前面全部字节) 的前 4 字节倒序
//
// 设计目标:任何 C++ 程序把本头文件加进来就能离线验序列号,不需要 OpenSSL / SDK。
// 公钥(modulus / public exponent)由调用方提供,见 doglicInit()。
//
// 典型用法:
// doglic::License lic;
// if (doglic::verify(serialFromUser, g_modulusHex, "010001", &lic))
// printf("welcome %s, exp %08X\n", lic.user.c_str(), lic.expDate);
#pragma once
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
namespace doglic {
// ---------------------------------------------------------------- chunk 定义(与 runtime 对齐)
enum Chunk {
CHUNK_VERSION = 0x01,
CHUNK_USER_NAME = 0x02,
CHUNK_EMAIL = 0x03,
CHUNK_HWID = 0x04,
CHUNK_EXP_DATE = 0x05,
CHUNK_RUNNING_TIME = 0x06,
CHUNK_PRODUCT_CODE = 0x07,
CHUNK_USER_DATA = 0x08,
CHUNK_MAX_BUILD = 0x09,
CHUNK_END = 0xFF
};
enum VerifyResult {
OK = 0,
ERR_BASE64 = 1,
ERR_SIZE = 2,
ERR_STRUCTURE = 3, // 解出来的块不是 00 02 ... 00 结构
ERR_VERSION = 4,
ERR_UNKNOWN_CHUNK = 5,
ERR_NO_END = 6,
ERR_CHECKSUM = 7,
ERR_EXPIRED = 8,
ERR_RUNNING_TIME = 9,
ERR_MAX_BUILD = 10
};
struct License {
std::string userName;
std::string email;
uint32_t expDate; // (年<<16)|(月<<8)|日,0 = 不限
uint32_t maxBuildDate; // 0 = 不限
uint8_t runningTimeLimit;
std::vector<uint8_t> hwid;
std::vector<uint8_t> userData;
License() : expDate(0), maxBuildDate(0), runningTimeLimit(0) {}
};
// ---------------------------------------------------------------- base64 / sha1 / bignum
inline int b64val(int c)
{
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
inline std::vector<uint8_t> b64decode(const std::string &src)
{
std::vector<uint8_t> out;
uint32_t buf = 0;
int bits = 0;
for (size_t i = 0; i < src.size(); i++) {
int v = b64val((unsigned char)src[i]);
if (v < 0) continue; // 忽略 '=' 与杂字符
buf = (buf << 6) | (uint32_t)v;
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push_back((uint8_t)(buf >> bits));
}
}
return out;
}
struct SHA1 {
uint32_t h[5];
uint8_t buf[64];
size_t len;
uint64_t total;
SHA1() : len(0), total(0)
{
h[0] = 0x67452301; h[1] = 0xEFCDAB89; h[2] = 0x98BADCFE;
h[3] = 0x10325476; h[4] = 0xC3D2E1F0;
}
static uint32_t rol(uint32_t v, int n) { return (v << n) | (v >> (32 - n)); }
void block(const uint8_t *p)
{
uint32_t w[80];
for (int i = 0; i < 16; i++)
w[i] = ((uint32_t)p[i * 4] << 24) | ((uint32_t)p[i * 4 + 1] << 16) |
((uint32_t)p[i * 4 + 2] << 8) | p[i * 4 + 3];
for (int i = 16; i < 80; i++)
w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
else { f = b ^ c ^ d; k = 0xCA62C1D6; }
uint32_t tmp = rol(a, 5) + f + e + k + w[i];
e = d; d = c; c = rol(b, 30); b = a; a = tmp;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
}
void update(const uint8_t *p, size_t n)
{
total += n;
while (n) {
size_t take = 64 - len;
if (take > n) take = n;
memcpy(buf + len, p, take);
len += take; p += take; n -= take;
if (len == 64) { block(buf); len = 0; }
}
}
void finish(uint8_t out[20])
{
uint64_t bits = total * 8;
uint8_t pad = 0x80;
update(&pad, 1);
uint8_t zero = 0;
while (len != 56) update(&zero, 1);
uint8_t l[8];
for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - i * 8));
update(l, 8);
for (int i = 0; i < 5; i++) {
out[i * 4 + 0] = (uint8_t)(h[i] >> 24);
out[i * 4 + 1] = (uint8_t)(h[i] >> 16);
out[i * 4 + 2] = (uint8_t)(h[i] >> 8);
out[i * 4 + 3] = (uint8_t)h[i];
}
}
};
// 大数:小端 32 位肢体,只做 modpow 需要的运算
struct Big {
std::vector<uint32_t> d;
Big() {}
explicit Big(uint32_t v) { if (v) d.push_back(v); }
bool zero() const { return d.empty(); }
void trim() { while (!d.empty() && d.back() == 0) d.pop_back(); }
size_t bits() const
{
if (d.empty()) return 0;
uint32_t top = d.back();
size_t n = (d.size() - 1) * 32;
while (top) { n++; top >>= 1; }
return n;
}
static Big fromBytesBE(const uint8_t *p, size_t n)
{
Big r;
r.d.assign((n + 3) / 4, 0);
for (size_t i = 0; i < n; i++) {
size_t pos = n - 1 - i;
r.d[pos / 4] |= (uint32_t)p[i] << ((pos % 4) * 8);
}
r.trim();
return r;
}
static Big fromHex(const std::string &hex)
{
std::vector<uint8_t> bytes;
for (size_t i = 0; i + 1 < hex.size(); i += 2) {
char t[3] = { hex[i], hex[i + 1], 0 };
bytes.push_back((uint8_t)strtol(t, 0, 16));
}
return fromBytesBE(bytes.empty() ? 0 : &bytes[0], bytes.size());
}
void toBytesBE(uint8_t *out, size_t n) const
{
memset(out, 0, n);
for (size_t i = 0; i < n; i++) {
size_t pos = n - 1 - i;
size_t limb = pos / 4;
if (limb < d.size())
out[i] = (uint8_t)(d[limb] >> ((pos % 4) * 8));
}
}
int cmp(const Big &o) const
{
if (d.size() != o.d.size()) return d.size() < o.d.size() ? -1 : 1;
for (size_t i = d.size(); i-- > 0;)
if (d[i] != o.d[i]) return d[i] < o.d[i] ? -1 : 1;
return 0;
}
Big sub(const Big &o) const
{
Big r;
r.d.assign(d.size(), 0);
int64_t borrow = 0;
for (size_t i = 0; i < d.size(); i++) {
int64_t cur = (int64_t)d[i] - borrow - (i < o.d.size() ? o.d[i] : 0);
if (cur < 0) { cur += ((int64_t)1 << 32); borrow = 1; } else borrow = 0;
r.d[i] = (uint32_t)cur;
}
r.trim();
return r;
}
Big shl(size_t n) const
{
if (zero()) return Big();
Big r;
size_t words = n / 32, bits = n % 32;
r.d.assign(d.size() + words + 1, 0);
for (size_t i = 0; i < d.size(); i++) {
uint64_t v = (uint64_t)d[i] << bits;
r.d[i + words] |= (uint32_t)v;
r.d[i + words + 1] |= (uint32_t)(v >> 32);
}
r.trim();
return r;
}
static Big mul(const Big &a, const Big &b)
{
Big r;
if (a.zero() || b.zero()) return r;
r.d.assign(a.d.size() + b.d.size(), 0);
for (size_t i = 0; i < a.d.size(); i++) {
uint64_t carry = 0;
for (size_t j = 0; j < b.d.size(); j++) {
uint64_t cur = (uint64_t)a.d[i] * b.d[j] + r.d[i + j] + carry;
r.d[i + j] = (uint32_t)cur;
carry = cur >> 32;
}
size_t k = i + b.d.size();
while (carry) {
uint64_t cur = (uint64_t)r.d[k] + carry;
r.d[k] = (uint32_t)cur;
carry = cur >> 32;
k++;
}
}
r.trim();
return r;
}
Big mod(const Big &m) const
{
Big r = *this;
if (m.zero()) return r;
while (r.cmp(m) >= 0) {
size_t shift = r.bits() - m.bits();
Big t = m.shl(shift);
while (t.cmp(r) > 0 && shift > 0) { shift--; t = m.shl(shift); }
r = r.sub(t);
}
return r;
}
};
inline Big modpow(const Big &base, const Big &exp, const Big &mod)
{
Big r(1);
Big b = base.mod(mod);
size_t bits = exp.bits();
for (size_t i = 0; i < bits; i++) {
if ((exp.d[i / 32] >> (i % 32)) & 1)
r = Big::mul(r, b).mod(mod);
b = Big::mul(b, b).mod(mod);
}
return r;
}
inline uint32_t rd32(const uint8_t *p)
{
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3];
}
inline uint32_t today()
{
time_t t = time(0);
struct tm tmv;
#ifdef _WIN32
localtime_s(&tmv, &t);
#else
localtime_r(&t, &tmv);
#endif
return ((uint32_t)(tmv.tm_year + 1900) << 16) | ((uint32_t)(tmv.tm_mon + 1) << 8) |
(uint32_t)tmv.tm_mday;
}
// 主入口:modulusHex/publicExpHex 用十六进制大端字符串(工程文件里 Modulus/PublicExp 的 base64 先解成字节再转 hex)
// 返回 OK 或具体错误码;lic 可传 NULL
inline int verify(const std::string &serial,
const std::string &modulusHex,
const std::string &publicExpHex,
License *lic = 0,
uint32_t now = 0)
{
std::vector<uint8_t> sig = b64decode(serial);
if (sig.size() < 16) return ERR_BASE64;
Big n = Big::fromHex(modulusHex);
Big e = Big::fromHex(publicExpHex);
if (n.zero() || e.zero()) return ERR_SIZE;
const size_t modLen = (n.bits() + 7) / 8; // 模长按字节向上取整(1024 位 = 128 字节)
std::vector<uint8_t> block(modLen, 0);
Big sigInt = Big::fromBytesBE(&sig[0], sig.size());
Big m = modpow(sigInt, e, n);
m.toBytesBE(&block[0], block.size());
if (block[0] != 0x00 || block[1] != 0x02) return ERR_STRUCTURE;
size_t pos = 2;
while (pos < block.size() && block[pos] != 0x00) pos++;
if (pos + 1 >= block.size()) return ERR_STRUCTURE;
pos++;
const uint8_t *p = &block[0] + pos;
size_t plen = block.size() - pos;
License tmp;
License &L = lic ? *lic : tmp;
L = License();
size_t i = 0;
uint32_t cur = now ? now : today();
while (i < plen) {
uint8_t tag = p[i];
if (tag == CHUNK_VERSION) {
if (i + 1 >= plen) return ERR_VERSION;
if (p[i + 1] < 1 || p[i + 1] > 2) return ERR_VERSION;
i += 2;
} else if (tag == CHUNK_USER_NAME || tag == CHUNK_EMAIL) {
if (i + 1 >= plen) return ERR_STRUCTURE;
uint8_t n2 = p[i + 1];
if (i + 2 + n2 > plen) return ERR_STRUCTURE;
std::string v((const char *)p + i + 2, n2);
if (tag == CHUNK_USER_NAME) L.userName = v; else L.email = v;
i += 2 + n2;
} else if (tag == CHUNK_EXP_DATE || tag == CHUNK_MAX_BUILD) {
if (i + 5 > plen) return ERR_STRUCTURE;
uint32_t date = rd32(p + i + 1);
if (tag == CHUNK_EXP_DATE) L.expDate = date; else L.maxBuildDate = date;
i += 5;
} else if (tag == CHUNK_RUNNING_TIME) {
if (i + 2 > plen) return ERR_STRUCTURE;
L.runningTimeLimit = p[i + 1];
i += 2;
} else if (tag == CHUNK_HWID || tag == CHUNK_USER_DATA) {
if (i + 1 >= plen) return ERR_STRUCTURE;
uint8_t n2 = p[i + 1];
if (i + 2 + n2 > plen) return ERR_STRUCTURE;
std::vector<uint8_t> v(p + i + 2, p + i + 2 + n2);
if (tag == CHUNK_HWID) L.hwid = v; else L.userData = v;
i += 2 + n2;
} else if (tag == CHUNK_PRODUCT_CODE) {
if (i + 9 > plen) return ERR_STRUCTURE;
i += 9;
} else if (tag == CHUNK_END) {
if (i + 5 > plen) return ERR_STRUCTURE;
uint8_t hash[20];
SHA1 sha;
sha.update(p, i);
sha.finish(hash);
if (p[i + 1] != hash[3] || p[i + 2] != hash[2] ||
p[i + 3] != hash[1] || p[i + 4] != hash[0])
return ERR_CHECKSUM;
if (L.expDate && cur > L.expDate) return ERR_EXPIRED;
if (L.maxBuildDate && cur > L.maxBuildDate) return ERR_MAX_BUILD;
return OK;
} else {
return ERR_UNKNOWN_CHUNK;
}
}
return ERR_NO_END;
}
// 便捷重载:直接吃 base64 的公钥编码(工程文件里的写法)
inline int verifyB64Key(const std::string &serial,
const std::string &modulusB64,
const std::string &publicExpB64,
License *lic = 0)
{
std::vector<uint8_t> mb = b64decode(modulusB64);
std::vector<uint8_t> eb = b64decode(publicExpB64);
std::string mh, eh;
static const char *hx = "0123456789abcdef";
for (size_t i = 0; i < mb.size(); i++) { mh += hx[mb[i] >> 4]; mh += hx[mb[i] & 15]; }
for (size_t i = 0; i < eb.size(); i++) { eh += hx[eb[i] >> 4]; eh += hx[eb[i] & 15]; }
return verify(serial, mh, eh, lic);
}
} // namespace doglic