1 | /* $Id: CertificateImpl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ICertificate COM class implementations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_MAIN_CERTIFICATE
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include <iprt/path.h>
|
---|
31 | #include <iprt/cpp/utils.h>
|
---|
32 | #include <VBox/com/array.h>
|
---|
33 | #include <iprt/crypto/x509.h>
|
---|
34 |
|
---|
35 | #include "ProgressImpl.h"
|
---|
36 | #include "CertificateImpl.h"
|
---|
37 | #include "AutoCaller.h"
|
---|
38 | #include "Global.h"
|
---|
39 | #include "LoggingNew.h"
|
---|
40 |
|
---|
41 | using namespace std;
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Private instance data for the #Certificate class.
|
---|
46 | * @see Certificate::m
|
---|
47 | */
|
---|
48 | struct Certificate::Data
|
---|
49 | {
|
---|
50 | Data()
|
---|
51 | : fTrusted(false)
|
---|
52 | , fExpired(false)
|
---|
53 | , fValidX509(false)
|
---|
54 | {
|
---|
55 | RT_ZERO(X509);
|
---|
56 | }
|
---|
57 |
|
---|
58 | ~Data()
|
---|
59 | {
|
---|
60 | if (fValidX509)
|
---|
61 | {
|
---|
62 | RTCrX509Certificate_Delete(&X509);
|
---|
63 | RT_ZERO(X509);
|
---|
64 | fValidX509 = false;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Whether the certificate is trusted. */
|
---|
69 | bool fTrusted;
|
---|
70 | /** Whether the certificate is trusted. */
|
---|
71 | bool fExpired;
|
---|
72 | /** Valid data in mX509. */
|
---|
73 | bool fValidX509;
|
---|
74 | /** Clone of the X.509 certificate. */
|
---|
75 | RTCRX509CERTIFICATE X509;
|
---|
76 |
|
---|
77 | private:
|
---|
78 | Data(const Certificate::Data &rTodo) { AssertFailed(); NOREF(rTodo); }
|
---|
79 | Data &operator=(const Certificate::Data &rTodo) { AssertFailed(); NOREF(rTodo); return *this; }
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | ///////////////////////////////////////////////////////////////////////////////////
|
---|
84 | //
|
---|
85 | // Certificate constructor / destructor
|
---|
86 | //
|
---|
87 | // ////////////////////////////////////////////////////////////////////////////////
|
---|
88 |
|
---|
89 | DEFINE_EMPTY_CTOR_DTOR(Certificate)
|
---|
90 |
|
---|
91 | HRESULT Certificate::FinalConstruct()
|
---|
92 | {
|
---|
93 | return BaseFinalConstruct();
|
---|
94 | }
|
---|
95 |
|
---|
96 | void Certificate::FinalRelease()
|
---|
97 | {
|
---|
98 | uninit();
|
---|
99 | BaseFinalRelease();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Initializes a certificate instance.
|
---|
104 | *
|
---|
105 | * @returns COM status code.
|
---|
106 | * @param a_pCert The certificate.
|
---|
107 | * @param a_fTrusted Whether the caller trusts the certificate or not.
|
---|
108 | * @param a_fExpired Whether the caller consideres the certificate to be
|
---|
109 | * expired.
|
---|
110 | */
|
---|
111 | HRESULT Certificate::initCertificate(PCRTCRX509CERTIFICATE a_pCert, bool a_fTrusted, bool a_fExpired)
|
---|
112 | {
|
---|
113 | HRESULT rc = S_OK;
|
---|
114 | LogFlowThisFuncEnter();
|
---|
115 |
|
---|
116 | AutoInitSpan autoInitSpan(this);
|
---|
117 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
118 |
|
---|
119 | m = new Data();
|
---|
120 |
|
---|
121 | int vrc = RTCrX509Certificate_Clone(&m->X509, a_pCert, &g_RTAsn1DefaultAllocator);
|
---|
122 | if (RT_SUCCESS(vrc))
|
---|
123 | {
|
---|
124 | m->fValidX509 = true;
|
---|
125 | m->fTrusted = a_fTrusted;
|
---|
126 | m->fExpired = a_fExpired;
|
---|
127 | autoInitSpan.setSucceeded();
|
---|
128 | }
|
---|
129 | else
|
---|
130 | rc = Global::vboxStatusCodeToCOM(vrc);
|
---|
131 |
|
---|
132 | LogFlowThisFunc(("returns rc=%Rhrc\n", rc));
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void Certificate::uninit()
|
---|
137 | {
|
---|
138 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
139 | AutoUninitSpan autoUninitSpan(this);
|
---|
140 | if (autoUninitSpan.uninitDone())
|
---|
141 | return;
|
---|
142 |
|
---|
143 | delete m;
|
---|
144 | m = NULL;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /** @name Wrapped ICertificate properties
|
---|
149 | * @{
|
---|
150 | */
|
---|
151 |
|
---|
152 | HRESULT Certificate::getVersionNumber(CertificateVersion_T *aVersionNumber)
|
---|
153 | {
|
---|
154 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
155 |
|
---|
156 | Assert(m->fValidX509);
|
---|
157 | switch (m->X509.TbsCertificate.T0.Version.uValue.u)
|
---|
158 | {
|
---|
159 | case RTCRX509TBSCERTIFICATE_V1: *aVersionNumber = CertificateVersion_V1; break;
|
---|
160 | case RTCRX509TBSCERTIFICATE_V2: *aVersionNumber = CertificateVersion_V2; break;
|
---|
161 | case RTCRX509TBSCERTIFICATE_V3: *aVersionNumber = CertificateVersion_V3; break;
|
---|
162 | default: AssertFailed(); *aVersionNumber = CertificateVersion_Unknown; break;
|
---|
163 | }
|
---|
164 | return S_OK;
|
---|
165 | }
|
---|
166 |
|
---|
167 | HRESULT Certificate::getSerialNumber(com::Utf8Str &aSerialNumber)
|
---|
168 | {
|
---|
169 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
170 |
|
---|
171 | Assert(m->fValidX509);
|
---|
172 |
|
---|
173 | char szTmp[_2K];
|
---|
174 | int vrc = RTAsn1Integer_ToString(&m->X509.TbsCertificate.SerialNumber, szTmp, sizeof(szTmp), 0, NULL);
|
---|
175 | if (RT_SUCCESS(vrc))
|
---|
176 | aSerialNumber = szTmp;
|
---|
177 | else
|
---|
178 | return Global::vboxStatusCodeToCOM(vrc);
|
---|
179 |
|
---|
180 | return S_OK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | HRESULT Certificate::getSignatureAlgorithmOID(com::Utf8Str &aSignatureAlgorithmOID)
|
---|
184 | {
|
---|
185 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
186 |
|
---|
187 | Assert(m->fValidX509);
|
---|
188 | aSignatureAlgorithmOID = m->X509.TbsCertificate.Signature.Algorithm.szObjId;
|
---|
189 |
|
---|
190 | return S_OK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | HRESULT Certificate::getSignatureAlgorithmName(com::Utf8Str &aSignatureAlgorithmName)
|
---|
194 | {
|
---|
195 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
196 |
|
---|
197 | Assert(m->fValidX509);
|
---|
198 | return i_getAlgorithmName(&m->X509.TbsCertificate.Signature, aSignatureAlgorithmName);
|
---|
199 | }
|
---|
200 |
|
---|
201 | HRESULT Certificate::getIssuerName(std::vector<com::Utf8Str> &aIssuerName)
|
---|
202 | {
|
---|
203 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
204 |
|
---|
205 | Assert(m->fValidX509);
|
---|
206 | return i_getX509Name(&m->X509.TbsCertificate.Issuer, aIssuerName);
|
---|
207 | }
|
---|
208 |
|
---|
209 | HRESULT Certificate::getSubjectName(std::vector<com::Utf8Str> &aSubjectName)
|
---|
210 | {
|
---|
211 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
212 |
|
---|
213 | Assert(m->fValidX509);
|
---|
214 | return i_getX509Name(&m->X509.TbsCertificate.Subject, aSubjectName);
|
---|
215 | }
|
---|
216 |
|
---|
217 | HRESULT Certificate::getFriendlyName(com::Utf8Str &aFriendlyName)
|
---|
218 | {
|
---|
219 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
220 |
|
---|
221 | Assert(m->fValidX509);
|
---|
222 |
|
---|
223 | PCRTCRX509NAME pName = &m->X509.TbsCertificate.Subject;
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Enumerate the subject name and pick interesting attributes we can use to
|
---|
227 | * form a name more friendly than the RTCrX509Name_FormatAsString output.
|
---|
228 | */
|
---|
229 | const char *pszOrg = NULL;
|
---|
230 | const char *pszOrgUnit = NULL;
|
---|
231 | const char *pszGivenName = NULL;
|
---|
232 | const char *pszSurname = NULL;
|
---|
233 | const char *pszEmail = NULL;
|
---|
234 | for (uint32_t i = 0; i < pName->cItems; i++)
|
---|
235 | {
|
---|
236 | PCRTCRX509RELATIVEDISTINGUISHEDNAME pRdn = pName->papItems[i];
|
---|
237 | for (uint32_t j = 0; j < pRdn->cItems; j++)
|
---|
238 | {
|
---|
239 | PCRTCRX509ATTRIBUTETYPEANDVALUE pComponent = pRdn->papItems[j];
|
---|
240 | AssertContinue(pComponent->Value.enmType == RTASN1TYPE_STRING);
|
---|
241 |
|
---|
242 | /* Select interesting components based on the short RDN prefix
|
---|
243 | string (easier to read and write than OIDs, for now). */
|
---|
244 | const char *pszPrefix = RTCrX509Name_GetShortRdn(&pComponent->Type);
|
---|
245 | if (pszPrefix)
|
---|
246 | {
|
---|
247 | const char *pszUtf8;
|
---|
248 | int vrc = RTAsn1String_QueryUtf8(&pComponent->Value.u.String, &pszUtf8, NULL);
|
---|
249 | if (RT_SUCCESS(vrc) && *pszUtf8)
|
---|
250 | {
|
---|
251 | if (!strcmp(pszPrefix, "Email"))
|
---|
252 | pszEmail = pszUtf8;
|
---|
253 | else if (!strcmp(pszPrefix, "O"))
|
---|
254 | pszOrg = pszUtf8;
|
---|
255 | else if (!strcmp(pszPrefix, "OU"))
|
---|
256 | pszOrgUnit = pszUtf8;
|
---|
257 | else if (!strcmp(pszPrefix, "S"))
|
---|
258 | pszSurname = pszUtf8;
|
---|
259 | else if (!strcmp(pszPrefix, "G"))
|
---|
260 | pszGivenName = pszUtf8;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (pszGivenName && pszSurname)
|
---|
267 | {
|
---|
268 | if (pszEmail)
|
---|
269 | aFriendlyName = Utf8StrFmt("%s, %s <%s>", pszSurname, pszGivenName, pszEmail);
|
---|
270 | else if (pszOrg)
|
---|
271 | aFriendlyName = Utf8StrFmt("%s, %s (%s)", pszSurname, pszGivenName, pszOrg);
|
---|
272 | else if (pszOrgUnit)
|
---|
273 | aFriendlyName = Utf8StrFmt("%s, %s (%s)", pszSurname, pszGivenName, pszOrgUnit);
|
---|
274 | else
|
---|
275 | aFriendlyName = Utf8StrFmt("%s, %s", pszSurname, pszGivenName);
|
---|
276 | }
|
---|
277 | else if (pszOrg && pszOrgUnit)
|
---|
278 | aFriendlyName = Utf8StrFmt("%s, %s", pszOrg, pszOrgUnit);
|
---|
279 | else if (pszOrg)
|
---|
280 | aFriendlyName = Utf8StrFmt("%s", pszOrg);
|
---|
281 | else if (pszOrgUnit)
|
---|
282 | aFriendlyName = Utf8StrFmt("%s", pszOrgUnit);
|
---|
283 | else
|
---|
284 | {
|
---|
285 | /*
|
---|
286 | * Fall back on unfriendly but accurate.
|
---|
287 | */
|
---|
288 | char szTmp[_8K];
|
---|
289 | RT_ZERO(szTmp);
|
---|
290 | RTCrX509Name_FormatAsString(pName, szTmp, sizeof(szTmp) - 1, NULL);
|
---|
291 | aFriendlyName = szTmp;
|
---|
292 | }
|
---|
293 |
|
---|
294 | return S_OK;
|
---|
295 | }
|
---|
296 |
|
---|
297 | HRESULT Certificate::getValidityPeriodNotBefore(com::Utf8Str &aValidityPeriodNotBefore)
|
---|
298 | {
|
---|
299 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
300 |
|
---|
301 | Assert(m->fValidX509);
|
---|
302 | return i_getTime(&m->X509.TbsCertificate.Validity.NotBefore, aValidityPeriodNotBefore);
|
---|
303 | }
|
---|
304 |
|
---|
305 | HRESULT Certificate::getValidityPeriodNotAfter(com::Utf8Str &aValidityPeriodNotAfter)
|
---|
306 | {
|
---|
307 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
308 |
|
---|
309 | Assert(m->fValidX509);
|
---|
310 | return i_getTime(&m->X509.TbsCertificate.Validity.NotAfter, aValidityPeriodNotAfter);
|
---|
311 | }
|
---|
312 |
|
---|
313 | HRESULT Certificate::getPublicKeyAlgorithmOID(com::Utf8Str &aPublicKeyAlgorithmOID)
|
---|
314 | {
|
---|
315 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
316 |
|
---|
317 | Assert(m->fValidX509);
|
---|
318 | aPublicKeyAlgorithmOID = m->X509.TbsCertificate.SubjectPublicKeyInfo.Algorithm.Algorithm.szObjId;
|
---|
319 | return S_OK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | HRESULT Certificate::getPublicKeyAlgorithm(com::Utf8Str &aPublicKeyAlgorithm)
|
---|
323 | {
|
---|
324 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
325 |
|
---|
326 | Assert(m->fValidX509);
|
---|
327 | return i_getAlgorithmName(&m->X509.TbsCertificate.SubjectPublicKeyInfo.Algorithm, aPublicKeyAlgorithm);
|
---|
328 | }
|
---|
329 |
|
---|
330 | HRESULT Certificate::getSubjectPublicKey(std::vector<BYTE> &aSubjectPublicKey)
|
---|
331 | {
|
---|
332 |
|
---|
333 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* Getting encoded ASN.1 bytes may make changes to X509. */
|
---|
334 | return i_getEncodedBytes(&m->X509.TbsCertificate.SubjectPublicKeyInfo.SubjectPublicKey.Asn1Core, aSubjectPublicKey);
|
---|
335 | }
|
---|
336 |
|
---|
337 | HRESULT Certificate::getIssuerUniqueIdentifier(com::Utf8Str &aIssuerUniqueIdentifier)
|
---|
338 | {
|
---|
339 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
340 |
|
---|
341 | return i_getUniqueIdentifier(&m->X509.TbsCertificate.T1.IssuerUniqueId, aIssuerUniqueIdentifier);
|
---|
342 | }
|
---|
343 |
|
---|
344 | HRESULT Certificate::getSubjectUniqueIdentifier(com::Utf8Str &aSubjectUniqueIdentifier)
|
---|
345 | {
|
---|
346 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
347 |
|
---|
348 | return i_getUniqueIdentifier(&m->X509.TbsCertificate.T2.SubjectUniqueId, aSubjectUniqueIdentifier);
|
---|
349 | }
|
---|
350 |
|
---|
351 | HRESULT Certificate::getCertificateAuthority(BOOL *aCertificateAuthority)
|
---|
352 | {
|
---|
353 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
354 |
|
---|
355 | *aCertificateAuthority = m->X509.TbsCertificate.T3.pBasicConstraints
|
---|
356 | && m->X509.TbsCertificate.T3.pBasicConstraints->CA.fValue;
|
---|
357 |
|
---|
358 | return S_OK;
|
---|
359 | }
|
---|
360 |
|
---|
361 | HRESULT Certificate::getKeyUsage(ULONG *aKeyUsage)
|
---|
362 | {
|
---|
363 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
364 |
|
---|
365 | *aKeyUsage = m->X509.TbsCertificate.T3.fKeyUsage;
|
---|
366 | return S_OK;
|
---|
367 | }
|
---|
368 |
|
---|
369 | HRESULT Certificate::getExtendedKeyUsage(std::vector<com::Utf8Str> &aExtendedKeyUsage)
|
---|
370 | {
|
---|
371 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
372 | NOREF(aExtendedKeyUsage);
|
---|
373 | return E_NOTIMPL;
|
---|
374 | }
|
---|
375 |
|
---|
376 | HRESULT Certificate::getRawCertData(std::vector<BYTE> &aRawCertData)
|
---|
377 | {
|
---|
378 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* Getting encoded ASN.1 bytes may make changes to X509. */
|
---|
379 | return i_getEncodedBytes(&m->X509.SeqCore.Asn1Core, aRawCertData);
|
---|
380 | }
|
---|
381 |
|
---|
382 | HRESULT Certificate::getSelfSigned(BOOL *aSelfSigned)
|
---|
383 | {
|
---|
384 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
385 |
|
---|
386 | Assert(m->fValidX509);
|
---|
387 | *aSelfSigned = RTCrX509Certificate_IsSelfSigned(&m->X509);
|
---|
388 |
|
---|
389 | return S_OK;
|
---|
390 | }
|
---|
391 |
|
---|
392 | HRESULT Certificate::getTrusted(BOOL *aTrusted)
|
---|
393 | {
|
---|
394 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
395 |
|
---|
396 | Assert(m->fValidX509);
|
---|
397 | *aTrusted = m->fTrusted;
|
---|
398 |
|
---|
399 | return S_OK;
|
---|
400 | }
|
---|
401 |
|
---|
402 | HRESULT Certificate::getExpired(BOOL *aExpired)
|
---|
403 | {
|
---|
404 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
405 | Assert(m->fValidX509);
|
---|
406 | *aExpired = m->fExpired;
|
---|
407 | return S_OK;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /** @} */
|
---|
411 |
|
---|
412 | /** @name Wrapped ICertificate methods
|
---|
413 | * @{
|
---|
414 | */
|
---|
415 |
|
---|
416 | HRESULT Certificate::isCurrentlyExpired(BOOL *aResult)
|
---|
417 | {
|
---|
418 | AssertReturnStmt(m->fValidX509, *aResult = TRUE, E_UNEXPECTED);
|
---|
419 | RTTIMESPEC Now;
|
---|
420 | *aResult = RTCrX509Validity_IsValidAtTimeSpec(&m->X509.TbsCertificate.Validity, RTTimeNow(&Now)) ? FALSE : TRUE;
|
---|
421 | return S_OK;
|
---|
422 | }
|
---|
423 |
|
---|
424 | HRESULT Certificate::queryInfo(LONG aWhat, com::Utf8Str &aResult)
|
---|
425 | {
|
---|
426 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
427 | /* Insurance. */
|
---|
428 | NOREF(aResult);
|
---|
429 | return setError(E_FAIL, tr("Unknown item %u"), aWhat);
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** @} */
|
---|
433 |
|
---|
434 |
|
---|
435 | /** @name Methods extracting COM data from the certificate object
|
---|
436 | * @{
|
---|
437 | */
|
---|
438 |
|
---|
439 | /**
|
---|
440 | * Translates an algorithm OID into a human readable string, if possible.
|
---|
441 | *
|
---|
442 | * @returns S_OK.
|
---|
443 | * @param a_pAlgId The algorithm.
|
---|
444 | * @param a_rReturn The return string value.
|
---|
445 | * @throws std::bad_alloc
|
---|
446 | */
|
---|
447 | HRESULT Certificate::i_getAlgorithmName(PCRTCRX509ALGORITHMIDENTIFIER a_pAlgId, com::Utf8Str &a_rReturn)
|
---|
448 | {
|
---|
449 | const char *pszOid = a_pAlgId->Algorithm.szObjId;
|
---|
450 | const char *pszName;
|
---|
451 | if (!pszOid) pszName = "";
|
---|
452 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_RSA)) pszName = "rsaEncryption";
|
---|
453 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_MD2_WITH_RSA)) pszName = "md2WithRSAEncryption";
|
---|
454 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_MD4_WITH_RSA)) pszName = "md4WithRSAEncryption";
|
---|
455 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_MD5_WITH_RSA)) pszName = "md5WithRSAEncryption";
|
---|
456 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA1_WITH_RSA)) pszName = "sha1WithRSAEncryption";
|
---|
457 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA224_WITH_RSA)) pszName = "sha224WithRSAEncryption";
|
---|
458 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA256_WITH_RSA)) pszName = "sha256WithRSAEncryption";
|
---|
459 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA384_WITH_RSA)) pszName = "sha384WithRSAEncryption";
|
---|
460 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA512_WITH_RSA)) pszName = "sha512WithRSAEncryption";
|
---|
461 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA512T224_WITH_RSA)) pszName = "sha512-224WithRSAEncryption";
|
---|
462 | else if (strcmp(pszOid, RTCRX509ALGORITHMIDENTIFIERID_SHA512T256_WITH_RSA)) pszName = "sha512-256WithRSAEncryption";
|
---|
463 | else
|
---|
464 | pszName = pszOid;
|
---|
465 | a_rReturn = pszName;
|
---|
466 | return S_OK;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Formats a X.509 name into a string array.
|
---|
471 | *
|
---|
472 | * The name is prefix with a short hand of the relative distinguished name
|
---|
473 | * type followed by an equal sign.
|
---|
474 | *
|
---|
475 | * @returns S_OK.
|
---|
476 | * @param a_pName The X.509 name.
|
---|
477 | * @param a_rReturn The return string array.
|
---|
478 | * @throws std::bad_alloc
|
---|
479 | */
|
---|
480 | HRESULT Certificate::i_getX509Name(PCRTCRX509NAME a_pName, std::vector<com::Utf8Str> &a_rReturn)
|
---|
481 | {
|
---|
482 | if (RTCrX509Name_IsPresent(a_pName))
|
---|
483 | {
|
---|
484 | for (uint32_t i = 0; i < a_pName->cItems; i++)
|
---|
485 | {
|
---|
486 | PCRTCRX509RELATIVEDISTINGUISHEDNAME pRdn = a_pName->papItems[i];
|
---|
487 | for (uint32_t j = 0; j < pRdn->cItems; j++)
|
---|
488 | {
|
---|
489 | PCRTCRX509ATTRIBUTETYPEANDVALUE pComponent = pRdn->papItems[j];
|
---|
490 |
|
---|
491 | AssertReturn(pComponent->Value.enmType == RTASN1TYPE_STRING,
|
---|
492 | setErrorVrc(VERR_CR_X509_NAME_NOT_STRING, "VERR_CR_X509_NAME_NOT_STRING"));
|
---|
493 |
|
---|
494 | /* Get the prefix for this name component. */
|
---|
495 | const char *pszPrefix = RTCrX509Name_GetShortRdn(&pComponent->Type);
|
---|
496 | AssertStmt(pszPrefix, pszPrefix = pComponent->Type.szObjId);
|
---|
497 |
|
---|
498 | /* Get the string. */
|
---|
499 | const char *pszUtf8;
|
---|
500 | int vrc = RTAsn1String_QueryUtf8(&pComponent->Value.u.String, &pszUtf8, NULL /*pcch*/);
|
---|
501 | AssertRCReturn(vrc, setErrorVrc(vrc, "RTAsn1String_QueryUtf8(%u/%u,,) -> %Rrc", i, j, vrc));
|
---|
502 |
|
---|
503 | a_rReturn.push_back(Utf8StrFmt("%s=%s", pszPrefix, pszUtf8));
|
---|
504 | }
|
---|
505 | }
|
---|
506 | }
|
---|
507 | return S_OK;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Translates an ASN.1 timestamp into an ISO timestamp string.
|
---|
512 | *
|
---|
513 | * @returns S_OK.
|
---|
514 | * @param a_pTime The timestamp
|
---|
515 | * @param a_rReturn The return string value.
|
---|
516 | * @throws std::bad_alloc
|
---|
517 | */
|
---|
518 | HRESULT Certificate::i_getTime(PCRTASN1TIME a_pTime, com::Utf8Str &a_rReturn)
|
---|
519 | {
|
---|
520 | char szTmp[128];
|
---|
521 | if (RTTimeToString(&a_pTime->Time, szTmp, sizeof(szTmp)))
|
---|
522 | {
|
---|
523 | a_rReturn = szTmp;
|
---|
524 | return S_OK;
|
---|
525 | }
|
---|
526 | AssertFailed();
|
---|
527 | return E_FAIL;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Translates a X.509 unique identifier to a string.
|
---|
532 | *
|
---|
533 | * @returns S_OK.
|
---|
534 | * @param a_pUniqueId The unique identifier.
|
---|
535 | * @param a_rReturn The return string value.
|
---|
536 | * @throws std::bad_alloc
|
---|
537 | */
|
---|
538 | HRESULT Certificate::i_getUniqueIdentifier(PCRTCRX509UNIQUEIDENTIFIER a_pUniqueId, com::Utf8Str &a_rReturn)
|
---|
539 | {
|
---|
540 | /* The a_pUniqueId may not be present! */
|
---|
541 | if (RTCrX509UniqueIdentifier_IsPresent(a_pUniqueId))
|
---|
542 | {
|
---|
543 | void const *pvData = RTASN1BITSTRING_GET_BIT0_PTR(a_pUniqueId);
|
---|
544 | size_t const cbData = RTASN1BITSTRING_GET_BYTE_SIZE(a_pUniqueId);
|
---|
545 | size_t const cbFormatted = cbData * 3 - 1 + 1;
|
---|
546 | a_rReturn.reserve(cbFormatted); /* throws */
|
---|
547 | int vrc = RTStrPrintHexBytes(a_rReturn.mutableRaw(), cbFormatted, pvData, cbData, RTSTRPRINTHEXBYTES_F_SEP_COLON);
|
---|
548 | a_rReturn.jolt();
|
---|
549 | AssertRCReturn(vrc, Global::vboxStatusCodeToCOM(vrc));
|
---|
550 | }
|
---|
551 | else
|
---|
552 | Assert(a_rReturn.isEmpty());
|
---|
553 | return S_OK;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Translates any ASN.1 object into a (DER encoded) byte array.
|
---|
558 | *
|
---|
559 | * @returns S_OK.
|
---|
560 | * @param a_pAsn1Obj The ASN.1 object to get the DER encoded bytes for.
|
---|
561 | * @param a_rReturn The return byte vector.
|
---|
562 | * @throws std::bad_alloc
|
---|
563 | */
|
---|
564 | HRESULT Certificate::i_getEncodedBytes(PRTASN1CORE a_pAsn1Obj, std::vector<BYTE> &a_rReturn)
|
---|
565 | {
|
---|
566 | HRESULT hrc = S_OK;
|
---|
567 | Assert(a_rReturn.size() == 0);
|
---|
568 | if (RTAsn1Core_IsPresent(a_pAsn1Obj))
|
---|
569 | {
|
---|
570 | uint32_t cbEncoded;
|
---|
571 | int vrc = RTAsn1EncodePrepare(a_pAsn1Obj, 0, &cbEncoded, NULL);
|
---|
572 | if (RT_SUCCESS(vrc))
|
---|
573 | {
|
---|
574 | a_rReturn.resize(cbEncoded);
|
---|
575 | Assert(a_rReturn.size() == cbEncoded);
|
---|
576 | if (cbEncoded)
|
---|
577 | {
|
---|
578 | vrc = RTAsn1EncodeToBuffer(a_pAsn1Obj, 0, &a_rReturn.front(), a_rReturn.size(), NULL);
|
---|
579 | if (RT_FAILURE(vrc))
|
---|
580 | hrc = setErrorVrc(vrc, tr("RTAsn1EncodeToBuffer failed with %Rrc"), vrc);
|
---|
581 | }
|
---|
582 | }
|
---|
583 | else
|
---|
584 | hrc = setErrorVrc(vrc, tr("RTAsn1EncodePrepare failed with %Rrc"), vrc);
|
---|
585 | }
|
---|
586 | return hrc;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /** @} */
|
---|
590 |
|
---|