1  | 
     | 
     | 
    /*	$OpenBSD: ssl_smtpd.c,v 1.13 2015/12/30 16:02:08 benno Exp $	*/  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org>  | 
    
    
    6  | 
     | 
     | 
     * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>  | 
    
    
    7  | 
     | 
     | 
     *  | 
    
    
    8  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    9  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    10  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    11  | 
     | 
     | 
     *  | 
    
    
    12  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    13  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    14  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    15  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    16  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
    
    
    17  | 
     | 
     | 
     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
    
    
    18  | 
     | 
     | 
     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    19  | 
     | 
     | 
     */  | 
    
    
    20  | 
     | 
     | 
     | 
    
    
    21  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    22  | 
     | 
     | 
    #include <sys/queue.h>  | 
    
    
    23  | 
     | 
     | 
    #include <sys/tree.h>  | 
    
    
    24  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    25  | 
     | 
     | 
    #include <sys/stat.h>  | 
    
    
    26  | 
     | 
     | 
     | 
    
    
    27  | 
     | 
     | 
    #include <ctype.h>  | 
    
    
    28  | 
     | 
     | 
    #include <event.h>  | 
    
    
    29  | 
     | 
     | 
    #include <fcntl.h>  | 
    
    
    30  | 
     | 
     | 
    #include <limits.h>  | 
    
    
    31  | 
     | 
     | 
    #include <imsg.h>  | 
    
    
    32  | 
     | 
     | 
    #include <pwd.h>  | 
    
    
    33  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    34  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    35  | 
     | 
     | 
    #include <string.h>  | 
    
    
    36  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    37  | 
     | 
     | 
     | 
    
    
    38  | 
     | 
     | 
    #include <openssl/ssl.h>  | 
    
    
    39  | 
     | 
     | 
    #include <openssl/engine.h>  | 
    
    
    40  | 
     | 
     | 
    #include <openssl/err.h>  | 
    
    
    41  | 
     | 
     | 
     | 
    
    
    42  | 
     | 
     | 
    #include "smtpd.h"  | 
    
    
    43  | 
     | 
     | 
    #include "log.h"  | 
    
    
    44  | 
     | 
     | 
    #include "ssl.h"  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
     | 
     | 
     | 
    
    
    47  | 
     | 
     | 
    void *  | 
    
    
    48  | 
     | 
     | 
    ssl_mta_init(void *pkiname, char *cert, off_t cert_len, const char *ciphers)  | 
    
    
    49  | 
     | 
     | 
    { | 
    
    
    50  | 
     | 
     | 
    	SSL_CTX	*ctx = NULL;  | 
    
    
    51  | 
     | 
     | 
    	SSL	*ssl = NULL;  | 
    
    
    52  | 
     | 
     | 
     | 
    
    
    53  | 
     | 
     | 
    	ctx = ssl_ctx_create(pkiname, cert, cert_len, ciphers);  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    	if ((ssl = SSL_new(ctx)) == NULL)  | 
    
    
    56  | 
     | 
     | 
    		goto err;  | 
    
    
    57  | 
     | 
     | 
    	if (!SSL_set_ssl_method(ssl, SSLv23_client_method()))  | 
    
    
    58  | 
     | 
     | 
    		goto err;  | 
    
    
    59  | 
     | 
     | 
     | 
    
    
    60  | 
     | 
     | 
    	SSL_CTX_free(ctx);  | 
    
    
    61  | 
     | 
     | 
    	return (void *)(ssl);  | 
    
    
    62  | 
     | 
     | 
     | 
    
    
    63  | 
     | 
     | 
    err:  | 
    
    
    64  | 
     | 
     | 
    	SSL_free(ssl);  | 
    
    
    65  | 
     | 
     | 
    	SSL_CTX_free(ctx);  | 
    
    
    66  | 
     | 
     | 
    	ssl_error("ssl_mta_init"); | 
    
    
    67  | 
     | 
     | 
    	return (NULL);  | 
    
    
    68  | 
     | 
     | 
    }  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    /* dummy_verify */  | 
    
    
    71  | 
     | 
     | 
    static int  | 
    
    
    72  | 
     | 
     | 
    dummy_verify(int ok, X509_STORE_CTX *store)  | 
    
    
    73  | 
     | 
     | 
    { | 
    
    
    74  | 
     | 
     | 
    	/*  | 
    
    
    75  | 
     | 
     | 
    	 * We *want* SMTP to request an optional client certificate, however we don't want the  | 
    
    
    76  | 
     | 
     | 
    	 * verification to take place in the SMTP process. This dummy verify will allow us to  | 
    
    
    77  | 
     | 
     | 
    	 * asynchronously verify in the lookup process.  | 
    
    
    78  | 
     | 
     | 
    	 */  | 
    
    
    79  | 
     | 
     | 
    	return 1;  | 
    
    
    80  | 
     | 
     | 
    }  | 
    
    
    81  | 
     | 
     | 
     | 
    
    
    82  | 
     | 
     | 
    void *  | 
    
    
    83  | 
     | 
     | 
    ssl_smtp_init(void *ssl_ctx, int verify)  | 
    
    
    84  | 
     | 
     | 
    { | 
    
    
    85  | 
     | 
     | 
    	SSL	*ssl = NULL;  | 
    
    
    86  | 
     | 
     | 
     | 
    
    
    87  | 
     | 
     | 
    	log_debug("debug: session_start_ssl: switching to SSL"); | 
    
    
    88  | 
     | 
     | 
     | 
    
    
    89  | 
     | 
     | 
    	if (verify)  | 
    
    
    90  | 
     | 
     | 
    		SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, dummy_verify);  | 
    
    
    91  | 
     | 
     | 
     | 
    
    
    92  | 
     | 
     | 
    	if ((ssl = SSL_new(ssl_ctx)) == NULL)  | 
    
    
    93  | 
     | 
     | 
    		goto err;  | 
    
    
    94  | 
     | 
     | 
    	if (!SSL_set_ssl_method(ssl, SSLv23_server_method()))  | 
    
    
    95  | 
     | 
     | 
    		goto err;  | 
    
    
    96  | 
     | 
     | 
     | 
    
    
    97  | 
     | 
     | 
    	return (void *)(ssl);  | 
    
    
    98  | 
     | 
     | 
     | 
    
    
    99  | 
     | 
     | 
    err:  | 
    
    
    100  | 
     | 
     | 
    	SSL_free(ssl);  | 
    
    
    101  | 
     | 
     | 
    	ssl_error("ssl_smtp_init"); | 
    
    
    102  | 
     | 
     | 
    	return (NULL);  | 
    
    
    103  | 
     | 
     | 
    }  |