Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool GinCertificateManager::verifyAnswer(const QString& ginCertRaw, const QString& ginIssuerCertRaw, const QString& request, const QString& signature) {
- qDebug() << "in verifyAnswer";
- if (!setCerts(ginCertRaw, ginIssuerCertRaw)) {
- return false;
- }
- X509 *ginCertificate = qStringCertToX509(this->ginCert);
- X509 *issuerCertificate = qStringCertToX509(this->ginIssuerCert);
- X509* rootCertificate = getRootCert(ginCertificate);
- if (rootCertificate == nullptr)
- return false;
- qDebug() << "___";
- qDebug() << QString(request);
- // QJsonDocument::fromRawData(request).toJson(QJsonDocument::Compact)
- qDebug() << "___";
- QByteArray sign = signature.split(".")[2].toUtf8();
- QByteArray signDecodedBase = QByteArray::fromBase64(sign, QByteArray::Base64UrlEncoding);
- //sign = QByteArray::fromBase64(sign, QByteArray::Base64UrlEncoding);
- QByteArray data = request.toUtf8().toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
- qDebug() << QString::fromUtf8(data);
- qDebug() << "Sign";
- verifySignature(ginCertificate, data, sign);
- verifySignature(issuerCertificate, data, sign);
- verifySignature(rootCertificate, data, sign);
- verifySignature(ginCertificate, data, signDecodedBase);
- verifySignature(issuerCertificate, data, signDecodedBase);
- verifySignature(rootCertificate, data, signDecodedBase);
- data = QString(request).replace("#", "").toUtf8().toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
- verifySignature(ginCertificate, data, sign);
- verifySignature(issuerCertificate, data, sign);
- verifySignature(rootCertificate, data, sign);
- verifySignature(ginCertificate, data, signDecodedBase);
- verifySignature(issuerCertificate, data, signDecodedBase);
- verifySignature(rootCertificate, data, signDecodedBase);
- return false;
- }
- bool GinCertificateManager::verifySignature(X509* cert, QByteArray data, QByteArray signature) {
- int digestId;
- const EVP_MD *algorithm = nullptr;
- int res;
- EVP_MD_CTX *ctx;
- EVP_PKEY_get_default_digest_nid(X509_get_pubkey(cert), &digestId);
- algorithm = EVP_get_digestbynid(digestId);
- if (algorithm == nullptr)
- qDebug() << "algorithm == nullptr";
- ctx = EVP_MD_CTX_create();
- EVP_MD_CTX_init(ctx);
- if (EVP_VerifyInit_ex(ctx, algorithm, NULL) != 1)
- qDebug() << "EVP_VerifyInit != 1";
- if (EVP_VerifyUpdate(ctx, data.constData(), data.length()) != 1)
- qDebug() << "EVP_VerifyInit != 1";
- res = EVP_VerifyFinal(ctx, reinterpret_cast<const unsigned char*>(signature.constData()),
- signature.length(), X509_get_pubkey(cert));
- qDebug() << res;
- qDebug() << (res == 1);
- return res == 1;
- }
Add Comment
Please, Sign In to add comment