summaryrefslogtreecommitdiff
path: root/drivers/openssl/stream_peer_ssl.cpp
blob: aaedd7dde9892029a01a7eda6f2310819a0dca68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stream_peer_ssl.h"


int StreamPeerSSL::bio_create( BIO *b ) {
	b->init = 1;
	b->num = 0;
	b->ptr = NULL;
	b->flags = 0;
	return 1;
}

int StreamPeerSSL::bio_destroy( BIO *b ) {

	if ( b == NULL ) return 0;
	b->ptr = NULL;		/* sb_tls_remove() will free it */
	b->init = 0;
	b->flags = 0;
	return 1;
}

int StreamPeerSSL::bio_read( BIO *b, char *buf, int len ) {

	if ( buf == NULL || len <= 0 ) return 0;

	StreamPeerSSL * sp = (StreamPeerSSL*)b->ptr;

	if (sp->base.is_null())
		return 0;



	BIO_clear_retry_flags( b );

	Error err;
	int ret=0;
	if (sp->block) {
		err = sp->base->get_data((const uint8_t*)buf,len);
		if (err==OK)
			ret=len;
	} else {

		err = sp->base->get_partial_data((const uint8_t*)buf,len,ret);
		if (err==OK && ret!=len) {
			BIO_set_retry_write( b );
		}

	}

	return ret;
}

int StreamPeerSSL::bio_write( BIO *b, const char *buf, int len ) {

	if ( buf == NULL || len <= 0 ) return 0;

	StreamPeerSSL * sp = (StreamPeerSSL*)b->ptr;

	if (sp->base.is_null())
		return 0;

	BIO_clear_retry_flags( b );

	Error err;
	int wrote=0;
	if (sp->block) {
		err = sp->base->put_data((const uint8_t*)buf,len);
		if (err==OK)
			wrote=len;
	} else {

		err = sp->base->put_partial_data((const uint8_t*)buf,len,wrote);
		if (err==OK && wrote!=len) {
			BIO_set_retry_write( b );
		}

	}

	return wrote;
}

long StreamPeerSSL::bio_ctrl( BIO *b, int cmd, long num, void *ptr ) {
	if ( cmd == BIO_CTRL_FLUSH ) {
		/* The OpenSSL library needs this */
		return 1;
	}
	return 0;
}

int StreamPeerSSL::bio_gets( BIO *b, char *buf, int len ) {
	return -1;
}

int StreamPeerSSL::bio_puts( BIO *b, const char *str ) {
	return StreamPeerSSL::bio_write( b, str, strlen( str ) );
}

BIO_METHOD StreamPeerSSL::bio_methods =
{
	( 100 | 0x400 ),		/* it's a source/sink BIO */
	"sockbuf glue",
	StreamPeerSSL::bio_write,
	StreamPeerSSL::bio_read,
	StreamPeerSSL::bio_puts,
	StreamPeerSSL::bio_gets,
	StreamPeerSSL::bio_ctrl,
	StreamPeerSSL::bio_create,
	StreamPeerSSL::bio_destroy
};

StreamPeerSSL::StreamPeerSSL() {
}