D
D
Dmitry Kamaev2015-09-03 19:21:58
C++ / C#
Dmitry Kamaev, 2015-09-03 19:21:58

How to add CA certificate to Trusted Publisher using WinAPI?

I'm trying to add the CA certificate in the store (current user) to trusted.

#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <windows.h>
#include <Wincrypt.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if ( argc == 1 ) {
        qDebug() << "Enter path to certificate";
        return 1;
    } else if ( argc > 2 ) {
        qDebug() << "Too much parameters";
        return 1;
    }
    QString fileName = argv[1];
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) return 1;
    QByteArray blob = file.readAll();
    HCERTSTORE systemStore = CertOpenStore(
                    CERT_STORE_PROV_SYSTEM,
                    0,
                    0,
                    CERT_SYSTEM_STORE_CURRENT_USER,
                    L"Trusted Publisher");
    qDebug() << "Store pointer";
    qDebug() << systemStore;
    unsigned char* blob2 = reinterpret_cast<unsigned char*>( blob.data( ) );
    PCCERT_CONTEXT context = CertCreateCertificateContext( ( X509_ASN_ENCODING | PKCS_7_ASN_ENCODING ), blob2, blob.size() );
    qDebug() << "New context";
    qDebug() << &context;

    PCCERT_CONTEXT newContext;
    bool ok = CertAddCertificateContextToStore( systemStore, context, CERT_STORE_ADD_REPLACE_EXISTING, NULL );
    if (!ok)
    {
        qDebug() << "Could not add certificate to system store!";
        qDebug() << GetLastError();
    }
    CertFreeCertificateContext(context);
    return a.exec();
}

Throws SIGSEGV exception on execution
Program received signal SIGSEGV, Segmentation fault.
0x75154c84 in CertAddCertificateContextToStore ()
   from C:\Windows\SysWOW64\crypt32.dll
(gdb) bt
#0  0x75154c84 in CertAddCertificateContextToStore ()
   from C:\Windows\SysWOW64\crypt32.dll
#1  0x00401859 in main (argc=2, argv=0x6a1278) at main.cpp:56
(gdb)

Tell me how to fix it? I tried to use CertAddEncodedCertificateToStore, but I returned false, and GetLastError gave 0x0.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2015-09-04
@Nipheris

qDebug() << &context;

Useless operation. I mean, that's probably not what you want to achieve. context is 99% a pointer (look at the type name: PCCERT_CONTEXT, and you can always open the declaration of this type and see what's there). If it's a pointer (which is logical, because CertCreateCertificateContext creates some heavy object and returns you a pointer to it), then you need to print the pointer itself, and not the pointer-to-pointer as you did:
And then, perhaps, you will see, that it is NULL. And if it is NULL (which you should check), then it should not be passed as a parameter to CertAddCertificateContextToStore, because it probably expects a normal object there, and does not process NULL itself (and why should it do this). Most likely for this reason, a segfault occurs in this function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question