passhand.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. * --- END COPYRIGHT BLOCK --- */
  5. // Created: 2-8-2005
  6. // Author(s): Scott Bridges
  7. #include "passhand.h"
  8. #include <time.h>
  9. #define KEY {0xe8, 0xa7, 0x7c, 0xe2, 0x05, 0x63, 0x6a, 0x31}
  10. #define IV {0xe4, 0xbb, 0x3b, 0xd3, 0xc3, 0x71, 0x2e, 0x58}
  11. void timeStamp(fstream* outFile)
  12. {
  13. if(outFile->is_open())
  14. {
  15. char dateBuf[32];
  16. char timeBuf[32];
  17. _strdate(dateBuf);
  18. _strtime(timeBuf);
  19. *outFile << dateBuf << " " << timeBuf << ": ";
  20. }
  21. }
  22. int saveSet(PASS_INFO_LIST* passInfoList, char* filename)
  23. {
  24. int result = 0;
  25. fstream outFile;
  26. PASS_INFO_LIST_ITERATOR currentPair;
  27. strstream plainTextStream;
  28. char* cipherTextBuf;
  29. int usernameLen;
  30. int passwordLen;
  31. int plainTextLen;
  32. int cipherTextLen;
  33. int resultTextLen = 0;
  34. int pairCount = passInfoList->size();
  35. // Write usernames and passwords to a strstream
  36. plainTextStream.write((char*)&pairCount, sizeof(pairCount));
  37. for(currentPair = passInfoList->begin(); currentPair != passInfoList->end(); currentPair++)
  38. {
  39. // Usernames
  40. usernameLen = strlen(currentPair->username) + 1;
  41. plainTextStream.write((char*)&usernameLen, sizeof(usernameLen));
  42. plainTextStream.write(currentPair->username, usernameLen);
  43. // Passwords
  44. passwordLen = strlen(currentPair->password) + 1;
  45. plainTextStream.write((char*)&passwordLen, sizeof(passwordLen));
  46. plainTextStream.write(currentPair->password, passwordLen);
  47. }
  48. plainTextLen = plainTextStream.tellp() - plainTextStream.tellg();
  49. // cipherTextBuf length must be at least plainTextLen + 8
  50. cipherTextLen = plainTextLen + 8;
  51. cipherTextBuf = (char*)malloc(cipherTextLen);
  52. if(encrypt(plainTextStream.str(), plainTextLen, cipherTextBuf, cipherTextLen, &resultTextLen) != 0)
  53. {
  54. result = -1;
  55. goto exit;
  56. }
  57. // Write cipher text to file
  58. outFile.open(filename, ios::out | ios::binary);
  59. if(!outFile.is_open())
  60. {
  61. result = -1;
  62. goto exit;
  63. }
  64. outFile.write(cipherTextBuf, resultTextLen);
  65. outFile.close();
  66. exit:
  67. return result;
  68. }
  69. int loadSet(PASS_INFO_LIST* passInfoList, char* filename)
  70. {
  71. int result = 0;
  72. int i;
  73. fstream inFile;
  74. PASS_INFO newPair;
  75. strstream* plainTextStream;
  76. char* cipherTextBuf;
  77. char* plainTextBuf;
  78. int usernameLen;
  79. int passwordLen;
  80. int plainTextLen;
  81. int cipherTextLen;
  82. int resultTextLen = 0;
  83. int pairCount;
  84. // Read in cipher text from file
  85. inFile.open(filename, ios::in | ios::binary);
  86. if(!inFile.is_open())
  87. {
  88. result = -1;
  89. goto exit;
  90. }
  91. // Determine file size
  92. inFile.seekg(0, ios::end);
  93. cipherTextLen = inFile.tellg();
  94. inFile.seekg(0, ios::beg);
  95. // plainTextLen length must be at least cipherTextLen
  96. plainTextLen = cipherTextLen;
  97. cipherTextBuf = (char*)malloc(cipherTextLen);
  98. plainTextBuf = (char*)malloc(plainTextLen);
  99. inFile.read(cipherTextBuf, cipherTextLen);
  100. inFile.close();
  101. if(decrypt(cipherTextBuf, cipherTextLen, plainTextBuf, plainTextLen, &resultTextLen) != 0)
  102. {
  103. result = -1;
  104. goto exit;
  105. }
  106. plainTextStream = new strstream(plainTextBuf, resultTextLen);
  107. plainTextStream->read((char*)&pairCount, sizeof(pairCount));
  108. // Read usernames and passwords from a strstream
  109. for(i = 0; i < pairCount; i++)
  110. {
  111. // Username
  112. plainTextStream->read((char*)&usernameLen, sizeof(usernameLen));
  113. newPair.username = (char*)malloc(usernameLen);
  114. plainTextStream->read((char*)newPair.username, usernameLen);
  115. // Password
  116. plainTextStream->read((char*)&passwordLen, sizeof(passwordLen));
  117. newPair.password = (char*)malloc(passwordLen);
  118. plainTextStream->read((char*)newPair.password, passwordLen);
  119. // Backoff
  120. newPair.backoffCount = 0;
  121. // Load time
  122. time(&newPair.atTime);
  123. passInfoList->push_back(newPair);
  124. }
  125. delete plainTextStream;
  126. exit:
  127. return result;
  128. }
  129. int clearSet(PASS_INFO_LIST* passInfoList)
  130. {
  131. // ToDo: zero out memory
  132. passInfoList->clear();
  133. return -1;
  134. }
  135. int encrypt(char* plainTextBuf, int plainTextLen, char* cipherTextBuf, int cipherTextLen, int* resultTextLen)
  136. {
  137. int result = 0;
  138. SECStatus rv1, rv2, rv3;
  139. PK11SlotInfo* slot = NULL;
  140. PK11SymKey* SymKey = NULL;
  141. SECItem* SecParam = NULL;
  142. PK11Context* EncContext = NULL;
  143. unsigned char gKey[] = KEY;
  144. unsigned char gIV[] = IV;
  145. PK11SymKey* key = NULL;
  146. SECItem keyItem;
  147. SECItem ivItem;
  148. CK_MECHANISM_TYPE cipherMech = CKM_DES_CBC_PAD;
  149. int offset;
  150. int tempTextLen;
  151. // Initialize NSS
  152. rv1 = NSS_NoDB_Init(".");
  153. if(rv1 != SECSuccess)
  154. {
  155. result = PR_GetError();
  156. goto exit;
  157. }
  158. // Get a key slot
  159. slot = PK11_GetInternalKeySlot();
  160. if(slot == NULL)
  161. {
  162. result = PR_GetError();
  163. goto exit;
  164. }
  165. // Generate a symmetric key
  166. keyItem.data = gKey;
  167. keyItem.len = sizeof(gKey);
  168. SymKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, NULL);
  169. if(SymKey == NULL)
  170. {
  171. result = PR_GetError();
  172. goto exit;
  173. }
  174. // Set up the PKCS11 encryption paramters
  175. ivItem.data = gIV;
  176. ivItem.len = sizeof(gIV);
  177. SecParam = PK11_ParamFromIV(cipherMech, &ivItem);
  178. if(SecParam == NULL)
  179. {
  180. if(SymKey != NULL)
  181. {
  182. PK11_FreeSymKey(SymKey);
  183. }
  184. result = PR_GetError();
  185. goto exit;
  186. }
  187. // ToDo: check parameters
  188. // Encrypt
  189. tempTextLen = 0;
  190. EncContext = PK11_CreateContextBySymKey(cipherMech, CKA_ENCRYPT, SymKey, SecParam);
  191. rv2 = PK11_CipherOp(EncContext, (unsigned char*)cipherTextBuf, &tempTextLen, cipherTextLen, (unsigned char*)plainTextBuf, plainTextLen);
  192. offset = tempTextLen;
  193. rv3 = PK11_DigestFinal(EncContext, (unsigned char*)cipherTextBuf + offset, (unsigned int*)&tempTextLen, cipherTextLen - offset);
  194. *resultTextLen = offset + tempTextLen;
  195. // Clean up
  196. PK11_DestroyContext(EncContext, PR_TRUE);
  197. PK11_FreeSymKey(SymKey);
  198. SECITEM_FreeItem(SecParam, PR_TRUE);
  199. if((rv2 != SECSuccess) || (rv2 != SECSuccess))
  200. {
  201. result = PR_GetError();
  202. goto exit;
  203. }
  204. exit:
  205. return result;
  206. }
  207. int decrypt(char* cipherTextBuf, int cipherTextLen, char* plainTextBuf, int plainTextLen, int* resultTextLen)
  208. {
  209. int result = 0;
  210. SECStatus rv1, rv2, rv3;
  211. PK11SlotInfo* slot = NULL;
  212. PK11SymKey* SymKey = NULL;
  213. SECItem* SecParam = NULL;
  214. PK11Context* EncContext = NULL;
  215. unsigned char gKey[] = KEY;
  216. unsigned char gIV[] = IV;
  217. PK11SymKey* key = NULL;
  218. SECItem keyItem;
  219. SECItem ivItem;
  220. CK_MECHANISM_TYPE cipherMech = CKM_DES_CBC_PAD;
  221. int offset;
  222. int tempTextLen;
  223. // Initialize NSS
  224. rv1 = NSS_NoDB_Init(".");
  225. if(rv1 != SECSuccess)
  226. {
  227. result = PR_GetError();
  228. goto exit;
  229. }
  230. // Get a key slot
  231. slot = PK11_GetInternalKeySlot();
  232. if(slot == NULL)
  233. {
  234. result = PR_GetError();
  235. goto exit;
  236. }
  237. // Generate a symmetric key
  238. keyItem.data = gKey;
  239. keyItem.len = sizeof(gKey);
  240. SymKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, NULL);
  241. if(SymKey == NULL)
  242. {
  243. result = PR_GetError();
  244. goto exit;
  245. }
  246. // Set up the PKCS11 encryption paramters
  247. ivItem.data = gIV;
  248. ivItem.len = sizeof(gIV);
  249. SecParam = PK11_ParamFromIV(cipherMech, &ivItem);
  250. if(SecParam == NULL)
  251. {
  252. if(SymKey != NULL)
  253. {
  254. PK11_FreeSymKey(SymKey);
  255. }
  256. result = PR_GetError();
  257. goto exit;
  258. }
  259. // ToDo: check parameters
  260. // Decrypt
  261. tempTextLen = 0;
  262. EncContext = PK11_CreateContextBySymKey(cipherMech, CKA_DECRYPT, SymKey, SecParam);
  263. rv2 = PK11_CipherOp(EncContext, (unsigned char*)plainTextBuf, &tempTextLen, plainTextLen, (unsigned char*)cipherTextBuf, cipherTextLen);
  264. offset = tempTextLen;
  265. rv3 = PK11_DigestFinal(EncContext, (unsigned char*)plainTextBuf + offset, (unsigned int*)&tempTextLen, plainTextLen - offset);
  266. *resultTextLen = offset + tempTextLen;
  267. // Clean up
  268. PK11_DestroyContext(EncContext, PR_TRUE);
  269. PK11_FreeSymKey(SymKey);
  270. SECITEM_FreeItem(SecParam, PR_TRUE);
  271. if((rv2 != SECSuccess) || (rv2 != SECSuccess))
  272. {
  273. result = PR_GetError();
  274. goto exit;
  275. }
  276. exit:
  277. return result;
  278. }