2 * SSL/TLS interface functions for NSS
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #include <nspr/prtypes.h>
11 #include <nspr/plarenas.h>
12 #include <nspr/plhash.h>
13 #include <nspr/prio.h>
14 #include <nspr/prclist.h>
15 #include <nspr/prlock.h>
16 #include <nspr/prinit.h>
17 #include <nspr/prerror.h>
18 #include <nspr/prmem.h>
20 #include <nss/nssilckt.h>
22 #include <nss/pk11func.h>
23 #include <nss/secerr.h>
28 static int tls_nss_ref_count = 0;
30 static PRDescIdentity nss_layer_id;
33 struct tls_connection {
38 u8 *push_buf, *pull_buf, *pull_buf_offset;
39 size_t push_buf_len, pull_buf_len;
43 static PRStatus nss_io_close(PRFileDesc *fd)
45 wpa_printf(MSG_DEBUG, "NSS: I/O close");
50 static PRInt32 nss_io_read(PRFileDesc *fd, void *buf, PRInt32 amount)
52 wpa_printf(MSG_DEBUG, "NSS: I/O read(%d)", amount);
57 static PRInt32 nss_io_write(PRFileDesc *fd, const void *buf, PRInt32 amount)
59 wpa_printf(MSG_DEBUG, "NSS: I/O write(%d)", amount);
64 static PRInt32 nss_io_writev(PRFileDesc *fd, const PRIOVec *iov,
65 PRInt32 iov_size, PRIntervalTime timeout)
67 wpa_printf(MSG_DEBUG, "NSS: I/O writev(%d)", iov_size);
72 static PRInt32 nss_io_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
73 PRIntn flags, PRIntervalTime timeout)
75 struct tls_connection *conn = (struct tls_connection *) fd->secret;
78 wpa_printf(MSG_DEBUG, "NSS: I/O recv(%d)", amount);
80 if (conn->pull_buf == NULL) {
81 wpa_printf(MSG_DEBUG, "NSS: No data available to be read yet");
85 end = conn->pull_buf + conn->pull_buf_len;
86 if (end - conn->pull_buf_offset < amount)
87 amount = end - conn->pull_buf_offset;
88 os_memcpy(buf, conn->pull_buf_offset, amount);
89 conn->pull_buf_offset += amount;
90 if (conn->pull_buf_offset == end) {
91 wpa_printf(MSG_DEBUG, "%s - pull_buf consumed", __func__);
92 os_free(conn->pull_buf);
93 conn->pull_buf = conn->pull_buf_offset = NULL;
94 conn->pull_buf_len = 0;
96 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in pull_buf",
98 (unsigned long) (end - conn->pull_buf_offset));
104 static PRInt32 nss_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
105 PRIntn flags, PRIntervalTime timeout)
107 struct tls_connection *conn = (struct tls_connection *) fd->secret;
110 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
111 wpa_hexdump(MSG_MSGDUMP, "NSS: I/O send data", buf, amount);
113 nbuf = os_realloc(conn->push_buf, conn->push_buf_len + amount);
115 wpa_printf(MSG_ERROR, "NSS: Failed to allocate memory for the "
119 os_memcpy(nbuf + conn->push_buf_len, buf, amount);
120 conn->push_buf = nbuf;
121 conn->push_buf_len += amount;
127 static PRInt32 nss_io_recvfrom(PRFileDesc *fd, void *buf, PRInt32 amount,
128 PRIntn flags, PRNetAddr *addr,
129 PRIntervalTime timeout)
131 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
136 static PRInt32 nss_io_sendto(PRFileDesc *fd, const void *buf, PRInt32 amount,
137 PRIntn flags, const PRNetAddr *addr,
138 PRIntervalTime timeout)
140 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
145 static PRStatus nss_io_getpeername(PRFileDesc *fd, PRNetAddr *addr)
147 wpa_printf(MSG_DEBUG, "NSS: I/O getpeername");
150 * It Looks like NSS only supports IPv4 and IPv6 TCP sockets. Provide a
151 * fake IPv4 address to work around this even though we are not really
154 os_memset(addr, 0, sizeof(*addr));
155 addr->inet.family = PR_AF_INET;
161 static PRStatus nss_io_getsocketoption(PRFileDesc *fd,
162 PRSocketOptionData *data)
164 switch (data->option) {
165 case PR_SockOpt_Nonblocking:
166 wpa_printf(MSG_DEBUG, "NSS: I/O getsocketoption(Nonblocking)");
167 data->value.non_blocking = PR_TRUE;
170 wpa_printf(MSG_DEBUG, "NSS: I/O getsocketoption(%d)",
177 static const PRIOMethods nss_io = {
182 NULL /* available */,
183 NULL /* available64 */,
188 NULL /* fileinfo64 */,
200 NULL /* acceptread */,
201 NULL /* transmitfile */,
202 NULL /* getsockname */,
204 NULL /* reserved_fn_6 */,
205 NULL /* reserved_fn_5 */,
206 nss_io_getsocketoption,
207 NULL /* setsocketoption */,
209 NULL /* connectcontinue */,
210 NULL /* reserved_fn_3 */,
211 NULL /* reserved_fn_2 */,
212 NULL /* reserved_fn_1 */,
213 NULL /* reserved_fn_0 */
217 static char * nss_password_cb(PK11SlotInfo *slot, PRBool retry, void *arg)
219 wpa_printf(MSG_ERROR, "NSS: TODO - %s", __func__);
224 void * tls_init(const struct tls_config *conf)
229 if (tls_nss_ref_count > 1)
232 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
234 nss_layer_id = PR_GetUniqueIdentity("wpa_supplicant");
236 PK11_SetPasswordFunc(nss_password_cb);
238 dir = getenv("SSL_DIR");
240 if (NSS_Init(dir) != SECSuccess) {
241 wpa_printf(MSG_ERROR, "NSS: NSS_Init(cert_dir=%s) "
246 if (NSS_NoDB_Init(NULL) != SECSuccess) {
247 wpa_printf(MSG_ERROR, "NSS: NSS_NoDB_Init(NULL) "
253 if (SSL_OptionSetDefault(SSL_V2_COMPATIBLE_HELLO, PR_FALSE) !=
255 SSL_OptionSetDefault(SSL_ENABLE_SSL3, PR_FALSE) != SECSuccess ||
256 SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE) != SECSuccess ||
257 SSL_OptionSetDefault(SSL_ENABLE_TLS, PR_TRUE) != SECSuccess) {
258 wpa_printf(MSG_ERROR, "NSS: SSL_OptionSetDefault failed");
262 if (NSS_SetDomesticPolicy() != SECSuccess) {
263 wpa_printf(MSG_ERROR, "NSS: NSS_SetDomesticPolicy() failed");
270 void tls_deinit(void *ssl_ctx)
273 if (tls_nss_ref_count == 0) {
274 if (NSS_Shutdown() != SECSuccess)
275 wpa_printf(MSG_ERROR, "NSS: NSS_Shutdown() failed");
280 int tls_get_errors(void *tls_ctx)
286 static SECStatus nss_bad_cert_cb(void *arg, PRFileDesc *fd)
288 struct tls_connection *conn = arg;
289 SECStatus res = SECSuccess;
291 CERTCertificate *cert;
292 char *subject, *issuer;
295 if (IS_SEC_ERROR(err))
296 wpa_printf(MSG_DEBUG, "NSS: Bad Server Certificate (sec err "
297 "%d)", err - SEC_ERROR_BASE);
299 wpa_printf(MSG_DEBUG, "NSS: Bad Server Certificate (err %d)",
301 cert = SSL_PeerCertificate(fd);
302 subject = CERT_NameToAscii(&cert->subject);
303 issuer = CERT_NameToAscii(&cert->issuer);
304 wpa_printf(MSG_DEBUG, "NSS: Peer certificate subject='%s' issuer='%s'",
306 CERT_DestroyCertificate(cert);
309 if (conn->verify_peer)
316 static void nss_handshake_cb(PRFileDesc *fd, void *client_data)
318 struct tls_connection *conn = client_data;
319 wpa_printf(MSG_DEBUG, "NSS: Handshake completed");
320 conn->established = 1;
324 struct tls_connection * tls_connection_init(void *tls_ctx)
326 struct tls_connection *conn;
328 conn = os_zalloc(sizeof(*conn));
332 conn->fd = PR_CreateIOLayerStub(nss_layer_id, &nss_io);
333 if (conn->fd == NULL) {
337 conn->fd->secret = (void *) conn;
339 conn->fd = SSL_ImportFD(NULL, conn->fd);
340 if (conn->fd == NULL) {
345 if (SSL_OptionSet(conn->fd, SSL_SECURITY, PR_TRUE) != SECSuccess ||
346 SSL_OptionSet(conn->fd, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) !=
348 SSL_OptionSet(conn->fd, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) !=
350 SSL_OptionSet(conn->fd, SSL_ENABLE_TLS, PR_TRUE) != SECSuccess ||
351 SSL_BadCertHook(conn->fd, nss_bad_cert_cb, conn) != SECSuccess ||
352 SSL_HandshakeCallback(conn->fd, nss_handshake_cb, conn) !=
354 wpa_printf(MSG_ERROR, "NSS: Failed to set options");
360 SSL_ResetHandshake(conn->fd, PR_FALSE);
366 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
369 os_free(conn->push_buf);
370 os_free(conn->pull_buf);
375 int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
377 return conn->established;
381 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
387 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
388 const struct tls_connection_params *params)
390 wpa_printf(MSG_ERROR, "NSS: TODO - %s", __func__);
395 int tls_global_set_params(void *tls_ctx,
396 const struct tls_connection_params *params)
402 int tls_global_set_verify(void *tls_ctx, int check_crl)
408 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
411 conn->verify_peer = verify_peer;
416 int tls_connection_get_keys(void *tls_ctx, struct tls_connection *conn,
417 struct tls_keys *keys)
419 /* NSS does not export master secret or client/server random. */
424 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
425 const char *label, int server_random_first,
426 u8 *out, size_t out_len)
428 if (conn == NULL || server_random_first) {
429 wpa_printf(MSG_INFO, "NSS: Unsupported PRF request "
430 "(server_random_first=%d)",
431 server_random_first);
435 if (SSL_ExportKeyingMaterial(conn->fd, label, NULL, 0, out, out_len) !=
437 wpa_printf(MSG_INFO, "NSS: Failed to use TLS extractor "
438 "(label='%s' out_len=%d", label, (int) out_len);
446 struct wpabuf * tls_connection_handshake(void *tls_ctx,
447 struct tls_connection *conn,
448 const struct wpabuf *in_data,
449 struct wpabuf **appl_data)
451 struct wpabuf *out_data;
453 wpa_printf(MSG_DEBUG, "NSS: handshake: in_len=%u",
454 in_data ? (unsigned int) wpabuf_len(in_data) : 0);
459 if (in_data && wpabuf_len(in_data) > 0) {
460 if (conn->pull_buf) {
461 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
462 "pull_buf", __func__,
463 (unsigned long) conn->pull_buf_len);
464 os_free(conn->pull_buf);
466 conn->pull_buf = os_malloc(wpabuf_len(in_data));
467 if (conn->pull_buf == NULL)
469 os_memcpy(conn->pull_buf, wpabuf_head(in_data),
470 wpabuf_len(in_data));
471 conn->pull_buf_offset = conn->pull_buf;
472 conn->pull_buf_len = wpabuf_len(in_data);
475 SSL_ForceHandshake(conn->fd);
477 if (conn->established && conn->push_buf == NULL) {
478 /* Need to return something to get final TLS ACK. */
479 conn->push_buf = os_malloc(1);
482 if (conn->push_buf == NULL)
484 out_data = wpabuf_alloc_ext_data(conn->push_buf, conn->push_buf_len);
485 if (out_data == NULL)
486 os_free(conn->push_buf);
487 conn->push_buf = NULL;
488 conn->push_buf_len = 0;
493 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
494 struct tls_connection *conn,
495 const struct wpabuf *in_data,
496 struct wpabuf **appl_data)
502 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
503 struct tls_connection *conn,
504 const struct wpabuf *in_data)
509 wpa_printf(MSG_DEBUG, "NSS: encrypt %d bytes",
510 (int) wpabuf_len(in_data));
511 res = PR_Send(conn->fd, wpabuf_head(in_data), wpabuf_len(in_data), 0,
514 wpa_printf(MSG_ERROR, "NSS: Encryption failed");
517 if (conn->push_buf == NULL)
519 buf = wpabuf_alloc_ext_data(conn->push_buf, conn->push_buf_len);
521 os_free(conn->push_buf);
522 conn->push_buf = NULL;
523 conn->push_buf_len = 0;
528 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
529 struct tls_connection *conn,
530 const struct wpabuf *in_data)
535 wpa_printf(MSG_DEBUG, "NSS: decrypt %d bytes",
536 (int) wpabuf_len(in_data));
537 if (conn->pull_buf) {
538 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
539 "pull_buf", __func__,
540 (unsigned long) conn->pull_buf_len);
541 os_free(conn->pull_buf);
543 conn->pull_buf = os_malloc(wpabuf_len(in_data));
544 if (conn->pull_buf == NULL)
546 os_memcpy(conn->pull_buf, wpabuf_head(in_data), wpabuf_len(in_data));
547 conn->pull_buf_offset = conn->pull_buf;
548 conn->pull_buf_len = wpabuf_len(in_data);
551 * Even though we try to disable TLS compression, it is possible that
552 * this cannot be done with all TLS libraries. Add extra buffer space
553 * to handle the possibility of the decrypted data being longer than
556 out = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
560 res = PR_Recv(conn->fd, wpabuf_mhead(out), wpabuf_size(out), 0, 0);
561 wpa_printf(MSG_DEBUG, "NSS: PR_Recv: %d", res);
566 wpabuf_put(out, res);
572 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
578 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
585 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
586 char *buf, size_t buflen)
592 int tls_connection_enable_workaround(void *tls_ctx,
593 struct tls_connection *conn)
599 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
600 int ext_type, const u8 *data,
607 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
613 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
619 int tls_connection_get_write_alerts(void *tls_ctx,
620 struct tls_connection *conn)
626 int tls_connection_get_keyblock_size(void *tls_ctx,
627 struct tls_connection *conn)
633 unsigned int tls_capabilities(void *tls_ctx)
639 int tls_connection_set_session_ticket_cb(void *tls_ctx,
640 struct tls_connection *conn,
641 tls_session_ticket_cb cb,