Commit 2e2bd898 authored by Oleg Korshul's avatar Oleg Korshul

.

parent 8f434efb
#-------------------------------------------------
#
# Project created by QtCreator 2017-06-05T12:59:43
#
#-------------------------------------------------
QT += core gui
QT += widgets
TARGET = OpenSSL_gui_test
TEMPLATE = app
DEFINES -= \
UNICODE \
_UNICODE
CORE_ROOT_DIR = $$PWD/../../../..
PWD_ROOT_DIR = $$PWD
include($$CORE_ROOT_DIR/Common/base.pri)
core_linux {
QMAKE_LFLAGS += -Wl,--rpath=./
QMAKE_LFLAGS += -static-libstdc++ -static-libgcc
}
DEFINES += XMLSEC_OPENSSL_110
#DEFINES += "OPENSSL_API_COMPAT=\"0x10100000\""
INCLUDEPATH += $$PWD/../../openssl/include
LIBS += -L$$PWD/../../openssl -lssl
LIBS += -L$$PWD/../../openssl -lcrypto
LIBS += -ldl
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
HEADERS += opensslwrapper.h
SOURCES += opensslwrapper.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QGridLayout>
#include <QFileDialog>
#include <QtNetwork/QSslSocket>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QRect rect = this->geometry();
this->setGeometry(rect.x(), rect.y(), 600, 170);
QGridLayout *mainGridLayout = new QGridLayout();
mainGridLayout->setSpacing( 0 );
mainGridLayout->setMargin( 0 );
setLayout( mainGridLayout );
QWidget *centralWidget = new QWidget( this );
centralWidget->setObjectName( "centralWidget" );
centralWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
this->setCentralWidget(centralWidget);
m_pKeyFile = new QPlainTextEdit(centralWidget);
m_pKeyPassword = new QPlainTextEdit(centralWidget);
m_pCertFile = new QPlainTextEdit(centralWidget);
m_pCertPassword = new QPlainTextEdit(centralWidget);
m_pKeyDialog = new QPushButton(centralWidget);
m_pCertDialog = new QPushButton(centralWidget);
m_pOK = new QPushButton(centralWidget);
m_pKeyDialog->setText("...");
m_pCertDialog->setText("...");
m_pOK->setText("OK");
m_pKeyFile->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pKeyPassword->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pCertFile->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pCertPassword->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QObject::connect( m_pKeyDialog, SIGNAL( clicked() ), this, SLOT( pushButtonKeyClicked()) );
QObject::connect( m_pCertDialog, SIGNAL( clicked() ), this, SLOT( pushButtonCertClicked()) );
QObject::connect( m_pOK, SIGNAL( clicked() ), this, SLOT( pushButtonOKClicked()) );
CalculatePlaces();
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent* e)
{
CalculatePlaces();
}
void MainWindow::CalculatePlaces()
{
int nWidth = this->width();
int nHeight = this->height();
int nWidthBetween = 30;
int nWidthButton = 40;
int nWidthPassword = 40;
int nHeightLine = 30;
int nWidthPath = nWidth - 40 - 2 * nWidthBetween - nWidthButton - nWidthPassword;
if (nWidthPath < 20)
nWidthPath = 20;
int nTop1 = 20;
m_pKeyFile->setGeometry(20, nTop1, nWidthPath, nHeightLine);
m_pKeyPassword->setGeometry(20 + nWidthPath + nWidthBetween, nTop1, nWidthPassword, nHeightLine);
m_pKeyDialog->setGeometry(nWidth - 20 - nWidthButton, nTop1, nWidthButton, nHeightLine);
nTop1 = 20 + nHeightLine + 20;
m_pCertFile->setGeometry(20, nTop1, nWidthPath, nHeightLine);
m_pCertPassword->setGeometry(20 + nWidthPath + nWidthBetween, nTop1, nWidthPassword, nHeightLine);
m_pCertDialog->setGeometry(nWidth - 20 - nWidthButton, nTop1, nWidthButton, nHeightLine);
m_pOK->setGeometry((nWidth - 40) >> 1, nTop1 + nHeightLine + 20, 40, nHeightLine);
}
void MainWindow::pushButtonKeyClicked()
{
std::wstring sFile = QFileDialog::getOpenFileName(NULL, QString(), QString()).toStdWString();
if (sFile.empty())
return;
m_pKeyFile->setPlainText(QString::fromStdWString(sFile));
}
void MainWindow::pushButtonCertClicked()
{
std::wstring sFile = QFileDialog::getOpenFileName(NULL, QString(), QString()).toStdWString();
if (sFile.empty())
return;
m_pCertFile->setPlainText(QString::fromStdWString(sFile));
}
void MainWindow::pushButtonOKClicked()
{
COpenssl_library library;
int nKeyValue = library.LoadKey(m_pKeyFile->toPlainText().toStdWString(), m_pKeyPassword->toPlainText().toStdString());
int nCertValue = library.LoadCert(m_pCertFile->toPlainText().toStdWString(), m_pCertPassword->toPlainText().toStdString());
int y = 0;
y++;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QResizeEvent>
#include <QPlainTextEdit>
#include "opensslwrapper.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
virtual void resizeEvent(QResizeEvent*);
void CalculatePlaces();
public:
QPlainTextEdit* m_pKeyFile;
QPlainTextEdit* m_pKeyPassword;
QPushButton* m_pKeyDialog;
QPlainTextEdit* m_pCertFile;
QPlainTextEdit* m_pCertPassword;
QPushButton* m_pCertDialog;
QPushButton* m_pOK;
public slots:
void pushButtonKeyClicked();
void pushButtonCertClicked();
void pushButtonOKClicked();
};
#endif // MAINWINDOW_H
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/pkcs12.h>
#include <openssl/sha.h>
#include <openssl/ssl.h>
#include <openssl/crypto.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/conf.h>
#include "../../../common/File.h"
#include "../../../common/String.h"
#include "opensslwrapper.h"
class COpenssl_library_private
{
private:
static bool m_bInit;
public:
EVP_PKEY* m_key;
public:
COpenssl_library_private()
{
if (!m_bInit)
{
SSL_library_init();
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
m_bInit = true;
}
}
~COpenssl_library_private()
{
ENGINE_cleanup();
CONF_modules_unload(1);
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
}
};
bool COpenssl_library_private::m_bInit = false;
std::string GetOpenSslErrors()
{
BIO* bio = BIO_new(BIO_s_mem());
ERR_print_errors(bio);
char *buf = NULL;
size_t len = BIO_get_mem_data(bio, &buf);
std::string sRet((char*)buf, len);
NSStringExt::ToLower(sRet);
BIO_free (bio);
return sRet;
}
bool IsOpenSslPasswordError(const std::string& str)
{
if (std::string::npos != str.find("mac verify error"))
return true;
if (std::string::npos != str.find("mac verify failure"))
return true;
if (std::string::npos != str.find("password"))
return true;
return false;
}
COpenssl_library::COpenssl_library()
{
m_internal = new COpenssl_library_private();
}
COpenssl_library::~COpenssl_library()
{
RELEASEOBJECT(m_internal);
}
int COpenssl_library::LoadKey(std::wstring file, std::string password)
{
BYTE* pData = NULL;
DWORD dwDataLen;
if (!NSFile::CFileBinary::ReadAllBytes(file, &pData, dwDataLen))
return OPEN_SSL_WARNING_ERR;
EVP_PKEY* pKey = NULL;
char* pPassword = (password.empty()) ? NULL : (char*)password.c_str();
BIO* bio = BIO_new_mem_buf((void*)pData, (int)dwDataLen);
if (PEM_read_bio_PrivateKey(bio, &pKey, NULL, (void*)pPassword))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_OK;
}
std::string sError = GetOpenSslErrors();
if (IsOpenSslPasswordError(sError))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_PASS;
}
BIO_free(bio);
bio = BIO_new_mem_buf((void*)pData, (int)dwDataLen);
if (d2i_PrivateKey_bio(bio, &pKey))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_OK;
}
sError = GetOpenSslErrors();
if (IsOpenSslPasswordError(sError))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_PASS;
}
BIO_free(bio);
bio = BIO_new_mem_buf((void*)pData, (int)dwDataLen);
if (d2i_PKCS8PrivateKey_bio(bio, &pKey, NULL, (void*)pPassword))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_OK;
}
sError = GetOpenSslErrors();
if (IsOpenSslPasswordError(sError))
{
EVP_PKEY_free(pKey);
BIO_free(bio);
return OPEN_SSL_WARNING_PASS;
}
BIO_free(bio);
bio = BIO_new_mem_buf((void*)pData, (int)dwDataLen);
PKCS12* p12 = d2i_PKCS12_bio(bio, NULL);
if (p12)
{
X509* pCert = NULL;
STACK_OF(X509)* pCa = NULL;
if (PKCS12_parse(p12, pPassword, &pKey, &pCert, &pCa))
{
sk_X509_pop_free(pCa, X509_free);
X509_free(pCert);
EVP_PKEY_free(pKey);
PKCS12_free(p12);
BIO_free(bio);
return OPEN_SSL_WARNING_ALL_OK;
}
sError = GetOpenSslErrors();
if (IsOpenSslPasswordError(sError))
{
PKCS12_free(p12);
BIO_free(bio);
return OPEN_SSL_WARNING_PASS;
}
PKCS12_free(p12);
}
BIO_free(bio);
return OPEN_SSL_WARNING_ERR;
}
int COpenssl_library::LoadCert(std::wstring file, std::string password)
{
return OPEN_SSL_WARNING_ERR;
}
#ifndef OPENSSL_WRAPPER
#define OPENSSL_WRAPPER
#define OPEN_SSL_WARNING_OK 0
#define OPEN_SSL_WARNING_ERR 1
#define OPEN_SSL_WARNING_ALL_OK 2
#define OPEN_SSL_WARNING_PASS 4
class COpenssl_library_private;
class COpenssl_library
{
private:
COpenssl_library_private* m_internal;
public:
COpenssl_library();
~COpenssl_library();
int LoadKey(std::wstring file, std::string password);
int LoadCert(std::wstring file, std::string password);
};
#endif // OPENSSL_WRAPPER
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment