Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.69 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "include\eTPkcs11.h"
  4. #include <windows.h>
  5. #include "base64.h"
  6. #include <wincrypt.h>
  7.  
  8. using namespace std;
  9.  
  10. #define PATH_PEM "C:\\Users\\akeela\\Desktop\\RNT\\Lab3\\coolkey.pem"
  11. #define PATH_CERT "C:\\Users\\akeela\\Desktop\\RNT\\Lab3\\coolcert.pfx"
  12. #define PASSWD "123123"
  13.  
  14. void init();
  15. void leave(const char*);
  16. static void GetPKBlob(CK_BYTE_PTR pk, int pkSize, CK_BYTE_PTR* subject, int* subjectSize);
  17. static void ImportPrivateKey(const char* fileName, const char* password);
  18. static void ImportFile(const char* fileName,const char* label, const char* password);
  19. static void ExportFile(const char* fileName,const char* password);
  20. static void DeleteData(const char* password);
  21. static CK_ULONG GetFirstSlotId();
  22. static bool CreatePKFromBlob(CK_SESSION_HANDLE hSession, CK_BYTE_PTR key, int keySize);
  23. bool getPrivateKey(CK_BYTE_PTR pkfile);
  24.  
  25. //Глобальные переменные
  26. CK_FUNCTION_LIST_PTR   pFunctionList=NULL;
  27. CK_C_GetFunctionList   pGFL    = 0;
  28. bool                   wasInit = false;
  29.  
  30. int main()
  31. {
  32.     init();
  33.     //char path[100];
  34.     //char pass[20];
  35.     ImportPrivateKey(PATH_PEM,PASSWD);
  36.     getchar();
  37.     leave(NULL);
  38.  
  39.     return 0;
  40. }
  41.  
  42. void init()
  43. {
  44.   // Загружаем dll
  45.   HINSTANCE hLib = LoadLibraryA("etpkcs11.DLL");
  46.   if (hLib == NULL)
  47.   {
  48.     leave ("Cannot load DLL.");
  49.   }
  50.  
  51.   // Ищем точку входа для C_GetFunctionList
  52.  
  53.   (FARPROC&)pGFL= GetProcAddress(hLib, "C_GetFunctionList");
  54.  
  55.   if (pGFL == NULL)
  56.   {
  57.     leave ("Cannot find GetFunctionList().");
  58.   }
  59.  
  60.     //Берем список функций
  61.   if (CKR_OK != pGFL(&pFunctionList))
  62.   {
  63.     leave ("Can't get function list. \n");
  64.   }
  65.  
  66.   // Инициализируем библиотеку PKCS#11
  67.   //
  68.   if (CKR_OK != pFunctionList->C_Initialize (0))
  69.   {
  70.     leave ("C_Initialize failed...\n");
  71.   }                
  72.   wasInit = true;      
  73. }
  74.  
  75. static void leave(const char * message)
  76. {
  77.   if (message) printf("%s ", message);
  78.  
  79.     if(wasInit)
  80.   {
  81.         // Закрываем библиотеку PKCS#11
  82.         if (CKR_OK != pFunctionList->C_Finalize(0))
  83.         {
  84.             printf ("C_Finalize failed...\n");
  85.         }
  86.  
  87.     wasInit = false;
  88.   }
  89.  
  90.     exit(message ? -1 : 0 );
  91. }
  92.  
  93.  
  94. /* Extracts the private key blob from pem */
  95. static void GetPKBlob(CK_BYTE_PTR pk        // pem файл
  96.                            , int pkSize                 // Размер файла
  97.                            , CK_BYTE_PTR* subject           // Указатель на раскодированный закр. ключ
  98.                            , int* subjectSize               // Размер закр. ключа
  99.                            ) {
  100.                                unsigned int i,size=0,count=0;
  101.                                unsigned char* current = pk;
  102.                                unsigned char* start;
  103.                                unsigned char* end;
  104.                                
  105.                                *subject = NULL;              
  106.                                *subjectSize = 0;
  107.  
  108.                                //находим начало base64 данных
  109.                                while (current[0] !='-') *current++;
  110.                                while (current[0] =='-') *current++;
  111.                                while (current[0] !='-') *current++;
  112.                                while (current[0] =='-') *current++;
  113.                                start = ++current;
  114.                                while (current[0] !='-')
  115.                                    {
  116.                                        if ((current[0] =='\n') || (current[0] =='-')) count++;
  117.                                        *current++;
  118.                                     }
  119.                                end = current;
  120.                                size = (int)(end-start)-count;
  121.                                //создаем новый массив, без мешуры
  122.                                
  123.                                BYTE *newbyte = new BYTE[size];
  124.                                
  125.  
  126.                                current=start;
  127.                                for (i=0;i<size;i++)
  128.                                {
  129.                                    if (current[0] =='\n') *current++;
  130.                                    newbyte[i]=(BYTE)current[0];
  131.                                    *current++;
  132.                                }
  133.                                        
  134.                                *subject = base64_decode((char*)newbyte,size, (unsigned int*)subjectSize);
  135.                                
  136. }
  137.  
  138. //чтение пем НОМЕР 2
  139. void readThatPEM(const char* fileName, CK_BYTE_PTR* pkfile, DWORD* pkfileSize)
  140. {
  141.     HANDLE hPEM;
  142.     hPEM = CreateFileA(fileName,
  143.                         GENERIC_READ,
  144.                         0,
  145.                         NULL,
  146.                         OPEN_EXISTING,
  147.                         0,
  148.                         NULL
  149.                         );
  150.     DWORD read;
  151.     *pkfileSize = GetFileSize(hPEM, NULL);
  152.     *pkfile = new CK_BYTE[*pkfileSize];
  153.     if(!(ReadFile(hPEM, *pkfile, *pkfileSize, &read, NULL)))
  154.         printf("I HATE WINAPI!!!11");
  155. }
  156.  
  157. bool getPrivateKey(CK_BYTE_PTR pkfile)
  158. {
  159.     unsigned char* key = pkfile;
  160.     if (key[0]!=0x30) return false; //неверный формат
  161.     BYTE *modulus, *pubE, *privE,*p1, *p2, *e1, *e2,*coeff; //компоненты закр. ключа
  162.     int modulusL, pubEL, privEL, p1L, p2L, e1L, e2L, coeffL; //длины компонентов
  163.     //напишите более красивый парсер, если сможете
  164.     int cur=0;
  165.     while ((key[cur]!=0x02)||((key[cur+1]!=0x81)&&(key[cur+1]!=0x82)))
  166.     {
  167.     cur++;
  168.     }
  169.     cur++;
  170.     if (key[cur] ==0x81) {modulusL = key[cur+1]; modulus=key+cur+2; if (key[cur+2]==0)
  171.     {modulusL--; modulus++; cur++;} cur = cur+2+modulusL;}
  172.     if (key[cur] ==0x82) {modulusL = key[cur+1]<<8|key[cur+2]; modulus=key+cur+3; if
  173.     (key[cur+3]==0) {modulusL--; modulus++; cur++;} cur = cur+3+modulusL;}
  174.     if (key[cur] !=0x02) return false; cur++;
  175.     pubEL = key[cur]; pubE=key+cur+1; cur = cur+1+pubEL;
  176.     if (key[cur] !=0x02) return false; cur++;
  177.     if (key[cur] ==0x81) {privEL = key[cur+1]; privE=key+cur+2; if (key[cur+2]==0) {privEL--; privE++; cur++;}
  178.     cur = cur+2+privEL;}
  179.     if (key[cur] ==0x82) {privEL = key[cur+1]<<8|key[cur+2]; privE=key+cur+3; if
  180.     (key[cur+3]==0) {privEL--; privE++; cur++;} cur = cur+3+privEL;}
  181.     if (key[cur] !=0x02) return false; cur++;
  182.     if ((key[cur] ==0x81)||(key[cur] ==0x80)) cur++;
  183.     p1L = key[cur]; p1=key+cur+1; if (key[cur+1]==0) {p1L--; p1++; cur++;} cur = cur+1+p1L;
  184.     if (key[cur] !=0x02) return false; cur++;
  185.     if ((key[cur] ==0x81)||(key[cur] ==0x80)) cur++;
  186.     p2L = key[cur]; p2=key+cur+1; if (key[cur+1]==0) {p2L--; p2++; cur++;} cur = cur+1+p2L;
  187.     if (key[cur] !=0x02) return false; cur++;
  188.     if ((key[cur] ==0x81)||(key[cur] ==0x80)) cur++;
  189.     e1L = key[cur]; e1=key+cur+1; if (key[cur+1]==0) {e1L--; e1++; cur++;} cur = cur+1+e1L;
  190.     if (key[cur] !=0x02) return false; cur++;
  191.     if ((key[cur] ==0x81)||(key[cur] ==0x80)) cur++;
  192.     e2L = key[cur]; e2=key+cur+1; if (key[cur+1]==0) {e2L--; e2++; cur++;} cur = cur+1+e2L;
  193.     if (key[cur] !=0x02) return false; cur++;
  194.     if ((key[cur] ==0x81)||(key[cur] ==0x80)) cur++;
  195.     coeffL = key[cur]; coeff=key+cur+1; if (key[cur+1]==0) {coeffL--; coeff++;}
  196. }
  197.  
  198. static void ImportPrivateKey(const char* fileName, const char* password)
  199. {
  200.     CK_BYTE_PTR     pkfile = NULL;
  201.     DWORD                   pkfileSize;
  202.     CK_BYTE_PTR     subject = NULL;
  203.     int                     subjSize;
  204.  
  205.     // Read the pem file
  206.     readThatPEM(fileName, &pkfile, &pkfileSize);
  207.     // decode base64 and extract the private key blob
  208.     GetPKBlob((unsigned char*)pkfile, pkfileSize, &subject, &subjSize);
  209.     getPrivateKey((unsigned char*)pkfile);
  210.     // Find connected token
  211.     CK_SESSION_HANDLE hSession;
  212.     CK_SLOT_ID slotId = GetFirstSlotId();
  213.     // login to token
  214.     pFunctionList->C_OpenSession(slotId, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL, NULL, &hSession);
  215.     pFunctionList->C_Login(hSession, CKU_USER, (LPBYTE)password, strlen(password));
  216.     // Import the private key to the token
  217.     CreatePKFromBlob(hSession, (unsigned char*)pkfile, pkfileSize);
  218.     //CreatePKFromBlob();
  219.     // Close session
  220.     pFunctionList->C_Logout(hSession);
  221.     pFunctionList->C_CloseSession(hSession);
  222.  
  223.     //чисти чисти чисти чисти
  224.     if (pkfile) {
  225.         delete[] pkfile;
  226.     }
  227.  
  228.  
  229. }
  230.  
  231. static void ImportFile(const char* fileName,const char* label, const char* password)
  232. {
  233.     CK_BYTE_PTR     file = NULL;
  234.     DWORD           fileSize;
  235.    
  236.     // Read the file
  237.    
  238.     // Find connected token
  239.  
  240.     // login to token
  241.  
  242.     // Import file to the token
  243.    
  244.     // Close session
  245.        
  246.     if (file) {
  247.         delete[] file;
  248.     }  
  249. }
  250.  
  251. static void ExportFile(const char* fileName,const char* password)
  252. {
  253.     CK_BYTE_PTR     file = NULL;
  254.     DWORD           fileSize;
  255.    
  256.    
  257.     // Find connected token
  258.    
  259.     // login to token
  260.    
  261.     // Read file from the token
  262.    
  263.     // Save the file
  264.    
  265.     // Close session
  266.  
  267.     if (file) {
  268.         delete[] file;
  269.     }  
  270. }
  271.  
  272. static void DeleteData(const char* password)
  273. {
  274.    
  275.     // Find connected token
  276.  
  277.     // login to token
  278.      
  279.     // read data and delete selected   
  280.    
  281.     // Close session
  282.  
  283. }
  284.  
  285. static CK_ULONG GetFirstSlotId()
  286. {
  287.     CK_ULONG slotId = -1;
  288.     CK_ULONG ulCount = 0;
  289.     CK_SLOT_ID_PTR pSlotId = NULL_PTR;
  290.     CK_ULONG i;
  291.  
  292.     if (pFunctionList->C_GetSlotList(TRUE, NULL_PTR, &ulCount) == CKR_OK)
  293.     {
  294.         if (ulCount > 0)
  295.         {
  296.             pSlotId = new CK_SLOT_ID[ulCount];
  297.             if ((pFunctionList->C_GetSlotList(TRUE, pSlotId, &ulCount)) == CKR_OK)
  298.             {
  299.                 for (i = 0; i < ulCount; i++)
  300.                 {
  301.                     CK_SLOT_INFO info;
  302.                     if ((pFunctionList->C_GetSlotInfo(pSlotId[i], &info)) == CKR_OK)
  303.                     {
  304.                         if (info.flags & (CKF_HW_SLOT | CKF_TOKEN_PRESENT))
  305.                         {
  306.                             slotId = pSlotId[i];
  307.                             break;
  308.                         }
  309.                     }
  310.                 }
  311.             }
  312.         }
  313.     }
  314.     if (pSlotId)
  315.     {
  316.         delete[] pSlotId;
  317.         pSlotId = NULL_PTR;
  318.     }
  319.     return slotId;
  320. }
  321.  
  322. static bool CreatePKFromBlob(CK_SESSION_HANDLE hSession, CK_BYTE_PTR key, int keySize)
  323. {
  324.     CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
  325.     CK_KEY_TYPE keyType = CKK_RSA;
  326.  
  327.     //далее нефыдоделал
  328.     //unsigned long certCategory = 2;
  329.     CK_BBOOL trueVal = CK_TRUE;
  330.     CK_OBJECT_HANDLE hKeyObject;
  331.     // ???????
  332.     CK_ATTRIBUTE templateArray[] = { {CKA_CLASS, &keyClass, sizeof(keyClass)},
  333.                                     {CKA_KEY_TYPE, &keyType, sizeof(keyType)},
  334.                                     {CKA_TOKEN, &trueVal, sizeof(trueVal)},
  335.                                     //{CKA_SUBJECT, subject, subjSize},
  336.                                     {CKA_VALUE, (void*)key, keySize},
  337.                                     //{CKA_CERTIFICATE_CATEGORY, (void*)&certCategory, sizeof(certCategory)},
  338.                                     };
  339.    
  340.     int sizeOfTemplate = sizeof(templateArray) / sizeof(CK_ATTRIBUTE);
  341.     CK_RV rv = pFunctionList->C_CreateObject(hSession, templateArray, sizeOfTemplate, &hKeyObject);
  342.     if (rv)
  343.     {
  344.         return false;
  345.     }
  346.     return true;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement