-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.qml
184 lines (140 loc) · 4.33 KB
/
main.qml
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12
import QtQuick.Dialogs 1.2
import PKCS12Util 1.0
Window {
id: app
visible: true
width: 640
height: 480
title: qsTr("Qt ImportPKCS12 Sample")
property var pkcs12
property bool pkcs12IsValid: pkcs12 !== null && pkcs12 !== undefined
property string privateKey: ""
property string certificate: ""
property var caCertificates: null
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
Text {
text: qsTr( "PKCS#12 file (*.pfx, *.p12)" )
font.pointSize: 12
font.bold: true
}
RowLayout {
Layout.fillWidth: true
TextField {
id: fileTextField
Layout.fillWidth: true
font.pointSize: 12
selectByMouse: true
onTextChanged: downloadPKCS12( text )
}
Button {
Layout.preferredWidth: height
text: "..."
font.pointSize: 12
onClicked: fileDialog.open()
}
}
Text {
text: pkcs12IsValid ? qsTr( "PKCS#12 selected" ) : qsTr( "PKCS#12 not selected" )
font.pointSize: 12
color: pkcs12IsValid ? "green" : "red"
}
Item {
Layout.preferredHeight: 20
}
Text {
text: qsTr( "passphrase")
font.pointSize: 12
font.bold: true
}
TextField {
id: passPhraseTextField
Layout.fillWidth: true
font.pointSize: 12
selectByMouse: true
echoMode: TextInput.Password
onTextChanged: testPKCS12()
}
Item {
Layout.preferredHeight: 20
}
Text {
text: qsTr( "Results" )
font.pointSize: 12
font.bold: true
}
Flickable {
id: flickable
Layout.fillWidth: true
Layout.fillHeight: true
contentWidth: columnLayout.width
contentHeight: columnLayout.height
clip: true
ScrollBar.vertical: ScrollBar { }
ColumnLayout {
id: columnLayout
width: flickable.width
Text {
property bool privateKeyValid: privateKey !== ""
text: privateKeyValid ? qsTr( "Private Key is valid" ) : qsTr( "Private Key is not valid" )
font.pointSize: 12
color: privateKeyValid ? "green" : "red"
}
Text {
property bool certificateValid: certificate !== ""
text: certificateValid ? qsTr( "Certificate is valid" ) : qsTr( "Certificate is not valid" )
font.pointSize: 12
color: certificateValid ? "green" : "red"
}
Text {
text: qsTr( "CA certificates: %1" ).arg( caCertificates ? caCertificates.length : 0 )
font.pointSize: 12
color: "green"
}
}
}
}
FileDialog {
id: fileDialog
onAccepted: fileTextField.text = fileUrl
}
PKCS12Util {
id: pkcs12Util
onLoaded: {
pkcs12 = data;
console.log( "PKCS#12 file loaded." );
Qt.callLater( testPKCS12 );
}
}
Component.onCompleted: Qt.callLater( testPKCS12 )
function downloadPKCS12( url )
{
console.log( "Downloading PKCS#12 file..." );
pkcs12Util.loadFromUrl( url )
}
function testPKCS12()
{
if ( !pkcs12IsValid )
{
return;
}
console.log( "Importing PKCS#12 file..." );
privateKey = "";
certificate = "";
caCertificates = [ ];
const obj = pkcs12Util.importPKCS12( pkcs12, passPhraseTextField.text );
console.log( !obj ? "PKCS#12 failed to be imported." : "PKCS#12 successfully imported!" );
if ( !obj )
{
return;
}
privateKey = obj[ "privateKey" ] || "";
certificate = obj[ "certificate" ] || "";
caCertificates = obj[ "caCertificates" ] || [ ];
}
}