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