2 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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 3 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 License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
26 #include <auth/cert.h>
27 #include <algorithms.h>
28 #include <gnutls_datum.h>
29 #include <gnutls_mpi.h>
30 #include <gnutls_global.h>
31 #include <gnutls_pk.h>
33 #include <gnutls_buffers.h>
34 #include <gnutls_sig.h>
35 #include <gnutls_kx.h>
37 #include <ext/signature.h>
38 #include <gnutls_state.h>
39 #include <x509/common.h>
40 #include <abstract_int.h>
43 sign_tls_hash (gnutls_session_t session
, gnutls_digest_algorithm_t hash_algo
,
44 gnutls_pcert_st
* cert
, gnutls_privkey_t pkey
,
45 const gnutls_datum_t
* hash_concat
,
46 gnutls_datum_t
* signature
);
49 /* While this is currently equal to the length of RSA/SHA512
50 * signature, it should also be sufficient for DSS signature and any
51 * other RSA signatures including one with the old MD5/SHA1-combined
54 #define MAX_SIG_SIZE 19 + MAX_HASH_SIZE
56 /* Generates a signature of all the random data and the parameters.
57 * Used in DHE_* ciphersuites.
60 _gnutls_handshake_sign_data (gnutls_session_t session
, gnutls_pcert_st
* cert
,
61 gnutls_privkey_t pkey
, gnutls_datum_t
* params
,
62 gnutls_datum_t
* signature
,
63 gnutls_sign_algorithm_t
* sign_algo
)
65 gnutls_datum_t dconcat
;
68 uint8_t concat
[MAX_SIG_SIZE
];
69 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
70 gnutls_digest_algorithm_t hash_algo
;
73 _gnutls_session_get_sign_algo (session
, cert
);
74 if (*sign_algo
== GNUTLS_SIGN_UNKNOWN
)
77 return GNUTLS_E_UNKNOWN_PK_ALGORITHM
;
80 gnutls_sign_algorithm_set(session
, *sign_algo
);
82 hash_algo
= gnutls_sign_get_hash_algorithm (*sign_algo
);
84 _gnutls_handshake_log ("HSK[%p]: signing handshake data: using %s\n",
85 session
, gnutls_sign_algorithm_get_name (*sign_algo
));
87 ret
= _gnutls_hash_init (&td_sha
, hash_algo
);
94 _gnutls_hash (&td_sha
, session
->security_parameters
.client_random
,
96 _gnutls_hash (&td_sha
, session
->security_parameters
.server_random
,
98 _gnutls_hash (&td_sha
, params
->data
, params
->size
);
100 switch (gnutls_privkey_get_pk_algorithm(pkey
, NULL
))
103 if (!_gnutls_version_has_selectable_sighash (ver
))
107 ret
= _gnutls_hash_init (&td_md5
, GNUTLS_MAC_MD5
);
114 _gnutls_hash (&td_md5
, session
->security_parameters
.client_random
,
116 _gnutls_hash (&td_md5
, session
->security_parameters
.server_random
,
118 _gnutls_hash (&td_md5
, params
->data
, params
->size
);
120 _gnutls_hash_deinit (&td_md5
, concat
);
121 _gnutls_hash_deinit (&td_sha
, &concat
[16]);
123 dconcat
.data
= concat
;
129 _gnutls_hash_deinit (&td_sha
, concat
);
131 dconcat
.data
= concat
;
132 dconcat
.size
= _gnutls_hash_get_algo_len (hash_algo
);
137 _gnutls_hash_deinit (&td_sha
, concat
);
139 if (!IS_SHA(hash_algo
))
142 return GNUTLS_E_INTERNAL_ERROR
;
144 dconcat
.data
= concat
;
145 dconcat
.size
= _gnutls_hash_get_algo_len (hash_algo
);
150 _gnutls_hash_deinit (&td_sha
, NULL
);
151 return GNUTLS_E_INTERNAL_ERROR
;
154 ret
= sign_tls_hash (session
, hash_algo
, cert
, pkey
, &dconcat
, signature
);
164 /* This will create a PKCS1 or DSA signature, as defined in the TLS protocol.
165 * Cert is the certificate of the corresponding private key. It is only checked if
166 * it supports signing.
169 sign_tls_hash (gnutls_session_t session
, gnutls_digest_algorithm_t hash_algo
,
170 gnutls_pcert_st
* cert
, gnutls_privkey_t pkey
,
171 const gnutls_datum_t
* hash_concat
,
172 gnutls_datum_t
* signature
)
174 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
175 unsigned int key_usage
= 0;
176 /* If our certificate supports signing
181 gnutls_pubkey_get_key_usage(cert
->pubkey
, &key_usage
);
184 if (!(key_usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
))
187 _gnutls_audit_log(session
, "Peer's certificate does not allow digital signatures. Key usage violation detected (ignored).\n");
190 /* External signing. Deprecated. To be removed. */
195 if (!session
->internals
.sign_func
)
196 return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS
);
198 if (!_gnutls_version_has_selectable_sighash (ver
))
199 return (*session
->internals
.sign_func
)
200 (session
, session
->internals
.sign_func_userdata
,
201 cert
->type
, &cert
->cert
, hash_concat
, signature
);
204 gnutls_datum_t digest
;
206 ret
= _gnutls_set_datum(&digest
, hash_concat
->data
, hash_concat
->size
);
208 return gnutls_assert_val(ret
);
210 ret
= pk_prepare_hash (gnutls_privkey_get_pk_algorithm(pkey
, NULL
), hash_algo
, &digest
);
217 ret
= (*session
->internals
.sign_func
)
218 (session
, session
->internals
.sign_func_userdata
,
219 cert
->type
, &cert
->cert
, &digest
, signature
);
221 gnutls_free(digest
.data
);
228 if (!_gnutls_version_has_selectable_sighash (ver
))
229 return _gnutls_privkey_sign_hash (pkey
, hash_concat
, signature
);
231 return gnutls_privkey_sign_hash (pkey
, hash_algo
, 0, hash_concat
, signature
);
235 verify_tls_hash (gnutls_session_t session
,
236 gnutls_protocol_t ver
, gnutls_pcert_st
* cert
,
237 const gnutls_datum_t
* hash_concat
,
238 gnutls_datum_t
* signature
, size_t sha1pos
,
239 gnutls_sign_algorithm_t sign_algo
,
240 gnutls_pk_algorithm_t pk_algo
)
243 gnutls_datum_t vdata
;
244 unsigned int key_usage
= 0, flags
;
249 return GNUTLS_E_CERTIFICATE_ERROR
;
252 gnutls_pubkey_get_key_usage(cert
->pubkey
, &key_usage
);
254 /* If the certificate supports signing continue.
257 if (!(key_usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
))
260 _gnutls_audit_log(session
, "Peer's certificate does not allow digital signatures. Key usage violation detected (ignored).\n");
263 if (pk_algo
== GNUTLS_PK_UNKNOWN
)
264 pk_algo
= gnutls_pubkey_get_pk_algorithm(cert
->pubkey
, NULL
);
269 vdata
.data
= hash_concat
->data
;
270 vdata
.size
= hash_concat
->size
;
272 /* verify signature */
273 if (!_gnutls_version_has_selectable_sighash (ver
))
274 flags
= GNUTLS_PUBKEY_VERIFY_FLAG_TLS_RSA
;
280 vdata
.data
= &hash_concat
->data
[sha1pos
];
281 vdata
.size
= hash_concat
->size
- sha1pos
;
288 return GNUTLS_E_INTERNAL_ERROR
;
291 ret
= gnutls_pubkey_verify_hash2(cert
->pubkey
, sign_algo
, flags
,
295 return gnutls_assert_val(ret
);
302 /* Generates a signature of all the random data and the parameters.
303 * Used in DHE_* ciphersuites.
306 _gnutls_handshake_verify_data (gnutls_session_t session
, gnutls_pcert_st
* cert
,
307 const gnutls_datum_t
* params
,
308 gnutls_datum_t
* signature
,
309 gnutls_sign_algorithm_t sign_algo
)
311 gnutls_datum_t dconcat
;
315 uint8_t concat
[MAX_SIG_SIZE
];
316 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
317 gnutls_digest_algorithm_t hash_algo
;
319 if (_gnutls_version_has_selectable_sighash (ver
))
321 _gnutls_handshake_log ("HSK[%p]: verify handshake data: using %s\n",
322 session
, gnutls_sign_algorithm_get_name (sign_algo
));
324 ret
= _gnutls_pubkey_compatible_with_sig(session
, cert
->pubkey
, ver
, sign_algo
);
326 return gnutls_assert_val(ret
);
328 ret
= _gnutls_session_sign_algo_enabled (session
, sign_algo
);
330 return gnutls_assert_val(ret
);
332 hash_algo
= gnutls_sign_get_hash_algorithm (sign_algo
);
336 ret
= _gnutls_hash_init (&td_md5
, GNUTLS_MAC_MD5
);
343 _gnutls_hash (&td_md5
, session
->security_parameters
.client_random
,
345 _gnutls_hash (&td_md5
, session
->security_parameters
.server_random
,
347 _gnutls_hash (&td_md5
, params
->data
, params
->size
);
349 hash_algo
= GNUTLS_DIG_SHA1
;
352 ret
= _gnutls_hash_init (&td_sha
, hash_algo
);
356 if (!_gnutls_version_has_selectable_sighash (ver
))
357 _gnutls_hash_deinit (&td_md5
, NULL
);
361 _gnutls_hash (&td_sha
, session
->security_parameters
.client_random
,
363 _gnutls_hash (&td_sha
, session
->security_parameters
.server_random
,
365 _gnutls_hash (&td_sha
, params
->data
, params
->size
);
367 if (!_gnutls_version_has_selectable_sighash (ver
))
369 _gnutls_hash_deinit (&td_md5
, concat
);
370 _gnutls_hash_deinit (&td_sha
, &concat
[16]);
371 dconcat
.data
= concat
;
376 _gnutls_hash_deinit (&td_sha
, concat
);
378 dconcat
.data
= concat
;
379 dconcat
.size
= _gnutls_hash_get_algo_len (hash_algo
);
382 ret
= verify_tls_hash (session
, ver
, cert
, &dconcat
, signature
,
383 dconcat
.size
- _gnutls_hash_get_algo_len (hash_algo
),
384 sign_algo
, gnutls_sign_get_pk_algorithm (sign_algo
));
395 /* Client certificate verify calculations
398 /* this is _gnutls_handshake_verify_crt_vrfy for TLS 1.2
401 _gnutls_handshake_verify_crt_vrfy12 (gnutls_session_t session
,
402 gnutls_pcert_st
* cert
,
403 gnutls_datum_t
* signature
,
404 gnutls_sign_algorithm_t sign_algo
)
407 uint8_t concat
[MAX_HASH_SIZE
];
408 gnutls_datum_t dconcat
;
409 gnutls_digest_algorithm_t hash_algo
;
410 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
411 gnutls_pk_algorithm_t pk
= gnutls_pubkey_get_pk_algorithm(cert
->pubkey
, NULL
);
413 ret
= _gnutls_session_sign_algo_enabled(session
, sign_algo
);
415 return gnutls_assert_val(ret
);
417 hash_algo
= gnutls_sign_get_hash_algorithm(sign_algo
);
419 ret
= _gnutls_hash_fast(hash_algo
, session
->internals
.handshake_hash_buffer
.data
,
420 session
->internals
.handshake_hash_buffer_prev_len
,
423 return gnutls_assert_val(ret
);
425 dconcat
.data
= concat
;
426 dconcat
.size
= _gnutls_hash_get_algo_len (hash_algo
);
429 verify_tls_hash (session
, ver
, cert
, &dconcat
, signature
, 0, sign_algo
, pk
);
440 /* Verifies a TLS signature (like the one in the client certificate
444 _gnutls_handshake_verify_crt_vrfy (gnutls_session_t session
,
445 gnutls_pcert_st
*cert
,
446 gnutls_datum_t
* signature
,
447 gnutls_sign_algorithm_t sign_algo
)
450 uint8_t concat
[MAX_SIG_SIZE
];
453 gnutls_datum_t dconcat
;
454 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
456 _gnutls_handshake_log ("HSK[%p]: verify cert vrfy: using %s\n",
457 session
, gnutls_sign_algorithm_get_name (sign_algo
));
460 if (_gnutls_version_has_selectable_sighash(ver
))
461 return _gnutls_handshake_verify_crt_vrfy12 (session
, cert
, signature
,
465 _gnutls_hash_init (&td_md5
, GNUTLS_DIG_MD5
);
473 _gnutls_hash_init (&td_sha
, GNUTLS_DIG_SHA1
);
477 _gnutls_hash_deinit (&td_md5
, NULL
);
478 return GNUTLS_E_HASH_FAILED
;
481 _gnutls_hash(&td_sha
, session
->internals
.handshake_hash_buffer
.data
, session
->internals
.handshake_hash_buffer_prev_len
);
482 _gnutls_hash(&td_md5
, session
->internals
.handshake_hash_buffer
.data
, session
->internals
.handshake_hash_buffer_prev_len
);
484 if (ver
== GNUTLS_SSL3
)
486 ret
= _gnutls_generate_master (session
, 1);
489 _gnutls_hash_deinit (&td_md5
, NULL
);
490 _gnutls_hash_deinit (&td_sha
, NULL
);
491 return gnutls_assert_val(ret
);
494 ret
= _gnutls_mac_deinit_ssl3_handshake (&td_md5
, concat
,
496 security_parameters
.master_secret
,
500 _gnutls_hash_deinit (&td_sha
, NULL
);
501 return gnutls_assert_val(ret
);
504 ret
= _gnutls_mac_deinit_ssl3_handshake (&td_sha
, &concat
[16],
506 security_parameters
.master_secret
,
510 return gnutls_assert_val(ret
);
515 _gnutls_hash_deinit (&td_md5
, concat
);
516 _gnutls_hash_deinit (&td_sha
, &concat
[16]);
519 dconcat
.data
= concat
;
520 dconcat
.size
= 20 + 16; /* md5+ sha */
523 verify_tls_hash (session
, ver
, cert
, &dconcat
, signature
, 16,
525 gnutls_pubkey_get_pk_algorithm(cert
->pubkey
, NULL
));
536 /* the same as _gnutls_handshake_sign_crt_vrfy except that it is made for TLS 1.2
539 _gnutls_handshake_sign_crt_vrfy12 (gnutls_session_t session
,
540 gnutls_pcert_st
* cert
, gnutls_privkey_t pkey
,
541 gnutls_datum_t
* signature
)
543 gnutls_datum_t dconcat
;
545 uint8_t concat
[MAX_SIG_SIZE
];
546 gnutls_sign_algorithm_t sign_algo
;
547 gnutls_digest_algorithm_t hash_algo
;
550 _gnutls_session_get_sign_algo (session
, cert
);
551 if (sign_algo
== GNUTLS_SIGN_UNKNOWN
)
554 return GNUTLS_E_UNKNOWN_PK_ALGORITHM
;
557 gnutls_sign_algorithm_set(session
, sign_algo
);
559 hash_algo
= gnutls_sign_get_hash_algorithm (sign_algo
);
561 _gnutls_debug_log ("sign handshake cert vrfy: picked %s with %s\n",
562 gnutls_sign_algorithm_get_name (sign_algo
),
563 gnutls_mac_get_name ((gnutls_mac_algorithm_t
)hash_algo
));
565 ret
= _gnutls_hash_fast (hash_algo
, session
->internals
.handshake_hash_buffer
.data
,
566 session
->internals
.handshake_hash_buffer
.length
,
569 return gnutls_assert_val(ret
);
571 dconcat
.data
= concat
;
572 dconcat
.size
= _gnutls_hash_get_algo_len (hash_algo
);
574 ret
= sign_tls_hash (session
, hash_algo
, cert
, pkey
, &dconcat
, signature
);
585 /* Generates a signature of all the previous sent packets in the
586 * handshake procedure.
587 * 20040227: now it works for SSL 3.0 as well
588 * 20091031: works for TLS 1.2 too!
590 * For TLS1.x, x<2 returns negative for failure and zero or unspecified for success.
591 * For TLS1.2 returns the signature algorithm used on success, or a negative error code;
594 _gnutls_handshake_sign_crt_vrfy (gnutls_session_t session
,
595 gnutls_pcert_st
* cert
, gnutls_privkey_t pkey
,
596 gnutls_datum_t
* signature
)
598 gnutls_datum_t dconcat
;
600 uint8_t concat
[MAX_SIG_SIZE
];
603 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
604 gnutls_pk_algorithm_t pk
= gnutls_privkey_get_pk_algorithm(pkey
, NULL
);
606 if (_gnutls_version_has_selectable_sighash(ver
))
607 return _gnutls_handshake_sign_crt_vrfy12 (session
, cert
, pkey
,
611 _gnutls_hash_init (&td_sha
, GNUTLS_DIG_SHA1
);
618 _gnutls_hash(&td_sha
, session
->internals
.handshake_hash_buffer
.data
, session
->internals
.handshake_hash_buffer
.length
);
620 if (ver
== GNUTLS_SSL3
)
622 ret
= _gnutls_generate_master (session
, 1);
626 _gnutls_hash_deinit (&td_sha
, NULL
);
630 ret
= _gnutls_mac_deinit_ssl3_handshake (&td_sha
, &concat
[16],
632 security_parameters
.master_secret
,
635 return gnutls_assert_val(ret
);
638 _gnutls_hash_deinit (&td_sha
, &concat
[16]);
640 /* ensure 1024 bit DSA keys are used */
641 ret
= _gnutls_pubkey_compatible_with_sig(session
, cert
->pubkey
, ver
, GNUTLS_SIGN_UNKNOWN
);
643 return gnutls_assert_val(ret
);
649 _gnutls_hash_init (&td_md5
, GNUTLS_DIG_MD5
);
651 return gnutls_assert_val(ret
);
653 _gnutls_hash(&td_md5
, session
->internals
.handshake_hash_buffer
.data
, session
->internals
.handshake_hash_buffer
.length
);
655 if (ver
== GNUTLS_SSL3
)
657 ret
= _gnutls_mac_deinit_ssl3_handshake (&td_md5
, concat
,
659 security_parameters
.master_secret
,
662 return gnutls_assert_val(ret
);
665 _gnutls_hash_deinit (&td_md5
, concat
);
667 dconcat
.data
= concat
;
673 dconcat
.data
= &concat
[16];
678 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR
);
680 ret
= sign_tls_hash (session
, GNUTLS_DIG_NULL
, cert
, pkey
, &dconcat
, signature
);
690 pk_hash_data (gnutls_pk_algorithm_t pk
, gnutls_digest_algorithm_t hash
,
691 gnutls_pk_params_st
* params
,
692 const gnutls_datum_t
* data
, gnutls_datum_t
* digest
)
696 digest
->size
= _gnutls_hash_get_algo_len (hash
);
697 digest
->data
= gnutls_malloc (digest
->size
);
698 if (digest
->data
== NULL
)
701 return GNUTLS_E_MEMORY_ERROR
;
704 ret
= _gnutls_hash_fast (hash
, data
->data
, data
->size
, digest
->data
);
714 gnutls_free (digest
->data
);
720 * This function will do RSA PKCS #1 1.5 encoding
721 * on the given digest. The given digest must be allocated
722 * and will be freed if replacement is required.
725 pk_prepare_hash (gnutls_pk_algorithm_t pk
,
726 gnutls_digest_algorithm_t hash
, gnutls_datum_t
* digest
)
729 gnutls_datum_t old_digest
= { digest
->data
, digest
->size
};
734 /* Encode the digest as a DigestInfo
736 if ((ret
= encode_ber_digest_info (hash
, &old_digest
, digest
)) != 0)
742 _gnutls_free_datum (&old_digest
);
749 return GNUTLS_E_UNIMPLEMENTED_FEATURE
;