/* Copyright (c) 2021, Grégory Soutadé All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "drmprocessorclientimpl.h" /* Digest interface */ void* DRMProcessorClientImpl::createDigest(const std::string& digestName) { EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); const EVP_MD* md = EVP_get_digestbyname(digestName.c_str()); if (EVP_DigestInit(md_ctx, md) != 1) { EVP_MD_CTX_free(md_ctx); return 0; } return md_ctx; } int DRMProcessorClientImpl::digestUpdate(void* handler, unsigned char* data, unsigned int length) { return (EVP_DigestUpdate((EVP_MD_CTX *)handler, data, length)) ? 0 : -1; } int DRMProcessorClientImpl::digestFinalize(void* handler, unsigned char* digestOut) { int res = EVP_DigestFinal((EVP_MD_CTX *)handler, digestOut, NULL); EVP_MD_CTX_free((EVP_MD_CTX *)handler); return (res == 1) ? 0 : -1; } int DRMProcessorClientImpl::digest(const std::string& digestName, unsigned char* data, unsigned int length, unsigned char* digestOut) { void* handler = createDigest(digestName); if (!handler) return -1; if (digestUpdate(handler, data, length)) return -1; return digestFinalize(handler, digestOut); } /* Random interface */ void DRMProcessorClientImpl::randBytes(unsigned char* bytesOut, unsigned int length) { RAND_bytes(bytesOut, length); } /* HTTP interface */ std::string DRMProcessorClientImpl::sendHTTPRequest(const std::string& URL, const std::string& POSTData, const std::string& contentType, std::map* responseHeaders) { QNetworkRequest request(QUrl(URL.c_str())); QNetworkAccessManager networkManager; QByteArray replyData; GOUROU_LOG(gourou::INFO, "Send request to " << URL); if (POSTData.size()) { GOUROU_LOG(gourou::DEBUG, "<<< " << std::endl << POSTData); } request.setRawHeader("Accept", "*/*"); request.setRawHeader("User-Agent", "book2png"); if (contentType.size()) request.setRawHeader("Content-Type", contentType.c_str()); QNetworkReply* reply; if (POSTData.size()) reply = networkManager.post(request, POSTData.c_str()); else reply = networkManager.get(request); QCoreApplication* app = QCoreApplication::instance(); networkManager.moveToThread(app->thread()); while (!reply->isFinished()) app->processEvents(); QByteArray location = reply->rawHeader("Location"); if (location.size() != 0) { GOUROU_LOG(gourou::DEBUG, "New location"); return sendHTTPRequest(location.constData(), POSTData, contentType, responseHeaders); } if (reply->error() != QNetworkReply::NoError) EXCEPTION(gourou::CLIENT_NETWORK_ERROR, "Error " << reply->errorString().toStdString()); QList headers = reply->rawHeaderList(); for (int i = 0; i < headers.size(); ++i) { if (gourou::logLevel >= gourou::DEBUG) std::cout << headers[i].constData() << " : " << reply->rawHeader(headers[i]).constData() << std::endl; if (responseHeaders) (*responseHeaders)[headers[i].constData()] = reply->rawHeader(headers[i]).constData(); } replyData = reply->readAll(); if (reply->rawHeader("Content-Type") == "application/vnd.adobe.adept+xml") { GOUROU_LOG(gourou::DEBUG, ">>> " << std::endl << replyData.data()); } return std::string(replyData.data(), replyData.length()); } void DRMProcessorClientImpl::RSAPrivateEncrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength, const RSA_KEY_TYPE keyType, const std::string& password, const unsigned char* data, unsigned dataLength, unsigned char* res) { PKCS12 * pkcs12; EVP_PKEY* pkey; X509* cert; STACK_OF(X509)* ca; RSA * rsa; pkcs12 = d2i_PKCS12(NULL, &RSAKey, RSAKeyLength); if (!pkcs12) EXCEPTION(gourou::CLIENT_INVALID_PKCS12, ERR_error_string(ERR_get_error(), NULL)); PKCS12_parse(pkcs12, password.c_str(), &pkey, &cert, &ca); rsa = EVP_PKEY_get1_RSA(pkey); int ret = RSA_private_encrypt(dataLength, data, res, rsa, RSA_PKCS1_PADDING); if (ret < 0) EXCEPTION(gourou::CLIENT_RSA_ERROR, ERR_error_string(ERR_get_error(), NULL)); if (gourou::logLevel >= gourou::DEBUG) { printf("Encrypted : "); for(int i=0; i= gourou::DEBUG) { printf("Decrypted : "); for(int i=0; i