ssize_t SslSocket::write(const void *buffer, size_t size) { if (!size) { TRACE("SslSocket.write(empty buffer)"); return 0; } ssize_t rv = gnutls_write(session_, buffer, size); TRACE("SslSocket.write(%ld bytes) = %ld", size, rv); if (rv >= 0) return rv; switch (rv) { case GNUTLS_E_AGAIN: errno = EAGAIN; break; case GNUTLS_E_INTERRUPTED: errno = EINTR; break; default: TRACE("gnutls_write error: %s", gnutls_strerror(rv)); errno = EINVAL; close(); break; } return -1; }
bool DTLS_Encrypt(NetworkAddress * destAddress, uint8_t * plainText, int plainTextLength, uint8_t * encryptedBuffer, int encryptedBufferLength, int * encryptedLength, void *context) { bool result = false; DTLS_Session * session = GetSession(destAddress); if (session) { if (session->SessionEstablished) { gnutls_transport_set_push_function(session->Session, EncryptCallBack); session->Buffer = encryptedBuffer; session->BufferLength = encryptedBufferLength; int written = gnutls_write(session->Session, plainText, plainTextLength); if (written >= 0) { *encryptedLength = encryptedBufferLength - session->BufferLength; result = (*encryptedLength > 0); } } else { session->UserContext = context; gnutls_transport_set_push_function(session->Session, SSLSendCallBack); session->SessionEstablished = (gnutls_handshake(session->Session) == GNUTLS_E_SUCCESS); if (session->SessionEstablished) Lwm2m_Info("DTLS Session established\n"); } } else { int index; for (index = 0;index < MAX_DTLS_SESSIONS; index++) { if (!sessions[index].Session) { SetupNewSession(index, destAddress, true); sessions[index].UserContext = context; gnutls_transport_set_push_function(sessions[index].Session, SSLSendCallBack); sessions[index].SessionEstablished = (gnutls_handshake(sessions[index].Session) == GNUTLS_E_SUCCESS); break; } } } return result; }
ssize_t ConnSSL_Write(CONNECTION *c, const void *buf, size_t count) { ssize_t bw; Conn_OPTION_DEL(c, CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ); assert(count > 0); #ifdef HAVE_LIBSSL bw = (ssize_t) SSL_write(c->ssl_state.ssl, buf, count); #endif #ifdef HAVE_LIBGNUTLS bw = gnutls_write(c->ssl_state.gnutls_session, buf, count); #endif if (bw > 0) return bw; if (ConnSSL_HandleError( c, bw, "ConnSSL_Write") == 0) errno = EAGAIN; /* try again */ return -1; }
void tlscomm_printf(struct connection_state *scs, const char *format, ...) { va_list args; char buf[1024]; int bytes; ssize_t unused __attribute__((unused)); if (scs == NULL) { DMA(DEBUG_ERROR, "null connection to tlscomm_printf\n"); abort(); } va_start(args, format); bytes = vsnprintf(buf, 1024, format, args); va_end(args); if (scs->sd != -1) { #ifdef USE_GNUTLS if (scs->tls_state) { int written = gnutls_write(scs->tls_state, buf, bytes); if (written < bytes) { TDM(DEBUG_ERROR, "Error %s prevented writing: %*s\n", gnutls_strerror(written), bytes, buf); return; } } else #endif /* Why???? */ unused = write(scs->sd, buf, bytes); } else { printf ("warning: tlscomm_printf called with an invalid socket descriptor\n"); return; } TDM(DEBUG_INFO, "wrote %*s", bytes, buf); }