/src/gnutls/lib/tls13/certificate.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2017 Red Hat, Inc.  | 
3  |  |  *  | 
4  |  |  * Author: Nikos Mavrogiannopoulos  | 
5  |  |  *  | 
6  |  |  * This file is part of GnuTLS.  | 
7  |  |  *  | 
8  |  |  * The GnuTLS is free software; you can redistribute it and/or  | 
9  |  |  * modify it under the terms of the GNU Lesser General Public License  | 
10  |  |  * as published by the Free Software Foundation; either version 2.1 of  | 
11  |  |  * the License, or (at your option) any later version.  | 
12  |  |  *  | 
13  |  |  * This library is distributed in the hope that it will be useful, but  | 
14  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
15  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
16  |  |  * Lesser General Public License for more details.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
20  |  |  *  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #include "gnutls_int.h"  | 
24  |  | #include "compress.h"  | 
25  |  | #include "errors.h"  | 
26  |  | #include "extv.h"  | 
27  |  | #include "handshake.h"  | 
28  |  | #include "tls13/certificate.h"  | 
29  |  | #include "auth/cert.h"  | 
30  |  | #include "mbuffers.h"  | 
31  |  | #include "ext/compress_certificate.h"  | 
32  |  | #include "ext/status_request.h"  | 
33  |  |  | 
34  |  | static int parse_cert_extension(void *ctx, unsigned tls_id, const uint8_t *data,  | 
35  |  |         unsigned data_size);  | 
36  |  | static int parse_cert_list(gnutls_session_t session, uint8_t *data,  | 
37  |  |          size_t data_size);  | 
38  |  | static int compress_certificate(gnutls_buffer_st *buf, unsigned cert_pos_mark,  | 
39  |  |         gnutls_compression_method_t comp_method);  | 
40  |  | static int decompress_certificate(gnutls_session_t session,  | 
41  |  |           gnutls_buffer_st *buf);  | 
42  |  |  | 
43  |  | int _gnutls13_recv_certificate(gnutls_session_t session)  | 
44  | 0  | { | 
45  | 0  |   int ret, err, decompress_cert = 0;  | 
46  | 0  |   gnutls_buffer_st buf;  | 
47  | 0  |   unsigned optional = 0;  | 
48  |  | 
  | 
49  | 0  |   if (!session->internals.initial_negotiation_completed &&  | 
50  | 0  |       session->internals.hsk_flags & HSK_PSK_SELECTED)  | 
51  | 0  |     return 0;  | 
52  |  |  | 
53  | 0  |   if (session->security_parameters.entity == GNUTLS_SERVER) { | 
54  |  |     /* if we didn't request a certificate, there will not be any */  | 
55  | 0  |     if (session->internals.send_cert_req == 0)  | 
56  | 0  |       return 0;  | 
57  |  |  | 
58  | 0  |     if (session->internals.send_cert_req != GNUTLS_CERT_REQUIRE)  | 
59  | 0  |       optional = 1;  | 
60  | 0  |   }  | 
61  |  |  | 
62  | 0  |   ret = _gnutls_recv_handshake(session, GNUTLS_HANDSHAKE_CERTIFICATE_PKT,  | 
63  | 0  |              0, &buf);  | 
64  | 0  |   if (ret == GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET) { | 
65  |  |     /* check if we received compressed certificate */  | 
66  | 0  |     err = _gnutls_recv_handshake(  | 
67  | 0  |       session, GNUTLS_HANDSHAKE_COMPRESSED_CERTIFICATE_PKT, 0,  | 
68  | 0  |       &buf);  | 
69  | 0  |     if (err >= 0) { | 
70  |  |       /* fail if we receive unsolicited compressed certificate */  | 
71  | 0  |       if (!(session->internals.hsk_flags &  | 
72  | 0  |             HSK_COMP_CRT_REQ_SENT))  | 
73  | 0  |         return gnutls_assert_val(  | 
74  | 0  |           GNUTLS_E_UNEXPECTED_PACKET);  | 
75  |  |  | 
76  | 0  |       decompress_cert = 1;  | 
77  | 0  |       ret = err;  | 
78  | 0  |     }  | 
79  | 0  |   }  | 
80  | 0  |   if (ret < 0) { | 
81  | 0  |     if (ret == GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET &&  | 
82  | 0  |         session->internals.send_cert_req)  | 
83  | 0  |       return gnutls_assert_val(GNUTLS_E_NO_CERTIFICATE_FOUND);  | 
84  |  |  | 
85  | 0  |     return gnutls_assert_val(ret);  | 
86  | 0  |   }  | 
87  |  |  | 
88  | 0  |   if (buf.length == 0) { | 
89  | 0  |     gnutls_assert();  | 
90  | 0  |     ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;  | 
91  | 0  |     goto cleanup;  | 
92  | 0  |   }  | 
93  |  |  | 
94  | 0  |   if (decompress_cert) { | 
95  | 0  |     ret = decompress_certificate(session, &buf);  | 
96  | 0  |     if (ret < 0) { | 
97  | 0  |       gnutls_assert();  | 
98  | 0  |       goto cleanup;  | 
99  | 0  |     }  | 
100  | 0  |   }  | 
101  |  |  | 
102  | 0  |   if (session->internals.initial_negotiation_completed &&  | 
103  | 0  |       session->internals.post_handshake_cr_context.size > 0) { | 
104  | 0  |     gnutls_datum_t context;  | 
105  |  |  | 
106  |  |     /* verify whether the context matches */  | 
107  | 0  |     ret = _gnutls_buffer_pop_datum_prefix8(&buf, &context);  | 
108  | 0  |     if (ret < 0) { | 
109  | 0  |       gnutls_assert();  | 
110  | 0  |       goto cleanup;  | 
111  | 0  |     }  | 
112  |  |  | 
113  | 0  |     if (context.size !=  | 
114  | 0  |           session->internals.post_handshake_cr_context.size ||  | 
115  | 0  |         memcmp(context.data,  | 
116  | 0  |          session->internals.post_handshake_cr_context.data,  | 
117  | 0  |          context.size) != 0) { | 
118  | 0  |       ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;  | 
119  | 0  |       gnutls_assert();  | 
120  | 0  |       goto cleanup;  | 
121  | 0  |     }  | 
122  | 0  |   } else { | 
123  | 0  |     if (buf.data[0] != 0) { | 
124  |  |       /* The context field must be empty during handshake */  | 
125  | 0  |       gnutls_assert();  | 
126  | 0  |       ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;  | 
127  | 0  |       goto cleanup;  | 
128  | 0  |     }  | 
129  |  |  | 
130  |  |     /* buf.length is positive */  | 
131  | 0  |     buf.data++;  | 
132  | 0  |     buf.length--;  | 
133  | 0  |   }  | 
134  |  |  | 
135  | 0  |   _gnutls_handshake_log("HSK[%p]: parsing certificate message\n", | 
136  | 0  |             session);  | 
137  |  | 
  | 
138  | 0  |   ret = parse_cert_list(session, buf.data, buf.length);  | 
139  | 0  |   if (ret < 0) { | 
140  | 0  |     if (ret == GNUTLS_E_NO_CERTIFICATE_FOUND) { | 
141  | 0  |       if (optional)  | 
142  | 0  |         ret = 0;  | 
143  | 0  |       else if (session->security_parameters.entity ==  | 
144  | 0  |          GNUTLS_SERVER)  | 
145  | 0  |         ret = GNUTLS_E_CERTIFICATE_REQUIRED;  | 
146  | 0  |     }  | 
147  | 0  |     gnutls_assert();  | 
148  | 0  |     goto cleanup;  | 
149  | 0  |   }  | 
150  |  |  | 
151  | 0  |   session->internals.hsk_flags |= HSK_CRT_VRFY_EXPECTED;  | 
152  |  | 
  | 
153  | 0  |   ret = 0;  | 
154  | 0  | cleanup:  | 
155  |  | 
  | 
156  | 0  |   _gnutls_buffer_clear(&buf);  | 
157  | 0  |   return ret;  | 
158  | 0  | }  | 
159  |  |  | 
160  |  | struct ocsp_req_ctx_st { | 
161  |  |   gnutls_pcert_st *pcert;  | 
162  |  |   unsigned cert_index;  | 
163  |  |   gnutls_session_t session;  | 
164  |  |   gnutls_certificate_credentials_t cred;  | 
165  |  | };  | 
166  |  |  | 
167  |  | static int append_status_request(void *_ctx, gnutls_buffer_st *buf)  | 
168  | 0  | { | 
169  | 0  |   struct ocsp_req_ctx_st *ctx = _ctx;  | 
170  | 0  |   gnutls_session_t session = ctx->session;  | 
171  | 0  |   int ret;  | 
172  | 0  |   gnutls_datum_t resp;  | 
173  | 0  |   unsigned free_resp = 0;  | 
174  |  | 
  | 
175  | 0  |   assert(session->internals.selected_ocsp_func != NULL ||  | 
176  | 0  |          session->internals.selected_ocsp_length != 0);  | 
177  |  |  | 
178  |  |   /* The global ocsp callback function can only be used to return  | 
179  |  |    * a single certificate request */  | 
180  | 0  |   if (session->internals.selected_ocsp_length == 1 &&  | 
181  | 0  |       ctx->cert_index != 0)  | 
182  | 0  |     return 0;  | 
183  |  |  | 
184  | 0  |   if (session->internals.selected_ocsp_length > 0) { | 
185  | 0  |     if (ctx->cert_index < session->internals.selected_ocsp_length) { | 
186  | 0  |       if ((session->internals.selected_ocsp[ctx->cert_index]  | 
187  | 0  |                .exptime != 0 &&  | 
188  | 0  |            gnutls_time(0) >=  | 
189  | 0  |              session->internals  | 
190  | 0  |                .selected_ocsp[ctx->cert_index]  | 
191  | 0  |                .exptime) ||  | 
192  | 0  |           session->internals.selected_ocsp[ctx->cert_index]  | 
193  | 0  |               .response.data == NULL) { | 
194  | 0  |         return 0;  | 
195  | 0  |       }  | 
196  |  |  | 
197  | 0  |       resp.data = session->internals  | 
198  | 0  |               .selected_ocsp[ctx->cert_index]  | 
199  | 0  |               .response.data;  | 
200  | 0  |       resp.size = session->internals  | 
201  | 0  |               .selected_ocsp[ctx->cert_index]  | 
202  | 0  |               .response.size;  | 
203  | 0  |       ret = 0;  | 
204  | 0  |     } else { | 
205  | 0  |       return 0;  | 
206  | 0  |     }  | 
207  | 0  |   } else if (session->internals.selected_ocsp_func) { | 
208  | 0  |     if (ctx->cert_index == 0) { | 
209  | 0  |       ret = session->internals.selected_ocsp_func(  | 
210  | 0  |         session,  | 
211  | 0  |         session->internals.selected_ocsp_func_ptr,  | 
212  | 0  |         &resp);  | 
213  | 0  |       free_resp = 1;  | 
214  | 0  |     } else { | 
215  | 0  |       return 0;  | 
216  | 0  |     }  | 
217  | 0  |   } else  | 
218  | 0  |     return 0;  | 
219  |  |  | 
220  | 0  |   if (ret == GNUTLS_E_NO_CERTIFICATE_STATUS || resp.data == 0) { | 
221  | 0  |     return 0;  | 
222  | 0  |   } else if (ret < 0) { | 
223  | 0  |     return gnutls_assert_val(ret);  | 
224  | 0  |   }  | 
225  |  |  | 
226  | 0  |   ret = _gnutls_buffer_append_data(buf, "\x01", 1);  | 
227  | 0  |   if (ret < 0) { | 
228  | 0  |     gnutls_assert();  | 
229  | 0  |     goto cleanup;  | 
230  | 0  |   }  | 
231  |  |  | 
232  | 0  |   ret = _gnutls_buffer_append_data_prefix(buf, 24, resp.data, resp.size);  | 
233  | 0  |   if (ret < 0) { | 
234  | 0  |     gnutls_assert();  | 
235  | 0  |     goto cleanup;  | 
236  | 0  |   }  | 
237  |  |  | 
238  | 0  |   ret = 0;  | 
239  | 0  | cleanup:  | 
240  | 0  |   if (free_resp)  | 
241  | 0  |     gnutls_free(resp.data);  | 
242  | 0  |   return ret;  | 
243  | 0  | }  | 
244  |  |  | 
245  |  | int _gnutls13_send_certificate(gnutls_session_t session, unsigned again)  | 
246  | 0  | { | 
247  | 0  |   int ret, compress_cert;  | 
248  | 0  |   gnutls_pcert_st *apr_cert_list = NULL;  | 
249  | 0  |   gnutls_privkey_t apr_pkey = NULL;  | 
250  | 0  |   int apr_cert_list_length = 0;  | 
251  | 0  |   mbuffer_st *bufel = NULL;  | 
252  | 0  |   gnutls_buffer_st buf;  | 
253  | 0  |   unsigned pos_mark, ext_pos_mark, cert_pos_mark;  | 
254  | 0  |   unsigned i;  | 
255  | 0  |   struct ocsp_req_ctx_st ctx;  | 
256  | 0  |   gnutls_certificate_credentials_t cred;  | 
257  | 0  |   gnutls_compression_method_t comp_method;  | 
258  | 0  |   gnutls_handshake_description_t h_type;  | 
259  |  | 
  | 
260  | 0  |   comp_method = gnutls_compress_certificate_get_selected_method(session);  | 
261  | 0  |   compress_cert = comp_method != GNUTLS_COMP_UNKNOWN;  | 
262  | 0  |   h_type = compress_cert ? GNUTLS_HANDSHAKE_COMPRESSED_CERTIFICATE_PKT :  | 
263  | 0  |          GNUTLS_HANDSHAKE_CERTIFICATE_PKT;  | 
264  |  | 
  | 
265  | 0  |   if (again == 0) { | 
266  | 0  |     if (!session->internals.initial_negotiation_completed &&  | 
267  | 0  |         session->internals.hsk_flags & HSK_PSK_SELECTED)  | 
268  | 0  |       return 0;  | 
269  |  |  | 
270  | 0  |     if (session->security_parameters.entity == GNUTLS_SERVER &&  | 
271  | 0  |         session->internals.resumed)  | 
272  | 0  |       return 0;  | 
273  |  |  | 
274  | 0  |     cred = (gnutls_certificate_credentials_t)_gnutls_get_cred(  | 
275  | 0  |       session, GNUTLS_CRD_CERTIFICATE);  | 
276  | 0  |     if (cred == NULL) { | 
277  | 0  |       gnutls_assert();  | 
278  | 0  |       return GNUTLS_E_INSUFFICIENT_CREDENTIALS;  | 
279  | 0  |     }  | 
280  |  |  | 
281  | 0  |     if (session->security_parameters.entity == GNUTLS_CLIENT &&  | 
282  | 0  |         !(session->internals.hsk_flags & HSK_CRT_ASKED)) { | 
283  | 0  |       return 0;  | 
284  | 0  |     }  | 
285  |  |  | 
286  | 0  |     ret = _gnutls_get_selected_cert(session, &apr_cert_list,  | 
287  | 0  |             &apr_cert_list_length,  | 
288  | 0  |             &apr_pkey);  | 
289  | 0  |     if (ret < 0)  | 
290  | 0  |       return gnutls_assert_val(ret);  | 
291  |  |  | 
292  | 0  |     ret = _gnutls_buffer_init_handshake_mbuffer(&buf);  | 
293  | 0  |     if (ret < 0)  | 
294  | 0  |       return gnutls_assert_val(ret);  | 
295  |  |  | 
296  | 0  |     cert_pos_mark = buf.length;  | 
297  |  | 
  | 
298  | 0  |     if (session->security_parameters.entity == GNUTLS_CLIENT) { | 
299  | 0  |       ret = _gnutls_buffer_append_data_prefix(  | 
300  | 0  |         &buf, 8,  | 
301  | 0  |         session->internals.post_handshake_cr_context  | 
302  | 0  |           .data,  | 
303  | 0  |         session->internals.post_handshake_cr_context  | 
304  | 0  |           .size);  | 
305  | 0  |       if (ret < 0) { | 
306  | 0  |         gnutls_assert();  | 
307  | 0  |         goto cleanup;  | 
308  | 0  |       }  | 
309  |  | 
  | 
310  | 0  |     } else { | 
311  | 0  |       ret = _gnutls_buffer_append_prefix(&buf, 8, 0);  | 
312  | 0  |       if (ret < 0) { | 
313  | 0  |         gnutls_assert();  | 
314  | 0  |         goto cleanup;  | 
315  | 0  |       }  | 
316  | 0  |     }  | 
317  |  |  | 
318  |  |     /* mark total size */  | 
319  | 0  |     pos_mark = buf.length;  | 
320  | 0  |     ret = _gnutls_buffer_append_prefix(&buf, 24, 0);  | 
321  | 0  |     if (ret < 0) { | 
322  | 0  |       gnutls_assert();  | 
323  | 0  |       goto cleanup;  | 
324  | 0  |     }  | 
325  |  |  | 
326  | 0  |     for (i = 0; i < (unsigned)apr_cert_list_length; i++) { | 
327  | 0  |       ret = _gnutls_buffer_append_data_prefix(  | 
328  | 0  |         &buf, 24, apr_cert_list[i].cert.data,  | 
329  | 0  |         apr_cert_list[i].cert.size);  | 
330  | 0  |       if (ret < 0) { | 
331  | 0  |         gnutls_assert();  | 
332  | 0  |         goto cleanup;  | 
333  | 0  |       }  | 
334  | 0  | #ifdef ENABLE_OCSP  | 
335  | 0  |       if ((session->internals.selected_ocsp_length > 0 ||  | 
336  | 0  |            session->internals.selected_ocsp_func) &&  | 
337  | 0  |           (((session->internals.hsk_flags &  | 
338  | 0  |              HSK_OCSP_REQUESTED) &&  | 
339  | 0  |             IS_SERVER(session)) ||  | 
340  | 0  |            ((session->internals.hsk_flags &  | 
341  | 0  |              HSK_CLIENT_OCSP_REQUESTED) &&  | 
342  | 0  |             !IS_SERVER(session)))) { | 
343  |  |         /* append status response if available */  | 
344  | 0  |         ret = _gnutls_extv_append_init(&buf);  | 
345  | 0  |         if (ret < 0) { | 
346  | 0  |           gnutls_assert();  | 
347  | 0  |           goto cleanup;  | 
348  | 0  |         }  | 
349  | 0  |         ext_pos_mark = ret;  | 
350  |  | 
  | 
351  | 0  |         ctx.pcert = &apr_cert_list[i];  | 
352  | 0  |         ctx.cert_index = i;  | 
353  | 0  |         ctx.session = session;  | 
354  | 0  |         ctx.cred = cred;  | 
355  | 0  |         ret = _gnutls_extv_append(  | 
356  | 0  |           &buf, STATUS_REQUEST_TLS_ID, &ctx,  | 
357  | 0  |           append_status_request);  | 
358  | 0  |         if (ret < 0) { | 
359  | 0  |           gnutls_assert();  | 
360  | 0  |           goto cleanup;  | 
361  | 0  |         }  | 
362  |  |  | 
363  | 0  |         ret = _gnutls_extv_append_final(  | 
364  | 0  |           &buf, ext_pos_mark, 0);  | 
365  | 0  |         if (ret < 0) { | 
366  | 0  |           gnutls_assert();  | 
367  | 0  |           goto cleanup;  | 
368  | 0  |         }  | 
369  | 0  |       } else  | 
370  | 0  | #endif  | 
371  | 0  |       { | 
372  | 0  |         ret = _gnutls_buffer_append_prefix(&buf, 16, 0);  | 
373  | 0  |         if (ret < 0) { | 
374  | 0  |           gnutls_assert();  | 
375  | 0  |           goto cleanup;  | 
376  | 0  |         }  | 
377  | 0  |       }  | 
378  | 0  |     }  | 
379  |  |  | 
380  | 0  |     _gnutls_write_uint24(buf.length - pos_mark - 3,  | 
381  | 0  |              &buf.data[pos_mark]);  | 
382  |  | 
  | 
383  | 0  |     if (compress_cert) { | 
384  | 0  |       ret = compress_certificate(&buf, cert_pos_mark,  | 
385  | 0  |                comp_method);  | 
386  | 0  |       if (ret < 0) { | 
387  | 0  |         gnutls_assert();  | 
388  | 0  |         goto cleanup;  | 
389  | 0  |       }  | 
390  | 0  |     }  | 
391  |  |  | 
392  | 0  |     bufel = _gnutls_buffer_to_mbuffer(&buf);  | 
393  | 0  |   }  | 
394  |  |  | 
395  | 0  |   return _gnutls_send_handshake(session, bufel, h_type);  | 
396  |  |  | 
397  | 0  | cleanup:  | 
398  | 0  |   _gnutls_buffer_clear(&buf);  | 
399  | 0  |   return ret;  | 
400  | 0  | }  | 
401  |  |  | 
402  |  | typedef struct crt_cert_ctx_st { | 
403  |  |   gnutls_session_t session;  | 
404  |  |   gnutls_datum_t *ocsp;  | 
405  |  |   unsigned idx;  | 
406  |  | } crt_cert_ctx_st;  | 
407  |  |  | 
408  |  | static int parse_cert_extension(void *_ctx, unsigned tls_id,  | 
409  |  |         const uint8_t *data, unsigned data_size)  | 
410  | 0  | { | 
411  | 0  |   crt_cert_ctx_st *ctx = _ctx;  | 
412  | 0  |   gnutls_session_t session = ctx->session;  | 
413  | 0  |   int ret;  | 
414  |  | 
  | 
415  | 0  |   if (tls_id == STATUS_REQUEST_TLS_ID) { | 
416  | 0  | #ifdef ENABLE_OCSP  | 
417  | 0  |     if (!_gnutls_hello_ext_is_present(session,  | 
418  | 0  |               ext_mod_status_request.gid)) { | 
419  | 0  |       gnutls_assert();  | 
420  | 0  |       goto unexpected;  | 
421  | 0  |     }  | 
422  |  |  | 
423  | 0  |     _gnutls_handshake_log("Found OCSP response on cert %d\n", | 
424  | 0  |               ctx->idx);  | 
425  |  | 
  | 
426  | 0  |     ret = _gnutls_parse_ocsp_response(session, data, data_size,  | 
427  | 0  |               ctx->ocsp);  | 
428  | 0  |     if (ret < 0)  | 
429  | 0  |       return gnutls_assert_val(ret);  | 
430  | 0  | #endif  | 
431  | 0  |   } else { | 
432  | 0  |     goto unexpected;  | 
433  | 0  |   }  | 
434  |  |  | 
435  | 0  |   return 0;  | 
436  |  |  | 
437  | 0  | unexpected:  | 
438  | 0  |   _gnutls_debug_log("received unexpected certificate extension (%d)\n", | 
439  | 0  |         (int)tls_id);  | 
440  | 0  |   return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);  | 
441  | 0  | }  | 
442  |  |  | 
443  |  | static int parse_cert_list(gnutls_session_t session, uint8_t *data,  | 
444  |  |          size_t data_size)  | 
445  | 0  | { | 
446  | 0  |   int ret;  | 
447  | 0  |   size_t len;  | 
448  | 0  |   uint8_t *p = data;  | 
449  | 0  |   cert_auth_info_t info;  | 
450  | 0  |   gnutls_certificate_credentials_t cred;  | 
451  | 0  |   size_t size;  | 
452  | 0  |   int i;  | 
453  | 0  |   unsigned npeer_certs, npeer_ocsp, j;  | 
454  | 0  |   crt_cert_ctx_st ctx;  | 
455  | 0  |   gnutls_datum_t *peer_certs = NULL;  | 
456  | 0  |   gnutls_datum_t *peer_ocsp = NULL;  | 
457  | 0  |   unsigned nentries = 0;  | 
458  |  | 
  | 
459  | 0  |   cred = (gnutls_certificate_credentials_t)_gnutls_get_cred(  | 
460  | 0  |     session, GNUTLS_CRD_CERTIFICATE);  | 
461  | 0  |   if (cred == NULL) { | 
462  | 0  |     gnutls_assert();  | 
463  | 0  |     return GNUTLS_E_INSUFFICIENT_CREDENTIALS;  | 
464  | 0  |   }  | 
465  |  |  | 
466  | 0  |   if ((ret = _gnutls_auth_info_init(session, GNUTLS_CRD_CERTIFICATE,  | 
467  | 0  |             sizeof(cert_auth_info_st), 1)) < 0) { | 
468  | 0  |     gnutls_assert();  | 
469  | 0  |     return ret;  | 
470  | 0  |   }  | 
471  |  |  | 
472  | 0  |   if (data == NULL || data_size == 0) { | 
473  |  |     /* no certificate was sent */  | 
474  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
475  | 0  |   }  | 
476  |  |  | 
477  | 0  |   info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);  | 
478  | 0  |   if (info == NULL)  | 
479  | 0  |     return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);  | 
480  |  |  | 
481  | 0  |   DECR_LEN(data_size, 3);  | 
482  | 0  |   size = _gnutls_read_uint24(p);  | 
483  | 0  |   p += 3;  | 
484  |  | 
  | 
