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.

4042 lines
113 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: ssl3.c
  7. //
  8. // Contents: Ssl3 protocol handling functions
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 8-08-95 Ramas Created
  15. // 1-14-97 Ramas Rewritten
  16. //
  17. //----------------------------------------------------------------------------
  18. #include <spbase.h>
  19. #include <_ssl3cli.h>
  20. #include <time.h>
  21. DWORD g_Ssl3CertTypes[] = { SSL3_CERTTYPE_RSA_SIGN,
  22. SSL3_CERTTYPE_DSS_SIGN};
  23. DWORD g_cSsl3CertTypes = sizeof(g_Ssl3CertTypes) / sizeof(DWORD);
  24. SP_STATUS WINAPI
  25. Ssl3ClientProtocolHandler(
  26. PSPContext pContext,
  27. PSPBuffer pCommInput,
  28. PSPBuffer pCommOutput);
  29. SP_STATUS
  30. UpdateAndDuplicateIssuerList(
  31. PSPCredentialGroup pCredGroup,
  32. PBYTE * ppbIssuerList,
  33. PDWORD pcbIssuerList);
  34. SP_STATUS WINAPI
  35. Ssl3ProtocolHandler(
  36. PSPContext pContext,
  37. PSPBuffer pCommInput,
  38. PSPBuffer pCommOutput)
  39. {
  40. SPBuffer MsgInput;
  41. SP_STATUS pctRet;
  42. DWORD cbInputData = 0;
  43. if(pContext->Flags & CONTEXT_FLAG_CONNECTION_MODE)
  44. {
  45. do
  46. {
  47. MsgInput.pvBuffer = (PUCHAR) pCommInput->pvBuffer + cbInputData;
  48. MsgInput.cbData = pCommInput->cbData - cbInputData;
  49. MsgInput.cbBuffer = pCommInput->cbBuffer - cbInputData;
  50. pctRet = Ssl3ClientProtocolHandler(pContext,
  51. &MsgInput,
  52. pCommOutput);
  53. cbInputData += MsgInput.cbData;
  54. if(SP_STATE_CONNECTED == pContext->State)
  55. {
  56. break;
  57. }
  58. if(PCT_ERR_OK != pctRet)
  59. {
  60. break;
  61. }
  62. } while(pCommInput->cbData - cbInputData);
  63. pCommInput->cbData = cbInputData;
  64. }
  65. else
  66. {
  67. pctRet = Ssl3ClientProtocolHandler(pContext,
  68. pCommInput,
  69. pCommOutput);
  70. }
  71. return(pctRet);
  72. }
  73. /*
  74. ***************************************************************************
  75. * Ssl3ProtocolHandler
  76. * Main Entry point for handling ssl3 type handshake messages...
  77. ****************************************************************************
  78. */
  79. SP_STATUS WINAPI
  80. Ssl3ClientProtocolHandler
  81. (
  82. PSPContext pContext, // in; state changes and temp data stored
  83. PSPBuffer pCommInput, // in: decrypted in-place...
  84. PSPBuffer pCommOutput) // out
  85. {
  86. SP_STATUS pctRet = PCT_ERR_OK;
  87. DWORD dwState;
  88. DWORD cbMsg;
  89. BYTE bContentType;
  90. BOOL fServer = (pContext->dwProtocol & SP_PROT_SERVERS);
  91. BOOL fProcessMultiple = FALSE;
  92. PBYTE pbData;
  93. DWORD cbData;
  94. DWORD cbBytesProcessed = 0;
  95. DWORD dwVersion;
  96. DWORD cbDecryptedMsg;
  97. if(NULL != pCommOutput)
  98. {
  99. pCommOutput->cbData = 0;
  100. }
  101. dwState = (pContext->State & 0xffff);
  102. if(FNoInputState(dwState))
  103. {
  104. // Process no input cases...
  105. goto GenResponse;
  106. }
  107. if(pContext->State == UNI_STATE_RECVD_UNIHELLO)
  108. {
  109. // We've just received a unified client_hello message.
  110. // This always consists of a single SSL2-format handshake
  111. // message.
  112. if(pCommInput->cbData < 3)
  113. {
  114. return(PCT_INT_INCOMPLETE_MSG);
  115. }
  116. bContentType = UNI_STATE_RECVD_UNIHELLO;
  117. pbData = pCommInput->pvBuffer;
  118. cbData = pCommInput->cbData;
  119. cbDecryptedMsg = cbData;
  120. cbMsg = cbData;
  121. goto Process;
  122. }
  123. //
  124. // The input buffer should contain one or more SSL3-format
  125. // messages.
  126. //
  127. if(pCommInput->cbData < CB_SSL3_HEADER_SIZE)
  128. {
  129. return (PCT_INT_INCOMPLETE_MSG);
  130. }
  131. //
  132. // If there are multiple messages in the input buffer, and
  133. // these messages exactly fill the buffer, then we should
  134. // process all of the messages during this call. If there
  135. // are any fractions, then we should just process the first
  136. // message.
  137. //
  138. pbData = pCommInput->pvBuffer;
  139. cbData = pCommInput->cbData;
  140. while(TRUE)
  141. {
  142. if(cbData < CB_SSL3_HEADER_SIZE)
  143. {
  144. break;
  145. }
  146. bContentType = pbData[0];
  147. if(bContentType != SSL3_CT_CHANGE_CIPHER_SPEC &&
  148. bContentType != SSL3_CT_ALERT &&
  149. bContentType != SSL3_CT_HANDSHAKE &&
  150. bContentType != SSL3_CT_APPLICATIONDATA)
  151. {
  152. break;
  153. }
  154. dwVersion = COMBINEBYTES(pbData[1], pbData[2]);
  155. if(dwVersion != SSL3_CLIENT_VERSION &&
  156. dwVersion != TLS1_CLIENT_VERSION)
  157. {
  158. break;
  159. }
  160. cbMsg = COMBINEBYTES(pbData[3], pbData[4]);
  161. cbDecryptedMsg = cbMsg;
  162. if(CB_SSL3_HEADER_SIZE + cbMsg > cbData)
  163. {
  164. break;
  165. }
  166. pbData += CB_SSL3_HEADER_SIZE + cbMsg;
  167. cbData -= CB_SSL3_HEADER_SIZE + cbMsg;
  168. if(cbData == 0)
  169. {
  170. fProcessMultiple = TRUE;
  171. break;
  172. }
  173. }
  174. //
  175. // Step through the messages in the input buffer, processing
  176. // each one in turn.
  177. //
  178. pbData = pCommInput->pvBuffer;
  179. cbData = pCommInput->cbData;
  180. while(TRUE)
  181. {
  182. //
  183. // Validate the message.
  184. //
  185. if(cbData < CB_SSL3_HEADER_SIZE)
  186. {
  187. return (PCT_INT_INCOMPLETE_MSG);
  188. }
  189. bContentType = pbData[0];
  190. if(bContentType != SSL3_CT_CHANGE_CIPHER_SPEC &&
  191. bContentType != SSL3_CT_ALERT &&
  192. bContentType != SSL3_CT_HANDSHAKE &&
  193. bContentType != SSL3_CT_APPLICATIONDATA)
  194. {
  195. return SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  196. }
  197. cbMsg = COMBINEBYTES(pbData[3], pbData[4]);
  198. cbDecryptedMsg = cbMsg;
  199. if(CB_SSL3_HEADER_SIZE + cbMsg > cbData)
  200. {
  201. return (PCT_INT_INCOMPLETE_MSG);
  202. }
  203. cbBytesProcessed += CB_SSL3_HEADER_SIZE + cbMsg;
  204. pCommInput->cbData = cbBytesProcessed;
  205. //
  206. // Decrypt the message.
  207. //
  208. if(FSsl3Cipher(fServer))
  209. {
  210. SPBuffer Message;
  211. Message.cbBuffer = CB_SSL3_HEADER_SIZE + cbMsg;
  212. Message.cbData = CB_SSL3_HEADER_SIZE + cbMsg;
  213. Message.pvBuffer = pbData;
  214. // Decrypt the message.
  215. pctRet = UnwrapSsl3Message(pContext, &Message);
  216. // if we have to send ALERT messages to the peer, build it!
  217. if(TLS1_STATE_ERROR == pContext->State)
  218. {
  219. goto GenResponse;
  220. }
  221. if(pctRet != PCT_ERR_OK)
  222. {
  223. return pctRet;
  224. }
  225. cbDecryptedMsg = COMBINEBYTES(pbData[3], pbData[4]);
  226. }
  227. pbData += CB_SSL3_HEADER_SIZE;
  228. cbData -= CB_SSL3_HEADER_SIZE;
  229. Process:
  230. pctRet = SPProcessMessage(pContext, bContentType, pbData, cbDecryptedMsg) ;
  231. if(pctRet != PCT_ERR_OK)
  232. {
  233. return pctRet;
  234. }
  235. pbData += cbMsg;
  236. cbData -= cbMsg;
  237. // If a response is required at this state then break out of the
  238. // message processing loop.
  239. if(F_RESPONSE(pContext->State))
  240. {
  241. GenResponse:
  242. if(pContext->State > SSL3_STATE_GEN_START)
  243. {
  244. pctRet = SPGenerateResponse(pContext, pCommOutput);
  245. }
  246. return pctRet;
  247. }
  248. // If the handshake is complete then stop processing messages.
  249. // We don't want to accidentally process any application data
  250. // messages.
  251. if(pContext->State == SP_STATE_CONNECTED)
  252. {
  253. break;
  254. }
  255. if(fProcessMultiple && cbData > 0)
  256. {
  257. continue;
  258. }
  259. break;
  260. }
  261. return pctRet;
  262. }
  263. /*
  264. ***************************************************************************
  265. * Ssl3HandleFinish
  266. * Handle the handshake finished message..
  267. ****************************************************************************
  268. */
  269. SP_STATUS
  270. Ssl3HandleFinish(
  271. PSPContext pContext,
  272. PBYTE pbMsg, // in
  273. DWORD cbMsg, // in
  274. BOOL fClient) // in
  275. {
  276. BYTE rgbDigest[CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN];
  277. SP_STATUS pctRet = PCT_ERR_OK;
  278. PBYTE pb = pbMsg;
  279. DWORD dwSize;
  280. DWORD dwSizeExpect = CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN;
  281. SP_BEGIN("Ssl3HandleFinish");
  282. //is this the right message type
  283. if(*pb != SSL3_HS_FINISHED)
  284. {
  285. pctRet = SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  286. goto error;
  287. }
  288. if(pContext->RipeZombie->fProtocol & SP_PROT_TLS1)
  289. {
  290. dwSizeExpect = CB_TLS1_VERIFYDATA;
  291. }
  292. dwSize = ((INT)pb[1] << 16) + ((INT)pb[2] << 8) + (INT)pb[3];
  293. pb += sizeof(SHSH);
  294. // Is the payload the size that we expect?
  295. if(dwSize != dwSizeExpect)
  296. {
  297. pctRet = SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  298. goto error;
  299. }
  300. // Is the overall message size what we expect? Don't allow buffer overrun!
  301. if(sizeof(SHSH) + dwSize != cbMsg)
  302. {
  303. pctRet = SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  304. goto error;
  305. }
  306. // Build our end finish message to compare
  307. if(pContext->RipeZombie->fProtocol & SP_PROT_SSL3)
  308. {
  309. pctRet = Ssl3BuildFinishMessage(pContext,
  310. rgbDigest,
  311. &rgbDigest[CB_MD5_DIGEST_LEN],
  312. fClient);
  313. }
  314. else
  315. {
  316. pctRet = Tls1BuildFinishMessage(pContext,
  317. rgbDigest,
  318. sizeof(rgbDigest),
  319. fClient);
  320. }
  321. if(pctRet != PCT_ERR_OK)
  322. {
  323. goto error;
  324. }
  325. // compare the two...
  326. if (memcmp(rgbDigest, pb, dwSizeExpect))
  327. {
  328. DebugLog((DEB_WARN, "Finished MAC didn't matchChecksum Invalid\n"));
  329. pctRet = SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  330. goto error;
  331. }
  332. SP_RETURN(PCT_ERR_OK);
  333. error:
  334. SP_RETURN(pctRet);
  335. }
  336. /*
  337. ***************************************************************************
  338. * Ssl3PackClientHello
  339. ****************************************************************************
  340. */
  341. SP_STATUS
  342. Ssl3PackClientHello(
  343. PSPContext pContext,
  344. PSsl2_Client_Hello pCanonical,
  345. PSPBuffer pCommOutput)
  346. {
  347. DWORD cbHandshake;
  348. DWORD cbMessage;
  349. PBYTE pbMessage = NULL;
  350. DWORD dwCipherSize;
  351. DWORD i;
  352. BOOL fAllocated = FALSE;
  353. //
  354. // opaque SessionID<0..32>;
  355. //
  356. // struct {
  357. // ProtocolVersion client_version;
  358. // Random random;
  359. // SessionID session_id;
  360. // CipherSuite cipher_suites<2..2^16-1>;
  361. // CompressionMethod compression_methods<1..2^8-1>;
  362. // } ClientHello;
  363. //
  364. SP_BEGIN("Ssl3PackClientHello");
  365. if(pCanonical == NULL || pCommOutput == NULL)
  366. {
  367. SP_RETURN(PCT_INT_INTERNAL_ERROR);
  368. }
  369. // Compute size of the ClientHello message.
  370. cbHandshake = sizeof(SHSH) +
  371. 2 +
  372. CB_SSL3_RANDOM +
  373. 1 + pCanonical->cbSessionID +
  374. 2 + pCanonical->cCipherSpecs * sizeof(short) +
  375. 2; // Size of compression algorithm 1 + null (0)
  376. // Compute size of encrypted ClientHello message.
  377. cbMessage = Ssl3CiphertextLen(pContext,
  378. cbHandshake,
  379. TRUE);
  380. if(pCommOutput->pvBuffer)
  381. {
  382. // Application has allocated memory.
  383. if(pCommOutput->cbBuffer < cbMessage)
  384. {
  385. pCommOutput->cbData = cbMessage;
  386. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  387. }
  388. fAllocated = TRUE;
  389. }
  390. else
  391. {
  392. // Schannel is to allocate memory.
  393. pCommOutput->cbBuffer = cbMessage;
  394. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  395. if(pCommOutput->pvBuffer == NULL)
  396. {
  397. return SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  398. }
  399. }
  400. pCommOutput->cbData = cbMessage;
  401. // Initialize the member variables.
  402. pbMessage = (PBYTE)pCommOutput->pvBuffer + sizeof(SWRAP) + sizeof(SHSH);
  403. *pbMessage++ = MSBOF(pCanonical->dwVer);
  404. *pbMessage++ = LSBOF(pCanonical->dwVer);
  405. CopyMemory(pbMessage, pCanonical->Challenge, CB_SSL3_RANDOM);
  406. pbMessage += CB_SSL3_RANDOM;
  407. *pbMessage++ = (BYTE)pCanonical->cbSessionID;
  408. CopyMemory(pbMessage, pCanonical->SessionID, pCanonical->cbSessionID);
  409. pbMessage += pCanonical->cbSessionID;
  410. dwCipherSize = pCanonical->cCipherSpecs * sizeof(short);
  411. *pbMessage++ = MSBOF(dwCipherSize);
  412. *pbMessage++ = LSBOF(dwCipherSize);
  413. for(i = 0; i < pCanonical->cCipherSpecs; i++)
  414. {
  415. *pbMessage++ = MSBOF(pCanonical->CipherSpecs[i]);
  416. *pbMessage++ = LSBOF(pCanonical->CipherSpecs[i]);
  417. }
  418. *pbMessage++ = 1; // One compression method;
  419. *pbMessage++ = 0x00; // NULL compression method.
  420. // Fill in Handshake structure.
  421. SetHandshake((PBYTE)pCommOutput->pvBuffer + sizeof(SWRAP),
  422. SSL3_HS_CLIENT_HELLO,
  423. NULL,
  424. (WORD)(cbHandshake - sizeof(SHSH)));
  425. // Save the ClientHello message so we can hash it later, once
  426. // we know what algorithm and CSP we're using.
  427. if(pContext->pClientHello)
  428. {
  429. SPExternalFree(pContext->pClientHello);
  430. }
  431. pContext->cbClientHello = cbHandshake;
  432. pContext->pClientHello = SPExternalAlloc(pContext->cbClientHello);
  433. if(pContext->pClientHello == NULL)
  434. {
  435. SP_RETURN(SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY));
  436. }
  437. CopyMemory(pContext->pClientHello,
  438. (PBYTE)pCommOutput->pvBuffer + sizeof(SWRAP),
  439. pContext->cbClientHello);
  440. pContext->dwClientHelloProtocol = SP_PROT_SSL3_CLIENT;
  441. // Fill in record header and encrypt the message.
  442. SP_RETURN(SPSetWrap(pContext,
  443. pCommOutput->pvBuffer,
  444. SSL3_CT_HANDSHAKE,
  445. cbHandshake,
  446. TRUE,
  447. NULL));
  448. }
  449. //+---------------------------------------------------------------------------
  450. //
  451. // Function: Ssl3GenerateRandom
  452. //
  453. // Synopsis: Create a client_random or server_random value.
  454. //
  455. // Arguments: [pRandom] -- Output buffer.
  456. //
  457. // History: 04-03-2001 jbanes Created.
  458. //
  459. // Notes: struct {
  460. // uint32 gmt_unix_time;
  461. // opaque random_bytes[28];
  462. // } Random;
  463. //
  464. // gmt_unix_time
  465. // The current time and date in standard UNIX 32-bit format
  466. // (seconds since the midnight starting Jan 1, 1970, GMT)
  467. // according to the sender's internal clock. Clocks are not
  468. // required to be set correctly by the basic TLS Protocol;
  469. // higher level or application protocols may define
  470. // additional requirements.
  471. //
  472. // random_bytes
  473. // 28 bytes generated by a secure random number generator.
  474. //
  475. //----------------------------------------------------------------------------
  476. NTSTATUS
  477. Ssl3GenerateRandom(
  478. PBYTE pRandom)
  479. {
  480. time_t UnixTime;
  481. time(&UnixTime);
  482. *(DWORD *)pRandom = htonl((DWORD)UnixTime);
  483. return GenerateRandomBits(pRandom + sizeof(DWORD), CB_SSL3_RANDOM - sizeof(DWORD));
  484. }
  485. /*
  486. ***************************************************************************
  487. * GenerateSsl3ClientHello
  488. * v3 client hello build it on pOutpu
  489. ****************************************************************************
  490. */
  491. SP_STATUS WINAPI
  492. GenerateSsl3ClientHello(
  493. PSPContext pContext,
  494. PSPBuffer pOutput)
  495. {
  496. Ssl2_Client_Hello HelloMessage;
  497. SP_STATUS pctRet;
  498. SP_BEGIN("GenerateSsl3ClientHello");
  499. pctRet = Ssl3GenerateRandom(pContext->pChallenge);
  500. if(NT_SUCCESS(pctRet))
  501. {
  502. pContext->cbChallenge = CB_SSL3_RANDOM;
  503. pctRet = GenerateUniHelloMessage(pContext, &HelloMessage, SP_PROT_SSL3_CLIENT);
  504. if(PCT_ERR_OK == pctRet)
  505. {
  506. pctRet = Ssl3PackClientHello(pContext, &HelloMessage, pOutput);
  507. }
  508. }
  509. SP_RETURN(pctRet);
  510. }
  511. SP_STATUS WINAPI
  512. GenerateTls1ClientHello(
  513. PSPContext pContext,
  514. PSPBuffer pOutput,
  515. DWORD dwProtocol)
  516. {
  517. Ssl2_Client_Hello HelloMessage;
  518. SP_STATUS pctRet;
  519. SP_BEGIN("GenerateTls1ClientHello");
  520. pctRet = Ssl3GenerateRandom(pContext->pChallenge);
  521. if(NT_SUCCESS(pctRet))
  522. {
  523. pContext->cbChallenge = CB_SSL3_RANDOM;
  524. pctRet = GenerateUniHelloMessage(pContext, &HelloMessage, dwProtocol);
  525. if(PCT_ERR_OK == pctRet)
  526. {
  527. pctRet = Ssl3PackClientHello(pContext, &HelloMessage, pOutput);
  528. }
  529. }
  530. SP_RETURN(pctRet);
  531. }
  532. /*
  533. ***************************************************************************
  534. * ParseCertificateRequest
  535. * if server is requesting client-auth, server will send this message.
  536. * parse them and store it in pContext, for later use....
  537. ****************************************************************************
  538. */
  539. SP_STATUS
  540. ParseCertificateRequest(
  541. PSPContext pContext,
  542. PBYTE pb,
  543. DWORD dwcb)
  544. {
  545. SP_STATUS pctRet;
  546. UCHAR cbCertType;
  547. DWORD cbIssuerList;
  548. PBYTE pbNewIssuerList;
  549. DWORD cbNewIssuerList;
  550. UCHAR i, j;
  551. //
  552. // enum {
  553. // rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
  554. // rsa_ephemeral_dh(5), dss_ephemeral_dh(6), fortezza_dms(20), (255)
  555. // } ClientCertificateType;
  556. //
  557. // opaque DistinguishedName<1..2^16-1>;
  558. //
  559. // struct {
  560. // ClientCertificateType certificate_types<1..2^8-1>;
  561. // DistinguishedName certificate_authorities<3..2^16-1>;
  562. // } CertificateRequest;
  563. //
  564. //
  565. // Skip over handshake header.
  566. //
  567. if(dwcb < sizeof(SHSH))
  568. {
  569. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  570. goto cleanup;
  571. }
  572. pb += sizeof(SHSH);
  573. dwcb -= sizeof(SHSH);
  574. //
  575. // Parse certificate type list.
  576. //
  577. if(dwcb < 1)
  578. {
  579. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  580. goto cleanup;
  581. }
  582. cbCertType = pb[0];
  583. pb += 1;
  584. dwcb -= 1;
  585. if(cbCertType > dwcb)
  586. {
  587. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  588. goto cleanup;
  589. }
  590. pContext->cSsl3ClientCertTypes = 0;
  591. for(i = 0; i < cbCertType; i++)
  592. {
  593. for(j = 0; j < g_cSsl3CertTypes; j++)
  594. {
  595. if(g_Ssl3CertTypes[j] == pb[i])
  596. {
  597. pContext->Ssl3ClientCertTypes[pContext->cSsl3ClientCertTypes++] = g_Ssl3CertTypes[j];
  598. }
  599. }
  600. }
  601. pb += cbCertType;
  602. dwcb -= cbCertType;
  603. //
  604. // Parse issuer list.
  605. //
  606. if(dwcb < 2)
  607. {
  608. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  609. goto cleanup;
  610. }
  611. cbIssuerList = COMBINEBYTES(pb[0], pb[1]);
  612. pb += 2;
  613. dwcb -= 2;
  614. if(dwcb < cbIssuerList)
  615. {
  616. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  617. goto cleanup;
  618. }
  619. pctRet = FormatIssuerList(pb,
  620. cbIssuerList,
  621. NULL,
  622. &cbNewIssuerList);
  623. if(pctRet != PCT_ERR_OK)
  624. {
  625. goto cleanup;
  626. }
  627. pbNewIssuerList = SPExternalAlloc(2 + cbNewIssuerList);
  628. if(pbNewIssuerList == NULL)
  629. {
  630. pctRet = SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  631. goto cleanup;
  632. }
  633. pbNewIssuerList[0] = MSBOF(cbNewIssuerList);
  634. pbNewIssuerList[1] = LSBOF(cbNewIssuerList);
  635. pctRet = FormatIssuerList(pb,
  636. cbIssuerList,
  637. pbNewIssuerList + 2,
  638. &cbNewIssuerList);
  639. if(pctRet != PCT_ERR_OK)
  640. {
  641. SPExternalFree(pbNewIssuerList);
  642. goto cleanup;
  643. }
  644. //
  645. // Store issuer list in context structure.
  646. //
  647. if(pContext->pbIssuerList)
  648. {
  649. SPExternalFree(pContext->pbIssuerList);
  650. }
  651. pContext->pbIssuerList = pbNewIssuerList;
  652. pContext->cbIssuerList = cbNewIssuerList + 2;
  653. cleanup:
  654. return (pctRet);
  655. }
  656. /*
  657. ***************************************************************************
  658. * BuildCertVerify
  659. * Build certificate Verify message. This is sent by client if sending
  660. * client certificate.
  661. ****************************************************************************
  662. */
  663. SP_STATUS
  664. BuildCertVerify(
  665. PSPContext pContext,
  666. PBYTE pbCertVerify,
  667. DWORD *pcbCertVerify)
  668. {
  669. SP_STATUS pctRet;
  670. PBYTE pbSigned;
  671. DWORD cbSigned;
  672. BYTE rgbHashValue[CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN];
  673. DWORD cbHashValue;
  674. ALG_ID aiHash;
  675. PBYTE pbMD5;
  676. PBYTE pbSHA;
  677. DWORD cbHeader;
  678. DWORD cbBytesRequired;
  679. PSPCredential pCred;
  680. if((pcbCertVerify == NULL) ||
  681. (pContext == NULL) ||
  682. (pContext->RipeZombie == NULL) ||
  683. (pContext->pActiveClientCred == NULL))
  684. {
  685. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  686. }
  687. pCred = pContext->pActiveClientCred;
  688. //
  689. // digitally-signed struct {
  690. // opaque md5_hash[16];
  691. // opaque sha_hash[20];
  692. // } Signature;
  693. //
  694. // struct {
  695. // Signature signature;
  696. // } CertificateVerify;
  697. //
  698. // CertificateVerify.signature.md5_hash = MD5(master_secret + pad2 +
  699. // MD5(handshake_messages + master_secret + pad1));
  700. //
  701. // CertificateVerify.signature.sha_hash = SHA(master_secret + pad2 +
  702. // SHA(handshake_messages + master_secret + pad1));
  703. //
  704. cbHeader = sizeof(SHSH);
  705. cbBytesRequired = cbHeader +
  706. 2 +
  707. pCred->pPublicKey->cbPublic;
  708. if(pbCertVerify == NULL)
  709. {
  710. *pcbCertVerify = cbBytesRequired;
  711. return PCT_ERR_OK;
  712. }
  713. if(*pcbCertVerify < sizeof(SHSH))
  714. {
  715. *pcbCertVerify = cbBytesRequired;
  716. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  717. }
  718. //
  719. // Generate hash values
  720. //
  721. switch(pCred->pPublicKey->pPublic->aiKeyAlg)
  722. {
  723. case CALG_RSA_SIGN:
  724. case CALG_RSA_KEYX:
  725. aiHash = CALG_SSL3_SHAMD5;
  726. pbMD5 = rgbHashValue;
  727. pbSHA = rgbHashValue + CB_MD5_DIGEST_LEN;
  728. cbHashValue = CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN;
  729. break;
  730. case CALG_DSS_SIGN:
  731. aiHash = CALG_SHA;
  732. pbMD5 = NULL;
  733. pbSHA = rgbHashValue;
  734. cbHashValue = CB_SHA_DIGEST_LEN;
  735. break;
  736. default:
  737. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  738. }
  739. if(pContext->RipeZombie->fProtocol & SP_PROT_TLS1)
  740. {
  741. pctRet = Tls1ComputeCertVerifyHashes(pContext, pbMD5, pbSHA);
  742. }
  743. else
  744. {
  745. pctRet = Ssl3ComputeCertVerifyHashes(pContext, pbMD5, pbSHA);
  746. }
  747. if(pctRet != PCT_ERR_OK)
  748. {
  749. return pctRet;
  750. }
  751. //
  752. // Sign hash values.
  753. //
  754. pbSigned = pbCertVerify + sizeof(SHSH) + 2;
  755. cbSigned = cbBytesRequired - sizeof(SHSH) - 2;
  756. DebugLog((DEB_TRACE, "Sign certificate_verify message.\n"));
  757. pctRet = SignHashUsingCred(pCred,
  758. aiHash,
  759. rgbHashValue,
  760. cbHashValue,
  761. pbSigned,
  762. &cbSigned);
  763. if(pctRet != PCT_ERR_OK)
  764. {
  765. return pctRet;
  766. }
  767. DebugLog((DEB_TRACE, "Certificate_verify message signed successfully.\n"));
  768. //
  769. // Fill in header.
  770. //
  771. pbCertVerify[cbHeader + 0] = MSBOF(cbSigned);
  772. pbCertVerify[cbHeader + 1] = LSBOF(cbSigned);
  773. SetHandshake(pbCertVerify, SSL3_HS_CERTIFICATE_VERIFY, NULL, (WORD)(cbSigned + 2));
  774. *pcbCertVerify = cbHeader + 2 + cbSigned;
  775. return PCT_ERR_OK;
  776. }
  777. SP_STATUS
  778. HandleCertVerify(
  779. PSPContext pContext,
  780. PBYTE pbMessage,
  781. DWORD cbMessage)
  782. {
  783. PBYTE pbSignature;
  784. DWORD cbSignature;
  785. BYTE rgbHashValue[CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN];
  786. DWORD cbHashValue;
  787. HCRYPTPROV hProv;
  788. ALG_ID aiHash;
  789. PBYTE pbMD5;
  790. PBYTE pbSHA;
  791. SP_STATUS pctRet;
  792. if((pContext == NULL) ||
  793. (pContext->RipeZombie == NULL))
  794. {
  795. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  796. }
  797. if(pContext->RipeZombie->pRemotePublic == NULL)
  798. {
  799. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  800. }
  801. if(cbMessage < sizeof(SHSH) + 2)
  802. {
  803. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  804. }
  805. pbSignature = pbMessage + sizeof(SHSH);
  806. cbSignature = ((DWORD)pbSignature[0] << 8) + pbSignature[1];
  807. pbSignature += 2;
  808. if(sizeof(SHSH) + 2 + cbSignature > cbMessage)
  809. {
  810. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  811. }
  812. switch(pContext->RipeZombie->pRemotePublic->pPublic->aiKeyAlg)
  813. {
  814. case CALG_RSA_SIGN:
  815. case CALG_RSA_KEYX:
  816. hProv = g_hRsaSchannel;
  817. aiHash = CALG_SSL3_SHAMD5;
  818. pbMD5 = rgbHashValue;
  819. pbSHA = rgbHashValue + CB_MD5_DIGEST_LEN;
  820. cbHashValue = CB_MD5_DIGEST_LEN + CB_SHA_DIGEST_LEN;
  821. break;
  822. case CALG_DSS_SIGN:
  823. hProv = g_hDhSchannelProv;
  824. aiHash = CALG_SHA;
  825. pbMD5 = NULL;
  826. pbSHA = rgbHashValue;
  827. cbHashValue = CB_SHA_DIGEST_LEN;
  828. break;
  829. default:
  830. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  831. }
  832. if(pContext->RipeZombie->fProtocol & SP_PROT_TLS1)
  833. {
  834. pctRet = Tls1ComputeCertVerifyHashes(pContext, pbMD5, pbSHA);
  835. }
  836. else
  837. {
  838. pctRet = Ssl3ComputeCertVerifyHashes(pContext, pbMD5, pbSHA);
  839. }
  840. if(pctRet != PCT_ERR_OK)
  841. {
  842. return pctRet;
  843. }
  844. // Verify signature.
  845. DebugLog((DEB_TRACE, "Verify certificate_verify signature.\n"));
  846. pctRet = SPVerifySignature(hProv,
  847. pContext->RipeZombie->pRemotePublic,
  848. aiHash,
  849. rgbHashValue,
  850. cbHashValue,
  851. pbSignature,
  852. cbSignature,
  853. FALSE);
  854. if(pctRet != PCT_ERR_OK)
  855. {
  856. return SP_LOG_RESULT(pctRet);
  857. }
  858. DebugLog((DEB_TRACE, "Certificate_verify verified successfully.\n"));
  859. return PCT_ERR_OK;
  860. }
  861. SP_STATUS
  862. FormatIssuerList(
  863. PBYTE pbInput,
  864. DWORD cbInput,
  865. PBYTE pbIssuerList,
  866. DWORD * pcbIssuerList)
  867. {
  868. DWORD cbIssuerList = 0;
  869. PBYTE pbList = pbInput;
  870. DWORD cbList = cbInput;
  871. DWORD cbIssuer;
  872. DWORD cbTag;
  873. while(cbList)
  874. {
  875. if(cbList < 2)
  876. {
  877. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  878. }
  879. cbIssuer = COMBINEBYTES(pbList[0], pbList[1]);
  880. pbList += 2;
  881. cbList -= 2;
  882. if(cbList < cbIssuer)
  883. {
  884. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  885. }
  886. if(pbIssuerList)
  887. {
  888. pbIssuerList[0] = MSBOF(cbIssuer);
  889. pbIssuerList[1] = LSBOF(cbIssuer);
  890. pbIssuerList += 2;
  891. }
  892. cbIssuerList += 2;
  893. if(pbList[0] != SEQUENCE_TAG)
  894. {
  895. // An old version of Netscape Enterprise Server had a bug, in that
  896. // the issuer names did not start off with a SEQUENCE tag. Patch
  897. // the name appropriately before storing it into pContext.
  898. cbTag = CbLenOfEncode(cbIssuer, pbIssuerList);
  899. if(pbIssuerList)
  900. {
  901. pbIssuerList += cbTag;
  902. }
  903. cbIssuerList += cbTag;
  904. }
  905. if(pbIssuerList)
  906. {
  907. memcpy(pbIssuerList, pbList, cbIssuer);
  908. pbIssuerList += cbIssuer;
  909. }
  910. cbIssuerList += cbIssuer;
  911. pbList += cbIssuer;
  912. cbList -= cbIssuer;
  913. }
  914. *pcbIssuerList = cbIssuerList;
  915. return(PCT_ERR_OK);
  916. }
  917. /*
  918. ***************************************************************************
  919. * CbLenOfEncode
  920. * Retunrs the length of the ASN encoding, for certificate
  921. ****************************************************************************
  922. */
  923. DWORD CbLenOfEncode(DWORD dw, PBYTE pbDst)
  924. {
  925. BYTE lenbuf[8];
  926. DWORD length = sizeof(lenbuf) - 1;
  927. LONG lth;
  928. if (0x80 > dw)
  929. {
  930. lenbuf[length] = (BYTE)dw;
  931. lth = 1;
  932. }
  933. else
  934. {
  935. while (0 < dw)
  936. {
  937. lenbuf[length] = (BYTE)(dw & 0xff);
  938. length -= 1;
  939. dw = (dw >> 8) & 0x00ffffff;
  940. }
  941. lth = sizeof(lenbuf) - length;
  942. lenbuf[length] = (BYTE)(0x80 | (lth - 1));
  943. }
  944. if(NULL != pbDst)
  945. {
  946. pbDst[0] = 0x30;
  947. memcpy(pbDst+1, &lenbuf[length], lth);
  948. }
  949. return ++lth; //for 0x30
  950. }
  951. /*
  952. ***************************************************************************
  953. * SPDigestServerHello
  954. * Parse the server hello from the server.
  955. ****************************************************************************
  956. */
  957. SP_STATUS
  958. SPDigestServerHello(
  959. PSPContext pContext,
  960. PUCHAR pbSrvHello,
  961. DWORD cbSrvHello,
  962. PBOOL pfRestart)
  963. {
  964. SSH *pssh;
  965. SP_STATUS pctRet = PCT_ERR_ILLEGAL_MESSAGE;
  966. SHORT wCipher, wCompression;
  967. BOOL fRestartServer = FALSE;
  968. PSessCacheItem pZombie;
  969. PSPCredentialGroup pCred;
  970. DWORD dwVersion;
  971. UNREFERENCED_PARAMETER(cbSrvHello);
  972. // We should have a zombie identity here
  973. if(pContext->RipeZombie == NULL)
  974. {
  975. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  976. }
  977. pZombie = pContext->RipeZombie;
  978. pCred = pContext->pCredGroup;
  979. if(!pCred)
  980. {
  981. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  982. }
  983. SP_BEGIN("SPDigestServerHello");
  984. // Pad the client random structure with zero's if the challenge is
  985. // smaller than the normal SSL3 size (SSL2 v3 hello, Unihello, PCT1 wierdness if
  986. // we add it)
  987. FillMemory(pContext->rgbS3CRandom, CB_SSL3_RANDOM - pContext->cbChallenge, 0);
  988. CopyMemory( pContext->rgbS3CRandom + CB_SSL3_RANDOM - pContext->cbChallenge,
  989. pContext->pChallenge,
  990. pContext->cbChallenge);
  991. pssh = (SSH *)pbSrvHello;
  992. if(pssh->cbSessionId > CB_SSL3_SESSION_ID)
  993. {
  994. SP_RETURN(PCT_ERR_ILLEGAL_MESSAGE);
  995. }
  996. dwVersion = COMBINEBYTES(pssh->bMajor, pssh->bMinor);
  997. if((dwVersion == SSL3_CLIENT_VERSION) &&
  998. (pCred->grbitEnabledProtocols & SP_PROT_SSL3_CLIENT))
  999. {
  1000. // This appears to be an SSL3 server_hello.
  1001. pContext->dwProtocol = SP_PROT_SSL3_CLIENT;
  1002. }
  1003. else if((dwVersion == TLS1_CLIENT_VERSION) &&
  1004. (pCred->grbitEnabledProtocols & SP_PROT_TLS1_CLIENT))
  1005. {
  1006. // This appears to be a TLS server_hello.
  1007. pContext->dwProtocol = SP_PROT_TLS1_CLIENT;
  1008. }
  1009. else
  1010. {
  1011. return SP_LOG_RESULT(PCT_INT_SPECS_MISMATCH);
  1012. }
  1013. DebugLog((DEB_TRACE, "**********Protocol***** %x\n", pContext->dwProtocol));
  1014. CopyMemory(pContext->rgbS3SRandom, pssh->rgbRandom, CB_SSL3_RANDOM);
  1015. wCipher = (SHORT)COMBINEBYTES(pssh->rgbSessionId[pssh->cbSessionId],
  1016. pssh->rgbSessionId[pssh->cbSessionId+1]);
  1017. wCompression = pssh->rgbSessionId[pssh->cbSessionId+2];
  1018. if( wCompression != 0)
  1019. {
  1020. SP_RETURN(PCT_ERR_ILLEGAL_MESSAGE);
  1021. }
  1022. if(pZombie->hMasterKey &&
  1023. pZombie->cbSessionID &&
  1024. memcmp(pZombie->SessionID, PbSessionid(pssh), pssh->cbSessionId) == 0)
  1025. {
  1026. fRestartServer = TRUE;
  1027. if(!pZombie->ZombieJuju)
  1028. {
  1029. DebugLog((DEB_WARN, "Session expired on client machine, but not on server.\n"));
  1030. }
  1031. }
  1032. if(!fRestartServer)
  1033. {
  1034. if(pZombie->hMasterKey != 0)
  1035. {
  1036. // We've attempted to do a reconnect and the server has
  1037. // blown us off. In this case we must use a new and different
  1038. // cache entry.
  1039. pZombie->ZombieJuju = FALSE;
  1040. if(!SPCacheClone(&pContext->RipeZombie))
  1041. {
  1042. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  1043. }
  1044. pZombie = pContext->RipeZombie;
  1045. }
  1046. pZombie->fProtocol = pContext->dwProtocol;
  1047. #if DBG
  1048. if(pssh->cbSessionId == 0)
  1049. {
  1050. DebugLog((DEB_WARN, "Server didn't give us a session id!\n"));
  1051. }
  1052. #endif
  1053. if(pssh->cbSessionId)
  1054. {
  1055. CopyMemory(pZombie->SessionID, PbSessionid(pssh), pssh->cbSessionId);
  1056. pZombie->cbSessionID = pssh->cbSessionId;
  1057. }
  1058. }
  1059. pctRet = Ssl3SelectCipher(pContext, wCipher);
  1060. if(pctRet != PCT_ERR_OK)
  1061. {
  1062. SP_RETURN(SP_LOG_RESULT(pctRet));
  1063. }
  1064. if(fRestartServer)
  1065. {
  1066. // Make a new set of session keys.
  1067. pctRet = MakeSessionKeys(pContext,
  1068. pZombie->hMasterProv,
  1069. pZombie->hMasterKey);
  1070. if(PCT_ERR_OK != pctRet)
  1071. {
  1072. SP_RETURN(SP_LOG_RESULT(pctRet));
  1073. }
  1074. }
  1075. // Initialize handshake hashes and hash ClientHello message. This
  1076. // must be done _after_ the ServerHello message is processed,
  1077. // so that the correct CSP context is used.
  1078. if(pContext->dwClientHelloProtocol == SP_PROT_PCT1_CLIENT ||
  1079. pContext->dwClientHelloProtocol == SP_PROT_SSL2_CLIENT)
  1080. {
  1081. // Skip over the 2 byte header.
  1082. pctRet = UpdateHandshakeHash(pContext,
  1083. pContext->pClientHello + 2,
  1084. pContext->cbClientHello - 2,
  1085. TRUE);
  1086. }
  1087. else
  1088. {
  1089. pctRet = UpdateHandshakeHash(pContext,
  1090. pContext->pClientHello,
  1091. pContext->cbClientHello,
  1092. TRUE);
  1093. }
  1094. SPExternalFree(pContext->pClientHello);
  1095. pContext->pClientHello = NULL;
  1096. pContext->cbClientHello = 0;
  1097. *pfRestart = fRestartServer;
  1098. SP_RETURN(pctRet);
  1099. }
  1100. /*
  1101. ***************************************************************************
  1102. * SpDigestRemoteCertificate
  1103. * Process the Certificate message. This is common for server/client.
  1104. ****************************************************************************
  1105. */
  1106. SP_STATUS
  1107. SpDigestRemoteCertificate (
  1108. PSPContext pContext,
  1109. PUCHAR pb,
  1110. DWORD cb)
  1111. {
  1112. SP_STATUS pctRet = PCT_ERR_OK;
  1113. CERT *pcert;
  1114. DWORD cbCert;
  1115. DWORD dwSize;
  1116. SP_BEGIN("SpDigestRemoteCertificate");
  1117. if(pContext == NULL)
  1118. {
  1119. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  1120. }
  1121. if((pContext->RipeZombie->fProtocol & SP_PROT_SERVERS) && (pContext->fCertReq == FALSE))
  1122. {
  1123. // We're a server and the client just sent us an
  1124. // unexpected certificate message.
  1125. SP_RETURN(SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG));
  1126. }
  1127. pcert = (CERT *)pb;
  1128. if(cb < CB_SSL3_CERT_VECTOR + sizeof(SHSH))
  1129. {
  1130. SP_RETURN(SP_LOG_RESULT(PCT_INT_INCOMPLETE_MSG));
  1131. }
  1132. dwSize = ((INT)pcert->bcb24 << 16) +
  1133. ((INT)pcert->bcbMSB << 8) +
  1134. (INT)pcert->bcbLSB;
  1135. cbCert = COMBINEBYTES(pcert->bcbMSBClist, pcert->bcbLSBClist);
  1136. cbCert |= (pcert->bcbClist24 << 16);
  1137. if(dwSize != cbCert + CB_SSL3_CERT_VECTOR)
  1138. {
  1139. SP_RETURN(SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG));
  1140. }
  1141. if(dwSize + sizeof(SHSH) > cb)
  1142. {
  1143. SP_RETURN(SP_LOG_RESULT(PCT_INT_INCOMPLETE_MSG));
  1144. }
  1145. if(cbCert == 0)
  1146. {
  1147. if(pContext->RipeZombie->fProtocol & SP_PROT_CLIENTS)
  1148. {
  1149. // Error out if the server certificate is zero length
  1150. SP_RETURN(SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG));
  1151. }
  1152. else
  1153. {
  1154. DebugLog((DEB_WARN, "Zero length client certificate received.\n"));
  1155. }
  1156. }
  1157. if(cbCert != 0) //for tls1, it could be zero length.
  1158. {
  1159. // The certificate type is derived from the key exchange method
  1160. // but most currently use X509_ASN_ENCODING
  1161. pctRet = SPLoadCertificate( SP_PROT_SSL3_CLIENT,
  1162. X509_ASN_ENCODING,
  1163. (PUCHAR)&pcert->bcbCert24,
  1164. cbCert,
  1165. &pContext->RipeZombie->pRemoteCert);
  1166. if(PCT_ERR_OK != pctRet)
  1167. {
  1168. SP_RETURN(pctRet);
  1169. }
  1170. if(pContext->RipeZombie->pRemotePublic != NULL)
  1171. {
  1172. SPExternalFree(pContext->RipeZombie->pRemotePublic);
  1173. pContext->RipeZombie->pRemotePublic = NULL;
  1174. }
  1175. pctRet = SPPublicKeyFromCert(pContext->RipeZombie->pRemoteCert,
  1176. &pContext->RipeZombie->pRemotePublic,
  1177. NULL);
  1178. if(PCT_ERR_OK != pctRet)
  1179. {
  1180. SP_RETURN(pctRet);
  1181. }
  1182. }
  1183. SP_RETURN(pctRet);
  1184. }
  1185. /*
  1186. ***************************************************************************
  1187. * SPDigestSrvKeyX
  1188. * Digest the Server key exhcnage message.
  1189. * this function encrypts the Pre-master secret with the public-key from this
  1190. * message
  1191. ****************************************************************************
  1192. */
  1193. SP_STATUS SPDigestSrvKeyX(
  1194. PSPContext pContext,
  1195. PUCHAR pbServerExchangeValue,
  1196. DWORD cbServerExchangeValue)
  1197. {
  1198. SP_STATUS pctRet;
  1199. if((pContext->pKeyExchInfo == NULL) || ((pContext->pKeyExchInfo->fProtocol & SP_PROT_SSL3TLS1_CLIENTS) == 0))
  1200. {
  1201. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  1202. }
  1203. SP_ASSERT(NULL == pContext->pbEncryptedKey);
  1204. pctRet = pContext->pKeyExchInfo->System->GenerateClientExchangeValue(
  1205. pContext,
  1206. pbServerExchangeValue,
  1207. cbServerExchangeValue,
  1208. NULL,
  1209. NULL,
  1210. NULL,
  1211. &pContext->cbEncryptedKey);
  1212. if(pctRet != PCT_ERR_OK)
  1213. {
  1214. return pctRet;
  1215. }
  1216. pContext->pbEncryptedKey = SPExternalAlloc(pContext->cbEncryptedKey);
  1217. if(pContext->pbEncryptedKey == NULL)
  1218. {
  1219. return SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  1220. }
  1221. pctRet = pContext->pKeyExchInfo->System->GenerateClientExchangeValue(
  1222. pContext,
  1223. pbServerExchangeValue,
  1224. cbServerExchangeValue,
  1225. NULL,
  1226. NULL,
  1227. pContext->pbEncryptedKey,
  1228. &pContext->cbEncryptedKey);
  1229. return pctRet;
  1230. }
  1231. //+---------------------------------------------------------------------------
  1232. //
  1233. // Function: Ssl3CheckForExistingCred
  1234. //
  1235. // Synopsis: Choose client certificate. Use one of the certificates
  1236. // attached to the credential handle if possible. If the
  1237. // credential handle is anonymous, then attempt to create
  1238. // a default credential.
  1239. //
  1240. // Notes: This routine is called by the client-side only.
  1241. //
  1242. // Returns: PCT_ERR_OK
  1243. // The function completed successfully. The
  1244. // pContext->pActiveClientCred field has been updated to
  1245. // point at a suitable client credential.
  1246. //
  1247. // SEC_E_INCOMPLETE_CREDENTIALS
  1248. // No suitable certificate has been found. Notify the
  1249. // application.
  1250. //
  1251. // SEC_I_INCOMPLETE_CREDENTIALS
  1252. // No suitable certificate has been found. Attempt an
  1253. // anonymous connection.
  1254. //
  1255. // <other>
  1256. // Fatal error.
  1257. //
  1258. //----------------------------------------------------------------------------
  1259. SP_STATUS
  1260. Ssl3CheckForExistingCred(PSPContext pContext)
  1261. {
  1262. SP_STATUS pctRet;
  1263. //
  1264. // Examine the certificates attached to the credential group and see
  1265. // if any of them are suitable.
  1266. //
  1267. if(pContext->pCredGroup->CredCount != 0)
  1268. {
  1269. DWORD i, j;
  1270. for(i = 0; i < pContext->cSsl3ClientCertTypes; i++)
  1271. {
  1272. for(j = 0; j < g_cCertTypes; j++)
  1273. {
  1274. if(pContext->Ssl3ClientCertTypes[i] != g_CertTypes[j].dwCertType)
  1275. {
  1276. continue;
  1277. }
  1278. pctRet = SPPickClientCertificate(
  1279. pContext,
  1280. g_CertTypes[j].dwExchSpec);
  1281. if(pctRet == PCT_ERR_OK)
  1282. {
  1283. // We found one.
  1284. DebugLog((DEB_TRACE, "Application provided suitable client certificate.\n"));
  1285. return PCT_ERR_OK;
  1286. }
  1287. }
  1288. }
  1289. // The credential group contained one or more certificates,
  1290. // but none were suitable. Don't even try to find a default
  1291. // certificate in this situation.
  1292. goto error;
  1293. }
  1294. //
  1295. // Attempt to acquire a default credential.
  1296. //
  1297. if(pContext->pCredGroup->dwFlags & CRED_FLAG_NO_DEFAULT_CREDS)
  1298. {
  1299. // Look in credential manager only.
  1300. pctRet = AcquireDefaultClientCredential(pContext, TRUE);
  1301. }
  1302. else
  1303. {
  1304. // Look in both credential manager and MY certificate store.
  1305. pctRet = AcquireDefaultClientCredential(pContext, FALSE);
  1306. }
  1307. if(pctRet == PCT_ERR_OK)
  1308. {
  1309. DebugLog((DEB_TRACE, "Default client certificate acquired.\n"));
  1310. return PCT_ERR_OK;
  1311. }
  1312. error:
  1313. if(pContext->Flags & CONTEXT_FLAG_NO_INCOMPLETE_CRED_MSG)
  1314. {
  1315. return SP_LOG_RESULT(SEC_I_INCOMPLETE_CREDENTIALS);
  1316. }
  1317. else
  1318. {
  1319. return SP_LOG_RESULT(SEC_E_INCOMPLETE_CREDENTIALS);
  1320. }
  1321. }
  1322. /*
  1323. ***************************************************************************
  1324. * SPGenerateSHResponse
  1325. * This is the main function which outgoing message to the wire
  1326. ****************************************************************************
  1327. */
  1328. SP_STATUS
  1329. SPGenerateSHResponse(PSPContext pContext, PSPBuffer pCommOutput)
  1330. {
  1331. PBYTE pbMessage = NULL;
  1332. DWORD cbMessage;
  1333. PBYTE pbHandshake = NULL;
  1334. DWORD cbHandshake;
  1335. PBYTE pbClientCert = NULL;
  1336. DWORD cbClientCert = 0;
  1337. DWORD cbDataOut;
  1338. SP_STATUS pctRet;
  1339. BOOL fAllocated = FALSE;
  1340. BOOL fClientAuth;
  1341. PSessCacheItem pZombie;
  1342. SP_BEGIN("SPGenerateSHResponse");
  1343. if((pContext == NULL) ||
  1344. (pCommOutput == NULL) ||
  1345. (pContext->RipeZombie == NULL) ||
  1346. (pContext->pKeyExchInfo == NULL))
  1347. {
  1348. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  1349. }
  1350. fClientAuth = pContext->fCertReq;
  1351. pZombie = pContext->RipeZombie;
  1352. //
  1353. // Estimate size of the ClientKeyExchange message group.
  1354. //
  1355. cbMessage = 0;
  1356. if(fClientAuth)
  1357. {
  1358. if(pContext->pActiveClientCred != NULL)
  1359. {
  1360. DWORD cbCertVerify;
  1361. pbClientCert = pContext->pActiveClientCred->pbSsl3SerializedChain;
  1362. cbClientCert = pContext->pActiveClientCred->cbSsl3SerializedChain;
  1363. if(cbClientCert > 0x3fff) //Separate Wrappers after this
  1364. { // is a BIG UNDONE...
  1365. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  1366. goto cleanup;
  1367. }
  1368. cbMessage += sizeof(SHSH) + // ClientCertificate
  1369. CB_SSL3_CERT_VECTOR +
  1370. cbClientCert;
  1371. pctRet = BuildCertVerify(pContext,
  1372. NULL,
  1373. &cbCertVerify);
  1374. if(pctRet != PCT_ERR_OK)
  1375. {
  1376. goto cleanup;
  1377. }
  1378. cbMessage += cbCertVerify; // CertificateVerify
  1379. }
  1380. else
  1381. {
  1382. LogNoClientCertFoundEvent();
  1383. //for ssl3, it's no_certificate alert
  1384. if((pContext->RipeZombie->fProtocol & SP_PROT_SSL3))
  1385. {
  1386. cbMessage += sizeof(SWRAP) + // no_certificate Alert
  1387. CB_SSL3_ALERT_ONLY +
  1388. SP_MAX_DIGEST_LEN +
  1389. SP_MAX_BLOCKCIPHER_SIZE;
  1390. }
  1391. // for tls1, it's certificate of zero length.
  1392. else
  1393. {
  1394. cbMessage += sizeof(SHSH) + CB_SSL3_CERT_VECTOR;
  1395. }
  1396. }
  1397. }
  1398. cbMessage += sizeof(SWRAP) + // ClientKeyExchange
  1399. sizeof(SHSH) +
  1400. pContext->cbEncryptedKey +
  1401. SP_MAX_DIGEST_LEN +
  1402. SP_MAX_BLOCKCIPHER_SIZE;
  1403. cbMessage += sizeof(SWRAP) + // ChangeCipherSpec
  1404. CB_SSL3_CHANGE_CIPHER_SPEC_ONLY +
  1405. SP_MAX_DIGEST_LEN +
  1406. SP_MAX_BLOCKCIPHER_SIZE;
  1407. cbMessage += sizeof(SWRAP) + // Finished
  1408. CB_SSL3_FINISHED_MSG_ONLY +
  1409. SP_MAX_DIGEST_LEN +
  1410. SP_MAX_BLOCKCIPHER_SIZE;
  1411. //
  1412. // Allocate memory for the ClientKeyExchange message group.
  1413. //
  1414. if(pCommOutput->pvBuffer)
  1415. {
  1416. // Application has allocated memory.
  1417. if(pCommOutput->cbBuffer < cbMessage)
  1418. {
  1419. pCommOutput->cbData = cbMessage;
  1420. pctRet = SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  1421. goto cleanup;
  1422. }
  1423. fAllocated = TRUE;
  1424. }
  1425. else
  1426. {
  1427. // Schannel is to allocate memory.
  1428. pCommOutput->cbBuffer = cbMessage;
  1429. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  1430. if(pCommOutput->pvBuffer == NULL)
  1431. {
  1432. pctRet = SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  1433. goto cleanup;
  1434. }
  1435. }
  1436. pCommOutput->cbData = 0;
  1437. // Build no_certificate alert (at the end of the output buffer).
  1438. if((pContext->RipeZombie->fProtocol & SP_PROT_SSL3) && fClientAuth && pbClientCert == NULL)
  1439. {
  1440. pbMessage = (PBYTE)pCommOutput->pvBuffer + pCommOutput->cbData;
  1441. // Build alert message.
  1442. BuildAlertMessage(pbMessage,
  1443. SSL3_ALERT_WARNING,
  1444. SSL3_ALERT_NO_CERTIFICATE);
  1445. // Build record header and encrypt message.
  1446. pctRet = SPSetWrap(pContext,
  1447. pbMessage,
  1448. SSL3_CT_ALERT,
  1449. CB_SSL3_ALERT_ONLY,
  1450. TRUE,
  1451. &cbDataOut);
  1452. if(pctRet != PCT_ERR_OK)
  1453. {
  1454. SP_LOG_RESULT(pctRet);
  1455. goto cleanup;
  1456. }
  1457. // Update buffer length.
  1458. pCommOutput->cbData += cbDataOut;
  1459. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  1460. }
  1461. // Keep pointer to record structure. This will represent the
  1462. // ClientCertificate, ClientKeyExchange, and CertificateVerify messages.
  1463. pbMessage = (PBYTE)pCommOutput->pvBuffer + pCommOutput->cbData;
  1464. cbMessage = 0;
  1465. pbHandshake = pbMessage + sizeof(SWRAP);
  1466. // Build ClientCertificate message.
  1467. if((pContext->RipeZombie->fProtocol & SP_PROT_TLS1) && fClientAuth && pbClientCert == NULL)
  1468. {
  1469. //Build a zero length certificate message for TLS1
  1470. pbMessage = (PBYTE)pCommOutput->pvBuffer + pCommOutput->cbData;
  1471. ((CERT *)pbHandshake)->bcbClist24 = 0;
  1472. ((CERT *)pbHandshake)->bcbMSBClist = 0;
  1473. ((CERT *)pbHandshake)->bcbLSBClist = 0;
  1474. cbHandshake = sizeof(SHSH) + CB_SSL3_CERT_VECTOR;
  1475. // Fill in Handshake structure.
  1476. SetHandshake(pbHandshake,
  1477. SSL3_HS_CERTIFICATE,
  1478. NULL,
  1479. CB_SSL3_CERT_VECTOR);
  1480. // Update handshake hash objects.
  1481. pctRet = UpdateHandshakeHash(pContext,
  1482. pbHandshake,
  1483. cbHandshake,
  1484. FALSE);
  1485. if(pctRet != PCT_ERR_OK)
  1486. {
  1487. goto cleanup;
  1488. }
  1489. pbHandshake += cbHandshake;
  1490. cbMessage += cbHandshake;
  1491. }
  1492. if(fClientAuth && pbClientCert != NULL)
  1493. {
  1494. memcpy(&((CERT *)pbHandshake)->bcbCert24,
  1495. pbClientCert,
  1496. cbClientCert);
  1497. ((CERT *)pbHandshake)->bcbClist24 = MS24BOF(cbClientCert);
  1498. ((CERT *)pbHandshake)->bcbMSBClist = MSBOF(cbClientCert);
  1499. ((CERT *)pbHandshake)->bcbLSBClist = LSBOF(cbClientCert);
  1500. cbHandshake = sizeof(SHSH) + CB_SSL3_CERT_VECTOR + cbClientCert;
  1501. // Fill in Handshake structure.
  1502. SetHandshake(pbHandshake,
  1503. SSL3_HS_CERTIFICATE,
  1504. NULL,
  1505. cbHandshake - sizeof(SHSH));
  1506. // Update handshake hash objects.
  1507. pctRet = UpdateHandshakeHash(pContext,
  1508. pbHandshake,
  1509. cbHandshake,
  1510. FALSE);
  1511. if(pctRet != PCT_ERR_OK)
  1512. {
  1513. goto cleanup;
  1514. }
  1515. pbHandshake += cbHandshake;
  1516. cbMessage += cbHandshake;
  1517. }
  1518. // Build ClientKeyExchange message.
  1519. {
  1520. SetHandshake(pbHandshake,
  1521. SSL3_HS_CLIENT_KEY_EXCHANGE,
  1522. pContext->pbEncryptedKey,
  1523. pContext->cbEncryptedKey);
  1524. cbHandshake = sizeof(SHSH) + pContext->cbEncryptedKey;
  1525. SPExternalFree(pContext->pbEncryptedKey);
  1526. pContext->pbEncryptedKey = NULL;
  1527. pContext->cbEncryptedKey = 0;
  1528. // Update handshake hash objects.
  1529. pctRet = UpdateHandshakeHash(pContext,
  1530. pbHandshake,
  1531. cbHandshake,
  1532. FALSE);
  1533. if(pctRet != PCT_ERR_OK)
  1534. {
  1535. goto cleanup;
  1536. }
  1537. pbHandshake += cbHandshake;
  1538. cbMessage += cbHandshake;
  1539. }
  1540. // Build CertificateVerify message.
  1541. if(fClientAuth && pbClientCert != NULL)
  1542. {
  1543. pctRet = BuildCertVerify(pContext, pbHandshake, &cbHandshake);
  1544. if(pctRet != PCT_ERR_OK)
  1545. {
  1546. SP_LOG_RESULT(pctRet);
  1547. goto cleanup;
  1548. }
  1549. // Update handshake hash objects.
  1550. pctRet = UpdateHandshakeHash(pContext,
  1551. pbHandshake,
  1552. cbHandshake,
  1553. FALSE);
  1554. if(pctRet != PCT_ERR_OK)
  1555. {
  1556. goto cleanup;
  1557. }
  1558. pbHandshake += cbHandshake;
  1559. cbMessage += cbHandshake;
  1560. }
  1561. // Add record header and encrypt handshake messages.
  1562. pctRet = SPSetWrap(pContext,
  1563. pbMessage,
  1564. SSL3_CT_HANDSHAKE,
  1565. cbMessage,
  1566. TRUE,
  1567. &cbDataOut);
  1568. if(pctRet != PCT_ERR_OK)
  1569. {
  1570. SP_LOG_RESULT(pctRet);
  1571. goto cleanup;
  1572. }
  1573. // Update buffer length.
  1574. pCommOutput->cbData += cbDataOut;
  1575. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  1576. // Build the ChangeCipherSpec and Finished messages.
  1577. {
  1578. pctRet = BuildCCSAndFinishMessage(pContext, pCommOutput, TRUE);
  1579. if(pctRet != PCT_ERR_OK)
  1580. {
  1581. SP_LOG_RESULT(pctRet);
  1582. goto cleanup;
  1583. }
  1584. }
  1585. // Advance state machine.
  1586. pContext->State = SSL3_STATE_CLIENT_FINISH;
  1587. pctRet = PCT_ERR_OK;
  1588. cleanup:
  1589. SP_RETURN(pctRet);
  1590. }
  1591. SP_STATUS
  1592. SPGenerateCloseNotify(
  1593. PSPContext pContext,
  1594. PSPBuffer pCommOutput)
  1595. {
  1596. PBYTE pbMessage = NULL;
  1597. DWORD cbMessage;
  1598. DWORD cbDataOut;
  1599. SP_STATUS pctRet;
  1600. SP_BEGIN("SPGenerateCloseNotify");
  1601. if((pContext == NULL) ||
  1602. (pCommOutput == NULL))
  1603. {
  1604. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  1605. }
  1606. //
  1607. // Estimate size of the message.
  1608. //
  1609. cbMessage = sizeof(SWRAP) +
  1610. CB_SSL3_ALERT_ONLY +
  1611. SP_MAX_DIGEST_LEN +
  1612. SP_MAX_BLOCKCIPHER_SIZE;
  1613. //
  1614. // Allocate memory for the message.
  1615. //
  1616. if(pCommOutput->pvBuffer)
  1617. {
  1618. // Application has allocated memory.
  1619. if(pCommOutput->cbBuffer < cbMessage)
  1620. {
  1621. pCommOutput->cbData = cbMessage;
  1622. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  1623. }
  1624. }
  1625. else
  1626. {
  1627. // Schannel is to allocate memory.
  1628. pCommOutput->cbBuffer = cbMessage;
  1629. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  1630. if(pCommOutput->pvBuffer == NULL)
  1631. {
  1632. SP_RETURN(SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY));
  1633. }
  1634. }
  1635. pCommOutput->cbData = 0;
  1636. //
  1637. // Build alert message.
  1638. //
  1639. pbMessage = pCommOutput->pvBuffer;
  1640. // Build alert message.
  1641. BuildAlertMessage(pbMessage,
  1642. SSL3_ALERT_WARNING,
  1643. SSL3_ALERT_CLOSE_NOTIFY);
  1644. // Build record header and encrypt message.
  1645. pctRet = SPSetWrap( pContext,
  1646. pbMessage,
  1647. SSL3_CT_ALERT,
  1648. CB_SSL3_ALERT_ONLY,
  1649. TRUE,
  1650. &cbDataOut);
  1651. if(pctRet != PCT_ERR_OK)
  1652. {
  1653. SP_RETURN(SP_LOG_RESULT(pctRet));
  1654. }
  1655. // Update buffer length.
  1656. pCommOutput->cbData += cbDataOut;
  1657. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  1658. pContext->State = SP_STATE_SHUTDOWN;
  1659. SP_RETURN(PCT_ERR_OK);
  1660. }
  1661. /*
  1662. ***************************************************************************
  1663. * SPProcessMessage
  1664. * This is the main function which parses and stores the incoming messages
  1665. * from the wire.
  1666. ****************************************************************************
  1667. */
  1668. SP_STATUS
  1669. SPProcessMessage(
  1670. PSPContext pContext,
  1671. BYTE bContentType,
  1672. PBYTE pbMsg,
  1673. DWORD cbMsg)
  1674. {
  1675. SP_STATUS pctRet = PCT_ERR_ILLEGAL_MESSAGE;
  1676. DWORD dwState = pContext->State;
  1677. // enum {
  1678. // change_cipher_spec(20), alert(21), handshake(22),
  1679. // application_data(23), (255)
  1680. // } ContentType;
  1681. switch(bContentType)
  1682. {
  1683. case SSL3_CT_ALERT:
  1684. DebugLog((DEB_TRACE, "Alert Message:\n"));
  1685. pctRet = ParseAlertMessage(pContext,
  1686. pbMsg,
  1687. cbMsg);
  1688. break;
  1689. case SSL3_CT_CHANGE_CIPHER_SPEC:
  1690. DebugLog((DEB_TRACE, "Change Cipher Spec:\n"));
  1691. if(SSL3_STATE_RESTART_CCS == dwState ||
  1692. SSL3_STATE_CLIENT_FINISH == dwState)
  1693. {
  1694. pctRet = Ssl3HandleCCS(
  1695. pContext,
  1696. pbMsg,
  1697. cbMsg);
  1698. if (PCT_ERR_OK == pctRet)
  1699. {
  1700. if(SSL3_STATE_RESTART_CCS == dwState)
  1701. pContext->State = SSL3_STATE_RESTART_SERVER_FINISH;
  1702. }
  1703. }
  1704. else if(SSL3_STATE_CLIENT_KEY_XCHANGE == dwState ||
  1705. SSL3_STATE_CERT_VERIFY == dwState ||
  1706. SSL3_STATE_RESTART_SER_HELLO == dwState)
  1707. {
  1708. pctRet = Ssl3HandleCCS(
  1709. pContext,
  1710. pbMsg,
  1711. cbMsg);
  1712. if (PCT_ERR_OK == pctRet)
  1713. {
  1714. if(SSL3_STATE_RESTART_SER_HELLO == dwState)
  1715. {
  1716. pContext->State = SSL3_STATE_SER_RESTART_CHANGE_CIPHER_SPEC;
  1717. }
  1718. }
  1719. }
  1720. else
  1721. {
  1722. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  1723. }
  1724. break;
  1725. case UNI_STATE_RECVD_UNIHELLO:
  1726. {
  1727. DebugLog((DEB_TRACE, "Unified Hello:\n"));
  1728. pctRet = Ssl3SrvHandleUniHello(pContext, pbMsg, cbMsg);
  1729. if (SP_FATAL(pctRet))
  1730. {
  1731. pContext->State = PCT1_STATE_ERROR;
  1732. }
  1733. }
  1734. break;
  1735. case SSL3_CT_HANDSHAKE:
  1736. {
  1737. DWORD dwcb;
  1738. if(pContext->State == SP_STATE_CONNECTED)
  1739. {
  1740. //We may be getting a REDO message
  1741. DebugLog((DEB_WARN, "May be a ReDO"));
  1742. pContext->State = SSL3_STATE_CLIENT_HELLO;
  1743. }
  1744. //Since multiple handshake can be put into on Record
  1745. //layer- we have to do this loop-here.
  1746. do
  1747. {
  1748. if(cbMsg < sizeof(SHSH))
  1749. break;
  1750. dwcb = ((INT)pbMsg[1] << 16) + ((INT)pbMsg[2] << 8) + (INT)pbMsg[3];
  1751. if(sizeof(SHSH) + dwcb > cbMsg)
  1752. break;
  1753. pctRet = SPProcessHandshake(pContext, pbMsg, dwcb + sizeof(SHSH));
  1754. CHECK_PCT_RET_BREAK(pctRet);
  1755. cbMsg -= dwcb + sizeof(SHSH);
  1756. pbMsg += dwcb + sizeof(SHSH);
  1757. } while(cbMsg > 0);
  1758. }
  1759. break;
  1760. default:
  1761. DebugLog((DEB_WARN, "Error in protocol, dwState is %lx\n", dwState));
  1762. pContext->State = PCT1_STATE_ERROR;
  1763. break;
  1764. }
  1765. if (pctRet & PCT_INT_DROP_CONNECTION)
  1766. {
  1767. pContext->State &= ~ SP_STATE_CONNECTED;
  1768. }
  1769. return(pctRet);
  1770. }
  1771. /*
  1772. ***************************************************************************
  1773. * Helper function to make connected state for ssl3
  1774. ****************************************************************************
  1775. */
  1776. void Ssl3StateConnected(PSPContext pContext)
  1777. {
  1778. pContext->State = SP_STATE_CONNECTED;
  1779. pContext->DecryptHandler = Ssl3DecryptHandler;
  1780. pContext->Encrypt = Ssl3EncryptMessage;
  1781. pContext->Decrypt = Ssl3DecryptMessage;
  1782. pContext->GetHeaderSize = Ssl3GetHeaderSize;
  1783. SPContextClean(pContext);
  1784. }
  1785. /*
  1786. ***************************************************************************
  1787. * SPProcessHandshake, Process all the handshake messages.
  1788. ****************************************************************************
  1789. */
  1790. SP_STATUS SPProcessHandshake(
  1791. PSPContext pContext,
  1792. PBYTE pb,
  1793. DWORD dwcb)
  1794. {
  1795. SP_STATUS pctRet;
  1796. SHSH * pshsh;
  1797. DWORD cbHandshake;
  1798. //
  1799. // char HandshakeType;
  1800. // char Length24
  1801. // char Length16
  1802. // char Length08
  1803. // <actual handshake message>
  1804. //
  1805. SP_BEGIN("SPProcessHandshake");
  1806. if(pContext == NULL || pb == NULL)
  1807. {
  1808. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  1809. }
  1810. if(dwcb < sizeof(SHSH))
  1811. {
  1812. SP_RETURN(PCT_INT_INCOMPLETE_MSG);
  1813. }
  1814. pshsh = (SHSH *)pb;
  1815. DebugLog((DEB_TRACE, "Protocol:%x, Message:%x, State:%x\n",
  1816. pContext->dwProtocol,
  1817. pshsh->typHS,
  1818. pContext->State));
  1819. cbHandshake = ((DWORD)pshsh->bcb24 << 16) +
  1820. ((DWORD)pshsh->bcbMSB << 8) +
  1821. (DWORD)pshsh->bcbLSB;
  1822. if(dwcb < sizeof(SHSH) + cbHandshake)
  1823. {
  1824. SP_RETURN(PCT_INT_INCOMPLETE_MSG);
  1825. }
  1826. if(pContext->dwProtocol & SP_PROT_CLIENTS)
  1827. {
  1828. //
  1829. // Handle client-side handshake states.
  1830. //
  1831. switch((pshsh->typHS << 16) | (pContext->State & 0xffff) )
  1832. {
  1833. case (SSL3_HS_SERVER_HELLO << 16) | SSL3_STATE_CLIENT_HELLO:
  1834. case (SSL3_HS_SERVER_HELLO << 16) | UNI_STATE_CLIENT_HELLO:
  1835. {
  1836. BOOL fRestart;
  1837. DebugLog((DEB_TRACE, "Server Hello:\n"));
  1838. pctRet = SPDigestServerHello(pContext, (PUCHAR) pb, dwcb, &fRestart);
  1839. if(PCT_ERR_OK != pctRet)
  1840. {
  1841. SP_LOG_RESULT(pctRet);
  1842. break;
  1843. }
  1844. if(fRestart)
  1845. {
  1846. pContext->State = SSL3_STATE_RESTART_CCS;
  1847. }
  1848. else
  1849. {
  1850. pContext->State = SSL3_STATE_SERVER_HELLO ;
  1851. }
  1852. pContext->fCertReq = FALSE;
  1853. break;
  1854. }
  1855. case (SSL3_HS_CERTIFICATE << 16) | SSL3_STATE_SERVER_HELLO:
  1856. {
  1857. DebugLog((DEB_TRACE, "Server Certificate:\n"));
  1858. // Process server Certificate message.
  1859. pctRet = SpDigestRemoteCertificate(pContext, pb, dwcb);
  1860. if(pctRet != PCT_ERR_OK)
  1861. {
  1862. SP_LOG_RESULT(pctRet);
  1863. break;
  1864. }
  1865. // Automatically validate server certificate if appropriate
  1866. // context flag is set.
  1867. pctRet = AutoVerifyServerCertificate(pContext);
  1868. if(pctRet != PCT_ERR_OK)
  1869. {
  1870. SP_LOG_RESULT(pctRet);
  1871. break;
  1872. }
  1873. pContext->State = SSL3_STATE_SERVER_CERTIFICATE ;
  1874. break;
  1875. }
  1876. case (SSL3_HS_SERVER_KEY_EXCHANGE << 16) | SSL3_STATE_SERVER_CERTIFICATE:
  1877. {
  1878. DebugLog((DEB_TRACE, "Key Exchange:\n"));
  1879. if((pContext->dwRequestedCF & pContext->RipeZombie->dwCF) != (pContext->dwRequestedCF))
  1880. {
  1881. if((pContext->dwRequestedCF & CF_FASTSGC) != 0)
  1882. {
  1883. pContext->State = SSL3_HS_SERVER_KEY_EXCHANGE;
  1884. pctRet = PCT_ERR_OK;
  1885. break;
  1886. }
  1887. }
  1888. // Store the server key exchange value in the context. This
  1889. // will be processed later when the ServerHelloDone message
  1890. // is received. This is necessary because Fortezza needs to
  1891. // process the CertificateRequest message before building the
  1892. // ClientKeyExchange value.
  1893. pContext->cbServerKeyExchange = dwcb - sizeof(SHSH);
  1894. pContext->pbServerKeyExchange = SPExternalAlloc(pContext->cbServerKeyExchange);
  1895. if(NULL == pContext->pbServerKeyExchange)
  1896. {
  1897. SP_RETURN(SEC_E_INSUFFICIENT_MEMORY);
  1898. }
  1899. CopyMemory(pContext->pbServerKeyExchange,
  1900. pb + sizeof(SHSH),
  1901. pContext->cbServerKeyExchange);
  1902. pContext->State = SSL3_HS_SERVER_KEY_EXCHANGE ;
  1903. pctRet = PCT_ERR_OK;
  1904. break;
  1905. }
  1906. case (SSL3_HS_CERTIFICATE_REQUEST << 16)| SSL3_HS_SERVER_KEY_EXCHANGE:
  1907. case (SSL3_HS_CERTIFICATE_REQUEST << 16)| SSL3_STATE_SERVER_CERTIFICATE:
  1908. {
  1909. DebugLog((DEB_TRACE, "Certificate Request:\n"));
  1910. if((pContext->dwRequestedCF & pContext->RipeZombie->dwCF) != (pContext->dwRequestedCF))
  1911. {
  1912. if((pContext->dwRequestedCF & CF_FASTSGC) != 0)
  1913. {
  1914. pContext->State = SSL3_STATE_SERVER_CERTREQ;
  1915. pctRet = PCT_ERR_OK;
  1916. break;
  1917. }
  1918. }
  1919. pctRet = ParseCertificateRequest(pContext, pb, dwcb);
  1920. CHECK_PCT_RET_BREAK(pctRet);
  1921. pctRet = Ssl3CheckForExistingCred(pContext);
  1922. if(pctRet == SEC_E_INCOMPLETE_CREDENTIALS)
  1923. {
  1924. pContext->fInsufficientCred = TRUE;
  1925. // Process all the messages and then return the warning...
  1926. pctRet = PCT_ERR_OK;
  1927. }
  1928. if(pctRet == SEC_I_INCOMPLETE_CREDENTIALS)
  1929. {
  1930. // we didn't have a cert, so we proceed, expecting
  1931. // to send a no cert alert
  1932. pctRet = PCT_ERR_OK;
  1933. }
  1934. CHECK_PCT_RET_BREAK(pctRet);
  1935. pContext->fCertReq = TRUE;
  1936. pContext->State = SSL3_STATE_SERVER_CERTREQ ;
  1937. break;
  1938. }
  1939. case (SSL3_HS_SERVER_HELLO_DONE << 16) | SSL3_HS_SERVER_KEY_EXCHANGE:
  1940. case (SSL3_HS_SERVER_HELLO_DONE << 16) | SSL3_STATE_SERVER_CERTIFICATE:
  1941. case (SSL3_HS_SERVER_HELLO_DONE << 16) | SSL3_STATE_SERVER_CERTREQ:
  1942. {
  1943. DebugLog((DEB_TRACE, "Server Hello Done:\n"));
  1944. if(dwcb > sizeof(SHSH))
  1945. {
  1946. pctRet = SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG);
  1947. break;
  1948. }
  1949. if((pContext->dwRequestedCF & pContext->RipeZombie->dwCF) != (pContext->dwRequestedCF))
  1950. {
  1951. if((pContext->dwRequestedCF & CF_FASTSGC) != 0)
  1952. {
  1953. pContext->State = SSL3_STATE_GEN_HELLO_REQUEST;
  1954. pContext->RipeZombie->dwCF = pContext->dwRequestedCF;
  1955. pctRet = PCT_ERR_OK;
  1956. SPContextClean(pContext);
  1957. break;
  1958. }
  1959. }
  1960. pctRet = SPDigestSrvKeyX(pContext,
  1961. pContext->pbServerKeyExchange,
  1962. pContext->cbServerKeyExchange);
  1963. CHECK_PCT_RET_BREAK(pctRet);
  1964. if(pContext->pbServerKeyExchange)
  1965. {
  1966. SPExternalFree(pContext->pbServerKeyExchange);
  1967. pContext->pbServerKeyExchange = NULL;
  1968. }
  1969. pContext->State = SSL3_STATE_GEN_SERVER_HELLORESP;
  1970. if(TRUE == pContext->fInsufficientCred)
  1971. {
  1972. pctRet = SEC_I_INCOMPLETE_CREDENTIALS;
  1973. }
  1974. else
  1975. {
  1976. pctRet = PCT_ERR_OK;
  1977. }
  1978. break;
  1979. }
  1980. case (SSL3_HS_FINISHED << 16) | SSL3_STATE_RESTART_SERVER_FINISH:
  1981. DebugLog((DEB_TRACE, "ServerFinished (reconnect):\n"));
  1982. pctRet = Ssl3HandleFinish(pContext, pb, dwcb, FALSE);
  1983. if(PCT_ERR_OK != pctRet)
  1984. {
  1985. break;
  1986. }
  1987. pContext->State = SSL3_STATE_GEN_CLIENT_FINISH;
  1988. break;
  1989. case (SSL3_HS_FINISHED << 16) | SSL3_STATE_CHANGE_CIPHER_SPEC_CLIENT:
  1990. DebugLog((DEB_TRACE, "ServerFinished (full):\n"));
  1991. pctRet = Ssl3HandleFinish(pContext, pb, dwcb, FALSE);
  1992. if(PCT_ERR_OK != pctRet)
  1993. {
  1994. break;
  1995. }
  1996. // Initiate SGC renegotiation if appropriate.
  1997. if((pContext->dwRequestedCF & pContext->RipeZombie->dwCF) != (pContext->dwRequestedCF))
  1998. {
  1999. if((pContext->dwRequestedCF & CF_FASTSGC) == 0)
  2000. {
  2001. SPContextClean(pContext);
  2002. pContext->State = SSL3_STATE_GEN_HELLO_REQUEST;
  2003. pContext->RipeZombie->dwCF = pContext->dwRequestedCF;
  2004. pctRet = PCT_ERR_OK;
  2005. break;
  2006. }
  2007. }
  2008. Ssl3StateConnected(pContext);
  2009. // We add to cache because this is where we are finishing
  2010. // a normal connect.
  2011. SPCacheAdd(pContext);
  2012. break;
  2013. default:
  2014. DebugLog((DEB_TRACE, "***********ILLEGAL ********\n"));
  2015. if(pContext->RipeZombie->fProtocol & SP_PROT_TLS1)
  2016. {
  2017. SetTls1Alert(pContext, TLS1_ALERT_FATAL, TLS1_ALERT_UNEXPECTED_MESSAGE);
  2018. }
  2019. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  2020. }
  2021. }
  2022. else
  2023. {
  2024. //
  2025. // Handle server-side handshake states.
  2026. //
  2027. switch((pshsh->typHS << 16) | (pContext->State & 0xffff) )
  2028. {
  2029. case (SSL3_HS_CLIENT_HELLO << 16) | SSL3_STATE_RENEGOTIATE:
  2030. DebugLog((DEB_TRACE, "ClientHello After REDO:\n"));
  2031. #if 1
  2032. // We need to do a full handshake, so lose the cache entry.
  2033. SPCacheDereference(pContext->RipeZombie);
  2034. pContext->RipeZombie = NULL;
  2035. pctRet = SPSsl3SrvHandleClientHello(pContext, pb, FALSE);
  2036. pContext->Flags &= ~CONTEXT_FLAG_MAPPED;
  2037. if(PCT_ERR_OK == pctRet)
  2038. {
  2039. pContext->State = SSL3_STATE_GEN_REDO;
  2040. }
  2041. #else
  2042. // Allow reconnects during redo, for testing purposes only
  2043. pctRet = SPSsl3SrvHandleClientHello(pContext, pb, TRUE);
  2044. pContext->Flags &= ~CONTEXT_FLAG_MAPPED;
  2045. #endif
  2046. break;
  2047. case (SSL3_HS_CLIENT_HELLO << 16) | SSL2_STATE_SERVER_HELLO:
  2048. DebugLog((DEB_TRACE, "ClientHello after fast SGC accepted:\n"));
  2049. // We need to do a full handshake, so lose the cache entry.
  2050. SPCacheDereference(pContext->RipeZombie);
  2051. pContext->RipeZombie = NULL;
  2052. pctRet = SPSsl3SrvHandleClientHello(pContext, pb, FALSE);
  2053. break;
  2054. case (SSL3_HS_CLIENT_HELLO << 16):
  2055. DebugLog((DEB_TRACE, "ClientHello:\n"));
  2056. pctRet = SPSsl3SrvHandleClientHello(pContext, pb, TRUE);
  2057. break;
  2058. case (SSL3_HS_CLIENT_KEY_EXCHANGE << 16) | SSL2_STATE_SERVER_HELLO:
  2059. case (SSL3_HS_CLIENT_KEY_EXCHANGE << 16) | SSL3_STATE_NO_CERT_ALERT:
  2060. case (SSL3_HS_CLIENT_KEY_EXCHANGE << 16) | SSL2_STATE_CLIENT_CERTIFICATE:
  2061. DebugLog((DEB_TRACE, "Client Key Exchange:\n"));
  2062. pctRet = ParseKeyExchgMsg(pContext, pb) ;
  2063. CHECK_PCT_RET_BREAK(pctRet);
  2064. if(PCT_ERR_OK == pctRet)
  2065. pContext->State = SSL3_STATE_CLIENT_KEY_XCHANGE;
  2066. if(!pContext->fCertReq)
  2067. pContext->State = SSL3_STATE_CLIENT_KEY_XCHANGE;
  2068. break;
  2069. case ( SSL3_HS_CERTIFICATE << 16) | SSL2_STATE_SERVER_HELLO:
  2070. DebugLog((DEB_TRACE, "Client Certificate:\n"));
  2071. pctRet = SpDigestRemoteCertificate(pContext, pb, dwcb);
  2072. CHECK_PCT_RET_BREAK(pctRet);
  2073. if(PCT_ERR_OK == pctRet)
  2074. pContext->State = SSL2_STATE_CLIENT_CERTIFICATE ;
  2075. break;
  2076. case (SSL3_HS_CERTIFICATE_VERIFY << 16) | SSL3_STATE_CLIENT_KEY_XCHANGE:
  2077. DebugLog((DEB_TRACE, "Certificate Verify :\n"));
  2078. pctRet = HandleCertVerify(pContext, pb, dwcb);
  2079. if(pctRet != PCT_ERR_OK)
  2080. {
  2081. break;
  2082. }
  2083. pctRet = SPContextDoMapping(pContext);
  2084. pContext->State = SSL3_STATE_CERT_VERIFY;
  2085. break;
  2086. case (SSL3_HS_FINISHED << 16) | SSL3_STATE_SER_RESTART_CHANGE_CIPHER_SPEC:
  2087. DebugLog((DEB_TRACE, "Finished(client) Restart :\n"));
  2088. pctRet = Ssl3HandleFinish(pContext, pb, dwcb, TRUE);
  2089. if(pctRet != PCT_ERR_OK)
  2090. {
  2091. break;
  2092. }
  2093. Ssl3StateConnected(pContext);
  2094. break;
  2095. case (SSL3_HS_FINISHED << 16) | SSL3_STATE_CHANGE_CIPHER_SPEC_SERVER:
  2096. DebugLog((DEB_TRACE, "Finished(Client):\n"));
  2097. pctRet = Ssl3HandleFinish(pContext, pb, dwcb, TRUE);
  2098. if(PCT_ERR_OK == pctRet)
  2099. {
  2100. pContext->State = SSL3_STATE_GEN_SERVER_FINISH;
  2101. }
  2102. break;
  2103. default:
  2104. DebugLog((DEB_TRACE, "***********ILLEGAL ********\n"));
  2105. if(pContext->dwProtocol & SP_PROT_TLS1)
  2106. {
  2107. SetTls1Alert(pContext, TLS1_ALERT_FATAL, TLS1_ALERT_UNEXPECTED_MESSAGE);
  2108. }
  2109. pctRet = SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  2110. break;
  2111. }
  2112. }
  2113. if(pctRet == PCT_ERR_OK || pctRet == SEC_I_INCOMPLETE_CREDENTIALS)
  2114. {
  2115. if(pContext->cbClientHello == 0)
  2116. {
  2117. if(UpdateHandshakeHash(pContext, pb, dwcb, FALSE) != PCT_ERR_OK)
  2118. {
  2119. pctRet = PCT_INT_INTERNAL_ERROR;
  2120. }
  2121. }
  2122. }
  2123. SP_RETURN(pctRet);
  2124. }
  2125. /*
  2126. ***************************************************************************
  2127. * SPGenerateResponse, All the messages are built from this function.
  2128. ****************************************************************************
  2129. */
  2130. SP_STATUS SPGenerateResponse(
  2131. PSPContext pContext,
  2132. PSPBuffer pCommOutput) //Out
  2133. {
  2134. SP_STATUS pctRet = PCT_ERR_OK;
  2135. DebugLog((DEB_TRACE, "**********Protocol***** %x\n", pContext->RipeZombie->fProtocol));
  2136. switch(pContext->State)
  2137. {
  2138. case TLS1_STATE_ERROR:
  2139. // TLS1 Alert message
  2140. DebugLog((DEB_TRACE, "GEN TLS1 Alert Message:\n"));
  2141. pctRet = SPBuildTlsAlertMessage(pContext, pCommOutput);
  2142. pContext->State = SP_STATE_SHUTDOWN;
  2143. break;
  2144. case SP_STATE_SHUTDOWN_PENDING:
  2145. DebugLog((DEB_TRACE, "GEN Close Notify:\n"));
  2146. pctRet = SPGenerateCloseNotify(pContext, pCommOutput);
  2147. break;
  2148. case SP_STATE_SHUTDOWN:
  2149. return PCT_INT_EXPIRED;
  2150. case SSL3_STATE_GEN_SERVER_HELLORESP:
  2151. DebugLog((DEB_TRACE, "GEN Server Hello Response:\n"));
  2152. pctRet = SPGenerateSHResponse(pContext, pCommOutput);
  2153. break;
  2154. case SSL3_STATE_GEN_HELLO_REQUEST:
  2155. DebugLog((DEB_TRACE, "GEN Hello Request:\n"));
  2156. //Temp Disabling Reconnect during REDO
  2157. if(!SPCacheClone(&pContext->RipeZombie))
  2158. {
  2159. pctRet = SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2160. break;
  2161. }
  2162. if(pContext->RipeZombie->fProtocol == SP_PROT_SSL3_CLIENT)
  2163. {
  2164. pctRet = GenerateSsl3ClientHello(pContext, pCommOutput);
  2165. }
  2166. else
  2167. {
  2168. pctRet = GenerateTls1ClientHello(pContext, pCommOutput, SP_PROT_TLS1_CLIENT);
  2169. }
  2170. pContext->Flags &= ~CONTEXT_FLAG_MAPPED;
  2171. pContext->State = SSL3_STATE_CLIENT_HELLO;
  2172. break;
  2173. case SSL3_STATE_GEN_CLIENT_FINISH:
  2174. {
  2175. DebugLog((DEB_TRACE, "GEN Client Finish:\n"));
  2176. pctRet = SPBuildCCSAndFinish(pContext, pCommOutput);
  2177. if(PCT_ERR_OK == pctRet)
  2178. {
  2179. Ssl3StateConnected(pContext);
  2180. }
  2181. }
  2182. break;
  2183. /*-------------------------------------SERVER SIDE------------------------------------*/
  2184. case SSL3_STATE_GEN_SERVER_FINISH:
  2185. DebugLog((DEB_TRACE, "GEN Server Finish:\n"));
  2186. pctRet = SPBuildCCSAndFinish(pContext, pCommOutput);
  2187. /* Cache Session Here */
  2188. if(pctRet == PCT_ERR_OK)
  2189. {
  2190. Ssl3StateConnected(pContext);
  2191. SPCacheAdd(pContext);
  2192. }
  2193. break;
  2194. case SSL3_STATE_GEN_SERVER_HELLO: // Generate the response
  2195. DebugLog((DEB_TRACE, "GEN Server hello:\n"));
  2196. pctRet = SPSsl3SrvGenServerHello(pContext, pCommOutput);
  2197. break;
  2198. case SSL3_STATE_GEN_SERVER_HELLO_RESTART:
  2199. pctRet = SPSsl3SrvGenRestart(pContext, pCommOutput);
  2200. break;
  2201. case SP_STATE_CONNECTED:
  2202. // We were called from a connected state, so the application
  2203. // must be requesting a redo.
  2204. if(pContext->RipeZombie->fProtocol & SP_PROT_SERVERS)
  2205. {
  2206. // Build a HelloRequest message.
  2207. DebugLog((DEB_TRACE, "GEN Hello Request:\n"));
  2208. pctRet = SPBuildHelloRequest(pContext, pCommOutput);
  2209. }
  2210. else
  2211. {
  2212. // Build a ClientHello message.
  2213. DebugLog((DEB_TRACE, "GEN renegotiate Client Hello\n"));
  2214. pctRet = GenerateHello(pContext, pCommOutput, TRUE);
  2215. // Clear flag, so that the context gets re-transferred when
  2216. // handshake completes.
  2217. pContext->Flags &= ~CONTEXT_FLAG_MAPPED;
  2218. }
  2219. break;
  2220. case SSL3_STATE_GEN_REDO:
  2221. DebugLog((DEB_TRACE, "GEN Server hello ( REDO ):\n"));
  2222. // We processed a client hello from the decrypt handler,
  2223. // so generate a server hello.
  2224. pctRet = SPSsl3SrvGenServerHello(pContext, pCommOutput);
  2225. break;
  2226. default:
  2227. break;
  2228. }
  2229. if (pctRet & PCT_INT_DROP_CONNECTION)
  2230. {
  2231. pContext->State &= ~ SP_STATE_CONNECTED;
  2232. }
  2233. return(pctRet);
  2234. }
  2235. /*
  2236. ***************************************************************************
  2237. * FNoInputState Are we in a state that all the inputs are handled and
  2238. * waiting for Response generation RETURN TRUE if yes
  2239. ****************************************************************************
  2240. */
  2241. BOOL FNoInputState(DWORD dwState)
  2242. {
  2243. switch(dwState)
  2244. {
  2245. default:
  2246. return(FALSE);
  2247. case SSL3_STATE_GEN_HELLO_REQUEST:
  2248. case SSL3_STATE_GEN_SERVER_HELLORESP:
  2249. case SSL3_STATE_GEN_SERVER_FINISH:
  2250. case SSL3_STATE_GEN_REDO:
  2251. case SP_STATE_CONNECTED:
  2252. case TLS1_STATE_ERROR:
  2253. case SP_STATE_SHUTDOWN_PENDING:
  2254. return(TRUE);
  2255. }
  2256. }
  2257. /*
  2258. ***************************************************************************
  2259. * SPBuildHelloRequest
  2260. *
  2261. * Build hello-request message, this is done, when server sees a GET and the
  2262. * GET object needs client-authentication.
  2263. * this may be needed when the server thinks that the session is for a long
  2264. * time or lots of bytes gon down the wire, to RENEGOTIATE the keys
  2265. ****************************************************************************
  2266. */
  2267. SP_STATUS
  2268. SPBuildHelloRequest(
  2269. PSPContext pContext,
  2270. PSPBuffer pCommOutput)
  2271. {
  2272. PBYTE pbMessage = NULL;
  2273. DWORD cbMessage;
  2274. PBYTE pbHandshake = NULL;
  2275. DWORD cbHandshake;
  2276. BOOL fAllocated = FALSE;
  2277. SP_STATUS pctRet;
  2278. DWORD cbDataOut;
  2279. // Estimate size of HelloRequest message.
  2280. cbMessage = sizeof(SWRAP) +
  2281. sizeof(SHSH) +
  2282. SP_MAX_DIGEST_LEN +
  2283. SP_MAX_BLOCKCIPHER_SIZE;
  2284. if(pCommOutput->pvBuffer)
  2285. {
  2286. // Application has allocated memory.
  2287. if(pCommOutput->cbBuffer < cbMessage)
  2288. {
  2289. pCommOutput->cbData = cbMessage;
  2290. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  2291. }
  2292. fAllocated = TRUE;
  2293. }
  2294. else
  2295. {
  2296. // Schannel is to allocate memory.
  2297. pCommOutput->cbBuffer = cbMessage;
  2298. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  2299. if(pCommOutput->pvBuffer == NULL)
  2300. {
  2301. return SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  2302. }
  2303. }
  2304. pCommOutput->cbData = 0;
  2305. pbMessage = pCommOutput->pvBuffer;
  2306. pbHandshake = pbMessage + sizeof(SWRAP);
  2307. cbHandshake = sizeof(SHSH);
  2308. // Fill in Handshake structure.
  2309. SetHandshake(pbHandshake,
  2310. SSL3_HS_HELLO_REQUEST,
  2311. NULL,
  2312. 0);
  2313. // Update handshake hash objects.
  2314. pctRet = UpdateHandshakeHash(pContext,
  2315. pbHandshake,
  2316. cbHandshake,
  2317. FALSE);
  2318. if(pctRet != PCT_ERR_OK)
  2319. {
  2320. return(pctRet);
  2321. }
  2322. // Add record header and encrypt handshake messages.
  2323. pctRet = SPSetWrap(pContext,
  2324. pbMessage,
  2325. SSL3_CT_HANDSHAKE,
  2326. cbHandshake,
  2327. FALSE,
  2328. &cbDataOut);
  2329. // Update buffer length.
  2330. pCommOutput->cbData += cbDataOut;
  2331. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  2332. return pctRet;
  2333. }
  2334. /*
  2335. ***************************************************************************
  2336. ****************************************************************************
  2337. */
  2338. SP_STATUS
  2339. SPSsl3SrvGenServerHello(
  2340. PSPContext pContext,
  2341. PSPBuffer pCommOutput)
  2342. {
  2343. SP_STATUS pctRet;
  2344. PSPCredentialGroup pCred;
  2345. BOOL fAllocated = FALSE;
  2346. PBYTE pbServerCert = NULL;
  2347. DWORD cbServerCert = 0;
  2348. PBYTE pbIssuerList = NULL;
  2349. DWORD cbIssuerList = 0;
  2350. PBYTE pbMessage = NULL;
  2351. DWORD cbMessage;
  2352. PBYTE pbHandshake = NULL;
  2353. DWORD cbHandshake;
  2354. DWORD cbDataOut;
  2355. DWORD cbServerExchange;
  2356. BOOL fClientAuth = ((pContext->Flags & CONTEXT_FLAG_MUTUAL_AUTH) != 0);
  2357. if(pCommOutput == NULL)
  2358. {
  2359. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2360. }
  2361. // Get pointer to server certificate chain.
  2362. pCred = pContext->RipeZombie->pServerCred;
  2363. pbServerCert = pContext->RipeZombie->pActiveServerCred->pbSsl3SerializedChain;
  2364. cbServerCert = pContext->RipeZombie->pActiveServerCred->cbSsl3SerializedChain;
  2365. if(pbServerCert == NULL)
  2366. {
  2367. pctRet = SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2368. goto cleanup;
  2369. }
  2370. //
  2371. // Estimate size of the ServerHello message group, which includes the
  2372. // ServerHello, ServerCertificate, ServerKeyExchange, CertificateRequest,
  2373. // and ServerHelloDone messages.
  2374. //
  2375. cbMessage = sizeof(SWRAP) + // ServerHello (plus encryption goo)
  2376. sizeof(SSH) +
  2377. SP_MAX_DIGEST_LEN +
  2378. SP_MAX_BLOCKCIPHER_SIZE;
  2379. cbMessage += sizeof(SHSH) + // ServerCertificate
  2380. CB_SSL3_CERT_VECTOR +
  2381. cbServerCert;
  2382. cbMessage += sizeof(SHSH); // ServerHelloDone
  2383. // Get pointer to key exchange system.
  2384. if((pContext->pKeyExchInfo == NULL) || ((pContext->pKeyExchInfo->fProtocol & SP_PROT_SSL3TLS1_CLIENTS) == 0))
  2385. {
  2386. pctRet = SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2387. goto cleanup;
  2388. }
  2389. pctRet = pContext->pKeyExchInfo->System->GenerateServerExchangeValue(
  2390. pContext,
  2391. NULL,
  2392. &cbServerExchange);
  2393. if(pctRet != PCT_ERR_OK)
  2394. {
  2395. goto cleanup;
  2396. }
  2397. if(pContext->fExchKey)
  2398. {
  2399. cbMessage += sizeof(SHSH) + cbServerExchange;
  2400. }
  2401. // Add in length of CertificateRequest message.
  2402. if(fClientAuth)
  2403. {
  2404. if(g_fSendIssuerList)
  2405. {
  2406. UpdateAndDuplicateIssuerList(pCred, &pbIssuerList, &cbIssuerList);
  2407. }
  2408. cbMessage += sizeof(SHSH) +
  2409. 1 + SSL3_CERTTYPE_MAX_COUNT +
  2410. 2 + cbIssuerList;
  2411. }
  2412. pContext->fCertReq = fClientAuth;
  2413. DebugLog((DEB_TRACE, "Server hello message %x\n", cbMessage));
  2414. //
  2415. // Allocate memory for the ServerHello message group.
  2416. //
  2417. if(pCommOutput->pvBuffer)
  2418. {
  2419. // Application has allocated memory.
  2420. if(pCommOutput->cbBuffer < cbMessage)
  2421. {
  2422. pCommOutput->cbData = cbMessage;
  2423. pctRet = SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  2424. goto cleanup;
  2425. }
  2426. fAllocated = TRUE;
  2427. }
  2428. else
  2429. {
  2430. // Schannel is to allocate memory.
  2431. pCommOutput->cbBuffer = cbMessage;
  2432. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  2433. if(pCommOutput->pvBuffer == NULL)
  2434. {
  2435. pctRet = SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  2436. goto cleanup;
  2437. }
  2438. }
  2439. pCommOutput->cbData = 0;
  2440. // Keep pointer to record structure. This will represent all of the
  2441. // handshake messages.
  2442. pbMessage = (PBYTE)pCommOutput->pvBuffer + pCommOutput->cbData;
  2443. cbMessage = 0;
  2444. pbHandshake = pbMessage + sizeof(SWRAP);
  2445. // Generate the session ID (actually previously generated)
  2446. pContext->RipeZombie->cbSessionID = CB_SSL3_SESSION_ID;
  2447. // Generate internal values to make server hello
  2448. pctRet = Ssl3GenerateRandom(pContext->rgbS3SRandom);
  2449. if(!NT_SUCCESS(pctRet))
  2450. {
  2451. goto cleanup;
  2452. }
  2453. // Build ServerHello
  2454. Ssl3BuildServerHello(pContext, pbHandshake);
  2455. pbHandshake += sizeof(SSH);
  2456. cbMessage += sizeof(SSH);
  2457. // Build ServerCertificate
  2458. {
  2459. memcpy(&((CERT *)pbHandshake)->bcbCert24,
  2460. pbServerCert,
  2461. cbServerCert);
  2462. ((CERT *)pbHandshake)->bcbClist24 = MS24BOF(cbServerCert);
  2463. ((CERT *)pbHandshake)->bcbMSBClist = MSBOF(cbServerCert);
  2464. ((CERT *)pbHandshake)->bcbLSBClist = LSBOF(cbServerCert);
  2465. cbHandshake = sizeof(SHSH) + CB_SSL3_CERT_VECTOR + cbServerCert;
  2466. // Fill in Handshake structure.
  2467. SetHandshake(pbHandshake,
  2468. SSL3_HS_CERTIFICATE,
  2469. NULL,
  2470. cbHandshake - sizeof(SHSH));
  2471. pbHandshake += cbHandshake;
  2472. cbMessage += cbHandshake;
  2473. }
  2474. // Build ServerKeyExchange.
  2475. if(pContext->fExchKey)
  2476. {
  2477. pctRet = pContext->pKeyExchInfo->System->GenerateServerExchangeValue(
  2478. pContext,
  2479. pbHandshake + sizeof(SHSH),
  2480. &cbServerExchange);
  2481. if(pctRet != PCT_ERR_OK)
  2482. {
  2483. SP_LOG_RESULT(pctRet);
  2484. goto cleanup;
  2485. }
  2486. SetHandshake(pbHandshake, SSL3_HS_SERVER_KEY_EXCHANGE, NULL, (WORD)cbServerExchange);
  2487. pbHandshake += sizeof(SHSH) + cbServerExchange;
  2488. cbMessage += sizeof(SHSH) + cbServerExchange;
  2489. }
  2490. // Build CertificateRequest.
  2491. if(fClientAuth)
  2492. {
  2493. pctRet = Ssl3BuildCertificateRequest(pbIssuerList,
  2494. cbIssuerList,
  2495. pbHandshake,
  2496. &cbHandshake);
  2497. if(pctRet != PCT_ERR_OK)
  2498. {
  2499. SP_LOG_RESULT(pctRet);
  2500. goto cleanup;
  2501. }
  2502. pbHandshake += cbHandshake;
  2503. cbMessage += cbHandshake;
  2504. }
  2505. // Build ServerHelloDone.
  2506. {
  2507. BuildServerHelloDone(pbHandshake);
  2508. pbHandshake += sizeof(SHSH);
  2509. cbMessage += sizeof(SHSH);
  2510. }
  2511. // Initialize handshake hashes and hash ClientHello message. This
  2512. // must be done _after_ the ServerKeyExchange message is built,
  2513. // so that the correct CSP context is used.
  2514. pctRet = UpdateHandshakeHash(pContext,
  2515. pContext->pClientHello,
  2516. pContext->cbClientHello,
  2517. TRUE);
  2518. SPExternalFree(pContext->pClientHello);
  2519. pContext->pClientHello = NULL;
  2520. pContext->cbClientHello = 0;
  2521. if(pctRet != PCT_ERR_OK)
  2522. {
  2523. goto cleanup;
  2524. }
  2525. // Update handshake hash objects.
  2526. pctRet = UpdateHandshakeHash(pContext,
  2527. pbMessage + sizeof(SWRAP),
  2528. cbMessage,
  2529. FALSE);
  2530. if(pctRet != PCT_ERR_OK)
  2531. {
  2532. goto cleanup;
  2533. }
  2534. // Add record header and encrypt handshake messages.
  2535. pctRet = SPSetWrap(pContext,
  2536. pbMessage,
  2537. SSL3_CT_HANDSHAKE,
  2538. cbMessage,
  2539. FALSE,
  2540. &cbDataOut);
  2541. if(pctRet != PCT_ERR_OK)
  2542. {
  2543. SP_LOG_RESULT(pctRet);
  2544. goto cleanup;
  2545. }
  2546. // Update buffer length.
  2547. pCommOutput->cbData += cbDataOut;
  2548. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  2549. // Advance state machine.
  2550. pContext->State = SSL2_STATE_SERVER_HELLO;
  2551. pctRet = PCT_ERR_OK;
  2552. cleanup:
  2553. if(pctRet != PCT_ERR_OK && !fAllocated)
  2554. {
  2555. SPExternalFree(pCommOutput->pvBuffer);
  2556. pCommOutput->pvBuffer =NULL;
  2557. }
  2558. if(pbIssuerList)
  2559. {
  2560. SPExternalFree(pbIssuerList);
  2561. }
  2562. return pctRet;
  2563. }
  2564. /*
  2565. ***************************************************************************
  2566. ****************************************************************************
  2567. */
  2568. SP_STATUS
  2569. SPSsl3SrvGenRestart(
  2570. PSPContext pContext,
  2571. PSPBuffer pCommOutput)
  2572. {
  2573. SP_STATUS pctRet;
  2574. PBYTE pbMessage = NULL;
  2575. DWORD cbMessage;
  2576. DWORD cbDataOut;
  2577. BOOL fAllocated = FALSE;
  2578. if(pCommOutput == NULL)
  2579. {
  2580. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2581. }
  2582. //
  2583. // Estimate size of the restart ServerHello message group, which includes
  2584. // the ServerHello, ChangeCipherSpec, and Finished messages.
  2585. //
  2586. cbMessage = sizeof(SWRAP) + // ServerHello (plus encryption goo)
  2587. sizeof(SSH) +
  2588. SP_MAX_DIGEST_LEN +
  2589. SP_MAX_BLOCKCIPHER_SIZE;
  2590. cbMessage += sizeof(SWRAP) + // ChangeCipherSpec
  2591. CB_SSL3_CHANGE_CIPHER_SPEC_ONLY +
  2592. SP_MAX_DIGEST_LEN +
  2593. SP_MAX_BLOCKCIPHER_SIZE;
  2594. cbMessage += sizeof(SWRAP) + // Finished
  2595. CB_SSL3_FINISHED_MSG_ONLY +
  2596. SP_MAX_DIGEST_LEN +
  2597. SP_MAX_BLOCKCIPHER_SIZE;
  2598. DebugLog((DEB_TRACE, "Server hello message %x\n", cbMessage));
  2599. if(pCommOutput->pvBuffer)
  2600. {
  2601. // Application has allocated memory.
  2602. if(pCommOutput->cbBuffer < cbMessage)
  2603. {
  2604. pCommOutput->cbData = cbMessage;
  2605. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  2606. }
  2607. fAllocated = TRUE;
  2608. }
  2609. else
  2610. {
  2611. // Schannel is to allocate memory.
  2612. pCommOutput->cbBuffer = cbMessage;
  2613. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  2614. if(pCommOutput->pvBuffer == NULL)
  2615. {
  2616. return (SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY));
  2617. }
  2618. }
  2619. pCommOutput->cbData = 0;
  2620. pbMessage = (PBYTE)pCommOutput->pvBuffer + pCommOutput->cbData;
  2621. // Generate internal values to make server hello
  2622. Ssl3GenerateRandom(pContext->rgbS3SRandom);
  2623. // Make a new set of session keys.
  2624. pctRet = MakeSessionKeys(pContext,
  2625. pContext->RipeZombie->hMasterProv,
  2626. pContext->RipeZombie->hMasterKey);
  2627. if(pctRet != PCT_ERR_OK)
  2628. {
  2629. return SP_LOG_RESULT(pctRet);
  2630. }
  2631. // Initialize handshake hashes and hash ClientHello message.
  2632. pctRet = UpdateHandshakeHash(pContext,
  2633. pContext->pClientHello,
  2634. pContext->cbClientHello,
  2635. TRUE);
  2636. SPExternalFree(pContext->pClientHello);
  2637. pContext->pClientHello = NULL;
  2638. pContext->cbClientHello = 0;
  2639. if(pctRet != PCT_ERR_OK)
  2640. {
  2641. return(pctRet);
  2642. }
  2643. // Build ServerHello message body.
  2644. Ssl3BuildServerHello(pContext, pbMessage + sizeof(SWRAP));
  2645. // Update handshake hash objects.
  2646. pctRet = UpdateHandshakeHash(pContext,
  2647. pbMessage + sizeof(SWRAP),
  2648. sizeof(SSH),
  2649. FALSE);
  2650. if(pctRet != PCT_ERR_OK)
  2651. {
  2652. return(pctRet);
  2653. }
  2654. // Build record header and encrypt message.
  2655. pctRet = SPSetWrap(pContext,
  2656. pbMessage,
  2657. SSL3_CT_HANDSHAKE,
  2658. sizeof(SSH),
  2659. FALSE,
  2660. &cbDataOut);
  2661. if(pctRet != PCT_ERR_OK)
  2662. {
  2663. SPExternalFree(pCommOutput->pvBuffer);
  2664. pCommOutput->pvBuffer = 0;
  2665. return pctRet;
  2666. }
  2667. // Update buffer length.
  2668. pCommOutput->cbData += cbDataOut;
  2669. SP_ASSERT(pCommOutput->cbData <= pCommOutput->cbBuffer);
  2670. pctRet = BuildCCSAndFinishMessage(pContext,
  2671. pCommOutput,
  2672. FALSE);
  2673. if(pctRet != PCT_ERR_OK)
  2674. {
  2675. SPExternalFree(pCommOutput->pvBuffer);
  2676. pCommOutput->pvBuffer = 0;
  2677. return pctRet;
  2678. }
  2679. pContext->State = SSL3_STATE_RESTART_SER_HELLO;
  2680. return(PCT_ERR_OK);
  2681. }
  2682. /*
  2683. ***************************************************************************
  2684. * SPSsl3SrvHandleClientHello
  2685. * Client-hello from ssl3 parsing the client hello
  2686. ****************************************************************************
  2687. */
  2688. SP_STATUS
  2689. SPSsl3SrvHandleClientHello(
  2690. PSPContext pContext,
  2691. PBYTE pb,
  2692. BOOL fAttemptReconnect)
  2693. {
  2694. BOOL fRestart = FALSE;
  2695. DWORD dwHandshakeLen;
  2696. SP_BEGIN("SPSsl3SrvHandleClientHello");
  2697. // Validate handshake type
  2698. if(pb[0] != SSL3_HS_CLIENT_HELLO)
  2699. {
  2700. SP_RETURN(SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG));
  2701. }
  2702. dwHandshakeLen = ((INT)pb[1] << 16) +
  2703. ((INT)pb[2] << 8) +
  2704. (INT)pb[3];
  2705. // Save the ClientHello message so we can hash it later, once
  2706. // we know what algorithm and CSP we're using.
  2707. if(pContext->pClientHello)
  2708. {
  2709. SPExternalFree(pContext->pClientHello);
  2710. }
  2711. pContext->cbClientHello = sizeof(SHSH) + dwHandshakeLen;
  2712. pContext->pClientHello = SPExternalAlloc(pContext->cbClientHello);
  2713. if(pContext->pClientHello == NULL)
  2714. {
  2715. SP_RETURN(SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY));
  2716. }
  2717. CopyMemory(pContext->pClientHello, pb, pContext->cbClientHello);
  2718. pContext->dwClientHelloProtocol = SP_PROT_SSL3_CLIENT;
  2719. pb += sizeof(SHSH);
  2720. if(!Ssl3ParseClientHello(pContext, pb, dwHandshakeLen, fAttemptReconnect, &fRestart))
  2721. {
  2722. SP_RETURN(SP_LOG_RESULT(PCT_INT_ILLEGAL_MSG));
  2723. }
  2724. if(fRestart)
  2725. {
  2726. pContext->State = SSL3_STATE_GEN_SERVER_HELLO_RESTART;
  2727. }
  2728. else
  2729. {
  2730. pContext->State = SSL3_STATE_GEN_SERVER_HELLO;
  2731. }
  2732. SP_RETURN(PCT_ERR_OK);
  2733. }
  2734. /*
  2735. ***************************************************************************
  2736. * SPBuildCCSAndFinish
  2737. * This is a common nroutine for client/server. it builds the change cipher
  2738. * spec message and finished message.
  2739. ****************************************************************************
  2740. */
  2741. SP_STATUS
  2742. SPBuildCCSAndFinish(
  2743. PSPContext pContext, // in, out
  2744. PSPBuffer pCommOutput) // out
  2745. {
  2746. DWORD cbMessage;
  2747. BOOL fClient;
  2748. SP_STATUS pctRet;
  2749. BOOL fAllocated = FALSE;
  2750. // Estimate size of the message group.
  2751. cbMessage = sizeof(SWRAP) + // ChangeCipherSpec
  2752. CB_SSL3_CHANGE_CIPHER_SPEC_ONLY +
  2753. SP_MAX_DIGEST_LEN +
  2754. SP_MAX_BLOCKCIPHER_SIZE;
  2755. cbMessage += sizeof(SWRAP) + // Finished
  2756. CB_SSL3_FINISHED_MSG_ONLY +
  2757. SP_MAX_DIGEST_LEN +
  2758. SP_MAX_BLOCKCIPHER_SIZE;
  2759. if(pCommOutput->pvBuffer)
  2760. {
  2761. // Application has allocated memory.
  2762. if(pCommOutput->cbBuffer < cbMessage)
  2763. {
  2764. pCommOutput->cbData = cbMessage;
  2765. return SP_LOG_RESULT(PCT_INT_BUFF_TOO_SMALL);
  2766. }
  2767. fAllocated = TRUE;
  2768. }
  2769. else
  2770. {
  2771. // Schannel is to allocate memory.
  2772. pCommOutput->cbBuffer = cbMessage;
  2773. pCommOutput->pvBuffer = SPExternalAlloc(cbMessage);
  2774. if(pCommOutput->pvBuffer == NULL)
  2775. {
  2776. return (SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY));
  2777. }
  2778. }
  2779. pCommOutput->cbData = 0;
  2780. // Are we the client?
  2781. if(pContext->RipeZombie->fProtocol & SP_PROT_SSL3TLS1_CLIENTS)
  2782. {
  2783. fClient = TRUE;
  2784. }
  2785. else
  2786. {
  2787. fClient = FALSE;
  2788. }
  2789. // Build messages.
  2790. pctRet = BuildCCSAndFinishMessage(pContext,
  2791. pCommOutput,
  2792. fClient);
  2793. if(pctRet != PCT_ERR_OK)
  2794. {
  2795. SPExternalFree(pCommOutput->pvBuffer);
  2796. pCommOutput->pvBuffer = NULL;
  2797. }
  2798. return pctRet;
  2799. }
  2800. /*
  2801. ***************************************************************************
  2802. * Ssl3SrvHandleUniHello:
  2803. * we can get an UniHello from client-side, parse and digest the info
  2804. ****************************************************************************
  2805. */
  2806. SP_STATUS
  2807. Ssl3SrvHandleUniHello(
  2808. PSPContext pContext,
  2809. PBYTE pbMsg,
  2810. DWORD cbMsg)
  2811. {
  2812. SP_STATUS pctRet;
  2813. PSsl2_Client_Hello pHello = NULL;
  2814. SPBuffer Input;
  2815. SP_BEGIN("Ssl3SrvHandleUniHello");
  2816. if(pContext == NULL)
  2817. {
  2818. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  2819. }
  2820. if(pContext->pCredGroup == NULL)
  2821. {
  2822. SP_RETURN(SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR));
  2823. }
  2824. //
  2825. // Decode the ClientHello message.
  2826. //
  2827. Input.pvBuffer = pbMsg;
  2828. Input.cbData = cbMsg;
  2829. Input.cbBuffer = cbMsg;
  2830. pctRet = Ssl2UnpackClientHello(&Input, &pHello);
  2831. if(PCT_ERR_OK != pctRet)
  2832. {
  2833. goto Ret;
  2834. }
  2835. // Save the ClientHello message so we can hash it later, once
  2836. // we know what algorithm and CSP we're using.
  2837. if(pContext->pClientHello)
  2838. {
  2839. SPExternalFree(pContext->pClientHello);
  2840. }
  2841. pContext->cbClientHello = Input.cbData - sizeof(SSL2_MESSAGE_HEADER);
  2842. pContext->pClientHello = SPExternalAlloc(pContext->cbClientHello);
  2843. if(pContext->pClientHello == NULL)
  2844. {
  2845. pctRet = SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  2846. goto Ret;
  2847. }
  2848. CopyMemory(pContext->pClientHello,
  2849. (PUCHAR)Input.pvBuffer + sizeof(SSL2_MESSAGE_HEADER),
  2850. pContext->cbClientHello);
  2851. pContext->dwClientHelloProtocol = SP_PROT_SSL2_CLIENT;
  2852. /* keep challenge around for later */
  2853. CopyMemory( pContext->pChallenge,
  2854. pHello->Challenge,
  2855. pHello->cbChallenge);
  2856. pContext->cbChallenge = pHello->cbChallenge;
  2857. /* Initialize the "Client.random" from the challenge */
  2858. FillMemory(pContext->rgbS3CRandom, CB_SSL3_RANDOM - pContext->cbChallenge, 0);
  2859. CopyMemory( pContext->rgbS3CRandom + CB_SSL3_RANDOM - pContext->cbChallenge,
  2860. pContext->pChallenge,
  2861. pContext->cbChallenge);
  2862. //
  2863. // We know that this isn't a reconnect, so allocate a new cache entry.
  2864. //
  2865. if(!SPCacheRetrieveNew(TRUE,
  2866. pContext->pszTarget,
  2867. &pContext->RipeZombie))
  2868. {
  2869. pctRet = SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  2870. goto Ret;
  2871. }
  2872. pContext->RipeZombie->fProtocol = pContext->dwProtocol;
  2873. pContext->RipeZombie->dwCF = pContext->dwRequestedCF;
  2874. SPCacheAssignNewServerCredential(pContext->RipeZombie,
  2875. pContext->pCredGroup);
  2876. //
  2877. // Determine cipher suite to use.
  2878. //
  2879. pctRet = Ssl3SelectCipherEx(pContext,
  2880. pHello->CipherSpecs,
  2881. pHello->cCipherSpecs);
  2882. if (pctRet != PCT_ERR_OK)
  2883. {
  2884. goto Ret;
  2885. }
  2886. pContext->State = SSL3_STATE_GEN_SERVER_HELLO;
  2887. Ret:
  2888. if(NULL != pHello)
  2889. {
  2890. SPExternalFree(pHello);
  2891. }
  2892. SP_RETURN( pctRet );
  2893. }
  2894. /*
  2895. ***************************************************************************
  2896. Build Server hello onto pb... we need to check the boundary condition with cb
  2897. ****************************************************************************
  2898. */
  2899. void
  2900. Ssl3BuildServerHello(PSPContext pContext, PBYTE pb)
  2901. {
  2902. SSH *pssh = (SSH *) pb;
  2903. WORD wT = sizeof(SSH) - sizeof(SHSH);
  2904. DWORD dwCipher = UniAvailableCiphers[pContext->dwPendingCipherSuiteIndex].CipherKind;
  2905. FillMemory(pssh, sizeof(SSH), 0);
  2906. pssh->typHS = SSL3_HS_SERVER_HELLO;
  2907. pssh->bcbMSB = MSBOF(wT) ;
  2908. pssh->bcbLSB = LSBOF(wT) ;
  2909. pssh->bMajor = SSL3_CLIENT_VERSION_MSB;
  2910. if(pContext->RipeZombie->fProtocol == SP_PROT_SSL3_SERVER)
  2911. {
  2912. pssh->bMinor = (UCHAR)SSL3_CLIENT_VERSION_LSB;
  2913. }
  2914. else
  2915. {
  2916. pssh->bMinor = (UCHAR)TLS1_CLIENT_VERSION_LSB;
  2917. }
  2918. pssh->wCipherSelectedMSB = MSBOF(dwCipher);
  2919. pssh->wCipherSelectedLSB = LSBOF(dwCipher);
  2920. pssh->cbSessionId = (char)pContext->RipeZombie->cbSessionID;
  2921. CopyMemory(pssh->rgbSessionId, pContext->RipeZombie->SessionID, pContext->RipeZombie->cbSessionID) ;
  2922. CopyMemory(pssh->rgbRandom, pContext->rgbS3SRandom, CB_SSL3_RANDOM);
  2923. }
  2924. /*
  2925. ***************************************************************************
  2926. Build Server Hello Done message
  2927. ****************************************************************************
  2928. */
  2929. void
  2930. BuildServerHelloDone(
  2931. PBYTE pb)
  2932. {
  2933. SHSH *pshsh = (SHSH *) pb ;
  2934. // struct { } ServerHelloDone;
  2935. SP_BEGIN("BuildServerHelloDone");
  2936. FillMemory(pshsh, sizeof(SHSH), 0);
  2937. pshsh->typHS = SSL3_HS_SERVER_HELLO_DONE;
  2938. SP_END();
  2939. }
  2940. //+---------------------------------------------------------------------------
  2941. //
  2942. // Function: ParseKeyExchgMsg
  2943. //
  2944. // Synopsis: Parse the ClientKeyExchange message.
  2945. //
  2946. // Arguments: [pContext] -- Schannel context.
  2947. // [pb] -- Pointer to message's 4-byte handshake
  2948. // header.
  2949. //
  2950. // History: 10-03-97 jbanes Server-side CAPI integration.
  2951. //
  2952. // Notes: This routine is called by the server-side only.
  2953. //
  2954. //----------------------------------------------------------------------------
  2955. SP_STATUS
  2956. ParseKeyExchgMsg(
  2957. PSPContext pContext,
  2958. PBYTE pb)
  2959. {
  2960. SP_STATUS pctRet;
  2961. DWORD cbEncryptedKey;
  2962. PBYTE pbEncryptedKey;
  2963. // check for correct state
  2964. if(SSL2_STATE_SERVER_HELLO == pContext->State && pContext->fCertReq)
  2965. {
  2966. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  2967. }
  2968. // make sure we're a server
  2969. if(!(pContext->pKeyExchInfo->fProtocol & SP_PROT_SSL3TLS1_CLIENTS))
  2970. {
  2971. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2972. }
  2973. if(*pb != SSL3_HS_CLIENT_KEY_EXCHANGE)
  2974. {
  2975. return SP_LOG_RESULT(PCT_ERR_ILLEGAL_MESSAGE);
  2976. }
  2977. cbEncryptedKey = ((INT)pb[1] << 16) + ((INT)pb[2] << 8) + (INT)pb[3];
  2978. pbEncryptedKey = pb + (sizeof(SHSH)) ;
  2979. if(pContext->pKeyExchInfo == NULL)
  2980. {
  2981. return SP_LOG_RESULT(PCT_INT_INTERNAL_ERROR);
  2982. }
  2983. /* Decrypt the encrypted portion of the master key */
  2984. pctRet = pContext->pKeyExchInfo->System->GenerateServerMasterKey(
  2985. pContext,
  2986. NULL,
  2987. 0,
  2988. pbEncryptedKey,
  2989. cbEncryptedKey);
  2990. if(pctRet != PCT_ERR_OK)
  2991. {
  2992. return SP_LOG_RESULT(pctRet);
  2993. }
  2994. pContext->State = SSL3_STATE_SERVER_KEY_XCHANGE;
  2995. return PCT_ERR_OK;
  2996. }
  2997. SP_STATUS
  2998. UpdateAndDuplicateIssuerList(
  2999. PSPCredentialGroup pCredGroup,
  3000. PBYTE * ppbIssuerList,
  3001. PDWORD pcbIssuerList)
  3002. {
  3003. SP_STATUS pctRet;
  3004. LockCredentialExclusive(pCredGroup);
  3005. *ppbIssuerList = NULL;
  3006. *pcbIssuerList = 0;
  3007. // Check for GP update from the domain controller.
  3008. SslCheckForGPEvent();
  3009. // Build list of trusted issuers.
  3010. if((pCredGroup->pbTrustedIssuers == NULL) ||
  3011. (pCredGroup->dwFlags & CRED_FLAG_UPDATE_ISSUER_LIST))
  3012. {
  3013. pctRet = SPContextGetIssuers(pCredGroup);
  3014. if(pctRet != PCT_ERR_OK)
  3015. {
  3016. UnlockCredential(pCredGroup);
  3017. return SP_LOG_RESULT(pctRet);
  3018. }
  3019. }
  3020. // Allocate memory.
  3021. *ppbIssuerList = SPExternalAlloc(pCredGroup->cbTrustedIssuers);
  3022. if(*ppbIssuerList == NULL)
  3023. {
  3024. UnlockCredential(pCredGroup);
  3025. return SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  3026. }
  3027. // Copy issuer list.
  3028. memcpy(*ppbIssuerList, pCredGroup->pbTrustedIssuers, pCredGroup->cbTrustedIssuers);
  3029. *pcbIssuerList = pCredGroup->cbTrustedIssuers;
  3030. UnlockCredential(pCredGroup);
  3031. return PCT_ERR_OK;
  3032. }
  3033. /*
  3034. * *****************************************************************************
  3035. * Ssl3BuildCertificateRequest
  3036. *
  3037. * Build the CERTIFICATE_REQUEST handshake message.
  3038. */
  3039. SP_STATUS
  3040. Ssl3BuildCertificateRequest(
  3041. PBYTE pbIssuerList, // in
  3042. DWORD cbIssuerList, // in
  3043. PBYTE pbMessage, // out
  3044. DWORD *pdwMessageLen) // out
  3045. {
  3046. PBYTE pbMessageStart = pbMessage;
  3047. DWORD dwBodyLength;
  3048. // HandshakeType
  3049. pbMessage[0] = SSL3_HS_CERTIFICATE_REQUEST;
  3050. pbMessage += 1;
  3051. // Skip message body length field (3 bytes)
  3052. pbMessage += 3;
  3053. //
  3054. // enum {
  3055. // rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
  3056. // rsa_ephemeral_dh(5), dss_ephemeral_dh(6), fortezza_dms(20), (255)
  3057. // } ClientCertificateType;
  3058. //
  3059. // opaque DistinguishedName<1..2^16-1>;
  3060. //
  3061. // struct {
  3062. // ClientCertificateType certificate_types<1..2^8-1>;
  3063. // DistinguishedName certificate_authorities<3..2^16-1>;
  3064. // } CertificateRequest;
  3065. //
  3066. // Certificate type
  3067. pbMessage[0] = 2; // certificate type vector length
  3068. pbMessage[1] = SSL3_CERTTYPE_RSA_SIGN;
  3069. pbMessage[2] = SSL3_CERTTYPE_DSS_SIGN;
  3070. pbMessage += 3;
  3071. // Trusted issuer list length
  3072. pbMessage[0] = MSBOF(cbIssuerList);
  3073. pbMessage[1] = LSBOF(cbIssuerList);
  3074. pbMessage += 2;
  3075. // Trusted issuer list
  3076. if(pbIssuerList != NULL)
  3077. {
  3078. CopyMemory(pbMessage, pbIssuerList, cbIssuerList);
  3079. pbMessage += cbIssuerList;
  3080. }
  3081. // Compute message body length (subtract 4 byte header)
  3082. dwBodyLength = (DWORD)(pbMessage - pbMessageStart) - 4;
  3083. // Fill in message body length field (3 bytes)
  3084. pbMessageStart[1] = (UCHAR) ((dwBodyLength & 0x00ff0000) >> 16);
  3085. pbMessageStart[2] = MSBOF(dwBodyLength);
  3086. pbMessageStart[3] = LSBOF(dwBodyLength);
  3087. *pdwMessageLen = dwBodyLength + 4;
  3088. return PCT_ERR_OK;
  3089. }
  3090. /*
  3091. * *****************************************************************************
  3092. * Ssl3ParseClientHello
  3093. *
  3094. * This routine parses just the CLIENT_HELLO message itself. The
  3095. * handshake crud has already been stripped off.
  3096. */
  3097. BOOL Ssl3ParseClientHello(
  3098. PSPContext pContext,
  3099. PBYTE pbMessage,
  3100. DWORD cbMessage,
  3101. BOOL fAttemptReconnect,
  3102. BOOL * pfReconnect)
  3103. {
  3104. DWORD dwVersion;
  3105. PBYTE pbSessionId;
  3106. DWORD cbSessionId;
  3107. DWORD cbCipherSpecLen;
  3108. DWORD cbCompMethodLen;
  3109. INT iCompMethod;
  3110. DWORD i;
  3111. SP_STATUS pctRet = PCT_ERR_OK;
  3112. DWORD dwProtocol = SP_PROT_SSL3_SERVER;
  3113. Ssl2_Cipher_Kind CipherSpecs[MAX_UNI_CIPHERS];
  3114. DWORD cCipherSpecs;
  3115. DWORD dwCacheCipher;
  3116. BOOL fFound;
  3117. BOOL fReconnect = FALSE;
  3118. //
  3119. // struct {
  3120. // ProtocolVersion client_version;
  3121. // Random random;
  3122. // SessinoID session_id;
  3123. // CipherSuite cipher_suites<2..2^16-1>
  3124. // CompressionMethod compression_methods<1..2^8-1>;
  3125. // } ClientHello;
  3126. //
  3127. *pfReconnect = FALSE;
  3128. //
  3129. // Parse the ClientHello message.
  3130. //
  3131. // ProtocolVersion = client_version;
  3132. if(cbMessage < 2)
  3133. {
  3134. return FALSE;
  3135. }
  3136. dwVersion = ((DWORD)pbMessage[0] << 8) + pbMessage[1];
  3137. if(dwVersion < SSL3_CLIENT_VERSION)
  3138. {
  3139. return FALSE;
  3140. }
  3141. //see if it's a TLS 1 version !
  3142. if(dwVersion >= TLS1_CLIENT_VERSION)
  3143. dwProtocol = SP_PROT_TLS1_SERVER;
  3144. pbMessage += 2;
  3145. cbMessage -= 2;
  3146. // Random random
  3147. if(cbMessage < CB_SSL3_RANDOM)
  3148. {
  3149. return FALSE;
  3150. }
  3151. CopyMemory(pContext->rgbS3CRandom, pbMessage, CB_SSL3_RANDOM);
  3152. pContext->cbChallenge = CB_SSL3_RANDOM;
  3153. pbMessage += CB_SSL3_RANDOM;
  3154. cbMessage -= CB_SSL3_RANDOM;
  3155. // SessionID session_id; (length)
  3156. if(cbMessage < 1)
  3157. {
  3158. return FALSE;
  3159. }
  3160. cbSessionId = pbMessage[0];
  3161. if(cbSessionId > CB_SSL3_SESSION_ID)
  3162. {
  3163. return FALSE;
  3164. }
  3165. pbMessage += 1;
  3166. cbMessage -= 1;
  3167. // SessionID session_id;
  3168. if(cbMessage < cbSessionId)
  3169. {
  3170. return FALSE;
  3171. }
  3172. pbSessionId = pbMessage;
  3173. pbMessage += cbSessionId;
  3174. cbMessage -= cbSessionId;
  3175. // CipherSuite cipher_suites<2..2^16-1>; (length)
  3176. if(cbMessage < 2)
  3177. {
  3178. return FALSE;
  3179. }
  3180. cbCipherSpecLen = ((INT)pbMessage[0] << 8) + pbMessage[1];
  3181. if(cbCipherSpecLen % 2)
  3182. {
  3183. return FALSE;
  3184. }
  3185. pbMessage += 2;
  3186. cbMessage -= 2;
  3187. if(cbMessage < cbCipherSpecLen)
  3188. {
  3189. return FALSE;
  3190. }
  3191. if(cbCipherSpecLen %2)
  3192. {
  3193. return FALSE;
  3194. }
  3195. // CipherSuite cipher_suites<2..2^16-1>;
  3196. if(cbCipherSpecLen / 2 > MAX_UNI_CIPHERS)
  3197. {
  3198. cCipherSpecs = MAX_UNI_CIPHERS;
  3199. }
  3200. else
  3201. {
  3202. cCipherSpecs = cbCipherSpecLen / 2;
  3203. }
  3204. // Build list of client cipher suites.
  3205. for(i = 0; i < cCipherSpecs; i++)
  3206. {
  3207. CipherSpecs[i] = COMBINEBYTES(pbMessage[i*2], pbMessage[(i*2)+1]);
  3208. }
  3209. pbMessage += cbCipherSpecLen;
  3210. cbMessage -= cbCipherSpecLen;
  3211. // CompressionMethod compression_methods<1..2^8-1>; (length)
  3212. if(cbMessage < 1)
  3213. {
  3214. return FALSE;
  3215. }
  3216. cbCompMethodLen = pbMessage[0];
  3217. if(cbCompMethodLen < 1)
  3218. {
  3219. return FALSE;
  3220. }
  3221. pbMessage += 1;
  3222. cbMessage -= 1;
  3223. if(cbMessage < cbCompMethodLen)
  3224. {
  3225. return FALSE;
  3226. }
  3227. iCompMethod = -1;
  3228. for(i = 0 ; i <cbCompMethodLen; i++)
  3229. {
  3230. if(pbMessage[i] == 0)
  3231. {
  3232. iCompMethod = 0;
  3233. break;
  3234. }
  3235. }
  3236. pbMessage += cbCompMethodLen;
  3237. cbMessage -= cbCompMethodLen;
  3238. if(iCompMethod != 0)
  3239. {
  3240. return FALSE;
  3241. }
  3242. //
  3243. // Check to see if this is a reconnect.
  3244. //
  3245. if(((pContext->Flags & CONTEXT_FLAG_NOCACHE) == 0) &&
  3246. (cbSessionId > 0) &&
  3247. fAttemptReconnect)
  3248. {
  3249. fReconnect = SPCacheRetrieveBySession(
  3250. pContext,
  3251. pbSessionId,
  3252. cbSessionId,
  3253. &pContext->RipeZombie);
  3254. if(fReconnect)
  3255. {
  3256. // Make sure client's cipher suite list includes one from cache.
  3257. fFound = FALSE;
  3258. dwCacheCipher = UniAvailableCiphers[pContext->RipeZombie->dwCipherSuiteIndex].CipherKind;
  3259. for(i = 0; i < cCipherSpecs; i++)
  3260. {
  3261. if(CipherSpecs[i] == dwCacheCipher)
  3262. {
  3263. fFound = TRUE;
  3264. break;
  3265. }
  3266. }
  3267. if(fFound)
  3268. {
  3269. // Transfer information from the cache entry to the context element.
  3270. pctRet = ContextInitCiphersFromCache(pContext);
  3271. }
  3272. if(!fFound || pctRet != PCT_ERR_OK)
  3273. {
  3274. // This cache entry is not suitable for some reason. We need
  3275. // to dump this cache entry and perform a full handshake.
  3276. // This is typically caused by a client-side implementation
  3277. // problem.
  3278. pContext->RipeZombie->ZombieJuju = FALSE;
  3279. SPCacheDereference(pContext->RipeZombie);
  3280. pContext->RipeZombie = NULL;
  3281. fReconnect = FALSE;
  3282. }
  3283. }
  3284. }
  3285. if(fReconnect)
  3286. {
  3287. // We're doing a reconnect.
  3288. DebugLog((DEB_TRACE, "Accept client's reconnect request.\n"));
  3289. *pfReconnect = TRUE;
  3290. }
  3291. else
  3292. {
  3293. // We're doing a full handshake, so allocate a cache entry.
  3294. if(!SPCacheRetrieveNew(TRUE,
  3295. pContext->pszTarget,
  3296. &pContext->RipeZombie))
  3297. {
  3298. SP_LOG_RESULT(SEC_E_INSUFFICIENT_MEMORY);
  3299. return FALSE;
  3300. }
  3301. pContext->RipeZombie->fProtocol = pContext->dwProtocol;
  3302. pContext->RipeZombie->dwCF = pContext->dwRequestedCF;
  3303. SPCacheAssignNewServerCredential(pContext->RipeZombie,
  3304. pContext->pCredGroup);
  3305. //
  3306. // Select cipher suite to use.
  3307. //
  3308. pctRet = Ssl3SelectCipherEx(pContext,
  3309. CipherSpecs,
  3310. cCipherSpecs);
  3311. if (pctRet != PCT_ERR_OK)
  3312. {
  3313. return FALSE;
  3314. }
  3315. }
  3316. return TRUE;
  3317. }