Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1055 lines
22 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLCRYPT_INL__
  11. #define __ATLCRYPT_INL__
  12. #pragma once
  13. #ifndef __ATLCRYPT_H__
  14. #error atlcrypt.inl requires atlcrypt.h to be included first
  15. #endif
  16. //REVIEW : All LPSTRs should probably be LPTSTRs in places where they are being cast to BYTE*s anyway.
  17. namespace ATL
  18. {
  19. inline CCryptProv::CCryptProv( const CCryptProv& prov ) throw()
  20. {
  21. m_hProv = prov.m_hProv;
  22. if (m_hProv)
  23. AddRef();
  24. }
  25. inline CCryptProv::CCryptProv( HCRYPTPROV hProv, BOOL bTakeOwnership ) throw()
  26. {
  27. m_hProv = hProv;
  28. if (m_hProv && !bTakeOwnership)
  29. AddRef();
  30. }
  31. inline CCryptProv::~CCryptProv() throw()
  32. {
  33. Release();
  34. }
  35. inline CCryptProv& CCryptProv::operator=( const CCryptProv& prov ) throw()
  36. {
  37. Release();
  38. m_hProv = prov.m_hProv;
  39. if( m_hProv != NULL )
  40. {
  41. AddRef();
  42. }
  43. return( *this );
  44. }
  45. inline HRESULT CCryptProv::AddRef() throw()
  46. {
  47. ATLASSERT( m_hProv != NULL );
  48. if (!CryptContextAddRef( m_hProv, NULL, 0))
  49. {
  50. return AtlHresultFromLastError();
  51. }
  52. return S_OK;
  53. }
  54. inline void CCryptProv::Attach( HCRYPTPROV hProv, BOOL bTakeOwnership ) throw()
  55. {
  56. ATLASSERT( m_hProv == NULL );
  57. m_hProv = hProv;
  58. if (m_hProv && !bTakeOwnership)
  59. AddRef();
  60. }
  61. inline HCRYPTPROV CCryptProv::Detach() throw()
  62. {
  63. HCRYPTPROV hProv;
  64. hProv = m_hProv;
  65. m_hProv = NULL;
  66. return( hProv );
  67. }
  68. inline CCryptProv::CCryptProv() throw() :
  69. m_hProv( NULL )
  70. {
  71. }
  72. inline HRESULT CCryptProv::Release() throw()
  73. {
  74. if( m_hProv != NULL )
  75. {
  76. if (!CryptReleaseContext( m_hProv, 0 ))
  77. {
  78. return AtlHresultFromLastError();
  79. }
  80. m_hProv = NULL;
  81. }
  82. return S_OK;
  83. }
  84. inline HRESULT CCryptProv::Initialize(
  85. DWORD dwProviderType,
  86. LPCTSTR szContainer,
  87. LPCTSTR szProvider,
  88. DWORD dwFlags) throw()
  89. {
  90. ATLASSERT(m_hProv == NULL);
  91. if (!CryptAcquireContext(&m_hProv, szContainer, szProvider, dwProviderType, dwFlags))
  92. {
  93. return AtlHresultFromLastError();
  94. }
  95. else return S_OK;
  96. }
  97. inline HRESULT CCryptProv::InitVerifyContext(
  98. DWORD dwProviderType,
  99. LPCTSTR szProvider,
  100. DWORD dwFlags) throw()
  101. {
  102. ATLASSERT(m_hProv == NULL);
  103. if (!CryptAcquireContext(&m_hProv, NULL, szProvider, dwProviderType, CRYPT_VERIFYCONTEXT | dwFlags))
  104. {
  105. return AtlHresultFromLastError();
  106. }
  107. else return S_OK;
  108. }
  109. inline HRESULT CCryptProv::InitCreateKeySet(
  110. DWORD dwProviderType,
  111. LPCTSTR szContainer,
  112. LPCTSTR szProvider,
  113. DWORD dwFlags) throw()
  114. {
  115. ATLASSERT(m_hProv == NULL);
  116. if (!CryptAcquireContext(&m_hProv, szContainer, szProvider, dwProviderType, CRYPT_NEWKEYSET | dwFlags))
  117. {
  118. return AtlHresultFromLastError();
  119. }
  120. else return S_OK;
  121. }
  122. inline HRESULT CCryptProv::DeleteKeySet(
  123. DWORD dwProviderType,
  124. LPCTSTR szContainer,
  125. LPCTSTR szProvider,
  126. DWORD dwFlags) throw()
  127. {
  128. HCRYPTPROV hProv = NULL;
  129. if (!CryptAcquireContext(&hProv, szContainer, szProvider, dwProviderType, CRYPT_DELETEKEYSET | dwFlags))
  130. {
  131. return AtlHresultFromLastError();
  132. }
  133. else return S_OK;
  134. }
  135. inline HRESULT CCryptProv::Uninitialize() throw()
  136. {
  137. ATLASSERT(m_hProv != NULL);
  138. if (!CryptReleaseContext(m_hProv, 0))
  139. {
  140. return AtlHresultFromLastError();
  141. }
  142. else
  143. {
  144. m_hProv = NULL;
  145. return S_OK;
  146. }
  147. }
  148. inline HRESULT CCryptProv::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
  149. {
  150. ATLASSERT(m_hProv != NULL);
  151. if (!CryptGetProvParam(m_hProv, dwParam, pbData, pdwDataLen, dwFlags))
  152. {
  153. return AtlHresultFromLastError();
  154. }
  155. else return S_OK;
  156. }
  157. inline HRESULT CCryptProv::SetParam( DWORD dwParam, BYTE* pbData, DWORD dwFlags) throw()
  158. {
  159. ATLASSERT(m_hProv != NULL);
  160. if (!CryptSetProvParam(m_hProv, dwParam, pbData, dwFlags ))
  161. {
  162. return AtlHresultFromLastError();
  163. }
  164. else return S_OK;
  165. }
  166. inline HRESULT CCryptProv::GetName(LPSTR szBuf, DWORD * pdwLength) throw()
  167. {
  168. return GetParam(PP_NAME, (BYTE *)szBuf, pdwLength);
  169. }
  170. inline HRESULT CCryptProv::GetContainer(LPSTR szBuf, DWORD * pdwLength) throw()
  171. {
  172. return GetParam(PP_CONTAINER, (BYTE *)szBuf, pdwLength);
  173. }
  174. inline HRESULT CCryptProv::GetImpType(DWORD * pdwImpType) throw()
  175. {
  176. DWORD dwLength = sizeof(DWORD);
  177. return GetParam(PP_IMPTYPE, (BYTE *)pdwImpType, &dwLength);
  178. }
  179. inline HRESULT CCryptProv::GetVersion(DWORD * pdwVersion) throw()
  180. {
  181. DWORD dwLength = sizeof(DWORD);
  182. return GetParam(PP_VERSION, (BYTE *)pdwVersion, &dwLength);
  183. }
  184. inline HRESULT CCryptProv::GetProvType(DWORD * pdwType) throw()
  185. {
  186. DWORD dwLength = sizeof(DWORD);
  187. return GetParam(PP_PROVTYPE, (BYTE * )pdwType, &dwLength);
  188. }
  189. inline HRESULT CCryptProv::GetSecurityDesc(SECURITY_INFORMATION * pSecInfo) throw()
  190. {
  191. DWORD dwSize = sizeof(SECURITY_INFORMATION);
  192. return GetParam(PP_KEYSET_SEC_DESCR, (BYTE *)pSecInfo, &dwSize);
  193. }
  194. inline HRESULT CCryptProv::SetSecurityDesc(SECURITY_INFORMATION SecInfo) throw()
  195. {
  196. return SetParam(PP_KEYSET_SEC_DESCR, (BYTE *)&SecInfo);
  197. }
  198. inline HRESULT CCryptProv::GenRandom(ULONG nLength, BYTE* pbBuffer ) throw()
  199. {
  200. ATLASSERT(m_hProv != NULL);
  201. if (!CryptGenRandom( m_hProv, nLength, pbBuffer ))
  202. {
  203. return AtlHresultFromLastError();
  204. }
  205. return S_OK;
  206. }
  207. inline CCryptHash::CCryptHash() throw() :
  208. m_hHash( NULL )
  209. {
  210. }
  211. inline CCryptHash::CCryptHash( const CCryptHash& hash ) throw()
  212. {
  213. m_hHash = hash.Duplicate();
  214. }
  215. inline CCryptHash::CCryptHash( HCRYPTHASH hHash, BOOL bTakeOwnership ) throw()
  216. {
  217. if (bTakeOwnership)
  218. m_hHash = hHash;
  219. else
  220. {
  221. m_hHash = NULL;
  222. BOOL bRet = ::CryptDuplicateHash( hHash, NULL, 0, &m_hHash );
  223. if (!bRet)
  224. m_hHash = NULL;
  225. }
  226. }
  227. inline CCryptHash::~CCryptHash() throw()
  228. {
  229. Destroy();
  230. }
  231. inline void CCryptHash::Attach( HCRYPTHASH hHash, BOOL bTakeOwnership ) throw()
  232. {
  233. ATLASSERT( m_hHash == NULL );
  234. if (bTakeOwnership)
  235. m_hHash = hHash;
  236. else
  237. {
  238. m_hHash = NULL;
  239. BOOL bRet = ::CryptDuplicateHash( hHash, NULL, 0, &m_hHash );
  240. if (!bRet)
  241. m_hHash = NULL;
  242. }
  243. }
  244. inline void CCryptHash::Destroy() throw()
  245. {
  246. if( m_hHash != NULL )
  247. {
  248. BOOL bSuccess;
  249. bSuccess = ::CryptDestroyHash( m_hHash );
  250. ATLASSERT( bSuccess );
  251. m_hHash = NULL;
  252. }
  253. }
  254. inline HCRYPTHASH CCryptHash::Detach() throw()
  255. {
  256. HCRYPTHASH hHash;
  257. hHash = m_hHash;
  258. m_hHash = NULL;
  259. return hHash;
  260. }
  261. inline HCRYPTHASH CCryptHash::Duplicate() const throw()
  262. {
  263. BOOL bSuccess;
  264. HCRYPTHASH hHash;
  265. ATLASSERT( m_hHash != NULL );
  266. hHash = NULL;
  267. bSuccess = ::CryptDuplicateHash( m_hHash, NULL, 0, &hHash );
  268. if( !bSuccess )
  269. {
  270. return NULL;
  271. }
  272. return hHash;
  273. }
  274. inline HRESULT CCryptHash::Uninitialize() throw()
  275. {
  276. ATLASSERT(m_hHash != NULL);
  277. if (!CryptDestroyHash(m_hHash))
  278. {
  279. return AtlHresultFromLastError();
  280. }
  281. else
  282. {
  283. m_hHash = NULL;
  284. return S_OK;
  285. }
  286. }
  287. inline HRESULT CCryptHash::Detach(HCRYPTHASH * phHash) throw()
  288. {
  289. ATLASSERT(phHash);
  290. if (!phHash)
  291. return E_INVALIDARG;
  292. *phHash = m_hHash;
  293. m_hHash = NULL;
  294. return S_OK;
  295. }
  296. inline HRESULT CCryptHash::AddData(const BYTE * pbData, DWORD dwDataLen, DWORD dwFlags) throw()
  297. {
  298. ATLASSERT(m_hHash != NULL);
  299. if (!CryptHashData(m_hHash, pbData, dwDataLen, dwFlags))
  300. {
  301. return AtlHresultFromLastError();
  302. }
  303. else return S_OK;
  304. }
  305. inline HRESULT CCryptHash::AddString(LPCTSTR szData, DWORD dwFlags) throw()
  306. {
  307. return AddData((BYTE *)szData, (DWORD)_tcslen(szData) * sizeof(TCHAR), dwFlags);
  308. }
  309. inline HRESULT CCryptHash::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
  310. {
  311. ATLASSERT(m_hHash != NULL);
  312. if (!CryptGetHashParam(m_hHash, dwParam, pbData, pdwDataLen, dwFlags))
  313. {
  314. return AtlHresultFromLastError();
  315. }
  316. else return S_OK;
  317. }
  318. inline HRESULT CCryptHash::SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags) throw()
  319. {
  320. ATLASSERT(m_hHash != NULL);
  321. if (!CryptSetHashParam(m_hHash, dwParam, pbData, dwFlags))
  322. {
  323. return AtlHresultFromLastError();
  324. }
  325. else return S_OK;
  326. }
  327. inline HRESULT CCryptHash::GetAlgId(ALG_ID * pAlgId) throw()
  328. {
  329. DWORD dwSize = sizeof(ALG_ID);
  330. return GetParam(HP_ALGID, (BYTE *)pAlgId, &dwSize);
  331. }
  332. inline HRESULT CCryptHash::GetSize(DWORD * pdwSize) throw()
  333. {
  334. DWORD dwLength = sizeof(DWORD);
  335. return GetParam(HP_HASHSIZE, (BYTE *)pdwSize, &dwLength);
  336. }
  337. inline HRESULT CCryptHash::GetValue(BYTE * pBuf, DWORD * pdwSize) throw()
  338. {
  339. return GetParam(HP_HASHVAL, pBuf, pdwSize);
  340. }
  341. inline HRESULT CCryptHash::SetValue(BYTE * pBuf) throw()
  342. {
  343. return SetParam(HP_HASHVAL, pBuf);
  344. }
  345. inline HRESULT CCryptHash::Sign(
  346. BYTE * pbSignature,
  347. DWORD * pdwSigLen,
  348. DWORD dwFlags,
  349. DWORD dwKeySpec) throw()
  350. {
  351. ATLASSERT(m_hHash != NULL);
  352. if (!CryptSignHash(m_hHash, dwKeySpec, NULL, dwFlags, pbSignature, pdwSigLen))
  353. {
  354. return AtlHresultFromLastError();
  355. }
  356. else return S_OK;
  357. }
  358. inline HRESULT CCryptHash::VerifySignature(
  359. const BYTE * pbSignature,
  360. DWORD dwSigLen,
  361. CCryptKey &PubKey,
  362. DWORD dwFlags) throw()
  363. {
  364. ATLASSERT(m_hHash != NULL);
  365. if (!CryptVerifySignature(m_hHash, pbSignature, dwSigLen, PubKey.GetHandle(), NULL, dwFlags))
  366. {
  367. return AtlHresultFromLastError();
  368. }
  369. else return S_OK;
  370. }
  371. __declspec(selectany) CCryptHash CCryptHash::EmptyHash = CCryptHash();
  372. __declspec(selectany) CCryptKey CCryptKey::EmptyKey = CCryptKey();
  373. inline CCryptKey::CCryptKey() throw() :
  374. m_hKey( NULL )
  375. {
  376. }
  377. inline CCryptKey::CCryptKey( const CCryptKey& key ) throw()
  378. {
  379. m_hKey = key.Duplicate();
  380. }
  381. inline CCryptKey::CCryptKey( HCRYPTKEY hKey, BOOL bTakeOwnership ) throw()
  382. {
  383. if (bTakeOwnership)
  384. m_hKey = hKey;
  385. else
  386. {
  387. BOOL bSuccess = ::CryptDuplicateKey( hKey, NULL, 0, &m_hKey );
  388. if( !bSuccess )
  389. m_hKey = NULL;
  390. }
  391. }
  392. inline CCryptKey::~CCryptKey() throw()
  393. {
  394. Destroy();
  395. }
  396. inline void CCryptKey::Attach( HCRYPTKEY hKey, BOOL bTakeOwnership ) throw()
  397. {
  398. ATLASSERT( m_hKey == NULL );
  399. if (bTakeOwnership)
  400. m_hKey = hKey;
  401. else
  402. {
  403. BOOL bSuccess = ::CryptDuplicateKey( hKey, NULL, 0, &m_hKey );
  404. if( !bSuccess )
  405. m_hKey = NULL;
  406. }
  407. }
  408. inline void CCryptKey::Destroy() throw()
  409. {
  410. if( m_hKey != NULL )
  411. {
  412. BOOL bSuccess;
  413. bSuccess = ::CryptDestroyKey( m_hKey );
  414. ATLASSERT( bSuccess );
  415. m_hKey = NULL;
  416. }
  417. }
  418. inline HCRYPTKEY CCryptKey::Detach() throw()
  419. {
  420. HCRYPTKEY hKey;
  421. hKey = m_hKey;
  422. m_hKey = NULL;
  423. return( hKey );
  424. }
  425. inline HCRYPTKEY CCryptKey::Duplicate() const throw()
  426. {
  427. BOOL bSuccess;
  428. ATLASSERT( m_hKey != NULL );
  429. HCRYPTKEY hKey = NULL;
  430. bSuccess = ::CryptDuplicateKey( m_hKey, NULL, 0, &hKey );
  431. if( !bSuccess )
  432. return NULL;
  433. return hKey;
  434. }
  435. inline HRESULT CCryptKey::Uninitialize() throw()
  436. {
  437. ATLASSERT(m_hKey != NULL);
  438. if (!CryptDestroyKey(m_hKey))
  439. {
  440. return AtlHresultFromLastError();
  441. }
  442. else
  443. {
  444. m_hKey = NULL;
  445. return S_OK;
  446. }
  447. }
  448. inline HRESULT CCryptKey::Encrypt(
  449. BOOL final,
  450. BYTE * pbData,
  451. DWORD * pdwDataLen,
  452. DWORD dwBufLen,
  453. CCryptHash &Hash) throw()
  454. {
  455. ATLASSERT(m_hKey != NULL);
  456. if (!::CryptEncrypt(m_hKey, Hash.GetHandle(), final, 0, pbData, pdwDataLen, dwBufLen))
  457. {
  458. return AtlHresultFromLastError();
  459. }
  460. else return S_OK;
  461. }
  462. inline HRESULT CCryptKey::Decrypt(BOOL final, BYTE * pbData, DWORD * pdwDataLen, CCryptHash &Hash) throw()
  463. {
  464. ATLASSERT(m_hKey != NULL);
  465. if (!::CryptDecrypt(m_hKey, Hash.GetHandle(), final, 0, pbData, pdwDataLen))
  466. {
  467. return AtlHresultFromLastError();
  468. }
  469. else return S_OK;
  470. }
  471. inline HRESULT CCryptKey::Encrypt(
  472. const BYTE * pbPlainText,
  473. DWORD dwPlainTextLen,
  474. BYTE * pbCipherText,
  475. DWORD * pdwCipherTextLen,
  476. CCryptHash &Hash) throw()
  477. {
  478. ATLASSERT(m_hKey != NULL);
  479. if (*pdwCipherTextLen < dwPlainTextLen)
  480. return ERROR_MORE_DATA;
  481. memcpy(pbCipherText, pbPlainText, dwPlainTextLen);
  482. DWORD dwSize = dwPlainTextLen;
  483. if (!::CryptEncrypt(m_hKey, Hash.GetHandle(), TRUE, 0, pbCipherText, &dwSize, *pdwCipherTextLen))
  484. {
  485. return AtlHresultFromLastError();
  486. }
  487. *pdwCipherTextLen = dwSize;
  488. return S_OK;
  489. }
  490. inline HRESULT CCryptKey::Decrypt(
  491. const BYTE * pbCipherText,
  492. DWORD dwCipherTextLen,
  493. BYTE * pbPlainText,
  494. DWORD * pdwPlainTextLen,
  495. CCryptHash &Hash) throw()
  496. {
  497. ATLASSERT(m_hKey != NULL);
  498. if (*pdwPlainTextLen < dwCipherTextLen)
  499. return ERROR_MORE_DATA;
  500. memcpy(pbPlainText, pbCipherText, dwCipherTextLen);
  501. DWORD dwSize = dwCipherTextLen;
  502. if (!::CryptDecrypt(m_hKey, Hash.GetHandle(), TRUE, 0, pbPlainText, &dwSize))
  503. {
  504. return AtlHresultFromLastError();
  505. }
  506. *pdwPlainTextLen = dwSize;
  507. return S_OK;
  508. }
  509. inline HRESULT CCryptKey::EncryptString(
  510. LPCTSTR szPlainText,
  511. BYTE * pbCipherText,
  512. DWORD * pdwCipherTextLen,
  513. CCryptHash &Hash) throw()
  514. {
  515. DWORD dwSize = ((DWORD)_tcslen(szPlainText) + 1) * sizeof(TCHAR);
  516. return Encrypt((BYTE *)szPlainText, dwSize, pbCipherText, pdwCipherTextLen, Hash);
  517. }
  518. inline HRESULT CCryptKey::ExportSimpleBlob(
  519. CCryptKey &ExpKey,
  520. DWORD dwFlags,
  521. BYTE * pbData,
  522. DWORD * pdwDataLen) throw()
  523. {
  524. ATLASSERT(m_hKey != NULL);
  525. if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), SIMPLEBLOB, dwFlags, pbData, pdwDataLen))
  526. {
  527. return AtlHresultFromLastError();
  528. }
  529. else return S_OK;
  530. }
  531. inline HRESULT CCryptKey::ExportPublicKeyBlob(
  532. CCryptKey &ExpKey,
  533. DWORD dwFlags,
  534. BYTE * pbData,
  535. DWORD * pdwDataLen) throw()
  536. {
  537. ATLASSERT(m_hKey != NULL);
  538. if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), PUBLICKEYBLOB, dwFlags, pbData, pdwDataLen))
  539. {
  540. return AtlHresultFromLastError();
  541. }
  542. else return S_OK;
  543. }
  544. inline HRESULT CCryptKey::ExportPrivateKeyBlob(
  545. CCryptKey &ExpKey,
  546. DWORD dwFlags,
  547. BYTE * pbData,
  548. DWORD * pdwDataLen) throw()
  549. {
  550. ATLASSERT(m_hKey != NULL);
  551. if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), PRIVATEKEYBLOB, dwFlags, pbData, pdwDataLen))
  552. {
  553. return AtlHresultFromLastError();
  554. }
  555. else return S_OK;
  556. }
  557. inline HRESULT CCryptKey::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
  558. {
  559. ATLASSERT(m_hKey != NULL);
  560. if (!CryptGetKeyParam(m_hKey, dwParam, pbData, pdwDataLen, dwFlags))
  561. {
  562. return AtlHresultFromLastError();
  563. }
  564. else return S_OK;
  565. }
  566. inline HRESULT CCryptKey::SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags) throw()
  567. {
  568. ATLASSERT(m_hKey != NULL);
  569. if (!CryptSetKeyParam(m_hKey, dwParam, pbData, dwFlags))
  570. {
  571. return AtlHresultFromLastError();
  572. }
  573. else return S_OK;
  574. }
  575. inline HRESULT CCryptKey::GetAlgId(ALG_ID * pAlgId) throw()
  576. {
  577. DWORD dwSize = sizeof(DWORD);
  578. return GetParam(KP_ALGID, (BYTE *)pAlgId, &dwSize);
  579. }
  580. inline HRESULT CCryptKey::SetAlgId(ALG_ID AlgId, DWORD dwFlags) throw()
  581. {
  582. return SetParam(KP_ALGID, (BYTE *)&AlgId, dwFlags);
  583. }
  584. inline HRESULT CCryptKey::GetBlockLength(DWORD * pdwBlockLen) throw()
  585. {
  586. DWORD dwSize = sizeof(DWORD);
  587. return GetParam(KP_BLOCKLEN, (BYTE *)pdwBlockLen, &dwSize);
  588. }
  589. inline HRESULT CCryptKey::GetKeyLength(DWORD * pdwKeyLen) throw()
  590. {
  591. DWORD dwSize = sizeof(DWORD);
  592. return GetParam(KP_KEYLEN, (BYTE *)pdwKeyLen, &dwSize);
  593. }
  594. inline HRESULT CCryptKey::GetSalt(BYTE * pbSalt, DWORD * pdwLength) throw()
  595. {
  596. return GetParam(KP_SALT, pbSalt, pdwLength);
  597. }
  598. inline HRESULT CCryptKey::SetSalt(BYTE * pbSalt) throw()
  599. {
  600. return SetParam(KP_SALT, pbSalt);
  601. }
  602. inline HRESULT CCryptKey::SetSaltEx(_CRYPTOAPI_BLOB * pBlobSalt) throw()
  603. {
  604. return SetParam(KP_SALT_EX, (BYTE *)pBlobSalt);
  605. }
  606. inline HRESULT CCryptKey::GetPermissions(DWORD * pdwPerms) throw()
  607. {
  608. DWORD dwSize = sizeof(DWORD);
  609. return GetParam(KP_PERMISSIONS, (BYTE *)pdwPerms, &dwSize);
  610. }
  611. inline HRESULT CCryptKey::SetPermissions(DWORD dwPerms) throw()
  612. {
  613. return SetParam(KP_PERMISSIONS, (BYTE *)&dwPerms);
  614. }
  615. inline HRESULT CCryptKey::GetP(BYTE * pbP, DWORD * pdwLength) throw()
  616. {
  617. return GetParam(KP_P, (BYTE *)pbP, pdwLength);
  618. }
  619. inline HRESULT CCryptKey::SetP(_CRYPTOAPI_BLOB * pBlobP) throw()
  620. {
  621. return SetParam(KP_P, (BYTE *)pBlobP);
  622. }
  623. inline HRESULT CCryptKey::SetP(BYTE * pbP, DWORD dwLength) throw()
  624. {
  625. _CRYPTOAPI_BLOB blob = { dwLength, pbP };
  626. return SetParam(KP_P, (BYTE *)&blob);
  627. }
  628. inline HRESULT CCryptKey::GetQ(BYTE * pbQ, DWORD * pdwLength) throw()
  629. {
  630. return GetParam(KP_Q, (BYTE *)pbQ, pdwLength);
  631. }
  632. inline HRESULT CCryptKey::SetQ(_CRYPTOAPI_BLOB * pBlobQ) throw()
  633. {
  634. return SetParam(KP_Q, (BYTE *)pBlobQ);
  635. }
  636. inline HRESULT CCryptKey::SetQ(BYTE * pbQ, DWORD dwLength) throw()
  637. {
  638. _CRYPTOAPI_BLOB blob = { dwLength, pbQ };
  639. return SetParam(KP_Q, (BYTE *)&blob);
  640. }
  641. inline HRESULT CCryptKey::GetG(BYTE * pbG, DWORD * pdwLength) throw()
  642. {
  643. return GetParam(KP_G, (BYTE *)pbG, pdwLength);
  644. }
  645. inline HRESULT CCryptKey::SetG(_CRYPTOAPI_BLOB * pBlobG) throw()
  646. {
  647. return SetParam(KP_G, (BYTE *)pBlobG);
  648. }
  649. inline HRESULT CCryptKey::SetG(BYTE * pbG, DWORD dwLength) throw()
  650. {
  651. _CRYPTOAPI_BLOB blob = { dwLength, pbG };
  652. return SetParam(KP_G, (BYTE *)&blob);
  653. }
  654. inline HRESULT CCryptKey::SetX() throw()
  655. {
  656. return SetParam(KP_X, NULL);
  657. }
  658. inline HRESULT CCryptKey::GetEffKeyLen(DWORD * pdwEffKeyLen) throw()
  659. {
  660. DWORD dwSize = sizeof(DWORD);
  661. return GetParam(KP_EFFECTIVE_KEYLEN, (BYTE *)pdwEffKeyLen, &dwSize);
  662. }
  663. inline HRESULT CCryptKey::SetEffKeyLen(DWORD dwEffKeyLen) throw()
  664. {
  665. return SetParam(KP_EFFECTIVE_KEYLEN, (BYTE *)&dwEffKeyLen);
  666. }
  667. inline HRESULT CCryptKey::GetPadding(DWORD * pdwPadding) throw()
  668. {
  669. DWORD dwSize = sizeof(DWORD);
  670. return GetParam(KP_PADDING, (BYTE *)pdwPadding, &dwSize);
  671. }
  672. inline HRESULT CCryptKey::SetPadding(DWORD dwPadding) throw()
  673. {
  674. return SetParam(KP_PADDING, (BYTE *)&dwPadding);
  675. }
  676. inline HRESULT CCryptKey::GetIV(BYTE * pbIV, DWORD * pdwLength) throw()
  677. {
  678. return GetParam(KP_IV, pbIV, pdwLength);
  679. }
  680. inline HRESULT CCryptKey::SetIV(BYTE * pbIV) throw()
  681. {
  682. return SetParam(KP_IV, pbIV);
  683. }
  684. inline HRESULT CCryptKey::GetMode(DWORD * pdwMode) throw()
  685. {
  686. DWORD dwSize = sizeof(DWORD);
  687. return GetParam(KP_MODE, (BYTE *)pdwMode, &dwSize);
  688. }
  689. inline HRESULT CCryptKey::SetMode(DWORD dwMode) throw()
  690. {
  691. return SetParam(KP_MODE, (BYTE *)&dwMode);
  692. }
  693. inline HRESULT CCryptKey::GetModeBits(DWORD * pdwModeBits) throw()
  694. {
  695. DWORD dwSize = sizeof(DWORD);
  696. return GetParam(KP_MODE_BITS, (BYTE *)pdwModeBits, &dwSize);
  697. }
  698. inline HRESULT CCryptKey::SetModeBits(DWORD dwModeBits) throw()
  699. {
  700. return SetParam(KP_MODE_BITS, (BYTE *)&dwModeBits);
  701. }
  702. inline HRESULT CCryptDerivedKey::Initialize(
  703. CCryptProv &Prov,
  704. CCryptHash &Hash,
  705. ALG_ID algid,
  706. DWORD dwFlags) throw()
  707. {
  708. ATLASSERT(m_hKey == NULL);
  709. if (!CryptDeriveKey(Prov.GetHandle(), algid, Hash.GetHandle(), dwFlags, &m_hKey))
  710. {
  711. return AtlHresultFromLastError();
  712. }
  713. else return S_OK;
  714. }
  715. inline HRESULT CCryptRandomKey::Initialize(CCryptProv &Prov, ALG_ID algid, DWORD dwFlags) throw()
  716. {
  717. ATLASSERT(m_hKey == NULL);
  718. if (!CryptGenKey(Prov.GetHandle(), algid, dwFlags, &m_hKey))
  719. {
  720. return AtlHresultFromLastError();
  721. }
  722. else return S_OK;
  723. }
  724. inline HRESULT CCryptUserExKey::Initialize(CCryptProv &Prov) throw()
  725. {
  726. ATLASSERT(m_hKey == NULL);
  727. if (!CryptGetUserKey(Prov.GetHandle(), AT_KEYEXCHANGE, &m_hKey))
  728. {
  729. return AtlHresultFromLastError();
  730. }
  731. else return S_OK;
  732. }
  733. inline HRESULT CCryptUserExKey::Create(CCryptProv &Prov) throw()
  734. {
  735. ATLASSERT(m_hKey == NULL);
  736. if (!CryptGenKey(Prov.GetHandle(), AT_KEYEXCHANGE, 0, &m_hKey))
  737. {
  738. return AtlHresultFromLastError();
  739. }
  740. else return S_OK;
  741. }
  742. inline HRESULT CCryptUserSigKey::Initialize(CCryptProv &Prov) throw()
  743. {
  744. ATLASSERT(m_hKey == NULL);
  745. if (!CryptGetUserKey(Prov.GetHandle(), AT_SIGNATURE, &m_hKey))
  746. {
  747. return AtlHresultFromLastError();
  748. }
  749. else return S_OK;
  750. }
  751. inline HRESULT CCryptUserSigKey::Create(CCryptProv &Prov) throw()
  752. {
  753. ATLASSERT(m_hKey == NULL);
  754. if (!CryptGenKey(Prov.GetHandle(), AT_SIGNATURE, 0, &m_hKey))
  755. {
  756. return AtlHresultFromLastError();
  757. }
  758. else return S_OK;
  759. }
  760. inline HRESULT CCryptImportKey::Initialize(
  761. CCryptProv &Prov,
  762. BYTE * pbData,
  763. DWORD dwDataLen,
  764. CCryptKey &PubKey,
  765. DWORD dwFlags) throw()
  766. {
  767. ATLASSERT(m_hKey == NULL);
  768. if (!CryptImportKey(Prov.GetHandle(), pbData, dwDataLen, PubKey.GetHandle(), dwFlags, &m_hKey))
  769. {
  770. return AtlHresultFromLastError();
  771. }
  772. else return S_OK;
  773. }
  774. inline HRESULT CCryptKeyedHash::Initialize(
  775. CCryptProv &Prov,
  776. ALG_ID Algid,
  777. CCryptKey &Key,
  778. DWORD dwFlags) throw()
  779. {
  780. ATLASSERT(m_hHash == NULL);
  781. if (!CryptCreateHash(Prov.GetHandle(), Algid, Key.GetHandle(), dwFlags, &m_hHash))
  782. {
  783. return AtlHresultFromLastError();
  784. }
  785. else return S_OK;
  786. }
  787. inline HRESULT CCryptMD5Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
  788. {
  789. ATLASSERT(m_hHash == NULL);
  790. if (!CryptCreateHash(Prov.GetHandle(), CALG_MD5, 0, 0, &m_hHash))
  791. {
  792. return AtlHresultFromLastError();
  793. }
  794. if (szText!=NULL)
  795. return AddString(szText);
  796. else return S_OK;
  797. }
  798. inline HRESULT CCryptMD4Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
  799. {
  800. ATLASSERT(m_hHash == NULL);
  801. if (!CryptCreateHash(Prov.GetHandle(), CALG_MD4, 0, 0, &m_hHash))
  802. {
  803. return AtlHresultFromLastError();
  804. }
  805. if (szText!=NULL)
  806. return AddString(szText);
  807. else return S_OK;
  808. }
  809. inline HRESULT CCryptMD2Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
  810. {
  811. ATLASSERT(m_hHash == NULL);
  812. if (!CryptCreateHash(Prov.GetHandle(), CALG_MD2, 0, 0, &m_hHash))
  813. {
  814. return AtlHresultFromLastError();
  815. }
  816. if (szText!=NULL)
  817. return AddString(szText);
  818. else return S_OK;
  819. }
  820. inline HRESULT CCryptSHAHash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
  821. {
  822. ATLASSERT(m_hHash == NULL);
  823. if (!CryptCreateHash(Prov.GetHandle(), CALG_SHA, 0, 0, &m_hHash))
  824. {
  825. return AtlHresultFromLastError();
  826. }
  827. if (szText!=NULL)
  828. return AddString(szText);
  829. else return S_OK;
  830. }
  831. inline HRESULT CCryptHMACHash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
  832. {
  833. ATLASSERT(m_hHash == NULL);
  834. if (!CryptCreateHash(Prov.GetHandle(), CALG_HMAC, Key.GetHandle(), 0, &m_hHash))
  835. {
  836. return AtlHresultFromLastError();
  837. }
  838. if (szText!=NULL)
  839. return AddString(szText);
  840. else return S_OK;
  841. }
  842. inline HRESULT CCryptMACHash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
  843. {
  844. ATLASSERT(m_hHash == NULL);
  845. if (!CryptCreateHash(Prov.GetHandle(), CALG_MAC, Key.GetHandle(), 0, &m_hHash))
  846. {
  847. return AtlHresultFromLastError();
  848. }
  849. if (szText!=NULL)
  850. return AddString(szText);
  851. else return S_OK;
  852. }
  853. inline HRESULT CCryptSSL3SHAMD4Hash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
  854. {
  855. ATLASSERT(m_hHash == NULL);
  856. if (!CryptCreateHash(Prov.GetHandle(), CALG_SSL3_SHAMD5, Key.GetHandle(), 0, &m_hHash))
  857. {
  858. return AtlHresultFromLastError();
  859. }
  860. if (szText!=NULL)
  861. return AddString(szText);
  862. else return S_OK;
  863. }
  864. }; // namespace ATL
  865. #endif //__ATLCRYPT_INL__