Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / crypto / tls_nss.c
blobad834b6493372a565c5060f2a384f472a74bcd3a
1 /*
2 * SSL/TLS interface functions for NSS
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <nspr/prtypes.h>
17 #include <nspr/plarenas.h>
18 #include <nspr/plhash.h>
19 #include <nspr/prio.h>
20 #include <nspr/prclist.h>
21 #include <nspr/prlock.h>
22 #include <nspr/prinit.h>
23 #include <nspr/prerror.h>
24 #include <nspr/prmem.h>
25 #include <nss/nss.h>
26 #include <nss/nssilckt.h>
27 #include <nss/ssl.h>
28 #include <nss/pk11func.h>
29 #include <nss/secerr.h>
31 #include "common.h"
32 #include "tls.h"
34 static int tls_nss_ref_count = 0;
36 static PRDescIdentity nss_layer_id;
39 struct tls_connection {
40 PRFileDesc *fd;
42 int established;
43 int verify_peer;
44 u8 *push_buf, *pull_buf, *pull_buf_offset;
45 size_t push_buf_len, pull_buf_len;
49 static PRStatus nss_io_close(PRFileDesc *fd)
51 wpa_printf(MSG_DEBUG, "NSS: I/O close");
52 return PR_SUCCESS;
56 static PRInt32 nss_io_read(PRFileDesc *fd, void *buf, PRInt32 amount)
58 wpa_printf(MSG_DEBUG, "NSS: I/O read(%d)", amount);
59 return PR_FAILURE;
63 static PRInt32 nss_io_write(PRFileDesc *fd, const void *buf, PRInt32 amount)
65 wpa_printf(MSG_DEBUG, "NSS: I/O write(%d)", amount);
66 return PR_FAILURE;
70 static PRInt32 nss_io_writev(PRFileDesc *fd, const PRIOVec *iov,
71 PRInt32 iov_size, PRIntervalTime timeout)
73 wpa_printf(MSG_DEBUG, "NSS: I/O writev(%d)", iov_size);
74 return PR_FAILURE;
78 static PRInt32 nss_io_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
79 PRIntn flags, PRIntervalTime timeout)
81 struct tls_connection *conn = (struct tls_connection *) fd->secret;
82 u8 *end;
84 wpa_printf(MSG_DEBUG, "NSS: I/O recv(%d)", amount);
86 if (conn->pull_buf == NULL) {
87 wpa_printf(MSG_DEBUG, "NSS: No data available to be read yet");
88 return PR_FAILURE;
91 end = conn->pull_buf + conn->pull_buf_len;
92 if (end - conn->pull_buf_offset < amount)
93 amount = end - conn->pull_buf_offset;
94 os_memcpy(buf, conn->pull_buf_offset, amount);
95 conn->pull_buf_offset += amount;
96 if (conn->pull_buf_offset == end) {
97 wpa_printf(MSG_DEBUG, "%s - pull_buf consumed", __func__);
98 os_free(conn->pull_buf);
99 conn->pull_buf = conn->pull_buf_offset = NULL;
100 conn->pull_buf_len = 0;
101 } else {
102 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in pull_buf",
103 __func__,
104 (unsigned long) (end - conn->pull_buf_offset));
106 return amount;
110 static PRInt32 nss_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
111 PRIntn flags, PRIntervalTime timeout)
113 struct tls_connection *conn = (struct tls_connection *) fd->secret;
114 u8 *nbuf;
116 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
117 wpa_hexdump(MSG_MSGDUMP, "NSS: I/O send data", buf, amount);
119 nbuf = os_realloc(conn->push_buf, conn->push_buf_len + amount);
120 if (nbuf == NULL) {
121 wpa_printf(MSG_ERROR, "NSS: Failed to allocate memory for the "
122 "data to be sent");
123 return PR_FAILURE;
125 os_memcpy(nbuf + conn->push_buf_len, buf, amount);
126 conn->push_buf = nbuf;
127 conn->push_buf_len += amount;
129 return amount;
133 static PRInt32 nss_io_recvfrom(PRFileDesc *fd, void *buf, PRInt32 amount,
134 PRIntn flags, PRNetAddr *addr,
135 PRIntervalTime timeout)
137 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
138 return PR_FAILURE;
142 static PRInt32 nss_io_sendto(PRFileDesc *fd, const void *buf, PRInt32 amount,
143 PRIntn flags, const PRNetAddr *addr,
144 PRIntervalTime timeout)
146 wpa_printf(MSG_DEBUG, "NSS: I/O %s", __func__);
147 return PR_FAILURE;
151 static PRStatus nss_io_getpeername(PRFileDesc *fd, PRNetAddr *addr)
153 wpa_printf(MSG_DEBUG, "NSS: I/O getpeername");
156 * It Looks like NSS only supports IPv4 and IPv6 TCP sockets. Provide a
157 * fake IPv4 address to work around this even though we are not really
158 * using TCP.
160 os_memset(addr, 0, sizeof(*addr));
161 addr->inet.family = PR_AF_INET;
163 return PR_SUCCESS;
167 static PRStatus nss_io_getsocketoption(PRFileDesc *fd,
168 PRSocketOptionData *data)
170 switch (data->option) {
171 case PR_SockOpt_Nonblocking:
172 wpa_printf(MSG_DEBUG, "NSS: I/O getsocketoption(Nonblocking)");
173 data->value.non_blocking = PR_TRUE;
174 return PR_SUCCESS;
175 default:
176 wpa_printf(MSG_DEBUG, "NSS: I/O getsocketoption(%d)",
177 data->option);
178 return PR_FAILURE;
183 static const PRIOMethods nss_io = {
184 PR_DESC_LAYERED,
185 nss_io_close,
186 nss_io_read,
187 nss_io_write,
188 NULL /* available */,
189 NULL /* available64 */,
190 NULL /* fsync */,
191 NULL /* fseek */,
192 NULL /* fseek64 */,
193 NULL /* fileinfo */,
194 NULL /* fileinfo64 */,
195 nss_io_writev,
196 NULL /* connect */,
197 NULL /* accept */,
198 NULL /* bind */,
199 NULL /* listen */,
200 NULL /* shutdown */,
201 nss_io_recv,
202 nss_io_send,
203 nss_io_recvfrom,
204 nss_io_sendto,
205 NULL /* poll */,
206 NULL /* acceptread */,
207 NULL /* transmitfile */,
208 NULL /* getsockname */,
209 nss_io_getpeername,
210 NULL /* reserved_fn_6 */,
211 NULL /* reserved_fn_5 */,
212 nss_io_getsocketoption,
213 NULL /* setsocketoption */,
214 NULL /* sendfile */,
215 NULL /* connectcontinue */,
216 NULL /* reserved_fn_3 */,
217 NULL /* reserved_fn_2 */,
218 NULL /* reserved_fn_1 */,
219 NULL /* reserved_fn_0 */
223 static char * nss_password_cb(PK11SlotInfo *slot, PRBool retry, void *arg)
225 wpa_printf(MSG_ERROR, "NSS: TODO - %s", __func__);
226 return NULL;
230 void * tls_init(const struct tls_config *conf)
232 char *dir;
234 tls_nss_ref_count++;
235 if (tls_nss_ref_count > 1)
236 return (void *) 1;
238 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
240 nss_layer_id = PR_GetUniqueIdentity("wpa_supplicant");
242 PK11_SetPasswordFunc(nss_password_cb);
244 dir = getenv("SSL_DIR");
245 if (dir) {
246 if (NSS_Init(dir) != SECSuccess) {
247 wpa_printf(MSG_ERROR, "NSS: NSS_Init(cert_dir=%s) "
248 "failed", dir);
249 return NULL;
251 } else {
252 if (NSS_NoDB_Init(NULL) != SECSuccess) {
253 wpa_printf(MSG_ERROR, "NSS: NSS_NoDB_Init(NULL) "
254 "failed");
255 return NULL;
259 if (SSL_OptionSetDefault(SSL_V2_COMPATIBLE_HELLO, PR_FALSE) !=
260 SECSuccess ||
261 SSL_OptionSetDefault(SSL_ENABLE_SSL3, PR_FALSE) != SECSuccess ||
262 SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE) != SECSuccess ||
263 SSL_OptionSetDefault(SSL_ENABLE_TLS, PR_TRUE) != SECSuccess) {
264 wpa_printf(MSG_ERROR, "NSS: SSL_OptionSetDefault failed");
265 return NULL;
268 if (NSS_SetDomesticPolicy() != SECSuccess) {
269 wpa_printf(MSG_ERROR, "NSS: NSS_SetDomesticPolicy() failed");
270 return NULL;
273 return (void *) 1;
276 void tls_deinit(void *ssl_ctx)
278 tls_nss_ref_count--;
279 if (tls_nss_ref_count == 0) {
280 if (NSS_Shutdown() != SECSuccess)
281 wpa_printf(MSG_ERROR, "NSS: NSS_Shutdown() failed");
286 int tls_get_errors(void *tls_ctx)
288 return 0;
292 static SECStatus nss_bad_cert_cb(void *arg, PRFileDesc *fd)
294 struct tls_connection *conn = arg;
295 SECStatus res = SECSuccess;
296 PRErrorCode err;
297 CERTCertificate *cert;
298 char *subject, *issuer;
300 err = PR_GetError();
301 if (IS_SEC_ERROR(err))
302 wpa_printf(MSG_DEBUG, "NSS: Bad Server Certificate (sec err "
303 "%d)", err - SEC_ERROR_BASE);
304 else
305 wpa_printf(MSG_DEBUG, "NSS: Bad Server Certificate (err %d)",
306 err);
307 cert = SSL_PeerCertificate(fd);
308 subject = CERT_NameToAscii(&cert->subject);
309 issuer = CERT_NameToAscii(&cert->issuer);
310 wpa_printf(MSG_DEBUG, "NSS: Peer certificate subject='%s' issuer='%s'",
311 subject, issuer);
312 CERT_DestroyCertificate(cert);
313 PR_Free(subject);
314 PR_Free(issuer);
315 if (conn->verify_peer)
316 res = SECFailure;
318 return res;
322 static void nss_handshake_cb(PRFileDesc *fd, void *client_data)
324 struct tls_connection *conn = client_data;
325 wpa_printf(MSG_DEBUG, "NSS: Handshake completed");
326 conn->established = 1;
330 struct tls_connection * tls_connection_init(void *tls_ctx)
332 struct tls_connection *conn;
334 conn = os_zalloc(sizeof(*conn));
335 if (conn == NULL)
336 return NULL;
338 conn->fd = PR_CreateIOLayerStub(nss_layer_id, &nss_io);
339 if (conn->fd == NULL) {
340 os_free(conn);
341 return NULL;
343 conn->fd->secret = (void *) conn;
345 conn->fd = SSL_ImportFD(NULL, conn->fd);
346 if (conn->fd == NULL) {
347 os_free(conn);
348 return NULL;
351 if (SSL_OptionSet(conn->fd, SSL_SECURITY, PR_TRUE) != SECSuccess ||
352 SSL_OptionSet(conn->fd, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) !=
353 SECSuccess ||
354 SSL_OptionSet(conn->fd, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) !=
355 SECSuccess ||
356 SSL_OptionSet(conn->fd, SSL_ENABLE_TLS, PR_TRUE) != SECSuccess ||
357 SSL_BadCertHook(conn->fd, nss_bad_cert_cb, conn) != SECSuccess ||
358 SSL_HandshakeCallback(conn->fd, nss_handshake_cb, conn) !=
359 SECSuccess) {
360 wpa_printf(MSG_ERROR, "NSS: Failed to set options");
361 PR_Close(conn->fd);
362 os_free(conn);
363 return NULL;
366 SSL_ResetHandshake(conn->fd, PR_FALSE);
368 return conn;
372 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
374 PR_Close(conn->fd);
375 os_free(conn->push_buf);
376 os_free(conn->pull_buf);
377 os_free(conn);
381 int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
383 return conn->established;
387 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
389 return -1;
393 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
394 const struct tls_connection_params *params)
396 wpa_printf(MSG_ERROR, "NSS: TODO - %s", __func__);
397 return 0;
401 int tls_global_set_params(void *tls_ctx,
402 const struct tls_connection_params *params)
404 return -1;
408 int tls_global_set_verify(void *tls_ctx, int check_crl)
410 return -1;
414 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
415 int verify_peer)
417 conn->verify_peer = verify_peer;
418 return 0;
422 int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
423 int tls_ia)
425 return -1;
429 int tls_connection_get_keys(void *tls_ctx, struct tls_connection *conn,
430 struct tls_keys *keys)
432 /* NSS does not export master secret or client/server random. */
433 return -1;
437 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
438 const char *label, int server_random_first,
439 u8 *out, size_t out_len)
441 if (conn == NULL || server_random_first) {
442 wpa_printf(MSG_INFO, "NSS: Unsupported PRF request "
443 "(server_random_first=%d)",
444 server_random_first);
445 return -1;
448 if (SSL_ExportKeyingMaterial(conn->fd, label, NULL, 0, out, out_len) !=
449 SECSuccess) {
450 wpa_printf(MSG_INFO, "NSS: Failed to use TLS extractor "
451 "(label='%s' out_len=%d", label, (int) out_len);
452 return -1;
455 return 0;
459 struct wpabuf * tls_connection_handshake(void *tls_ctx,
460 struct tls_connection *conn,
461 const struct wpabuf *in_data,
462 struct wpabuf **appl_data)
464 struct wpabuf *out_data;
466 wpa_printf(MSG_DEBUG, "NSS: handshake: in_len=%u",
467 in_data ? (unsigned int) wpabuf_len(in_data) : 0);
469 if (appl_data)
470 *appl_data = NULL;
472 if (in_data && wpabuf_len(in_data) > 0) {
473 if (conn->pull_buf) {
474 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
475 "pull_buf", __func__,
476 (unsigned long) conn->pull_buf_len);
477 os_free(conn->pull_buf);
479 conn->pull_buf = os_malloc(wpabuf_len(in_data));
480 if (conn->pull_buf == NULL)
481 return NULL;
482 os_memcpy(conn->pull_buf, wpabuf_head(in_data),
483 wpabuf_len(in_data));
484 conn->pull_buf_offset = conn->pull_buf;
485 conn->pull_buf_len = wpabuf_len(in_data);
488 SSL_ForceHandshake(conn->fd);
490 if (conn->established && conn->push_buf == NULL) {
491 /* Need to return something to get final TLS ACK. */
492 conn->push_buf = os_malloc(1);
495 if (conn->push_buf == NULL)
496 return NULL;
497 out_data = wpabuf_alloc_ext_data(conn->push_buf, conn->push_buf_len);
498 if (out_data == NULL)
499 os_free(conn->push_buf);
500 conn->push_buf = NULL;
501 conn->push_buf_len = 0;
502 return out_data;
506 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
507 struct tls_connection *conn,
508 const struct wpabuf *in_data,
509 struct wpabuf **appl_data)
511 return NULL;
515 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
516 struct tls_connection *conn,
517 const struct wpabuf *in_data)
519 PRInt32 res;
520 struct wpabuf *buf;
522 wpa_printf(MSG_DEBUG, "NSS: encrypt %d bytes",
523 (int) wpabuf_len(in_data));
524 res = PR_Send(conn->fd, wpabuf_head(in_data), wpabuf_len(in_data), 0,
526 if (res < 0) {
527 wpa_printf(MSG_ERROR, "NSS: Encryption failed");
528 return NULL;
530 if (conn->push_buf == NULL)
531 return NULL;
532 buf = wpabuf_alloc_ext_data(conn->push_buf, conn->push_buf_len);
533 if (buf == NULL)
534 os_free(conn->push_buf);
535 conn->push_buf = NULL;
536 conn->push_buf_len = 0;
537 return buf;
541 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
542 struct tls_connection *conn,
543 const struct wpabuf *in_data)
545 PRInt32 res;
546 struct wpabuf *out;
548 wpa_printf(MSG_DEBUG, "NSS: decrypt %d bytes",
549 (int) wpabuf_len(in_data));
550 if (conn->pull_buf) {
551 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
552 "pull_buf", __func__,
553 (unsigned long) conn->pull_buf_len);
554 os_free(conn->pull_buf);
556 conn->pull_buf = os_malloc(wpabuf_len(in_data));
557 if (conn->pull_buf == NULL)
558 return NULL;
559 os_memcpy(conn->pull_buf, wpabuf_head(in_data), wpabuf_len(in_data));
560 conn->pull_buf_offset = conn->pull_buf;
561 conn->pull_buf_len = wpabuf_len(in_data);
564 * Even though we try to disable TLS compression, it is possible that
565 * this cannot be done with all TLS libraries. Add extra buffer space
566 * to handle the possibility of the decrypted data being longer than
567 * input data.
569 out = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
570 if (out == NULL)
571 return NULL;
573 res = PR_Recv(conn->fd, wpabuf_mhead(out), wpabuf_size(out), 0, 0);
574 wpa_printf(MSG_DEBUG, "NSS: PR_Recv: %d", res);
575 if (res < 0) {
576 wpabuf_free(out);
577 return NULL;
579 wpabuf_put(out, res);
581 return out;
585 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
587 return 0;
591 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
592 u8 *ciphers)
594 return -1;
598 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
599 char *buf, size_t buflen)
601 return -1;
605 int tls_connection_enable_workaround(void *tls_ctx,
606 struct tls_connection *conn)
608 return -1;
612 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
613 int ext_type, const u8 *data,
614 size_t data_len)
616 return -1;
620 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
622 return 0;
626 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
628 return 0;
632 int tls_connection_get_write_alerts(void *tls_ctx,
633 struct tls_connection *conn)
635 return 0;
639 int tls_connection_get_keyblock_size(void *tls_ctx,
640 struct tls_connection *conn)
642 return -1;
646 unsigned int tls_capabilities(void *tls_ctx)
648 return 0;
652 struct wpabuf * tls_connection_ia_send_phase_finished(
653 void *tls_ctx, struct tls_connection *conn, int final)
655 return NULL;
659 int tls_connection_ia_final_phase_finished(void *tls_ctx,
660 struct tls_connection *conn)
662 return -1;
666 int tls_connection_ia_permute_inner_secret(void *tls_ctx,
667 struct tls_connection *conn,
668 const u8 *key, size_t key_len)
670 return -1;
674 int tls_connection_set_session_ticket_cb(void *tls_ctx,
675 struct tls_connection *conn,
676 tls_session_ticket_cb cb,
677 void *ctx)
679 return -1;