2 #include <netinet/in.h>
8 #include <sys/socket.h>
10 #include <sys/types.h>
14 #include <openssl/crypto.h>
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 #include <openssl/ssl.h>
18 #include <openssl/err.h>
26 int conn_read(struct conn
*conn
, char *buf
, int len
)
29 return SSL_read(conn
->ssl
, buf
, len
);
30 return read(conn
->fd
, buf
, len
);
33 int conn_write(struct conn
*conn
, char *buf
, int len
)
36 return SSL_write(conn
->ssl
, buf
, len
);
37 return write(conn
->fd
, buf
, len
);
40 int conn_tls(struct conn
*conn
, char *certfile
)
42 SSLeay_add_ssl_algorithms();
43 SSL_load_error_strings();
44 conn
->ctx
= SSL_CTX_new(SSLv23_method());
47 conn
->ssl
= SSL_new(conn
->ctx
);
51 SSL_CTX_set_verify(conn
->ctx
, SSL_VERIFY_PEER
, NULL
);
52 SSL_CTX_load_verify_locations(conn
->ctx
, certfile
, NULL
);
54 SSL_set_fd(conn
->ssl
, conn
->fd
);
55 if (SSL_connect(conn
->ssl
) != 1)
57 if (certfile
&& SSL_get_verify_result(conn
->ssl
) != X509_V_OK
)
62 struct conn
*conn_connect(char *addr
, char *port
, char *certfile
)
64 struct addrinfo hints
, *addrinfo
;
68 memset(&hints
, 0, sizeof(hints
));
69 hints
.ai_family
= AF_UNSPEC
;
70 hints
.ai_socktype
= SOCK_STREAM
;
71 hints
.ai_flags
= AI_PASSIVE
;
73 if (getaddrinfo(addr
, port
, &hints
, &addrinfo
))
75 fd
= socket(addrinfo
->ai_family
, addrinfo
->ai_socktype
,
76 addrinfo
->ai_protocol
);
78 if (connect(fd
, addrinfo
->ai_addr
, addrinfo
->ai_addrlen
) == -1) {
80 freeaddrinfo(addrinfo
);
83 freeaddrinfo(addrinfo
);
85 conn
= malloc(sizeof(*conn
));
86 memset(conn
, 0, sizeof(*conn
));
91 int conn_close(struct conn
*conn
)
94 SSL_shutdown(conn
->ssl
);
96 SSL_CTX_free(conn
->ctx
);