PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/ssl/ssl3ext.c

http://github.com/zpao/v8monkey
C | 1649 lines | 1230 code | 175 blank | 244 comment | 378 complexity | f231d734ef3053f506af435dfc1388c7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /*
  2. * SSL3 Protocol
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is the Netscape security libraries.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Dr Vipul Gupta <vipul.gupta@sun.com> and
  26. * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
  27. * Nagendra Modadugu <ngm@google.com>, Google Inc.
  28. *
  29. * Alternatively, the contents of this file may be used under the terms of
  30. * either the GNU General Public License Version 2 or later (the "GPL"), or
  31. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  32. * in which case the provisions of the GPL or the LGPL are applicable instead
  33. * of those above. If you wish to allow use of your version of this file only
  34. * under the terms of either the GPL or the LGPL, and not to allow others to
  35. * use your version of this file under the terms of the MPL, indicate your
  36. * decision by deleting the provisions above and replace them with the notice
  37. * and other provisions required by the GPL or the LGPL. If you do not delete
  38. * the provisions above, a recipient may use your version of this file under
  39. * the terms of any one of the MPL, the GPL or the LGPL.
  40. *
  41. * ***** END LICENSE BLOCK ***** */
  42. /* TLS extension code moved here from ssl3ecc.c */
  43. /* $Id: ssl3ext.c,v 1.20 2011/11/16 19:12:35 kaie%kuix.de Exp $ */
  44. #include "nssrenam.h"
  45. #include "nss.h"
  46. #include "ssl.h"
  47. #include "sslproto.h"
  48. #include "sslimpl.h"
  49. #include "pk11pub.h"
  50. #include "blapi.h"
  51. #include "prinit.h"
  52. static unsigned char key_name[SESS_TICKET_KEY_NAME_LEN];
  53. static PK11SymKey *session_ticket_enc_key_pkcs11 = NULL;
  54. static PK11SymKey *session_ticket_mac_key_pkcs11 = NULL;
  55. static unsigned char session_ticket_enc_key[AES_256_KEY_LENGTH];
  56. static unsigned char session_ticket_mac_key[SHA256_LENGTH];
  57. static PRBool session_ticket_keys_initialized = PR_FALSE;
  58. static PRCallOnceType generate_session_keys_once;
  59. /* forward static function declarations */
  60. static SECStatus ssl3_ParseEncryptedSessionTicket(sslSocket *ss,
  61. SECItem *data, EncryptedSessionTicket *enc_session_ticket);
  62. static SECStatus ssl3_AppendToItem(SECItem *item, const unsigned char *buf,
  63. PRUint32 bytes);
  64. static SECStatus ssl3_AppendNumberToItem(SECItem *item, PRUint32 num,
  65. PRInt32 lenSize);
  66. static SECStatus ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss,
  67. PK11SymKey **aes_key, PK11SymKey **mac_key);
  68. static SECStatus ssl3_GetSessionTicketKeys(const unsigned char **aes_key,
  69. PRUint32 *aes_key_length, const unsigned char **mac_key,
  70. PRUint32 *mac_key_length);
  71. static PRInt32 ssl3_SendRenegotiationInfoXtn(sslSocket * ss,
  72. PRBool append, PRUint32 maxBytes);
  73. static SECStatus ssl3_HandleRenegotiationInfoXtn(sslSocket *ss,
  74. PRUint16 ex_type, SECItem *data);
  75. static SECStatus ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss,
  76. PRUint16 ex_type, SECItem *data);
  77. static SECStatus ssl3_ServerHandleNextProtoNegoXtn(sslSocket *ss,
  78. PRUint16 ex_type, SECItem *data);
  79. static PRInt32 ssl3_ClientSendNextProtoNegoXtn(sslSocket *ss, PRBool append,
  80. PRUint32 maxBytes);
  81. /*
  82. * Write bytes. Using this function means the SECItem structure
  83. * cannot be freed. The caller is expected to call this function
  84. * on a shallow copy of the structure.
  85. */
  86. static SECStatus
  87. ssl3_AppendToItem(SECItem *item, const unsigned char *buf, PRUint32 bytes)
  88. {
  89. if (bytes > item->len)
  90. return SECFailure;
  91. PORT_Memcpy(item->data, buf, bytes);
  92. item->data += bytes;
  93. item->len -= bytes;
  94. return SECSuccess;
  95. }
  96. /*
  97. * Write a number in network byte order. Using this function means the
  98. * SECItem structure cannot be freed. The caller is expected to call
  99. * this function on a shallow copy of the structure.
  100. */
  101. static SECStatus
  102. ssl3_AppendNumberToItem(SECItem *item, PRUint32 num, PRInt32 lenSize)
  103. {
  104. SECStatus rv;
  105. uint8 b[4];
  106. uint8 * p = b;
  107. switch (lenSize) {
  108. case 4:
  109. *p++ = (uint8) (num >> 24);
  110. case 3:
  111. *p++ = (uint8) (num >> 16);
  112. case 2:
  113. *p++ = (uint8) (num >> 8);
  114. case 1:
  115. *p = (uint8) num;
  116. }
  117. rv = ssl3_AppendToItem(item, &b[0], lenSize);
  118. return rv;
  119. }
  120. static SECStatus ssl3_SessionTicketShutdown(void* appData, void* nssData)
  121. {
  122. if (session_ticket_enc_key_pkcs11) {
  123. PK11_FreeSymKey(session_ticket_enc_key_pkcs11);
  124. session_ticket_enc_key_pkcs11 = NULL;
  125. }
  126. if (session_ticket_mac_key_pkcs11) {
  127. PK11_FreeSymKey(session_ticket_mac_key_pkcs11);
  128. session_ticket_mac_key_pkcs11 = NULL;
  129. }
  130. PORT_Memset(&generate_session_keys_once, 0,
  131. sizeof(generate_session_keys_once));
  132. return SECSuccess;
  133. }
  134. static PRStatus
  135. ssl3_GenerateSessionTicketKeysPKCS11(void *data)
  136. {
  137. SECStatus rv;
  138. sslSocket *ss = (sslSocket *)data;
  139. SECKEYPrivateKey *svrPrivKey = ss->serverCerts[kt_rsa].SERVERKEY;
  140. SECKEYPublicKey *svrPubKey = ss->serverCerts[kt_rsa].serverKeyPair->pubKey;
  141. if (svrPrivKey == NULL || svrPubKey == NULL) {
  142. SSL_DBG(("%d: SSL[%d]: Pub or priv key(s) is NULL.",
  143. SSL_GETPID(), ss->fd));
  144. goto loser;
  145. }
  146. /* Get a copy of the session keys from shared memory. */
  147. PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX,
  148. sizeof(SESS_TICKET_KEY_NAME_PREFIX));
  149. if (!ssl_GetSessionTicketKeysPKCS11(svrPrivKey, svrPubKey,
  150. ss->pkcs11PinArg, &key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN],
  151. &session_ticket_enc_key_pkcs11, &session_ticket_mac_key_pkcs11))
  152. return PR_FAILURE;
  153. rv = NSS_RegisterShutdown(ssl3_SessionTicketShutdown, NULL);
  154. if (rv != SECSuccess)
  155. goto loser;
  156. return PR_SUCCESS;
  157. loser:
  158. ssl3_SessionTicketShutdown(NULL, NULL);
  159. return PR_FAILURE;
  160. }
  161. static SECStatus
  162. ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss, PK11SymKey **aes_key,
  163. PK11SymKey **mac_key)
  164. {
  165. if (PR_CallOnceWithArg(&generate_session_keys_once,
  166. ssl3_GenerateSessionTicketKeysPKCS11, ss) != PR_SUCCESS)
  167. return SECFailure;
  168. if (session_ticket_enc_key_pkcs11 == NULL ||
  169. session_ticket_mac_key_pkcs11 == NULL)
  170. return SECFailure;
  171. *aes_key = session_ticket_enc_key_pkcs11;
  172. *mac_key = session_ticket_mac_key_pkcs11;
  173. return SECSuccess;
  174. }
  175. static PRStatus
  176. ssl3_GenerateSessionTicketKeys(void)
  177. {
  178. PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX,
  179. sizeof(SESS_TICKET_KEY_NAME_PREFIX));
  180. if (!ssl_GetSessionTicketKeys(&key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN],
  181. session_ticket_enc_key, session_ticket_mac_key))
  182. return PR_FAILURE;
  183. session_ticket_keys_initialized = PR_TRUE;
  184. return PR_SUCCESS;
  185. }
  186. static SECStatus
  187. ssl3_GetSessionTicketKeys(const unsigned char **aes_key,
  188. PRUint32 *aes_key_length, const unsigned char **mac_key,
  189. PRUint32 *mac_key_length)
  190. {
  191. if (PR_CallOnce(&generate_session_keys_once,
  192. ssl3_GenerateSessionTicketKeys) != SECSuccess)
  193. return SECFailure;
  194. if (!session_ticket_keys_initialized)
  195. return SECFailure;
  196. *aes_key = session_ticket_enc_key;
  197. *aes_key_length = sizeof(session_ticket_enc_key);
  198. *mac_key = session_ticket_mac_key;
  199. *mac_key_length = sizeof(session_ticket_mac_key);
  200. return SECSuccess;
  201. }
  202. /* Table of handlers for received TLS hello extensions, one per extension.
  203. * In the second generation, this table will be dynamic, and functions
  204. * will be registered here.
  205. */
  206. /* This table is used by the server, to handle client hello extensions. */
  207. static const ssl3HelloExtensionHandler clientHelloHandlers[] = {
  208. { ssl_server_name_xtn, &ssl3_HandleServerNameXtn },
  209. #ifdef NSS_ENABLE_ECC
  210. { ssl_elliptic_curves_xtn, &ssl3_HandleSupportedCurvesXtn },
  211. { ssl_ec_point_formats_xtn, &ssl3_HandleSupportedPointFormatsXtn },
  212. #endif
  213. { ssl_session_ticket_xtn, &ssl3_ServerHandleSessionTicketXtn },
  214. { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
  215. { ssl_next_proto_neg_xtn, &ssl3_ServerHandleNextProtoNegoXtn },
  216. { -1, NULL }
  217. };
  218. /* These two tables are used by the client, to handle server hello
  219. * extensions. */
  220. static const ssl3HelloExtensionHandler serverHelloHandlersTLS[] = {
  221. { ssl_server_name_xtn, &ssl3_HandleServerNameXtn },
  222. /* TODO: add a handler for ssl_ec_point_formats_xtn */
  223. { ssl_session_ticket_xtn, &ssl3_ClientHandleSessionTicketXtn },
  224. { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
  225. { ssl_next_proto_neg_xtn, &ssl3_ClientHandleNextProtoNegoXtn },
  226. { -1, NULL }
  227. };
  228. static const ssl3HelloExtensionHandler serverHelloHandlersSSL3[] = {
  229. { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
  230. { -1, NULL }
  231. };
  232. /* Tables of functions to format TLS hello extensions, one function per
  233. * extension.
  234. * These static tables are for the formatting of client hello extensions.
  235. * The server's table of hello senders is dynamic, in the socket struct,
  236. * and sender functions are registered there.
  237. */
  238. static const
  239. ssl3HelloExtensionSender clientHelloSendersTLS[SSL_MAX_EXTENSIONS] = {
  240. { ssl_server_name_xtn, &ssl3_SendServerNameXtn },
  241. { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn },
  242. #ifdef NSS_ENABLE_ECC
  243. { ssl_elliptic_curves_xtn, &ssl3_SendSupportedCurvesXtn },
  244. { ssl_ec_point_formats_xtn, &ssl3_SendSupportedPointFormatsXtn },
  245. #endif
  246. { ssl_session_ticket_xtn, &ssl3_SendSessionTicketXtn },
  247. { ssl_next_proto_neg_xtn, &ssl3_ClientSendNextProtoNegoXtn }
  248. /* any extra entries will appear as { 0, NULL } */
  249. };
  250. static const
  251. ssl3HelloExtensionSender clientHelloSendersSSL3[SSL_MAX_EXTENSIONS] = {
  252. { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn }
  253. /* any extra entries will appear as { 0, NULL } */
  254. };
  255. static PRBool
  256. arrayContainsExtension(const PRUint16 *array, PRUint32 len, PRUint16 ex_type)
  257. {
  258. int i;
  259. for (i = 0; i < len; i++) {
  260. if (ex_type == array[i])
  261. return PR_TRUE;
  262. }
  263. return PR_FALSE;
  264. }
  265. PRBool
  266. ssl3_ExtensionNegotiated(sslSocket *ss, PRUint16 ex_type) {
  267. TLSExtensionData *xtnData = &ss->xtnData;
  268. return arrayContainsExtension(xtnData->negotiated,
  269. xtnData->numNegotiated, ex_type);
  270. }
  271. static PRBool
  272. ssl3_ClientExtensionAdvertised(sslSocket *ss, PRUint16 ex_type) {
  273. TLSExtensionData *xtnData = &ss->xtnData;
  274. return arrayContainsExtension(xtnData->advertised,
  275. xtnData->numAdvertised, ex_type);
  276. }
  277. /* Format an SNI extension, using the name from the socket's URL,
  278. * unless that name is a dotted decimal string.
  279. * Used by client and server.
  280. */
  281. PRInt32
  282. ssl3_SendServerNameXtn(sslSocket * ss, PRBool append,
  283. PRUint32 maxBytes)
  284. {
  285. SECStatus rv;
  286. if (!ss)
  287. return 0;
  288. if (!ss->sec.isServer) {
  289. PRUint32 len;
  290. PRNetAddr netAddr;
  291. /* must have a hostname */
  292. if (!ss->url || !ss->url[0])
  293. return 0;
  294. /* must not be an IPv4 or IPv6 address */
  295. if (PR_SUCCESS == PR_StringToNetAddr(ss->url, &netAddr)) {
  296. /* is an IP address (v4 or v6) */
  297. return 0;
  298. }
  299. len = PORT_Strlen(ss->url);
  300. if (append && maxBytes >= len + 9) {
  301. /* extension_type */
  302. rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2);
  303. if (rv != SECSuccess) return -1;
  304. /* length of extension_data */
  305. rv = ssl3_AppendHandshakeNumber(ss, len + 5, 2);
  306. if (rv != SECSuccess) return -1;
  307. /* length of server_name_list */
  308. rv = ssl3_AppendHandshakeNumber(ss, len + 3, 2);
  309. if (rv != SECSuccess) return -1;
  310. /* Name Type (sni_host_name) */
  311. rv = ssl3_AppendHandshake(ss, "\0", 1);
  312. if (rv != SECSuccess) return -1;
  313. /* HostName (length and value) */
  314. rv = ssl3_AppendHandshakeVariable(ss, (PRUint8 *)ss->url, len, 2);
  315. if (rv != SECSuccess) return -1;
  316. if (!ss->sec.isServer) {
  317. TLSExtensionData *xtnData = &ss->xtnData;
  318. xtnData->advertised[xtnData->numAdvertised++] =
  319. ssl_server_name_xtn;
  320. }
  321. }
  322. return len + 9;
  323. }
  324. /* Server side */
  325. if (append && maxBytes >= 4) {
  326. rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2);
  327. if (rv != SECSuccess) return -1;
  328. /* length of extension_data */
  329. rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
  330. if (rv != SECSuccess) return -1;
  331. }
  332. return 4;
  333. }
  334. /* handle an incoming SNI extension, by ignoring it. */
  335. SECStatus
  336. ssl3_HandleServerNameXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data)
  337. {
  338. SECItem *names = NULL;
  339. PRUint32 listCount = 0, namesPos = 0, i;
  340. TLSExtensionData *xtnData = &ss->xtnData;
  341. SECItem ldata;
  342. PRInt32 listLenBytes = 0;
  343. if (!ss->sec.isServer) {
  344. /* Verify extension_data is empty. */
  345. if (data->data || data->len ||
  346. !ssl3_ExtensionNegotiated(ss, ssl_server_name_xtn)) {
  347. /* malformed or was not initiated by the client.*/
  348. return SECFailure;
  349. }
  350. return SECSuccess;
  351. }
  352. /* Server side - consume client data and register server sender. */
  353. /* do not parse the data if don't have user extension handling function. */
  354. if (!ss->sniSocketConfig) {
  355. return SECSuccess;
  356. }
  357. /* length of server_name_list */
  358. listLenBytes = ssl3_ConsumeHandshakeNumber(ss, 2, &data->data, &data->len);
  359. if (listLenBytes == 0 || listLenBytes != data->len) {
  360. return SECFailure;
  361. }
  362. ldata = *data;
  363. /* Calculate the size of the array.*/
  364. while (listLenBytes > 0) {
  365. SECItem litem;
  366. SECStatus rv;
  367. PRInt32 type;
  368. /* Name Type (sni_host_name) */
  369. type = ssl3_ConsumeHandshakeNumber(ss, 1, &ldata.data, &ldata.len);
  370. if (!ldata.len) {
  371. return SECFailure;
  372. }
  373. rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 2, &ldata.data, &ldata.len);
  374. if (rv != SECSuccess) {
  375. return SECFailure;
  376. }
  377. /* Adjust total length for cunsumed item, item len and type.*/
  378. listLenBytes -= litem.len + 3;
  379. if (listLenBytes > 0 && !ldata.len) {
  380. return SECFailure;
  381. }
  382. listCount += 1;
  383. }
  384. if (!listCount) {
  385. return SECFailure;
  386. }
  387. names = PORT_ZNewArray(SECItem, listCount);
  388. if (!names) {
  389. return SECFailure;
  390. }
  391. for (i = 0;i < listCount;i++) {
  392. int j;
  393. PRInt32 type;
  394. SECStatus rv;
  395. PRBool nametypePresent = PR_FALSE;
  396. /* Name Type (sni_host_name) */
  397. type = ssl3_ConsumeHandshakeNumber(ss, 1, &data->data, &data->len);
  398. /* Check if we have such type in the list */
  399. for (j = 0;j < listCount && names[j].data;j++) {
  400. if (names[j].type == type) {
  401. nametypePresent = PR_TRUE;
  402. break;
  403. }
  404. }
  405. /* HostName (length and value) */
  406. rv = ssl3_ConsumeHandshakeVariable(ss, &names[namesPos], 2,
  407. &data->data, &data->len);
  408. if (rv != SECSuccess) {
  409. goto loser;
  410. }
  411. if (nametypePresent == PR_FALSE) {
  412. namesPos += 1;
  413. }
  414. }
  415. /* Free old and set the new data. */
  416. if (xtnData->sniNameArr) {
  417. PORT_Free(ss->xtnData.sniNameArr);
  418. }
  419. xtnData->sniNameArr = names;
  420. xtnData->sniNameArrSize = namesPos;
  421. xtnData->negotiated[xtnData->numNegotiated++] = ssl_server_name_xtn;
  422. return SECSuccess;
  423. loser:
  424. PORT_Free(names);
  425. return SECFailure;
  426. }
  427. /* Called by both clients and servers.
  428. * Clients sends a filled in session ticket if one is available, and otherwise
  429. * sends an empty ticket. Servers always send empty tickets.
  430. */
  431. PRInt32
  432. ssl3_SendSessionTicketXtn(
  433. sslSocket * ss,
  434. PRBool append,
  435. PRUint32 maxBytes)
  436. {
  437. PRInt32 extension_length;
  438. NewSessionTicket *session_ticket = NULL;
  439. /* Ignore the SessionTicket extension if processing is disabled. */
  440. if (!ss->opt.enableSessionTickets)
  441. return 0;
  442. /* Empty extension length = extension_type (2-bytes) +
  443. * length(extension_data) (2-bytes)
  444. */
  445. extension_length = 4;
  446. /* If we are a client then send a session ticket if one is availble.
  447. * Servers that support the extension and are willing to negotiate the
  448. * the extension always respond with an empty extension.
  449. */
  450. if (!ss->sec.isServer) {
  451. sslSessionID *sid = ss->sec.ci.sid;
  452. session_ticket = &sid->u.ssl3.sessionTicket;
  453. if (session_ticket->ticket.data) {
  454. if (ss->xtnData.ticketTimestampVerified) {
  455. extension_length += session_ticket->ticket.len;
  456. } else if (!append &&
  457. (session_ticket->ticket_lifetime_hint == 0 ||
  458. (session_ticket->ticket_lifetime_hint +
  459. session_ticket->received_timestamp > ssl_Time()))) {
  460. extension_length += session_ticket->ticket.len;
  461. ss->xtnData.ticketTimestampVerified = PR_TRUE;
  462. }
  463. }
  464. }
  465. if (append && maxBytes >= extension_length) {
  466. SECStatus rv;
  467. /* extension_type */
  468. rv = ssl3_AppendHandshakeNumber(ss, ssl_session_ticket_xtn, 2);
  469. if (rv != SECSuccess)
  470. goto loser;
  471. if (session_ticket && session_ticket->ticket.data &&
  472. ss->xtnData.ticketTimestampVerified) {
  473. rv = ssl3_AppendHandshakeVariable(ss, session_ticket->ticket.data,
  474. session_ticket->ticket.len, 2);
  475. ss->xtnData.ticketTimestampVerified = PR_FALSE;
  476. } else {
  477. rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
  478. }
  479. if (rv != SECSuccess)
  480. goto loser;
  481. if (!ss->sec.isServer) {
  482. TLSExtensionData *xtnData = &ss->xtnData;
  483. xtnData->advertised[xtnData->numAdvertised++] =
  484. ssl_session_ticket_xtn;
  485. }
  486. } else if (maxBytes < extension_length) {
  487. PORT_Assert(0);
  488. return 0;
  489. }
  490. return extension_length;
  491. loser:
  492. ss->xtnData.ticketTimestampVerified = PR_FALSE;
  493. return -1;
  494. }
  495. /* handle an incoming Next Protocol Negotiation extension. */
  496. static SECStatus
  497. ssl3_ServerHandleNextProtoNegoXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data)
  498. {
  499. if (ss->firstHsDone || data->len != 0) {
  500. /* Clients MUST send an empty NPN extension, if any. */
  501. PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
  502. return SECFailure;
  503. }
  504. return SECSuccess;
  505. }
  506. /* ssl3_ValidateNextProtoNego checks that the given block of data is valid: none
  507. * of the lengths may be 0 and the sum of the lengths must equal the length of
  508. * the block. */
  509. SECStatus
  510. ssl3_ValidateNextProtoNego(const unsigned char* data, unsigned int length)
  511. {
  512. unsigned int offset = 0;
  513. while (offset < length) {
  514. unsigned int newOffset = offset + 1 + (unsigned int) data[offset];
  515. /* Reject embedded nulls to protect against buggy applications that
  516. * store protocol identifiers in null-terminated strings.
  517. */
  518. if (newOffset > length || data[offset] == 0) {
  519. PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
  520. return SECFailure;
  521. }
  522. offset = newOffset;
  523. }
  524. if (offset > length) {
  525. PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
  526. return SECFailure;
  527. }
  528. return SECSuccess;
  529. }
  530. static SECStatus
  531. ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss, PRUint16 ex_type,
  532. SECItem *data)
  533. {
  534. SECStatus rv;
  535. unsigned char resultBuffer[255];
  536. SECItem result = { siBuffer, resultBuffer, 0 };
  537. if (ss->firstHsDone) {
  538. PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
  539. return SECFailure;
  540. }
  541. rv = ssl3_ValidateNextProtoNego(data->data, data->len);
  542. if (rv != SECSuccess)
  543. return rv;
  544. /* ss->nextProtoCallback cannot normally be NULL if we negotiated the
  545. * extension. However, It is possible that an application erroneously
  546. * cleared the callback between the time we sent the ClientHello and now.
  547. */
  548. PORT_Assert(ss->nextProtoCallback != NULL);
  549. if (!ss->nextProtoCallback) {
  550. PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
  551. return SECFailure;
  552. }
  553. rv = ss->nextProtoCallback(ss->nextProtoArg, ss->fd, data->data, data->len,
  554. result.data, &result.len, sizeof resultBuffer);
  555. if (rv != SECSuccess)
  556. return rv;
  557. /* If the callback wrote more than allowed to |result| it has corrupted our
  558. * stack. */
  559. if (result.len > sizeof result) {
  560. PORT_SetError(SEC_ERROR_OUTPUT_LEN);
  561. return SECFailure;
  562. }
  563. SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE);
  564. return SECITEM_CopyItem(NULL, &ss->ssl3.nextProto, &result);
  565. }
  566. static PRInt32
  567. ssl3_ClientSendNextProtoNegoXtn(sslSocket * ss, PRBool append,
  568. PRUint32 maxBytes)
  569. {
  570. PRInt32 extension_length;
  571. /* Renegotiations do not send this extension. */
  572. if (!ss->nextProtoCallback || ss->firstHsDone) {
  573. return 0;
  574. }
  575. extension_length = 4;
  576. if (append && maxBytes >= extension_length) {
  577. SECStatus rv;
  578. rv = ssl3_AppendHandshakeNumber(ss, ssl_next_proto_neg_xtn, 2);
  579. if (rv != SECSuccess)
  580. goto loser;
  581. rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
  582. if (rv != SECSuccess)
  583. goto loser;
  584. ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
  585. ssl_next_proto_neg_xtn;
  586. } else if (maxBytes < extension_length) {
  587. return 0;
  588. }
  589. return extension_length;
  590. loser:
  591. return -1;
  592. }
  593. /*
  594. * NewSessionTicket
  595. * Called from ssl3_HandleFinished
  596. */
  597. SECStatus
  598. ssl3_SendNewSessionTicket(sslSocket *ss)
  599. {
  600. int i;
  601. SECStatus rv;
  602. NewSessionTicket ticket;
  603. SECItem plaintext;
  604. SECItem plaintext_item = {0, NULL, 0};
  605. SECItem ciphertext = {0, NULL, 0};
  606. PRUint32 ciphertext_length;
  607. PRBool ms_is_wrapped;
  608. unsigned char wrapped_ms[SSL3_MASTER_SECRET_LENGTH];
  609. SECItem ms_item = {0, NULL, 0};
  610. SSL3KEAType effectiveExchKeyType = ssl_kea_null;
  611. PRUint32 padding_length;
  612. PRUint32 message_length;
  613. PRUint32 cert_length;
  614. uint8 length_buf[4];
  615. PRUint32 now;
  616. PK11SymKey *aes_key_pkcs11;
  617. PK11SymKey *mac_key_pkcs11;
  618. const unsigned char *aes_key;
  619. const unsigned char *mac_key;
  620. PRUint32 aes_key_length;
  621. PRUint32 mac_key_length;
  622. PRUint64 aes_ctx_buf[MAX_CIPHER_CONTEXT_LLONGS];
  623. AESContext *aes_ctx;
  624. CK_MECHANISM_TYPE cipherMech = CKM_AES_CBC;
  625. PK11Context *aes_ctx_pkcs11;
  626. const SECHashObject *hashObj = NULL;
  627. PRUint64 hmac_ctx_buf[MAX_MAC_CONTEXT_LLONGS];
  628. HMACContext *hmac_ctx;
  629. CK_MECHANISM_TYPE macMech = CKM_SHA256_HMAC;
  630. PK11Context *hmac_ctx_pkcs11;
  631. unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH];
  632. unsigned int computed_mac_length;
  633. unsigned char iv[AES_BLOCK_SIZE];
  634. SECItem ivItem;
  635. SECItem *srvName = NULL;
  636. PRUint32 srvNameLen = 0;
  637. CK_MECHANISM_TYPE msWrapMech = 0; /* dummy default value,
  638. * must be >= 0 */
  639. SSL_TRC(3, ("%d: SSL3[%d]: send session_ticket handshake",
  640. SSL_GETPID(), ss->fd));
  641. PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss));
  642. PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
  643. ticket.ticket_lifetime_hint = TLS_EX_SESS_TICKET_LIFETIME_HINT;
  644. cert_length = (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) ?
  645. 3 + ss->sec.ci.sid->peerCert->derCert.len : 0;
  646. /* Get IV and encryption keys */
  647. ivItem.data = iv;
  648. ivItem.len = sizeof(iv);
  649. rv = PK11_GenerateRandom(iv, sizeof(iv));
  650. if (rv != SECSuccess) goto loser;
  651. if (ss->opt.bypassPKCS11) {
  652. rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length,
  653. &mac_key, &mac_key_length);
  654. } else {
  655. rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11,
  656. &mac_key_pkcs11);
  657. }
  658. if (rv != SECSuccess) goto loser;
  659. if (ss->ssl3.pwSpec->msItem.len && ss->ssl3.pwSpec->msItem.data) {
  660. /* The master secret is available unwrapped. */
  661. ms_item.data = ss->ssl3.pwSpec->msItem.data;
  662. ms_item.len = ss->ssl3.pwSpec->msItem.len;
  663. ms_is_wrapped = PR_FALSE;
  664. } else {
  665. /* Extract the master secret wrapped. */
  666. sslSessionID sid;
  667. PORT_Memset(&sid, 0, sizeof(sslSessionID));
  668. if (ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa) {
  669. effectiveExchKeyType = kt_rsa;
  670. } else {
  671. effectiveExchKeyType = ss->ssl3.hs.kea_def->exchKeyType;
  672. }
  673. rv = ssl3_CacheWrappedMasterSecret(ss, &sid, ss->ssl3.pwSpec,
  674. effectiveExchKeyType);
  675. if (rv == SECSuccess) {
  676. if (sid.u.ssl3.keys.wrapped_master_secret_len > sizeof(wrapped_ms))
  677. goto loser;
  678. memcpy(wrapped_ms, sid.u.ssl3.keys.wrapped_master_secret,
  679. sid.u.ssl3.keys.wrapped_master_secret_len);
  680. ms_item.data = wrapped_ms;
  681. ms_item.len = sid.u.ssl3.keys.wrapped_master_secret_len;
  682. msWrapMech = sid.u.ssl3.masterWrapMech;
  683. } else {
  684. /* TODO: else send an empty ticket. */
  685. goto loser;
  686. }
  687. ms_is_wrapped = PR_TRUE;
  688. }
  689. /* Prep to send negotiated name */
  690. srvName = &ss->ssl3.pwSpec->srvVirtName;
  691. if (srvName->data && srvName->len) {
  692. srvNameLen = 2 + srvName->len; /* len bytes + name len */
  693. }
  694. ciphertext_length =
  695. sizeof(PRUint16) /* ticket_version */
  696. + sizeof(SSL3ProtocolVersion) /* ssl_version */
  697. + sizeof(ssl3CipherSuite) /* ciphersuite */
  698. + 1 /* compression */
  699. + 10 /* cipher spec parameters */
  700. + 1 /* SessionTicket.ms_is_wrapped */
  701. + 1 /* effectiveExchKeyType */
  702. + 4 /* msWrapMech */
  703. + 2 /* master_secret.length */
  704. + ms_item.len /* master_secret */
  705. + 1 /* client_auth_type */
  706. + cert_length /* cert */
  707. + 1 /* server name type */
  708. + srvNameLen /* name len + length field */
  709. + sizeof(ticket.ticket_lifetime_hint);
  710. padding_length = AES_BLOCK_SIZE -
  711. (ciphertext_length % AES_BLOCK_SIZE);
  712. ciphertext_length += padding_length;
  713. message_length =
  714. sizeof(ticket.ticket_lifetime_hint) /* ticket_lifetime_hint */
  715. + 2 /* length field for NewSessionTicket.ticket */
  716. + SESS_TICKET_KEY_NAME_LEN /* key_name */
  717. + AES_BLOCK_SIZE /* iv */
  718. + 2 /* length field for NewSessionTicket.ticket.encrypted_state */
  719. + ciphertext_length /* encrypted_state */
  720. + TLS_EX_SESS_TICKET_MAC_LENGTH; /* mac */
  721. if (SECITEM_AllocItem(NULL, &plaintext_item, ciphertext_length) == NULL)
  722. goto loser;
  723. plaintext = plaintext_item;
  724. /* ticket_version */
  725. rv = ssl3_AppendNumberToItem(&plaintext, TLS_EX_SESS_TICKET_VERSION,
  726. sizeof(PRUint16));
  727. if (rv != SECSuccess) goto loser;
  728. /* ssl_version */
  729. rv = ssl3_AppendNumberToItem(&plaintext, ss->version,
  730. sizeof(SSL3ProtocolVersion));
  731. if (rv != SECSuccess) goto loser;
  732. /* ciphersuite */
  733. rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.cipher_suite,
  734. sizeof(ssl3CipherSuite));
  735. if (rv != SECSuccess) goto loser;
  736. /* compression */
  737. rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.compression, 1);
  738. if (rv != SECSuccess) goto loser;
  739. /* cipher spec parameters */
  740. rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authAlgorithm, 1);
  741. if (rv != SECSuccess) goto loser;
  742. rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authKeyBits, 4);
  743. if (rv != SECSuccess) goto loser;
  744. rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaType, 1);
  745. if (rv != SECSuccess) goto loser;
  746. rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaKeyBits, 4);
  747. if (rv != SECSuccess) goto loser;
  748. /* master_secret */
  749. rv = ssl3_AppendNumberToItem(&plaintext, ms_is_wrapped, 1);
  750. if (rv != SECSuccess) goto loser;
  751. rv = ssl3_AppendNumberToItem(&plaintext, effectiveExchKeyType, 1);
  752. if (rv != SECSuccess) goto loser;
  753. rv = ssl3_AppendNumberToItem(&plaintext, msWrapMech, 4);
  754. if (rv != SECSuccess) goto loser;
  755. rv = ssl3_AppendNumberToItem(&plaintext, ms_item.len, 2);
  756. if (rv != SECSuccess) goto loser;
  757. rv = ssl3_AppendToItem(&plaintext, ms_item.data, ms_item.len);
  758. if (rv != SECSuccess) goto loser;
  759. /* client_identity */
  760. if (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) {
  761. rv = ssl3_AppendNumberToItem(&plaintext, CLIENT_AUTH_CERTIFICATE, 1);
  762. if (rv != SECSuccess) goto loser;
  763. rv = ssl3_AppendNumberToItem(&plaintext,
  764. ss->sec.ci.sid->peerCert->derCert.len, 3);
  765. if (rv != SECSuccess) goto loser;
  766. rv = ssl3_AppendToItem(&plaintext,
  767. ss->sec.ci.sid->peerCert->derCert.data,
  768. ss->sec.ci.sid->peerCert->derCert.len);
  769. if (rv != SECSuccess) goto loser;
  770. } else {
  771. rv = ssl3_AppendNumberToItem(&plaintext, 0, 1);
  772. if (rv != SECSuccess) goto loser;
  773. }
  774. /* timestamp */
  775. now = ssl_Time();
  776. rv = ssl3_AppendNumberToItem(&plaintext, now,
  777. sizeof(ticket.ticket_lifetime_hint));
  778. if (rv != SECSuccess) goto loser;
  779. if (srvNameLen) {
  780. /* Name Type (sni_host_name) */
  781. rv = ssl3_AppendNumberToItem(&plaintext, srvName->type, 1);
  782. if (rv != SECSuccess) goto loser;
  783. /* HostName (length and value) */
  784. rv = ssl3_AppendNumberToItem(&plaintext, srvName->len, 2);
  785. if (rv != SECSuccess) goto loser;
  786. rv = ssl3_AppendToItem(&plaintext, srvName->data, srvName->len);
  787. if (rv != SECSuccess) goto loser;
  788. } else {
  789. /* No Name */
  790. rv = ssl3_AppendNumberToItem(&plaintext, (char)TLS_STE_NO_SERVER_NAME,
  791. 1);
  792. if (rv != SECSuccess) goto loser;
  793. }
  794. PORT_Assert(plaintext.len == padding_length);
  795. for (i = 0; i < padding_length; i++)
  796. plaintext.data[i] = (unsigned char)padding_length;
  797. if (SECITEM_AllocItem(NULL, &ciphertext, ciphertext_length) == NULL) {
  798. rv = SECFailure;
  799. goto loser;
  800. }
  801. /* Generate encrypted portion of ticket. */
  802. if (ss->opt.bypassPKCS11) {
  803. aes_ctx = (AESContext *)aes_ctx_buf;
  804. rv = AES_InitContext(aes_ctx, aes_key, aes_key_length, iv,
  805. NSS_AES_CBC, 1, AES_BLOCK_SIZE);
  806. if (rv != SECSuccess) goto loser;
  807. rv = AES_Encrypt(aes_ctx, ciphertext.data, &ciphertext.len,
  808. ciphertext.len, plaintext_item.data,
  809. plaintext_item.len);
  810. if (rv != SECSuccess) goto loser;
  811. } else {
  812. aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech,
  813. CKA_ENCRYPT, aes_key_pkcs11, &ivItem);
  814. if (!aes_ctx_pkcs11)
  815. goto loser;
  816. rv = PK11_CipherOp(aes_ctx_pkcs11, ciphertext.data,
  817. (int *)&ciphertext.len, ciphertext.len,
  818. plaintext_item.data, plaintext_item.len);
  819. PK11_Finalize(aes_ctx_pkcs11);
  820. PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE);
  821. if (rv != SECSuccess) goto loser;
  822. }
  823. /* Convert ciphertext length to network order. */
  824. length_buf[0] = (ciphertext.len >> 8) & 0xff;
  825. length_buf[1] = (ciphertext.len ) & 0xff;
  826. /* Compute MAC. */
  827. if (ss->opt.bypassPKCS11) {
  828. hmac_ctx = (HMACContext *)hmac_ctx_buf;
  829. hashObj = HASH_GetRawHashObject(HASH_AlgSHA256);
  830. if (HMAC_Init(hmac_ctx, hashObj, mac_key,
  831. mac_key_length, PR_FALSE) != SECSuccess)
  832. goto loser;
  833. HMAC_Begin(hmac_ctx);
  834. HMAC_Update(hmac_ctx, key_name, SESS_TICKET_KEY_NAME_LEN);
  835. HMAC_Update(hmac_ctx, iv, sizeof(iv));
  836. HMAC_Update(hmac_ctx, (unsigned char *)length_buf, 2);
  837. HMAC_Update(hmac_ctx, ciphertext.data, ciphertext.len);
  838. HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length,
  839. sizeof(computed_mac));
  840. } else {
  841. SECItem macParam;
  842. macParam.data = NULL;
  843. macParam.len = 0;
  844. hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech,
  845. CKA_SIGN, mac_key_pkcs11, &macParam);
  846. if (!hmac_ctx_pkcs11)
  847. goto loser;
  848. rv = PK11_DigestBegin(hmac_ctx_pkcs11);
  849. rv = PK11_DigestOp(hmac_ctx_pkcs11, key_name,
  850. SESS_TICKET_KEY_NAME_LEN);
  851. rv = PK11_DigestOp(hmac_ctx_pkcs11, iv, sizeof(iv));
  852. rv = PK11_DigestOp(hmac_ctx_pkcs11, (unsigned char *)length_buf, 2);
  853. rv = PK11_DigestOp(hmac_ctx_pkcs11, ciphertext.data, ciphertext.len);
  854. rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac,
  855. &computed_mac_length, sizeof(computed_mac));
  856. PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
  857. if (rv != SECSuccess) goto loser;
  858. }
  859. /* Serialize the handshake message. */
  860. rv = ssl3_AppendHandshakeHeader(ss, new_session_ticket, message_length);
  861. if (rv != SECSuccess) goto loser;
  862. rv = ssl3_AppendHandshakeNumber(ss, ticket.ticket_lifetime_hint,
  863. sizeof(ticket.ticket_lifetime_hint));
  864. if (rv != SECSuccess) goto loser;
  865. rv = ssl3_AppendHandshakeNumber(ss,
  866. message_length - sizeof(ticket.ticket_lifetime_hint) - 2, 2);
  867. if (rv != SECSuccess) goto loser;
  868. rv = ssl3_AppendHandshake(ss, key_name, SESS_TICKET_KEY_NAME_LEN);
  869. if (rv != SECSuccess) goto loser;
  870. rv = ssl3_AppendHandshake(ss, iv, sizeof(iv));
  871. if (rv != SECSuccess) goto loser;
  872. rv = ssl3_AppendHandshakeVariable(ss, ciphertext.data, ciphertext.len, 2);
  873. if (rv != SECSuccess) goto loser;
  874. rv = ssl3_AppendHandshake(ss, computed_mac, computed_mac_length);
  875. if (rv != SECSuccess) goto loser;
  876. loser:
  877. if (plaintext_item.data)
  878. SECITEM_FreeItem(&plaintext_item, PR_FALSE);
  879. if (ciphertext.data)
  880. SECITEM_FreeItem(&ciphertext, PR_FALSE);
  881. return rv;
  882. }
  883. /* When a client receives a SessionTicket extension a NewSessionTicket
  884. * message is expected during the handshake.
  885. */
  886. SECStatus
  887. ssl3_ClientHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type,
  888. SECItem *data)
  889. {
  890. if (data->len != 0)
  891. return SECFailure;
  892. /* Keep track of negotiated extensions. */
  893. ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
  894. return SECSuccess;
  895. }
  896. SECStatus
  897. ssl3_ServerHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type,
  898. SECItem *data)
  899. {
  900. SECStatus rv;
  901. SECItem *decrypted_state = NULL;
  902. SessionTicket *parsed_session_ticket = NULL;
  903. sslSessionID *sid = NULL;
  904. SSL3Statistics *ssl3stats;
  905. /* Ignore the SessionTicket extension if processing is disabled. */
  906. if (!ss->opt.enableSessionTickets)
  907. return SECSuccess;
  908. /* Keep track of negotiated extensions. */
  909. ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
  910. /* Parse the received ticket sent in by the client. We are
  911. * lenient about some parse errors, falling back to a fullshake
  912. * instead of terminating the current connection.
  913. */
  914. if (data->len == 0) {
  915. ss->xtnData.emptySessionTicket = PR_TRUE;
  916. } else {
  917. int i;
  918. SECItem extension_data;
  919. EncryptedSessionTicket enc_session_ticket;
  920. unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH];
  921. unsigned int computed_mac_length;
  922. const SECHashObject *hashObj;
  923. const unsigned char *aes_key;
  924. const unsigned char *mac_key;
  925. PK11SymKey *aes_key_pkcs11;
  926. PK11SymKey *mac_key_pkcs11;
  927. PRUint32 aes_key_length;
  928. PRUint32 mac_key_length;
  929. PRUint64 hmac_ctx_buf[MAX_MAC_CONTEXT_LLONGS];
  930. HMACContext *hmac_ctx;
  931. PK11Context *hmac_ctx_pkcs11;
  932. CK_MECHANISM_TYPE macMech = CKM_SHA256_HMAC;
  933. PRUint64 aes_ctx_buf[MAX_CIPHER_CONTEXT_LLONGS];
  934. AESContext *aes_ctx;
  935. PK11Context *aes_ctx_pkcs11;
  936. CK_MECHANISM_TYPE cipherMech = CKM_AES_CBC;
  937. unsigned char * padding;
  938. PRUint32 padding_length;
  939. unsigned char *buffer;
  940. unsigned int buffer_len;
  941. PRInt32 temp;
  942. SECItem cert_item;
  943. PRInt8 nameType = TLS_STE_NO_SERVER_NAME;
  944. /* Turn off stateless session resumption if the client sends a
  945. * SessionTicket extension, even if the extension turns out to be
  946. * malformed (ss->sec.ci.sid is non-NULL when doing session
  947. * renegotiation.)
  948. */
  949. if (ss->sec.ci.sid != NULL) {
  950. ss->sec.uncache(ss->sec.ci.sid);
  951. ssl_FreeSID(ss->sec.ci.sid);
  952. ss->sec.ci.sid = NULL;
  953. }
  954. extension_data.data = data->data; /* Keep a copy for future use. */
  955. extension_data.len = data->len;
  956. if (ssl3_ParseEncryptedSessionTicket(ss, data, &enc_session_ticket)
  957. != SECSuccess)
  958. return SECFailure;
  959. /* Get session ticket keys. */
  960. if (ss->opt.bypassPKCS11) {
  961. rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length,
  962. &mac_key, &mac_key_length);
  963. } else {
  964. rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11,
  965. &mac_key_pkcs11);
  966. }
  967. if (rv != SECSuccess) {
  968. SSL_DBG(("%d: SSL[%d]: Unable to get/generate session ticket keys.",
  969. SSL_GETPID(), ss->fd));
  970. goto loser;
  971. }
  972. /* If the ticket sent by the client was generated under a key different
  973. * from the one we have, bypass ticket processing.
  974. */
  975. if (PORT_Memcmp(enc_session_ticket.key_name, key_name,
  976. SESS_TICKET_KEY_NAME_LEN) != 0) {
  977. SSL_DBG(("%d: SSL[%d]: Session ticket key_name sent mismatch.",
  978. SSL_GETPID(), ss->fd));
  979. goto no_ticket;
  980. }
  981. /* Verify the MAC on the ticket. MAC verification may also
  982. * fail if the MAC key has been recently refreshed.
  983. */
  984. if (ss->opt.bypassPKCS11) {
  985. hmac_ctx = (HMACContext *)hmac_ctx_buf;
  986. hashObj = HASH_GetRawHashObject(HASH_AlgSHA256);
  987. if (HMAC_Init(hmac_ctx, hashObj, mac_key,
  988. sizeof(session_ticket_mac_key), PR_FALSE) != SECSuccess)
  989. goto no_ticket;
  990. HMAC_Begin(hmac_ctx);
  991. HMAC_Update(hmac_ctx, extension_data.data,
  992. extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH);
  993. if (HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length,
  994. sizeof(computed_mac)) != SECSuccess)
  995. goto no_ticket;
  996. } else {
  997. SECItem macParam;
  998. macParam.data = NULL;
  999. macParam.len = 0;
  1000. hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech,
  1001. CKA_SIGN, mac_key_pkcs11, &macParam);
  1002. if (!hmac_ctx_pkcs11) {
  1003. SSL_DBG(("%d: SSL[%d]: Unable to create HMAC context: %d.",
  1004. SSL_GETPID(), ss->fd, PORT_GetError()));
  1005. goto no_ticket;
  1006. } else {
  1007. SSL_DBG(("%d: SSL[%d]: Successfully created HMAC context.",
  1008. SSL_GETPID(), ss->fd));
  1009. }
  1010. rv = PK11_DigestBegin(hmac_ctx_pkcs11);
  1011. rv = PK11_DigestOp(hmac_ctx_pkcs11, extension_data.data,
  1012. extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH);
  1013. if (rv != SECSuccess) {
  1014. PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
  1015. goto no_ticket;
  1016. }
  1017. rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac,
  1018. &computed_mac_length, sizeof(computed_mac));
  1019. PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
  1020. if (rv != SECSuccess)
  1021. goto no_ticket;
  1022. }
  1023. if (NSS_SecureMemcmp(computed_mac, enc_session_ticket.mac,
  1024. computed_mac_length) != 0) {
  1025. SSL_DBG(("%d: SSL[%d]: Session ticket MAC mismatch.",
  1026. SSL_GETPID(), ss->fd));
  1027. goto no_ticket;
  1028. }
  1029. /* We ignore key_name for now.
  1030. * This is ok as MAC verification succeeded.
  1031. */
  1032. /* Decrypt the ticket. */
  1033. /* Plaintext is shorter than the ciphertext due to padding. */
  1034. decrypted_state = SECITEM_AllocItem(NULL, NULL,
  1035. enc_session_ticket.encrypted_state.len);
  1036. if (ss->opt.bypassPKCS11) {
  1037. aes_ctx = (AESContext *)aes_ctx_buf;
  1038. rv = AES_InitContext(aes_ctx, aes_key,
  1039. sizeof(session_ticket_enc_key), enc_session_ticket.iv,
  1040. NSS_AES_CBC, 0,AES_BLOCK_SIZE);
  1041. if (rv != SECSuccess) {
  1042. SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
  1043. SSL_GETPID(), ss->fd));
  1044. goto no_ticket;
  1045. }
  1046. rv = AES_Decrypt(aes_ctx, decrypted_state->data,
  1047. &decrypted_state->len, decrypted_state->len,
  1048. enc_session_ticket.encrypted_state.data,
  1049. enc_session_ticket.encrypted_state.len);
  1050. if (rv != SECSuccess)
  1051. goto no_ticket;
  1052. } else {
  1053. SECItem ivItem;
  1054. ivItem.data = enc_session_ticket.iv;
  1055. ivItem.len = AES_BLOCK_SIZE;
  1056. aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech,
  1057. CKA_DECRYPT, aes_key_pkcs11, &ivItem);
  1058. if (!aes_ctx_pkcs11) {
  1059. SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
  1060. SSL_GETPID(), ss->fd));
  1061. goto no_ticket;
  1062. }
  1063. rv = PK11_CipherOp(aes_ctx_pkcs11, decrypted_state->data,
  1064. (int *)&decrypted_state->len, decrypted_state->len,
  1065. enc_session_ticket.encrypted_state.data,
  1066. enc_session_ticket.encrypted_state.len);
  1067. PK11_Finalize(aes_ctx_pkcs11);
  1068. PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE);
  1069. if (rv != SECSuccess)
  1070. goto no_ticket;
  1071. }
  1072. /* Check padding. */
  1073. padding_length =
  1074. (PRUint32)decrypted_state->data[decrypted_state->len - 1];
  1075. if (padding_length == 0 || padding_length > AES_BLOCK_SIZE)
  1076. goto no_ticket;
  1077. padding = &decrypted_state->data[decrypted_state->len - padding_length];
  1078. for (i = 0; i < padding_length; i++, padding++) {
  1079. if (padding_length != (PRUint32)*padding)
  1080. goto no_ticket;
  1081. }
  1082. /* Deserialize session state. */
  1083. buffer = decrypted_state->data;
  1084. buffer_len = decrypted_state->len;
  1085. parsed_session_ticket = PORT_ZAlloc(sizeof(SessionTicket));
  1086. if (parsed_session_ticket == NULL) {
  1087. rv = SECFailure;
  1088. goto loser;
  1089. }
  1090. /* Read ticket_version (which is ignored for now.) */
  1091. temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
  1092. if (temp < 0) goto no_ticket;
  1093. parsed_session_ticket->ticket_version = (SSL3ProtocolVersion)temp;
  1094. /* Read SSLVersion. */
  1095. temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
  1096. if (temp < 0) goto no_ticket;
  1097. parsed_session_ticket->ssl_version = (SSL3ProtocolVersion)temp;
  1098. /* Read cipher_suite. */
  1099. temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
  1100. if (temp < 0) goto no_ticket;
  1101. parsed_session_ticket->cipher_suite = (ssl3CipherSuite)temp;
  1102. /* Read compression_method. */
  1103. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1104. if (temp < 0) goto no_ticket;
  1105. parsed_session_ticket->compression_method = (SSLCompressionMethod)temp;
  1106. /* Read cipher spec parameters. */
  1107. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1108. if (temp < 0) goto no_ticket;
  1109. parsed_session_ticket->authAlgorithm = (SSLSignType)temp;
  1110. temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
  1111. if (temp < 0) goto no_ticket;
  1112. parsed_session_ticket->authKeyBits = (PRUint32)temp;
  1113. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1114. if (temp < 0) goto no_ticket;
  1115. parsed_session_ticket->keaType = (SSLKEAType)temp;
  1116. temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
  1117. if (temp < 0) goto no_ticket;
  1118. parsed_session_ticket->keaKeyBits = (PRUint32)temp;
  1119. /* Read wrapped master_secret. */
  1120. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1121. if (temp < 0) goto no_ticket;
  1122. parsed_session_ticket->ms_is_wrapped = (PRBool)temp;
  1123. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1124. if (temp < 0) goto no_ticket;
  1125. parsed_session_ticket->exchKeyType = (SSL3KEAType)temp;
  1126. temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
  1127. if (temp < 0) goto no_ticket;
  1128. parsed_session_ticket->msWrapMech = (CK_MECHANISM_TYPE)temp;
  1129. temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
  1130. if (temp < 0) goto no_ticket;
  1131. parsed_session_ticket->ms_length = (PRUint16)temp;
  1132. if (parsed_session_ticket->ms_length == 0 || /* sanity check MS. */
  1133. parsed_session_ticket->ms_length >
  1134. sizeof(parsed_session_ticket->master_secret))
  1135. goto no_ticket;
  1136. /* Allow for the wrapped master secret to be longer. */
  1137. if (buffer_len < sizeof(SSL3_MASTER_SECRET_LENGTH))
  1138. goto no_ticket;
  1139. PORT_Memcpy(parsed_session_ticket->master_secret, buffer,
  1140. parsed_session_ticket->ms_length);
  1141. buffer += parsed_session_ticket->ms_length;
  1142. buffer_len -= parsed_session_ticket->ms_length;
  1143. /* Read client_identity */
  1144. temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1145. if (temp < 0)
  1146. goto no_ticket;
  1147. parsed_session_ticket->client_identity.client_auth_type =
  1148. (ClientAuthenticationType)temp;
  1149. switch(parsed_session_ticket->client_identity.client_auth_type) {
  1150. case CLIENT_AUTH_ANONYMOUS:
  1151. break;
  1152. case CLIENT_AUTH_CERTIFICATE:
  1153. rv = ssl3_ConsumeHandshakeVariable(ss, &cert_item, 3,
  1154. &buffer, &buffer_len);
  1155. if (rv != SECSuccess) goto no_ticket;
  1156. rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->peer_cert,
  1157. &cert_item);
  1158. if (rv != SECSuccess) goto no_ticket;
  1159. break;
  1160. default:
  1161. goto no_ticket;
  1162. }
  1163. /* Read timestamp. */
  1164. temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
  1165. if (temp < 0)
  1166. goto no_ticket;
  1167. parsed_session_ticket->timestamp = (PRUint32)temp;
  1168. /* Read server name */
  1169. nameType =
  1170. ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
  1171. if (nameType != TLS_STE_NO_SERVER_NAME) {
  1172. SECItem name_item;
  1173. rv = ssl3_ConsumeHandshakeVariable(ss, &name_item, 2, &buffer,
  1174. &buffer_len);
  1175. if (rv != SECSuccess) goto no_ticket;
  1176. rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->srvName,
  1177. &name_item);
  1178. if (rv != SECSuccess) goto no_ticket;
  1179. parsed_session_ticket->srvName.type = nameType;
  1180. }
  1181. /* Done parsing. Check that all bytes have been consumed. */
  1182. if (buffer_len != padding_length)
  1183. goto no_ticket;
  1184. /* Use the ticket if it has not expired, otherwise free the allocated
  1185. * memory since the ticket is of no use.
  1186. */
  1187. if (parsed_session_ticket->timestamp != 0 &&
  1188. parsed_session_ticket->timestamp +
  1189. TLS_EX_SESS_TICKET_LIFETIME_HINT > ssl_Time()) {
  1190. sid = ssl3_NewSessionID(ss, PR_TRUE);
  1191. if (sid == NULL) {
  1192. rv = SECFailure;
  1193. goto loser;
  1194. }
  1195. /* Copy over parameters. */
  1196. sid->version = parsed_session_ticket->ssl_version;
  1197. sid->u.ssl3.cipherSuite = parsed_session_ticket->cipher_suite;
  1198. sid->u.ssl3.compression = parsed_session_ticket->compression_method;
  1199. sid->authAlgorithm = parsed_session_ticket->authAlgorithm;
  1200. sid->authKeyBits = parsed_session_ticket->authKeyBits;
  1201. sid->keaType = parsed_session_ticket->keaType;
  1202. sid->keaKeyBits = parsed_session_ticket->keaKeyBits;
  1203. /* Copy master secret. */
  1204. if (ss->opt.bypassPKCS11 &&
  1205. parsed_session_ticket->ms_is_wrapped)
  1206. goto no_ticket;
  1207. if (parsed_session_ticket->ms_length >
  1208. sizeof(sid->u.ssl3.keys.wrapped_master_secret))
  1209. goto no_ticket;
  1210. PORT_Memcpy(sid->u.ssl3.keys.wrapped_master_secret,
  1211. parsed_session_ticket->master_secret,
  1212. parsed_session_ticket->ms_length);
  1213. sid->u.ssl3.keys.wrapped_master_secret_len =
  1214. parsed_session_ticket->ms_length;
  1215. sid->u.ssl3.exchKeyType = parsed_session_ticket->exchKeyType;
  1216. sid->u.ssl3.masterWrapMech = parsed_session_ticket->msWrapMech;
  1217. sid->u.ssl3.keys.msIsWrapped =
  1218. parsed_session_ticket->ms_is_wrapped;
  1219. sid->u.ssl3.masterValid = PR_TRUE;
  1220. sid->u.ssl3.keys.resumable = PR_TRUE;
  1221. /* Copy over client cert from session ticket if there is one. */
  1222. if (parsed_session_ticket->peer_cert.data != NULL) {
  1223. if (sid->peerCert != NULL)
  1224. CERT_DestroyCertificate(sid->peerCert);
  1225. sid->peerCert = CERT_NewTempCertificate(ss->dbHandle,
  1226. &parsed_session_ticket->peer_cert, NULL, PR_FALSE, PR_TRUE);
  1227. if (sid->peerCert == NULL) {
  1228. rv = SECFailure;
  1229. goto loser;
  1230. }
  1231. }
  1232. if (parsed_session_ticket->srvName.data != NULL) {
  1233. sid->u.ssl3.srvName = parsed_session_ticket->srvName;
  1234. }
  1235. ss->statelessResume = PR_TRUE;
  1236. ss->sec.ci.sid = sid;
  1237. }
  1238. }
  1239. if (0) {
  1240. no_ticket:
  1241. SSL_DBG(("%d: SSL[%d]: Session ticket parsing failed.",
  1242. SSL_GETPID(), ss->fd));
  1243. ssl3stats = SSL_GetStatistics();
  1244. SSL_AtomicIncrementLong(& ssl3stats->hch_sid_ticket_parse_failures );
  1245. }
  1246. rv = SECSuccess;
  1247. loser:
  1248. /* ss->sec.ci.sid == sid if it did NOT come here via goto statement
  1249. * in that case do not free sid
  1250. */
  1251. if (sid && (ss->sec.ci.sid != sid)) {
  1252. ssl_FreeSID(sid);
  1253. sid = NULL;
  1254. }
  1255. if (decrypted_state != NULL) {
  1256. SECITEM_FreeItem(decrypted_state, PR_TRUE);
  1257. decrypted_state = NULL;
  1258. }
  1259. if (parsed_session_ticket != NULL) {
  1260. if (parsed_session_ticket->peer_cert.data) {
  1261. SECITEM_FreeItem(&parsed_session_ticket->peer_cert, PR_FALSE);
  1262. }
  1263. PORT_ZFree(parsed_session_ticket, sizeof(SessionTicket));
  1264. }
  1265. return rv;
  1266. }
  1267. /*
  1268. * Read bytes. Using this function means the SECItem structure
  1269. * cannot be freed. The caller is expected to call this function
  1270. * on a shallow copy of the structure.
  1271. */
  1272. static SECStatus
  1273. ssl3_ConsumeFromItem(SECItem *item, unsigned char **buf, PRUint32 bytes)
  1274. {
  1275. if (bytes > item->len)
  1276. return SECFailure;
  1277. *buf = item->data;
  1278. item->data += bytes;
  1279. item->len -= bytes;
  1280. return SECSuccess;
  1281. }
  1282. static SECStatus
  1283. ssl3_ParseEncryptedSessionTicket(sslSocket *ss, SECItem *data,
  1284. EncryptedSessionTicket *enc_session_ticket)
  1285. {
  1286. if (ssl3_ConsumeFromItem(data, &enc_session_ticket->key_name,
  1287. SESS_TICKET_KEY_NAME_LEN) != SECSuccess)
  1288. return SECFailure;
  1289. if (ssl3_ConsumeFromItem(data, &enc_session_ticket->iv,
  1290. AES_BLOCK_SIZE) != SECSuccess)
  1291. return SECFailure;
  1292. if (ssl3_ConsumeHandshakeVariable(ss, &enc_session_ticket->encrypted_state,
  1293. 2, &data->data, &data->len) != SECSuccess)
  1294. return SECFailure;
  1295. if (ssl3_ConsumeFromItem(data, &enc_session_ticket->mac,
  1296. TLS_EX_SESS_TICKET_MAC_LENGTH) != SECSuccess)
  1297. return SECFailure;
  1298. if (data->len != 0) /* Make sure that we have consumed all bytes. */
  1299. return SECFailure;
  1300. return SECSuccess;
  1301. }
  1302. /* go through hello extensions in buffer "b".
  1303. * For each one, find the extension handler in the table, and
  1304. * if present, invoke that handler.
  1305. * Servers ignore any extensions with unknown extension types.
  1306. * Clients reject any extensions with unadvertised extension types.
  1307. */
  1308. SECStatus
  1309. ssl3_HandleHelloExtensions(sslSocket *ss, SSL3Opaque **b, PRUint32 *length)
  1310. {
  1311. const ssl3HelloExtensionHandler * handlers;
  1312. if (ss->sec.isServer) {
  1313. handlers = clientHelloHandlers;
  1314. } else if (ss->version > SSL_LIBRARY_VERSION_3_0) {
  1315. handlers = serverHelloHandlersTLS;
  1316. } else {
  1317. handlers = serverHelloHandlersSSL3;
  1318. }
  1319. while (*length) {
  1320. const ssl3HelloExtensionHandler * handler;
  1321. SECStatus rv;
  1322. PRInt32 extension_type;
  1323. SECItem extension_data;
  1324. /* Get the extension's type field */
  1325. extension_type = ssl3_ConsumeHandshakeNumber(ss, 2, b, length);
  1326. if (extension_type < 0) /* failure to decode extension_type */
  1327. return SECFailure; /* alert already sent */
  1328. /* get the data for this extension, so we can pass it or skip it. */
  1329. rv = ssl3_ConsumeHandshakeVariable(ss, &extension_data, 2, b, length);
  1330. if (rv != SECSuccess)
  1331. return rv;
  1332. /* Check whether the server sent an extension which was not advertised
  1333. * in the ClientHello.
  1334. */
  1335. if (!ss->sec.isServer &&
  1336. !ssl3_ClientExtensionAdvertised(ss, extension_type))
  1337. return SECFailure; /* TODO: send unsupported_extension alert */
  1338. /* Check whether an extension has been sent multiple times. */
  1339. if (ssl3_ExtensionNegotiated(ss, extension_type))
  1340. return SECFailure;
  1341. /* find extension_type in table of Hello Extension Handlers */
  1342. for (handler = handlers; handler->ex_type >= 0; handler++) {
  1343. /* if found, call this handler */
  1344. if (handler->ex_type == extension_type) {
  1345. rv = (*handler->ex_handler)(ss, (PRUint16)extension_type,
  1346. &extension_data);
  1347. /* Ignore this result */
  1348. /* Treat all bad extensions as unrecognized types. */
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. return SECSuccess;
  1354. }
  1355. /* Add a callback function to the table of senders of server hello extensions.
  1356. */
  1357. SECStatus
  1358. ssl3_RegisterServerHelloExtensionSender(sslSocket *ss, PRUint16 ex_type,
  1359. ssl3HelloExtensionSenderFunc cb)
  1360. {
  1361. int i;
  1362. ssl3HelloExtensionSender *sender = &ss->xtnData.serverSenders[0];
  1363. for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) {
  1364. if (!sender->ex_sender) {
  1365. sender->ex_type = ex_type;
  1366. sender->ex_sender = cb;
  1367. return SECSuccess;
  1368. }
  1369. /* detect duplicate senders */
  1370. PORT_Assert(sender->ex_type != ex_type);
  1371. if (sender->ex_type == ex_type) {
  1372. /* duplicate */
  1373. break;
  1374. }
  1375. }
  1376. PORT_Assert(i < SSL_MAX_EXTENSIONS); /* table needs to grow */
  1377. PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
  1378. return SECFailure;
  1379. }
  1380. /* call each of the extension senders and return the accumulated length */
  1381. PRInt32
  1382. ssl3_CallHelloExtensionSenders(sslSocket *ss, PRBool append, PRUint32 maxBytes,
  1383. const ssl3HelloExtensionSender *sender)
  1384. {
  1385. PRInt32 total_exten_len = 0;
  1386. int i;
  1387. if (!sender) {
  1388. sender = ss->version > SSL_LIBRARY_VERSION_3_0 ?
  1389. &clientHelloSendersTLS[0] : &clientHelloSendersSSL3[0];
  1390. }
  1391. for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) {
  1392. if (sender->ex_sender) {
  1393. PRInt32 extLen = (*sender->ex_sender)(ss, append, maxBytes);
  1394. if (extLen < 0)
  1395. return -1;
  1396. maxBytes -= extLen;
  1397. total_exten_len += extLen;
  1398. }
  1399. }
  1400. return total_exten_len;
  1401. }
  1402. /* Extension format:
  1403. * Extension number: 2 bytes
  1404. * Extension length: 2 bytes
  1405. * Verify Data Length: 1 byte
  1406. * Verify Data (TLS): 12 bytes (client) or 24 bytes (server)
  1407. * Verify Data (SSL): 36 bytes (client) or 72 bytes (server)
  1408. */
  1409. static PRInt32
  1410. ssl3_SendRenegotiationInfoXtn(
  1411. sslSocket * ss,
  1412. PRBool append,
  1413. PRUint32 maxBytes)
  1414. {
  1415. PRInt32 len, needed;
  1416. /* In draft-ietf-tls-renegotiation-03, it is NOT RECOMMENDED to send
  1417. * both the SCSV and the empty RI, so when we send SCSV in
  1418. * the initial handshake, we don't also send RI.
  1419. */
  1420. if (!ss || ss->ssl3.hs.sendingSCSV)
  1421. return 0;
  1422. len = !ss->firstHsDone ? 0 :
  1423. (ss->sec.isServer ? ss->ssl3.hs.finishedBytes * 2
  1424. : ss->ssl3.hs.finishedBytes);
  1425. needed = 5 + len;
  1426. if (append && maxBytes >= needed) {
  1427. SECStatus rv;
  1428. /* extension_type */
  1429. rv = ssl3_AppendHandshakeNumber(ss, ssl_renegotiation_info_xtn, 2);
  1430. if (rv != SECSuccess) return -1;
  1431. /* length of extension_data */
  1432. rv = ssl3_AppendHandshakeNumber(ss, len + 1, 2);
  1433. if (rv != SECSuccess) return -1;
  1434. /* verify_Data from previous Finished message(s) */
  1435. rv = ssl3_AppendHandshakeVariable(ss,
  1436. ss->ssl3.hs.finishedMsgs.data, len, 1);
  1437. if (rv != SECSuccess) return -1;
  1438. if (!ss->sec.isServer) {
  1439. TLSExtensionData *xtnData = &ss->xtnData;
  1440. xtnData->advertised[xtnData->numAdvertised++] =
  1441. ssl_renegotiation_info_xtn;
  1442. }
  1443. }
  1444. return needed;
  1445. }
  1446. /* This function runs in both the client and server. */
  1447. static SECStatus
  1448. ssl3_HandleRenegotiationInfoXtn(sslSocket *ss, PRUint16 ex_type, SECItem *data)
  1449. {
  1450. SECStatus rv = SECSuccess;
  1451. PRUint32 len = 0;
  1452. if (ss->firstHsDone) {
  1453. len = ss->sec.isServer ? ss->ssl3.hs.finishedBytes
  1454. : ss->ssl3.hs.finishedBytes * 2;
  1455. }
  1456. if (data->len != 1 + len ||
  1457. data->data[0] != len || (len &&
  1458. NSS_SecureMemcmp(ss->ssl3.hs.finishedMsgs.data,
  1459. data->data + 1, len))) {
  1460. /* Can we do this here? Or, must we arrange for the caller to do it? */
  1461. (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure);
  1462. PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE);
  1463. return SECFailure;
  1464. }
  1465. /* remember that we got this extension and it was correct. */
  1466. ss->peerRequestedProtection = 1;
  1467. ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
  1468. if (ss->sec.isServer) {
  1469. /* prepare to send back the appropriate response */
  1470. rv = ssl3_RegisterServerHelloExtensionSender(ss, ex_type,
  1471. ssl3_SendRenegotiationInfoXtn);
  1472. }
  1473. return rv;
  1474. }