File indexing completed on 2024-02-25 04:52:36

0001 /*
0002  * Copyright (C) 2004,2005  Justin Karneges <justin@affinix.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017  * 02110-1301  USA
0018  *
0019  */
0020 
0021 // need to define this immediately
0022 #define _WIN32_WINNT 0x400
0023 
0024 #include "qca_systemstore.h"
0025 
0026 #include <windows.h>
0027 // do not remove this comment. it keeps windows.h on the top during clang-format phase
0028 #include <wincrypt.h>
0029 
0030 namespace QCA {
0031 
0032 bool qca_have_systemstore()
0033 {
0034     bool       ok = false;
0035     HCERTSTORE hSystemStore;
0036     hSystemStore = CertOpenSystemStoreA(0, "ROOT");
0037     if (hSystemStore)
0038         ok = true;
0039     CertCloseStore(hSystemStore, 0);
0040     return ok;
0041 }
0042 
0043 CertificateCollection qca_get_systemstore(const QString &provider)
0044 {
0045     CertificateCollection col;
0046     HCERTSTORE            hSystemStore;
0047     hSystemStore = CertOpenSystemStoreA(0, "ROOT");
0048     if (!hSystemStore)
0049         return col;
0050     PCCERT_CONTEXT pc = NULL;
0051     while (1) {
0052         pc = CertFindCertificateInStore(
0053             hSystemStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, pc);
0054         if (!pc)
0055             break;
0056         int        size = pc->cbCertEncoded;
0057         QByteArray der(size, 0);
0058         memcpy(der.data(), pc->pbCertEncoded, size);
0059 
0060         Certificate cert = Certificate::fromDER(der, 0, provider);
0061         if (!cert.isNull())
0062             col.addCertificate(cert);
0063     }
0064     CertCloseStore(hSystemStore, 0);
0065     return col;
0066 }
0067 
0068 }