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

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