-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpkcs12_util.cpp
144 lines (122 loc) · 3.79 KB
/
pkcs12_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "pkcs12_util.h"
#include "import_pkcs12_openssl.h"
#include <QBuffer>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QQmlEngine>
#include <QEventLoop>
#include <QNetworkReply>
#include <QCryptographicHash>
#if defined( Q_OS_MACOS ) || defined( Q_OS_IOS )
#include "import_pkcs12_openssl.h"
#endif
PKCS12Util::PKCS12Util( QObject* parent ) :
QObject( parent )
{
}
QByteArray PKCS12Util::loadFromFile( const QString& path ) const
{
QFile file( path );
if ( !file.open( QIODevice::ReadOnly ) )
{
return QByteArray();
}
return file.readAll();
}
void PKCS12Util::loadFromUrl( const QUrl& url )
{
if ( !url.isValid() )
{
emit loaded( url, QByteArray() );
return;
}
if ( url.isLocalFile() )
{
emit loaded( url, loadFromFile( url.toLocalFile() ) );
return;
}
QNetworkAccessManager* mgr = qmlEngine(this)->networkAccessManager();
QNetworkRequest req( url );
QNetworkReply* reply = mgr->get( req );
QEventLoop loop;
QObject::connect( reply, &QNetworkReply::finished, this, &PKCS12Util::onFinished );
}
QVariant PKCS12Util::importPKCS12( const QVariant& data, const QString& passPhrase )
{
if ( !data.isValid() || data.isNull() )
{
return QVariant();
}
if ( data.type() != QVariant::ByteArray )
{
qDebug() << __FILE__ << __LINE__ << "data.type: " << data.type() << " unexpected.";
return QVariant();
}
QByteArray bytes = data.toByteArray();
QBuffer buffer;
buffer.open( QIODevice::ReadWrite );
buffer.write( bytes );
buffer.reset();
QVariantMap result;
QSslKey privateKey;
QSslCertificate certificate;
QList<QSslCertificate> caCertificates;
#if defined( Q_OS_MACOS ) || defined( Q_OS_IOS )
bool ok = importPkcs12_openssl( &buffer, &privateKey, &certificate, &caCertificates, passPhrase.toUtf8() );
#else
bool ok = QSslCertificate::importPKCS12( importPkcs12_openssls( &buffer, &privateKey, &certificate, &caCertificates, passPhrase.toUtf8() );
#endif
if ( !ok )
{
return QVariant();
}
result[ "privateKey" ] = QString::fromUtf8( privateKey.toPem() );
result[ "certificate" ] = QString::fromUtf8( certificate.toPem() );
QStringList _ca;
foreach ( QSslCertificate caCert, caCertificates )
{
_ca.append( caCert.toPem() );
}
result[ "caCertificates" ] = _ca;
return result;
}
void PKCS12Util::onFinished()
{
QNetworkReply* reply = ::qobject_cast<QNetworkReply*>( QObject::sender() );
if ( !reply )
{
return;
}
QUrl url = reply->url();
QNetworkReply::NetworkError error = reply->error();
if ( error != QNetworkReply::NoError )
{
qDebug() << Q_FUNC_INFO << __LINE__ << url << "error: " << error;
emit loaded( url, QVariant() );
return;
}
int statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if ( statusCode != 200 )
{
qDebug() << Q_FUNC_INFO << __LINE__ << url << "statusCode: " << statusCode;
emit loaded( url, QVariant() );
return;
}
int contentLength = reply->header( QNetworkRequest::ContentLengthHeader).toInt();
if ( contentLength == 0 )
{
qDebug() << Q_FUNC_INFO << __LINE__ << url << "contentLength: " << contentLength;
emit loaded( url, QVariant() );
QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
qDebug() << Q_FUNC_INFO << __LINE__ << "contentType: " << contentType;
return;
}
QByteArray data = reply->readAll();
if ( data.isNull() || data.isEmpty() )
{
qDebug() << Q_FUNC_INFO << __LINE__ << url << " error: data is empty";
emit loaded( url, QVariant() );
return;
}
emit loaded( url, data );
}