⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cert-installer.c

📁 非常不错的CSP源码学习
💻 C
📖 第 1 页 / 共 3 页
字号:
    heapHandle = HeapCreate(0, 600*sizeof(CERTIFICATE_INFO),0);    wc.style = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc = MainWndProc;    wc.hInstance = hInstance;    wc.lpszClassName = "csp11CertInstaller";    wc.lpszMenuName = "MMenu";    wc.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );    wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( 100 ) );    wc.hCursor = LoadCursor( NULL, IDC_ARROW );    wc.cbClsExtra = wc.cbWndExtra = 0;    /** - Register the main window structure.*/    if(!RegisterClass(&wc))    {        return FALSE;    }        /** - Search for certificate on smart card.*/    printf("Certificates research...\n");    certificatesNumber = findCertificates();        /** - Create the main window.*/    hMainWnd = CreateWindowEx(0, "csp11CertInstaller",                             "CSP #11 certificate installer",                            WS_MINIMIZEBOX |                            WS_SIZEBOX |                            WS_CAPTION |                            WS_MAXIMIZEBOX |                            WS_POPUP |                            WS_SYSMENU,                            0, 0,                            640, 480,                            NULL, NULL, hInstance, NULL );    if( ! hMainWnd )    {        return FALSE;    }    if(certificatesNumber < 1)    {        MessageBox(hMainWnd,                    "Perhaps no Smart Card inserted ?",                    "No certificate found",                    MB_OK);        return FALSE;    }    hLnd = CreateWindowEx(0, "LISTBOX",                           NULL,                          LBS_STANDARD |                          WS_CHILD |                          WS_VISIBLE |                          LBS_SORT |                          WS_VSCROLL |                          WS_TABSTOP,                          10, 15,                          610, 200,                          hMainWnd, (HMENU)IDL_CERTLIST, hInstance, NULL );        for(i=0;i<certificatesNumber;i++)    {        /*printf("%d: '%s'\n",i,certificatesList[i].cName);*/        /** - Test if the certificate key can be a AT_KEYEXCHANGE.*/        keyUsage = certKeyCouldBe(certificatesList[i]);        if(keyUsage < 0)        {            return FALSE;        }        label = certificatesList[i].label;        /** - If key usage is equal to 1 or to 3, then only signature.*/        if((keyUsage == 1) ||(keyUsage == 3))        {            listLabel = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY,                                  strlen(label)+strlen(sigPrefix)+1);            if(listLabel == NULL)            {                SetLastError(NTE_NO_MEMORY);                exit(1);            }            strcpy(listLabel, sigPrefix);            strcat(listLabel, label);            certKeySpec = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY,                                    sizeof(CERTIFICATE_KEY_SPEC));            if(certKeySpec == NULL)            {                SetLastError(NTE_NO_MEMORY);                exit(1);            }            certKeySpec->index = i;            certKeySpec->dwKeySpec = AT_SIGNATURE;            j = SendMessage(hLnd, LB_ADDSTRING, 0, listLabel);            SendMessage(hLnd, LB_SETITEMDATA, j, (LPARAM) certKeySpec);         }        /** - If key usage more than 1, key exchange (too).*/        if (keyUsage >1)        {            listLabel = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY,                                  strlen(label)+strlen(keyXPrefix)+1);            if(listLabel == NULL)            {                SetLastError(NTE_NO_MEMORY);                exit(1);            }            strcpy(listLabel, keyXPrefix);            strcat(listLabel, label);            certKeySpec = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY,                                    sizeof(CERTIFICATE_KEY_SPEC));            if(certKeySpec == NULL)            {                SetLastError(NTE_NO_MEMORY);                exit(1);            }            certKeySpec->index = i;            certKeySpec->dwKeySpec = AT_KEYEXCHANGE;            j = SendMessage(hLnd, LB_ADDSTRING, 0, listLabel);            SendMessage(hLnd, LB_SETITEMDATA, j, (LPARAM) certKeySpec);        }    }        ShowWindow( hMainWnd, d );    UpdateWindow( hMainWnd );    /** - Launche the Windows main loop.*/    while( GetMessage( &msg, NULL, 0, 0 ) )    {        TranslateMessage( &msg );        DispatchMessage( &msg );    }    return FALSE; }   /** \brief Store a certificate in the personnal store. * *  \param index Index of the certificate in the founded certificates list. *  \param dwKeySpec The key specification (signature ou exchange). *   *  \todo test if the private key is present on the SC. *  \return TRUE if the certificate is stored, false if not. */BOOL storeCertificate(int index, DWORD dwKeySpec){    // Write the certificat cert into the Personnal Store :    PCCERT_CONTEXT pCertContext = NULL;    HCERTSTORE LocalStore = NULL;    HCRYPTPROV hProv = 0;    char cName[MAX_PATH];    wchar_t wName[MAX_PATH];    DWORD datalen;    CRYPT_KEY_PROV_INFO ckp;    CERTIFICATE_INFO certificateInfo; /* Info of the current certificate.*/    HCRYPTKEY hKey = 0; /* The cryptographic key handle.*/    /** - Local partial copy of the selected certificate information.*/    certificateInfo = certificatesList[index];    /** - Construct container name.*/    if(dwKeySpec == AT_SIGNATURE)    {        sprintf(cName, "%s-%x-SHA1-%s-0--", certificateInfo.tokenLabel,                        certificateInfo.keyId, certificateInfo.keyHash);    }    else    {        sprintf(cName, "%s-0---%x-SHA1-%s", certificateInfo.tokenLabel,                        certificateInfo.keyId, certificateInfo.keyHash);    }    printf("Storing '%s'...\n", cName);    /** - Open the personnal certificate store (Current-user / My).*/    if (!(LocalStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,0,               NULL,CERT_SYSTEM_STORE_CURRENT_USER,L"My")))    {           return FALSE;    }    /** - Acquire a certificate context with the container name to validate     *    It.*/    /*if(!CryptAcquireContext(&hProv, NULL,"CSP Eleven", PROV_RSA_FULL, 0))*/    if(!CryptAcquireContext(&hProv, cName,"CSP Eleven", 900,        CRYPT_SILENT))    {            return FALSE;    }    /** - Get the dwKeySpec key in order to test cName validity.*/    if(!CryptGetUserKey(hProv, dwKeySpec, &hKey))    {        printf("%x(%d)\n",GetLastError());        return FALSE;    }        /** - Destroy it.*/    if(!CryptDestroyKey(hKey))    {        return FALSE;    }        /** - Release cryptographic context.*/    if(!CryptReleaseContext(hProv,0))    {        return FALSE;    }    /** - Create the certificate context from the certificate to store.*/    pCertContext=(CERT_CONTEXT*)CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,      certificateInfo.derCert,certificateInfo.certLen);        /** - Convert the cName to WORD string.*/    if (mbstowcs(wName, cName, strlen(cName)+1) == -1)    {        return FALSE;    }     /** - Fill certificate key provider information structure.*/    ckp.pwszContainerName=wName;    ckp.pwszProvName=L"CSP Eleven";    /*ckp.dwProvType=PROV_RSA_FULL;*/    ckp.dwProvType=900;    ckp.dwFlags=0;    ckp.dwKeySpec = dwKeySpec;    // maintenant on remplit la keylist :    ckp.cProvParam = 0;    ckp.rgProvParam = NULL;    /** - Bind the certificate context to the key provider information.*/    if (!CertSetCertificateContextProperty(pCertContext,CERT_KEY_PROV_INFO_PROP_ID,0,&ckp))    {          return FALSE;    }    /** - Add the certificate context to the user personnal store.*/    if (!CertAddCertificateContextToStore(LocalStore,pCertContext, CERT_STORE_ADD_REPLACE_EXISTING, NULL))    {        return FALSE;    }    /** - Close the store.*/    CertCloseStore(LocalStore,CERT_CLOSE_STORE_FORCE_FLAG);    return TRUE;}LRESULT WINAPI MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    HWND hwndList;    int nItem; /* The n selected item.*/    int i; /*  The item data (index in the certs list).*/    static CERTIFICATE_KEY_SPEC *certKeySpec;        switch(msg)    {        case WM_DESTROY:            PostQuitMessage(0);            break;        case WM_COMMAND:            /**  - Clic on the 'Install' menu item.*/            if(wParam == 100)            {                if(!storeCertificate(certKeySpec->index, certKeySpec->dwKeySpec))                {                    if (GetLastError()!=CRYPT_E_EXISTS)                    {                        MessageBox(hWnd,                             "Cert-Installer was not able to store the selected certificate.",                            "Certificate not stored",MB_OK);                        return FALSE;                    }                    else                    {                        MessageBox(hWnd, "Certificate not stored",                            "Certificate already stored.", MB_OK);                        return FALSE;                    }                }                else                {                    MessageBox(hWnd, "Certificate successfully stored.",                            "Certificate stored", MB_OK);                    return TRUE;                }                break;            }            /**  - Clic on the 'Exit' menu item.*/            if(wParam == 101)            {                DestroyWindow(hWnd);                break;            }            /** - If the command is on the certlist list box.*/            if(LOWORD(wParam) == IDL_CERTLIST)            {                switch(HIWORD(wParam))                {                    /**  - If the selection changed.*/                    case LBN_SELCHANGE:                        /**   - Get the dialog handle.*/                        hwndList = GetDlgItem(hWnd, IDL_CERTLIST);                        nItem = SendMessage(hwndList, LB_GETCURSEL, 0, 0);                        certKeySpec = (CERTIFICATE_KEY_SPEC *) SendMessage(hwndList, LB_GETITEMDATA, nItem, 0);                        printf("Selected: %s\n",certificatesList[certKeySpec->index].label);                        printf("Key ID:%x\n",                        certificatesList[certKeySpec->index].keyId);                        break;                    default:                        return FALSE;                }                return TRUE;            }            break;            default:                return DefWindowProc(hWnd, msg, wParam, lParam);        return TRUE;    }    return FALSE;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -