hcoop: move dispatcher to gibran
[hcoop/domtool2.git] / openssl / openssl_sml.c
1 #include <openssl/ssl.h>
2 #include <openssl/err.h>
3 #include <openssl/asn1.h>
4
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <unistd.h>
9 #include <netinet/in.h>
10
11 void OpenSSL_SML_init() {
12 SSL_library_init();
13 }
14
15 void OpenSSL_SML_load_error_strings() {
16 SSL_load_error_strings();
17 ERR_load_X509_strings();
18 }
19
20 void OpenSSL_SML_load_BIO_strings() {
21 ERR_load_BIO_strings();
22 }
23
24 int OpenSSL_SML_get_error() {
25 return ERR_get_error();
26 }
27
28 int OpenSSL_SML_should_retry(BIO *b) {
29 return BIO_should_retry(b);
30 }
31
32 const char *OpenSSL_SML_lib_error_string(int err) {
33 return ERR_lib_error_string(err);
34 }
35
36 const char *OpenSSL_SML_func_error_string(int err) {
37 return ERR_func_error_string(err);
38 }
39
40 const char *OpenSSL_SML_reason_error_string(int err) {
41 return ERR_reason_error_string(err);
42 }
43
44 int OpenSSL_SML_read(BIO *b, void *data, int len) {
45 return BIO_read(b, data, len);
46 }
47
48 int OpenSSL_SML_write(BIO *b, const void *data, int len) {
49 return BIO_write(b, data, len);
50 }
51
52 BIO *OpenSSL_SML_new_connect(char *addr) {
53 return BIO_new_connect(addr);
54 }
55
56 void OpenSSL_SML_free_all(BIO *b) {
57 BIO_free_all(b);
58 }
59
60 int OpenSSL_SML_do_connect(BIO *b) {
61 return BIO_do_connect(b);
62 }
63
64 int OpenSSL_SML_do_accept(BIO *b) {
65 return BIO_do_accept(b);
66 }
67
68 int OpenSSL_SML_do_handshake(BIO *b) {
69 return BIO_do_handshake(b);
70 }
71
72 SSL_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);
77 SSL_CTX_set_verify_depth(ctx,
78 2);
79 return ctx;
80 }
81
82 void OpenSSL_SML_CTX_free(SSL_CTX *ctx) {
83 return SSL_CTX_free(ctx);
84 }
85
86 const SSL_METHOD *OpenSSL_SML_SSLv23_method() {
87 return SSLv23_method();
88 }
89
90 int 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
94 BIO *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
104 BIO *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
114 BIO *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 }
122
123 SSL *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
132 int OpenSSL_SML_set_conn_hostname(BIO *bio, char *hostname) {
133 BIO_set_conn_hostname(bio, hostname);
134 }
135
136 int OpenSSL_SML_set_accept_port(BIO *bio, char *port) {
137 BIO_set_accept_port(bio, port);
138 }
139
140 int 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
163 int OpenSSL_SML_accept(int sock) {
164 return accept(sock, 0, 0);
165 }
166
167 BIO *OpenSSL_SML_new_socket(int sock) {
168 return BIO_new_socket(sock, BIO_NOCLOSE);
169 }
170
171 SSL *OpenSSL_SML_SSL_new(SSL_CTX *ctx) {
172 SSL *ssl = SSL_new(ctx);
173
174 return ssl;
175 }
176
177 int OpenSSL_SML_SSL_shutdown(SSL *ssl) {
178 return SSL_shutdown(ssl);
179 }
180
181 void OpenSSL_SML_shutdown(int sock) {
182 shutdown(sock, 1);
183 }
184
185 void 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);
188 BIO_set_ssl(b1, ssl, BIO_NOCLOSE);
189 }
190
191 int 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
197 int OpenSSL_SML_SSL_accept(SSL *ssl) {
198 return SSL_accept(ssl);
199 }
200
201 int OpenSSL_SML_use_certificate_chain_file(SSL_CTX *ctx, char *keyfile) {
202 return SSL_CTX_use_certificate_chain_file(ctx,
203 keyfile);
204 }
205
206 const char *OpenSSL_SML_get_peer_name(SSL *ssl) {
207 X509 *x = SSL_get_peer_certificate(ssl);
208
209 if (x) {
210 X509_NAME *name = X509_get_subject_name(x);
211
212 if (name) {
213 X509_NAME_ENTRY *ne;
214
215 ne = X509_NAME_get_entry(name, 0);
216
217 if (ne) {
218 ASN1_STRING *s = X509_NAME_ENTRY_get_data(ne);
219 static char ret[1024];
220
221 if (ASN1_STRING_length(s) >= sizeof ret)
222 return NULL;
223 else {
224 memcpy(ret, ASN1_STRING_get0_data(s), ASN1_STRING_length(s));
225 ret[ASN1_STRING_length(s)] = 0;
226 return ret;
227 }
228 } else {
229 printf("Null entry\n");
230 return NULL;
231 }
232 } else {
233 printf("Null name\n");
234 return NULL;
235 }
236 } else {
237 printf("Null certificate\n");
238 return NULL;
239 }
240 }
241
242 BIO *OpenSSL_SML_pop(BIO *b) {
243 return BIO_pop(b);
244 }
245
246 BIO *OpenSSL_SML_next(BIO *b) {
247 return BIO_next(b);
248 }
249
250 int OpenSSL_SML_puts(BIO *b, const char *buf) {
251 return BIO_puts(b, buf);
252 }