passhand.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // Created: 2-8-2005
  2. // Author(s): Scott Bridges
  3. #include "passhand.h"
  4. #include <time.h>
  5. #define KEY {0xe8, 0xa7, 0x7c, 0xe2, 0x05, 0x63, 0x6a, 0x31}
  6. #define IV {0xe4, 0xbb, 0x3b, 0xd3, 0xc3, 0x71, 0x2e, 0x58}
  7. void timeStamp(fstream* outFile)
  8. {
  9. if(outFile->is_open())
  10. {
  11. char dateBuf[32];
  12. char timeBuf[32];
  13. _strdate(dateBuf);
  14. _strtime(timeBuf);
  15. *outFile << dateBuf << " " << timeBuf << ": ";
  16. }
  17. }
  18. PasswordHandler::PasswordHandler()
  19. {
  20. outLog.open("./passhand.log", ios::out | ios::app);
  21. }
  22. PasswordHandler::~PasswordHandler()
  23. {
  24. outLog.close();
  25. }
  26. int PasswordHandler::SaveSet(char* filename)
  27. {
  28. int result = 0;
  29. fstream outFile;
  30. list<USER_PASS_PAIR>::iterator currentPair;
  31. strstream plainTextStream;
  32. char* cipherTextBuf;
  33. int usernameLen;
  34. int passwordLen;
  35. int plainTextLen;
  36. int cipherTextLen;
  37. int resultTextLen = 0;
  38. int pairCount = userPassPairs.size();
  39. if(outLog.is_open())
  40. {
  41. timeStamp(&outLog);
  42. outLog << "SaveSet: saving " << userPassPairs.size() << " entries to file" << endl;
  43. }
  44. // Write usernames and passwords to a strstream
  45. plainTextStream.write((char*)&pairCount, sizeof(pairCount));
  46. for(currentPair = userPassPairs.begin(); currentPair != userPassPairs.end(); currentPair++)
  47. {
  48. // Usernames
  49. usernameLen = strlen(currentPair->username) + 1;
  50. plainTextStream.write((char*)&usernameLen, sizeof(usernameLen));
  51. plainTextStream.write(currentPair->username, usernameLen);
  52. // Passwords
  53. passwordLen = strlen(currentPair->password) + 1;
  54. plainTextStream.write((char*)&passwordLen, sizeof(passwordLen));
  55. plainTextStream.write(currentPair->password, passwordLen);
  56. }
  57. plainTextLen = plainTextStream.tellp() - plainTextStream.tellg();
  58. // cipherTextBuf length must be at least plainTextLen + 8
  59. cipherTextLen = plainTextLen + 8;
  60. cipherTextBuf = (char*)malloc(cipherTextLen);
  61. if(encrypt(plainTextStream.str(), plainTextLen, cipherTextBuf, cipherTextLen, &resultTextLen) != 0)
  62. {
  63. result = -1;
  64. goto exit;
  65. }
  66. // Write cipher text to file
  67. outFile.open(filename, ios::out | ios::binary);
  68. if(!outFile.is_open())
  69. {
  70. result = -1;
  71. goto exit;
  72. }
  73. outFile.write(cipherTextBuf, resultTextLen);
  74. outFile.close();
  75. // ToDo: zero out memory
  76. userPassPairs.clear();
  77. exit:
  78. return result;
  79. }
  80. int PasswordHandler::LoadSet(char* filename)
  81. {
  82. int result = 0;
  83. int i;
  84. fstream inFile;
  85. USER_PASS_PAIR newPair;
  86. strstream* plainTextStream;
  87. char* cipherTextBuf;
  88. char* plainTextBuf;
  89. int usernameLen;
  90. int passwordLen;
  91. int plainTextLen;
  92. int cipherTextLen;
  93. int resultTextLen = 0;
  94. int pairCount;
  95. // Read in cipher text from file
  96. inFile.open(filename, ios::in | ios::binary);
  97. if(!inFile.is_open())
  98. {
  99. result = -1;
  100. goto exit;
  101. }
  102. // Determine file size
  103. inFile.seekg(0, ios::end);
  104. cipherTextLen = inFile.tellg();
  105. inFile.seekg(0, ios::beg);
  106. // plainTextLen length must be at least cipherTextLen
  107. plainTextLen = cipherTextLen;
  108. cipherTextBuf = (char*)malloc(cipherTextLen);
  109. plainTextBuf = (char*)malloc(plainTextLen);
  110. inFile.read(cipherTextBuf, cipherTextLen);
  111. inFile.close();
  112. if(decrypt(cipherTextBuf, cipherTextLen, plainTextBuf, plainTextLen, &resultTextLen) != 0)
  113. {
  114. result = -1;
  115. goto exit;
  116. }
  117. plainTextStream = new strstream(plainTextBuf, resultTextLen);
  118. plainTextStream->read((char*)&pairCount, sizeof(pairCount));
  119. // Read usernames and passwords from a strstream
  120. for(i = 0; i < pairCount; i++)
  121. {
  122. // Username
  123. plainTextStream->read((char*)&usernameLen, sizeof(usernameLen));
  124. newPair.username = (char*)malloc(usernameLen);
  125. plainTextStream->read((char*)newPair.username, usernameLen);
  126. // Password
  127. plainTextStream->read((char*)&passwordLen, sizeof(passwordLen));
  128. newPair.password = (char*)malloc(passwordLen);
  129. plainTextStream->read((char*)newPair.password, passwordLen);
  130. userPassPairs.push_back(newPair);
  131. }
  132. delete plainTextStream;
  133. if(outLog.is_open())
  134. {
  135. timeStamp(&outLog);
  136. outLog << "LoadSet: "<< userPassPairs.size() << " entries loaded from file" << endl;
  137. }
  138. exit:
  139. return result;
  140. }
  141. int PasswordHandler::PushUserPass(char* username, char* password)
  142. {
  143. USER_PASS_PAIR newPair;
  144. newPair.username = (char*)malloc(strlen(username) + 1);
  145. strcpy(newPair.username, username);
  146. newPair.password = (char*)malloc(strlen(password) + 1);
  147. strcpy(newPair.password, password);
  148. userPassPairs.push_back(newPair);
  149. if(outLog.is_open())
  150. {
  151. timeStamp(&outLog);
  152. outLog << "PushUserPass: pushed user password pair, new length " << userPassPairs.size() << endl;
  153. }
  154. return 0;
  155. }
  156. int PasswordHandler::PeekUserPass(char* username, char* password)
  157. {
  158. int result = 0;
  159. list<USER_PASS_PAIR>::iterator currentPair;
  160. if(userPassPairs.size() < 1)
  161. {
  162. result = -1;
  163. goto exit;
  164. }
  165. currentPair = userPassPairs.begin();
  166. strcpy(username, currentPair->username);
  167. strcpy(password, currentPair->password);
  168. if(outLog.is_open())
  169. {
  170. timeStamp(&outLog);
  171. outLog << "PeekUserPass: current length " << userPassPairs.size() << endl;
  172. }
  173. exit:
  174. return result;
  175. }
  176. int PasswordHandler::PopUserPass()
  177. {
  178. // ToDo: zero out memory.
  179. userPassPairs.pop_front();
  180. if(outLog.is_open())
  181. {
  182. timeStamp(&outLog);
  183. outLog << "PopUserPass: popped user password pair, new length " << userPassPairs.size() << endl;
  184. }
  185. return 0;
  186. }
  187. int PasswordHandler::encrypt(char* plainTextBuf, int plainTextLen, char* cipherTextBuf, int cipherTextLen, int* resultTextLen)
  188. {
  189. int result = 0;
  190. SECStatus rv1, rv2, rv3;
  191. PK11SlotInfo* slot = NULL;
  192. PK11SymKey* SymKey = NULL;
  193. SECItem* SecParam = NULL;
  194. PK11Context* EncContext = NULL;
  195. unsigned char gKey[] = KEY;
  196. unsigned char gIV[] = IV;
  197. PK11SymKey* key = NULL;
  198. SECItem keyItem;
  199. SECItem ivItem;
  200. CK_MECHANISM_TYPE cipherMech = CKM_DES_CBC_PAD;
  201. int offset;
  202. int tempTextLen;
  203. // Initialize NSS
  204. rv1 = NSS_NoDB_Init(".");
  205. if(rv1 != SECSuccess)
  206. {
  207. result = PR_GetError();
  208. goto exit;
  209. }
  210. // Get a key slot
  211. slot = PK11_GetInternalKeySlot();
  212. if(slot == NULL)
  213. {
  214. result = PR_GetError();
  215. goto exit;
  216. }
  217. // Generate a symmetric key
  218. keyItem.data = gKey;
  219. keyItem.len = sizeof(gKey);
  220. SymKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, NULL);
  221. if(SymKey == NULL)
  222. {
  223. result = PR_GetError();
  224. goto exit;
  225. }
  226. // Set up the PKCS11 encryption paramters
  227. ivItem.data = gIV;
  228. ivItem.len = sizeof(gIV);
  229. SecParam = PK11_ParamFromIV(cipherMech, &ivItem);
  230. if(SecParam == NULL)
  231. {
  232. if(SymKey != NULL)
  233. {
  234. PK11_FreeSymKey(SymKey);
  235. }
  236. result = PR_GetError();
  237. goto exit;
  238. }
  239. // ToDo: check parameters
  240. // Encrypt
  241. tempTextLen = 0;
  242. EncContext = PK11_CreateContextBySymKey(cipherMech, CKA_ENCRYPT, SymKey, SecParam);
  243. rv2 = PK11_CipherOp(EncContext, (unsigned char*)cipherTextBuf, &tempTextLen, cipherTextLen, (unsigned char*)plainTextBuf, plainTextLen);
  244. offset = tempTextLen;
  245. rv3 = PK11_DigestFinal(EncContext, (unsigned char*)cipherTextBuf + offset, (unsigned int*)&tempTextLen, cipherTextLen - offset);
  246. *resultTextLen = offset + tempTextLen;
  247. // Clean up
  248. PK11_DestroyContext(EncContext, PR_TRUE);
  249. PK11_FreeSymKey(SymKey);
  250. SECITEM_FreeItem(SecParam, PR_TRUE);
  251. if((rv2 != SECSuccess) || (rv2 != SECSuccess))
  252. {
  253. result = PR_GetError();
  254. goto exit;
  255. }
  256. exit:
  257. if(outLog.is_open())
  258. {
  259. if(result == 0)
  260. {
  261. timeStamp(&outLog);
  262. outLog << "encrypt: success" << endl;
  263. }
  264. else
  265. {
  266. timeStamp(&outLog);
  267. outLog << "encrypt: failure" << endl;
  268. }
  269. }
  270. return result;
  271. }
  272. int PasswordHandler::decrypt(char* cipherTextBuf, int cipherTextLen, char* plainTextBuf, int plainTextLen, int* resultTextLen)
  273. {
  274. int result = 0;
  275. SECStatus rv1, rv2, rv3;
  276. PK11SlotInfo* slot = NULL;
  277. PK11SymKey* SymKey = NULL;
  278. SECItem* SecParam = NULL;
  279. PK11Context* EncContext = NULL;
  280. unsigned char gKey[] = KEY;
  281. unsigned char gIV[] = IV;
  282. PK11SymKey* key = NULL;
  283. SECItem keyItem;
  284. SECItem ivItem;
  285. CK_MECHANISM_TYPE cipherMech = CKM_DES_CBC_PAD;
  286. int offset;
  287. int tempTextLen;
  288. // Initialize NSS
  289. rv1 = NSS_NoDB_Init(".");
  290. if(rv1 != SECSuccess)
  291. {
  292. result = PR_GetError();
  293. goto exit;
  294. }
  295. // Get a key slot
  296. slot = PK11_GetInternalKeySlot();
  297. if(slot == NULL)
  298. {
  299. result = PR_GetError();
  300. goto exit;
  301. }
  302. // Generate a symmetric key
  303. keyItem.data = gKey;
  304. keyItem.len = sizeof(gKey);
  305. SymKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, NULL);
  306. if(SymKey == NULL)
  307. {
  308. result = PR_GetError();
  309. goto exit;
  310. }
  311. // Set up the PKCS11 encryption paramters
  312. ivItem.data = gIV;
  313. ivItem.len = sizeof(gIV);
  314. SecParam = PK11_ParamFromIV(cipherMech, &ivItem);
  315. if(SecParam == NULL)
  316. {
  317. if(SymKey != NULL)
  318. {
  319. PK11_FreeSymKey(SymKey);
  320. }
  321. result = PR_GetError();
  322. goto exit;
  323. }
  324. // ToDo: check parameters
  325. // Decrypt
  326. tempTextLen = 0;
  327. EncContext = PK11_CreateContextBySymKey(cipherMech, CKA_DECRYPT, SymKey, SecParam);
  328. rv2 = PK11_CipherOp(EncContext, (unsigned char*)plainTextBuf, &tempTextLen, plainTextLen, (unsigned char*)cipherTextBuf, cipherTextLen);
  329. offset = tempTextLen;
  330. rv3 = PK11_DigestFinal(EncContext, (unsigned char*)plainTextBuf + offset, (unsigned int*)&tempTextLen, plainTextLen - offset);
  331. *resultTextLen = offset + tempTextLen;
  332. // Clean up
  333. PK11_DestroyContext(EncContext, PR_TRUE);
  334. PK11_FreeSymKey(SymKey);
  335. SECITEM_FreeItem(SecParam, PR_TRUE);
  336. if((rv2 != SECSuccess) || (rv2 != SECSuccess))
  337. {
  338. result = PR_GetError();
  339. goto exit;
  340. }
  341. exit:
  342. if(outLog.is_open())
  343. {
  344. if(result == 0)
  345. {
  346. timeStamp(&outLog);
  347. outLog << "decrypt: success" << endl;
  348. }
  349. else
  350. {
  351. timeStamp(&outLog);
  352. outLog << "decrypt: failure" << endl;
  353. }
  354. }
  355. return result;
  356. }