Sync with TP.
[gnutls.git] / lib / ext_cert_type.c
blob60fd2a05bb280bd412e61d1b6a3165f9555c99d8
1 /*
2 * Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains the code the Certificate Type TLS extension.
26 * This extension is currently gnutls specific.
29 #include "gnutls_int.h"
30 #include "gnutls_errors.h"
31 #include "gnutls_num.h"
32 #include "ext_cert_type.h"
33 #include <gnutls_state.h>
34 #include <gnutls_num.h>
36 inline static int _gnutls_num2cert_type (int num);
37 inline static int _gnutls_cert_type2num (int record_size);
39 /*
40 * In case of a server: if a CERT_TYPE extension type is received then it stores
41 * into the session security parameters the new value. The server may use gnutls_session_certificate_type_get(),
42 * to access it.
44 * In case of a client: If a cert_types have been specified then we send the extension.
48 int
49 _gnutls_cert_type_recv_params (gnutls_session_t session,
50 const opaque * data, size_t _data_size)
52 int new_type = -1, ret, i;
53 ssize_t data_size = _data_size;
55 if (session->security_parameters.entity == GNUTLS_CLIENT)
57 if (data_size > 0)
59 if (data_size != 1)
61 gnutls_assert ();
62 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
65 new_type = _gnutls_num2cert_type (data[0]);
67 if (new_type < 0)
69 gnutls_assert ();
70 return new_type;
73 /* Check if we support this cert_type */
74 if ((ret =
75 _gnutls_session_cert_type_supported (session, new_type)) < 0)
77 gnutls_assert ();
78 return ret;
81 _gnutls_session_cert_type_set (session, new_type);
84 else
85 { /* SERVER SIDE - we must check if the sent cert type is the right one
87 if (data_size > 1)
89 uint8_t len;
91 DECR_LEN (data_size, 1);
92 len = data[0];
93 DECR_LEN (data_size, len);
95 for (i = 0; i < len; i++)
97 new_type = _gnutls_num2cert_type (data[i + 1]);
99 if (new_type < 0)
100 continue;
102 /* Check if we support this cert_type */
103 if ((ret =
104 _gnutls_session_cert_type_supported (session,
105 new_type)) < 0)
107 gnutls_assert ();
108 continue;
110 else
111 break;
112 /* new_type is ok */
115 if (new_type < 0)
117 gnutls_assert ();
118 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
121 if ((ret =
122 _gnutls_session_cert_type_supported (session, new_type)) < 0)
124 gnutls_assert ();
125 /* The peer has requested unsupported certificate
126 * types. Instead of failing, procceed normally.
127 * (the ciphersuite selection would fail, or a
128 * non certificate ciphersuite will be selected).
130 return 0;
133 _gnutls_session_cert_type_set (session, new_type);
139 return 0;
142 /* returns data_size or a negative number on failure
145 _gnutls_cert_type_send_params (gnutls_session_t session, opaque * data,
146 size_t data_size)
148 unsigned len, i;
150 /* this function sends the client extension data (dnsname) */
151 if (session->security_parameters.entity == GNUTLS_CLIENT)
154 if (session->internals.priorities.cert_type.algorithms > 0)
157 len = session->internals.priorities.cert_type.algorithms;
159 if (len == 1 &&
160 session->internals.priorities.cert_type.priority[0] ==
161 GNUTLS_CRT_X509)
163 /* We don't use this extension if X.509 certificates
164 * are used.
166 return 0;
169 if (data_size < len + 1)
171 gnutls_assert ();
172 return GNUTLS_E_SHORT_MEMORY_BUFFER;
175 /* this is a vector!
177 data[0] = (uint8_t) len;
179 for (i = 0; i < len; i++)
181 data[i + 1] =
182 _gnutls_cert_type2num (session->internals.
183 priorities.cert_type.priority[i]);
185 return len + 1;
189 else
190 { /* server side */
191 if (session->security_parameters.cert_type != DEFAULT_CERT_TYPE)
193 len = 1;
194 if (data_size < len)
196 gnutls_assert ();
197 return GNUTLS_E_SHORT_MEMORY_BUFFER;
200 data[0] =
201 _gnutls_cert_type2num (session->security_parameters.cert_type);
202 return len;
208 return 0;
211 /* Maps numbers to record sizes according to the
212 * extensions draft.
214 inline static int
215 _gnutls_num2cert_type (int num)
217 switch (num)
219 case 0:
220 return GNUTLS_CRT_X509;
221 case 1:
222 return GNUTLS_CRT_OPENPGP;
223 default:
224 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
228 /* Maps record size to numbers according to the
229 * extensions draft.
231 inline static int
232 _gnutls_cert_type2num (int cert_type)
234 switch (cert_type)
236 case GNUTLS_CRT_X509:
237 return 0;
238 case GNUTLS_CRT_OPENPGP:
239 return 1;
240 default:
241 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;