2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 static const char *ssl_keyfile
= ""; //!< file containing the private key in PEM format
43 static const char *ssl_certfile
= ""; //!< file containing the server's certificate in PEM format
44 static const char *ssl_rootfile
= ""; //!< file containing the list of CAs trusted by the client
45 // TODO: a way to set ssl_rootfile from the command line, or an envvar?
50 void ssl_set_certfile(const char *certfile
)
52 ssl_certfile
= certfile
;
55 void ssl_set_keyfile(const char *keyfile
)
57 ssl_keyfile
= keyfile
;
60 int ssl_init_once(int is_server
, int enable_compression
, char *errbuf
, size_t errbuflen
)
62 static int inited
= 0;
66 SSL_load_error_strings();
67 OpenSSL_add_ssl_algorithms();
68 if (enable_compression
)
69 SSL_COMP_get_compression_methods();
71 SSL_METHOD
const *meth
=
72 is_server
? SSLv23_server_method() : SSLv23_client_method();
73 ctx
= SSL_CTX_new(meth
);
76 pcap_snprintf(errbuf
, errbuflen
, "Cannot get a new SSL context: %s", ERR_error_string(ERR_get_error(), NULL
));
80 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
84 char const *certfile
= ssl_certfile
[0] ? ssl_certfile
: "cert.pem";
85 if (1 != SSL_CTX_use_certificate_file(ctx
, certfile
, SSL_FILETYPE_PEM
))
87 pcap_snprintf(errbuf
, errbuflen
, "Cannot read certificate file %s: %s", certfile
, ERR_error_string(ERR_get_error(), NULL
));
91 char const *keyfile
= ssl_keyfile
[0] ? ssl_keyfile
: "key.pem";
92 if (1 != SSL_CTX_use_PrivateKey_file(ctx
, keyfile
, SSL_FILETYPE_PEM
))
94 pcap_snprintf(errbuf
, errbuflen
, "Cannot read private key file %s: %s", keyfile
, ERR_error_string(ERR_get_error(), NULL
));
102 if (! SSL_CTX_load_verify_locations(ctx
, ssl_rootfile
, 0))
104 pcap_snprintf(errbuf
, errbuflen
, "Cannot read CA list from %s", ssl_rootfile
);
110 SSL_CTX_set_verify(ctx
, SSL_VERIFY_NONE
, NULL
);
115 if (! RAND_load_file(RANDOM
, 1024*1024))
117 pcap_snprintf(errbuf
, errbuflen
, "Cannot init random");
123 SSL_CTX_set_session_id_context(ctx
, (void *)&s_server_session_id_context
, sizeof(s_server_session_id_context
));
134 SSL
*ssl_promotion_rw(int is_server
, SOCKET in
, SOCKET out
, char *errbuf
, size_t errbuflen
)
136 if (ssl_init_once(is_server
, 1, errbuf
, errbuflen
) < 0) {
140 SSL
*ssl
= SSL_new(ctx
); // TODO: also a DTLS context
141 SSL_set_rfd(ssl
, in
);
142 SSL_set_wfd(ssl
, out
);
145 if (SSL_accept(ssl
) <= 0) {
146 pcap_snprintf(errbuf
, errbuflen
, "SSL_accept(): %s",
147 ERR_error_string(ERR_get_error(), NULL
));
151 if (SSL_connect(ssl
) <= 0) {
152 pcap_snprintf(errbuf
, errbuflen
, "SSL_connect(): %s",
153 ERR_error_string(ERR_get_error(), NULL
));
161 SSL
*ssl_promotion(int is_server
, SOCKET s
, char *errbuf
, size_t errbuflen
)
163 return ssl_promotion_rw(is_server
, s
, s
, errbuf
, errbuflen
);
166 // Same return value as sock_send:
167 // 0 on OK, -1 on error but closed connection (-2).
168 int ssl_send(SSL
*ssl
, char const *buffer
, int size
, char *errbuf
, size_t errbuflen
)
170 int status
= SSL_write(ssl
, buffer
, size
);
173 // "SSL_write() will only return with success, when the complete contents (...) has been written."
178 int ssl_err
= SSL_get_error(ssl
, status
); // TODO: does it pop the error?
179 if (ssl_err
== SSL_ERROR_ZERO_RETURN
)
183 else if (ssl_err
== SSL_ERROR_SYSCALL
)
186 if (errno
== ECONNRESET
|| errno
== EPIPE
) return -2;
189 pcap_snprintf(errbuf
, errbuflen
, "SSL_write(): %s",
190 ERR_error_string(ERR_get_error(), NULL
));
195 // Returns the number of bytes read, or -1 on syserror, or -2 on SSL error.
196 int ssl_recv(SSL
*ssl
, char *buffer
, int size
, char *errbuf
, size_t errbuflen
)
198 int status
= SSL_read(ssl
, buffer
, size
);
201 int ssl_err
= SSL_get_error(ssl
, status
);
202 if (ssl_err
== SSL_ERROR_ZERO_RETURN
)
206 else if (ssl_err
== SSL_ERROR_SYSCALL
)
213 pcap_snprintf(errbuf
, errbuflen
, "SSL_read(): %s",
214 ERR_error_string(ERR_get_error(), NULL
));
224 #endif // HAVE_OPENSSL