Mercurial > trustbridge
annotate ui/certificate.cpp @ 351:ee59ab0eb7ff
Add test for Certificate::fromFile
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Thu, 10 Apr 2014 15:31:02 +0200 |
parents | a49766196a7d |
children | 5f1494fab517 |
rev | line source |
---|---|
21
dc1e1e9e62ce
Add certificate class and use it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
1 #include "certificate.h" |
82
1f27d6db5ee3
Polarssl based certificate handling
Andre Heinecke <aheinecke@intevation.de>
parents:
81
diff
changeset
|
2 #include <QDebug> |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
3 #include <QFile> |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
4 #include <QStringList> |
21
dc1e1e9e62ce
Add certificate class and use it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 #include <QObject> |
dc1e1e9e62ce
Add certificate class and use it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 |
338
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
7 #include "certhelp.h" |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
8 #include "listutil.h" |
94
f1ebab8639dc
Do not save the x509 cert as a member variable
Andre Heinecke <aheinecke@intevation.de>
parents:
83
diff
changeset
|
9 |
338
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
10 /* Qt wrapper around certhelp functions. */ |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
11 QString getX509Value(x509_name *namebuf, unsigned char *oid) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
12 QString retval; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
13 char * buf = get_oid_valstr(namebuf, oid); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
14 if (buf == NULL) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
15 return retval; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
16 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
17 retval = QString::fromUtf8(buf, -1); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
18 free(buf); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
19 return retval; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
20 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
21 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
22 void Certificate::parseDetails(const QByteArray& cert) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
23 x509_crt chain; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
24 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
25 x509_crt_init(&chain); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
26 if (x509_crt_parse_der(&chain, (const unsigned char *)cert.data(), |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
27 cert.size()) != 0) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
28 qDebug() << "Failed to parse cert.."; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
29 return; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
30 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
31 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
32 mValidFrom = QDateTime(QDate(chain.valid_from.year, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
33 chain.valid_from.mon, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
34 chain.valid_from.day), |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
35 QTime(chain.valid_from.hour, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
36 chain.valid_from.min, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
37 chain.valid_from.sec)); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
38 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
39 mValidTo = QDateTime(QDate(chain.valid_to.year, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
40 chain.valid_to.mon, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
41 chain.valid_to.day), |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
42 QTime(chain.valid_to.hour, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
43 chain.valid_to.min, |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
44 chain.valid_to.sec)); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
45 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
46 mSubjectCN = getX509Value(&(chain.subject), CERT_OID_CN); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
47 mSubjectOU = getX509Value(&(chain.subject), CERT_OID_OU); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
48 mSubjectO = getX509Value(&(chain.subject), CERT_OID_O); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
49 mSubjectSN = getX509Value(&(chain.subject), CERT_OID_SN); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
50 x509_crt_free(&chain); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
51 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
52 mDetails = QObject::tr("Certificate:\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
53 " <bold>%1</bold>\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
54 " %2, %3\n\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
55 "Serial number:\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
56 "%4\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
57 "Valid from: <bold>%5</bold> to <bold>%6</bold>\n\n" |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
58 "Issued by: ..") |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
59 .arg(mSubjectCN) |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
60 .arg(mSubjectO) |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
61 .arg(mSubjectOU) |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
62 .arg(mSubjectSN) |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
63 .arg(QLocale::system().toString(mValidFrom)) |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
64 .arg(QLocale::system().toString(mValidTo)); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
65 } |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
66 |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
67 Certificate::Certificate(const QByteArray& derData) : |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
68 mValid(false) |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
69 { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
70 if (derData.isEmpty()) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
71 return; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
72 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
73 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
74 parseDetails(derData); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
75 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
76 mValid = !mSubjectCN.isEmpty(); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
77 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
78 mBaseLine = derData.toBase64(); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
79 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
80 |
83
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
81 Certificate::Certificate(const QString& b64Line) : |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
82 mValid(false) |
81
112228bd7e4b
Remove platform specific certificate immplementations.
Andre Heinecke <aheinecke@intevation.de>
parents:
78
diff
changeset
|
83 { |
204
825b42da1855
Avoid printing an error when an empty certificate ist constructed
Andre Heinecke <andre.heinecke@intevation.de>
parents:
186
diff
changeset
|
84 if (b64Line.isEmpty()) { |
825b42da1855
Avoid printing an error when an empty certificate ist constructed
Andre Heinecke <andre.heinecke@intevation.de>
parents:
186
diff
changeset
|
85 return; |
825b42da1855
Avoid printing an error when an empty certificate ist constructed
Andre Heinecke <andre.heinecke@intevation.de>
parents:
186
diff
changeset
|
86 } |
825b42da1855
Avoid printing an error when an empty certificate ist constructed
Andre Heinecke <andre.heinecke@intevation.de>
parents:
186
diff
changeset
|
87 |
83
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
88 /* Cut of the first two chars (e.g. I: and decode) */ |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
89 QByteArray derData = QByteArray::fromBase64( |
83
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
90 b64Line.right(b64Line.size() - 2).toLatin1()); |
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
91 |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
92 parseDetails(derData); |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
93 |
338
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
94 /* If the subject CN is set then at least one x509parse |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
95 * in polarssl was successfull. And a root certificate |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
96 * always needs to have a subject CN */ |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
97 mValid = !mSubjectCN.isEmpty(); |
83
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
98 |
ba8a548ff252
Expand certificate class to make raw data accessible
Andre Heinecke <aheinecke@intevation.de>
parents:
82
diff
changeset
|
99 mBaseLine = b64Line; |
81
112228bd7e4b
Remove platform specific certificate immplementations.
Andre Heinecke <aheinecke@intevation.de>
parents:
78
diff
changeset
|
100 } |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
101 |
338
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
102 QString Certificate::shortDescription() const { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
103 if (!isValid()) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
104 return QObject::tr("Failed to parse certificate"); |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
105 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
106 |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
107 QString ret = mSubjectCN; /* Necessary by definition */ |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
108 if (!mSubjectO.isEmpty()) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
109 ret += " - " + mSubjectO; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
110 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
111 if (!mSubjectOU.isEmpty()) { |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
112 ret += ", " + mSubjectOU; |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
113 } |
64e38886f903
Use certhelp for certificate parsing and add some dummy info
Andre Heinecke <aheinecke@intevation.de>
parents:
204
diff
changeset
|
114 return ret; |
186
2551ad24d3c2
Get subject from the certificate and parse it's attributes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
94
diff
changeset
|
115 } |
349
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
116 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
117 QList<Certificate> Certificate::fromFileName(const QString& file_name) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
118 /* We read the file using Qt to avoid filename encoding problems |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
119 * on Windows */ |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
120 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
121 /* TODO change qDebug errors into messageboxes */ |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
122 QFile certificateFile(file_name); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
123 QByteArray fileContent; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
124 QList<Certificate> retval; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
125 x509_crt chain; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
126 int ret = 0; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
127 if (!certificateFile.open(QIODevice::ReadOnly)) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
128 qDebug() << "Failed to read file."; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
129 return retval; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
130 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
131 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
132 if (certificateFile.size() > MAX_LINE_LENGTH * MAX_LINES) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
133 qDebug() << "File too large"; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
134 return retval; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
135 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
136 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
137 fileContent = certificateFile.readAll(); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
138 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
139 x509_crt_init(&chain); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
140 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
141 ret = x509_crt_parse(&chain, |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
142 reinterpret_cast<const unsigned char*>(fileContent.constData()), |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
143 fileContent.size()); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
144 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
145 if (ret < 0) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
146 qDebug() << "Failed to parse certificates."; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
147 return retval; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
148 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
149 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
150 if (ret > 0) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
151 qDebug() << "Some certificates could not be parsed."; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
152 /* Maybe return here? */ |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
153 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
154 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
155 x509_crt *iter = &chain; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
156 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
157 while (iter) { |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
158 QByteArray derData(reinterpret_cast<const char*>(iter->raw.p), |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
159 static_cast<int>(iter->raw.len)); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
160 retval << Certificate(derData); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
161 iter = iter->next; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
162 } |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
163 x509_crt_free(&chain); |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
164 |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
165 return retval; |
a49766196a7d
Add certificateFromFile method
Andre Heinecke <andre.heinecke@intevation.de>
parents:
338
diff
changeset
|
166 } |