Leaked source code of windows server 2003
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.

1108 lines
32 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: ldapstor.cpp
  7. //
  8. // Contents: Ldap Store Provider implementation
  9. //
  10. // History: 17-Oct-97 kirtd Created
  11. // 01-Jan-02 philh Changed to internally use UNICODE Urls
  12. //
  13. //----------------------------------------------------------------------------
  14. #include <global.hxx>
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Member: CLdapStore::CLdapStore, public
  18. //
  19. // Synopsis: Constructor
  20. //
  21. //----------------------------------------------------------------------------
  22. CLdapStore::CLdapStore (
  23. OUT BOOL& rfResult
  24. )
  25. : m_pBinding( NULL ),
  26. m_hCacheStore( NULL ),
  27. m_dwOpenFlags( 0 ),
  28. m_fDirty( FALSE )
  29. {
  30. rfResult = TRUE;
  31. memset( &m_UrlComponents, 0, sizeof( m_UrlComponents ) );
  32. if (! Pki_InitializeCriticalSection( &m_StoreLock ))
  33. {
  34. rfResult = FALSE;
  35. }
  36. }
  37. //+---------------------------------------------------------------------------
  38. //
  39. // Member: CLdapStore::~CLdapStore, public
  40. //
  41. // Synopsis: Destructor
  42. //
  43. //----------------------------------------------------------------------------
  44. CLdapStore::~CLdapStore ()
  45. {
  46. DeleteCriticalSection( &m_StoreLock );
  47. }
  48. //+---------------------------------------------------------------------------
  49. //
  50. // Member: CLdapStore::OpenStore, public
  51. //
  52. // Synopsis: open store
  53. //
  54. //----------------------------------------------------------------------------
  55. BOOL
  56. CLdapStore::OpenStore (
  57. LPCSTR pszStoreProv,
  58. DWORD dwMsgAndCertEncodingType,
  59. HCRYPTPROV hCryptProv,
  60. DWORD dwFlags,
  61. const void* pvPara,
  62. HCERTSTORE hCertStore,
  63. PCERT_STORE_PROV_INFO pStoreProvInfo
  64. )
  65. {
  66. BOOL fResult = TRUE;
  67. DWORD dwRetrievalFlags;
  68. DWORD dwBindFlags;
  69. assert( m_pBinding == NULL );
  70. assert( m_hCacheStore == NULL );
  71. assert( m_dwOpenFlags == 0 );
  72. assert( m_fDirty == FALSE );
  73. m_dwOpenFlags = dwFlags;
  74. m_hCacheStore = hCertStore;
  75. if ( pvPara == NULL )
  76. {
  77. SetLastError( (DWORD) E_INVALIDARG );
  78. return( FALSE );
  79. }
  80. if ( (dwFlags & CERT_STORE_UNSAFE_PHYSICAL_FLAG) &&
  81. (dwFlags & CERT_LDAP_STORE_OPENED_FLAG) )
  82. {
  83. SetLastError( (DWORD) E_INVALIDARG );
  84. return( FALSE );
  85. }
  86. __try
  87. {
  88. LPCWSTR pwszUrl;
  89. if (dwFlags & CERT_LDAP_STORE_OPENED_FLAG)
  90. {
  91. PCERT_LDAP_STORE_OPENED_PARA pOpenedPara;
  92. // Will set this before returning. Don't want to do an
  93. // unbind if the open fails.
  94. m_dwOpenFlags &= ~CERT_LDAP_STORE_UNBIND_FLAG;
  95. pOpenedPara = (PCERT_LDAP_STORE_OPENED_PARA) pvPara;
  96. m_pBinding = (LDAP *) pOpenedPara->pvLdapSessionHandle;
  97. pwszUrl = pOpenedPara->pwszLdapUrl;
  98. }
  99. else
  100. {
  101. pwszUrl = (LPCWSTR) pvPara;
  102. m_dwOpenFlags |= CERT_LDAP_STORE_UNBIND_FLAG;
  103. }
  104. if ( LdapCrackUrl( pwszUrl, &m_UrlComponents ) == FALSE )
  105. {
  106. return( FALSE );
  107. }
  108. }
  109. __except(EXCEPTION_EXECUTE_HANDLER)
  110. {
  111. SetLastError( GetExceptionCode() );
  112. return( FALSE );
  113. }
  114. dwRetrievalFlags = 0;
  115. dwBindFlags = LDAP_BIND_AUTH_SSPI_ENABLE_FLAG |
  116. LDAP_BIND_AUTH_SIMPLE_ENABLE_FLAG;
  117. if (dwFlags & CERT_LDAP_STORE_SIGN_FLAG)
  118. {
  119. dwRetrievalFlags |= CRYPT_LDAP_SIGN_RETRIEVAL;
  120. dwBindFlags &= ~LDAP_BIND_AUTH_SIMPLE_ENABLE_FLAG;
  121. }
  122. if (dwFlags & CERT_LDAP_STORE_AREC_EXCLUSIVE_FLAG)
  123. {
  124. dwRetrievalFlags |= CRYPT_LDAP_AREC_EXCLUSIVE_RETRIEVAL;
  125. dwBindFlags &= ~LDAP_BIND_AUTH_SIMPLE_ENABLE_FLAG;
  126. }
  127. if (!( dwFlags & CERT_LDAP_STORE_OPENED_FLAG ) )
  128. {
  129. fResult = LdapGetBindings(
  130. m_UrlComponents.pwszHost,
  131. m_UrlComponents.Port,
  132. dwRetrievalFlags,
  133. dwBindFlags,
  134. LDAP_STORE_TIMEOUT,
  135. NULL,
  136. &m_pBinding
  137. );
  138. }
  139. if ( ( fResult == TRUE ) && !( dwFlags & CERT_STORE_READONLY_FLAG ) )
  140. {
  141. fResult = LdapHasWriteAccess( m_pBinding, &m_UrlComponents,
  142. LDAP_STORE_TIMEOUT );
  143. if ( fResult == FALSE )
  144. {
  145. SetLastError( (DWORD) ERROR_ACCESS_DENIED );
  146. }
  147. }
  148. if ( fResult == TRUE )
  149. {
  150. if ( m_dwOpenFlags & CERT_STORE_DELETE_FLAG )
  151. {
  152. m_fDirty = TRUE;
  153. fResult = InternalCommit( 0 );
  154. if ( fResult == TRUE )
  155. {
  156. pStoreProvInfo->dwStoreProvFlags = CERT_STORE_PROV_DELETED_FLAG;
  157. if (dwFlags & CERT_LDAP_STORE_UNBIND_FLAG)
  158. {
  159. m_dwOpenFlags |= CERT_LDAP_STORE_UNBIND_FLAG;
  160. }
  161. }
  162. CloseStore( 0 );
  163. return( fResult );
  164. }
  165. fResult = FillCacheStore( FALSE );
  166. // Note, LDAP_REFERRAL gets mapped to ERROR_MORE_DATA. Sometimes
  167. // referral errors aren't detected until doing the ldap search.
  168. if ( !fResult &&
  169. !(dwFlags & CERT_LDAP_STORE_OPENED_FLAG) &&
  170. ((LDAP_BIND_AUTH_SSPI_ENABLE_FLAG |
  171. LDAP_BIND_AUTH_SIMPLE_ENABLE_FLAG) == dwBindFlags)
  172. &&
  173. (ERROR_MORE_DATA == GetLastError()) )
  174. {
  175. // Try again doing simple bind.
  176. LdapFreeBindings( m_pBinding );
  177. m_pBinding = NULL;
  178. fResult = LdapGetBindings(
  179. m_UrlComponents.pwszHost,
  180. m_UrlComponents.Port,
  181. dwRetrievalFlags,
  182. LDAP_BIND_AUTH_SIMPLE_ENABLE_FLAG,
  183. LDAP_STORE_TIMEOUT,
  184. NULL,
  185. &m_pBinding
  186. );
  187. if ( fResult )
  188. {
  189. fResult = FillCacheStore( FALSE );
  190. }
  191. }
  192. }
  193. if ( fResult == TRUE )
  194. {
  195. pStoreProvInfo->cStoreProvFunc = LDAP_PROV_FUNC_COUNT;
  196. pStoreProvInfo->rgpvStoreProvFunc = (void **)rgpvLdapProvFunc;
  197. pStoreProvInfo->hStoreProv = (HCERTSTOREPROV)this;
  198. if (dwFlags & CERT_LDAP_STORE_UNBIND_FLAG)
  199. {
  200. m_dwOpenFlags |= CERT_LDAP_STORE_UNBIND_FLAG;
  201. }
  202. }
  203. else
  204. {
  205. CloseStore( 0 );
  206. }
  207. return( fResult );
  208. }
  209. //+---------------------------------------------------------------------------
  210. //
  211. // Member: CLdapStore::CloseStore, public
  212. //
  213. // Synopsis: close the store
  214. //
  215. //----------------------------------------------------------------------------
  216. VOID
  217. CLdapStore::CloseStore (DWORD dwFlags)
  218. {
  219. DWORD LastError = GetLastError();
  220. EnterCriticalSection( &m_StoreLock );
  221. InternalCommit( 0 );
  222. LdapFreeUrlComponents( &m_UrlComponents );
  223. memset( &m_UrlComponents, 0, sizeof( m_UrlComponents ) );
  224. if (m_dwOpenFlags & CERT_LDAP_STORE_UNBIND_FLAG)
  225. {
  226. LdapFreeBindings( m_pBinding );
  227. }
  228. m_pBinding = NULL;
  229. LeaveCriticalSection( &m_StoreLock );
  230. SetLastError( LastError );
  231. }
  232. //+---------------------------------------------------------------------------
  233. //
  234. // Member: CLdapStore::DeleteCert, public
  235. //
  236. // Synopsis: delete cert from store
  237. //
  238. //----------------------------------------------------------------------------
  239. BOOL
  240. CLdapStore::DeleteCert (PCCERT_CONTEXT pCertContext, DWORD dwFlags)
  241. {
  242. return( WriteCheckSetDirtyWithLock(
  243. CONTEXT_OID_CERTIFICATE,
  244. (LPVOID)pCertContext,
  245. 0
  246. ) );
  247. }
  248. //+---------------------------------------------------------------------------
  249. //
  250. // Member: CLdapStore::DeleteCrl, public
  251. //
  252. // Synopsis: delete CRL from store
  253. //
  254. //----------------------------------------------------------------------------
  255. BOOL
  256. CLdapStore::DeleteCrl (PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
  257. {
  258. return( WriteCheckSetDirtyWithLock(
  259. CONTEXT_OID_CRL,
  260. (LPVOID)pCrlContext,
  261. 0
  262. ) );
  263. }
  264. //+---------------------------------------------------------------------------
  265. //
  266. // Member: CLdapStore::DeleteCtl, public
  267. //
  268. // Synopsis: delete CTL from store
  269. //
  270. //----------------------------------------------------------------------------
  271. BOOL
  272. CLdapStore::DeleteCtl (PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
  273. {
  274. return( WriteCheckSetDirtyWithLock(
  275. CONTEXT_OID_CTL,
  276. (LPVOID)pCtlContext,
  277. 0
  278. ) );
  279. }
  280. //+---------------------------------------------------------------------------
  281. //
  282. // Member: CLdapStore::SetCertProperty, public
  283. //
  284. // Synopsis: set a property on the cert
  285. //
  286. //----------------------------------------------------------------------------
  287. BOOL
  288. CLdapStore::SetCertProperty (
  289. PCCERT_CONTEXT pCertContext,
  290. DWORD dwPropId,
  291. DWORD dwFlags,
  292. const void* pvPara
  293. )
  294. {
  295. // NOTENOTE: Properties are NOT persisted
  296. return( TRUE );
  297. }
  298. //+---------------------------------------------------------------------------
  299. //
  300. // Member: CLdapStore::SetCrlProperty, public
  301. //
  302. // Synopsis: set a property on the CRL
  303. //
  304. //----------------------------------------------------------------------------
  305. BOOL
  306. CLdapStore::SetCrlProperty (
  307. PCCRL_CONTEXT pCrlContext,
  308. DWORD dwPropId,
  309. DWORD dwFlags,
  310. const void* pvPara
  311. )
  312. {
  313. // NOTENOTE: Properties are NOT persisted
  314. return( TRUE );
  315. }
  316. //+---------------------------------------------------------------------------
  317. //
  318. // Member: CLdapStore::SetCtlProperty, public
  319. //
  320. // Synopsis: set a property on the CTL
  321. //
  322. //----------------------------------------------------------------------------
  323. BOOL
  324. CLdapStore::SetCtlProperty (
  325. PCCTL_CONTEXT pCrlContext,
  326. DWORD dwPropId,
  327. DWORD dwFlags,
  328. const void* pvPara
  329. )
  330. {
  331. // NOTENOTE: Properties are NOT persisted
  332. return( TRUE );
  333. }
  334. //+---------------------------------------------------------------------------
  335. //
  336. // Member: CLdapStore::WriteCert, public
  337. //
  338. // Synopsis: write cert to store
  339. //
  340. //----------------------------------------------------------------------------
  341. BOOL
  342. CLdapStore::WriteCert (PCCERT_CONTEXT pCertContext, DWORD dwFlags)
  343. {
  344. return( WriteCheckSetDirtyWithLock(
  345. CONTEXT_OID_CERTIFICATE,
  346. (LPVOID)pCertContext,
  347. dwFlags
  348. ) );
  349. }
  350. //+---------------------------------------------------------------------------
  351. //
  352. // Member: CLdapStore::WriteCrl, public
  353. //
  354. // Synopsis: write CRL to store
  355. //
  356. //----------------------------------------------------------------------------
  357. BOOL
  358. CLdapStore::WriteCrl (PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
  359. {
  360. return( WriteCheckSetDirtyWithLock(
  361. CONTEXT_OID_CRL,
  362. (LPVOID)pCrlContext,
  363. dwFlags
  364. ) );
  365. }
  366. //+---------------------------------------------------------------------------
  367. //
  368. // Member: CLdapStore::WriteCtl, public
  369. //
  370. // Synopsis: write CTL to store
  371. //
  372. //----------------------------------------------------------------------------
  373. BOOL
  374. CLdapStore::WriteCtl (PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
  375. {
  376. return( WriteCheckSetDirtyWithLock(
  377. CONTEXT_OID_CTL,
  378. (LPVOID)pCtlContext,
  379. dwFlags
  380. ) );
  381. }
  382. //+---------------------------------------------------------------------------
  383. //
  384. // Member: CLdapStore::StoreControl, public
  385. //
  386. // Synopsis: store control dispatch
  387. //
  388. //----------------------------------------------------------------------------
  389. BOOL
  390. CLdapStore::StoreControl (DWORD dwFlags, DWORD dwCtrlType, LPVOID pvCtrlPara)
  391. {
  392. switch ( dwCtrlType )
  393. {
  394. case CERT_STORE_CTRL_COMMIT:
  395. return( Commit( dwFlags ) );
  396. case CERT_STORE_CTRL_RESYNC:
  397. return( Resync() );
  398. }
  399. SetLastError( (DWORD) ERROR_CALL_NOT_IMPLEMENTED );
  400. return( FALSE );
  401. }
  402. //+---------------------------------------------------------------------------
  403. //
  404. // Member: CLdapStore::Commit, public
  405. //
  406. // Synopsis: commit the store
  407. //
  408. //----------------------------------------------------------------------------
  409. BOOL
  410. CLdapStore::Commit (DWORD dwFlags)
  411. {
  412. BOOL fResult;
  413. EnterCriticalSection( &m_StoreLock );
  414. fResult = InternalCommit( dwFlags );
  415. LeaveCriticalSection( &m_StoreLock );
  416. return( fResult );
  417. }
  418. //+---------------------------------------------------------------------------
  419. //
  420. // Member: CLdapStore::Resync, public
  421. //
  422. // Synopsis: resync the store
  423. //
  424. //----------------------------------------------------------------------------
  425. BOOL
  426. CLdapStore::Resync ()
  427. {
  428. BOOL fResult;
  429. EnterCriticalSection( &m_StoreLock );
  430. fResult = FillCacheStore( TRUE );
  431. LeaveCriticalSection( &m_StoreLock );
  432. return( fResult );
  433. }
  434. //+---------------------------------------------------------------------------
  435. //
  436. // Member: CLdapStore::FillCacheStore, private
  437. //
  438. // Synopsis: fill the cache store
  439. //
  440. //----------------------------------------------------------------------------
  441. BOOL
  442. CLdapStore::FillCacheStore (BOOL fClearCache)
  443. {
  444. BOOL fResult;
  445. CRYPT_BLOB_ARRAY cba;
  446. if ( fClearCache == TRUE )
  447. {
  448. if ( ObjectContextDeleteAllObjectsFromStore( m_hCacheStore ) == FALSE )
  449. {
  450. return( FALSE );
  451. }
  452. }
  453. fResult = LdapSendReceiveUrlRequest(
  454. m_pBinding,
  455. &m_UrlComponents,
  456. 0, // dwRetrievalFlags
  457. LDAP_STORE_TIMEOUT,
  458. &cba,
  459. NULL
  460. );
  461. if (fResult)
  462. {
  463. HCERTSTORE hStore = NULL;
  464. fResult = CreateObjectContext (
  465. CRYPT_RETRIEVE_MULTIPLE_OBJECTS,
  466. &cba,
  467. CERT_QUERY_CONTENT_FLAG_CERT |
  468. CERT_QUERY_CONTENT_FLAG_CTL |
  469. CERT_QUERY_CONTENT_FLAG_CRL |
  470. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
  471. CERT_QUERY_CONTENT_FLAG_CERT_PAIR,
  472. FALSE, // fQuerySingleContext
  473. (LPVOID*) &hStore
  474. );
  475. if (fResult)
  476. {
  477. fResult = I_CertUpdateStore( m_hCacheStore, hStore, 0, NULL );
  478. CertCloseStore( hStore, 0 );
  479. }
  480. CCryptBlobArray BlobArray( &cba, 0 );
  481. BlobArray.FreeArray( TRUE );
  482. }
  483. else if (GetLastError() == CRYPT_E_NOT_FOUND)
  484. {
  485. fResult = TRUE;
  486. }
  487. return( fResult );
  488. }
  489. //+---------------------------------------------------------------------------
  490. //
  491. // Member: CLdapStore::InternalCommit, private
  492. //
  493. // Synopsis: commit current changes
  494. //
  495. //----------------------------------------------------------------------------
  496. BOOL
  497. CLdapStore::InternalCommit (DWORD dwFlags)
  498. {
  499. BOOL fResult = TRUE;
  500. LPCSTR pszContextOid = CONTEXT_OID_CERTIFICATE;
  501. LPVOID pvContext = NULL;
  502. struct berval** abv;
  503. struct berval** newabv;
  504. DWORD cbv = 0;
  505. DWORD cCount;
  506. DWORD cArray = MIN_BERVAL;
  507. if ( dwFlags & CERT_STORE_CTRL_COMMIT_CLEAR_FLAG )
  508. {
  509. m_fDirty = FALSE;
  510. return( TRUE );
  511. }
  512. if ( m_dwOpenFlags & CERT_STORE_READONLY_FLAG )
  513. {
  514. SetLastError( (DWORD) ERROR_ACCESS_DENIED );
  515. return( FALSE );
  516. }
  517. if ( ( m_fDirty == FALSE ) &&
  518. !( dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG ) )
  519. {
  520. return( TRUE );
  521. }
  522. abv = (struct berval**)malloc( cArray * sizeof( struct berval* ) );
  523. if ( abv == NULL )
  524. {
  525. SetLastError( (DWORD) E_OUTOFMEMORY );
  526. return( FALSE );
  527. }
  528. memset( abv, 0, cArray * sizeof( struct berval * ) );
  529. while ( ( fResult == TRUE ) &&
  530. ( ( pvContext = ObjectContextEnumObjectsInStore(
  531. m_hCacheStore,
  532. pszContextOid,
  533. pvContext,
  534. &pszContextOid
  535. ) ) != NULL ) )
  536. {
  537. abv[cbv] = (struct berval*)malloc( sizeof( struct berval ) );
  538. if ( abv[cbv] != NULL )
  539. {
  540. ObjectContextGetEncodedBits(
  541. pszContextOid,
  542. pvContext,
  543. &(abv[cbv]->bv_len),
  544. (LPBYTE *)&(abv[cbv]->bv_val)
  545. );
  546. cbv += 1;
  547. }
  548. else
  549. {
  550. SetLastError( (DWORD) E_OUTOFMEMORY );
  551. fResult = FALSE;
  552. ObjectContextFree( pszContextOid, pvContext );
  553. }
  554. if ( cbv == ( cArray - 1 ) )
  555. {
  556. newabv = (struct berval**)realloc(
  557. abv,
  558. ( cArray + GROW_BERVAL ) *
  559. sizeof( struct berval* )
  560. );
  561. if ( newabv != NULL )
  562. {
  563. abv = newabv;
  564. memset( &abv[cArray], 0, GROW_BERVAL * sizeof( struct berval* ) );
  565. cArray += GROW_BERVAL;
  566. }
  567. else
  568. {
  569. SetLastError( (DWORD) E_OUTOFMEMORY );
  570. fResult = FALSE;
  571. }
  572. }
  573. }
  574. if ( fResult == TRUE )
  575. {
  576. ULONG lderr;
  577. LDAPModW mod;
  578. LDAPModW* amod[2];
  579. assert( m_UrlComponents.cAttr == 1 );
  580. mod.mod_type = m_UrlComponents.apwszAttr[0];
  581. mod.mod_op = LDAP_MOD_BVALUES;
  582. amod[0] = &mod;
  583. amod[1] = NULL;
  584. if ( cbv > 0 )
  585. {
  586. mod.mod_op |= LDAP_MOD_REPLACE;
  587. mod.mod_bvalues = abv;
  588. }
  589. else
  590. {
  591. mod.mod_op |= LDAP_MOD_DELETE;
  592. mod.mod_bvalues = NULL;
  593. }
  594. if ( ( lderr = ldap_modify_sW(
  595. m_pBinding,
  596. m_UrlComponents.pwszDN,
  597. amod
  598. ) ) == LDAP_SUCCESS )
  599. {
  600. m_fDirty = FALSE;
  601. }
  602. else
  603. {
  604. SetLastError( I_CryptNetLdapMapErrorToWin32( m_pBinding, lderr ) );
  605. fResult = FALSE;
  606. }
  607. }
  608. for ( cCount = 0; cCount < cbv; cCount++ )
  609. {
  610. free( abv[cCount] );
  611. }
  612. free( abv );
  613. return( fResult );
  614. }
  615. //+---------------------------------------------------------------------------
  616. //
  617. // Member: CLdapStore::WriteCheckSetDirtyWithLock, private
  618. //
  619. // Synopsis: if the store is writable, set the dirty flag taking the store
  620. // lock where appropriate
  621. //
  622. //----------------------------------------------------------------------------
  623. BOOL
  624. CLdapStore::WriteCheckSetDirtyWithLock (
  625. LPCSTR pszContextOid,
  626. LPVOID pvContext,
  627. DWORD dwFlags
  628. )
  629. {
  630. if ( m_dwOpenFlags & CERT_STORE_READONLY_FLAG )
  631. {
  632. SetLastError( (DWORD) ERROR_ACCESS_DENIED );
  633. return( FALSE );
  634. }
  635. EnterCriticalSection( &m_StoreLock );
  636. if ( ( dwFlags >> 16 ) == CERT_STORE_ADD_ALWAYS )
  637. {
  638. LPVOID pv;
  639. if ( ( pv = ObjectContextFindCorrespondingObject(
  640. m_hCacheStore,
  641. pszContextOid,
  642. pvContext
  643. ) ) != NULL )
  644. {
  645. ObjectContextFree( pszContextOid, pv );
  646. SetLastError( (DWORD) CRYPT_E_EXISTS );
  647. LeaveCriticalSection( &m_StoreLock );
  648. return( FALSE );
  649. }
  650. }
  651. m_fDirty = TRUE;
  652. LeaveCriticalSection( &m_StoreLock );
  653. return( TRUE );
  654. }
  655. //+---------------------------------------------------------------------------
  656. //
  657. // Function: LdapProvOpenStore
  658. //
  659. // Synopsis: provider open store entry point
  660. //
  661. //----------------------------------------------------------------------------
  662. BOOL WINAPI LdapProvOpenStore (
  663. IN LPCSTR pszStoreProv,
  664. IN DWORD dwMsgAndCertEncodingType,
  665. IN HCRYPTPROV hCryptProv,
  666. IN DWORD dwFlags,
  667. IN const void* pvPara,
  668. IN HCERTSTORE hCertStore,
  669. IN OUT PCERT_STORE_PROV_INFO pStoreProvInfo
  670. )
  671. {
  672. BOOL fResult = FALSE;
  673. CLdapStore* pLdap;
  674. pLdap = new CLdapStore ( fResult );
  675. if ( pLdap == NULL )
  676. {
  677. SetLastError( (DWORD) E_OUTOFMEMORY );
  678. return( FALSE );
  679. }
  680. if ( fResult == FALSE )
  681. {
  682. delete pLdap;
  683. return( FALSE );
  684. }
  685. fResult = pLdap->OpenStore(
  686. pszStoreProv,
  687. dwMsgAndCertEncodingType,
  688. hCryptProv,
  689. dwFlags,
  690. pvPara,
  691. hCertStore,
  692. pStoreProvInfo
  693. );
  694. if ( fResult == FALSE )
  695. {
  696. delete pLdap;
  697. }
  698. return( fResult );
  699. }
  700. //+---------------------------------------------------------------------------
  701. //
  702. // Function: LdapProvCloseStore
  703. //
  704. // Synopsis: provider close store entry point
  705. //
  706. //----------------------------------------------------------------------------
  707. void WINAPI LdapProvCloseStore (
  708. IN HCERTSTOREPROV hStoreProv,
  709. IN DWORD dwFlags
  710. )
  711. {
  712. ( (CLdapStore *)hStoreProv )->CloseStore( dwFlags );
  713. delete (CLdapStore *)hStoreProv;
  714. }
  715. //+---------------------------------------------------------------------------
  716. //
  717. // Function: LdapProvDeleteCert
  718. //
  719. // Synopsis: provider delete certificate entry point
  720. //
  721. //----------------------------------------------------------------------------
  722. BOOL WINAPI LdapProvDeleteCert (
  723. IN HCERTSTOREPROV hStoreProv,
  724. IN PCCERT_CONTEXT pCertContext,
  725. IN DWORD dwFlags
  726. )
  727. {
  728. return( ( (CLdapStore *)hStoreProv )->DeleteCert( pCertContext, dwFlags ) );
  729. }
  730. //+---------------------------------------------------------------------------
  731. //
  732. // Function: LdapProvDeleteCrl
  733. //
  734. // Synopsis: provider delete CRL entry point
  735. //
  736. //----------------------------------------------------------------------------
  737. BOOL WINAPI LdapProvDeleteCrl (
  738. IN HCERTSTOREPROV hStoreProv,
  739. IN PCCRL_CONTEXT pCrlContext,
  740. IN DWORD dwFlags
  741. )
  742. {
  743. return( ( (CLdapStore *)hStoreProv )->DeleteCrl( pCrlContext, dwFlags ) );
  744. }
  745. //+---------------------------------------------------------------------------
  746. //
  747. // Function: LdapProvDeleteCtl
  748. //
  749. // Synopsis: provider delete CTL entry point
  750. //
  751. //----------------------------------------------------------------------------
  752. BOOL WINAPI LdapProvDeleteCtl (
  753. IN HCERTSTOREPROV hStoreProv,
  754. IN PCCTL_CONTEXT pCtlContext,
  755. IN DWORD dwFlags
  756. )
  757. {
  758. return( ( (CLdapStore *)hStoreProv )->DeleteCtl( pCtlContext, dwFlags ) );
  759. }
  760. //+---------------------------------------------------------------------------
  761. //
  762. // Function: LdapProvSetCertProperty
  763. //
  764. // Synopsis: provider set certificate property entry point
  765. //
  766. //----------------------------------------------------------------------------
  767. BOOL WINAPI LdapProvSetCertProperty (
  768. IN HCERTSTOREPROV hStoreProv,
  769. IN PCCERT_CONTEXT pCertContext,
  770. IN DWORD dwPropId,
  771. IN DWORD dwFlags,
  772. IN const void* pvData
  773. )
  774. {
  775. return( ( (CLdapStore *)hStoreProv )->SetCertProperty(
  776. pCertContext,
  777. dwPropId,
  778. dwFlags,
  779. pvData
  780. ) );
  781. }
  782. //+---------------------------------------------------------------------------
  783. //
  784. // Function: LdapProvSetCrlProperty
  785. //
  786. // Synopsis: provider set CRL property entry point
  787. //
  788. //----------------------------------------------------------------------------
  789. BOOL WINAPI LdapProvSetCrlProperty (
  790. IN HCERTSTOREPROV hStoreProv,
  791. IN PCCRL_CONTEXT pCrlContext,
  792. IN DWORD dwPropId,
  793. IN DWORD dwFlags,
  794. IN const void* pvData
  795. )
  796. {
  797. return( ( (CLdapStore *)hStoreProv )->SetCrlProperty(
  798. pCrlContext,
  799. dwPropId,
  800. dwFlags,
  801. pvData
  802. ) );
  803. }
  804. //+---------------------------------------------------------------------------
  805. //
  806. // Function: LdapProvSetCtlProperty
  807. //
  808. // Synopsis: provider set CTL property entry point
  809. //
  810. //----------------------------------------------------------------------------
  811. BOOL WINAPI LdapProvSetCtlProperty (
  812. IN HCERTSTOREPROV hStoreProv,
  813. IN PCCTL_CONTEXT pCtlContext,
  814. IN DWORD dwPropId,
  815. IN DWORD dwFlags,
  816. IN const void* pvData
  817. )
  818. {
  819. return( ( (CLdapStore *)hStoreProv )->SetCtlProperty(
  820. pCtlContext,
  821. dwPropId,
  822. dwFlags,
  823. pvData
  824. ) );
  825. }
  826. //+---------------------------------------------------------------------------
  827. //
  828. // Function: LdapProvWriteCert
  829. //
  830. // Synopsis: provider write certificate entry point
  831. //
  832. //----------------------------------------------------------------------------
  833. BOOL WINAPI LdapProvWriteCert (
  834. IN HCERTSTOREPROV hStoreProv,
  835. IN PCCERT_CONTEXT pCertContext,
  836. IN DWORD dwFlags
  837. )
  838. {
  839. return( ( (CLdapStore *)hStoreProv )->WriteCert( pCertContext, dwFlags ) );
  840. }
  841. //+---------------------------------------------------------------------------
  842. //
  843. // Function: LdapProvWriteCrl
  844. //
  845. // Synopsis: provider write CRL entry point
  846. //
  847. //----------------------------------------------------------------------------
  848. BOOL WINAPI LdapProvWriteCrl (
  849. IN HCERTSTOREPROV hStoreProv,
  850. IN PCCRL_CONTEXT pCrlContext,
  851. IN DWORD dwFlags
  852. )
  853. {
  854. return( ( (CLdapStore *)hStoreProv )->WriteCrl( pCrlContext, dwFlags ) );
  855. }
  856. //+---------------------------------------------------------------------------
  857. //
  858. // Function: LdapProvWriteCtl
  859. //
  860. // Synopsis: provider write CTL entry point
  861. //
  862. //----------------------------------------------------------------------------
  863. BOOL WINAPI LdapProvWriteCtl (
  864. IN HCERTSTOREPROV hStoreProv,
  865. IN PCCTL_CONTEXT pCtlContext,
  866. IN DWORD dwFlags
  867. )
  868. {
  869. return( ( (CLdapStore *)hStoreProv )->WriteCtl( pCtlContext, dwFlags ) );
  870. }
  871. //+---------------------------------------------------------------------------
  872. //
  873. // Function: LdapProvStoreControl
  874. //
  875. // Synopsis: provider control entry point
  876. //
  877. //----------------------------------------------------------------------------
  878. BOOL WINAPI LdapProvStoreControl (
  879. IN HCERTSTOREPROV hStoreProv,
  880. IN DWORD dwFlags,
  881. IN DWORD dwCtrlType,
  882. IN LPVOID pvCtrlPara
  883. )
  884. {
  885. return( ( (CLdapStore *)hStoreProv )->StoreControl(
  886. dwFlags,
  887. dwCtrlType,
  888. pvCtrlPara
  889. ) );
  890. }
  891. //+---------------------------------------------------------------------------
  892. //
  893. // Function: I_CryptNetGetUserDsStoreUrl
  894. //
  895. // Synopsis: get user DS store URL
  896. //
  897. //----------------------------------------------------------------------------
  898. BOOL WINAPI
  899. I_CryptNetGetUserDsStoreUrl (
  900. IN LPWSTR pwszUserAttribute,
  901. OUT LPWSTR* ppwszUrl
  902. )
  903. {
  904. BOOL fResult;
  905. DWORD dwLastError = 0;
  906. WCHAR wszUser[MAX_PATH];
  907. ULONG nUser = MAX_PATH;
  908. LPWSTR pwszUser = wszUser;
  909. HMODULE hModule = NULL;
  910. PFN_GETUSERNAMEEXW pfnGetUserNameExW = NULL;
  911. hModule = LoadLibraryA( "secur32.dll" );
  912. if ( hModule == NULL )
  913. {
  914. return( FALSE );
  915. }
  916. pfnGetUserNameExW = (PFN_GETUSERNAMEEXW)GetProcAddress(
  917. hModule,
  918. "GetUserNameExW"
  919. );
  920. if ( pfnGetUserNameExW == NULL )
  921. {
  922. FreeLibrary( hModule );
  923. return( FALSE );
  924. }
  925. fResult = ( *pfnGetUserNameExW )( NameFullyQualifiedDN, pwszUser, &nUser );
  926. if ( fResult == FALSE )
  927. {
  928. if ( ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) ||
  929. ( GetLastError() == ERROR_MORE_DATA ) )
  930. {
  931. pwszUser = new WCHAR [nUser];
  932. if ( pwszUser != NULL )
  933. {
  934. fResult = ( *pfnGetUserNameExW )(
  935. NameFullyQualifiedDN,
  936. pwszUser,
  937. &nUser
  938. );
  939. }
  940. else
  941. {
  942. SetLastError( (DWORD) E_OUTOFMEMORY );
  943. fResult = FALSE;
  944. }
  945. }
  946. }
  947. if ( fResult == TRUE )
  948. {
  949. DWORD cchUrl = 0;
  950. LPWSTR pwszUrl = NULL;
  951. cchUrl = wcslen(USER_DS_STORE_URL_PREFIX) + wcslen(pwszUser) +
  952. wcslen(USER_DS_STORE_URL_SEPARATOR) + wcslen(pwszUserAttribute) + 1;
  953. pwszUrl = (LPWSTR)CryptMemAlloc(cchUrl * sizeof(WCHAR));
  954. if ( pwszUrl != NULL )
  955. {
  956. wcscpy(pwszUrl, USER_DS_STORE_URL_PREFIX);
  957. wcscat(pwszUrl, pwszUser);
  958. wcscat(pwszUrl, USER_DS_STORE_URL_SEPARATOR);
  959. wcscat(pwszUrl, pwszUserAttribute);
  960. *ppwszUrl = pwszUrl;
  961. }
  962. else
  963. {
  964. SetLastError( (DWORD) E_OUTOFMEMORY );
  965. fResult = FALSE;
  966. }
  967. }
  968. dwLastError = GetLastError();
  969. if ( pwszUser != wszUser )
  970. {
  971. delete [] pwszUser;
  972. }
  973. FreeLibrary( hModule );
  974. SetLastError(dwLastError);
  975. return( fResult );
  976. }