Imported Upstream version 2.7
[ossec-hids.git] / src / os_auth / ssl.c
1 /* @(#) $Id: ./src/os_auth/ssl.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2010 Trend Micro Inc.
5  * All rights reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  *
12  * In addition, as a special exception, the copyright holders give
13  * permission to link the code of portions of this program with the
14  * OpenSSL library under certain conditions as described in each
15  * individual source file, and distribute linked combinations
16  * including the two.
17  *
18  * You must obey the GNU General Public License in all respects
19  * for all of the code used other than OpenSSL.  If you modify
20  * file(s) with this exception, you may extend this exception to your
21  * version of the file(s), but you are not obligated to do so.  If you
22  * do not wish to do so, delete this exception statement from your
23  * version.  If you delete this exception statement from all source
24  * files in the program, then also delete it here.
25  *
26  */
27
28
29 #ifdef USE_OPENSSL
30
31 #include "shared.h"
32 #include "auth.h"
33
34
35 void *os_ssl_keys(int isclient, char *dir)
36 {
37     SSL_METHOD *sslmeth;
38     SSL_CTX *ctx;
39     char certf[1024 +1];
40     char keyf[1024 +1];
41
42     SSL_library_init();
43     SSL_load_error_strings();
44     OpenSSL_add_all_algorithms();
45     bio_err = BIO_new_fp(stderr,BIO_NOCLOSE);
46
47
48     /* Create our context */
49     sslmeth = (SSL_METHOD *)SSLv23_method();
50     ctx = SSL_CTX_new(sslmeth);
51
52     if(isclient)
53     {
54         debug1("%s: DEBUG: Returning CTX for client.", ARGV0);
55         return(ctx);
56     }
57
58     if(!dir)
59     {
60         return(NULL);
61     }
62
63
64     /* Setting final cert/key files */
65     certf[1024] = '\0';
66     keyf[1024] = '\0';
67     snprintf(certf, 1023, "%s%s", dir, CERTFILE);
68     snprintf(keyf, 1023, "%s%s", dir, KEYFILE);
69
70
71     if(File_DateofChange(certf) <= 0)
72     {
73         merror("%s: ERROR: Unable to read certificate file (not found): %s", ARGV0, certf);
74         return(NULL);
75     }
76
77     /* Load our keys and certificates*/
78     if(!(SSL_CTX_use_certificate_chain_file(ctx, certf)))
79     {
80         merror("%s: ERROR: Unable to read certificate file: %s", ARGV0, certf);
81         ERR_print_errors_fp(stderr);
82         return(NULL);
83     }
84
85     if(!(SSL_CTX_use_PrivateKey_file(ctx, keyf, SSL_FILETYPE_PEM)))
86     {
87         merror("%s: ERROR: Unable to read private key file: %s", ARGV0, keyf);
88         return(NULL);
89     }
90
91     if (!SSL_CTX_check_private_key(ctx))
92     {
93         merror("%s: ERROR: Unable to verify private key file", ARGV0);
94         return(NULL);
95     }
96
97
98     #if(OPENSSL_VERSION_NUMBER < 0x00905100L)
99     SSL_CTX_set_verify_depth(ctx,1);
100     #endif
101
102     return ctx;
103 }
104
105
106 #endif
107
108 /* EOF */