hcoop: move dispatcher to gibran
[hcoop/domtool2.git] / openssl / openssl_sml.c
CommitLineData
a62a739c
CE
1#include <openssl/ssl.h>
2#include <openssl/err.h>
3#include <openssl/asn1.h>
559e89e9 4
a62a739c 5#include <string.h>
3b267643
AC
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <unistd.h>
9#include <netinet/in.h>
10
11void OpenSSL_SML_init() {
12 SSL_library_init();
559e89e9
AC
13}
14
15void OpenSSL_SML_load_error_strings() {
16 SSL_load_error_strings();
3b267643 17 ERR_load_X509_strings();
559e89e9
AC
18}
19
20void OpenSSL_SML_load_BIO_strings() {
21 ERR_load_BIO_strings();
22}
23
24int OpenSSL_SML_get_error() {
25 return ERR_get_error();
26}
27
64a44dc0
AC
28int OpenSSL_SML_should_retry(BIO *b) {
29 return BIO_should_retry(b);
30}
31
559e89e9
AC
32const char *OpenSSL_SML_lib_error_string(int err) {
33 return ERR_lib_error_string(err);
34}
35
36const char *OpenSSL_SML_func_error_string(int err) {
37 return ERR_func_error_string(err);
38}
39
40const char *OpenSSL_SML_reason_error_string(int err) {
41 return ERR_reason_error_string(err);
42}
43
07cc384c
AC
44int OpenSSL_SML_read(BIO *b, void *data, int len) {
45 return BIO_read(b, data, len);
46}
47
559e89e9
AC
48int OpenSSL_SML_write(BIO *b, const void *data, int len) {
49 return BIO_write(b, data, len);
50}
51
52BIO *OpenSSL_SML_new_connect(char *addr) {
53 return BIO_new_connect(addr);
54}
55
56void OpenSSL_SML_free_all(BIO *b) {
57 BIO_free_all(b);
58}
59
60int OpenSSL_SML_do_connect(BIO *b) {
61 return BIO_do_connect(b);
62}
07cc384c
AC
63
64int OpenSSL_SML_do_accept(BIO *b) {
65 return BIO_do_accept(b);
66}
3b267643 67
60534712
AC
68int OpenSSL_SML_do_handshake(BIO *b) {
69 return BIO_do_handshake(b);
70}
71
3b267643
AC
72SSL_CTX *OpenSSL_SML_CTX_new(SSL_METHOD *meth) {
73 SSL_CTX *ctx = SSL_CTX_new(meth);
74 SSL_CTX_set_verify(ctx,
75 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
76 0);
2ee50226
AC
77 SSL_CTX_set_verify_depth(ctx,
78 2);
3b267643
AC
79 return ctx;
80}
81
82void OpenSSL_SML_CTX_free(SSL_CTX *ctx) {
83 return SSL_CTX_free(ctx);
84}
85
a62a739c 86const SSL_METHOD *OpenSSL_SML_SSLv23_method() {
3b267643
AC
87 return SSLv23_method();
88}
89
90int OpenSSL_SML_load_verify_locations(SSL_CTX *ctx, const char *trust, const char *certs) {
91 return SSL_CTX_load_verify_locations(ctx, trust, certs);
92}
93
94BIO *OpenSSL_SML_new_ssl_connect(SSL_CTX *ctx) {
95 BIO *bio = BIO_new_ssl_connect(ctx);
96 SSL *ssl;
97
98 BIO_get_ssl(bio, &ssl);
99 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
100
101 return bio;
102}
103
60534712
AC
104BIO *OpenSSL_SML_new_ssl(SSL_CTX *ctx) {
105 BIO *bio = BIO_new_ssl_connect(ctx);
106 SSL *ssl;
107
108 BIO_get_ssl(bio, &ssl);
109 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
110
111 return bio;
112}
113
114BIO *OpenSSL_SML_new_accept(SSL_CTX *ctx, char *port) {
115 BIO *sbio = BIO_new_ssl(ctx, 0);
116 BIO *acpt = BIO_new_accept(port);
117
118 BIO_set_accept_bios(acpt, sbio);
119
120 return acpt;
121}
3b267643
AC
122
123SSL *OpenSSL_SML_get_ssl(BIO *bio) {
124 SSL *ssl;
125
126 if (BIO_get_ssl(bio, &ssl) <= 0)
127 return NULL;
128 else
129 return ssl;
130}
131
132int OpenSSL_SML_set_conn_hostname(BIO *bio, char *hostname) {
133 BIO_set_conn_hostname(bio, hostname);
134}
135
136int OpenSSL_SML_set_accept_port(BIO *bio, char *port) {
137 BIO_set_accept_port(bio, port);
138}
139
140int OpenSSL_SML_tcp_listen(int port, int qsize) {
141 int sock;
142 struct sockaddr_in sin;
143 int val=1;
144
145 if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
146 return -1;
147
148 memset(&sin,0,sizeof(sin));
149 sin.sin_addr.s_addr=INADDR_ANY;
150 sin.sin_family=AF_INET;
151 sin.sin_port=htons(port);
152 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,
153 &val,sizeof(val));
154
155 if(bind(sock,(struct sockaddr *)&sin,
156 sizeof(sin))<0)
157 return -1;
158 listen(sock, qsize);
159
160 return sock;
161}
162
163int OpenSSL_SML_accept(int sock) {
164 return accept(sock, 0, 0);
165}
166
167BIO *OpenSSL_SML_new_socket(int sock) {
168 return BIO_new_socket(sock, BIO_NOCLOSE);
169}
170
171SSL *OpenSSL_SML_SSL_new(SSL_CTX *ctx) {
172 SSL *ssl = SSL_new(ctx);
173
174 return ssl;
175}
176
177int OpenSSL_SML_SSL_shutdown(SSL *ssl) {
178 return SSL_shutdown(ssl);
179}
180
181void OpenSSL_SML_shutdown(int sock) {
182 shutdown(sock, 1);
183}
184
185void OpenSSL_SML_SSL_set_bio(SSL *ssl, BIO *b1, BIO *b2) {
186 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
187 SSL_set_bio(ssl, b1, b2);
60534712 188 BIO_set_ssl(b1, ssl, BIO_NOCLOSE);
3b267643
AC
189}
190
191int OpenSSL_SML_use_PrivateKey_file(SSL_CTX *ctx, char *keyfile) {
192 return SSL_CTX_use_PrivateKey_file(ctx,
193 keyfile,
194 SSL_FILETYPE_PEM);
195}
196
197int OpenSSL_SML_SSL_accept(SSL *ssl) {
198 return SSL_accept(ssl);
199}
200
201int OpenSSL_SML_use_certificate_chain_file(SSL_CTX *ctx, char *keyfile) {
202 return SSL_CTX_use_certificate_chain_file(ctx,
203 keyfile);
204}
205
3b267643
AC
206const char *OpenSSL_SML_get_peer_name(SSL *ssl) {
207 X509 *x = SSL_get_peer_certificate(ssl);
3b267643
AC
208
209 if (x) {
210 X509_NAME *name = X509_get_subject_name(x);
211
212 if (name) {
3b267643
AC
213 X509_NAME_ENTRY *ne;
214
51be7da6 215 ne = X509_NAME_get_entry(name, 0);
3b267643
AC
216
217 if (ne) {
218 ASN1_STRING *s = X509_NAME_ENTRY_get_data(ne);
219 static char ret[1024];
220
a62a739c 221 if (ASN1_STRING_length(s) >= sizeof ret)
3b267643
AC
222 return NULL;
223 else {
a62a739c
CE
224 memcpy(ret, ASN1_STRING_get0_data(s), ASN1_STRING_length(s));
225 ret[ASN1_STRING_length(s)] = 0;
3b267643
AC
226 return ret;
227 }
60534712
AC
228 } else {
229 printf("Null entry\n");
3b267643 230 return NULL;
60534712
AC
231 }
232 } else {
233 printf("Null name\n");
3b267643 234 return NULL;
60534712
AC
235 }
236 } else {
237 printf("Null certificate\n");
3b267643 238 return NULL;
60534712
AC
239 }
240}
241
242BIO *OpenSSL_SML_pop(BIO *b) {
243 return BIO_pop(b);
3b267643 244}
19f3c4e6
AC
245
246BIO *OpenSSL_SML_next(BIO *b) {
247 return BIO_next(b);
248}
36e42cb8
AC
249
250int OpenSSL_SML_puts(BIO *b, const char *buf) {
251 return BIO_puts(b, buf);
252}