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.

3121 lines
80 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1996
  6. //
  7. // File: pkauth.cxx
  8. //
  9. // Contents: Routines for supporting public-key authentication
  10. //
  11. //
  12. // History: 14-October-1997 Created MikeSw
  13. //
  14. //------------------------------------------------------------------------
  15. #include <kerb.hxx>
  16. #include <kerbp.h>
  17. //#ifndef WIN32_CHICAGO
  18. extern "C"
  19. {
  20. #include <stdlib.h>
  21. #include <cryptdll.h>
  22. }
  23. //#endif // WIN32_CHICAGO
  24. #ifdef RETAIL_LOG_SUPPORT
  25. static TCHAR THIS_FILE[]=TEXT(__FILE__);
  26. #endif
  27. KERB_OBJECT_ID KerbSignatureAlg[10];
  28. #define KERB_SCLOGON_DOMAIN_SUFFIX L"-sclogon"
  29. #define KERB_SCLOGON_DOMAIN_SUFFIX_SIZE (sizeof(KERB_SCLOGON_DOMAIN_SUFFIX) - sizeof(WCHAR))
  30. #ifndef SHA1DIGESTLEN
  31. #define SHA1DIGESTLEN 20
  32. #endif
  33. NTSTATUS
  34. KerbInitializeHProvFromCert(
  35. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds
  36. );
  37. //+-------------------------------------------------------------------------
  38. //
  39. // Function: KerbComparePublicKeyCreds
  40. //
  41. // Synopsis: Verfies a certificate is valid for the specified usage
  42. //
  43. // Effects:
  44. //
  45. // Arguments:
  46. //
  47. // Requires:
  48. //
  49. // Returns:
  50. //
  51. // Notes:
  52. //
  53. //
  54. //--------------------------------------------------------------------------
  55. BOOL
  56. KerbComparePublicKeyCreds(
  57. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds1,
  58. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds2
  59. )
  60. {
  61. return CertCompareCertificate(
  62. X509_ASN_ENCODING,
  63. PkCreds1->CertContext->pCertInfo,
  64. PkCreds2->CertContext->pCertInfo
  65. );
  66. // more later?
  67. //return (fRet);
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Function: KerbCheckCertificate
  72. //
  73. // Synopsis: Verfies a certificate is valid for the specified usage
  74. //
  75. // Effects:
  76. //
  77. // Arguments:
  78. //
  79. // Requires:
  80. //
  81. // Returns:
  82. //
  83. // Notes:
  84. //
  85. //
  86. //--------------------------------------------------------------------------
  87. NTSTATUS
  88. KerbCheckCertificate(
  89. IN PCCERT_CONTEXT CertContext,
  90. IN LPSTR Usage,
  91. IN BOOLEAN LocalLogon // AllowRevocationCheckFailure
  92. )
  93. {
  94. NTSTATUS Status = STATUS_SUCCESS;
  95. CERT_CHAIN_PARA ChainParameters = {0};
  96. PCCERT_CHAIN_CONTEXT ChainContext = NULL;
  97. ChainParameters.cbSize = sizeof(CERT_CHAIN_PARA);
  98. ChainParameters.RequestedUsage.dwType = USAGE_MATCH_TYPE_AND;
  99. ChainParameters.RequestedUsage.Usage.cUsageIdentifier = 1;
  100. ChainParameters.RequestedUsage.Usage.rgpszUsageIdentifier = &Usage;
  101. if (!CertGetCertificateChain(
  102. HCCE_LOCAL_MACHINE,
  103. CertContext,
  104. NULL, // evaluate at current time
  105. NULL, // no additional stores
  106. &ChainParameters,
  107. (LocalLogon?
  108. CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY|CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT:
  109. CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT),
  110. NULL, // reserved
  111. &ChainContext
  112. ))
  113. {
  114. DebugLog((DEB_WARN,"Failed to verify certificate chain: %0x%x\n",GetLastError()));
  115. Status = STATUS_PKINIT_FAILURE;
  116. }
  117. else
  118. {
  119. CERT_CHAIN_POLICY_PARA ChainPolicy;
  120. CERT_CHAIN_POLICY_STATUS PolicyStatus;
  121. ZeroMemory(&ChainPolicy, sizeof(ChainPolicy));
  122. ChainPolicy.cbSize = sizeof(ChainPolicy);
  123. if (LocalLogon)
  124. {
  125. ChainPolicy.dwFlags = CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS;
  126. }
  127. ZeroMemory(&PolicyStatus, sizeof(PolicyStatus));
  128. PolicyStatus.cbSize = sizeof(PolicyStatus);
  129. PolicyStatus.lChainIndex = -1;
  130. PolicyStatus.lElementIndex = -1;
  131. if (!CertVerifyCertificateChainPolicy(
  132. CERT_CHAIN_POLICY_BASE,
  133. ChainContext,
  134. &ChainPolicy,
  135. &PolicyStatus))
  136. {
  137. DebugLog((DEB_WARN,"CertVerifyCertificateChainPolicy failure: %0x%x\n", GetLastError()));
  138. Status = STATUS_PKINIT_FAILURE;
  139. }
  140. if(PolicyStatus.dwError != S_OK)
  141. {
  142. DebugLog((DEB_WARN,"CertVerifyCertificateChainPolicy - Chain Status failure: %0x%x\n",PolicyStatus.dwError));
  143. KerbReportPkinitError(
  144. PolicyStatus.dwError,
  145. CertContext
  146. );
  147. Status = KerbMapCertChainError(PolicyStatus.dwError, FALSE);
  148. }
  149. }
  150. if (ChainContext != NULL)
  151. {
  152. CertFreeCertificateChain(ChainContext);
  153. }
  154. return(Status);
  155. }
  156. //+-------------------------------------------------------------------------
  157. //
  158. // Function: KerbVerifyPkAsReply
  159. //
  160. // Synopsis: Verifies the reply from the KDC and retrieves the
  161. // ticket encryption key
  162. //
  163. // Effects:
  164. //
  165. // Arguments:
  166. //
  167. // Requires:
  168. //
  169. // Returns:
  170. //
  171. // Notes:
  172. //
  173. //
  174. //--------------------------------------------------------------------------
  175. NTSTATUS
  176. KerbVerifyPkAsReply(
  177. IN PKERB_PA_DATA_LIST InputPaData,
  178. IN PKERB_PRIMARY_CREDENTIAL Credentials,
  179. IN ULONG Nonce,
  180. OUT PKERB_ENCRYPTION_KEY EncryptionKey,
  181. OUT PBOOLEAN Done
  182. )
  183. {
  184. NTSTATUS Status = STATUS_SUCCESS;
  185. KERBERR KerbErr = KDC_ERR_NONE;
  186. PKERB_PA_PK_AS_REP Reply = NULL;
  187. PCCERT_CONTEXT KdcCertContext = NULL;
  188. PBYTE EncodedKeyPackage = NULL;
  189. ULONG KeyPackageSize = 0;
  190. PKERB_SIGNED_REPLY_KEY_PACKAGE KeyPackage = NULL;
  191. PKERB_REPLY_KEY_PACKAGE ReplyKeyPackage = NULL;
  192. PBYTE PackedKeyPack = NULL;
  193. ULONG PackedKeyPackSize = 0;
  194. HCRYPTKEY PrivateKey = NULL;
  195. PKERB_ENCRYPTION_KEY TempKey = NULL;
  196. HCRYPTPROV KdcProvider = NULL;
  197. BOOLEAN InitializedPkCreds = FALSE;
  198. NTSTATUS TokenStatus = STATUS_SUCCESS;
  199. HANDLE ImpersonationToken = NULL;
  200. *Done = TRUE;
  201. //
  202. // Unpack the request
  203. //
  204. KerbErr = KerbUnpackData(
  205. InputPaData->value.preauth_data.value,
  206. InputPaData->value.preauth_data.length,
  207. KERB_PA_PK_AS_REP_PDU,
  208. (PVOID *) &Reply
  209. );
  210. if (!KERB_SUCCESS(KerbErr))
  211. {
  212. Status = STATUS_INSUFFICIENT_RESOURCES;
  213. goto Cleanup;
  214. }
  215. if (Reply->choice != key_package_chosen)
  216. {
  217. Status = STATUS_INVALID_PARAMETER;
  218. goto Cleanup;
  219. }
  220. //
  221. // Now we need to verify the signature on the message
  222. //
  223. //
  224. // Make sure the csp data is available
  225. //
  226. if ((Credentials->PublicKeyCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  227. {
  228. Status = KerbInitializePkCreds(
  229. Credentials->PublicKeyCreds
  230. );
  231. if (!NT_SUCCESS(Status))
  232. {
  233. goto Cleanup;
  234. }
  235. InitializedPkCreds = TRUE;
  236. }
  237. else if ((Credentials->PublicKeyCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS) != 0)
  238. {
  239. // need to set the PIN and this function does that
  240. Status = KerbInitializeHProvFromCert(
  241. Credentials->PublicKeyCreds
  242. );
  243. if (!NT_SUCCESS(Status))
  244. {
  245. goto Cleanup;
  246. }
  247. }
  248. //
  249. // Decode the contents as an encrypted data buffer
  250. //
  251. Status = __ScHelperDecryptMessage(
  252. &Credentials->PublicKeyCreds->Pin,
  253. Credentials->PublicKeyCreds->CspData,
  254. Credentials->PublicKeyCreds->KerbHProv,
  255. Credentials->PublicKeyCreds->CertContext,
  256. Reply->u.key_package.value,
  257. Reply->u.key_package.length,
  258. EncodedKeyPackage,
  259. &KeyPackageSize
  260. );
  261. if ((Status != STATUS_BUFFER_TOO_SMALL) && (Status != STATUS_SUCCESS))
  262. {
  263. DebugLog((DEB_ERROR,"Failed to decrypt pkcs message: %x\n",Status));
  264. goto Cleanup;
  265. }
  266. SafeAllocaAllocate(EncodedKeyPackage, KeyPackageSize);
  267. if (EncodedKeyPackage == NULL)
  268. {
  269. Status = STATUS_INSUFFICIENT_RESOURCES;
  270. goto Cleanup;
  271. }
  272. Status = __ScHelperDecryptMessage(
  273. &Credentials->PublicKeyCreds->Pin,
  274. Credentials->PublicKeyCreds->CspData,
  275. Credentials->PublicKeyCreds->KerbHProv,
  276. Credentials->PublicKeyCreds->CertContext,
  277. Reply->u.key_package.value,
  278. Reply->u.key_package.length,
  279. EncodedKeyPackage,
  280. &KeyPackageSize
  281. );
  282. if (Status != STATUS_SUCCESS)
  283. {
  284. DebugLog((DEB_ERROR,"Failed to decrypt pkcs message: %x\n",Status));
  285. goto Cleanup;
  286. }
  287. //
  288. // Verify the signature
  289. //
  290. Status = ScHelperVerifyPkcsMessage(
  291. Credentials->PublicKeyCreds->CspData,
  292. NULL, // we don't care which CSP is used for the verification
  293. EncodedKeyPackage,
  294. KeyPackageSize,
  295. PackedKeyPack,
  296. &PackedKeyPackSize,
  297. NULL // don't return certificate context
  298. );
  299. if ((Status != STATUS_BUFFER_TOO_SMALL) && (Status != STATUS_SUCCESS))
  300. {
  301. DebugLog((DEB_ERROR,"Failed to verify message: %x\n",Status));
  302. goto Cleanup;
  303. }
  304. PackedKeyPack = (PBYTE) MIDL_user_allocate(PackedKeyPackSize);
  305. if (PackedKeyPack == NULL)
  306. {
  307. KerbErr = KRB_ERR_GENERIC;
  308. Status = STATUS_INSUFFICIENT_RESOURCES;
  309. goto Cleanup;
  310. }
  311. Status = ScHelperVerifyPkcsMessage(
  312. Credentials->PublicKeyCreds->CspData,
  313. NULL, // we don't care which CSP is used for the verification
  314. EncodedKeyPackage,
  315. KeyPackageSize,
  316. PackedKeyPack,
  317. &PackedKeyPackSize,
  318. &KdcCertContext
  319. );
  320. if (Status != STATUS_SUCCESS)
  321. {
  322. DebugLog((DEB_ERROR,"Failed to verify message: %x\n",Status));
  323. goto Cleanup;
  324. }
  325. KerbErr = KerbUnpackData(
  326. PackedKeyPack,
  327. PackedKeyPackSize,
  328. KERB_REPLY_KEY_PACKAGE_PDU,
  329. (PVOID *) &ReplyKeyPackage
  330. );
  331. if (!KERB_SUCCESS(KerbErr))
  332. {
  333. D_DebugLog((DEB_ERROR,"Failed to unpack reply key package\n"));
  334. Status = KerbMapKerbError(KerbErr);
  335. goto Cleanup;
  336. }
  337. if (Nonce != (ULONG) ReplyKeyPackage->nonce)
  338. {
  339. D_DebugLog((DEB_ERROR,"Returned nonce is not correct: 0x%x instead of 0x%x. %ws, line %d\n",
  340. ReplyKeyPackage->nonce, Nonce, THIS_FILE, __LINE__ ));
  341. Status = STATUS_LOGON_FAILURE;
  342. goto Cleanup;
  343. }
  344. //
  345. // Finally, copy the encryption key out and return it.
  346. //
  347. if (!KERB_SUCCESS(KerbDuplicateKey(
  348. EncryptionKey,
  349. &ReplyKeyPackage->reply_key
  350. )))
  351. {
  352. Status = STATUS_INSUFFICIENT_RESOURCES;
  353. goto Cleanup;
  354. }
  355. //
  356. // Verify the certificate
  357. //
  358. // If we're impersonating, revert, and save off old token. This keeps us from
  359. // going recursive.
  360. //
  361. // Are we impersonating?
  362. //
  363. TokenStatus = NtOpenThreadToken(
  364. NtCurrentThread(),
  365. TOKEN_QUERY | TOKEN_IMPERSONATE,
  366. TRUE,
  367. &ImpersonationToken
  368. );
  369. if( NT_SUCCESS(TokenStatus) )
  370. {
  371. RevertToSelf();
  372. }
  373. else if (TokenStatus != STATUS_NO_TOKEN)
  374. {
  375. Status = TokenStatus;
  376. goto Cleanup;
  377. }
  378. Status = KerbCheckCertificate(
  379. KdcCertContext,
  380. KERB_PKINIT_KDC_CERT_TYPE,
  381. FALSE // don't allow revocation failures
  382. );
  383. if (!NT_SUCCESS(Status))
  384. {
  385. DebugLog((DEB_ERROR,"Failed to verify KDC certificate: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  386. goto Cleanup;
  387. }
  388. Cleanup:
  389. //
  390. // re-impersonate
  391. //
  392. if( ImpersonationToken != NULL ) {
  393. //
  394. // put the thread token back if we were impersonating.
  395. //
  396. SetThreadToken( NULL, ImpersonationToken );
  397. NtClose( ImpersonationToken );
  398. }
  399. //
  400. // If we initialized these, reset them
  401. //
  402. if (InitializedPkCreds)
  403. {
  404. KerbReleasePkCreds(
  405. NULL,
  406. Credentials->PublicKeyCreds,
  407. FALSE
  408. );
  409. }
  410. if (Reply != NULL)
  411. {
  412. KerbFreeData(
  413. KERB_PA_PK_AS_REP_PDU,
  414. Reply
  415. );
  416. }
  417. if (KdcCertContext != NULL)
  418. {
  419. CertFreeCertificateContext(KdcCertContext);
  420. }
  421. if (KeyPackage != NULL)
  422. {
  423. KerbFreeData(
  424. KERB_SIGNED_REPLY_KEY_PACKAGE_PDU,
  425. KeyPackage
  426. );
  427. }
  428. if (ReplyKeyPackage != NULL)
  429. {
  430. KerbFreeData(
  431. KERB_REPLY_KEY_PACKAGE_PDU,
  432. ReplyKeyPackage
  433. );
  434. }
  435. if (PackedKeyPack != NULL)
  436. {
  437. MIDL_user_free(PackedKeyPack);
  438. }
  439. if (PrivateKey != NULL)
  440. {
  441. CryptDestroyKey(PrivateKey);
  442. }
  443. if (TempKey != NULL)
  444. {
  445. KerbFreeData(
  446. KERB_ENCRYPTION_KEY_PDU,
  447. TempKey
  448. );
  449. }
  450. if (KdcProvider != NULL)
  451. {
  452. CryptReleaseContext(
  453. KdcProvider,
  454. 0 // no flags
  455. );
  456. }
  457. SafeAllocaFree(EncodedKeyPackage);
  458. return(Status);
  459. }
  460. //+-------------------------------------------------------------------------
  461. //
  462. // Function: KerbFreePKCreds
  463. //
  464. // Synopsis: Frees the public key creds
  465. //
  466. // Effects:
  467. //
  468. // Arguments: OkForReuse - Allows us to release info which may not be valid,
  469. // but that we can reuse to reaquire a good ScHelperHandle.
  470. //
  471. // Requires:
  472. //
  473. // Returns:
  474. //
  475. // Notes:
  476. //
  477. //
  478. //--------------------------------------------------------------------------
  479. VOID
  480. KerbFreePKCreds(
  481. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds,
  482. IN BOOLEAN OkForReuse
  483. )
  484. {
  485. if ( NULL != PkCreds )
  486. {
  487. if ((( PkCreds->InitializationInfo & CSP_DATA_INITIALIZED ) != 0) &&
  488. (( PkCreds->InitializationInfo &
  489. (CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS | CONTEXT_INITIALIZED_WITH_ACH) ) == 0))
  490. {
  491. __ScHelperRelease(
  492. PkCreds->CspData
  493. );
  494. PkCreds->InitializationInfo &= ~CSP_DATA_INITIALIZED;
  495. }
  496. if ( PkCreds->KerbHProv != NULL )
  497. {
  498. __ScHelper_CryptReleaseContext( PkCreds->KerbHProv );
  499. PkCreds->KerbHProv = NULL;
  500. }
  501. if ( PkCreds->CertContext != NULL )
  502. {
  503. CertFreeCertificateContext( PkCreds->CertContext );
  504. PkCreds->CertContext = NULL;
  505. }
  506. if ( !OkForReuse )
  507. {
  508. RtlSecureZeroMemory(PkCreds->Pin.Buffer, PkCreds->Pin.MaximumLength);
  509. KerbFreeString( &PkCreds->Pin );
  510. KerbFree( PkCreds );
  511. }
  512. }
  513. }
  514. //+-------------------------------------------------------------------------
  515. //
  516. // Function: KerbInitializeHProvFromCert
  517. //
  518. // Synopsis: Initializes the out parameter phProv by getting the key
  519. // prov info from the cert context and acquiring a CSP context
  520. // given this information.
  521. //
  522. // Effects:
  523. //
  524. // Arguments:
  525. //
  526. // Requires:
  527. //
  528. // Returns:
  529. //
  530. // Notes:
  531. //
  532. //
  533. //--------------------------------------------------------------------------
  534. NTSTATUS
  535. KerbInitializeHProvFromCert(
  536. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds
  537. )
  538. {
  539. ULONG cPin;
  540. LPWSTR pwszPin = NULL;
  541. LPSTR pszPin = NULL;
  542. NTSTATUS Status = STATUS_SUCCESS;
  543. DWORD dw;
  544. if (PkCreds->KerbHProv != NULL)
  545. {
  546. goto Cleanup;
  547. }
  548. //
  549. // This function validates that the cert matches the key on the smartcard.
  550. //
  551. Status = __ScHelper_CryptAcquireCertificatePrivateKey(
  552. PkCreds->CertContext,
  553. &PkCreds->KerbHProv,
  554. &dw);
  555. if (!NT_SUCCESS(Status))
  556. {
  557. DebugLog((DEB_ERROR,
  558. "CryptAcquireCertificatePrivateKey failed - %x\n",
  559. Status));
  560. goto Cleanup;
  561. }
  562. //
  563. // Convert the pin to ANSI, but only for creds acquired by ACH, as the
  564. // credman isn't "allowed" to cache pins anymore..
  565. //
  566. if (( PkCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_ACH ) != 0)
  567. {
  568. if (0 == PkCreds->Pin.Length)
  569. {
  570. Status = STATUS_LOGON_FAILURE;
  571. goto Cleanup;
  572. }
  573. SafeAllocaAllocate(pwszPin, PkCreds->Pin.Length + sizeof(WCHAR));
  574. if (NULL == pwszPin)
  575. {
  576. Status = STATUS_INSUFFICIENT_RESOURCES;
  577. goto Cleanup;
  578. }
  579. RtlCopyMemory(pwszPin, PkCreds->Pin.Buffer, PkCreds->Pin.Length);
  580. pwszPin[PkCreds->Pin.Length / sizeof(WCHAR)] = L'\0';
  581. cPin = WideCharToMultiByte(
  582. GetACP(),
  583. 0,
  584. pwszPin,
  585. -1,
  586. NULL,
  587. 0,
  588. NULL,
  589. NULL);
  590. SafeAllocaAllocate(pszPin, (cPin + 1) * sizeof(CHAR));
  591. if (NULL == pszPin)
  592. {
  593. Status = STATUS_INSUFFICIENT_RESOURCES;
  594. goto Cleanup;
  595. }
  596. cPin = WideCharToMultiByte(
  597. GetACP(),
  598. 0,
  599. pwszPin,
  600. -1,
  601. pszPin,
  602. cPin,
  603. NULL,
  604. NULL);
  605. Status = __ScHelper_CryptSetProvParam(
  606. PkCreds->KerbHProv,
  607. pszPin,
  608. &dw
  609. );
  610. if (!NT_SUCCESS(Status))
  611. {
  612. DebugLog((DEB_ERROR, "CryptSetProvParam failed %x\n", Status));
  613. goto Cleanup;
  614. }
  615. }
  616. Cleanup:
  617. SafeAllocaFree(pwszPin);
  618. SafeAllocaFree(pszPin);
  619. return Status;
  620. }
  621. //+-------------------------------------------------------------------------
  622. //
  623. // Function: KerbInitializePkCreds
  624. //
  625. // Synopsis: Initializes or re-initailizes the smart card data in
  626. // the public key creds
  627. //
  628. // Effects:
  629. //
  630. // Arguments:
  631. //
  632. // Requires:
  633. //
  634. // Returns:
  635. //
  636. // Notes:
  637. //
  638. //
  639. //--------------------------------------------------------------------------
  640. NTSTATUS
  641. KerbInitializePkCreds(
  642. IN PKERB_PUBLIC_KEY_CREDENTIALS PkCreds
  643. )
  644. {
  645. NTSTATUS Status = STATUS_SUCCESS;
  646. if ((PkCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  647. {
  648. //
  649. // Credman and explicit creds
  650. //
  651. if (((PkCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS) == 0) &&
  652. ((PkCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_ACH) == 0))
  653. {
  654. //
  655. // This is the default logon case - we're using data just handed to us
  656. // from LsaLogonUser().
  657. //
  658. Status = __ScHelperInitializeContext(
  659. PkCreds->CspData,
  660. PkCreds->CspDataLength
  661. );
  662. if (!NT_SUCCESS(Status))
  663. {
  664. DebugLog((DEB_ERROR,"ScHelperInitializeContext failed- %x\n", Status));
  665. goto Cleanup;
  666. }
  667. PkCreds->InitializationInfo |= CSP_DATA_INITIALIZED;
  668. }
  669. else
  670. {
  671. if (PkCreds->CertContext == NULL)
  672. {
  673. D_DebugLog((DEB_ERROR,"Using cred man creds but cert context is NULL.\n"));
  674. Status = STATUS_INVALID_PARAMETER;
  675. goto Cleanup;
  676. }
  677. PkCreds->InitializationInfo |= CSP_DATA_INITIALIZED;
  678. }
  679. }
  680. if (PkCreds->CertContext == NULL)
  681. {
  682. Status = __ScHelperGetCertFromLogonInfo(
  683. PkCreds->CspData,
  684. &PkCreds->Pin,
  685. &PkCreds->CertContext
  686. );
  687. if (Status != STATUS_SUCCESS)
  688. {
  689. DebugLog((DEB_ERROR,"Failed to get cert from logon info: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  690. if (NT_SUCCESS(Status))
  691. {
  692. Status = STATUS_LOGON_FAILURE;
  693. }
  694. goto Cleanup;
  695. }
  696. }
  697. Cleanup:
  698. if (!NT_SUCCESS(Status))
  699. {
  700. if (((PkCreds->InitializationInfo & CSP_DATA_INITIALIZED) != 0) &&
  701. ((PkCreds->InitializationInfo & ( CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS | CONTEXT_INITIALIZED_WITH_ACH)) == 0))
  702. {
  703. __ScHelperRelease(
  704. PkCreds->CspData
  705. );
  706. PkCreds->InitializationInfo &= ~CSP_DATA_INITIALIZED;
  707. }
  708. }
  709. return(Status);
  710. }
  711. //+-------------------------------------------------------------------------
  712. //
  713. // Function: KerbReleasePkCreds
  714. //
  715. // Synopsis: Releaes smart-card resources in the public key creds.
  716. //
  717. // Effects:
  718. //
  719. // Arguments:
  720. //
  721. // Requires:
  722. //
  723. // Returns:
  724. //
  725. // Notes:
  726. //
  727. //
  728. //--------------------------------------------------------------------------
  729. VOID
  730. KerbReleasePkCreds(
  731. IN OPTIONAL PKERB_LOGON_SESSION LogonSession,
  732. IN OPTIONAL PKERB_PUBLIC_KEY_CREDENTIALS PkCreds,
  733. IN BOOLEAN OkForReuse
  734. )
  735. {
  736. if (ARGUMENT_PRESENT(LogonSession))
  737. {
  738. KerbWriteLockLogonSessions(
  739. LogonSession
  740. );
  741. PkCreds = LogonSession->PrimaryCredentials.PublicKeyCreds;
  742. }
  743. KerbFreePKCreds(PkCreds, OkForReuse);
  744. if (ARGUMENT_PRESENT(LogonSession))
  745. {
  746. if (!OkForReuse)
  747. {
  748. LogonSession->PrimaryCredentials.PublicKeyCreds = NULL;
  749. }
  750. else
  751. {
  752. LogonSession->PrimaryCredentials.PublicKeyCreds->InitializationInfo |= CSP_DATA_REUSED;
  753. }
  754. KerbUnlockLogonSessions(
  755. LogonSession
  756. );
  757. }
  758. }
  759. //+-------------------------------------------------------------------------
  760. //
  761. // Function: KerbComputePkAuthenticatorSignature
  762. //
  763. // Synopsis: Computes the signature of the PK authenticator by
  764. // marshalling the authenticator, checksumming it, then
  765. // encrypting the checksum with the public key, more or less
  766. //
  767. // Effects:
  768. //
  769. // Arguments: AuthPackage - authenticator to sign
  770. // Credentials - Client's credentials (containing keys)
  771. // Signature - receives signature
  772. //
  773. // Requires:
  774. //
  775. // Returns:
  776. //
  777. // Notes:
  778. //
  779. //
  780. //--------------------------------------------------------------------------
  781. NTSTATUS
  782. KerbComputePkAuthenticatorSignature(
  783. IN PKERB_AUTH_PACKAGE AuthPackage,
  784. IN PKERB_PRIMARY_CREDENTIAL Credentials,
  785. OUT PKERB_SIGNATURE Signature
  786. )
  787. {
  788. NTSTATUS Status = STATUS_SUCCESS;
  789. KERBERR KerbErr = KDC_ERR_NONE;
  790. PBYTE PackedAuthenticator = NULL;
  791. ULONG PackedAuthenticatorSize;
  792. BOOLEAN InitializedPkCreds = FALSE;
  793. PUNICODE_STRING TmpPin = NULL;
  794. #define KERB_PK_MAX_SIGNATURE_SIZE 128
  795. BYTE PkSignature[KERB_PK_MAX_SIGNATURE_SIZE];
  796. ULONG PkSignatureLength = KERB_PK_MAX_SIGNATURE_SIZE;
  797. RtlZeroMemory(
  798. Signature,
  799. sizeof(KERB_SIGNATURE)
  800. );
  801. //
  802. // First marshall the auth package
  803. //
  804. KerbErr = KerbPackData(
  805. AuthPackage,
  806. KERB_AUTH_PACKAGE_PDU,
  807. &PackedAuthenticatorSize,
  808. &PackedAuthenticator
  809. );
  810. if (!KERB_SUCCESS(KerbErr))
  811. {
  812. Status = STATUS_INSUFFICIENT_RESOURCES;
  813. goto Cleanup;
  814. }
  815. //
  816. // Make sure the csp data is available
  817. //
  818. if ((Credentials->PublicKeyCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  819. {
  820. Status = KerbInitializePkCreds(
  821. Credentials->PublicKeyCreds
  822. );
  823. if (!NT_SUCCESS(Status))
  824. {
  825. goto Cleanup;
  826. }
  827. InitializedPkCreds = TRUE;
  828. }
  829. else if (((Credentials->PublicKeyCreds->InitializationInfo
  830. & (CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS | CONTEXT_INITIALIZED_WITH_ACH)) != 0))
  831. {
  832. // need to set the PIN and this function does that
  833. Status = KerbInitializeHProvFromCert(
  834. Credentials->PublicKeyCreds
  835. );
  836. if (!NT_SUCCESS(Status))
  837. {
  838. goto Cleanup;
  839. }
  840. }
  841. // Initialize the PIN for ScHelperSignPkcs routines.
  842. if (((Credentials->PublicKeyCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS) == 0) &&
  843. (Credentials->PublicKeyCreds->Pin.Buffer != NULL))
  844. {
  845. TmpPin = &Credentials->PublicKeyCreds->Pin;
  846. }
  847. //
  848. // Now generate the checksum
  849. //
  850. Status = __ScHelperSignMessage(
  851. TmpPin,
  852. Credentials->PublicKeyCreds->CspData,
  853. Credentials->PublicKeyCreds->KerbHProv,
  854. KERB_PKINIT_SIGNATURE_ALG,
  855. PackedAuthenticator,
  856. PackedAuthenticatorSize,
  857. PkSignature,
  858. &PkSignatureLength
  859. );
  860. if (!NT_SUCCESS(Status))
  861. {
  862. DebugLog((DEB_ERROR,"Failed to sign message with card: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  863. goto Cleanup;
  864. }
  865. //
  866. // Build the signature
  867. //
  868. Signature->signature_algorithm.algorithm = KerbSignatureAlg;
  869. //
  870. // Copy the temporary signature into the return structure
  871. //
  872. Signature->pkcs_signature.length = PkSignatureLength * 8; // because it is a bit string
  873. Signature->pkcs_signature.value = (PBYTE) KerbAllocate( PkSignatureLength );
  874. if (Signature->pkcs_signature.value == NULL)
  875. {
  876. Status = STATUS_INSUFFICIENT_RESOURCES;
  877. goto Cleanup;
  878. }
  879. RtlCopyMemory(
  880. Signature->pkcs_signature.value,
  881. PkSignature,
  882. PkSignatureLength
  883. );
  884. Status = STATUS_SUCCESS;
  885. Cleanup:
  886. if (InitializedPkCreds)
  887. {
  888. KerbReleasePkCreds(
  889. NULL,
  890. Credentials->PublicKeyCreds,
  891. FALSE
  892. );
  893. }
  894. if (PackedAuthenticator != NULL)
  895. {
  896. MIDL_user_free(PackedAuthenticator);
  897. }
  898. return(Status);
  899. }
  900. NTSTATUS
  901. KerbGetProvParamWrapper(
  902. IN PUNICODE_STRING pPin,
  903. IN PBYTE pbLogonInfo,
  904. IN OPTIONAL ULONG_PTR KerbHProv,
  905. DWORD dwParam,
  906. BYTE*pbData,
  907. DWORD *pdwDataLen,
  908. DWORD dwFlags
  909. )
  910. {
  911. NTSTATUS Status = STATUS_SUCCESS;
  912. Status = __ScHelperGetProvParam(
  913. pPin,
  914. pbLogonInfo,
  915. KerbHProv,
  916. dwParam,
  917. pbData,
  918. pdwDataLen,
  919. dwFlags
  920. );
  921. if (!NT_SUCCESS(Status))
  922. {
  923. DebugLog((DEB_ERROR, "Failure in SC subsytem - %x\n",Status));
  924. }
  925. return Status;
  926. }
  927. //+-------------------------------------------------------------------------
  928. //
  929. // Function: KerbGetSmartCardAlgorithms
  930. //
  931. // Synopsis: Gets the supported encryption types from the
  932. // smart card provider
  933. //
  934. // Effects:
  935. //
  936. // Arguments:
  937. //
  938. // Requires:
  939. //
  940. // Returns:
  941. //
  942. // Notes:
  943. //
  944. //
  945. //--------------------------------------------------------------------------
  946. NTSTATUS
  947. KerbGetSmartCardAlgorithms(
  948. IN PKERB_PRIMARY_CREDENTIAL Credentials,
  949. OUT PKERB_CRYPT_LIST * CryptList
  950. )
  951. {
  952. NTSTATUS Status = STATUS_SUCCESS;
  953. PROV_ENUMALGS Data;
  954. ULONG DataSize;
  955. ULONG Flags = CRYPT_FIRST;
  956. #define KERB_SUPPORTED_PK_CRYPT_COUNT 2
  957. ULONG CryptTypes[KERB_SUPPORTED_PK_CRYPT_COUNT];
  958. ULONG CryptCount = 0;
  959. //
  960. // Enumerate through to get the encrypt types
  961. //
  962. while (1)
  963. {
  964. DataSize = sizeof(Data);
  965. Status = KerbGetProvParamWrapper(
  966. &Credentials->PublicKeyCreds->Pin,
  967. Credentials->PublicKeyCreds->CspData,
  968. Credentials->PublicKeyCreds->KerbHProv,
  969. PP_ENUMALGS,
  970. (BYTE *) &Data,
  971. &DataSize,
  972. Flags
  973. );
  974. if (Status == STATUS_NO_MORE_ENTRIES)
  975. {
  976. Status = STATUS_SUCCESS;
  977. break;
  978. }
  979. if (!NT_SUCCESS(Status))
  980. {
  981. DebugLog((DEB_ERROR, "GetProvPram failed: 0x%x\n", Status));
  982. return(Status);
  983. }
  984. //
  985. // Reset the flags to enumerate though
  986. //
  987. Flags = 0; // CRYPT_NEXT
  988. //
  989. // Check if it is an encryption algorithm. We only want
  990. // to know about 3des and RC4
  991. //
  992. if (GET_ALG_CLASS(Data.aiAlgid) == ALG_CLASS_DATA_ENCRYPT)
  993. {
  994. //
  995. // Check the type
  996. //
  997. if (GET_ALG_TYPE(Data.aiAlgid) == ALG_TYPE_BLOCK)
  998. {
  999. //
  1000. // Check for 3des
  1001. //
  1002. if (GET_ALG_SID(Data.aiAlgid) == ALG_SID_3DES)
  1003. {
  1004. //
  1005. // Add it to the list.
  1006. //
  1007. CryptTypes[CryptCount++] = KERB_ETYPE_DES_EDE3_CBC_ENV;
  1008. }
  1009. else if (GET_ALG_SID(Data.aiAlgid) == ALG_SID_RC2)
  1010. {
  1011. //
  1012. // Add it to the list.
  1013. //
  1014. CryptTypes[CryptCount++] = KERB_ETYPE_RC2_CBC_ENV;
  1015. }
  1016. }
  1017. }
  1018. if (CryptCount == KERB_SUPPORTED_PK_CRYPT_COUNT)
  1019. {
  1020. break;
  1021. }
  1022. }
  1023. //
  1024. // Now, if there are any crypt types, convert them.
  1025. //
  1026. if (CryptCount != 0)
  1027. {
  1028. KERBERR KerbErr;
  1029. KerbErr = KerbConvertArrayToCryptList(
  1030. CryptList,
  1031. CryptTypes,
  1032. CryptCount,
  1033. FALSE
  1034. );
  1035. return(KerbMapKerbError(KerbErr));
  1036. }
  1037. else
  1038. {
  1039. //
  1040. // We needed one of these, so bail now.
  1041. //
  1042. DebugLog((DEB_ERROR,"Smart card doesn't support rc2 or 3des for logon - failing out.\n"));
  1043. return(STATUS_CRYPTO_SYSTEM_INVALID);
  1044. }
  1045. }
  1046. //+-------------------------------------------------------------------------
  1047. //
  1048. // Function: KerbBuildPkinitPreAuthData
  1049. //
  1050. // Synopsis: Builds the pre-auth data for a PK-INIT AS request
  1051. //
  1052. // Effects:
  1053. //
  1054. // Arguments: Credentials - Credentials to use for this request
  1055. // InputPaData - Any PA data returned from DC on previous
  1056. // call
  1057. // TimeSkew - Known time skew with KDC
  1058. // ServiceName - Name for which we are requesting a ticket
  1059. // RealmName - name of realm in which we are requesting a ticket
  1060. // PreAuthData - receives new PA data
  1061. // Done - if returned as TRUE, then routine need not be called
  1062. // again
  1063. //
  1064. // Requires:
  1065. //
  1066. // Returns:
  1067. //
  1068. // Notes:
  1069. //
  1070. //
  1071. //--------------------------------------------------------------------------
  1072. NTSTATUS
  1073. KerbBuildPkinitPreauthData(
  1074. IN PKERB_PRIMARY_CREDENTIAL Credentials,
  1075. IN OPTIONAL PKERB_PA_DATA_LIST InputPaData,
  1076. IN PTimeStamp TimeSkew,
  1077. IN PKERB_INTERNAL_NAME ServiceName,
  1078. IN PUNICODE_STRING RealmName,
  1079. IN ULONG Nonce,
  1080. OUT PKERB_PA_DATA_LIST * PreAuthData,
  1081. OUT PKERB_ENCRYPTION_KEY EncryptionKey,
  1082. OUT PKERB_CRYPT_LIST * CryptList,
  1083. OUT PBOOLEAN Done
  1084. )
  1085. {
  1086. NTSTATUS Status = STATUS_SUCCESS;
  1087. KERB_PA_PK_AS_REQ Request = {0};
  1088. KERB_AUTH_PACKAGE AuthPack = {0};
  1089. PKERB_PA_DATA_LIST ListElement = NULL;
  1090. ULONG PackedRequestSize = 0;
  1091. PBYTE PackedRequest = NULL;
  1092. PBYTE PackedAuthPack = NULL;
  1093. ULONG PackedAuthPackSize = 0;
  1094. PBYTE SignedAuthPack = NULL;
  1095. ULONG SignedAuthPackSize = 0;
  1096. TimeStamp TimeNow;
  1097. KERBERR KerbErr;
  1098. BOOLEAN FreePkCreds = FALSE;
  1099. CRYPT_ALGORITHM_IDENTIFIER CryptAlg = {0};
  1100. PUNICODE_STRING TmpPin = NULL;
  1101. BOOLEAN CleartextPin = FALSE;
  1102. //
  1103. // For the duration of this function, reveal the pin from
  1104. // its encrypted form.
  1105. //
  1106. if ( Credentials->PublicKeyCreds->Pin.Buffer )
  1107. {
  1108. KerbRevealPassword(&Credentials->PublicKeyCreds->Pin);
  1109. CleartextPin = TRUE;
  1110. }
  1111. //
  1112. // If there is any input, check to see if we succeeded the last time
  1113. // around
  1114. //
  1115. if (ARGUMENT_PRESENT(InputPaData))
  1116. {
  1117. Status = KerbVerifyPkAsReply(
  1118. InputPaData,
  1119. Credentials,
  1120. Nonce,
  1121. EncryptionKey,
  1122. Done
  1123. );
  1124. goto Cleanup;
  1125. }
  1126. //
  1127. // Make sure the csp data is available
  1128. //
  1129. if ((Credentials->PublicKeyCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  1130. {
  1131. Status = KerbInitializePkCreds(
  1132. Credentials->PublicKeyCreds
  1133. );
  1134. if (!NT_SUCCESS(Status))
  1135. {
  1136. goto Cleanup;
  1137. }
  1138. //
  1139. // In some cases, we free up the cspdata, then reacquire it later. This
  1140. // is a hack for TS cases, where the SC rpc server dies when the temp
  1141. // winlogon session goes away, thus invalidating the SChelper handle. But,
  1142. // once we've reacquired it, we should hold onto it.
  1143. //
  1144. if ((Credentials->PublicKeyCreds->InitializationInfo & CSP_DATA_REUSED) == 0)
  1145. {
  1146. FreePkCreds = TRUE;
  1147. }
  1148. }
  1149. else if (((Credentials->PublicKeyCreds->InitializationInfo
  1150. & (CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS | CONTEXT_INITIALIZED_WITH_ACH)) != 0))
  1151. {
  1152. // need to set the PIN and this function does that
  1153. Status = KerbInitializeHProvFromCert(
  1154. Credentials->PublicKeyCreds
  1155. );
  1156. if (!NT_SUCCESS(Status))
  1157. {
  1158. goto Cleanup;
  1159. }
  1160. }
  1161. // Initialize the PIN for ScHelperSignPkcs routines.
  1162. if (((Credentials->PublicKeyCreds->InitializationInfo & CONTEXT_INITIALIZED_WITH_CRED_MAN_CREDS) == 0) &&
  1163. (Credentials->PublicKeyCreds->Pin.Buffer != NULL))
  1164. {
  1165. TmpPin = &Credentials->PublicKeyCreds->Pin;
  1166. }
  1167. Status = KerbGetSmartCardAlgorithms(
  1168. Credentials,
  1169. CryptList
  1170. );
  1171. if (!NT_SUCCESS(Status))
  1172. {
  1173. DebugLog((DEB_ERROR,"Failed to get crypt list for smart card: 0x%x\n",
  1174. Status));
  1175. goto Cleanup;
  1176. }
  1177. //
  1178. // Do the new pa-pk-as-req
  1179. //
  1180. //
  1181. // Now comes the hard part - the PK authenticator
  1182. //
  1183. //
  1184. // First the KDC name
  1185. //
  1186. if (!KERB_SUCCESS(
  1187. KerbConvertKdcNameToPrincipalName(
  1188. &AuthPack.pk_authenticator.kdc_name,
  1189. ServiceName
  1190. )))
  1191. {
  1192. Status = STATUS_SUCCESS;
  1193. goto Cleanup;
  1194. }
  1195. //
  1196. // Then the realm
  1197. //
  1198. if (!KERB_SUCCESS(
  1199. KerbConvertUnicodeStringToRealm(
  1200. &AuthPack.pk_authenticator.kdc_realm,
  1201. RealmName)))
  1202. {
  1203. Status = STATUS_SUCCESS;
  1204. goto Cleanup;
  1205. }
  1206. //
  1207. // Now the time
  1208. //
  1209. GetSystemTimeAsFileTime((PFILETIME) &TimeNow);
  1210. #ifndef WIN32_CHICAGO
  1211. TimeNow.QuadPart += TimeSkew->QuadPart;
  1212. #else // !WIN32_CHICAGO
  1213. TimeNow += *TimeSkew;
  1214. #endif // WIN32_CHICAGO
  1215. KerbConvertLargeIntToGeneralizedTimeWrapper(
  1216. &AuthPack.pk_authenticator.client_time,
  1217. &AuthPack.pk_authenticator.cusec,
  1218. &TimeNow);
  1219. //
  1220. // And finally the nonce
  1221. //
  1222. AuthPack.pk_authenticator.nonce = Nonce;
  1223. //
  1224. // Pack up the auth pack so we can sign it
  1225. //
  1226. KerbErr = KerbPackData(
  1227. &AuthPack,
  1228. KERB_AUTH_PACKAGE_PDU,
  1229. &PackedAuthPackSize,
  1230. &PackedAuthPack
  1231. );
  1232. if (!KERB_SUCCESS(KerbErr))
  1233. {
  1234. DebugLog((DEB_ERROR,"Failed to pack auth package\n"));
  1235. Status = KerbMapKerbError(KerbErr);
  1236. goto Cleanup;
  1237. }
  1238. //
  1239. // Now sign it.
  1240. //
  1241. //
  1242. // Now generate the checksum
  1243. //
  1244. CryptAlg.pszObjId = KERB_PKINIT_SIGNATURE_OID;
  1245. Status = __ScHelperSignPkcsMessage(
  1246. TmpPin,
  1247. Credentials->PublicKeyCreds->CspData,
  1248. Credentials->PublicKeyCreds->KerbHProv,
  1249. Credentials->PublicKeyCreds->CertContext,
  1250. &CryptAlg,
  1251. CRYPT_MESSAGE_SILENT_KEYSET_FLAG, // dwSignMessageFlags
  1252. PackedAuthPack,
  1253. PackedAuthPackSize,
  1254. SignedAuthPack,
  1255. &SignedAuthPackSize
  1256. );
  1257. if ((Status != STATUS_BUFFER_TOO_SMALL) && (Status != STATUS_SUCCESS))
  1258. {
  1259. DebugLog((DEB_ERROR,"Failed to sign message: %x\n",Status));
  1260. goto Cleanup;
  1261. }
  1262. SignedAuthPack = (PBYTE) MIDL_user_allocate(SignedAuthPackSize);
  1263. if (SignedAuthPack == NULL)
  1264. {
  1265. KerbErr = KRB_ERR_GENERIC;
  1266. goto Cleanup;
  1267. }
  1268. Status = __ScHelperSignPkcsMessage(
  1269. TmpPin,
  1270. Credentials->PublicKeyCreds->CspData,
  1271. Credentials->PublicKeyCreds->KerbHProv,
  1272. Credentials->PublicKeyCreds->CertContext,
  1273. &CryptAlg,
  1274. CRYPT_MESSAGE_SILENT_KEYSET_FLAG, // dwSignMessageFlags
  1275. PackedAuthPack,
  1276. PackedAuthPackSize,
  1277. SignedAuthPack,
  1278. &SignedAuthPackSize
  1279. );
  1280. if (Status != STATUS_SUCCESS)
  1281. {
  1282. DebugLog((DEB_ERROR,"Failed to sign pkcs message: 0x%x\n",Status));
  1283. goto Cleanup;
  1284. }
  1285. Request.signed_auth_pack.value = SignedAuthPack;
  1286. Request.signed_auth_pack.length = SignedAuthPackSize;
  1287. //
  1288. // Marshall the request
  1289. //
  1290. if (!KERB_SUCCESS(KerbPackData(
  1291. &Request,
  1292. KERB_PA_PK_AS_REQ_PDU,
  1293. &PackedRequestSize,
  1294. &PackedRequest)))
  1295. {
  1296. Status = STATUS_INSUFFICIENT_RESOURCES;
  1297. goto Cleanup;
  1298. }
  1299. ListElement = (PKERB_PA_DATA_LIST) KerbAllocate(sizeof(KERB_PA_DATA_LIST));
  1300. if (ListElement == NULL)
  1301. {
  1302. Status = STATUS_INSUFFICIENT_RESOURCES;
  1303. goto Cleanup;
  1304. }
  1305. ListElement->value.preauth_data_type = KRB5_PADATA_PK_AS_REP;
  1306. ListElement->value.preauth_data.value = PackedRequest;
  1307. ListElement->value.preauth_data.length = PackedRequestSize;
  1308. PackedRequest = NULL;
  1309. ListElement->next = *PreAuthData;
  1310. *PreAuthData = ListElement;
  1311. ListElement = NULL;
  1312. Cleanup:
  1313. if (CleartextPin)
  1314. {
  1315. KerbHidePassword( &Credentials->PublicKeyCreds->Pin );
  1316. }
  1317. KerbFreeRealm(
  1318. &AuthPack.pk_authenticator.kdc_realm
  1319. );
  1320. KerbFreePrincipalName(
  1321. &AuthPack.pk_authenticator.kdc_name
  1322. );
  1323. if (ListElement != NULL)
  1324. {
  1325. KerbFree(ListElement);
  1326. }
  1327. if (PackedRequest != NULL)
  1328. {
  1329. MIDL_user_free(PackedRequest);
  1330. }
  1331. if (PackedAuthPack != NULL)
  1332. {
  1333. MIDL_user_free(PackedAuthPack);
  1334. }
  1335. if (SignedAuthPack != NULL)
  1336. {
  1337. MIDL_user_free(SignedAuthPack);
  1338. }
  1339. if ( FreePkCreds )
  1340. {
  1341. KerbReleasePkCreds(
  1342. NULL,
  1343. Credentials->PublicKeyCreds,
  1344. FALSE
  1345. );
  1346. }
  1347. return(Status);
  1348. }
  1349. //+-------------------------------------------------------------------------
  1350. //
  1351. // Function: KerbRetrieveDomainFromDn
  1352. //
  1353. // Synopsis: Looks for a subject DN in a smartcard, then calls DsCrackName
  1354. // (local, no wire traffic) to determine the domain name.
  1355. //
  1356. // Effects: Allocates a unicode string
  1357. //
  1358. // Arguments: IN PCERT_CONTEXT smartcard cert
  1359. // IN OUT PUNICODE_STRING CrackedName (free w/ KerbFreeString)
  1360. //
  1361. // Requires:
  1362. //
  1363. // Returns:
  1364. //
  1365. // Notes: Returns STATUS_NOT_FOUND if the name is missing
  1366. //
  1367. //
  1368. //--------------------------------------------------------------------------
  1369. BOOLEAN
  1370. KerbRetrieveDomainFromDn(
  1371. IN PCCERT_CONTEXT Cert,
  1372. IN OUT PUNICODE_STRING CrackedDomain
  1373. )
  1374. {
  1375. LPWSTR RDN = NULL;
  1376. NTSTATUS Status;
  1377. PCERT_NAME_INFO NameInfo = NULL;
  1378. DWORD Result, NameInfoLength = 0;
  1379. LONG i, LastPart = 0, FirstPart = 0xFFFFFFFF;
  1380. PWCHAR tmp;
  1381. UNICODE_STRING TmpString;
  1382. BOOLEAN fRet = FALSE;
  1383. if (!CryptDecodeObjectEx(
  1384. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1385. X509_UNICODE_NAME,
  1386. Cert->pCertInfo->Subject.pbData,
  1387. Cert->pCertInfo->Subject.cbData,
  1388. CRYPT_DECODE_ALLOC_FLAG,
  1389. NULL,
  1390. &NameInfo,
  1391. &NameInfoLength
  1392. ))
  1393. {
  1394. Result = GetLastError();
  1395. DebugLog((DEB_ERROR, "CryptDecodeObject failed %x\n", Result));
  1396. goto Cleanup;
  1397. }
  1398. //
  1399. // Now walk through the NameInfoStruct, looking for DC=
  1400. //
  1401. NameInfoLength = 0;
  1402. for ( i = 0; i < (LONG) NameInfo->cRDN ; i ++)
  1403. {
  1404. if (strcmp(NameInfo->rgRDN[i].rgRDNAttr->pszObjId,szOID_DOMAIN_COMPONENT))
  1405. {
  1406. continue;
  1407. }
  1408. if (FirstPart == 0xFFFFFFFF)
  1409. {
  1410. FirstPart = i;
  1411. }
  1412. NameInfoLength += NameInfo->rgRDN[i].rgRDNAttr->Value.cbData + sizeof(WCHAR);
  1413. LastPart = i;
  1414. }
  1415. if (FirstPart == 0xFFFFFFFF)
  1416. {
  1417. DebugLog((DEB_ERROR, "No DC component in RDN\n"));
  1418. KerbReportMissingRDN();
  1419. DsysAssert(FALSE);
  1420. goto Cleanup;
  1421. }
  1422. SafeAllocaAllocate(RDN, NameInfoLength);
  1423. if ( RDN == NULL )
  1424. {
  1425. goto Cleanup;
  1426. }
  1427. //
  1428. // The names are in reverse order, e.g. DC=COM, DC=MICROSOFT, DC=NTDEV
  1429. //
  1430. tmp = (PWCHAR) RDN;
  1431. for ( i = LastPart ; i >= 0 ; i-- )
  1432. {
  1433. RtlCopyMemory(
  1434. tmp,
  1435. NameInfo->rgRDN[i].rgRDNAttr->Value.pbData,
  1436. NameInfo->rgRDN[i].rgRDNAttr->Value.cbData
  1437. );
  1438. tmp += ( NameInfo->rgRDN[i].rgRDNAttr->Value.cbData / sizeof(WCHAR) );
  1439. if (i != FirstPart)
  1440. {
  1441. *tmp = L'.';
  1442. tmp++;
  1443. }
  1444. else
  1445. {
  1446. *tmp = L'\0';
  1447. }
  1448. }
  1449. RtlInitUnicodeString(
  1450. &TmpString,
  1451. RDN
  1452. );
  1453. Status = KerbDuplicateStringEx(
  1454. CrackedDomain,
  1455. &TmpString,
  1456. FALSE
  1457. );
  1458. if (!NT_SUCCESS(Status))
  1459. {
  1460. goto Cleanup;
  1461. }
  1462. fRet = TRUE;
  1463. Cleanup:
  1464. if ( NameInfo )
  1465. {
  1466. LocalFree( NameInfo );
  1467. }
  1468. SafeAllocaFree(RDN);
  1469. return (fRet);
  1470. }
  1471. //+-------------------------------------------------------------------------
  1472. //
  1473. // Function: KerbCreateSmartCardLogonSessionFromCertContext
  1474. //
  1475. // Synopsis: Creats a logon session from the cert context and passed in
  1476. // data. Retrieves the email name from the certificate.
  1477. //
  1478. // This function is for use with LogonUser when a marshalled
  1479. // smart card cert is passed in the user name and the PIN is
  1480. // passed as the password.
  1481. //
  1482. // Effects:
  1483. //
  1484. // Arguments:
  1485. //
  1486. // Requires:
  1487. //
  1488. // Returns:
  1489. //
  1490. // Notes:
  1491. //
  1492. //
  1493. //--------------------------------------------------------------------------
  1494. NTSTATUS
  1495. KerbCreateSmartCardLogonSessionFromCertContext(
  1496. IN PCERT_CONTEXT *ppCertContext,
  1497. IN PLUID pLogonId,
  1498. IN PUNICODE_STRING pAuthorityName,
  1499. IN PUNICODE_STRING pPin,
  1500. IN PUCHAR pCspData,
  1501. IN ULONG CspDataLength,
  1502. OUT PKERB_LOGON_SESSION *ppLogonSession,
  1503. OUT PUNICODE_STRING pAccountName
  1504. )
  1505. {
  1506. PKERB_LOGON_SESSION pLogonSession = NULL;
  1507. PKERB_PUBLIC_KEY_CREDENTIALS PkCredentials = NULL;
  1508. ULONG cbPkCreds = 0;
  1509. NTSTATUS Status = STATUS_SUCCESS;
  1510. //
  1511. // Get the client name from the cert.
  1512. // Place it in the return location
  1513. //
  1514. Status = KerbGetPrincipalNameFromCertificate(*ppCertContext, pAccountName);
  1515. if (!NT_SUCCESS(Status))
  1516. {
  1517. goto Cleanup;
  1518. }
  1519. //
  1520. // Create a normal logon session. We willa add the public-key information
  1521. // later
  1522. //
  1523. Status = KerbCreateLogonSession(
  1524. pLogonId,
  1525. pAccountName,
  1526. pAuthorityName,
  1527. NULL, // no password
  1528. NULL, // no old password
  1529. 0, // no flags
  1530. KERB_LOGON_SMARTCARD,
  1531. FALSE, // don't allow dup. This is a primary logon.
  1532. &pLogonSession
  1533. );
  1534. if (!NT_SUCCESS(Status))
  1535. {
  1536. goto Cleanup;
  1537. }
  1538. //
  1539. // Now create the public key credentials to be put in the logon
  1540. // session.
  1541. //
  1542. cbPkCreds = sizeof(KERB_PUBLIC_KEY_CREDENTIALS);
  1543. if ((NULL != pCspData) && (0 != CspDataLength))
  1544. {
  1545. cbPkCreds += CspDataLength;
  1546. }
  1547. PkCredentials = (PKERB_PUBLIC_KEY_CREDENTIALS) KerbAllocate(cbPkCreds);
  1548. if (PkCredentials == NULL)
  1549. {
  1550. Status = STATUS_INSUFFICIENT_RESOURCES;
  1551. goto Cleanup;
  1552. }
  1553. PkCredentials->CertContext = *ppCertContext;
  1554. *ppCertContext = NULL;
  1555. Status = KerbDuplicateString(
  1556. &PkCredentials->Pin,
  1557. pPin
  1558. );
  1559. if (!NT_SUCCESS(Status))
  1560. {
  1561. goto Cleanup;
  1562. }
  1563. KerbHidePassword(&PkCredentials->Pin);
  1564. //
  1565. // Copy in the CSP data for later use
  1566. //
  1567. if ((NULL != pCspData) && (0 != CspDataLength))
  1568. {
  1569. PkCredentials->CspDataLength = CspDataLength;
  1570. RtlCopyMemory(
  1571. PkCredentials->CspData,
  1572. pCspData,
  1573. CspDataLength
  1574. );
  1575. PkCredentials->InitializationInfo |= CSP_DATA_INITIALIZED;
  1576. }
  1577. else
  1578. {
  1579. PkCredentials->InitializationInfo |= CSP_DATA_INITIALIZED | CONTEXT_INITIALIZED_WITH_ACH;
  1580. }
  1581. KerbWriteLockLogonSessions(pLogonSession);
  1582. pLogonSession->PrimaryCredentials.PublicKeyCreds = PkCredentials;
  1583. PkCredentials = NULL;
  1584. KerbUnlockLogonSessions(pLogonSession);
  1585. *ppLogonSession = pLogonSession;
  1586. pLogonSession = NULL;
  1587. Cleanup:
  1588. if (*ppCertContext != NULL)
  1589. {
  1590. CertFreeCertificateContext(*ppCertContext);
  1591. }
  1592. KerbFreePKCreds(PkCredentials, FALSE);
  1593. if (pLogonSession != NULL)
  1594. {
  1595. KerbReferenceLogonSessionByPointer(pLogonSession, TRUE);
  1596. KerbDereferenceLogonSession(pLogonSession);
  1597. KerbDereferenceLogonSession(pLogonSession);
  1598. }
  1599. return Status;
  1600. }
  1601. //+-------------------------------------------------------------------------
  1602. //
  1603. // Function: KerbMapCertChainError
  1604. //
  1605. // Synopsis: We don't have good winerrors for chaining //
  1606. // Effects:
  1607. //
  1608. // Arguments:
  1609. //
  1610. // Requires:
  1611. //
  1612. // Returns:
  1613. //
  1614. // Notes:
  1615. //
  1616. //
  1617. //--------------------------------------------------------------------------
  1618. NTSTATUS
  1619. KerbMapCertChainError(ULONG ChainStatus, BOOLEAN Client)
  1620. {
  1621. NTSTATUS Status;
  1622. switch(ChainStatus)
  1623. {
  1624. case CRYPT_E_REVOKED:
  1625. Status = (Client ? STATUS_SMARTCARD_CERT_REVOKED : STATUS_KDC_CERT_REVOKED);
  1626. break;
  1627. case CERT_E_EXPIRED:
  1628. Status = (Client ? STATUS_SMARTCARD_CERT_EXPIRED : STATUS_KDC_CERT_EXPIRED);
  1629. break;
  1630. case CERT_E_UNTRUSTEDCA:
  1631. case CERT_E_UNTRUSTEDROOT:
  1632. Status = (Client ? STATUS_ISSUING_CA_UNTRUSTED : STATUS_ISSUING_CA_UNTRUSTED_KDC);
  1633. break;
  1634. case CRYPT_E_REVOCATION_OFFLINE:
  1635. Status = (Client ? STATUS_REVOCATION_OFFLINE_C : STATUS_REVOCATION_OFFLINE_KDC);
  1636. break;
  1637. // W2k or old whistler DC
  1638. case ERROR_NOT_SUPPORTED:
  1639. default:
  1640. Status = (Client ? STATUS_PKINIT_CLIENT_FAILURE : STATUS_PKINIT_FAILURE);
  1641. }
  1642. return Status;
  1643. }
  1644. //+-------------------------------------------------------------------------
  1645. //
  1646. // Function: KerbCreateSmardCardLogonSession
  1647. //
  1648. // Synopsis: Creats a logon session from the smart card logon info. It
  1649. // creates a certificate context from the logon information,
  1650. // retrieves the email name from the certificate, and then
  1651. // uses that to create a context.
  1652. //
  1653. // Effects:
  1654. //
  1655. // Arguments:
  1656. //
  1657. // Requires:
  1658. //
  1659. // Returns:
  1660. //
  1661. // Notes:
  1662. //
  1663. //
  1664. //--------------------------------------------------------------------------
  1665. NTSTATUS
  1666. KerbCreateSmartCardLogonSession(
  1667. IN PVOID ProtocolSubmitBuffer,
  1668. IN PVOID ClientBufferBase,
  1669. IN ULONG SubmitBufferSize,
  1670. IN SECURITY_LOGON_TYPE LogonType,
  1671. OUT PKERB_LOGON_SESSION *ReturnedLogonSession,
  1672. OUT PLUID ReturnedLogonId,
  1673. OUT PUNICODE_STRING AccountName,
  1674. OUT PUNICODE_STRING AuthorityName
  1675. )
  1676. {
  1677. PCERT_CONTEXT CertContext = NULL;
  1678. NTSTATUS Status = STATUS_SUCCESS;
  1679. PKERB_SMART_CARD_LOGON LogonInfo = (PKERB_SMART_CARD_LOGON) ProtocolSubmitBuffer;
  1680. LUID LogonId = {0};
  1681. BOOLEAN InitializedContext = FALSE;
  1682. //
  1683. // We were passed a blob of data. First we need to update the pointers
  1684. // to be in this address space
  1685. //
  1686. RELOCATE_ONE(&LogonInfo->Pin);
  1687. LogonInfo->CspData = LogonInfo->CspData - (ULONG_PTR) ClientBufferBase + (ULONG_PTR) LogonInfo;
  1688. //
  1689. // Make sure it all fits in our address space
  1690. //
  1691. if ((LogonInfo->CspDataLength + LogonInfo->CspData) >
  1692. ((PUCHAR) ProtocolSubmitBuffer + SubmitBufferSize))
  1693. {
  1694. Status = STATUS_INVALID_PARAMETER;
  1695. goto Cleanup;
  1696. }
  1697. //
  1698. // First, initialize the crypt context
  1699. //
  1700. Status = __ScHelperInitializeContext(
  1701. LogonInfo->CspData,
  1702. LogonInfo->CspDataLength
  1703. );
  1704. if (!NT_SUCCESS(Status))
  1705. {
  1706. DebugLog((DEB_ERROR,"Failed to initialize context from csp data: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  1707. goto Cleanup;
  1708. }
  1709. InitializedContext = TRUE;
  1710. //
  1711. // The first thing to do is to convert the CSP data into a certificate
  1712. // context
  1713. //
  1714. Status = __ScHelperGetCertFromLogonInfo(
  1715. LogonInfo->CspData,
  1716. &LogonInfo->Pin,
  1717. (PCCERT_CONTEXT*)&CertContext
  1718. );
  1719. if (Status != STATUS_SUCCESS)
  1720. {
  1721. DebugLog((DEB_ERROR,"Failed to get cert from logon info: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  1722. if (NT_SUCCESS(Status))
  1723. {
  1724. Status = STATUS_LOGON_FAILURE;
  1725. }
  1726. goto Cleanup;
  1727. }
  1728. RtlInitUnicodeString(
  1729. AuthorityName,
  1730. NULL
  1731. );
  1732. //
  1733. // Now we have just about everything to create a logon session
  1734. //
  1735. Status = NtAllocateLocallyUniqueId( &LogonId );
  1736. if (!NT_SUCCESS(Status))
  1737. {
  1738. DebugLog((DEB_ERROR,"Failed to allocate locally unique ID: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  1739. goto Cleanup;
  1740. }
  1741. //
  1742. // For win95, if there is a logon session in our list, remove it.
  1743. // This is generated from the logon session dumped in the registry.
  1744. // But, we are about to do a new logon. Get rid of the old logon.
  1745. // If the new one does not succeed, too bad. But, that's by design.
  1746. //
  1747. #ifdef WIN32_CHICAGO
  1748. LsaApLogonTerminated(&LogonId);
  1749. #endif // WIN32_CHICAGO
  1750. Status = KerbCreateSmartCardLogonSessionFromCertContext(
  1751. &CertContext,
  1752. &LogonId,
  1753. AuthorityName,
  1754. &LogonInfo->Pin,
  1755. LogonInfo->CspData,
  1756. LogonInfo->CspDataLength,
  1757. ReturnedLogonSession,
  1758. AccountName
  1759. );
  1760. if (!NT_SUCCESS(Status))
  1761. {
  1762. goto Cleanup;
  1763. }
  1764. LogonInfo->CspDataLength = 0;
  1765. *ReturnedLogonId = LogonId;
  1766. Cleanup:
  1767. if (InitializedContext && LogonInfo->CspDataLength != 0)
  1768. {
  1769. __ScHelperRelease(
  1770. LogonInfo->CspData
  1771. );
  1772. }
  1773. return(Status);
  1774. }
  1775. //+-------------------------------------------------------------------------
  1776. //
  1777. // Function: KerbGetCertificateName
  1778. //
  1779. // Synopsis: Gets a name from a certificate name blob. The name is:
  1780. // subject@issuer
  1781. //
  1782. // Effects:
  1783. //
  1784. // Arguments:
  1785. //
  1786. // Requires:
  1787. //
  1788. // Returns:
  1789. //
  1790. // Notes:
  1791. //
  1792. //
  1793. //--------------------------------------------------------------------------
  1794. NTSTATUS
  1795. KerbGetCertificateName(
  1796. OUT PUNICODE_STRING Name,
  1797. IN PCERT_INFO Certificate
  1798. )
  1799. {
  1800. NTSTATUS Status = STATUS_SUCCESS;
  1801. ULONG IssuerLength;
  1802. ULONG SubjectLength;
  1803. RtlInitUnicodeString(
  1804. Name,
  1805. NULL
  1806. );
  1807. //
  1808. // First find the size of the name. The lengths include the
  1809. // null terminators.
  1810. //
  1811. SubjectLength = CertNameToStr(
  1812. X509_ASN_ENCODING,
  1813. &Certificate->Subject,
  1814. CERT_X500_NAME_STR,
  1815. NULL,
  1816. 0
  1817. );
  1818. if (SubjectLength == 0)
  1819. {
  1820. DebugLog((DEB_ERROR,"Failed to convert name: %0x%x. %ws, line %d\n",GetLastError(), THIS_FILE, __LINE__));
  1821. Status = STATUS_PKINIT_FAILURE;
  1822. goto Cleanup;
  1823. }
  1824. IssuerLength = CertNameToStr(
  1825. X509_ASN_ENCODING,
  1826. &Certificate->Issuer,
  1827. CERT_X500_NAME_STR,
  1828. NULL,
  1829. 0
  1830. );
  1831. if (IssuerLength == 0)
  1832. {
  1833. DebugLog((DEB_ERROR,"Failed to convert name: %0x%x. %ws, line %d\n",GetLastError(), THIS_FILE, __LINE__));
  1834. Status = STATUS_PKINIT_FAILURE;
  1835. goto Cleanup;
  1836. }
  1837. //
  1838. // Remove the null terminator from one name, but leave space for a
  1839. // ":" in the middle
  1840. //
  1841. Name->Buffer = (LPWSTR) KerbAllocate((SubjectLength + IssuerLength) * sizeof(WCHAR));
  1842. if (Name->Buffer == NULL)
  1843. {
  1844. Status = STATUS_INSUFFICIENT_RESOURCES;
  1845. goto Cleanup;
  1846. }
  1847. //
  1848. // Now get the name itself
  1849. //
  1850. SubjectLength = CertNameToStr(
  1851. X509_ASN_ENCODING,
  1852. &Certificate->Subject,
  1853. CERT_X500_NAME_STR,
  1854. Name->Buffer,
  1855. SubjectLength
  1856. );
  1857. if (SubjectLength == 0)
  1858. {
  1859. DebugLog((DEB_ERROR,"Failed to convert name: %0x%x. %ws, line %d\n",GetLastError(), THIS_FILE, __LINE__));
  1860. KerbFree(Name->Buffer);
  1861. Name->Buffer = NULL;
  1862. Status = STATUS_PKINIT_FAILURE;
  1863. goto Cleanup;
  1864. }
  1865. //
  1866. // Put an "@" in the middle so it is recognized by MSV as a UPN (just in case)
  1867. //
  1868. Name->Buffer[SubjectLength-1] = L'@';
  1869. IssuerLength = CertNameToStr(
  1870. X509_ASN_ENCODING,
  1871. &Certificate->Issuer,
  1872. CERT_X500_NAME_STR,
  1873. Name->Buffer + SubjectLength,
  1874. IssuerLength
  1875. );
  1876. if (IssuerLength == 0)
  1877. {
  1878. DebugLog((DEB_ERROR,"Failed to convert name: %0x%x. %ws, line %d\n",GetLastError(), THIS_FILE, __LINE__));
  1879. KerbFree(Name->Buffer);
  1880. Name->Buffer = NULL;
  1881. Status = STATUS_PKINIT_FAILURE;
  1882. goto Cleanup;
  1883. }
  1884. RtlInitUnicodeString(
  1885. Name,
  1886. Name->Buffer
  1887. );
  1888. Cleanup:
  1889. return(Status);
  1890. }
  1891. //+-------------------------------------------------------------------------
  1892. //
  1893. // Function: KerbLookupSmartCardCachedLogon
  1894. //
  1895. // Synopsis: Looks up a cached smart card logon in the MSV cache
  1896. //
  1897. // Effects:
  1898. //
  1899. // Arguments:
  1900. //
  1901. // Requires:
  1902. //
  1903. // Returns:
  1904. //
  1905. // Notes: Free ValidationInfor with LocalFree
  1906. //
  1907. //
  1908. //--------------------------------------------------------------------------
  1909. BOOLEAN
  1910. KerbLookupSmartCardCachedLogon(
  1911. IN PCCERT_CONTEXT Certificate,
  1912. OUT PNETLOGON_VALIDATION_SAM_INFO4 * ValidationInfo,
  1913. OUT PKERB_MESSAGE_BUFFER SupplementalCreds
  1914. )
  1915. {
  1916. NTSTATUS Status = STATUS_SUCCESS;
  1917. UNICODE_STRING IssuerName = {0};
  1918. PMSV1_0_CACHE_LOOKUP_REQUEST CacheRequest = NULL;
  1919. PMSV1_0_CACHE_LOOKUP_RESPONSE CacheResponse = NULL;
  1920. UNICODE_STRING MsvPackageName = CONSTANT_UNICODE_STRING(TEXT(MSV1_0_PACKAGE_NAME));
  1921. NTSTATUS SubStatus = STATUS_SUCCESS;
  1922. ULONG OutputBufferSize = 0;
  1923. ULONG RequestSize = 0;
  1924. BOOLEAN Result = FALSE;
  1925. SupplementalCreds->BufferSize = 0;
  1926. SupplementalCreds->Buffer = NULL;
  1927. RequestSize = sizeof( MSV1_0_CACHE_LOOKUP_REQUEST ) +
  1928. SHA1DIGESTLEN -
  1929. sizeof( UCHAR );
  1930. SafeAllocaAllocate(CacheRequest, RequestSize);
  1931. if ( CacheRequest == NULL )
  1932. {
  1933. return( FALSE );
  1934. }
  1935. RtlZeroMemory(CacheRequest, RequestSize);
  1936. *ValidationInfo = NULL;
  1937. //
  1938. // Get the issuer & subject name from the cert. These will be used as
  1939. // user name & domain name for the lookup
  1940. //
  1941. Status = KerbGetCertificateName(
  1942. &IssuerName,
  1943. Certificate->pCertInfo
  1944. );
  1945. if (NT_SUCCESS(Status))
  1946. {
  1947. Status = KerbGetCertificateHash(
  1948. CacheRequest->CredentialSubmitBuffer,
  1949. SHA1DIGESTLEN,
  1950. Certificate
  1951. );
  1952. }
  1953. if (!NT_SUCCESS(Status))
  1954. {
  1955. goto Cleanup;
  1956. }
  1957. CacheRequest->MessageType = MsV1_0CacheLookup;
  1958. CacheRequest->UserName = IssuerName;
  1959. CacheRequest->CredentialType = MSV1_0_CACHE_LOOKUP_CREDTYPE_RAW;
  1960. CacheRequest->CredentialInfoLength = SHA1DIGESTLEN;
  1961. //
  1962. // Leave the domain name portion blank.
  1963. //
  1964. //
  1965. // Call MSV1_0 to do the work
  1966. //
  1967. Status = LsaFunctions->CallPackage(
  1968. &MsvPackageName,
  1969. CacheRequest,
  1970. RequestSize,
  1971. (PVOID *) &CacheResponse,
  1972. &OutputBufferSize,
  1973. &SubStatus
  1974. );
  1975. if (!NT_SUCCESS(Status) || !NT_SUCCESS(SubStatus))
  1976. {
  1977. DebugLog((DEB_ERROR, "Failed to lookup cache credentials: 0x%x, 0x%x. %ws, line %d\n", Status, SubStatus, THIS_FILE, __LINE__));
  1978. goto Cleanup;
  1979. }
  1980. if (OutputBufferSize < sizeof(MSV1_0_CACHE_LOOKUP_RESPONSE))
  1981. {
  1982. DebugLog((DEB_ERROR, "Invalid response from cache lookup - return too small: %d bytes. %ws, line %d\n",
  1983. OutputBufferSize, THIS_FILE, __LINE__ ));
  1984. //
  1985. // Free it here so we don't do too much freeing in the cleanup portion.
  1986. // Don't free the internals as this is pretty bad.
  1987. //
  1988. LsaFunctions->FreeReturnBuffer(CacheResponse);
  1989. CacheResponse = NULL;
  1990. goto Cleanup;
  1991. }
  1992. if (CacheResponse->MessageType != MsV1_0CacheLookup)
  1993. {
  1994. DebugLog((DEB_ERROR, "Wrong message type from cache lookup: %d. %ws, line %d\n",
  1995. CacheResponse->MessageType, THIS_FILE, __LINE__ ));
  1996. //
  1997. // Free it here so we don't do too much freeing in the cleanup portion.
  1998. // Don't free the internals as this is pretty bad.
  1999. //
  2000. LsaFunctions->FreeReturnBuffer(CacheResponse);
  2001. CacheResponse = NULL;
  2002. goto Cleanup;
  2003. }
  2004. *ValidationInfo = (PNETLOGON_VALIDATION_SAM_INFO4) CacheResponse->ValidationInformation;
  2005. CacheResponse->ValidationInformation = NULL;
  2006. SupplementalCreds->Buffer = (PBYTE) CacheResponse->SupplementalCacheData;
  2007. SupplementalCreds->BufferSize = CacheResponse->SupplementalCacheDataLength;
  2008. CacheResponse->SupplementalCacheData = NULL;
  2009. Result = TRUE;
  2010. Cleanup:
  2011. SafeAllocaFree(CacheRequest);
  2012. if (CacheResponse != NULL)
  2013. {
  2014. //
  2015. // At this point we know it was a valid cache response, so we
  2016. // can free the validation info if it is present. NTLM uses
  2017. // MIDL_user_allocate to allocate these.
  2018. //
  2019. if (CacheResponse->ValidationInformation != NULL)
  2020. {
  2021. MIDL_user_free(CacheResponse->ValidationInformation);
  2022. }
  2023. if (CacheResponse->SupplementalCacheData != NULL)
  2024. {
  2025. MIDL_user_free(CacheResponse->SupplementalCacheData);
  2026. }
  2027. LsaFunctions->FreeReturnBuffer(CacheResponse);
  2028. }
  2029. KerbFreeString(&IssuerName);
  2030. return(Result);
  2031. }
  2032. //+-------------------------------------------------------------------------
  2033. //
  2034. // Function: KerbDoLocalSmartCardLogon
  2035. //
  2036. // Synopsis: Performs a local logon with the smart card by validating the
  2037. // card and PIN & then trying to map the name locally
  2038. //
  2039. // Effects:
  2040. //
  2041. // Arguments:
  2042. //
  2043. // Requires:
  2044. //
  2045. // Returns:
  2046. //
  2047. // Notes:
  2048. //
  2049. //
  2050. //--------------------------------------------------------------------------
  2051. NTSTATUS
  2052. KerbDoLocalSmartCardLogon(
  2053. IN PKERB_LOGON_SESSION LogonSession,
  2054. OUT PLSA_TOKEN_INFORMATION_TYPE TokenInformationType,
  2055. OUT PVOID *NewTokenInformation,
  2056. OUT PULONG ProfileBufferLength,
  2057. OUT PVOID * ProfileBuffer,
  2058. OUT PSECPKG_PRIMARY_CRED PrimaryCredentials,
  2059. OUT PSECPKG_SUPPLEMENTAL_CRED_ARRAY * CachedCredentials,
  2060. IN OUT PNETLOGON_VALIDATION_SAM_INFO4 * Validation4
  2061. )
  2062. {
  2063. NTSTATUS Status = STATUS_SUCCESS;
  2064. #ifndef WIN32_CHICAGO
  2065. PPACTYPE Pac = NULL;
  2066. PPAC_INFO_BUFFER LogonInfo = NULL;
  2067. PNETLOGON_VALIDATION_SAM_INFO3 ValidationInfo = NULL;
  2068. PNETLOGON_VALIDATION_SAM_INFO4 MsvValidationInfo = NULL;
  2069. PNETLOGON_VALIDATION_SAM_INFO3 PacValidationInfo = NULL;
  2070. PLSA_TOKEN_INFORMATION_V2 TokenInformation = NULL;
  2071. KERB_MESSAGE_BUFFER SupplementalCreds = {0};
  2072. #endif // !WIN32_CHICAGO
  2073. PKERB_INTERNAL_NAME ClientName = NULL;
  2074. PKERB_PUBLIC_KEY_CREDENTIALS PkCreds;
  2075. PBYTE DecryptedCreds = NULL;
  2076. ULONG DecryptedCredSize = 0;
  2077. NETLOGON_VALIDATION_SAM_INFO3 ValidationInfo3 = {0};
  2078. *Validation4 = NULL;
  2079. PrimaryCredentials->Flags = 0;
  2080. PkCreds = LogonSession->PrimaryCredentials.PublicKeyCreds;
  2081. //
  2082. // First, verify the card. This will verify the certificate as well
  2083. // as verify the PIN & that the ceritifcate matches the private key on
  2084. // the card.
  2085. //
  2086. if ((PkCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  2087. {
  2088. Status = KerbInitializePkCreds(
  2089. PkCreds
  2090. );
  2091. if (!NT_SUCCESS(Status))
  2092. {
  2093. goto Cleanup;
  2094. }
  2095. }
  2096. //
  2097. // Now build a PAC for the user
  2098. //
  2099. if (!KERB_SUCCESS(KerbConvertStringToKdcName(
  2100. &ClientName,
  2101. &LogonSession->PrimaryCredentials.UserName
  2102. )))
  2103. {
  2104. Status = STATUS_INSUFFICIENT_RESOURCES;
  2105. goto Cleanup;
  2106. }
  2107. #ifndef WIN32_CHICAGO
  2108. //
  2109. // First check for a cached logon entry
  2110. //
  2111. if (KerbLookupSmartCardCachedLogon(
  2112. PkCreds->CertContext,
  2113. &MsvValidationInfo,
  2114. &SupplementalCreds))
  2115. {
  2116. MsvValidationInfo->UserFlags |= LOGON_CACHED_ACCOUNT;
  2117. PrimaryCredentials->Flags |= PRIMARY_CRED_CACHED_LOGON;
  2118. //
  2119. // Strip the domain postfix
  2120. //
  2121. if (MsvValidationInfo->LogonDomainName.Length >= KERB_SCLOGON_DOMAIN_SUFFIX_SIZE)
  2122. {
  2123. MsvValidationInfo->LogonDomainName.Length -= KERB_SCLOGON_DOMAIN_SUFFIX_SIZE;
  2124. }
  2125. if ((SupplementalCreds.Buffer != NULL) &&
  2126. (SupplementalCreds.BufferSize != 0))
  2127. {
  2128. DecryptedCredSize = SupplementalCreds.BufferSize;
  2129. DecryptedCreds = (PBYTE) MIDL_user_allocate(DecryptedCredSize);
  2130. if (DecryptedCreds == NULL)
  2131. {
  2132. Status = STATUS_INSUFFICIENT_RESOURCES;
  2133. goto Cleanup;
  2134. }
  2135. }
  2136. //
  2137. // NOTE: this makes use of the fact that _INFO3 and _INFO4 structures
  2138. // expand the _INFO2 structure in different ways, so we're keeping the
  2139. // common portion. The rest of _INFO3 is zeroed out in the declaration,
  2140. // so no ZeroMemory() call is necessary here
  2141. //
  2142. RtlCopyMemory(
  2143. &ValidationInfo3,
  2144. MsvValidationInfo,
  2145. sizeof( NETLOGON_VALIDATION_SAM_INFO2 )
  2146. );
  2147. ValidationInfo = &ValidationInfo3;
  2148. }
  2149. else
  2150. {
  2151. //
  2152. // Look for a name mapping
  2153. //
  2154. Status = KerbCreatePacForKerbClient(
  2155. &Pac,
  2156. ClientName,
  2157. &LogonSession->PrimaryCredentials.DomainName,
  2158. NULL
  2159. );
  2160. if (!NT_SUCCESS(Status))
  2161. {
  2162. goto Cleanup;
  2163. }
  2164. //
  2165. // Find the SAM validation info
  2166. //
  2167. LogonInfo = PAC_Find(
  2168. Pac,
  2169. PAC_LOGON_INFO,
  2170. NULL
  2171. );
  2172. if (LogonInfo == NULL)
  2173. {
  2174. DebugLog((DEB_ERROR, "Failed to find logon info! %ws, line %d\n", THIS_FILE, __LINE__));
  2175. Status = STATUS_INVALID_PARAMETER;
  2176. goto Cleanup;
  2177. }
  2178. //
  2179. // Now unmarshall the validation info
  2180. //
  2181. Status = PAC_UnmarshallValidationInfo(
  2182. &PacValidationInfo,
  2183. LogonInfo->Data,
  2184. LogonInfo->cbBufferSize
  2185. );
  2186. if (!NT_SUCCESS(Status))
  2187. {
  2188. DebugLog((DEB_ERROR, "Failed to unmarshall validation info: 0x%x. %ws, line %d\n",
  2189. Status, THIS_FILE, __LINE__));
  2190. goto Cleanup;
  2191. }
  2192. ValidationInfo = PacValidationInfo;
  2193. }
  2194. KerbRevealPassword(&PkCreds->Pin);
  2195. Status = __ScHelperVerifyCardAndCreds(
  2196. &PkCreds->Pin,
  2197. PkCreds->CertContext,
  2198. PkCreds->CspData,
  2199. SupplementalCreds.Buffer,
  2200. SupplementalCreds.BufferSize,
  2201. DecryptedCreds,
  2202. &DecryptedCredSize
  2203. );
  2204. KerbHidePassword(&PkCreds->Pin);
  2205. if (!NT_SUCCESS(Status))
  2206. {
  2207. DebugLog((DEB_ERROR, "Failed to verify card: 0x%x. %ws, line %d\n", Status, THIS_FILE, __LINE__));
  2208. goto Cleanup;
  2209. }
  2210. //
  2211. // If we have any encrypted credentials, decode them here for return.
  2212. //
  2213. if (DecryptedCredSize != 0)
  2214. {
  2215. Status = PAC_UnmarshallCredentials(
  2216. CachedCredentials,
  2217. DecryptedCreds,
  2218. DecryptedCredSize
  2219. );
  2220. if (!NT_SUCCESS(Status))
  2221. {
  2222. goto Cleanup;
  2223. }
  2224. }
  2225. //
  2226. // Check to see if this is a non-user account. If so, don't allow the logon
  2227. //
  2228. if ((ValidationInfo->ExpansionRoom[SAMINFO_USER_ACCOUNT_CONTROL] & USER_MACHINE_ACCOUNT_MASK) != 0)
  2229. {
  2230. DebugLog((DEB_ERROR, "Logons to non-user accounts not allowed. UserAccountControl = 0x%x\n",
  2231. ValidationInfo->ExpansionRoom[SAMINFO_USER_ACCOUNT_CONTROL] ));
  2232. Status = STATUS_LOGON_TYPE_NOT_GRANTED;
  2233. goto Cleanup;
  2234. }
  2235. //
  2236. // Now we need to build a LSA_TOKEN_INFORMATION_V2 from the validation
  2237. // information
  2238. //
  2239. Status = KerbMakeTokenInformationV2(
  2240. ValidationInfo,
  2241. FALSE, // not local system
  2242. &TokenInformation
  2243. );
  2244. if (!NT_SUCCESS(Status))
  2245. {
  2246. DebugLog((DEB_ERROR, "Failed to make token informatin v2: 0x%x\n",
  2247. Status));
  2248. goto Cleanup;
  2249. }
  2250. //
  2251. // Allocate the client profile
  2252. //
  2253. Status = KerbAllocateInteractiveProfile(
  2254. (PKERB_INTERACTIVE_PROFILE *) ProfileBuffer,
  2255. ProfileBufferLength,
  2256. ValidationInfo,
  2257. LogonSession,
  2258. NULL,
  2259. NULL
  2260. );
  2261. if (!KERB_SUCCESS(Status))
  2262. {
  2263. goto Cleanup;
  2264. }
  2265. //
  2266. // Build the primary credential. We let someone else fill in the
  2267. // password.
  2268. //
  2269. PrimaryCredentials->LogonId = LogonSession->LogonId;
  2270. Status = KerbDuplicateString(
  2271. &PrimaryCredentials->DownlevelName,
  2272. &ValidationInfo->EffectiveName
  2273. );
  2274. if (!NT_SUCCESS(Status))
  2275. {
  2276. goto Cleanup;
  2277. }
  2278. Status = KerbDuplicateString(
  2279. &PrimaryCredentials->DomainName,
  2280. &ValidationInfo->LogonDomainName
  2281. );
  2282. if (!NT_SUCCESS(Status))
  2283. {
  2284. goto Cleanup;
  2285. }
  2286. Status = KerbDuplicateSid(
  2287. &PrimaryCredentials->UserSid,
  2288. TokenInformation->User.User.Sid
  2289. );
  2290. if (!NT_SUCCESS(Status))
  2291. {
  2292. goto Cleanup;
  2293. }
  2294. *Validation4 = MsvValidationInfo;
  2295. MsvValidationInfo = NULL;
  2296. *NewTokenInformation = TokenInformation;
  2297. *TokenInformationType = LsaTokenInformationV2;
  2298. #endif // !WIN32_CHICAGO
  2299. Cleanup:
  2300. if (PacValidationInfo != NULL)
  2301. {
  2302. MIDL_user_free(PacValidationInfo);
  2303. }
  2304. KerbFreeKdcName(
  2305. &ClientName
  2306. );
  2307. if (MsvValidationInfo != NULL)
  2308. {
  2309. MIDL_user_free(MsvValidationInfo);
  2310. MsvValidationInfo = NULL;
  2311. }
  2312. if (SupplementalCreds.Buffer != NULL)
  2313. {
  2314. MIDL_user_free(SupplementalCreds.Buffer);
  2315. SupplementalCreds.Buffer = NULL;
  2316. }
  2317. #ifndef WIN32_CHICAGO
  2318. if (Pac != NULL)
  2319. {
  2320. MIDL_user_free(Pac);
  2321. }
  2322. if (!NT_SUCCESS(Status))
  2323. {
  2324. if (TokenInformation != NULL)
  2325. {
  2326. KerbFree( TokenInformation );
  2327. }
  2328. if (*ProfileBuffer != NULL)
  2329. {
  2330. LsaFunctions->FreeClientBuffer(NULL, *ProfileBuffer);
  2331. *ProfileBuffer = NULL;
  2332. }
  2333. KerbFreeString(
  2334. &PrimaryCredentials->DownlevelName
  2335. );
  2336. KerbFreeString(
  2337. &PrimaryCredentials->DomainName
  2338. );
  2339. if (PrimaryCredentials->UserSid != NULL)
  2340. {
  2341. KerbFree(PrimaryCredentials->UserSid);
  2342. PrimaryCredentials->UserSid = NULL;
  2343. }
  2344. }
  2345. #endif // WIN32_CHICAGO
  2346. return(Status);
  2347. }
  2348. //+-------------------------------------------------------------------------
  2349. //
  2350. // Function: KerbCacheSmartCardLogon
  2351. //
  2352. // Synopsis:
  2353. //
  2354. // Effects:
  2355. //
  2356. // Arguments:
  2357. //
  2358. // Requires:
  2359. //
  2360. // Returns:
  2361. //
  2362. // Notes:
  2363. //
  2364. //
  2365. //--------------------------------------------------------------------------
  2366. VOID
  2367. KerbCacheSmartCardLogon(
  2368. IN PNETLOGON_VALIDATION_SAM_INFO3 ValidationInfo,
  2369. IN OPTIONAL PUNICODE_STRING DnsDomainName,
  2370. IN OPTIONAL PUNICODE_STRING UPN,
  2371. IN PKERB_LOGON_SESSION LogonSession,
  2372. IN OPTIONAL PSECPKG_SUPPLEMENTAL_CRED_ARRAY CachedCredentials
  2373. )
  2374. {
  2375. NTSTATUS Status;
  2376. UNICODE_STRING IssuerName = {0};
  2377. UNICODE_STRING DomainName = {0};
  2378. UNICODE_STRING TempLogonDomainName = {0};
  2379. UNICODE_STRING LogonDomainName = {0};
  2380. BYTE CertificateHash[ SHA1DIGESTLEN ];
  2381. UNICODE_STRING CertificateHashString;
  2382. ULONG EncodedCredSize = 0;
  2383. PBYTE EncodedCreds = NULL;
  2384. ULONG EncryptedCredSize = 0;
  2385. PBYTE EncryptedCreds = NULL;
  2386. BOOLEAN LogonSessionLocked = FALSE;
  2387. BOOLEAN InitializedPkCreds = FALSE;
  2388. BOOLEAN CleartextPin = FALSE;
  2389. //
  2390. // Build the temporary logon domain name that indicates this is a
  2391. // smart card logon.
  2392. //
  2393. TempLogonDomainName.MaximumLength =
  2394. TempLogonDomainName.Length =
  2395. ValidationInfo->LogonDomainName.Length + KERB_SCLOGON_DOMAIN_SUFFIX_SIZE;
  2396. TempLogonDomainName.Buffer = (LPWSTR) MIDL_user_allocate(TempLogonDomainName.Length);
  2397. if (TempLogonDomainName.Buffer == NULL)
  2398. {
  2399. goto Cleanup;
  2400. }
  2401. //
  2402. // Create the new name
  2403. //
  2404. RtlCopyMemory(
  2405. TempLogonDomainName.Buffer,
  2406. ValidationInfo->LogonDomainName.Buffer,
  2407. ValidationInfo->LogonDomainName.Length
  2408. );
  2409. RtlCopyMemory(
  2410. ((PUCHAR) TempLogonDomainName.Buffer) + ValidationInfo->LogonDomainName.Length,
  2411. KERB_SCLOGON_DOMAIN_SUFFIX,
  2412. KERB_SCLOGON_DOMAIN_SUFFIX_SIZE
  2413. );
  2414. LogonDomainName = ValidationInfo->LogonDomainName;
  2415. ValidationInfo->LogonDomainName = TempLogonDomainName;
  2416. //
  2417. // Get the name under which to store this.
  2418. //
  2419. DsysAssert( !LogonSessionLocked );
  2420. KerbReadLockLogonSessions(LogonSession);
  2421. LogonSessionLocked = TRUE;
  2422. Status = KerbGetCertificateName(
  2423. &IssuerName,
  2424. LogonSession->PrimaryCredentials.PublicKeyCreds->CertContext->pCertInfo
  2425. );
  2426. if ( Status == STATUS_SUCCESS )
  2427. {
  2428. Status = KerbGetCertificateHash(
  2429. CertificateHash,
  2430. SHA1DIGESTLEN,
  2431. LogonSession->PrimaryCredentials.PublicKeyCreds->CertContext
  2432. );
  2433. }
  2434. if (!NT_SUCCESS(Status))
  2435. {
  2436. goto Cleanup;
  2437. }
  2438. CertificateHashString.Length = SHA1DIGESTLEN;
  2439. CertificateHashString.Buffer = (LPWSTR)CertificateHash;
  2440. CertificateHashString.MaximumLength = SHA1DIGESTLEN;
  2441. if (ARGUMENT_PRESENT(CachedCredentials))
  2442. {
  2443. ScHelper_RandomCredBits RandomBits;
  2444. Status = PAC_EncodeCredentialData(
  2445. CachedCredentials,
  2446. &EncodedCreds,
  2447. &EncodedCredSize
  2448. );
  2449. if (!NT_SUCCESS(Status))
  2450. {
  2451. goto Cleanup;
  2452. }
  2453. if ((LogonSession->PrimaryCredentials.PublicKeyCreds->InitializationInfo & CSP_DATA_INITIALIZED) == 0)
  2454. {
  2455. Status = KerbInitializePkCreds(
  2456. LogonSession->PrimaryCredentials.PublicKeyCreds
  2457. );
  2458. if (!NT_SUCCESS(Status))
  2459. {
  2460. goto Cleanup;
  2461. }
  2462. InitializedPkCreds = TRUE;
  2463. }
  2464. Status = __ScHelperGenRandBits(
  2465. LogonSession->PrimaryCredentials.PublicKeyCreds->CspData,
  2466. &RandomBits
  2467. );
  2468. if (!NT_SUCCESS(Status))
  2469. {
  2470. DebugLog((DEB_ERROR,"Failed to generate random bits: 0x%x\n",Status));
  2471. goto Cleanup;
  2472. }
  2473. KerbRevealPassword( &LogonSession->PrimaryCredentials.PublicKeyCreds->Pin );
  2474. CleartextPin = TRUE;
  2475. Status = __ScHelperEncryptCredentials(
  2476. &LogonSession->PrimaryCredentials.PublicKeyCreds->Pin,
  2477. LogonSession->PrimaryCredentials.PublicKeyCreds->CertContext,
  2478. &RandomBits,
  2479. LogonSession->PrimaryCredentials.PublicKeyCreds->CspData,
  2480. EncodedCreds,
  2481. EncodedCredSize,
  2482. NULL,
  2483. &EncryptedCredSize
  2484. );
  2485. if ((Status != STATUS_SUCCESS) && (Status != STATUS_BUFFER_TOO_SMALL))
  2486. {
  2487. DebugLog((DEB_ERROR,"Failed to encrypt creds: 0x%x\n",Status));
  2488. goto Cleanup;
  2489. }
  2490. EncryptedCreds = (PBYTE) KerbAllocate(EncryptedCredSize);
  2491. if (EncryptedCreds == NULL)
  2492. {
  2493. Status = STATUS_INSUFFICIENT_RESOURCES;
  2494. goto Cleanup;
  2495. }
  2496. //
  2497. // Do the real encryption
  2498. //
  2499. Status = __ScHelperEncryptCredentials(
  2500. &LogonSession->PrimaryCredentials.PublicKeyCreds->Pin,
  2501. LogonSession->PrimaryCredentials.PublicKeyCreds->CertContext,
  2502. &RandomBits,
  2503. LogonSession->PrimaryCredentials.PublicKeyCreds->CspData,
  2504. EncodedCreds,
  2505. EncodedCredSize,
  2506. EncryptedCreds,
  2507. &EncryptedCredSize
  2508. );
  2509. if (Status != STATUS_SUCCESS)
  2510. {
  2511. DebugLog((DEB_ERROR,"Failed to encrypt creds: 0x%x\n",Status));
  2512. goto Cleanup;
  2513. }
  2514. }
  2515. DsysAssert( LogonSessionLocked );
  2516. KerbUnlockLogonSessions(LogonSession);
  2517. LogonSessionLocked = FALSE;
  2518. KerbCacheLogonInformation(
  2519. &IssuerName, // used as username
  2520. &DomainName, // blank - no domain
  2521. &CertificateHashString, // password is certificate hash,
  2522. DnsDomainName,
  2523. NULL, // UPN,
  2524. NULL, // not MIT realm logon, do not need to pass in LogonSession
  2525. MSV1_0_CACHE_LOGON_REQUEST_SMARTCARD_ONLY, // smartcard only
  2526. ValidationInfo,
  2527. EncryptedCreds,
  2528. EncryptedCredSize
  2529. );
  2530. Cleanup:
  2531. if (CleartextPin)
  2532. {
  2533. KerbHidePassword(&LogonSession->PrimaryCredentials.PublicKeyCreds->Pin);
  2534. }
  2535. if (InitializedPkCreds)
  2536. {
  2537. KerbFreePKCreds(
  2538. LogonSession->PrimaryCredentials.PublicKeyCreds,
  2539. FALSE
  2540. );
  2541. }
  2542. if (LogonSessionLocked)
  2543. {
  2544. KerbUnlockLogonSessions(LogonSession);
  2545. }
  2546. KerbFreeString(&IssuerName);
  2547. KerbFreeString(&TempLogonDomainName);
  2548. //
  2549. // Restore the original logon domain name
  2550. //
  2551. if (LogonDomainName.Buffer != NULL)
  2552. {
  2553. ValidationInfo->LogonDomainName = LogonDomainName;
  2554. }
  2555. if (EncodedCreds != NULL)
  2556. {
  2557. MIDL_user_free(EncodedCreds);
  2558. }
  2559. }
  2560. //+-------------------------------------------------------------------------
  2561. //
  2562. // Function: KerbInitializePkinit
  2563. //
  2564. // Synopsis: Inializes structures needed for PKINIT
  2565. //
  2566. // Effects:
  2567. //
  2568. // Arguments:
  2569. //
  2570. // Requires:
  2571. //
  2572. // Returns:
  2573. //
  2574. // Notes:
  2575. //
  2576. //
  2577. //--------------------------------------------------------------------------
  2578. NTSTATUS
  2579. KerbInitializePkinit(
  2580. VOID
  2581. )
  2582. {
  2583. ULONG Index;
  2584. LPSTR StringCopy = NULL, TempString = NULL,EndPtr = NULL;
  2585. //
  2586. // Initialize the object IDs
  2587. //
  2588. Index = 0;
  2589. SafeAllocaAllocate(StringCopy, (ULONG) strlen(KERB_PKINIT_SIGNATURE_OID) + 1);
  2590. if (StringCopy == NULL)
  2591. {
  2592. return( STATUS_INSUFFICIENT_RESOURCES);
  2593. }
  2594. //
  2595. // Scan the string for every '.' separated number
  2596. //
  2597. strcpy(
  2598. StringCopy,
  2599. KERB_PKINIT_SIGNATURE_OID
  2600. );
  2601. TempString = StringCopy;
  2602. EndPtr = TempString;
  2603. while (TempString != NULL)
  2604. {
  2605. ULONG Temp;
  2606. while (*EndPtr != '\0' && *EndPtr != '.')
  2607. {
  2608. EndPtr++;
  2609. }
  2610. if (*EndPtr == '.')
  2611. {
  2612. *EndPtr = '\0';
  2613. EndPtr++;
  2614. }
  2615. else
  2616. {
  2617. EndPtr = NULL;
  2618. }
  2619. if (0 == sscanf(TempString,"%u",&Temp))
  2620. {
  2621. return STATUS_INSUFFICIENT_RESOURCES;
  2622. }
  2623. KerbSignatureAlg[Index].value = (USHORT) Temp;
  2624. KerbSignatureAlg[Index].next = &KerbSignatureAlg[Index+1];
  2625. Index++;
  2626. TempString = EndPtr;
  2627. }
  2628. DsysAssert(Index != 0);
  2629. KerbSignatureAlg[Index-1].next = NULL;
  2630. SafeAllocaFree(StringCopy);
  2631. TempString = NULL;
  2632. return(STATUS_SUCCESS);
  2633. }