1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Certificate info class for TLS untrusted certificate dialog. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "UntrustedCertificateInfoBase.h" |
|
19 |
|
20 const char *KHexNumberFormatSimple = "%X"; |
|
21 const char *KHexNumberFormatTwoDigitsWithLeadingZeroes = "%02X"; |
|
22 const int KCharsPerBlock = 2; |
|
23 const char KBlockSeparator = ' '; |
|
24 |
|
25 |
|
26 // ======== MEMBER FUNCTIONS ======== |
|
27 |
|
28 // ---------------------------------------------------------------------------- |
|
29 // UntrustedCertificateInfoBase::UntrustedCertificateInfoBase() |
|
30 // ---------------------------------------------------------------------------- |
|
31 // |
|
32 UntrustedCertificateInfoBase::UntrustedCertificateInfoBase() : QObject(), |
|
33 mSubjectName(), mIssuerName(), mFingerprint(), mSerialNumber(), mValidFrom(), mValidTo(), |
|
34 mFormat(UnknownCertificate), mDigestAlgorithm(Unknown), mAsymmetricAlgorithm(Unknown) |
|
35 { |
|
36 } |
|
37 |
|
38 // ---------------------------------------------------------------------------- |
|
39 // UntrustedCertificateInfoBase::~UntrustedCertificateInfoBase() |
|
40 // ---------------------------------------------------------------------------- |
|
41 // |
|
42 UntrustedCertificateInfoBase::~UntrustedCertificateInfoBase() |
|
43 { |
|
44 } |
|
45 |
|
46 // ---------------------------------------------------------------------------- |
|
47 // UntrustedCertificateInfoBase::subjectName() |
|
48 // ---------------------------------------------------------------------------- |
|
49 // |
|
50 const QString UntrustedCertificateInfoBase::subjectName() const |
|
51 { |
|
52 return mSubjectName; |
|
53 } |
|
54 |
|
55 // ---------------------------------------------------------------------------- |
|
56 // UntrustedCertificateInfoBase::issuerName() |
|
57 // ---------------------------------------------------------------------------- |
|
58 // |
|
59 const QString UntrustedCertificateInfoBase::issuerName() const |
|
60 { |
|
61 return mIssuerName; |
|
62 } |
|
63 |
|
64 // ---------------------------------------------------------------------------- |
|
65 // UntrustedCertificateInfoBase::fingerprint() |
|
66 // ---------------------------------------------------------------------------- |
|
67 // |
|
68 const QByteArray UntrustedCertificateInfoBase::fingerprint() const |
|
69 { |
|
70 return mFingerprint; |
|
71 } |
|
72 |
|
73 // ---------------------------------------------------------------------------- |
|
74 // UntrustedCertificateInfoBase::serialNumber() |
|
75 // ---------------------------------------------------------------------------- |
|
76 // |
|
77 const QByteArray UntrustedCertificateInfoBase::serialNumber() const |
|
78 { |
|
79 return mSerialNumber; |
|
80 } |
|
81 |
|
82 // ---------------------------------------------------------------------------- |
|
83 // UntrustedCertificateInfoBase::validFrom() |
|
84 // ---------------------------------------------------------------------------- |
|
85 // |
|
86 const QDateTime UntrustedCertificateInfoBase::validFrom() const |
|
87 { |
|
88 return mValidFrom; |
|
89 } |
|
90 |
|
91 // ---------------------------------------------------------------------------- |
|
92 // UntrustedCertificateInfoBase::validTo() |
|
93 // ---------------------------------------------------------------------------- |
|
94 // |
|
95 const QDateTime UntrustedCertificateInfoBase::validTo() const |
|
96 { |
|
97 return mValidTo; |
|
98 } |
|
99 |
|
100 // ---------------------------------------------------------------------------- |
|
101 // UntrustedCertificateInfoBase::format() |
|
102 // ---------------------------------------------------------------------------- |
|
103 // |
|
104 const QString UntrustedCertificateInfoBase::format() const |
|
105 { |
|
106 QString format; |
|
107 switch (mFormat) { |
|
108 case X509Certificate: |
|
109 //: Type name for X509 certificates displayed in certificate details. |
|
110 // TODO: localised UI string |
|
111 format = tr("X.509"); |
|
112 break; |
|
113 default: |
|
114 //: Type name for unknown certificates displayed in certificate details. |
|
115 // TODO: localised UI string |
|
116 format = tr("Unknown"); |
|
117 break; |
|
118 } |
|
119 return format; |
|
120 } |
|
121 |
|
122 // ---------------------------------------------------------------------------- |
|
123 // UntrustedCertificateInfoBase::digestAlgorithm() |
|
124 // ---------------------------------------------------------------------------- |
|
125 // |
|
126 const QString UntrustedCertificateInfoBase::digestAlgorithm() const |
|
127 { |
|
128 return algorithmName(mDigestAlgorithm); |
|
129 } |
|
130 |
|
131 // ---------------------------------------------------------------------------- |
|
132 // UntrustedCertificateInfoBase::AsymmetricAlgorithm() |
|
133 // ---------------------------------------------------------------------------- |
|
134 // |
|
135 const QString UntrustedCertificateInfoBase::asymmetricAlgorithm() const |
|
136 { |
|
137 return algorithmName(mAsymmetricAlgorithm); |
|
138 } |
|
139 |
|
140 // ---------------------------------------------------------------------------- |
|
141 // UntrustedCertificateInfoBase::AsymmetricAlgorithm() |
|
142 // ---------------------------------------------------------------------------- |
|
143 // |
|
144 const QString UntrustedCertificateInfoBase::combinedAlgorithmName() const |
|
145 { |
|
146 if (mDigestAlgorithm != Unknown && mAsymmetricAlgorithm != Unknown) { |
|
147 //: Format to combine digest and asymmetric algorithm names. |
|
148 //: %1 is digest (signing) and %2 is asymmetric (public-key) algorithm. |
|
149 // TODO: localised UI string needed |
|
150 return tr("%1%2").arg(digestAlgorithm()).arg(asymmetricAlgorithm()); |
|
151 } |
|
152 //: Algorithm name for unknown algorithm. |
|
153 // TODO: localised UI string needed |
|
154 return tr("Unknown"); |
|
155 } |
|
156 |
|
157 // ---------------------------------------------------------------------------- |
|
158 // UntrustedCertificateInfoBase::formattedFingerprint() |
|
159 // ---------------------------------------------------------------------------- |
|
160 // |
|
161 const QString UntrustedCertificateInfoBase::formattedFingerprint( |
|
162 const QByteArray &byteArray) const |
|
163 { |
|
164 QString formatted; |
|
165 |
|
166 QString number; |
|
167 int blockIndex = 0; |
|
168 int count = byteArray.count(); |
|
169 for (int index = 0; index < count; ++index) { |
|
170 if (blockIndex == KCharsPerBlock) { |
|
171 formatted.append(KBlockSeparator); |
|
172 blockIndex = 0; |
|
173 } |
|
174 number.sprintf(KHexNumberFormatTwoDigitsWithLeadingZeroes, |
|
175 static_cast<unsigned char>(byteArray.at(index))); |
|
176 formatted.append(number); |
|
177 ++blockIndex; |
|
178 } |
|
179 |
|
180 return formatted; |
|
181 } |
|
182 |
|
183 // ---------------------------------------------------------------------------- |
|
184 // UntrustedCertificateInfoBase::formattedSerialNumber() |
|
185 // ---------------------------------------------------------------------------- |
|
186 // |
|
187 const QString UntrustedCertificateInfoBase::formattedSerialNumber( |
|
188 const QByteArray &serialNumber) const |
|
189 { |
|
190 QString formatted; |
|
191 |
|
192 QString number; |
|
193 int count = serialNumber.count(); |
|
194 for (int index = 0; index < count; ++index) { |
|
195 number.sprintf(KHexNumberFormatSimple, |
|
196 static_cast<unsigned char>(serialNumber.at(index))); |
|
197 formatted.append(number); |
|
198 } |
|
199 |
|
200 return formatted; |
|
201 } |
|
202 |
|
203 // ---------------------------------------------------------------------------- |
|
204 // UntrustedCertificateInfoBase::isDateValid() |
|
205 // ---------------------------------------------------------------------------- |
|
206 // |
|
207 bool UntrustedCertificateInfoBase::isDateValid() const |
|
208 { |
|
209 QDateTime current = QDateTime::currentDateTime(); |
|
210 return (mValidFrom <= current && mValidTo >= current); |
|
211 } |
|
212 |
|
213 // ---------------------------------------------------------------------------- |
|
214 // UntrustedCertificateInfoBase::algorithmName() |
|
215 // ---------------------------------------------------------------------------- |
|
216 // |
|
217 const QString UntrustedCertificateInfoBase::algorithmName(Algorithm algorithm) const |
|
218 { |
|
219 QString name; |
|
220 switch (algorithm) { |
|
221 case RSA: |
|
222 //: Certificate details algorithm name |
|
223 // TODO: localized UI string |
|
224 name = tr("RSA"); |
|
225 break; |
|
226 case DSA: |
|
227 //: Certificate details algorithm name |
|
228 // TODO: localized UI string |
|
229 name = tr("DSA"); |
|
230 break; |
|
231 case DH: |
|
232 //: Certificate details algorithm name |
|
233 // TODO: localized UI string |
|
234 name = tr("DH"); |
|
235 break; |
|
236 case MD2: |
|
237 //: Certificate details algorithm name |
|
238 // TODO: localized UI string |
|
239 name = tr("MD2"); |
|
240 break; |
|
241 case MD5: |
|
242 //: Certificate details algorithm name |
|
243 // TODO: localized UI string |
|
244 name = tr("MD5"); |
|
245 break; |
|
246 case SHA1: |
|
247 //: Certificate details algorithm name |
|
248 // TODO: localized UI string |
|
249 name = tr("SHA1"); |
|
250 break; |
|
251 case SHA224: |
|
252 case SHA256: |
|
253 case SHA384: |
|
254 case SHA512: |
|
255 //: Certificate details algorithm name |
|
256 // TODO: localized UI string |
|
257 name = tr("SHA2"); |
|
258 break; |
|
259 case Unknown: |
|
260 default: |
|
261 //: Certificate details algorithm name |
|
262 // TODO: localized UI string |
|
263 name = tr("Unknown"); |
|
264 break; |
|
265 } |
|
266 return name; |
|
267 } |
|
268 |
|