485  | 0  |   if (size != data_size)  | 
486  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
487  |  |  | 
488  | 0  |   if (size == 0)  | 
489  | 0  |     return gnutls_assert_val(GNUTLS_E_NO_CERTIFICATE_FOUND);  | 
490  |  |  | 
491  | 0  |   i = data_size;  | 
492  |  | 
  | 
493  | 0  |   while (i > 0) { | 
494  | 0  |     DECR_LEN(data_size, 3);  | 
495  | 0  |     len = _gnutls_read_uint24(p);  | 
496  | 0  |     if (len == 0)  | 
497  | 0  |       return gnutls_assert_val(  | 
498  | 0  |         GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
499  |  |  | 
500  | 0  |     DECR_LEN(data_size, len);  | 
501  | 0  |     p += len + 3;  | 
502  | 0  |     i -= len + 3;  | 
503  |  | 
  | 
504  | 0  |     DECR_LEN(data_size, 2);  | 
505  | 0  |     len = _gnutls_read_uint16(p);  | 
506  | 0  |     DECR_LEN(data_size, len);  | 
507  |  |  | 
508  | 0  |     i -= len + 2;  | 
509  | 0  |     p += len + 2;  | 
510  |  | 
  | 
511  | 0  |     nentries++;  | 
512  | 0  |   }  | 
513  |  |  | 
514  | 0  |   if (data_size != 0)  | 
515  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
516  |  |  | 
517  |  |   /* this is unnecessary - keeping to avoid a regression due to a re-org  | 
518  |  |    * of the loop above */  | 
519  | 0  |   if (nentries == 0)  | 
520  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
521  |  |  | 
522  | 0  |   npeer_ocsp = 0;  | 
523  | 0  |   npeer_certs = 0;  | 
524  |  |  | 
525  |  |   /* Ok we now allocate the memory to hold the  | 
526  |  |    * certificate list  | 
527  |  |    */  | 
528  | 0  |   peer_certs = gnutls_calloc(nentries, sizeof(gnutls_datum_t));  | 
529  | 0  |   if (peer_certs == NULL)  | 
530  | 0  |     return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
531  |  |  | 
532  | 0  |   peer_ocsp = gnutls_calloc(nentries, sizeof(gnutls_datum_t));  | 
533  | 0  |   if (peer_ocsp == NULL) { | 
534  | 0  |     ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
535  | 0  |     goto cleanup;  | 
536  | 0  |   }  | 
537  |  |  | 
538  | 0  |   p = data + 3;  | 
539  |  |  | 
540  |  |   /* Now we start parsing the list (again).  | 
541  |  |    * We don't use DECR_LEN since the list has  | 
542  |  |    * been parsed before.  | 
543  |  |    */  | 
544  |  | 
  | 
545  | 0  |   ctx.session = session;  | 
546  |  | 
  | 
547  | 0  |   for (j = 0; j < nentries; j++) { | 
548  | 0  |     len = _gnutls_read_uint24(p);  | 
549  | 0  |     p += 3;  | 
550  |  | 
  | 
551  | 0  |     ret = _gnutls_set_datum(&peer_certs[j], p, len);  | 
552  | 0  |     if (ret < 0) { | 
553  | 0  |       gnutls_assert();  | 
554  | 0  |       ret = GNUTLS_E_CERTIFICATE_ERROR;  | 
555  | 0  |       goto cleanup;  | 
556  | 0  |     }  | 
557  | 0  |     npeer_certs++;  | 
558  |  | 
  | 
559  | 0  |     p += len;  | 
560  |  | 
  | 
561  | 0  |     len = _gnutls_read_uint16(p);  | 
562  |  | 
  | 
563  | 0  |     ctx.ocsp = &peer_ocsp[j];  | 
564  | 0  |     ctx.idx = j;  | 
565  |  | 
  | 
566  | 0  |     ret = _gnutls_extv_parse(&ctx, parse_cert_extension, p,  | 
567  | 0  |            len + 2);  | 
568  | 0  |     if (ret < 0) { | 
569  | 0  |       gnutls_assert();  | 
570  | 0  |       goto cleanup;  | 
571  | 0  |     }  | 
572  |  |  | 
573  | 0  |     p += len + 2;  | 
574  | 0  |     npeer_ocsp++;  | 
575  | 0  |   }  | 
576  |  |  | 
577  |  |   /* The OCSP entries match the certificate entries, although  | 
578  |  |    * the contents of each OCSP entry may be NULL.  | 
579  |  |    */  | 
580  | 0  |   for (j = 0; j < info->ncerts; j++)  | 
581  | 0  |     gnutls_free(info->raw_certificate_list[j].data);  | 
582  | 0  |   gnutls_free(info->raw_certificate_list);  | 
583  |  | 
  | 
584  | 0  |   for (j = 0; j < info->nocsp; j++)  | 
585  | 0  |     gnutls_free(info->raw_ocsp_list[j].data);  | 
586  | 0  |   gnutls_free(info->raw_ocsp_list);  | 
587  |  | 
  | 
588  | 0  |   info->raw_certificate_list = peer_certs;  | 
589  | 0  |   info->ncerts = npeer_certs;  | 
590  |  | 
  | 
591  | 0  |   info->raw_ocsp_list = peer_ocsp;  | 
592  | 0  |   info->nocsp = npeer_ocsp;  | 
593  |  | 
  | 
594  | 0  |   return 0;  | 
595  |  |  | 
596  | 0  | cleanup:  | 
597  | 0  |   for (j = 0; j < npeer_certs; j++)  | 
598  | 0  |     gnutls_free(peer_certs[j].data);  | 
599  |  | 
  | 
600  | 0  |   for (j = 0; j < npeer_ocsp; j++)  | 
601  | 0  |     gnutls_free(peer_ocsp[j].data);  | 
602  | 0  |   gnutls_free(peer_certs);  | 
603  | 0  |   gnutls_free(peer_ocsp);  | 
604  | 0  |   return ret;  | 
605  | 0  | }  | 
606  |  |  | 
607  |  | static int compress_certificate(gnutls_buffer_st *buf, unsigned cert_pos_mark,  | 
608  |  |         gnutls_compression_method_t comp_method)  | 
609  | 0  | { | 
610  | 0  |   int ret, method_num;  | 
611  | 0  |   size_t comp_bound;  | 
612  | 0  |   gnutls_datum_t plain, comp = { NULL, 0 }; | 
613  |  | 
  | 
614  | 0  |   method_num = _gnutls_compress_certificate_method2num(comp_method);  | 
615  | 0  |   if (method_num == GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER)  | 
616  | 0  |     return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);  | 
617  |  |  | 
618  | 0  |   plain.data = buf->data + cert_pos_mark;  | 
619  | 0  |   plain.size = buf->length - cert_pos_mark;  | 
620  |  | 
  | 
