s3:tls: Remove #ifdef for GnuTLS
[samba.git] / source4 / lib / tls / tlscert.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    auto-generate self signed TLS certificates
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/tls/tls.h"
24
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
27
28 #define ORGANISATION_NAME "Samba Administration"
29 #define CA_NAME           "Samba - temporary autogenerated CA certificate"
30 #define UNIT_NAME         "Samba - temporary autogenerated HOST certificate"
31 #define LIFETIME          700*24*60*60
32 #define RSA_BITS          4096
33
34 /* 
35    auto-generate a set of self signed certificates
36 */
37 void tls_cert_generate(TALLOC_CTX *mem_ctx, 
38                        const char *hostname, 
39                        const char *keyfile, const char *certfile,
40                        const char *cafile)
41 {
42         gnutls_x509_crt cacrt, crt;
43         gnutls_x509_privkey key, cakey;
44         uint32_t serial = (uint32_t)time(NULL);
45         unsigned char keyid[100];
46         char buf[4096];
47         size_t bufsize;
48         size_t keyidsize = sizeof(keyid);
49         time_t activation = time(NULL), expiry = activation + LIFETIME;
50         int ret;
51
52         if (file_exist(keyfile) || file_exist(certfile) || file_exist(cafile)) {
53                 DEBUG(0,("TLS autogeneration skipped - some TLS files already exist\n"));
54                 return;
55         }
56
57 #define TLSCHECK(call) do { \
58         ret = call; \
59         if (ret < 0) { \
60                 DEBUG(0,("TLS %s - %s\n", #call, gnutls_strerror(ret))); \
61                 goto failed; \
62         } \
63 } while (0)
64
65         TLSCHECK(gnutls_global_init());
66
67         DEBUG(0,("Attempting to autogenerate TLS self-signed keys for https for hostname '%s'\n", 
68                  hostname));
69
70         DEBUG(3,("Generating private key\n"));
71         TLSCHECK(gnutls_x509_privkey_init(&key));
72         TLSCHECK(gnutls_x509_privkey_generate(key,   GNUTLS_PK_RSA, RSA_BITS, 0));
73
74         DEBUG(3,("Generating CA private key\n"));
75         TLSCHECK(gnutls_x509_privkey_init(&cakey));
76         TLSCHECK(gnutls_x509_privkey_generate(cakey, GNUTLS_PK_RSA, RSA_BITS, 0));
77
78         DEBUG(3,("Generating CA certificate\n"));
79         TLSCHECK(gnutls_x509_crt_init(&cacrt));
80         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt, 
81                                       GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
82                                       ORGANISATION_NAME, strlen(ORGANISATION_NAME)));
83         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt, 
84                                       GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, 0,
85                                       CA_NAME, strlen(CA_NAME)));
86         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt,
87                                       GNUTLS_OID_X520_COMMON_NAME, 0,
88                                       hostname, strlen(hostname)));
89         TLSCHECK(gnutls_x509_crt_set_key(cacrt, cakey));
90         TLSCHECK(gnutls_x509_crt_set_serial(cacrt, &serial, sizeof(serial)));
91         TLSCHECK(gnutls_x509_crt_set_activation_time(cacrt, activation));
92         TLSCHECK(gnutls_x509_crt_set_expiration_time(cacrt, expiry));
93         TLSCHECK(gnutls_x509_crt_set_ca_status(cacrt, 1));
94         TLSCHECK(gnutls_x509_crt_set_key_usage(cacrt, GNUTLS_KEY_KEY_CERT_SIGN | GNUTLS_KEY_CRL_SIGN));
95         TLSCHECK(gnutls_x509_crt_set_version(cacrt, 3));
96         TLSCHECK(gnutls_x509_crt_get_key_id(cacrt, 0, keyid, &keyidsize));
97         TLSCHECK(gnutls_x509_crt_set_subject_key_id(cacrt, keyid, keyidsize));
98         TLSCHECK(gnutls_x509_crt_sign2(cacrt, cacrt, cakey,
99                                        GNUTLS_DIG_SHA256, 0));
100
101         DEBUG(3,("Generating TLS certificate\n"));
102         TLSCHECK(gnutls_x509_crt_init(&crt));
103         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt, 
104                                       GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
105                                       ORGANISATION_NAME, strlen(ORGANISATION_NAME)));
106         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt, 
107                                       GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, 0,
108                                       UNIT_NAME, strlen(UNIT_NAME)));
109         TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt,
110                                       GNUTLS_OID_X520_COMMON_NAME, 0,
111                                       hostname, strlen(hostname)));
112         TLSCHECK(gnutls_x509_crt_set_key(crt, key));
113         TLSCHECK(gnutls_x509_crt_set_serial(crt, &serial, sizeof(serial)));
114         TLSCHECK(gnutls_x509_crt_set_activation_time(crt, activation));
115         TLSCHECK(gnutls_x509_crt_set_expiration_time(crt, expiry));
116         TLSCHECK(gnutls_x509_crt_set_ca_status(crt, 0));
117         TLSCHECK(gnutls_x509_crt_set_key_purpose_oid(crt, GNUTLS_KP_TLS_WWW_SERVER, 0));
118         TLSCHECK(gnutls_x509_crt_set_version(crt, 3));
119         TLSCHECK(gnutls_x509_crt_get_key_id(crt, 0, keyid, &keyidsize));
120         TLSCHECK(gnutls_x509_crt_set_subject_key_id(crt, keyid, keyidsize));
121         TLSCHECK(gnutls_x509_crt_sign2(crt, crt, key,
122                                        GNUTLS_DIG_SHA256, 0));
123         TLSCHECK(gnutls_x509_crt_sign2(crt, cacrt, cakey,
124                                        GNUTLS_DIG_SHA256, 0));
125
126         DEBUG(3,("Exporting TLS keys\n"));
127
128         bufsize = sizeof(buf);
129         TLSCHECK(gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, buf, &bufsize));
130         if (!file_save(certfile, buf, bufsize)) {
131                 DEBUG(0,("Unable to save certificate in %s parent dir exists ?\n", certfile));
132                 goto failed;
133         }
134
135         bufsize = sizeof(buf);
136         TLSCHECK(gnutls_x509_crt_export(cacrt, GNUTLS_X509_FMT_PEM, buf, &bufsize));
137         if (!file_save(cafile, buf, bufsize)) {
138                 DEBUG(0,("Unable to save ca cert in %s parent dir exists ?\n", cafile));
139                 goto failed;
140         }
141
142         bufsize = sizeof(buf);
143         TLSCHECK(gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, buf, &bufsize));
144         if (!file_save_mode(keyfile, buf, bufsize, 0600)) {
145                 DEBUG(0,("Unable to save privatekey in %s parent dir exists ?\n", keyfile));
146                 goto failed;
147         }
148
149         gnutls_x509_privkey_deinit(key);
150         gnutls_x509_privkey_deinit(cakey);
151         gnutls_x509_crt_deinit(cacrt);
152         gnutls_x509_crt_deinit(crt);
153         gnutls_global_deinit();
154
155         DEBUG(0,("TLS self-signed keys generated OK\n"));
156         return;
157
158 failed:
159         DEBUG(0,("TLS certificate generation failed\n"));
160 }