Mercurial > emacs
annotate src/gnutls.c @ 110651:a163b2dbf867
nt/configure.bat: Fix condition for copying paths.h into ../src/epaths.h.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Wed, 29 Sep 2010 15:58:11 +0200 |
parents | 2f0d755fa21b |
children | 056ce44cedcd |
rev | line source |
---|---|
110584 | 1 /* GnuTLS glue for GNU Emacs. |
2 Copyright (C) 2010 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software: you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation, either version 3 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | |
19 #include <config.h> | |
20 #include <errno.h> | |
21 #include <setjmp.h> | |
22 | |
23 #include "lisp.h" | |
24 #include "process.h" | |
25 | |
26 #ifdef HAVE_GNUTLS | |
27 #include <gnutls/gnutls.h> | |
28 | |
29 Lisp_Object Qgnutls_code; | |
30 Lisp_Object Qgnutls_anon, Qgnutls_x509pki; | |
31 Lisp_Object Qgnutls_e_interrupted, Qgnutls_e_again, | |
32 Qgnutls_e_invalid_session, Qgnutls_e_not_ready_for_handshake; | |
33 int global_initialized; | |
34 | |
110649
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
35 void |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
36 emacs_gnutls_handshake (struct Lisp_Process *proc) |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
37 { |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
38 gnutls_session_t state = proc->gnutls_state; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
39 int ret; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
40 |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
41 if (proc->gnutls_initstage < GNUTLS_STAGE_HANDSHAKE_CANDO) |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
42 return; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
43 |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
44 if (proc->gnutls_initstage < GNUTLS_STAGE_TRANSPORT_POINTERS_SET) |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
45 { |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
46 /* FIXME: This can't be right: infd and outfd are integers (file handles) |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
47 whereas the function expects args of type gnutls_transport_ptr_t. */ |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
48 gnutls_transport_set_ptr2 (state, proc->infd, proc->outfd); |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
49 |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
50 proc->gnutls_initstage = GNUTLS_STAGE_TRANSPORT_POINTERS_SET; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
51 } |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
52 |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
53 ret = gnutls_handshake (state); |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
54 proc->gnutls_initstage = GNUTLS_STAGE_HANDSHAKE_TRIED; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
55 |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
56 if (ret == GNUTLS_E_SUCCESS) |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
57 { |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
58 /* here we're finally done. */ |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
59 proc->gnutls_initstage = GNUTLS_STAGE_READY; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
60 } |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
61 } |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
62 |
110584 | 63 int |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
64 emacs_gnutls_write (int fildes, struct Lisp_Process *proc, char *buf, |
110584 | 65 unsigned int nbyte) |
66 { | |
67 register int rtnval, bytes_written; | |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
68 gnutls_session_t state = proc->gnutls_state; |
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
69 |
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
70 if (proc->gnutls_initstage != GNUTLS_STAGE_READY) |
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
71 return 0; |
110584 | 72 |
73 bytes_written = 0; | |
74 | |
75 while (nbyte > 0) | |
76 { | |
77 rtnval = gnutls_write (state, buf, nbyte); | |
78 | |
79 if (rtnval == -1) | |
80 { | |
81 if (errno == EINTR) | |
82 continue; | |
83 else | |
84 return (bytes_written ? bytes_written : -1); | |
85 } | |
86 | |
87 buf += rtnval; | |
88 nbyte -= rtnval; | |
89 bytes_written += rtnval; | |
90 } | |
91 fsync (STDOUT_FILENO); | |
92 | |
93 return (bytes_written); | |
94 } | |
95 | |
96 int | |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
97 emacs_gnutls_read (int fildes, struct Lisp_Process *proc, char *buf, |
110584 | 98 unsigned int nbyte) |
99 { | |
100 register int rtnval; | |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
101 gnutls_session_t state = proc->gnutls_state; |
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
102 |
110649
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
103 if (proc->gnutls_initstage != GNUTLS_STAGE_READY) { |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
104 emacs_gnutls_handshake (proc); |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
105 return -1; |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
106 } |
110584 | 107 |
110608
06497cf3e920
(emacs_gnutls_read): Don't infloop while reading.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110606
diff
changeset
|
108 rtnval = gnutls_read (state, buf, nbyte); |
06497cf3e920
(emacs_gnutls_read): Don't infloop while reading.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110606
diff
changeset
|
109 if (rtnval >= 0) |
06497cf3e920
(emacs_gnutls_read): Don't infloop while reading.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110606
diff
changeset
|
110 return rtnval; |
06497cf3e920
(emacs_gnutls_read): Don't infloop while reading.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110606
diff
changeset
|
111 else |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
112 return 0; |
110584 | 113 } |
114 | |
115 /* convert an integer error to a Lisp_Object; it will be either a | |
116 known symbol like `gnutls_e_interrupted' and `gnutls_e_again' or | |
117 simply the integer value of the error. GNUTLS_E_SUCCESS is mapped | |
118 to Qt. */ | |
119 Lisp_Object gnutls_make_error (int error) | |
120 { | |
121 switch (error) | |
122 { | |
123 case GNUTLS_E_SUCCESS: | |
124 return Qt; | |
125 case GNUTLS_E_AGAIN: | |
126 return Qgnutls_e_again; | |
127 case GNUTLS_E_INTERRUPTED: | |
128 return Qgnutls_e_interrupted; | |
129 case GNUTLS_E_INVALID_SESSION: | |
130 return Qgnutls_e_invalid_session; | |
131 } | |
132 | |
133 return make_number (error); | |
134 } | |
135 | |
136 DEFUN ("gnutls-get-initstage", Fgnutls_get_initstage, Sgnutls_get_initstage, 1, 1, 0, | |
137 doc: /* Return the GnuTLS init stage of PROCESS. | |
138 See also `gnutls-boot'. */) | |
139 (Lisp_Object proc) | |
140 { | |
141 CHECK_PROCESS (proc); | |
142 | |
143 return make_number (GNUTLS_INITSTAGE (proc)); | |
144 } | |
145 | |
146 DEFUN ("gnutls-errorp", Fgnutls_errorp, Sgnutls_errorp, 1, 1, 0, | |
147 doc: /* Returns t if ERROR (as generated by gnutls_make_error) | |
148 indicates a GnuTLS problem. */) | |
149 (Lisp_Object error) | |
150 { | |
151 if (EQ (error, Qt)) return Qnil; | |
152 | |
153 return Qt; | |
154 } | |
155 | |
156 DEFUN ("gnutls-error-fatalp", Fgnutls_error_fatalp, Sgnutls_error_fatalp, 1, 1, 0, | |
157 doc: /* Checks if ERROR is fatal. | |
158 ERROR is an integer or a symbol with an integer `gnutls-code' property. */) | |
159 (Lisp_Object err) | |
160 { | |
161 Lisp_Object code; | |
162 | |
163 if (EQ (err, Qt)) return Qnil; | |
164 | |
165 if (SYMBOLP (err)) | |
166 { | |
167 code = Fget (err, Qgnutls_code); | |
168 if (NUMBERP (code)) | |
169 { | |
170 err = code; | |
171 } | |
172 else | |
173 { | |
174 error ("Symbol has no numeric gnutls-code property"); | |
175 } | |
176 } | |
177 | |
178 if (!NUMBERP (err)) | |
179 error ("Not an error symbol or code"); | |
180 | |
181 if (0 == gnutls_error_is_fatal (XINT (err))) | |
182 return Qnil; | |
183 | |
184 return Qt; | |
185 } | |
186 | |
187 DEFUN ("gnutls-error-string", Fgnutls_error_string, Sgnutls_error_string, 1, 1, 0, | |
188 doc: /* Returns a description of ERROR. | |
189 ERROR is an integer or a symbol with an integer `gnutls-code' property. */) | |
190 (Lisp_Object err) | |
191 { | |
192 Lisp_Object code; | |
193 | |
194 if (EQ (err, Qt)) return build_string ("Not an error"); | |
195 | |
196 if (SYMBOLP (err)) | |
197 { | |
198 code = Fget (err, Qgnutls_code); | |
199 if (NUMBERP (code)) | |
200 { | |
201 err = code; | |
202 } | |
203 else | |
204 { | |
205 return build_string ("Symbol has no numeric gnutls-code property"); | |
206 } | |
207 } | |
208 | |
209 if (!NUMBERP (err)) | |
210 return build_string ("Not an error symbol or code"); | |
211 | |
212 return build_string (gnutls_strerror (XINT (err))); | |
213 } | |
214 | |
215 DEFUN ("gnutls-deinit", Fgnutls_deinit, Sgnutls_deinit, 1, 1, 0, | |
216 doc: /* Deallocate GNU TLS resources associated with PROCESS. | |
217 See also `gnutls-init'. */) | |
218 (Lisp_Object proc) | |
219 { | |
220 gnutls_session_t state; | |
221 | |
222 CHECK_PROCESS (proc); | |
223 state = XPROCESS (proc)->gnutls_state; | |
224 | |
225 if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT) | |
226 { | |
227 gnutls_deinit (state); | |
228 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT - 1; | |
229 } | |
230 | |
231 return Qt; | |
232 } | |
233 | |
234 /* Initializes global GNU TLS state to defaults. | |
235 Call `gnutls-global-deinit' when GNU TLS usage is no longer needed. | |
236 Returns zero on success. */ | |
237 Lisp_Object gnutls_emacs_global_init (void) | |
238 { | |
239 int ret = GNUTLS_E_SUCCESS; | |
240 | |
241 if (!global_initialized) | |
242 ret = gnutls_global_init (); | |
243 | |
244 global_initialized = 1; | |
245 | |
246 return gnutls_make_error (ret); | |
247 } | |
248 | |
249 /* Deinitializes global GNU TLS state. | |
250 See also `gnutls-global-init'. */ | |
251 Lisp_Object gnutls_emacs_global_deinit (void) | |
252 { | |
253 if (global_initialized) | |
254 gnutls_global_deinit (); | |
255 | |
256 global_initialized = 0; | |
257 | |
258 return gnutls_make_error (GNUTLS_E_SUCCESS); | |
259 } | |
260 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
261 static void gnutls_log_function (int level, const char* string) |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
262 { |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
263 message("gnutls.c: [%d] %s", level, string); |
110606
b4f4c3e9b976
Add debugging to the gnutls library, and finish handshaking when it's done.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110584
diff
changeset
|
264 } |
b4f4c3e9b976
Add debugging to the gnutls library, and finish handshaking when it's done.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110584
diff
changeset
|
265 |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
266 DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 7, 0, |
110584 | 267 doc: /* Initializes client-mode GnuTLS for process PROC. |
268 Currently only client mode is supported. Returns a success/failure | |
269 value you can check with `gnutls-errorp'. | |
270 | |
271 PRIORITY_STRING is a string describing the priority. | |
272 TYPE is either `gnutls-anon' or `gnutls-x509pki'. | |
273 TRUSTFILE is a PEM encoded trust file for `gnutls-x509pki'. | |
274 KEYFILE is ... for `gnutls-x509pki' (TODO). | |
275 CALLBACK is ... for `gnutls-x509pki' (TODO). | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
276 LOGLEVEL is the debug level requested from GnuTLS, try 4. |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
277 |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
278 LOGLEVEL will be set for this process AND globally for GnuTLS. So if |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
279 you set it higher or lower at any point, it affects global debugging. |
110584 | 280 |
281 Note that the priority is set on the client. The server does not use | |
282 the protocols's priority except for disabling protocols that were not | |
283 specified. | |
284 | |
285 Processes must be initialized with this function before other GNU TLS | |
286 functions are used. This function allocates resources which can only | |
287 be deallocated by calling `gnutls-deinit' or by calling it again. | |
288 | |
289 Each authentication type may need additional information in order to | |
290 work. For X.509 PKI (`gnutls-x509pki'), you need TRUSTFILE and | |
291 KEYFILE and optionally CALLBACK. */) | |
292 (Lisp_Object proc, Lisp_Object priority_string, Lisp_Object type, | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
293 Lisp_Object trustfile, Lisp_Object keyfile, Lisp_Object callback, |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
294 Lisp_Object loglevel) |
110584 | 295 { |
296 int ret = GNUTLS_E_SUCCESS; | |
297 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
298 int max_log_level = 0; |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
299 |
110584 | 300 /* TODO: GNUTLS_X509_FMT_DER is also an option. */ |
301 int file_format = GNUTLS_X509_FMT_PEM; | |
302 | |
303 gnutls_session_t state; | |
304 gnutls_certificate_credentials_t x509_cred; | |
305 gnutls_anon_client_credentials_t anon_cred; | |
306 Lisp_Object global_init; | |
307 | |
308 CHECK_PROCESS (proc); | |
309 CHECK_SYMBOL (type); | |
310 CHECK_STRING (priority_string); | |
311 | |
312 state = XPROCESS (proc)->gnutls_state; | |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
313 XPROCESS (proc)->gnutls_p = 1; |
110584 | 314 |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
315 if (NUMBERP (loglevel)) |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
316 { |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
317 message ("setting up log level %d", XINT (loglevel)); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
318 gnutls_global_set_log_function (gnutls_log_function); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
319 gnutls_global_set_log_level (XINT (loglevel)); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
320 max_log_level = XINT (loglevel); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
321 XPROCESS (proc)->gnutls_log_level = max_log_level; |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
322 } |
110648
256dd50b2a63
Make sure all reads/writes to gnutls streams go via the gnutls functions.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110636
diff
changeset
|
323 |
110584 | 324 /* always initialize globals. */ |
325 global_init = gnutls_emacs_global_init (); | |
326 if (! NILP (Fgnutls_errorp (global_init))) | |
327 return global_init; | |
328 | |
329 /* deinit and free resources. */ | |
330 if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_CRED_ALLOC) | |
331 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
332 GNUTLS_LOG (1, max_log_level, "deallocating credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
333 |
110584 | 334 if (EQ (type, Qgnutls_x509pki)) |
335 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
336 GNUTLS_LOG (2, max_log_level, "deallocating x509 credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
337 x509_cred = XPROCESS (proc)->gnutls_x509_cred; |
110584 | 338 gnutls_certificate_free_credentials (x509_cred); |
339 } | |
340 else if (EQ (type, Qgnutls_anon)) | |
341 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
342 GNUTLS_LOG (2, max_log_level, "deallocating anon credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
343 anon_cred = XPROCESS (proc)->gnutls_anon_cred; |
110584 | 344 gnutls_anon_free_client_credentials (anon_cred); |
345 } | |
346 else | |
347 { | |
348 error ("unknown credential type"); | |
349 ret = GNUTLS_EMACS_ERROR_INVALID_TYPE; | |
350 } | |
351 | |
352 if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT) | |
353 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
354 GNUTLS_LOG (1, max_log_level, "deallocating x509 credentials"); |
110584 | 355 Fgnutls_deinit (proc); |
356 } | |
357 } | |
358 | |
359 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_EMPTY; | |
360 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
361 GNUTLS_LOG (1, max_log_level, "allocating credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
362 |
110584 | 363 if (EQ (type, Qgnutls_x509pki)) |
364 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
365 GNUTLS_LOG (2, max_log_level, "allocating x509 credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
366 x509_cred = XPROCESS (proc)->gnutls_x509_cred; |
110584 | 367 if (gnutls_certificate_allocate_credentials (&x509_cred) < 0) |
368 memory_full (); | |
369 } | |
370 else if (EQ (type, Qgnutls_anon)) | |
371 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
372 GNUTLS_LOG (2, max_log_level, "allocating anon credentials"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
373 anon_cred = XPROCESS (proc)->gnutls_anon_cred; |
110584 | 374 if (gnutls_anon_allocate_client_credentials (&anon_cred) < 0) |
375 memory_full (); | |
376 } | |
377 else | |
378 { | |
379 error ("unknown credential type"); | |
380 ret = GNUTLS_EMACS_ERROR_INVALID_TYPE; | |
381 } | |
382 | |
383 if (ret < GNUTLS_E_SUCCESS) | |
384 return gnutls_make_error (ret); | |
385 | |
386 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_CRED_ALLOC; | |
387 | |
388 if (EQ (type, Qgnutls_x509pki)) | |
389 { | |
390 if (STRINGP (trustfile)) | |
391 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
392 GNUTLS_LOG (1, max_log_level, "setting the trustfile"); |
110584 | 393 ret = gnutls_certificate_set_x509_trust_file |
394 (x509_cred, | |
110636
31e098898561
* src/gnutls.c (Fgnutls_boot): Remove unused vars `data' and `srp_cred'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110611
diff
changeset
|
395 SDATA (trustfile), |
110584 | 396 file_format); |
397 | |
398 if (ret < GNUTLS_E_SUCCESS) | |
399 return gnutls_make_error (ret); | |
400 } | |
401 | |
402 if (STRINGP (keyfile)) | |
403 { | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
404 GNUTLS_LOG (1, max_log_level, "setting the keyfile"); |
110584 | 405 ret = gnutls_certificate_set_x509_crl_file |
406 (x509_cred, | |
110636
31e098898561
* src/gnutls.c (Fgnutls_boot): Remove unused vars `data' and `srp_cred'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110611
diff
changeset
|
407 SDATA (keyfile), |
110584 | 408 file_format); |
409 | |
410 if (ret < GNUTLS_E_SUCCESS) | |
411 return gnutls_make_error (ret); | |
412 } | |
413 } | |
414 | |
415 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_FILES; | |
416 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
417 GNUTLS_LOG (1, max_log_level, "gnutls_init"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
418 |
110584 | 419 ret = gnutls_init (&state, GNUTLS_CLIENT); |
420 | |
421 if (ret < GNUTLS_E_SUCCESS) | |
422 return gnutls_make_error (ret); | |
423 | |
424 XPROCESS (proc)->gnutls_state = state; | |
425 | |
426 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT; | |
427 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
428 GNUTLS_LOG (1, max_log_level, "setting the priority string"); |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
429 |
110584 | 430 ret = gnutls_priority_set_direct(state, |
431 (char*) SDATA (priority_string), | |
432 NULL); | |
433 | |
434 if (ret < GNUTLS_E_SUCCESS) | |
435 return gnutls_make_error (ret); | |
436 | |
437 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_PRIORITY; | |
438 | |
439 message ("gnutls: setting the credentials"); | |
440 | |
441 if (EQ (type, Qgnutls_x509pki)) | |
442 { | |
443 message ("gnutls: setting the x509 credentials"); | |
444 | |
445 ret = gnutls_cred_set (state, GNUTLS_CRD_CERTIFICATE, x509_cred); | |
446 } | |
447 else if (EQ (type, Qgnutls_anon)) | |
448 { | |
449 message ("gnutls: setting the anon credentials"); | |
450 | |
451 ret = gnutls_cred_set (state, GNUTLS_CRD_ANON, anon_cred); | |
452 } | |
453 else | |
454 { | |
455 error ("unknown credential type"); | |
456 ret = GNUTLS_EMACS_ERROR_INVALID_TYPE; | |
457 } | |
458 | |
459 if (ret < GNUTLS_E_SUCCESS) | |
460 return gnutls_make_error (ret); | |
461 | |
110611
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
462 XPROCESS (proc)->gnutls_anon_cred = anon_cred; |
6c735824d0c1
Add gnutls logging and clean up various gnutls bits.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110608
diff
changeset
|
463 XPROCESS (proc)->gnutls_x509_cred = x509_cred; |
110584 | 464 XPROCESS (proc)->gnutls_cred_type = type; |
465 | |
466 GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_CRED_SET; | |
467 | |
110649
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
468 emacs_gnutls_handshake (XPROCESS (proc)); |
2f0d755fa21b
Do the gnutls handshake from the reader loop, instead of looping over
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
110648
diff
changeset
|
469 |
110584 | 470 return gnutls_make_error (GNUTLS_E_SUCCESS); |
471 } | |
472 | |
473 DEFUN ("gnutls-bye", Fgnutls_bye, | |
474 Sgnutls_bye, 2, 2, 0, | |
475 doc: /* Terminate current GNU TLS connection for PROCESS. | |
476 The connection should have been initiated using `gnutls-handshake'. | |
477 | |
478 If CONT is not nil the TLS connection gets terminated and further | |
479 receives and sends will be disallowed. If the return value is zero you | |
480 may continue using the connection. If CONT is nil, GnuTLS actually | |
481 sends an alert containing a close request and waits for the peer to | |
482 reply with the same message. In order to reuse the connection you | |
483 should wait for an EOF from the peer. | |
484 | |
485 This function may also return `gnutls-e-again', or | |
486 `gnutls-e-interrupted'. */) | |
487 (Lisp_Object proc, Lisp_Object cont) | |
488 { | |
489 gnutls_session_t state; | |
490 int ret; | |
491 | |
492 CHECK_PROCESS (proc); | |
493 | |
494 state = XPROCESS (proc)->gnutls_state; | |
495 | |
496 ret = gnutls_bye (state, | |
497 NILP (cont) ? GNUTLS_SHUT_RDWR : GNUTLS_SHUT_WR); | |
498 | |
499 return gnutls_make_error (ret); | |
500 } | |
501 | |
502 void | |
503 syms_of_gnutls (void) | |
504 { | |
505 global_initialized = 0; | |
506 | |
507 Qgnutls_code = intern_c_string ("gnutls-code"); | |
508 staticpro (&Qgnutls_code); | |
509 | |
510 Qgnutls_anon = intern_c_string ("gnutls-anon"); | |
511 staticpro (&Qgnutls_anon); | |
512 | |
513 Qgnutls_x509pki = intern_c_string ("gnutls-x509pki"); | |
514 staticpro (&Qgnutls_x509pki); | |
515 | |
516 Qgnutls_e_interrupted = intern_c_string ("gnutls-e-interrupted"); | |
517 staticpro (&Qgnutls_e_interrupted); | |
518 Fput (Qgnutls_e_interrupted, Qgnutls_code, | |
519 make_number (GNUTLS_E_INTERRUPTED)); | |
520 | |
521 Qgnutls_e_again = intern_c_string ("gnutls-e-again"); | |
522 staticpro (&Qgnutls_e_again); | |
523 Fput (Qgnutls_e_again, Qgnutls_code, | |
524 make_number (GNUTLS_E_AGAIN)); | |
525 | |
526 Qgnutls_e_invalid_session = intern_c_string ("gnutls-e-invalid-session"); | |
527 staticpro (&Qgnutls_e_invalid_session); | |
528 Fput (Qgnutls_e_invalid_session, Qgnutls_code, | |
529 make_number (GNUTLS_E_INVALID_SESSION)); | |
530 | |
531 Qgnutls_e_not_ready_for_handshake = | |
532 intern_c_string ("gnutls-e-not-ready-for-handshake"); | |
533 staticpro (&Qgnutls_e_not_ready_for_handshake); | |
534 Fput (Qgnutls_e_not_ready_for_handshake, Qgnutls_code, | |
535 make_number (GNUTLS_E_APPLICATION_ERROR_MIN)); | |
536 | |
537 defsubr (&Sgnutls_get_initstage); | |
538 defsubr (&Sgnutls_errorp); | |
539 defsubr (&Sgnutls_error_fatalp); | |
540 defsubr (&Sgnutls_error_string); | |
541 defsubr (&Sgnutls_boot); | |
542 defsubr (&Sgnutls_deinit); | |
543 defsubr (&Sgnutls_bye); | |
544 } | |
545 #endif |