621  | 0  |   comp_bound = _gnutls_compress_bound(comp_method, plain.size);  | 
622  | 0  |   if (comp_bound == 0)  | 
623  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
624  | 0  |   comp.data = gnutls_malloc(comp_bound);  | 
625  | 0  |   if (comp.data == NULL)  | 
626  | 0  |     return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
627  | 0  |   ret = _gnutls_compress(comp_method, comp.data, comp_bound, plain.data,  | 
628  | 0  |              plain.size);  | 
629  | 0  |   if (ret < 0) { | 
630  | 0  |     gnutls_assert();  | 
631  | 0  |     goto cleanup;  | 
632  | 0  |   }  | 
633  | 0  |   comp.size = ret;  | 
634  |  | 
  | 
635  | 0  |   buf->length = cert_pos_mark;  | 
636  | 0  |   ret = _gnutls_buffer_append_prefix(buf, 16, method_num);  | 
637  | 0  |   if (ret < 0) { | 
638  | 0  |     gnutls_assert();  | 
639  | 0  |     goto cleanup;  | 
640  | 0  |   }  | 
641  | 0  |   ret = _gnutls_buffer_append_prefix(buf, 24, plain.size);  | 
642  | 0  |   if (ret < 0) { | 
643  | 0  |     gnutls_assert();  | 
644  | 0  |     goto cleanup;  | 
645  | 0  |   }  | 
646  | 0  |   ret = _gnutls_buffer_append_data_prefix(buf, 24, comp.data, comp.size);  | 
647  | 0  |   if (ret < 0) { | 
648  | 0  |     gnutls_assert();  | 
649  | 0  |     goto cleanup;  | 
650  | 0  |   }  | 
651  |  |  | 
652  | 0  | cleanup:  | 
653  | 0  |   gnutls_free(comp.data);  | 
654  | 0  |   return ret;  | 
655  | 0  | }  | 
656  |  |  | 
657  |  | static int decompress_certificate(gnutls_session_t session,  | 
658  |  |           gnutls_buffer_st *buf)  | 
659  | 0  | { | 
660  | 0  |   int ret;  | 
661  | 0  |   size_t method_num, plain_exp_len;  | 
662  | 0  |   gnutls_datum_t comp, plain = { NULL, 0 }; | 
663  | 0  |   gnutls_compression_method_t comp_method;  | 
664  |  | 
  | 
665  | 0  |   ret = _gnutls_buffer_pop_prefix16(buf, &method_num, 0);  | 
666  | 0  |   if (ret < 0)  | 
667  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
668  | 0  |   comp_method = _gnutls_compress_certificate_num2method(method_num);  | 
669  |  | 
  | 
670  | 0  |   if (!_gnutls_compress_certificate_is_method_enabled(session,  | 
671  | 0  |                   comp_method))  | 
672  | 0  |     return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);  | 
673  |  |  | 
674  | 0  |   ret = _gnutls_buffer_pop_prefix24(buf, &plain_exp_len, 0);  | 
675  | 0  |   if (ret < 0)  | 
676  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
677  |  |  | 
678  | 0  |   ret = _gnutls_buffer_pop_datum_prefix24(buf, &comp);  | 
679  | 0  |   if (ret < 0 || buf->length > 0 || comp.size == 0)  | 
680  | 0  |     return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);  | 
681  |  |  | 
682  | 0  |   plain.data = gnutls_malloc(plain_exp_len);  | 
683  | 0  |   if (plain.data == NULL)  | 
684  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
685  | 0  |   ret = _gnutls_decompress(comp_method, plain.data, plain_exp_len,  | 
686  | 0  |          comp.data, comp.size);  | 
687  | 0  |   if (ret < 0) { | 
688  | 0  |     ret = gnutls_assert_val(GNUTLS_E_CERTIFICATE_ERROR);  | 
689  | 0  |     goto cleanup;  | 
690  | 0  |   }  | 
691  | 0  |   plain.size = ret;  | 
692  |  | 
  | 
693  | 0  |   if (plain.size != plain_exp_len) { | 
694  | 0  |     ret = gnutls_assert_val(GNUTLS_E_CERTIFICATE_ERROR);  | 
695  | 0  |     goto cleanup;  | 
696  | 0  |   }  | 
697  |  |  | 
698  | 0  |   _gnutls_buffer_clear(buf);  | 
699  | 0  |   ret = _gnutls_buffer_append_data(buf, plain.data, plain.size);  | 
700  | 0  |   if (ret < 0) { | 
701  | 0  |     ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
702  | 0  |     goto cleanup;  | 
703  | 0  |   }  | 
704  |  |  | 
705  | 0  | cleanup:  | 
706  | 0  |   gnutls_free(plain.data);  | 
707  | 0  |   return ret;  | 
708  | 0  | }  |