Server gets client's CN
[hcoop/domtool2.git] / openssl / openssl_sml.c
CommitLineData
559e89e9
AC
1#include "openssl/bio.h"
2#include "openssl/ssl.h"
3#include "openssl/err.h"
4
3b267643
AC
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <unistd.h>
8#include <netinet/in.h>
9
10void OpenSSL_SML_init() {
11 SSL_library_init();
559e89e9
AC
12}
13
14void OpenSSL_SML_load_error_strings() {
15 SSL_load_error_strings();
3b267643 16 ERR_load_X509_strings();
559e89e9
AC
17}
18
19void OpenSSL_SML_load_BIO_strings() {
20 ERR_load_BIO_strings();
21}
22
23int OpenSSL_SML_get_error() {
24 return ERR_get_error();
25}
26
27const char *OpenSSL_SML_lib_error_string(int err) {
28 return ERR_lib_error_string(err);
29}
30
31const char *OpenSSL_SML_func_error_string(int err) {
32 return ERR_func_error_string(err);
33}
34
35const char *OpenSSL_SML_reason_error_string(int err) {
36 return ERR_reason_error_string(err);
37}
38
07cc384c
AC
39int OpenSSL_SML_read(BIO *b, void *data, int len) {
40 return BIO_read(b, data, len);
41}
42
559e89e9
AC
43int OpenSSL_SML_write(BIO *b, const void *data, int len) {
44 return BIO_write(b, data, len);
45}
46
47BIO *OpenSSL_SML_new_connect(char *addr) {
48 return BIO_new_connect(addr);
49}
50
51void OpenSSL_SML_free_all(BIO *b) {
52 BIO_free_all(b);
53}
54
55int OpenSSL_SML_do_connect(BIO *b) {
56 return BIO_do_connect(b);
57}
07cc384c
AC
58
59int OpenSSL_SML_do_accept(BIO *b) {
60 return BIO_do_accept(b);
61}
3b267643 62
60534712
AC
63int OpenSSL_SML_do_handshake(BIO *b) {
64 return BIO_do_handshake(b);
65}
66
3b267643
AC
67SSL_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
75void OpenSSL_SML_CTX_free(SSL_CTX *ctx) {
76 return SSL_CTX_free(ctx);
77}
78
79SSL_METHOD *OpenSSL_SML_SSLv23_method() {
80 return SSLv23_method();
81}
82
83int 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
87BIO *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
60534712
AC
97BIO *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
107BIO *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}
3b267643
AC
115
116SSL *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
125int OpenSSL_SML_set_conn_hostname(BIO *bio, char *hostname) {
126 BIO_set_conn_hostname(bio, hostname);
127}
128
129int OpenSSL_SML_set_accept_port(BIO *bio, char *port) {
130 BIO_set_accept_port(bio, port);
131}
132
133int 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
156int OpenSSL_SML_accept(int sock) {
157 return accept(sock, 0, 0);
158}
159
160BIO *OpenSSL_SML_new_socket(int sock) {
161 return BIO_new_socket(sock, BIO_NOCLOSE);
162}
163
164SSL *OpenSSL_SML_SSL_new(SSL_CTX *ctx) {
165 SSL *ssl = SSL_new(ctx);
166
167 return ssl;
168}
169
170int OpenSSL_SML_SSL_shutdown(SSL *ssl) {
171 return SSL_shutdown(ssl);
172}
173
174void OpenSSL_SML_shutdown(int sock) {
175 shutdown(sock, 1);
176}
177
178void 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);
60534712 181 BIO_set_ssl(b1, ssl, BIO_NOCLOSE);
3b267643
AC
182}
183
184int 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
190int OpenSSL_SML_SSL_accept(SSL *ssl) {
191 return SSL_accept(ssl);
192}
193
194int OpenSSL_SML_use_certificate_chain_file(SSL_CTX *ctx, char *keyfile) {
195 return SSL_CTX_use_certificate_chain_file(ctx,
196 keyfile);
197}
198
199static unsigned char subject[] = "Subject";
200
201const char *OpenSSL_SML_get_peer_name(SSL *ssl) {
202 X509 *x = SSL_get_peer_certificate(ssl);
203 unsigned char *name = subject;
204 X509_NAME *nm;
205
206 if (x) {
207 X509_NAME *name = X509_get_subject_name(x);
208
209 if (name) {
210 unsigned char *out;
211 X509_NAME_ENTRY *ne;
212
213 ne = X509_NAME_get_entry(name, 3);
214
215 if (ne) {
216 ASN1_STRING *s = X509_NAME_ENTRY_get_data(ne);
217 static char ret[1024];
218
219 if (M_ASN1_STRING_length(s) >= sizeof ret)
220 return NULL;
221 else {
222 memcpy(ret, M_ASN1_STRING_data(s), M_ASN1_STRING_length(s));
223 ret[M_ASN1_STRING_length(s)] = 0;
224 return ret;
225 }
60534712
AC
226 } else {
227 printf("Null entry\n");
3b267643 228 return NULL;
60534712
AC
229 }
230 } else {
231 printf("Null name\n");
3b267643 232 return NULL;
60534712
AC
233 }
234 } else {
235 printf("Null certificate\n");
3b267643 236 return NULL;
60534712
AC
237 }
238}
239
240BIO *OpenSSL_SML_pop(BIO *b) {
241 return BIO_pop(b);
3b267643 242}
19f3c4e6
AC
243
244BIO *OpenSSL_SML_next(BIO *b) {
245 return BIO_next(b);
246}