// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "IdentityCertificate.h" #include #include #include #include #include #include #include #include // EC_KEY and friends are deprecated in OpenSSL 3, but the provider-based // fromdata path cannot build a keypair from a chosen scalar in one step (it // imports the private half and forgets the public). This TU routes around the // deprecation warnings on purpose; the legacy API is the only in-process path // that yields a fully formed EC keypair from a given scalar. #if defined(__GNUC__) #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif namespace koutnet { namespace { constexpr int kSeedLen = 32; // P = d*G with d = seed. Returns nullptr when the scalar cannot be a P-256 // private value (practically: the all-zero seed). EVP_PKEY *keypairFromSeed(const QByteArray &seed) { if (seed.size() != kSeedLen) return nullptr; BIGNUM *priv = BN_bin2bn(reinterpret_cast(seed.constData()), seed.size(), nullptr); if (!priv) return nullptr; EC_KEY *ekey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); EC_POINT *pub = nullptr; EVP_PKEY *pkey = nullptr; if (ekey) { pub = EC_POINT_new(EC_KEY_get0_group(ekey)); if (pub && EC_KEY_set_private_key(ekey, priv) == 1 && EC_POINT_mul(EC_KEY_get0_group(ekey), pub, priv, nullptr, nullptr, nullptr) == 1 && EC_KEY_set_public_key(ekey, pub) == 1) { pkey = EVP_PKEY_new(); if (pkey) EVP_PKEY_assign_EC_KEY(pkey, ekey); // takes ownership of ekey else EC_KEY_free(ekey); } } if (pub) EC_POINT_free(pub); if (ekey && !pkey) EC_KEY_free(ekey); BN_free(priv); return pkey; } // One self-signed certificate per station. Serial and validity window come // from the identity and the wall clock, not from RNG, so nothing about the // certificate is random except the ECDSA signature over it - and what TOFU // pins is the public key, never the signature. QSslCertificate selfSignedCertificate(EVP_PKEY *pkey, const QString &identityId) { X509 *x = X509_new(); if (!x) return QSslCertificate(); X509_set_version(x, 2); // serial = the top 63 bits of a hash of the identity id. Deterministic, so // restarts of the same identity keep the serial; certificates of different // identities never collide on it by construction. const QByteArray idDigest = QCryptographicHash::hash(identityId.toUtf8(), QCryptographicHash::Sha256); quint64 serial = 0; for (int i = 0; i < 8; ++i) serial = (serial << 8) | quint8(idDigest.at(i)); serial &= Q_UINT64_C(0x7FFFFFFFFFFFFFFF); // serials are positive integers ASN1_INTEGER_set(X509_get_serialNumber(x), long(serial)); X509_gmtime_adj(X509_get_notBefore(x), -3600); X509_gmtime_adj(X509_get_notAfter(x), 400L * 24 * 3600); X509_set_pubkey(x, pkey); X509_NAME *nm = X509_get_subject_name(x); const QByteArray cn = identityId.toUtf8(); X509_NAME_add_entry_by_txt(nm, "CN", MBSTRING_UTF8, reinterpret_cast(cn.constData()), cn.size(), -1, 0); X509_set_issuer_name(x, nm); if (X509_sign(x, pkey, EVP_sha256()) <= 0) { X509_free(x); return QSslCertificate(); } const int len = i2d_X509(x, nullptr); QByteArray der(len, '\0'); unsigned char *p = reinterpret_cast(der.data()); i2d_X509(x, &p); X509_free(x); return QSslCertificate(der, QSsl::Der); } } // namespace IdentityCertificate buildIdentityCertificate(const QByteArray &seed32, const QString &identityId) { IdentityCertificate out; out.identityId = identityId; EVP_PKEY *pkey = keypairFromSeed(seed32); if (!pkey) return out; out.cert = selfSignedCertificate(pkey, identityId); EVP_PKEY_free(pkey); if (out.cert.isNull()) return out; // the private half travels in PKCS8 DER, read back through a QIODevice the // way QSslKey wants it; keyDer stays alive in the struct beside the key. EVP_PKEY *keypkey = keypairFromSeed(seed32); if (!keypkey) return out; BIO *bio = BIO_new(BIO_s_mem()); i2d_PKCS8PrivateKey_bio(bio, keypkey, nullptr, nullptr, 0, nullptr, nullptr); BUF_MEM *bm = nullptr; BIO_get_mem_ptr(bio, &bm); out.keyDer = QByteArray(bm->data, int(bm->length)); BIO_free(bio); EVP_PKEY_free(keypkey); if (out.keyDer.isEmpty()) return out; QBuffer buf(&out.keyDer); buf.open(QIODevice::ReadOnly); out.key = QSslKey(&buf, QSsl::Ec, QSsl::Der, QSsl::PrivateKey); if (out.key.isNull()) out.keyDer.clear(); return out; } QByteArray identityFingerprint(const QSslCertificate &cert) { // SPKI DER straight off the certificate: the public key stays identical // across every restart and every random signature, which is the property // a pin has to be built on. QSslKey has no public-key DER accessor in Qt // 6, so the certificate is decoded with OpenSSL once more. const QByteArray der = cert.toDer(); if (der.isEmpty()) { return {}; } const unsigned char *p = reinterpret_cast(der.constData()); X509 *x = d2i_X509(nullptr, &p, long(der.size())); if (!x) { return {}; } X509_PUBKEY *pk = X509_get_X509_PUBKEY(x); unsigned char *spki = nullptr; const int len = i2d_X509_PUBKEY(pk, &spki); if (len <= 0 || !spki) { X509_free(x); return {}; } const QByteArray spkiDer = QByteArray(reinterpret_cast(spki), len); OPENSSL_free(spki); X509_free(x); return QCryptographicHash::hash(spkiDer, QCryptographicHash::Sha256); } } // namespace koutnet