1 /*-------------------------------------------------------------------------
4 * functions related to setting up a secure connection to the frontend.
5 * Secure connections are expected to provide confidentiality,
6 * message integrity and endpoint authentication.
9 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * src/backend/libpq/be-secure.c
16 * Since the server static private key ($DataDir/server.key)
17 * will normally be stored unencrypted so that the database
18 * backend can restart automatically, it is important that
19 * we select an algorithm that continues to provide confidentiality
20 * even if the attacker has the server's private key. Ephemeral
21 * DH (EDH) keys provide this, and in fact provide Perfect Forward
22 * Secrecy (PFS) except for situations where the session can
23 * be hijacked during a periodic handshake/renegotiation.
24 * Even that backdoor can be closed if client certificates
25 * are used (since the imposter will be unable to successfully
26 * complete renegotiation).
28 * N.B., the static private key should still be protected to
29 * the largest extent possible, to minimize the risk of
32 * Another benefit of EDH is that it allows the backend and
33 * clients to use DSA keys. DSA keys can only provide digital
34 * signatures, not encryption, and are often acceptable in
35 * jurisdictions where RSA keys are unacceptable.
37 * The downside to EDH is that it makes it impossible to
38 * use ssldump(1) if there's a problem establishing an SSL
39 * session. In this case you'll need to temporarily disable
40 * EDH by commenting out the callback.
44 * Because the risk of cryptanalysis increases as large
45 * amounts of data are sent with the same session key, the
46 * session keys are periodically renegotiated.
48 *-------------------------------------------------------------------------
57 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #ifdef HAVE_NETINET_TCP_H
62 #include <netinet/tcp.h>
63 #include <arpa/inet.h>
67 #include <openssl/ssl.h>
68 #include <openssl/dh.h>
69 #if SSLEAY_VERSION_NUMBER >= 0x0907000L
70 #include <openssl/conf.h>
74 #include "libpq/libpq.h"
75 #include "tcop/tcopprot.h"
76 #include "utils/memutils.h"
81 static DH *load_dh_file(int keylength);
82 static DH *load_dh_buffer(const char *, size_t);
83 static DH *tmp_dh_cb(SSL *s, int is_export, int keylength);
84 static int verify_cb(int, X509_STORE_CTX *);
85 static void info_cb(const SSL *ssl, int type, int args);
86 static void initialize_SSL(void);
87 static int open_server_SSL(Port *);
88 static void close_SSL(Port *);
89 static const char *SSLerrmessage(void);
98 * How much data can be sent across a secure connection
99 * (total in both directions) before we require renegotiation.
100 * Set to 0 to disable renegotiation completely.
102 int ssl_renegotiation_limit;
105 static SSL_CTX *SSL_context = NULL;
106 static bool ssl_loaded_verify_locations = false;
109 /* GUC variable controlling SSL cipher list */
110 char *SSLCipherSuites = NULL;
112 /* ------------------------------------------------------------ */
113 /* Hardcoded values */
114 /* ------------------------------------------------------------ */
117 * Hardcoded DH parameters, used in ephemeral DH keying.
118 * As discussed above, EDH protects the confidentiality of
119 * sessions even if the static private key is compromised,
120 * so we are *highly* motivated to ensure that we can use
121 * EDH even if the DBA... or an attacker... deletes the
122 * $DataDir/dh*.pem files.
124 * We could refuse SSL connections unless a good DH parameter
125 * file exists, but some clients may quietly renegotiate an
126 * unsecured connection without fully informing the user.
129 * Alternatively, the backend could attempt to load these files
130 * on startup if SSL is enabled - and refuse to start if any
131 * do not exist - but this would tend to piss off DBAs.
133 * If you want to create your own hardcoded DH parameters
134 * for fun and profit, review "Assigned Number for SKIP
135 * Protocols" (http://www.skip-vpn.org/spec/numbers.html)
140 static const char file_dh512[] =
141 "-----BEGIN DH PARAMETERS-----\n\
142 MEYCQQD1Kv884bEpQBgRjXyEpwpy1obEAxnIByl6ypUM2Zafq9AKUJsCRtMIPWak\n\
143 XUGfnHy9iUsiGSa6q6Jew1XpKgVfAgEC\n\
144 -----END DH PARAMETERS-----\n";
146 static const char file_dh1024[] =
147 "-----BEGIN DH PARAMETERS-----\n\
148 MIGHAoGBAPSI/VhOSdvNILSd5JEHNmszbDgNRR0PfIizHHxbLY7288kjwEPwpVsY\n\
149 jY67VYy4XTjTNP18F1dDox0YbN4zISy1Kv884bEpQBgRjXyEpwpy1obEAxnIByl6\n\
150 ypUM2Zafq9AKUJsCRtMIPWakXUGfnHy9iUsiGSa6q6Jew1XpL3jHAgEC\n\
151 -----END DH PARAMETERS-----\n";
153 static const char file_dh2048[] =
154 "-----BEGIN DH PARAMETERS-----\n\
155 MIIBCAKCAQEA9kJXtwh/CBdyorrWqULzBej5UxE5T7bxbrlLOCDaAadWoxTpj0BV\n\
156 89AHxstDqZSt90xkhkn4DIO9ZekX1KHTUPj1WV/cdlJPPT2N286Z4VeSWc39uK50\n\
157 T8X8dryDxUcwYc58yWb/Ffm7/ZFexwGq01uejaClcjrUGvC/RgBYK+X0iP1YTknb\n\
158 zSC0neSRBzZrM2w4DUUdD3yIsxx8Wy2O9vPJI8BD8KVbGI2Ou1WMuF040zT9fBdX\n\
159 Q6MdGGzeMyEstSr/POGxKUAYEY18hKcKctaGxAMZyAcpesqVDNmWn6vQClCbAkbT\n\
160 CD1mpF1Bn5x8vYlLIhkmuquiXsNV6TILOwIBAg==\n\
161 -----END DH PARAMETERS-----\n";
163 static const char file_dh4096[] =
164 "-----BEGIN DH PARAMETERS-----\n\
165 MIICCAKCAgEA+hRyUsFN4VpJ1O8JLcCo/VWr19k3BCgJ4uk+d+KhehjdRqNDNyOQ\n\
166 l/MOyQNQfWXPeGKmOmIig6Ev/nm6Nf9Z2B1h3R4hExf+zTiHnvVPeRBhjdQi81rt\n\
167 Xeoh6TNrSBIKIHfUJWBh3va0TxxjQIs6IZOLeVNRLMqzeylWqMf49HsIXqbcokUS\n\
168 Vt1BkvLdW48j8PPv5DsKRN3tloTxqDJGo9tKvj1Fuk74A+Xda1kNhB7KFlqMyN98\n\
169 VETEJ6c7KpfOo30mnK30wqw3S8OtaIR/maYX72tGOno2ehFDkq3pnPtEbD2CScxc\n\
170 alJC+EL7RPk5c/tgeTvCngvc1KZn92Y//EI7G9tPZtylj2b56sHtMftIoYJ9+ODM\n\
171 sccD5Piz/rejE3Ome8EOOceUSCYAhXn8b3qvxVI1ddd1pED6FHRhFvLrZxFvBEM9\n\
172 ERRMp5QqOaHJkM+Dxv8Cj6MqrCbfC4u+ZErxodzuusgDgvZiLF22uxMZbobFWyte\n\
173 OvOzKGtwcTqO/1wV5gKkzu1ZVswVUQd5Gg8lJicwqRWyyNRczDDoG9jVDxmogKTH\n\
174 AaqLulO7R8Ifa1SwF2DteSGVtgWEN8gDpN3RBmmPTDngyF2DHb5qmpnznwtFKdTL\n\
175 KWbuHn491xNO25CQWMtem80uKw+pTnisBRF/454n1Jnhub144YRBoN8CAQI=\n\
176 -----END DH PARAMETERS-----\n";
179 /* ------------------------------------------------------------ */
180 /* Procedures common to all secure sessions */
181 /* ------------------------------------------------------------ */
184 * Initialize global context
187 secure_initialize(void)
197 * Indicate if we have loaded the root CA store to verify certificates
200 secure_loaded_verify_locations(void)
203 return ssl_loaded_verify_locations;
210 * Attempt to negotiate secure session.
213 secure_open_server(Port *port)
218 r = open_server_SSL(port);
225 * Close secure session.
228 secure_close(Port *port)
237 * Read data from a secure connection.
240 secure_read(Port *port, void *ptr, size_t len)
251 n = SSL_read(port->ssl, ptr, len);
252 err = SSL_get_error(port->ssl, n);
258 case SSL_ERROR_WANT_READ:
259 case SSL_ERROR_WANT_WRITE:
267 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
268 (err == SSL_ERROR_WANT_READ) ?
269 FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE,
273 case SSL_ERROR_SYSCALL:
274 /* leave it to caller to ereport the value of errno */
283 (errcode(ERRCODE_PROTOCOL_VIOLATION),
284 errmsg("SSL error: %s", SSLerrmessage())));
286 case SSL_ERROR_ZERO_RETURN:
292 (errcode(ERRCODE_PROTOCOL_VIOLATION),
293 errmsg("unrecognized SSL error code: %d",
302 prepare_for_client_read();
304 n = recv(port->sock, ptr, len, 0);
313 * Write data to a secure connection.
316 secure_write(Port *port, void *ptr, size_t len)
325 if (ssl_renegotiation_limit && port->count > ssl_renegotiation_limit * 1024L)
327 SSL_set_session_id_context(port->ssl, (void *) &SSL_context,
328 sizeof(SSL_context));
329 if (SSL_renegotiate(port->ssl) <= 0)
331 (errcode(ERRCODE_PROTOCOL_VIOLATION),
332 errmsg("SSL renegotiation failure")));
333 if (SSL_do_handshake(port->ssl) <= 0)
335 (errcode(ERRCODE_PROTOCOL_VIOLATION),
336 errmsg("SSL renegotiation failure")));
337 if (port->ssl->state != SSL_ST_OK)
339 (errcode(ERRCODE_PROTOCOL_VIOLATION),
340 errmsg("SSL failed to send renegotiation request")));
341 port->ssl->state |= SSL_ST_ACCEPT;
342 SSL_do_handshake(port->ssl);
343 if (port->ssl->state != SSL_ST_OK)
345 (errcode(ERRCODE_PROTOCOL_VIOLATION),
346 errmsg("SSL renegotiation failure")));
352 n = SSL_write(port->ssl, ptr, len);
353 err = SSL_get_error(port->ssl, n);
359 case SSL_ERROR_WANT_READ:
360 case SSL_ERROR_WANT_WRITE:
362 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
363 (err == SSL_ERROR_WANT_READ) ?
364 FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE,
368 case SSL_ERROR_SYSCALL:
369 /* leave it to caller to ereport the value of errno */
378 (errcode(ERRCODE_PROTOCOL_VIOLATION),
379 errmsg("SSL error: %s", SSLerrmessage())));
381 case SSL_ERROR_ZERO_RETURN:
387 (errcode(ERRCODE_PROTOCOL_VIOLATION),
388 errmsg("unrecognized SSL error code: %d",
396 n = send(port->sock, ptr, len, 0);
401 /* ------------------------------------------------------------ */
402 /* SSL specific code */
403 /* ------------------------------------------------------------ */
407 * Private substitute BIO: this does the sending and receiving using send() and
408 * recv() instead. This is so that we can enable and disable interrupts
409 * just while calling recv(). We cannot have interrupts occurring while
410 * the bulk of openssl runs, because it uses malloc() and possibly other
411 * non-reentrant libc facilities. We also need to call send() and recv()
412 * directly so it gets passed through the socket/signals layer on Win32.
414 * They are closely modelled on the original socket implementations in OpenSSL.
417 static bool my_bio_initialized = false;
418 static BIO_METHOD my_bio_methods;
421 my_sock_read(BIO *h, char *buf, int size)
425 prepare_for_client_read();
429 res = recv(h->num, buf, size, 0);
430 BIO_clear_retry_flags(h);
433 /* If we were interrupted, tell caller to retry */
436 BIO_set_retry_read(h);
447 my_sock_write(BIO *h, const char *buf, int size)
451 res = send(h->num, buf, size, 0);
456 BIO_set_retry_write(h);
464 my_BIO_s_socket(void)
466 if (!my_bio_initialized)
468 memcpy(&my_bio_methods, BIO_s_socket(), sizeof(BIO_METHOD));
469 my_bio_methods.bread = my_sock_read;
470 my_bio_methods.bwrite = my_sock_write;
471 my_bio_initialized = true;
473 return &my_bio_methods;
476 /* This should exactly match openssl's SSL_set_fd except for using my BIO */
478 my_SSL_set_fd(SSL *s, int fd)
483 bio = BIO_new(my_BIO_s_socket());
487 SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
490 BIO_set_fd(bio, fd, BIO_NOCLOSE);
491 SSL_set_bio(s, bio, bio);
498 * Load precomputed DH parameters.
500 * To prevent "downgrade" attacks, we perform a number of checks
501 * to verify that the DBA-generated DH parameters file contains
502 * what we expect it to contain.
505 load_dh_file(int keylength)
508 char fnbuf[MAXPGPATH];
512 /* attempt to open file. It's not an error if it doesn't exist. */
513 snprintf(fnbuf, sizeof(fnbuf), "dh%d.pem", keylength);
514 if ((fp = fopen(fnbuf, "r")) == NULL)
517 /* flock(fileno(fp), LOCK_SH); */
518 dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
519 /* flock(fileno(fp), LOCK_UN); */
522 /* is the prime the correct size? */
523 if (dh != NULL && 8 * DH_size(dh) < keylength)
525 elog(LOG, "DH errors (%s): %d bits expected, %d bits found",
526 fnbuf, keylength, 8 * DH_size(dh));
530 /* make sure the DH parameters are usable */
533 if (DH_check(dh, &codes) == 0)
535 elog(LOG, "DH_check error (%s): %s", fnbuf, SSLerrmessage());
538 if (codes & DH_CHECK_P_NOT_PRIME)
540 elog(LOG, "DH error (%s): p is not prime", fnbuf);
543 if ((codes & DH_NOT_SUITABLE_GENERATOR) &&
544 (codes & DH_CHECK_P_NOT_SAFE_PRIME))
547 "DH error (%s): neither suitable generator or safe prime",
557 * Load hardcoded DH parameters.
559 * To prevent problems if the DH parameters files don't even
560 * exist, we can load DH parameters hardcoded into this file.
563 load_dh_buffer(const char *buffer, size_t len)
568 bio = BIO_new_mem_buf((char *) buffer, len);
571 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
574 (errmsg_internal("DH load buffer: %s",
582 * Generate an ephemeral DH key. Because this can take a long
583 * time to compute, we can use precomputed parameters of the
586 * Since few sites will bother to precompute these parameter
587 * files, we also provide a fallback to the parameters provided
588 * by the OpenSSL project.
590 * These values can be static (once loaded or computed) since
591 * the OpenSSL library can efficiently generate random keys from
592 * the information provided.
595 tmp_dh_cb(SSL *s, int is_export, int keylength)
598 static DH *dh = NULL;
599 static DH *dh512 = NULL;
600 static DH *dh1024 = NULL;
601 static DH *dh2048 = NULL;
602 static DH *dh4096 = NULL;
608 dh512 = load_dh_file(keylength);
610 dh512 = load_dh_buffer(file_dh512, sizeof file_dh512);
616 dh1024 = load_dh_file(keylength);
618 dh1024 = load_dh_buffer(file_dh1024, sizeof file_dh1024);
624 dh2048 = load_dh_file(keylength);
626 dh2048 = load_dh_buffer(file_dh2048, sizeof file_dh2048);
632 dh4096 = load_dh_file(keylength);
634 dh4096 = load_dh_buffer(file_dh4096, sizeof file_dh4096);
640 dh = load_dh_file(keylength);
644 /* this may take a long time, but it may be necessary... */
645 if (r == NULL || 8 * DH_size(r) < keylength)
648 (errmsg_internal("DH: generating parameters (%d bits)",
650 r = DH_generate_parameters(keylength, DH_GENERATOR_2, NULL, NULL);
657 * Certificate verification callback
659 * This callback allows us to log intermediate problems during
660 * verification, but for now we'll see if the final error message
661 * contains enough information.
663 * This callback also allows us to override the default acceptance
664 * criteria (e.g., accepting self-signed or expired certs), but
665 * for now we accept the default checks.
668 verify_cb(int ok, X509_STORE_CTX *ctx)
674 * This callback is used to copy SSL information messages
675 * into the PostgreSQL log.
678 info_cb(const SSL *ssl, int type, int args)
682 case SSL_CB_HANDSHAKE_START:
684 (errmsg_internal("SSL: handshake start")));
686 case SSL_CB_HANDSHAKE_DONE:
688 (errmsg_internal("SSL: handshake done")));
690 case SSL_CB_ACCEPT_LOOP:
692 (errmsg_internal("SSL: accept loop")));
694 case SSL_CB_ACCEPT_EXIT:
696 (errmsg_internal("SSL: accept exit (%d)", args)));
698 case SSL_CB_CONNECT_LOOP:
700 (errmsg_internal("SSL: connect loop")));
702 case SSL_CB_CONNECT_EXIT:
704 (errmsg_internal("SSL: connect exit (%d)", args)));
706 case SSL_CB_READ_ALERT:
708 (errmsg_internal("SSL: read alert (0x%04x)", args)));
710 case SSL_CB_WRITE_ALERT:
712 (errmsg_internal("SSL: write alert (0x%04x)", args)));
718 * Initialize global SSL context.
725 STACK_OF(X509_NAME) *root_cert_list = NULL;
729 #if SSLEAY_VERSION_NUMBER >= 0x0907000L
730 OPENSSL_config(NULL);
733 SSL_load_error_strings();
734 SSL_context = SSL_CTX_new(SSLv23_method());
737 (errmsg("could not create SSL context: %s",
741 * Disable OpenSSL's moving-write-buffer sanity check, because it
742 * causes unnecessary failures in nonblocking send cases.
744 SSL_CTX_set_mode(SSL_context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
747 * Load and verify server's certificate and private key
749 if (SSL_CTX_use_certificate_chain_file(SSL_context,
752 (errcode(ERRCODE_CONFIG_FILE_ERROR),
753 errmsg("could not load server certificate file \"%s\": %s",
754 ssl_cert_file, SSLerrmessage())));
756 if (stat(ssl_key_file, &buf) != 0)
758 (errcode_for_file_access(),
759 errmsg("could not access private key file \"%s\": %m",
763 * Require no public access to key file.
765 * XXX temporarily suppress check when on Windows, because there may
766 * not be proper support for Unix-y file permissions. Need to think
767 * of a reasonable check to apply on Windows. (See also the data
768 * directory permission check in postmaster.c)
770 #if !defined(WIN32) && !defined(__CYGWIN__)
771 if (!S_ISREG(buf.st_mode) || buf.st_mode & (S_IRWXG | S_IRWXO))
773 (errcode(ERRCODE_CONFIG_FILE_ERROR),
774 errmsg("private key file \"%s\" has group or world access",
776 errdetail("Permissions should be u=rw (0600) or less.")));
779 if (SSL_CTX_use_PrivateKey_file(SSL_context,
781 SSL_FILETYPE_PEM) != 1)
783 (errmsg("could not load private key file \"%s\": %s",
784 ssl_key_file, SSLerrmessage())));
786 if (SSL_CTX_check_private_key(SSL_context) != 1)
788 (errmsg("check of private key failed: %s",
792 /* set up ephemeral DH keys, and disallow SSL v2 while at it */
793 SSL_CTX_set_tmp_dh_callback(SSL_context, tmp_dh_cb);
794 SSL_CTX_set_options(SSL_context, SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv2);
796 /* set up the allowed cipher list */
797 if (SSL_CTX_set_cipher_list(SSL_context, SSLCipherSuites) != 1)
798 elog(FATAL, "could not set the cipher list (no valid ciphers available)");
801 * Load CA store, so we can verify client certificates if needed.
805 if (SSL_CTX_load_verify_locations(SSL_context, ssl_ca_file, NULL) != 1 ||
806 (root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
808 (errmsg("could not load root certificate file \"%s\": %s",
809 ssl_ca_file, SSLerrmessage())));
813 * Load the Certificate Revocation List (CRL).
814 * http://searchsecurity.techtarget.com/sDefinition/0,,sid14_gci803160,00.html
819 X509_STORE *cvstore = SSL_CTX_get_cert_store(SSL_context);
823 /* Set the flags to check against the complete CRL chain */
824 if (X509_STORE_load_locations(cvstore, ssl_crl_file, NULL) == 1)
826 /* OpenSSL 0.96 does not support X509_V_FLAG_CRL_CHECK */
827 #ifdef X509_V_FLAG_CRL_CHECK
828 X509_STORE_set_flags(cvstore,
829 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
832 (errmsg("SSL certificate revocation list file \"%s\" ignored",
834 errdetail("SSL library does not support certificate revocation lists.")));
839 (errmsg("could not load SSL certificate revocation list file \"%s\": %s",
840 ssl_crl_file, SSLerrmessage())));
847 * Always ask for SSL client cert, but don't fail if it's not
848 * presented. We might fail such connections later, depending on what
849 * we find in pg_hba.conf.
851 SSL_CTX_set_verify(SSL_context,
853 SSL_VERIFY_CLIENT_ONCE),
856 /* Set flag to remember CA store is successfully loaded */
857 ssl_loaded_verify_locations = true;
860 * Tell OpenSSL to send the list of root certs we trust to clients in
861 * CertificateRequests. This lets a client with a keystore select the
862 * appropriate client certificate to send to us.
864 SSL_CTX_set_client_CA_list(SSL_context, root_cert_list);
869 * Attempt to negotiate SSL connection.
872 open_server_SSL(Port *port)
880 if (!(port->ssl = SSL_new(SSL_context)))
883 (errcode(ERRCODE_PROTOCOL_VIOLATION),
884 errmsg("could not initialize SSL connection: %s",
889 if (!my_SSL_set_fd(port->ssl, port->sock))
892 (errcode(ERRCODE_PROTOCOL_VIOLATION),
893 errmsg("could not set SSL socket: %s",
900 r = SSL_accept(port->ssl);
903 err = SSL_get_error(port->ssl, r);
906 case SSL_ERROR_WANT_READ:
907 case SSL_ERROR_WANT_WRITE:
909 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
910 (err == SSL_ERROR_WANT_READ) ?
911 FD_READ | FD_CLOSE | FD_ACCEPT : FD_WRITE | FD_CLOSE,
915 case SSL_ERROR_SYSCALL:
918 (errcode_for_socket_access(),
919 errmsg("could not accept SSL connection: %m")));
922 (errcode(ERRCODE_PROTOCOL_VIOLATION),
923 errmsg("could not accept SSL connection: EOF detected")));
927 (errcode(ERRCODE_PROTOCOL_VIOLATION),
928 errmsg("could not accept SSL connection: %s",
931 case SSL_ERROR_ZERO_RETURN:
933 (errcode(ERRCODE_PROTOCOL_VIOLATION),
934 errmsg("could not accept SSL connection: EOF detected")));
938 (errcode(ERRCODE_PROTOCOL_VIOLATION),
939 errmsg("unrecognized SSL error code: %d",
949 /* Get client certificate, if available. */
950 port->peer = SSL_get_peer_certificate(port->ssl);
952 /* and extract the Common Name from it. */
953 port->peer_cn = NULL;
954 if (port->peer != NULL)
958 len = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
959 NID_commonName, NULL, 0);
964 peer_cn = MemoryContextAlloc(TopMemoryContext, len + 1);
965 r = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
966 NID_commonName, peer_cn, len + 1);
970 /* shouldn't happen */
977 * Reject embedded NULLs in certificate common name to prevent
978 * attacks like CVE-2009-4034.
980 if (len != strlen(peer_cn))
983 (errcode(ERRCODE_PROTOCOL_VIOLATION),
984 errmsg("SSL certificate's common name contains embedded null")));
990 port->peer_cn = peer_cn;
995 (errmsg("SSL connection from \"%s\"",
996 port->peer_cn ? port->peer_cn : "(anonymous)")));
998 /* set up debugging/info callback */
999 SSL_CTX_set_info_callback(SSL_context, info_cb);
1005 * Close SSL connection.
1008 close_SSL(Port *port)
1012 SSL_shutdown(port->ssl);
1013 SSL_free(port->ssl);
1019 X509_free(port->peer);
1025 pfree(port->peer_cn);
1026 port->peer_cn = NULL;
1031 * Obtain reason string for last SSL error
1033 * Some caution is needed here since ERR_reason_error_string will
1034 * return NULL if it doesn't recognize the error code. We don't
1035 * want to return NULL ever.
1040 unsigned long errcode;
1041 const char *errreason;
1042 static char errbuf[32];
1044 errcode = ERR_get_error();
1046 return _("no SSL error reported");
1047 errreason = ERR_reason_error_string(errcode);
1048 if (errreason != NULL)
1050 snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), errcode);
1054 #endif /* USE_SSL */