2 * Copyright (C) 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 * 2010 Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include "gnutls_int.h"
27 #include "gnutls_algorithms.h"
28 #include "gnutls_errors.h"
29 #include "gnutls_cert.h"
30 #include <x509/common.h>
36 gnutls_sec_param_t sec_param;
37 int bits; /* security level */
38 int pk_bits; /* DH, RSA, SRP */
39 int dsa_bits; /* bits for DSA. Handled differently since
40 * choice of key size in DSA is political.
42 int subgroup_bits; /* subgroup bits */
43 int ecc_bits; /* bits for ECC keys */
44 } gnutls_sec_params_entry;
46 static const gnutls_sec_params_entry sec_params[] = {
47 {"Weak", GNUTLS_SEC_PARAM_WEAK, 64, 816, 1024, 128, 128},
48 {"Low", GNUTLS_SEC_PARAM_LOW, 80, 1248, 2048, 160, 160},
49 {"Normal", GNUTLS_SEC_PARAM_NORMAL, 112, 2432, 3072, 224, 224},
50 {"High", GNUTLS_SEC_PARAM_HIGH, 128, 3248, 3072, 256, 256},
51 {"Ultra", GNUTLS_SEC_PARAM_ULTRA, 256, 15424, 3072, 512, 512},
55 #define GNUTLS_SEC_PARAM_LOOP(b) \
56 { const gnutls_sec_params_entry *p; \
57 for(p = sec_params; p->name != NULL; p++) { b ; } }
60 /* Cred type mappings to KX algorithms
61 * FIXME: The mappings are not 1-1. Some KX such as SRP_RSA require
62 * more than one credentials type.
66 gnutls_kx_algorithm_t algorithm;
67 gnutls_credentials_type_t client_type;
68 gnutls_credentials_type_t server_type; /* The type of credentials a server
72 static const gnutls_cred_map cred_mappings[] = {
73 {GNUTLS_KX_ANON_DH, GNUTLS_CRD_ANON, GNUTLS_CRD_ANON},
74 {GNUTLS_KX_RSA, GNUTLS_CRD_CERTIFICATE, GNUTLS_CRD_CERTIFICATE},
75 {GNUTLS_KX_RSA_EXPORT, GNUTLS_CRD_CERTIFICATE, GNUTLS_CRD_CERTIFICATE},
76 {GNUTLS_KX_DHE_DSS, GNUTLS_CRD_CERTIFICATE, GNUTLS_CRD_CERTIFICATE},
77 {GNUTLS_KX_DHE_RSA, GNUTLS_CRD_CERTIFICATE, GNUTLS_CRD_CERTIFICATE},
78 {GNUTLS_KX_PSK, GNUTLS_CRD_PSK, GNUTLS_CRD_PSK},
79 {GNUTLS_KX_DHE_PSK, GNUTLS_CRD_PSK, GNUTLS_CRD_PSK},
80 {GNUTLS_KX_SRP, GNUTLS_CRD_SRP, GNUTLS_CRD_SRP},
81 {GNUTLS_KX_SRP_RSA, GNUTLS_CRD_SRP, GNUTLS_CRD_CERTIFICATE},
82 {GNUTLS_KX_SRP_DSS, GNUTLS_CRD_SRP, GNUTLS_CRD_CERTIFICATE},
86 #define GNUTLS_KX_MAP_LOOP(b) \
87 const gnutls_cred_map *p; \
88 for(p = cred_mappings; p->algorithm != 0; p++) { b ; }
90 #define GNUTLS_KX_MAP_ALG_LOOP_SERVER(a) \
91 GNUTLS_KX_MAP_LOOP( if(p->server_type == type) { a; break; })
93 /* KX mappings to PK algorithms */
96 gnutls_kx_algorithm_t kx_algorithm;
97 gnutls_pk_algorithm_t pk_algorithm;
98 enum encipher_type encipher_type; /* CIPHER_ENCRYPT if this algorithm is to be used
99 * for encryption, CIPHER_SIGN if signature only,
100 * CIPHER_IGN if this does not apply at all.
102 * This is useful to certificate cipher suites, which check
103 * against the certificate key usage bits.
107 /* This table maps the Key exchange algorithms to
108 * the certificate algorithms. Eg. if we have
109 * RSA algorithm in the certificate then we can
110 * use GNUTLS_KX_RSA or GNUTLS_KX_DHE_RSA.
112 static const gnutls_pk_map pk_mappings[] = {
113 {GNUTLS_KX_RSA, GNUTLS_PK_RSA, CIPHER_ENCRYPT},
114 {GNUTLS_KX_RSA_EXPORT, GNUTLS_PK_RSA, CIPHER_SIGN},
115 {GNUTLS_KX_DHE_RSA, GNUTLS_PK_RSA, CIPHER_SIGN},
116 {GNUTLS_KX_SRP_RSA, GNUTLS_PK_RSA, CIPHER_SIGN},
117 {GNUTLS_KX_DHE_DSS, GNUTLS_PK_DSA, CIPHER_SIGN},
118 {GNUTLS_KX_SRP_DSS, GNUTLS_PK_DSA, CIPHER_SIGN},
122 #define GNUTLS_PK_MAP_LOOP(b) \
123 const gnutls_pk_map *p; \
124 for(p = pk_mappings; p->kx_algorithm != 0; p++) { b }
126 #define GNUTLS_PK_MAP_ALG_LOOP(a) \
127 GNUTLS_PK_MAP_LOOP( if(p->kx_algorithm == kx_algorithm) { a; break; })
136 gnutls_protocol_t id; /* gnutls internal version number */
137 int major; /* defined by the protocol */
138 int minor; /* defined by the protocol */
139 int supported; /* 0 not supported, > 0 is supported */
140 } gnutls_version_entry;
142 static const gnutls_version_entry sup_versions[] = {
143 {"SSL3.0", GNUTLS_SSL3, 3, 0, 1},
144 {"TLS1.0", GNUTLS_TLS1, 3, 1, 1},
145 {"TLS1.1", GNUTLS_TLS1_1, 3, 2, 1},
146 {"TLS1.2", GNUTLS_TLS1_2, 3, 3, 1},
150 /* Keep the contents of this struct the same as the previous one. */
151 static const gnutls_protocol_t supported_protocols[] = {
159 #define GNUTLS_VERSION_LOOP(b) \
160 const gnutls_version_entry *p; \
161 for(p = sup_versions; p->name != NULL; p++) { b ; }
163 #define GNUTLS_VERSION_ALG_LOOP(a) \
164 GNUTLS_VERSION_LOOP( if(p->id == version) { a; break; })
166 struct gnutls_cipher_entry
169 gnutls_cipher_algorithm_t id;
174 int export_flag; /* 0 non export */
176 typedef struct gnutls_cipher_entry gnutls_cipher_entry;
178 /* Note that all algorithms are in CBC or STREAM modes.
179 * Do not add any algorithms in other modes (avoid modified algorithms).
180 * View first: "The order of encryption and authentication for
181 * protecting communications" by Hugo Krawczyk - CRYPTO 2001
183 * Make sure to updated MAX_CIPHER_BLOCK_SIZE and MAX_CIPHER_KEY_SIZE as well.
185 static const gnutls_cipher_entry algorithms[] = {
186 {"AES-256-CBC", GNUTLS_CIPHER_AES_256_CBC, 16, 32, CIPHER_BLOCK, 16, 0},
187 {"AES-192-CBC", GNUTLS_CIPHER_AES_192_CBC, 16, 24, CIPHER_BLOCK, 16, 0},
188 {"AES-128-CBC", GNUTLS_CIPHER_AES_128_CBC, 16, 16, CIPHER_BLOCK, 16, 0},
189 {"3DES-CBC", GNUTLS_CIPHER_3DES_CBC, 8, 24, CIPHER_BLOCK, 8, 0},
190 {"DES-CBC", GNUTLS_CIPHER_DES_CBC, 8, 8, CIPHER_BLOCK, 8, 0},
191 {"ARCFOUR-128", GNUTLS_CIPHER_ARCFOUR_128, 1, 16, CIPHER_STREAM, 0, 0},
192 {"ARCFOUR-40", GNUTLS_CIPHER_ARCFOUR_40, 1, 5, CIPHER_STREAM, 0, 1},
193 {"RC2-40", GNUTLS_CIPHER_RC2_40_CBC, 8, 5, CIPHER_BLOCK, 8, 1},
194 #ifdef ENABLE_CAMELLIA
195 {"CAMELLIA-256-CBC", GNUTLS_CIPHER_CAMELLIA_256_CBC, 16, 32, CIPHER_BLOCK,
197 {"CAMELLIA-128-CBC", GNUTLS_CIPHER_CAMELLIA_128_CBC, 16, 16, CIPHER_BLOCK,
201 #ifdef ENABLE_OPENPGP
202 {"IDEA-PGP-CFB", GNUTLS_CIPHER_IDEA_PGP_CFB, 8, 16, CIPHER_BLOCK, 8, 0},
203 {"3DES-PGP-CFB", GNUTLS_CIPHER_3DES_PGP_CFB, 8, 24, CIPHER_BLOCK, 8, 0},
204 {"CAST5-PGP-CFB", GNUTLS_CIPHER_CAST5_PGP_CFB, 8, 16, CIPHER_BLOCK, 8, 0},
205 {"BLOWFISH-PGP-CFB", GNUTLS_CIPHER_BLOWFISH_PGP_CFB, 8,
206 16 /*actually unlimited */ , CIPHER_BLOCK, 8, 0},
207 {"SAFER-SK128-PGP-CFB", GNUTLS_CIPHER_SAFER_SK128_PGP_CFB, 8, 16,
209 {"AES-128-PGP-CFB", GNUTLS_CIPHER_AES128_PGP_CFB, 16, 16, CIPHER_BLOCK, 16,
211 {"AES-192-PGP-CFB", GNUTLS_CIPHER_AES192_PGP_CFB, 16, 24, CIPHER_BLOCK, 16,
213 {"AES-256-PGP-CFB", GNUTLS_CIPHER_AES256_PGP_CFB, 16, 32, CIPHER_BLOCK, 16,
215 {"TWOFISH-PGP-CFB", GNUTLS_CIPHER_TWOFISH_PGP_CFB, 16, 16, CIPHER_BLOCK, 16,
218 {"NULL", GNUTLS_CIPHER_NULL, 1, 0, CIPHER_STREAM, 0, 0},
219 {0, 0, 0, 0, 0, 0, 0}
222 /* Keep the contents of this struct the same as the previous one. */
223 static const gnutls_cipher_algorithm_t supported_ciphers[] = {
224 GNUTLS_CIPHER_AES_256_CBC,
225 GNUTLS_CIPHER_AES_128_CBC,
226 GNUTLS_CIPHER_3DES_CBC,
227 GNUTLS_CIPHER_DES_CBC,
228 GNUTLS_CIPHER_ARCFOUR_128,
229 GNUTLS_CIPHER_ARCFOUR_40,
230 GNUTLS_CIPHER_RC2_40_CBC,
231 #ifdef ENABLE_CAMELLIA
232 GNUTLS_CIPHER_CAMELLIA_256_CBC,
233 GNUTLS_CIPHER_CAMELLIA_128_CBC,
239 #define GNUTLS_LOOP(b) \
240 const gnutls_cipher_entry *p; \
241 for(p = algorithms; p->name != NULL; p++) { b ; }
243 #define GNUTLS_ALG_LOOP(a) \
244 GNUTLS_LOOP( if(p->id == algorithm) { a; break; } )
247 struct gnutls_hash_entry
251 gnutls_mac_algorithm_t id;
252 size_t key_size; /* in case of mac */
254 typedef struct gnutls_hash_entry gnutls_hash_entry;
256 static const gnutls_hash_entry hash_algorithms[] = {
257 {"SHA1", HASH_OID_SHA1, GNUTLS_MAC_SHA1, 20},
258 {"MD5", HASH_OID_MD5, GNUTLS_MAC_MD5, 16},
259 {"SHA256", HASH_OID_SHA256, GNUTLS_MAC_SHA256, 32},
260 {"SHA384", HASH_OID_SHA384, GNUTLS_MAC_SHA384, 48},
261 {"SHA512", HASH_OID_SHA512, GNUTLS_MAC_SHA512, 64},
262 {"MD2", HASH_OID_MD2, GNUTLS_MAC_MD2, 0}, /* not used as MAC */
263 {"RIPEMD160", HASH_OID_RMD160, GNUTLS_MAC_RMD160, 20},
264 {"MAC-NULL", NULL, GNUTLS_MAC_NULL, 0},
268 /* Keep the contents of this struct the same as the previous one. */
269 static const gnutls_mac_algorithm_t supported_macs[] = {
281 #define GNUTLS_HASH_LOOP(b) \
282 const gnutls_hash_entry *p; \
283 for(p = hash_algorithms; p->name != NULL; p++) { b ; }
285 #define GNUTLS_HASH_ALG_LOOP(a) \
286 GNUTLS_HASH_LOOP( if(p->id == algorithm) { a; break; } )
288 /* Key Exchange Section */
291 extern mod_auth_st rsa_auth_struct;
292 extern mod_auth_st rsa_export_auth_struct;
293 extern mod_auth_st dhe_rsa_auth_struct;
294 extern mod_auth_st dhe_dss_auth_struct;
295 extern mod_auth_st anon_auth_struct;
296 extern mod_auth_st srp_auth_struct;
297 extern mod_auth_st psk_auth_struct;
298 extern mod_auth_st dhe_psk_auth_struct;
299 extern mod_auth_st srp_rsa_auth_struct;
300 extern mod_auth_st srp_dss_auth_struct;
302 struct gnutls_kx_algo_entry
305 gnutls_kx_algorithm_t algorithm;
306 mod_auth_st *auth_struct;
308 int needs_rsa_params;
310 typedef struct gnutls_kx_algo_entry gnutls_kx_algo_entry;
312 static const gnutls_kx_algo_entry _gnutls_kx_algorithms[] = {
314 {"ANON-DH", GNUTLS_KX_ANON_DH, &anon_auth_struct, 1, 0},
316 {"RSA", GNUTLS_KX_RSA, &rsa_auth_struct, 0, 0},
317 {"RSA-EXPORT", GNUTLS_KX_RSA_EXPORT, &rsa_export_auth_struct, 0,
318 1 /* needs RSA params */ },
319 {"DHE-RSA", GNUTLS_KX_DHE_RSA, &dhe_rsa_auth_struct, 1, 0},
320 {"DHE-DSS", GNUTLS_KX_DHE_DSS, &dhe_dss_auth_struct, 1, 0},
323 {"SRP-DSS", GNUTLS_KX_SRP_DSS, &srp_dss_auth_struct, 0, 0},
324 {"SRP-RSA", GNUTLS_KX_SRP_RSA, &srp_rsa_auth_struct, 0, 0},
325 {"SRP", GNUTLS_KX_SRP, &srp_auth_struct, 0, 0},
328 {"PSK", GNUTLS_KX_PSK, &psk_auth_struct, 0, 0},
329 {"DHE-PSK", GNUTLS_KX_DHE_PSK, &dhe_psk_auth_struct,
330 1 /* needs DHE params */ , 0},
335 /* Keep the contents of this struct the same as the previous one. */
336 static const gnutls_kx_algorithm_t supported_kxs[] = {
341 GNUTLS_KX_RSA_EXPORT,
356 #define GNUTLS_KX_LOOP(b) \
357 const gnutls_kx_algo_entry *p; \
358 for(p = _gnutls_kx_algorithms; p->name != NULL; p++) { b ; }
360 #define GNUTLS_KX_ALG_LOOP(a) \
361 GNUTLS_KX_LOOP( if(p->algorithm == algorithm) { a; break; } )
366 #define GNUTLS_CIPHER_SUITE_ENTRY( name, block_algorithm, kx_algorithm, mac_algorithm, min_version, max_version ) \
367 { #name, {name}, block_algorithm, kx_algorithm, mac_algorithm, min_version, max_version }
373 gnutls_cipher_algorithm_t block_algorithm;
374 gnutls_kx_algorithm_t kx_algorithm;
375 gnutls_mac_algorithm_t mac_algorithm;
376 gnutls_protocol_t min_version; /* this cipher suite is supported
377 * from 'version' and above;
379 gnutls_protocol_t max_version; /* this cipher suite is not supported after that */
380 } gnutls_cipher_suite_entry;
382 /* RSA with NULL cipher and MD5 MAC
385 #define GNUTLS_RSA_NULL_MD5 { 0x00, 0x01 }
386 #define GNUTLS_RSA_NULL_SHA1 { 0x00, 0x02 }
387 #define GNUTLS_RSA_NULL_SHA256 { 0x00, 0x3B }
389 /* ANONymous cipher suites.
392 #define GNUTLS_ANON_DH_3DES_EDE_CBC_SHA1 { 0x00, 0x1B }
393 #define GNUTLS_ANON_DH_ARCFOUR_MD5 { 0x00, 0x18 }
396 #define GNUTLS_ANON_DH_AES_128_CBC_SHA1 { 0x00, 0x34 }
397 #define GNUTLS_ANON_DH_AES_256_CBC_SHA1 { 0x00, 0x3A }
400 #ifdef ENABLE_CAMELLIA
401 #define GNUTLS_ANON_DH_CAMELLIA_128_CBC_SHA1 { 0x00,0x46 }
402 #define GNUTLS_ANON_DH_CAMELLIA_256_CBC_SHA1 { 0x00,0x89 }
405 #define GNUTLS_ANON_DH_AES_128_CBC_SHA256 { 0x00, 0x6C }
406 #define GNUTLS_ANON_DH_AES_256_CBC_SHA256 { 0x00, 0x6D }
408 /* PSK (not in TLS 1.0)
409 * draft-ietf-tls-psk:
411 #define GNUTLS_PSK_SHA_ARCFOUR_SHA1 { 0x00, 0x8A }
412 #define GNUTLS_PSK_SHA_3DES_EDE_CBC_SHA1 { 0x00, 0x8B }
413 #define GNUTLS_PSK_SHA_AES_128_CBC_SHA1 { 0x00, 0x8C }
414 #define GNUTLS_PSK_SHA_AES_256_CBC_SHA1 { 0x00, 0x8D }
416 #define GNUTLS_DHE_PSK_SHA_ARCFOUR_SHA1 { 0x00, 0x8E }
417 #define GNUTLS_DHE_PSK_SHA_3DES_EDE_CBC_SHA1 { 0x00, 0x8F }
418 #define GNUTLS_DHE_PSK_SHA_AES_128_CBC_SHA1 { 0x00, 0x90 }
419 #define GNUTLS_DHE_PSK_SHA_AES_256_CBC_SHA1 { 0x00, 0x91 }
424 #define GNUTLS_SRP_SHA_3DES_EDE_CBC_SHA1 { 0xC0, 0x1A }
425 #define GNUTLS_SRP_SHA_RSA_3DES_EDE_CBC_SHA1 { 0xC0, 0x1B }
426 #define GNUTLS_SRP_SHA_DSS_3DES_EDE_CBC_SHA1 { 0xC0, 0x1C }
428 #define GNUTLS_SRP_SHA_AES_128_CBC_SHA1 { 0xC0, 0x1D }
429 #define GNUTLS_SRP_SHA_RSA_AES_128_CBC_SHA1 { 0xC0, 0x1E }
430 #define GNUTLS_SRP_SHA_DSS_AES_128_CBC_SHA1 { 0xC0, 0x1F }
432 #define GNUTLS_SRP_SHA_AES_256_CBC_SHA1 { 0xC0, 0x20 }
433 #define GNUTLS_SRP_SHA_RSA_AES_256_CBC_SHA1 { 0xC0, 0x21 }
434 #define GNUTLS_SRP_SHA_DSS_AES_256_CBC_SHA1 { 0xC0, 0x22 }
438 #define GNUTLS_RSA_ARCFOUR_SHA1 { 0x00, 0x05 }
439 #define GNUTLS_RSA_ARCFOUR_MD5 { 0x00, 0x04 }
440 #define GNUTLS_RSA_3DES_EDE_CBC_SHA1 { 0x00, 0x0A }
442 #define GNUTLS_RSA_EXPORT_ARCFOUR_40_MD5 { 0x00, 0x03 }
446 #define GNUTLS_RSA_AES_128_CBC_SHA1 { 0x00, 0x2F }
447 #define GNUTLS_RSA_AES_256_CBC_SHA1 { 0x00, 0x35 }
450 #ifdef ENABLE_CAMELLIA
451 #define GNUTLS_RSA_CAMELLIA_128_CBC_SHA1 { 0x00,0x41 }
452 #define GNUTLS_RSA_CAMELLIA_256_CBC_SHA1 { 0x00,0x84 }
455 #define GNUTLS_RSA_AES_128_CBC_SHA256 { 0x00, 0x3C }
456 #define GNUTLS_RSA_AES_256_CBC_SHA256 { 0x00, 0x3D }
461 #define GNUTLS_DHE_DSS_3DES_EDE_CBC_SHA1 { 0x00, 0x13 }
464 /* draft-ietf-tls-56-bit-ciphersuites-01:
466 #define GNUTLS_DHE_DSS_ARCFOUR_SHA1 { 0x00, 0x66 }
471 #define GNUTLS_DHE_DSS_AES_256_CBC_SHA1 { 0x00, 0x38 }
472 #define GNUTLS_DHE_DSS_AES_128_CBC_SHA1 { 0x00, 0x32 }
475 #ifdef ENABLE_CAMELLIA
476 #define GNUTLS_DHE_DSS_CAMELLIA_128_CBC_SHA1 { 0x00,0x44 }
477 #define GNUTLS_DHE_DSS_CAMELLIA_256_CBC_SHA1 { 0x00,0x87 }
480 #define GNUTLS_DHE_DSS_AES_128_CBC_SHA256 { 0x00, 0x40 }
481 #define GNUTLS_DHE_DSS_AES_256_CBC_SHA256 { 0x00, 0x6A }
485 #define GNUTLS_DHE_RSA_3DES_EDE_CBC_SHA1 { 0x00, 0x16 }
489 #define GNUTLS_DHE_RSA_AES_128_CBC_SHA1 { 0x00, 0x33 }
490 #define GNUTLS_DHE_RSA_AES_256_CBC_SHA1 { 0x00, 0x39 }
493 #ifdef ENABLE_CAMELLIA
494 #define GNUTLS_DHE_RSA_CAMELLIA_128_CBC_SHA1 { 0x00,0x45 }
495 #define GNUTLS_DHE_RSA_CAMELLIA_256_CBC_SHA1 { 0x00,0x88 }
498 #define GNUTLS_DHE_RSA_AES_128_CBC_SHA256 { 0x00, 0x67 }
499 #define GNUTLS_DHE_RSA_AES_256_CBC_SHA256 { 0x00, 0x6B }
501 /* Safe renegotiation */
503 #define CIPHER_SUITES_COUNT sizeof(cs_algorithms)/sizeof(gnutls_cipher_suite_entry)-1
505 static const gnutls_cipher_suite_entry cs_algorithms[] = {
507 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_ARCFOUR_MD5,
508 GNUTLS_CIPHER_ARCFOUR_128,
509 GNUTLS_KX_ANON_DH, GNUTLS_MAC_MD5,
510 GNUTLS_SSL3, GNUTLS_VERSION_MAX),
511 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_3DES_EDE_CBC_SHA1,
512 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_ANON_DH,
513 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
515 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_128_CBC_SHA1,
516 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_ANON_DH,
517 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
519 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_256_CBC_SHA1,
520 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_ANON_DH,
521 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
523 #ifdef ENABLE_CAMELLIA
524 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_CAMELLIA_128_CBC_SHA1,
525 GNUTLS_CIPHER_CAMELLIA_128_CBC,
527 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
529 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_CAMELLIA_256_CBC_SHA1,
530 GNUTLS_CIPHER_CAMELLIA_256_CBC,
532 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
535 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_128_CBC_SHA256,
536 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_ANON_DH,
537 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
539 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_ANON_DH_AES_256_CBC_SHA256,
540 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_ANON_DH,
541 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
545 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_PSK_SHA_ARCFOUR_SHA1,
546 GNUTLS_CIPHER_ARCFOUR, GNUTLS_KX_PSK,
547 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
549 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_PSK_SHA_3DES_EDE_CBC_SHA1,
550 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_PSK,
551 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
553 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_PSK_SHA_AES_128_CBC_SHA1,
554 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_PSK,
555 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
557 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_PSK_SHA_AES_256_CBC_SHA1,
558 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_PSK,
559 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
563 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_PSK_SHA_ARCFOUR_SHA1,
564 GNUTLS_CIPHER_ARCFOUR, GNUTLS_KX_DHE_PSK,
565 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
567 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_PSK_SHA_3DES_EDE_CBC_SHA1,
568 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_DHE_PSK,
569 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
571 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_PSK_SHA_AES_128_CBC_SHA1,
572 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_DHE_PSK,
573 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
575 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_PSK_SHA_AES_256_CBC_SHA1,
576 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_DHE_PSK,
577 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
581 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_3DES_EDE_CBC_SHA1,
582 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_SRP,
583 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
585 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_AES_128_CBC_SHA1,
586 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_SRP,
587 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
589 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_AES_256_CBC_SHA1,
590 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_SRP,
591 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
594 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_3DES_EDE_CBC_SHA1,
595 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_SRP_DSS,
596 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
599 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_3DES_EDE_CBC_SHA1,
600 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_SRP_RSA,
601 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
604 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_AES_128_CBC_SHA1,
605 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_SRP_DSS,
606 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
609 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_AES_128_CBC_SHA1,
610 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_SRP_RSA,
611 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
614 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_DSS_AES_256_CBC_SHA1,
615 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_SRP_DSS,
616 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
619 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_SRP_SHA_RSA_AES_256_CBC_SHA1,
620 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_SRP_RSA,
621 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
625 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_ARCFOUR_SHA1,
626 GNUTLS_CIPHER_ARCFOUR_128, GNUTLS_KX_DHE_DSS,
627 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
629 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_3DES_EDE_CBC_SHA1,
630 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_DHE_DSS,
631 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
633 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_128_CBC_SHA1,
634 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_DHE_DSS,
635 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
637 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_256_CBC_SHA1,
638 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_DHE_DSS,
639 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
641 #ifdef ENABLE_CAMELLIA
642 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_CAMELLIA_128_CBC_SHA1,
643 GNUTLS_CIPHER_CAMELLIA_128_CBC,
645 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
647 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_CAMELLIA_256_CBC_SHA1,
648 GNUTLS_CIPHER_CAMELLIA_256_CBC,
650 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
653 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_128_CBC_SHA256,
654 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_DHE_DSS,
655 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
657 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_DSS_AES_256_CBC_SHA256,
658 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_DHE_DSS,
659 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
662 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_3DES_EDE_CBC_SHA1,
663 GNUTLS_CIPHER_3DES_CBC, GNUTLS_KX_DHE_RSA,
664 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
666 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_128_CBC_SHA1,
667 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_DHE_RSA,
668 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
670 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_256_CBC_SHA1,
671 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_DHE_RSA,
672 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
674 #ifdef ENABLE_CAMELLIA
675 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_CAMELLIA_128_CBC_SHA1,
676 GNUTLS_CIPHER_CAMELLIA_128_CBC,
678 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
680 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_CAMELLIA_256_CBC_SHA1,
681 GNUTLS_CIPHER_CAMELLIA_256_CBC,
683 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
686 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_128_CBC_SHA256,
687 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_DHE_RSA,
688 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
690 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_DHE_RSA_AES_256_CBC_SHA256,
691 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_DHE_RSA,
692 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
695 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_NULL_MD5,
697 GNUTLS_KX_RSA, GNUTLS_MAC_MD5, GNUTLS_SSL3,
699 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_NULL_SHA1,
701 GNUTLS_KX_RSA, GNUTLS_MAC_SHA1, GNUTLS_SSL3,
703 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_NULL_SHA256,
705 GNUTLS_KX_RSA, GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
709 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_EXPORT_ARCFOUR_40_MD5,
710 GNUTLS_CIPHER_ARCFOUR_40,
711 GNUTLS_KX_RSA_EXPORT, GNUTLS_MAC_MD5,
712 GNUTLS_SSL3, GNUTLS_TLS1_0),
715 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_ARCFOUR_SHA1,
716 GNUTLS_CIPHER_ARCFOUR_128,
717 GNUTLS_KX_RSA, GNUTLS_MAC_SHA1, GNUTLS_SSL3,
719 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_ARCFOUR_MD5,
720 GNUTLS_CIPHER_ARCFOUR_128,
721 GNUTLS_KX_RSA, GNUTLS_MAC_MD5, GNUTLS_SSL3,
723 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_3DES_EDE_CBC_SHA1,
724 GNUTLS_CIPHER_3DES_CBC,
725 GNUTLS_KX_RSA, GNUTLS_MAC_SHA1, GNUTLS_SSL3,
727 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_128_CBC_SHA1,
728 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_RSA,
729 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
731 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_256_CBC_SHA1,
732 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_RSA,
733 GNUTLS_MAC_SHA1, GNUTLS_SSL3,
735 #ifdef ENABLE_CAMELLIA
736 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_CAMELLIA_128_CBC_SHA1,
737 GNUTLS_CIPHER_CAMELLIA_128_CBC, GNUTLS_KX_RSA,
738 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
740 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_CAMELLIA_256_CBC_SHA1,
741 GNUTLS_CIPHER_CAMELLIA_256_CBC, GNUTLS_KX_RSA,
742 GNUTLS_MAC_SHA1, GNUTLS_TLS1,
745 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_128_CBC_SHA256,
746 GNUTLS_CIPHER_AES_128_CBC, GNUTLS_KX_RSA,
747 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
749 GNUTLS_CIPHER_SUITE_ENTRY (GNUTLS_RSA_AES_256_CBC_SHA256,
750 GNUTLS_CIPHER_AES_256_CBC, GNUTLS_KX_RSA,
751 GNUTLS_MAC_SHA256, GNUTLS_TLS1_2,
753 {0, {{0, 0}}, 0, 0, 0, 0, 0}
756 #define GNUTLS_CIPHER_SUITE_LOOP(b) \
757 const gnutls_cipher_suite_entry *p; \
758 for(p = cs_algorithms; p->name != NULL; p++) { b ; }
760 #define GNUTLS_CIPHER_SUITE_ALG_LOOP(a) \
761 GNUTLS_CIPHER_SUITE_LOOP( if( (p->id.suite[0] == suite->suite[0]) && (p->id.suite[1] == suite->suite[1])) { a; break; } )
765 /* Generic Functions */
768 _gnutls_mac_priority (gnutls_session_t session,
769 gnutls_mac_algorithm_t algorithm)
770 { /* actually returns the priority */
772 for (i = 0; i < session->internals.priorities.mac.algorithms; i++)
774 if (session->internals.priorities.mac.priority[i] == algorithm)
781 * gnutls_mac_get_name:
782 * @algorithm: is a MAC algorithm
784 * Convert a #gnutls_mac_algorithm_t value to a string.
786 * Returns: a string that contains the name of the specified MAC
787 * algorithm, or %NULL.
790 gnutls_mac_get_name (gnutls_mac_algorithm_t algorithm)
792 const char *ret = NULL;
795 GNUTLS_HASH_ALG_LOOP (ret = p->name);
802 * @name: is a MAC algorithm name
804 * Convert a string to a #gnutls_mac_algorithm_t value. The names are
805 * compared in a case insensitive way.
807 * Returns: a #gnutls_mac_algorithm_t id of the specified MAC
808 * algorithm string, or %GNUTLS_MAC_UNKNOWN on failures.
810 gnutls_mac_algorithm_t
811 gnutls_mac_get_id (const char *name)
813 gnutls_mac_algorithm_t ret = GNUTLS_MAC_UNKNOWN;
815 GNUTLS_HASH_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id);
821 * gnutls_mac_get_key_size:
822 * @algorithm: is an encryption algorithm
824 * Get size of MAC key.
826 * Returns: length (in bytes) of the given MAC key size, or 0 if the
827 * given MAC algorithm is invalid.
830 gnutls_mac_get_key_size (gnutls_mac_algorithm_t algorithm)
835 GNUTLS_HASH_ALG_LOOP (ret = p->key_size);
843 * Get a list of hash algorithms for use as MACs. Note that not
844 * necessarily all MACs are supported in TLS cipher suites. For
845 * example, MD2 is not supported as a cipher suite, but is supported
846 * for other purposes (e.g., X.509 signature verification or similar).
848 * Returns: Return a zero-terminated list of #gnutls_mac_algorithm_t
849 * integers indicating the available MACs.
851 const gnutls_mac_algorithm_t *
852 gnutls_mac_list (void)
854 return supported_macs;
858 _gnutls_x509_mac_to_oid (gnutls_mac_algorithm_t algorithm)
860 const char *ret = NULL;
863 GNUTLS_HASH_ALG_LOOP (ret = p->oid);
868 gnutls_mac_algorithm_t
869 _gnutls_x509_oid2mac_algorithm (const char *oid)
871 gnutls_mac_algorithm_t ret = 0;
873 GNUTLS_HASH_LOOP (if (p->oid && strcmp (oid, p->oid) == 0)
879 return GNUTLS_MAC_UNKNOWN;
885 _gnutls_mac_is_ok (gnutls_mac_algorithm_t algorithm)
888 GNUTLS_HASH_ALG_LOOP (ret = p->id);
896 /* CIPHER functions */
899 * gnutls_cipher_get_block_size:
900 * @algorithm: is an encryption algorithm
902 * Get block size for encryption algorithm.
904 * Returns: block size for encryption algorithm.
909 gnutls_cipher_get_block_size (gnutls_cipher_algorithm_t algorithm)
912 GNUTLS_ALG_LOOP (ret = p->blocksize);
917 /* returns the priority */
919 _gnutls_cipher_priority (gnutls_session_t session,
920 gnutls_cipher_algorithm_t algorithm)
923 for (i = 0; i < session->internals.priorities.cipher.algorithms; i++)
925 if (session->internals.priorities.cipher.priority[i] == algorithm)
933 _gnutls_cipher_is_block (gnutls_cipher_algorithm_t algorithm)
937 GNUTLS_ALG_LOOP (ret = p->block);
943 * gnutls_cipher_get_key_size:
944 * @algorithm: is an encryption algorithm
946 * Get key size for cipher.
948 * Returns: length (in bytes) of the given cipher's key size, or 0 if
949 * the given cipher is invalid.
952 gnutls_cipher_get_key_size (gnutls_cipher_algorithm_t algorithm)
955 GNUTLS_ALG_LOOP (ret = p->keysize);
961 _gnutls_cipher_get_iv_size (gnutls_cipher_algorithm_t algorithm)
964 GNUTLS_ALG_LOOP (ret = p->iv);
970 _gnutls_cipher_get_export_flag (gnutls_cipher_algorithm_t algorithm)
973 GNUTLS_ALG_LOOP (ret = p->export_flag);
979 * gnutls_cipher_get_name:
980 * @algorithm: is an encryption algorithm
982 * Convert a #gnutls_cipher_algorithm_t type to a string.
984 * Returns: a pointer to a string that contains the name of the
985 * specified cipher, or %NULL.
988 gnutls_cipher_get_name (gnutls_cipher_algorithm_t algorithm)
990 const char *ret = NULL;
993 GNUTLS_ALG_LOOP (ret = p->name);
999 * gnutls_cipher_get_id:
1000 * @name: is a MAC algorithm name
1002 * The names are compared in a case insensitive way.
1004 * Returns: return a #gnutls_cipher_algorithm_t value corresponding to
1005 * the specified cipher, or %GNUTLS_CIPHER_UNKNOWN on error.
1007 gnutls_cipher_algorithm_t
1008 gnutls_cipher_get_id (const char *name)
1010 gnutls_cipher_algorithm_t ret = GNUTLS_CIPHER_UNKNOWN;
1012 GNUTLS_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id);
1018 * gnutls_cipher_list:
1020 * Get a list of supported cipher algorithms. Note that not
1021 * necessarily all ciphers are supported as TLS cipher suites. For
1022 * example, DES is not supported as a cipher suite, but is supported
1023 * for other purposes (e.g., PKCS#8 or similar).
1025 * Returns: a zero-terminated list of #gnutls_cipher_algorithm_t
1026 * integers indicating the available ciphers.
1029 const gnutls_cipher_algorithm_t *
1030 gnutls_cipher_list (void)
1032 return supported_ciphers;
1036 _gnutls_cipher_is_ok (gnutls_cipher_algorithm_t algorithm)
1039 GNUTLS_ALG_LOOP (ret = p->id);
1047 /* Key EXCHANGE functions */
1049 _gnutls_kx_auth_struct (gnutls_kx_algorithm_t algorithm)
1051 mod_auth_st *ret = NULL;
1052 GNUTLS_KX_ALG_LOOP (ret = p->auth_struct);
1059 _gnutls_kx_priority (gnutls_session_t session,
1060 gnutls_kx_algorithm_t algorithm)
1063 for (i = 0; i < session->internals.priorities.kx.algorithms; i++)
1065 if (session->internals.priorities.kx.priority[i] == algorithm)
1072 * gnutls_kx_get_name:
1073 * @algorithm: is a key exchange algorithm
1075 * Convert a #gnutls_kx_algorithm_t value to a string.
1077 * Returns: a pointer to a string that contains the name of the
1078 * specified key exchange algorithm, or %NULL.
1081 gnutls_kx_get_name (gnutls_kx_algorithm_t algorithm)
1083 const char *ret = NULL;
1086 GNUTLS_KX_ALG_LOOP (ret = p->name);
1093 * @name: is a KX name
1095 * Convert a string to a #gnutls_kx_algorithm_t value. The names are
1096 * compared in a case insensitive way.
1098 * Returns: an id of the specified KX algorithm, or %GNUTLS_KX_UNKNOWN
1101 gnutls_kx_algorithm_t
1102 gnutls_kx_get_id (const char *name)
1104 gnutls_cipher_algorithm_t ret = GNUTLS_KX_UNKNOWN;
1106 GNUTLS_KX_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->algorithm);
1114 * Get a list of supported key exchange algorithms.
1116 * Returns: a zero-terminated list of #gnutls_kx_algorithm_t integers
1117 * indicating the available key exchange algorithms.
1119 const gnutls_kx_algorithm_t *
1120 gnutls_kx_list (void)
1122 return supported_kxs;
1126 _gnutls_kx_is_ok (gnutls_kx_algorithm_t algorithm)
1129 GNUTLS_KX_ALG_LOOP (ret = p->algorithm);
1138 _gnutls_kx_needs_rsa_params (gnutls_kx_algorithm_t algorithm)
1141 GNUTLS_KX_ALG_LOOP (ret = p->needs_rsa_params);
1146 _gnutls_kx_needs_dh_params (gnutls_kx_algorithm_t algorithm)
1149 GNUTLS_KX_ALG_LOOP (ret = p->needs_dh_params);
1156 _gnutls_version_priority (gnutls_session_t session, gnutls_protocol_t version)
1157 { /* actually returns the priority */
1160 for (i = 0; i < session->internals.priorities.protocol.algorithms; i++)
1162 if (session->internals.priorities.protocol.priority[i] == version)
1169 _gnutls_version_lowest (gnutls_session_t session)
1170 { /* returns the lowest version supported */
1171 unsigned int i, min = 0xff;
1173 for (i = 0; i < session->internals.priorities.protocol.algorithms; i++)
1175 if (session->internals.priorities.protocol.priority[i] < min)
1176 min = session->internals.priorities.protocol.priority[i];
1180 return GNUTLS_VERSION_UNKNOWN; /* unknown version */
1186 _gnutls_version_max (gnutls_session_t session)
1187 { /* returns the maximum version supported */
1188 unsigned int i, max = 0x00;
1190 for (i = 0; i < session->internals.priorities.protocol.algorithms; i++)
1192 if (session->internals.priorities.protocol.priority[i] > max)
1193 max = session->internals.priorities.protocol.priority[i];
1197 return GNUTLS_VERSION_UNKNOWN; /* unknown version */
1204 * gnutls_protocol_get_name:
1205 * @version: is a (gnutls) version number
1207 * Convert a #gnutls_protocol_t value to a string.
1209 * Returns: a string that contains the name of the specified TLS
1210 * version (e.g., "TLS1.0"), or %NULL.
1213 gnutls_protocol_get_name (gnutls_protocol_t version)
1215 const char *ret = NULL;
1218 GNUTLS_VERSION_ALG_LOOP (ret = p->name);
1223 * gnutls_protocol_get_id:
1224 * @name: is a protocol name
1226 * The names are compared in a case insensitive way.
1228 * Returns: an id of the specified protocol, or
1229 * %GNUTLS_VERSION_UNKNOWN on error.
1232 gnutls_protocol_get_id (const char *name)
1234 gnutls_protocol_t ret = GNUTLS_VERSION_UNKNOWN;
1236 GNUTLS_VERSION_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id);
1242 * gnutls_protocol_list:
1244 * Get a list of supported protocols, e.g. SSL 3.0, TLS 1.0 etc.
1246 * Returns: a zero-terminated list of #gnutls_protocol_t integers
1247 * indicating the available protocols.
1250 const gnutls_protocol_t *
1251 gnutls_protocol_list (void)
1253 return supported_protocols;
1257 _gnutls_version_get_minor (gnutls_protocol_t version)
1261 GNUTLS_VERSION_ALG_LOOP (ret = p->minor);
1266 _gnutls_version_get (int major, int minor)
1270 GNUTLS_VERSION_LOOP (if ((p->major == major) && (p->minor == minor))
1276 _gnutls_version_get_major (gnutls_protocol_t version)
1280 GNUTLS_VERSION_ALG_LOOP (ret = p->major);
1284 /* Version Functions */
1287 _gnutls_version_is_supported (gnutls_session_t session,
1288 const gnutls_protocol_t version)
1292 GNUTLS_VERSION_ALG_LOOP (ret = p->supported);
1296 if (_gnutls_version_priority (session, version) < 0)
1297 return 0; /* disabled by the user */
1303 /* This function determines if the version specified has a
1304 cipher-suite selected PRF hash function instead of the old
1305 hardcoded MD5+SHA1. */
1307 _gnutls_version_has_selectable_prf (gnutls_protocol_t version)
1309 return version == GNUTLS_TLS1_2;
1312 /* This function determines if the version specified has selectable
1313 signature/hash functions for certificate authentification. */
1315 _gnutls_version_has_selectable_sighash (gnutls_protocol_t version)
1317 return version == GNUTLS_TLS1_2;
1320 /* This function determines if the version specified has support for
1323 _gnutls_version_has_extensions (gnutls_protocol_t version)
1336 /* This function determines if the version specified has explicit IVs
1337 (for CBC attack prevention). */
1339 _gnutls_version_has_explicit_iv (gnutls_protocol_t version)
1351 /* This function determines if the version specified can have
1352 non-minimal padding. */
1354 _gnutls_version_has_variable_padding (gnutls_protocol_t version)
1367 /* Type to KX mappings */
1368 gnutls_kx_algorithm_t
1369 _gnutls_map_kx_get_kx (gnutls_credentials_type_t type, int server)
1371 gnutls_kx_algorithm_t ret = -1;
1375 GNUTLS_KX_MAP_ALG_LOOP_SERVER (ret = p->algorithm);
1379 GNUTLS_KX_MAP_ALG_LOOP_SERVER (ret = p->algorithm);
1384 gnutls_credentials_type_t
1385 _gnutls_map_kx_get_cred (gnutls_kx_algorithm_t algorithm, int server)
1387 gnutls_credentials_type_t ret = -1;
1390 GNUTLS_KX_MAP_LOOP (if (p->algorithm == algorithm) ret =
1395 GNUTLS_KX_MAP_LOOP (if (p->algorithm == algorithm) ret =
1403 /* Cipher Suite's functions */
1404 gnutls_cipher_algorithm_t
1405 _gnutls_cipher_suite_get_cipher_algo (const cipher_suite_st * suite)
1408 GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->block_algorithm);
1413 _gnutls_cipher_suite_is_version_supported (const cipher_suite_st * suite,
1414 gnutls_protocol_t version)
1417 GNUTLS_CIPHER_SUITE_ALG_LOOP ((version >= p->min_version
1418 && version <= p->max_version) ? (ret =
1424 gnutls_kx_algorithm_t
1425 _gnutls_cipher_suite_get_kx_algo (const cipher_suite_st * suite)
1429 GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->kx_algorithm);
1434 gnutls_mac_algorithm_t
1435 _gnutls_cipher_suite_get_mac_algo (const cipher_suite_st * suite)
1438 GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->mac_algorithm);
1444 _gnutls_cipher_suite_get_name (cipher_suite_st * suite)
1446 const char *ret = NULL;
1449 GNUTLS_CIPHER_SUITE_ALG_LOOP (ret = p->name + sizeof ("GNUTLS_") - 1);
1455 * gnutls_cipher_suite_get_name:
1456 * @kx_algorithm: is a Key exchange algorithm
1457 * @cipher_algorithm: is a cipher algorithm
1458 * @mac_algorithm: is a MAC algorithm
1460 * Note that the full cipher suite name must be prepended by TLS or
1461 * SSL depending of the protocol in use.
1463 * Returns: a string that contains the name of a TLS cipher suite,
1464 * specified by the given algorithms, or %NULL.
1467 gnutls_cipher_suite_get_name (gnutls_kx_algorithm_t kx_algorithm,
1468 gnutls_cipher_algorithm_t cipher_algorithm,
1469 gnutls_mac_algorithm_t mac_algorithm)
1471 const char *ret = NULL;
1474 GNUTLS_CIPHER_SUITE_LOOP (if (kx_algorithm == p->kx_algorithm &&
1475 cipher_algorithm == p->block_algorithm &&
1476 mac_algorithm == p->mac_algorithm)
1477 ret = p->name + sizeof ("GNUTLS_") - 1);
1483 * gnutls_cipher_suite_info:
1484 * @idx: index of cipher suite to get information about, starts on 0.
1485 * @cs_id: output buffer with room for 2 bytes, indicating cipher suite value
1486 * @kx: output variable indicating key exchange algorithm, or %NULL.
1487 * @cipher: output variable indicating cipher, or %NULL.
1488 * @mac: output variable indicating MAC algorithm, or %NULL.
1489 * @version: output variable indicating TLS protocol version, or %NULL.
1491 * Get information about supported cipher suites. Use the function
1492 * iteratively to get information about all supported cipher suites.
1493 * Call with idx=0 to get information about first cipher suite, then
1494 * idx=1 and so on until the function returns NULL.
1496 * Returns: the name of @idx cipher suite, and set the information
1497 * about the cipher suite in the output variables. If @idx is out of
1498 * bounds, %NULL is returned.
1501 gnutls_cipher_suite_info (size_t idx,
1503 gnutls_kx_algorithm_t * kx,
1504 gnutls_cipher_algorithm_t * cipher,
1505 gnutls_mac_algorithm_t * mac,
1506 gnutls_protocol_t * min_version)
1508 if (idx >= CIPHER_SUITES_COUNT)
1512 memcpy (cs_id, cs_algorithms[idx].id.suite, 2);
1514 *kx = cs_algorithms[idx].kx_algorithm;
1516 *cipher = cs_algorithms[idx].block_algorithm;
1518 *mac = cs_algorithms[idx].mac_algorithm;
1520 *min_version = cs_algorithms[idx].min_version;
1522 return cs_algorithms[idx].name + sizeof ("GNU") - 1;
1527 _gnutls_cipher_suite_is_ok (cipher_suite_st * suite)
1530 const char *name = NULL;
1532 GNUTLS_CIPHER_SUITE_ALG_LOOP (name = p->name);
1541 #define SWAP(x, y) memcpy(tmp,x,size); \
1545 #define MAX_ELEM_SIZE 4
1547 _gnutls_partition (gnutls_session_t session, void *_base,
1548 size_t nmemb, size_t size,
1549 int (*compar) (gnutls_session_t,
1550 const void *, const void *))
1552 uint8_t *base = _base;
1553 uint8_t tmp[MAX_ELEM_SIZE];
1554 uint8_t ptmp[MAX_ELEM_SIZE];
1560 j = full = (nmemb - 1) * size;
1562 memcpy (ptmp, &base[0], size); /* set pivot item */
1566 while ((compar (session, &base[i], ptmp) <= 0) && (i < full))
1570 while ((compar (session, &base[j], ptmp) >= 0) && (j > 0))
1575 SWAP (&base[j], &base[i]);
1581 SWAP (&base[pivot], &base[j]);
1586 SWAP (&base[pivot], &base[i]);
1589 return pivot / size;
1593 _gnutls_qsort (gnutls_session_t session, void *_base, size_t nmemb,
1594 size_t size, int (*compar) (gnutls_session_t, const void *,
1599 size_t snmemb = nmemb;
1602 if (size > MAX_ELEM_SIZE)
1605 _gnutls_debug_log ("QSORT BUG\n");
1612 pivot = _gnutls_partition (session, _base, nmemb, size, compar);
1614 _gnutls_qsort (session, base, pivot < nmemb ? pivot + 1 : pivot, size,
1616 _gnutls_qsort (session, &base[(pivot + 1) * size], nmemb - pivot - 1,
1621 /* a compare function for KX algorithms (using priorities).
1622 * For use with qsort
1625 _gnutls_compare_algo (gnutls_session_t session, const void *i_A1,
1628 gnutls_kx_algorithm_t kA1 =
1629 _gnutls_cipher_suite_get_kx_algo ((const cipher_suite_st *) i_A1);
1630 gnutls_kx_algorithm_t kA2 =
1631 _gnutls_cipher_suite_get_kx_algo ((const cipher_suite_st *) i_A2);
1632 gnutls_cipher_algorithm_t cA1 =
1633 _gnutls_cipher_suite_get_cipher_algo ((const cipher_suite_st *) i_A1);
1634 gnutls_cipher_algorithm_t cA2 =
1635 _gnutls_cipher_suite_get_cipher_algo ((const cipher_suite_st *) i_A2);
1636 gnutls_mac_algorithm_t mA1 =
1637 _gnutls_cipher_suite_get_mac_algo ((const cipher_suite_st *) i_A1);
1638 gnutls_mac_algorithm_t mA2 =
1639 _gnutls_cipher_suite_get_mac_algo ((const cipher_suite_st *) i_A2);
1641 int p1 = (_gnutls_kx_priority (session, kA1) + 1) * 64;
1642 int p2 = (_gnutls_kx_priority (session, kA2) + 1) * 64;
1643 p1 += (_gnutls_cipher_priority (session, cA1) + 1) * 8;
1644 p2 += (_gnutls_cipher_priority (session, cA2) + 1) * 8;
1645 p1 += _gnutls_mac_priority (session, mA1);
1646 p2 += _gnutls_mac_priority (session, mA2);
1664 _gnutls_bsort (gnutls_session_t session, void *_base, size_t nmemb,
1665 size_t size, int (*compar) (gnutls_session_t, const void *,
1669 int full = nmemb * size;
1671 char tmp[MAX_ELEM_SIZE];
1673 for (i = 0; i < full; i += size)
1675 for (j = 0; j < full; j += size)
1677 if (compar (session, &base[i], &base[j]) < 0)
1679 SWAP (&base[j], &base[i]);
1688 _gnutls_supported_ciphersuites_sorted (gnutls_session_t session,
1689 cipher_suite_st ** ciphers)
1697 count = _gnutls_supported_ciphersuites (session, ciphers);
1704 _gnutls_debug_log ("Unsorted: \n");
1705 for (i = 0; i < count; i++)
1706 _gnutls_debug_log ("\t%d: %s\n", i,
1707 _gnutls_cipher_suite_get_name ((*ciphers)[i]));
1710 _gnutls_qsort (session, *ciphers, count,
1711 sizeof (cipher_suite_st), _gnutls_compare_algo);
1714 _gnutls_debug_log ("Sorted: \n");
1715 for (i = 0; i < count; i++)
1716 _gnutls_debug_log ("\t%d: %s\n", i,
1717 _gnutls_cipher_suite_get_name ((*ciphers)[i]));
1724 _gnutls_supported_ciphersuites (gnutls_session_t session,
1725 cipher_suite_st ** _ciphers)
1728 unsigned int i, ret_count, j;
1729 unsigned int count = CIPHER_SUITES_COUNT;
1730 cipher_suite_st *tmp_ciphers;
1731 cipher_suite_st *ciphers;
1732 gnutls_protocol_t version;
1739 tmp_ciphers = gnutls_malloc (count * sizeof (cipher_suite_st));
1740 if (tmp_ciphers == NULL)
1741 return GNUTLS_E_MEMORY_ERROR;
1743 ciphers = gnutls_malloc (count * sizeof (cipher_suite_st));
1744 if (ciphers == NULL)
1746 gnutls_free (tmp_ciphers);
1747 return GNUTLS_E_MEMORY_ERROR;
1750 version = gnutls_protocol_get_version (session);
1752 for (i = 0; i < count; i++)
1754 memcpy (&tmp_ciphers[i], &cs_algorithms[i].id,
1755 sizeof (cipher_suite_st));
1758 for (i = j = 0; i < count; i++)
1760 /* remove private cipher suites, if requested.
1762 if (tmp_ciphers[i].suite[0] == 0xFF &&
1763 session->internals.enable_private == 0)
1766 /* remove cipher suites which do not support the
1767 * protocol version used.
1769 if (_gnutls_cipher_suite_is_version_supported (&tmp_ciphers[i], version)
1773 if (_gnutls_kx_priority
1774 (session, _gnutls_cipher_suite_get_kx_algo (&tmp_ciphers[i])) < 0)
1776 if (_gnutls_mac_priority
1777 (session, _gnutls_cipher_suite_get_mac_algo (&tmp_ciphers[i])) < 0)
1779 if (_gnutls_cipher_priority
1781 _gnutls_cipher_suite_get_cipher_algo (&tmp_ciphers[i])) < 0)
1784 memcpy (&ciphers[j], &tmp_ciphers[i], sizeof (cipher_suite_st));
1790 #if 0 /* expensive */
1791 if (ret_count > 0 && ret_count != count)
1794 gnutls_realloc_fast (ciphers, ret_count * sizeof (cipher_suite_st));
1798 if (ret_count != count)
1800 gnutls_free (ciphers);
1806 gnutls_free (tmp_ciphers);
1808 /* This function can no longer return 0 cipher suites.
1809 * It returns an error code instead.
1814 gnutls_free (ciphers);
1815 return GNUTLS_E_NO_CIPHER_SUITES;
1817 *_ciphers = ciphers;
1822 * gnutls_certificate_type_get_name:
1823 * @type: is a certificate type
1825 * Convert a #gnutls_certificate_type_t type to a string.
1827 * Returns: a string that contains the name of the specified
1828 * certificate type, or %NULL in case of unknown types.
1831 gnutls_certificate_type_get_name (gnutls_certificate_type_t type)
1833 const char *ret = NULL;
1835 if (type == GNUTLS_CRT_X509)
1837 if (type == GNUTLS_CRT_OPENPGP)
1844 * gnutls_certificate_type_get_id:
1845 * @name: is a certificate type name
1847 * The names are compared in a case insensitive way.
1849 * Returns: a #gnutls_certificate_type_t for the specified in a
1850 * string certificate type, or %GNUTLS_CRT_UNKNOWN on error.
1852 gnutls_certificate_type_t
1853 gnutls_certificate_type_get_id (const char *name)
1855 gnutls_certificate_type_t ret = GNUTLS_CRT_UNKNOWN;
1857 if (strcasecmp (name, "X.509") == 0 || strcasecmp (name, "X509") == 0)
1858 return GNUTLS_CRT_X509;
1859 if (strcasecmp (name, "OPENPGP") == 0)
1860 return GNUTLS_CRT_OPENPGP;
1865 static const gnutls_certificate_type_t supported_certificate_types[] = {
1872 * gnutls_certificate_type_list:
1874 * Get a list of certificate types. Note that to be able to use
1875 * OpenPGP certificates, you must link to libgnutls-extra and call
1876 * gnutls_global_init_extra().
1878 * Returns: a zero-terminated list of #gnutls_certificate_type_t
1879 * integers indicating the available certificate types.
1881 const gnutls_certificate_type_t *
1882 gnutls_certificate_type_list (void)
1884 return supported_certificate_types;
1887 /* returns the gnutls_pk_algorithm_t which is compatible with
1888 * the given gnutls_kx_algorithm_t.
1890 gnutls_pk_algorithm_t
1891 _gnutls_map_pk_get_pk (gnutls_kx_algorithm_t kx_algorithm)
1893 gnutls_pk_algorithm_t ret = -1;
1895 GNUTLS_PK_MAP_ALG_LOOP (ret = p->pk_algorithm) return ret;
1898 /* Returns the encipher type for the given key exchange algorithm.
1899 * That one of CIPHER_ENCRYPT, CIPHER_SIGN, CIPHER_IGN.
1901 * ex. GNUTLS_KX_RSA requires a certificate able to encrypt... so returns CIPHER_ENCRYPT.
1904 _gnutls_kx_encipher_type (gnutls_kx_algorithm_t kx_algorithm)
1906 int ret = CIPHER_IGN;
1907 GNUTLS_PK_MAP_ALG_LOOP (ret = p->encipher_type) return ret;
1911 /* signature algorithms;
1913 struct gnutls_sign_entry
1917 gnutls_sign_algorithm_t id;
1918 gnutls_pk_algorithm_t pk;
1919 gnutls_mac_algorithm_t mac;
1920 /* See RFC 5246 HashAlgorithm and SignatureAlgorithm
1921 for values to use in aid struct. */
1922 const sign_algorithm_st aid;
1924 typedef struct gnutls_sign_entry gnutls_sign_entry;
1926 #define TLS_SIGN_AID_UNKNOWN {255, 255}
1927 static const sign_algorithm_st unknown_tls_aid = TLS_SIGN_AID_UNKNOWN;
1929 static const gnutls_sign_entry sign_algorithms[] = {
1930 {"RSA-SHA1", SIG_RSA_SHA1_OID, GNUTLS_SIGN_RSA_SHA1, GNUTLS_PK_RSA,
1931 GNUTLS_MAC_SHA1, {2, 1}},
1932 {"RSA-SHA224", SIG_RSA_SHA224_OID, GNUTLS_SIGN_RSA_SHA224, GNUTLS_PK_RSA,
1933 GNUTLS_MAC_SHA224, {3, 1}},
1934 {"RSA-SHA256", SIG_RSA_SHA256_OID, GNUTLS_SIGN_RSA_SHA256, GNUTLS_PK_RSA,
1935 GNUTLS_MAC_SHA256, {4, 1}},
1936 {"RSA-SHA384", SIG_RSA_SHA384_OID, GNUTLS_SIGN_RSA_SHA384, GNUTLS_PK_RSA,
1937 GNUTLS_MAC_SHA384, {5, 1}},
1938 {"RSA-SHA512", SIG_RSA_SHA512_OID, GNUTLS_SIGN_RSA_SHA512, GNUTLS_PK_RSA,
1939 GNUTLS_MAC_SHA512, {6, 1}},
1940 {"RSA-RMD160", SIG_RSA_RMD160_OID, GNUTLS_SIGN_RSA_RMD160, GNUTLS_PK_RSA,
1941 GNUTLS_MAC_RMD160, TLS_SIGN_AID_UNKNOWN},
1942 {"DSA-SHA1", SIG_DSA_SHA1_OID, GNUTLS_SIGN_DSA_SHA1, GNUTLS_PK_DSA,
1943 GNUTLS_MAC_SHA1, {2, 2}},
1944 {"DSA-SHA224", SIG_DSA_SHA224_OID, GNUTLS_SIGN_DSA_SHA224, GNUTLS_PK_DSA,
1945 GNUTLS_MAC_SHA224, {3, 2}},
1946 {"DSA-SHA256", SIG_DSA_SHA256_OID, GNUTLS_SIGN_DSA_SHA256, GNUTLS_PK_DSA,
1947 GNUTLS_MAC_SHA256, {4, 2}},
1948 {"RSA-MD5", SIG_RSA_MD5_OID, GNUTLS_SIGN_RSA_MD5, GNUTLS_PK_RSA,
1949 GNUTLS_MAC_MD5, {1, 1}},
1950 {"RSA-MD2", SIG_RSA_MD2_OID, GNUTLS_SIGN_RSA_MD2, GNUTLS_PK_RSA,
1951 GNUTLS_MAC_MD2, TLS_SIGN_AID_UNKNOWN},
1952 {"GOST R 34.10-2001", SIG_GOST_R3410_2001_OID, 0, 0, 0,
1953 TLS_SIGN_AID_UNKNOWN},
1954 {"GOST R 34.10-94", SIG_GOST_R3410_94_OID, 0, 0, 0, TLS_SIGN_AID_UNKNOWN},
1955 {0, 0, 0, 0, 0, TLS_SIGN_AID_UNKNOWN}
1958 /* Keep the contents of this struct the same as the previous one. */
1959 static const gnutls_sign_algorithm_t supported_sign[] = {
1960 GNUTLS_SIGN_RSA_SHA1,
1961 GNUTLS_SIGN_RSA_SHA224,
1962 GNUTLS_SIGN_RSA_SHA256,
1963 GNUTLS_SIGN_RSA_SHA384,
1964 GNUTLS_SIGN_RSA_SHA512,
1965 GNUTLS_SIGN_RSA_RMD160,
1966 GNUTLS_SIGN_DSA_SHA1,
1967 GNUTLS_SIGN_DSA_SHA224,
1968 GNUTLS_SIGN_DSA_SHA256,
1969 GNUTLS_SIGN_RSA_MD5,
1970 GNUTLS_SIGN_RSA_MD2,
1974 #define GNUTLS_SIGN_LOOP(b) \
1976 const gnutls_sign_entry *p; \
1977 for(p = sign_algorithms; p->name != NULL; p++) { b ; } \
1980 #define GNUTLS_SIGN_ALG_LOOP(a) \
1981 GNUTLS_SIGN_LOOP( if(p->id && p->id == sign) { a; break; } )
1984 * gnutls_sign_algorithm_get_name:
1985 * @sign: is a sign algorithm
1987 * Convert a #gnutls_sign_algorithm_t value to a string.
1989 * Returns: a string that contains the name of the specified sign
1990 * algorithm, or %NULL.
1993 gnutls_sign_algorithm_get_name (gnutls_sign_algorithm_t sign)
1995 const char *ret = NULL;
1998 GNUTLS_SIGN_ALG_LOOP (ret = p->name);
2006 * Get a list of supported public key signature algorithms.
2008 * Returns: a zero-terminated list of #gnutls_sign_algorithm_t
2009 * integers indicating the available ciphers.
2012 const gnutls_sign_algorithm_t *
2013 gnutls_sign_list (void)
2015 return supported_sign;
2019 * gnutls_sign_get_id:
2020 * @name: is a MAC algorithm name
2022 * The names are compared in a case insensitive way.
2024 * Returns: return a #gnutls_sign_algorithm_t value corresponding to
2025 * the specified cipher, or %GNUTLS_SIGN_UNKNOWN on error.
2027 gnutls_sign_algorithm_t
2028 gnutls_sign_get_id (const char *name)
2030 gnutls_sign_algorithm_t ret = GNUTLS_SIGN_UNKNOWN;
2032 GNUTLS_SIGN_LOOP (if (strcasecmp (p->name, name) == 0) ret = p->id);
2039 * gnutls_sign_get_name:
2040 * @algorithm: is a public key signature algorithm
2042 * Convert a #gnutls_sign_algorithm_t value to a string.
2044 * Returns: a pointer to a string that contains the name of the
2045 * specified public key signature algorithm, or %NULL.
2050 gnutls_sign_get_name (gnutls_sign_algorithm_t algorithm)
2052 const char *ret = "SIGN_UNKNOWN";
2054 GNUTLS_SIGN_LOOP (if (p->id == algorithm) ret = p->name);
2059 gnutls_sign_algorithm_t
2060 _gnutls_x509_oid2sign_algorithm (const char *oid)
2062 gnutls_sign_algorithm_t ret = 0;
2064 GNUTLS_SIGN_LOOP (if (p->oid && strcmp (oid, p->oid) == 0)
2066 ret = p->id; break;}
2071 _gnutls_x509_log ("Unknown SIGN OID: '%s'\n", oid);
2072 return GNUTLS_SIGN_UNKNOWN;
2077 gnutls_sign_algorithm_t
2078 _gnutls_x509_pk_to_sign (gnutls_pk_algorithm_t pk, gnutls_mac_algorithm_t mac)
2080 gnutls_sign_algorithm_t ret = 0;
2082 GNUTLS_SIGN_LOOP (if (pk == p->pk && mac == p->mac)
2084 ret = p->id; break;}
2088 return GNUTLS_SIGN_UNKNOWN;
2093 _gnutls_x509_sign_to_oid (gnutls_pk_algorithm_t pk,
2094 gnutls_mac_algorithm_t mac)
2096 gnutls_sign_algorithm_t sign;
2097 const char *ret = NULL;
2099 sign = _gnutls_x509_pk_to_sign (pk, mac);
2100 if (sign == GNUTLS_SIGN_UNKNOWN)
2103 GNUTLS_SIGN_ALG_LOOP (ret = p->oid);
2107 gnutls_mac_algorithm_t
2108 _gnutls_sign_get_hash_algorithm (gnutls_sign_algorithm_t sign)
2110 gnutls_mac_algorithm_t ret = GNUTLS_DIG_UNKNOWN;
2112 GNUTLS_SIGN_ALG_LOOP (ret = p->mac);
2117 gnutls_pk_algorithm_t
2118 _gnutls_sign_get_pk_algorithm (gnutls_sign_algorithm_t sign)
2120 gnutls_pk_algorithm_t ret = GNUTLS_PK_UNKNOWN;
2122 GNUTLS_SIGN_ALG_LOOP (ret = p->pk);
2127 gnutls_sign_algorithm_t
2128 _gnutls_tls_aid_to_sign (const sign_algorithm_st * aid)
2130 gnutls_sign_algorithm_t ret = GNUTLS_SIGN_UNKNOWN;
2132 if (memcmp(aid, &unknown_tls_aid, sizeof(*aid))==0)
2135 GNUTLS_SIGN_LOOP (if (p->aid.hash_algorithm == aid->hash_algorithm
2136 && p->aid.sign_algorithm == aid->sign_algorithm)
2146 /* Returns NULL if a valid AID is not found
2148 const sign_algorithm_st*
2149 _gnutls_sign_to_tls_aid (gnutls_sign_algorithm_t sign)
2151 const sign_algorithm_st * ret = NULL;
2153 GNUTLS_SIGN_ALG_LOOP (ret = &p->aid);
2155 if (ret != NULL && memcmp(ret, &unknown_tls_aid, sizeof(*ret))==0)
2165 struct gnutls_pk_entry
2169 gnutls_pk_algorithm_t id;
2171 typedef struct gnutls_pk_entry gnutls_pk_entry;
2173 static const gnutls_pk_entry pk_algorithms[] = {
2174 /* having duplicate entries is ok, as long as the one
2175 * we want to return OID from is first */
2176 {"UNKNOWN", NULL, GNUTLS_PK_UNKNOWN},
2177 {"RSA", PK_PKIX1_RSA_OID, GNUTLS_PK_RSA},
2178 {"RSA (X.509)", PK_X509_RSA_OID, GNUTLS_PK_RSA}, /* some certificates use this OID for RSA */
2179 {"RSA (MD5)", SIG_RSA_MD5_OID, GNUTLS_PK_RSA}, /* some other broken certificates set RSA with MD5 as an indicator of RSA */
2180 {"RSA (SHA1)", SIG_RSA_SHA1_OID, GNUTLS_PK_RSA}, /* some other broken certificates set RSA with SHA1 as an indicator of RSA */
2181 {"DSA", PK_DSA_OID, GNUTLS_PK_DSA},
2182 {"GOST R 34.10-2001", PK_GOST_R3410_2001_OID, GNUTLS_PK_UNKNOWN},
2183 {"GOST R 34.10-94", PK_GOST_R3410_94_OID, GNUTLS_PK_UNKNOWN},
2188 * gnutls_pk_algorithm_get_name:
2189 * @algorithm: is a pk algorithm
2191 * Convert a #gnutls_pk_algorithm_t value to a string.
2193 * Returns: a string that contains the name of the specified public
2194 * key algorithm, or %NULL.
2197 gnutls_pk_algorithm_get_name (gnutls_pk_algorithm_t algorithm)
2199 const char *ret = NULL;
2200 const gnutls_pk_entry *p;
2202 for (p = pk_algorithms; p->name != NULL; p++)
2203 if (p->id == algorithm)
2215 * Get a list of supported public key algorithms.
2217 * Returns: a zero-terminated list of #gnutls_pk_algorithm_t integers
2218 * indicating the available ciphers.
2222 const gnutls_pk_algorithm_t *
2223 gnutls_pk_list (void)
2225 static const gnutls_pk_algorithm_t supported_pks[] = {
2228 /* GNUTLS_PK_DH is not returned because it is not
2229 * a real public key algorithm. I.e. cannot be used
2230 * as a public key algorithm of a certificate.
2235 return supported_pks;
2240 * @name: is a string containing a public key algorithm name.
2242 * Convert a string to a #gnutls_pk_algorithm_t value. The names are
2243 * compared in a case insensitive way. For example,
2244 * gnutls_pk_get_id("RSA") will return %GNUTLS_PK_RSA.
2246 * Returns: a #gnutls_pk_algorithm_t id of the specified public key
2247 * algorithm string, or %GNUTLS_PK_UNKNOWN on failures.
2251 gnutls_pk_algorithm_t
2252 gnutls_pk_get_id (const char *name)
2254 gnutls_pk_algorithm_t ret = GNUTLS_PK_UNKNOWN;
2255 const gnutls_pk_entry *p;
2257 for (p = pk_algorithms; p->name != NULL; p++)
2258 if (name && strcmp (p->name, name) == 0)
2268 * gnutls_pk_get_name:
2269 * @algorithm: is a public key algorithm
2271 * Convert a #gnutls_pk_algorithm_t value to a string.
2273 * Returns: a pointer to a string that contains the name of the
2274 * specified public key algorithm, or %NULL.
2279 gnutls_pk_get_name (gnutls_pk_algorithm_t algorithm)
2281 const char *ret = "Unknown";
2282 const gnutls_pk_entry *p;
2284 for (p = pk_algorithms; p->name != NULL; p++)
2285 if (algorithm == p->id)
2294 gnutls_pk_algorithm_t
2295 _gnutls_x509_oid2pk_algorithm (const char *oid)
2297 gnutls_pk_algorithm_t ret = GNUTLS_PK_UNKNOWN;
2298 const gnutls_pk_entry *p;
2300 for (p = pk_algorithms; p->name != NULL; p++)
2301 if (p->oid && strcmp (p->oid, oid) == 0)
2311 _gnutls_x509_pk_to_oid (gnutls_pk_algorithm_t algorithm)
2313 const char *ret = NULL;
2314 const gnutls_pk_entry *p;
2316 for (p = pk_algorithms; p->name != NULL; p++)
2317 if (p->id == algorithm)
2327 * gnutls_sec_param_to_pk_bits:
2328 * @algo: is a public key algorithm
2329 * @param: is a security parameter
2331 * When generating private and public key pairs a difficult question
2332 * is which size of "bits" the modulus will be in RSA and the group size
2333 * in DSA. The easy answer is 1024, which is also wrong. This function
2334 * will convert a human understandable security parameter to an
2335 * appropriate size for the specific algorithm.
2337 * Returns: The number of bits, or zero.
2341 gnutls_sec_param_to_pk_bits (gnutls_pk_algorithm_t algo,
2342 gnutls_sec_param_t param)
2344 unsigned int ret = 0;
2346 /* handle DSA differently */
2347 if (algo == GNUTLS_PK_DSA)
2349 GNUTLS_SEC_PARAM_LOOP (if (p->sec_param == param)
2351 ret = p->dsa_bits; break;}
2356 GNUTLS_SEC_PARAM_LOOP (if (p->sec_param == param)
2358 ret = p->pk_bits; break;}
2364 /* Returns the corresponding size for subgroup bits (q),
2365 * given the group bits (p).
2368 _gnutls_pk_bits_to_subgroup_bits (unsigned int pk_bits)
2370 unsigned int ret = 0;
2372 GNUTLS_SEC_PARAM_LOOP (if (p->pk_bits >= pk_bits)
2374 ret = p->subgroup_bits; break;}
2381 * gnutls_sec_param_get_name:
2382 * @param: is a security parameter
2384 * Convert a #gnutls_sec_param_t value to a string.
2386 * Returns: a pointer to a string that contains the name of the
2387 * specified public key algorithm, or %NULL.
2391 gnutls_sec_param_get_name (gnutls_sec_param_t param)
2393 const char *ret = "Unknown";
2395 GNUTLS_SEC_PARAM_LOOP (if (p->sec_param == param)
2397 ret = p->name; break;}
2404 * gnutls_pk_bits_to_sec_param:
2405 * @algo: is a public key algorithm
2406 * @bits: is the number of bits
2408 * This is the inverse of gnutls_sec_param_to_pk_bits(). Given an algorithm
2409 * and the number of bits, it will return the security parameter. This is
2410 * a rough indication.
2412 * Returns: The security parameter.
2416 gnutls_pk_bits_to_sec_param (gnutls_pk_algorithm_t algo, unsigned int bits)
2418 gnutls_sec_param_t ret = GNUTLS_SEC_PARAM_WEAK;
2420 GNUTLS_SEC_PARAM_LOOP (if (p->pk_bits > bits)
2423 ret = p->sec_param;);