2 * Copyright (C) 2002-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_auth.h"
25 #include "gnutls_errors.h"
26 #include <auth/cert.h>
27 #include "gnutls_dh.h"
28 #include "gnutls_num.h"
29 #include "gnutls_datum.h"
30 #include <gnutls_pk.h>
31 #include <algorithms.h>
32 #include <gnutls_global.h>
33 #include <gnutls_record.h>
34 #include <gnutls_sig.h>
35 #include <gnutls_state.h>
36 #include <gnutls_pk.h>
37 #include <gnutls_str.h>
40 #include <gnutls_x509.h>
41 #include "x509/common.h"
42 #include "x509/x509_int.h"
43 #include <gnutls_str_array.h>
44 #include "read-file.h"
46 # include <wincrypt.h>
50 * some x509 certificate parsing functions.
53 /* Check if the number of bits of the key in the certificate
57 check_bits (gnutls_x509_crt_t crt
, unsigned int max_bits
)
62 ret
= gnutls_x509_crt_get_pk_algorithm (crt
, &bits
);
69 if (bits
> max_bits
&& max_bits
> 0)
72 return GNUTLS_E_CONSTRAINT_ERROR
;
79 #define CLEAR_CERTS for(x=0;x<peer_certificate_list_size;x++) { \
80 if (peer_certificate_list[x]) \
81 gnutls_x509_crt_deinit(peer_certificate_list[x]); \
83 gnutls_free( peer_certificate_list)
86 * _gnutls_x509_cert_verify_peers - return the peer's certificate status
87 * @session: is a gnutls session
89 * This function will try to verify the peer's certificate and return its status (TRUSTED, REVOKED etc.).
90 * The return value (status) should be one of the gnutls_certificate_status_t enumerated elements.
91 * However you must also check the peer's name in order to check if the verified certificate belongs to the
92 * actual peer. Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
95 _gnutls_x509_cert_verify_peers (gnutls_session_t session
,
98 cert_auth_info_t info
;
99 gnutls_certificate_credentials_t cred
;
100 gnutls_x509_crt_t
*peer_certificate_list
;
101 int peer_certificate_list_size
, i
, x
, ret
;
103 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
105 info
= _gnutls_get_auth_info (session
);
109 return GNUTLS_E_INVALID_REQUEST
;
112 cred
= (gnutls_certificate_credentials_t
)
113 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
117 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
120 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
121 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
123 if (info
->ncerts
> cred
->verify_depth
&& cred
->verify_depth
> 0)
126 return GNUTLS_E_CONSTRAINT_ERROR
;
129 /* generate a list of gnutls_certs based on the auth info
132 peer_certificate_list_size
= info
->ncerts
;
133 peer_certificate_list
=
134 gnutls_calloc (peer_certificate_list_size
, sizeof (gnutls_x509_crt_t
));
135 if (peer_certificate_list
== NULL
)
138 return GNUTLS_E_MEMORY_ERROR
;
141 for (i
= 0; i
< peer_certificate_list_size
; i
++)
143 ret
= gnutls_x509_crt_init (&peer_certificate_list
[i
]);
152 gnutls_x509_crt_import (peer_certificate_list
[i
],
153 &info
->raw_certificate_list
[i
],
154 GNUTLS_X509_FMT_DER
);
162 ret
= check_bits (peer_certificate_list
[i
], cred
->verify_bits
);
172 /* Verify certificate
175 ret
= gnutls_x509_trust_list_verify_crt (cred
->tlist
, peer_certificate_list
,
176 peer_certificate_list_size
,
177 cred
->verify_flags
| session
->internals
.
178 priorities
.additional_verify_flags
,
193 * Read certificates and private keys, from files, memory etc.
196 /* returns error if the certificate has different algorithm than
197 * the given key parameters.
200 _gnutls_check_key_cert_match (gnutls_certificate_credentials_t res
)
202 int pk
= gnutls_pubkey_get_pk_algorithm(res
->certs
[res
->ncerts
-1].cert_list
[0].pubkey
, NULL
);
204 if (gnutls_privkey_get_pk_algorithm (res
->pkey
[res
->ncerts
- 1], NULL
) !=
208 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH
;
214 /* Returns the name of the certificate of a null name
216 static int get_x509_name(gnutls_x509_crt_t crt
, gnutls_str_array_t
*names
)
219 int i
, ret
= 0, ret2
;
222 for (i
= 0; !(ret
< 0); i
++)
224 max_size
= sizeof(name
);
226 ret
= gnutls_x509_crt_get_subject_alt_name(crt
, i
, name
, &max_size
, NULL
);
227 if (ret
== GNUTLS_SAN_DNSNAME
)
229 ret2
= _gnutls_str_array_append(names
, name
, max_size
);
232 _gnutls_str_array_clear(names
);
233 return gnutls_assert_val(ret2
);
238 max_size
= sizeof(name
);
239 ret
= gnutls_x509_crt_get_dn_by_oid (crt
, OID_X520_COMMON_NAME
, 0, 0, name
, &max_size
);
242 ret
= _gnutls_str_array_append(names
, name
, max_size
);
245 _gnutls_str_array_clear(names
);
246 return gnutls_assert_val(ret
);
253 static int get_x509_name_raw(gnutls_datum_t
*raw
, gnutls_x509_crt_fmt_t type
, gnutls_str_array_t
*names
)
256 gnutls_x509_crt_t crt
;
258 ret
= gnutls_x509_crt_init (&crt
);
265 ret
= gnutls_x509_crt_import (crt
, raw
, type
);
269 gnutls_x509_crt_deinit (crt
);
273 ret
= get_x509_name(crt
, names
);
274 gnutls_x509_crt_deinit (crt
);
278 /* Reads a DER encoded certificate list from memory and stores it to a
279 * gnutls_cert structure. Returns the number of certificates parsed.
282 parse_der_cert_mem (gnutls_certificate_credentials_t res
,
283 const void *input_cert
, int input_cert_size
)
286 gnutls_x509_crt_t crt
;
287 gnutls_pcert_st
*ccert
;
289 gnutls_str_array_t names
;
291 _gnutls_str_array_init(&names
);
293 ccert
= gnutls_malloc (sizeof (*ccert
));
297 return GNUTLS_E_MEMORY_ERROR
;
300 ret
= gnutls_x509_crt_init (&crt
);
307 tmp
.data
= (uint8_t *) input_cert
;
308 tmp
.size
= input_cert_size
;
310 ret
= gnutls_x509_crt_import (crt
, &tmp
, GNUTLS_X509_FMT_DER
);
314 gnutls_x509_crt_deinit (crt
);
318 ret
= get_x509_name(crt
, &names
);
322 gnutls_x509_crt_deinit (crt
);
326 ret
= gnutls_pcert_import_x509 (ccert
, crt
, 0);
327 gnutls_x509_crt_deinit (crt
);
335 ret
= certificate_credential_append_crt_list (res
, names
, ccert
, 1);
345 _gnutls_str_array_clear(&names
);
350 /* Reads a base64 encoded certificate list from memory and stores it to
351 * a gnutls_cert structure. Returns the number of certificate parsed.
354 parse_pem_cert_mem (gnutls_certificate_credentials_t res
,
355 const char *input_cert
, int input_cert_size
)
361 gnutls_pcert_st
*certs
= NULL
;
362 gnutls_str_array_t names
;
364 _gnutls_str_array_init(&names
);
366 /* move to the certificate
368 ptr
= memmem (input_cert
, input_cert_size
,
369 PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
371 ptr
= memmem (input_cert
, input_cert_size
,
372 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
377 return GNUTLS_E_BASE64_DECODING_ERROR
;
379 size
= input_cert_size
- (ptr
- input_cert
);
385 certs
= gnutls_realloc_fast (certs
, (count
+ 1) * sizeof (gnutls_pcert_st
));
390 ret
= GNUTLS_E_MEMORY_ERROR
;
394 tmp
.data
= (void*)ptr
;
399 ret
= get_x509_name_raw(&tmp
, GNUTLS_X509_FMT_PEM
, &names
);
407 ret
= gnutls_pcert_import_x509_raw (&certs
[count
], &tmp
, GNUTLS_X509_FMT_PEM
, 0);
414 /* now we move ptr after the pem header
417 /* find the next certificate (if any)
419 size
= input_cert_size
- (ptr
- input_cert
);
425 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
427 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP2
,
428 sizeof (PEM_CERT_SEP2
) - 1);
440 ret
= certificate_credential_append_crt_list (res
, names
, certs
, count
);
450 _gnutls_str_array_clear(&names
);
451 for (i
=0;i
<count
;i
++)
452 gnutls_pcert_deinit(&certs
[i
]);
459 /* Reads a DER or PEM certificate from memory
462 read_cert_mem (gnutls_certificate_credentials_t res
, const void *cert
,
463 int cert_size
, gnutls_x509_crt_fmt_t type
)
467 if (type
== GNUTLS_X509_FMT_DER
)
468 ret
= parse_der_cert_mem (res
, cert
, cert_size
);
470 ret
= parse_pem_cert_mem (res
, cert
, cert_size
);
481 /* Reads a PEM encoded PKCS-1 RSA/DSA private key from memory. Type
482 * indicates the certificate format. KEY can be NULL, to indicate
483 * that GnuTLS doesn't know the private key.
486 read_key_mem (gnutls_certificate_credentials_t res
,
487 const void *key
, int key_size
, gnutls_x509_crt_fmt_t type
)
491 gnutls_privkey_t privkey
;
495 tmp
.data
= (uint8_t *) key
;
498 ret
= gnutls_privkey_init(&privkey
);
506 gnutls_privkey_set_pin_function(privkey
, res
->pin
.cb
, res
->pin
.data
);
508 ret
= gnutls_privkey_import_x509_raw (privkey
, &tmp
, type
, NULL
);
515 ret
= certificate_credentials_append_pkey (res
, privkey
);
519 gnutls_privkey_deinit (privkey
);
527 return GNUTLS_E_INVALID_REQUEST
;
535 /* Reads a private key from a token.
538 read_key_url (gnutls_certificate_credentials_t res
, const char *url
)
541 gnutls_privkey_t pkey
= NULL
;
543 /* allocate space for the pkey list
545 ret
= gnutls_privkey_init (&pkey
);
553 gnutls_privkey_set_pin_function(pkey
, res
->pin
.cb
, res
->pin
.data
);
555 ret
= gnutls_privkey_import_url (pkey
, url
, 0);
562 ret
= certificate_credentials_append_pkey (res
, pkey
);
573 gnutls_privkey_deinit (pkey
);
579 /* Reads a private key from a token.
582 read_cas_url (gnutls_certificate_credentials_t res
, const char *url
)
585 gnutls_x509_crt_t
*xcrt_list
= NULL
;
586 gnutls_pkcs11_obj_t
*pcrt_list
= NULL
;
587 unsigned int pcrt_list_size
= 0;
589 /* FIXME: should we use login? */
591 gnutls_pkcs11_obj_list_import_url (NULL
, &pcrt_list_size
, url
,
592 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED
, 0);
593 if (ret
< 0 && ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
599 if (pcrt_list_size
== 0)
605 pcrt_list
= gnutls_malloc (sizeof (*pcrt_list
) * pcrt_list_size
);
606 if (pcrt_list
== NULL
)
609 return GNUTLS_E_MEMORY_ERROR
;
613 gnutls_pkcs11_obj_list_import_url (pcrt_list
, &pcrt_list_size
, url
,
614 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED
, 0);
621 xcrt_list
= gnutls_malloc (sizeof (*xcrt_list
) * pcrt_list_size
);
622 if (xcrt_list
== NULL
)
625 ret
= GNUTLS_E_MEMORY_ERROR
;
630 gnutls_x509_crt_list_import_pkcs11 (xcrt_list
, pcrt_list_size
, pcrt_list
,
632 if (xcrt_list
== NULL
)
635 ret
= GNUTLS_E_MEMORY_ERROR
;
639 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, xcrt_list
, pcrt_list_size
, 0);
647 gnutls_free (xcrt_list
);
648 gnutls_free (pcrt_list
);
655 /* Reads a certificate key from a token.
658 read_cert_url (gnutls_certificate_credentials_t res
, const char *url
)
661 gnutls_x509_crt_t crt
;
662 gnutls_pcert_st
*ccert
;
663 gnutls_str_array_t names
;
665 _gnutls_str_array_init(&names
);
667 ccert
= gnutls_malloc (sizeof (*ccert
));
671 return GNUTLS_E_MEMORY_ERROR
;
674 ret
= gnutls_x509_crt_init (&crt
);
682 gnutls_x509_crt_set_pin_function(crt
, res
->pin
.cb
, res
->pin
.data
);
684 ret
= gnutls_x509_crt_import_pkcs11_url (crt
, url
, 0);
685 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
687 gnutls_x509_crt_import_pkcs11_url (crt
, url
,
688 GNUTLS_PKCS11_OBJ_FLAG_LOGIN
);
693 gnutls_x509_crt_deinit (crt
);
697 ret
= get_x509_name(crt
, &names
);
701 gnutls_x509_crt_deinit (crt
);
705 ret
= gnutls_pcert_import_x509 (ccert
, crt
, 0);
706 gnutls_x509_crt_deinit (crt
);
714 ret
= certificate_credential_append_crt_list (res
, names
, ccert
, 1);
724 _gnutls_str_array_clear(&names
);
730 /* Reads a certificate file
733 read_cert_file (gnutls_certificate_credentials_t res
,
734 const char *certfile
, gnutls_x509_crt_fmt_t type
)
741 if (strncmp (certfile
, "pkcs11:", 7) == 0)
743 return read_cert_url (res
, certfile
);
745 #endif /* ENABLE_PKCS11 */
747 data
= read_binary_file (certfile
, &size
);
752 return GNUTLS_E_FILE_ERROR
;
755 ret
= read_cert_mem (res
, data
, size
, type
);
764 /* Reads PKCS-1 RSA private key file or a DSA file (in the format openssl
768 read_key_file (gnutls_certificate_credentials_t res
,
769 const char *keyfile
, gnutls_x509_crt_fmt_t type
)
775 if (gnutls_url_is_supported(keyfile
))
777 return read_key_url (res
, keyfile
);
780 data
= read_binary_file (keyfile
, &size
);
785 return GNUTLS_E_FILE_ERROR
;
788 ret
= read_key_mem (res
, data
, size
, type
);
795 * gnutls_certificate_set_x509_key_mem:
796 * @res: is a #gnutls_certificate_credentials_t structure.
797 * @cert: contains a certificate list (path) for the specified private key
798 * @key: is the private key, or %NULL
799 * @type: is PEM or DER
801 * This function sets a certificate/private key pair in the
802 * gnutls_certificate_credentials_t structure. This function may be called
803 * more than once, in case multiple keys/certificates exist for the
806 * Note that the keyUsage (2.5.29.15) PKIX extension in X.509 certificates
807 * is supported. This means that certificates intended for signing cannot
808 * be used for ciphersuites that require encryption.
810 * If the certificate and the private key are given in PEM encoding
811 * then the strings that hold their values must be null terminated.
813 * The @key may be %NULL if you are using a sign callback, see
814 * gnutls_sign_callback_set().
816 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
819 gnutls_certificate_set_x509_key_mem (gnutls_certificate_credentials_t res
,
820 const gnutls_datum_t
* cert
,
821 const gnutls_datum_t
* key
,
822 gnutls_x509_crt_fmt_t type
)
826 /* this should be first
828 if ((ret
= read_key_mem (res
, key
? key
->data
: NULL
,
829 key
? key
->size
: 0, type
)) < 0)
832 if ((ret
= read_cert_mem (res
, cert
->data
, cert
->size
, type
)) < 0)
837 if (key
&& (ret
= _gnutls_check_key_cert_match (res
)) < 0)
846 static int check_if_sorted(gnutls_pcert_st
* crt
, int nr
)
848 gnutls_x509_crt_t x509
;
849 char prev_dn
[MAX_DN
];
851 size_t prev_dn_size
, dn_size
;
854 /* check if the X.509 list is ordered */
855 if (nr
> 1 && crt
[0].type
== GNUTLS_CRT_X509
)
860 ret
= gnutls_x509_crt_init(&x509
);
862 return gnutls_assert_val(ret
);
864 ret
= gnutls_x509_crt_import(x509
, &crt
[i
].cert
, GNUTLS_X509_FMT_DER
);
867 ret
= gnutls_assert_val(ret
);
873 dn_size
= sizeof(dn
);
874 ret
= gnutls_x509_crt_get_dn(x509
, dn
, &dn_size
);
877 ret
= gnutls_assert_val(ret
);
881 if (dn_size
!= prev_dn_size
|| memcmp(dn
, prev_dn
, dn_size
) != 0)
883 ret
= gnutls_assert_val(GNUTLS_E_CERTIFICATE_LIST_UNSORTED
);
888 prev_dn_size
= sizeof(prev_dn
);
889 ret
= gnutls_x509_crt_get_issuer_dn(x509
, prev_dn
, &prev_dn_size
);
892 ret
= gnutls_assert_val(ret
);
896 gnutls_x509_crt_deinit(x509
);
903 gnutls_x509_crt_deinit(x509
);
908 certificate_credential_append_crt_list (gnutls_certificate_credentials_t res
,
909 gnutls_str_array_t names
, gnutls_pcert_st
* crt
, int nr
)
913 ret
= check_if_sorted(crt
, nr
);
915 return gnutls_assert_val(ret
);
917 res
->certs
= gnutls_realloc_fast (res
->certs
,
920 if (res
->certs
== NULL
)
923 return GNUTLS_E_MEMORY_ERROR
;
926 res
->certs
[res
->ncerts
].cert_list
= crt
;
927 res
->certs
[res
->ncerts
].cert_list_length
= nr
;
928 res
->certs
[res
->ncerts
].names
= names
;
935 certificate_credentials_append_pkey (gnutls_certificate_credentials_t res
,
936 gnutls_privkey_t pkey
)
938 res
->pkey
= gnutls_realloc_fast (res
->pkey
,
940 sizeof (gnutls_privkey_t
));
941 if (res
->pkey
== NULL
)
944 return GNUTLS_E_MEMORY_ERROR
;
946 res
->pkey
[res
->ncerts
] = pkey
;
952 * gnutls_certificate_set_x509_key:
953 * @res: is a #gnutls_certificate_credentials_t structure.
954 * @cert_list: contains a certificate list (path) for the specified private key
955 * @cert_list_size: holds the size of the certificate list
956 * @key: is a #gnutls_x509_privkey_t key
958 * This function sets a certificate/private key pair in the
959 * gnutls_certificate_credentials_t structure. This function may be
960 * called more than once, in case multiple keys/certificates exist for
961 * the server. For clients that wants to send more than their own end
962 * entity certificate (e.g., also an intermediate CA cert) then put
963 * the certificate chain in @cert_list.
965 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
970 gnutls_certificate_set_x509_key (gnutls_certificate_credentials_t res
,
971 gnutls_x509_crt_t
* cert_list
,
973 gnutls_x509_privkey_t key
)
976 gnutls_privkey_t pkey
;
977 gnutls_pcert_st
*pcerts
= NULL
;
978 gnutls_str_array_t names
;
980 _gnutls_str_array_init(&names
);
982 /* this should be first
984 ret
= gnutls_privkey_init (&pkey
);
992 gnutls_privkey_set_pin_function(pkey
, res
->pin
.cb
, res
->pin
.data
);
994 ret
= gnutls_privkey_import_x509 (pkey
, key
, GNUTLS_PRIVKEY_IMPORT_COPY
);
1001 ret
= certificate_credentials_append_pkey (res
, pkey
);
1008 /* load certificates */
1009 pcerts
= gnutls_malloc (sizeof (gnutls_pcert_st
) * cert_list_size
);
1013 return GNUTLS_E_MEMORY_ERROR
;
1016 ret
= get_x509_name(cert_list
[0], &names
);
1018 return gnutls_assert_val(ret
);
1020 for (i
= 0; i
< cert_list_size
; i
++)
1022 ret
= gnutls_pcert_import_x509 (&pcerts
[i
], cert_list
[i
], 0);
1030 ret
= certificate_credential_append_crt_list (res
, names
, pcerts
, cert_list_size
);
1039 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1048 _gnutls_str_array_clear(&names
);
1053 * gnutls_certificate_set_key:
1054 * @res: is a #gnutls_certificate_credentials_t structure.
1055 * @names: is an array of DNS name of the certificate (NULL if none)
1056 * @names_size: holds the size of the names list
1057 * @pcert_list: contains a certificate list (path) for the specified private key
1058 * @pcert_list_size: holds the size of the certificate list
1059 * @key: is a #gnutls_privkey_t key
1061 * This function sets a certificate/private key pair in the
1062 * gnutls_certificate_credentials_t structure. This function may be
1063 * called more than once, in case multiple keys/certificates exist for
1064 * the server. For clients that wants to send more than its own end
1065 * entity certificate (e.g., also an intermediate CA cert) then put
1066 * the certificate chain in @pcert_list. The @pcert_list and @key will
1067 * become part of the credentials structure and must not
1068 * be deallocated. They will be automatically deallocated when @res
1071 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1076 gnutls_certificate_set_key (gnutls_certificate_credentials_t res
,
1079 gnutls_pcert_st
* pcert_list
,
1080 int pcert_list_size
,
1081 gnutls_privkey_t key
)
1084 gnutls_str_array_t str_names
;
1086 _gnutls_str_array_init(&str_names
);
1088 if (names
!= NULL
&& names_size
> 0)
1090 for (i
=0;i
<names_size
;i
++)
1092 ret
= _gnutls_str_array_append(&str_names
, names
[i
], strlen(names
[i
]));
1095 ret
= gnutls_assert_val(ret
);
1102 gnutls_privkey_set_pin_function(key
, res
->pin
.cb
, res
->pin
.data
);
1104 ret
= certificate_credentials_append_pkey (res
, key
);
1111 ret
= certificate_credential_append_crt_list (res
, str_names
, pcert_list
, pcert_list_size
);
1120 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1129 _gnutls_str_array_clear(&str_names
);
1134 * gnutls_certificate_set_x509_key_file:
1135 * @res: is a #gnutls_certificate_credentials_t structure.
1136 * @certfile: is a file that containing the certificate list (path) for
1137 * the specified private key, in PKCS7 format, or a list of certificates
1138 * @keyfile: is a file that contains the private key
1139 * @type: is PEM or DER
1141 * This function sets a certificate/private key pair in the
1142 * gnutls_certificate_credentials_t structure. This function may be
1143 * called more than once, in case multiple keys/certificates exist for
1144 * the server. For clients that need to send more than its own end
1145 * entity certificate, e.g., also an intermediate CA cert, then the
1146 * @certfile must contain the ordered certificate chain.
1148 * This function can also accept URLs at @keyfile and @certfile. In that case it
1149 * will import the private key and certificate indicated by the URLs. Note
1150 * that the supported URLs are the ones indicated by gnutls_url_is_supported().
1152 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1155 gnutls_certificate_set_x509_key_file (gnutls_certificate_credentials_t res
,
1156 const char *certfile
,
1157 const char *keyfile
,
1158 gnutls_x509_crt_fmt_t type
)
1162 /* this should be first
1164 if ((ret
= read_key_file (res
, keyfile
, type
)) < 0)
1167 if ((ret
= read_cert_file (res
, certfile
, type
)) < 0)
1172 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1182 add_new_crt_to_rdn_seq (gnutls_certificate_credentials_t res
, gnutls_x509_crt_t
* crts
,
1183 unsigned int crt_size
)
1188 unsigned char *newdata
;
1191 /* Add DN of the last added CAs to the RDN sequence
1192 * This will be sent to clients when a certificate
1193 * request message is sent.
1196 /* FIXME: in case of a client it is not needed
1197 * to do that. This would save time and memory.
1198 * However we don't have that information available
1200 * Further, this function is now much more efficient,
1201 * so optimizing that is less important.
1204 for (i
= 0; i
< crt_size
; i
++)
1206 if ((ret
= gnutls_x509_crt_get_raw_dn (crts
[i
], &tmp
)) < 0)
1212 newsize
= res
->x509_rdn_sequence
.size
+ 2 + tmp
.size
;
1213 if (newsize
< res
->x509_rdn_sequence
.size
)
1216 _gnutls_free_datum (&tmp
);
1217 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1220 newdata
= gnutls_realloc (res
->x509_rdn_sequence
.data
, newsize
);
1221 if (newdata
== NULL
)
1224 _gnutls_free_datum (&tmp
);
1225 return GNUTLS_E_MEMORY_ERROR
;
1228 _gnutls_write_datum16 (newdata
+ res
->x509_rdn_sequence
.size
, tmp
);
1229 _gnutls_free_datum (&tmp
);
1231 res
->x509_rdn_sequence
.size
= newsize
;
1232 res
->x509_rdn_sequence
.data
= newdata
;
1238 /* Returns 0 if it's ok to use the gnutls_kx_algorithm_t with this
1239 * certificate (uses the KeyUsage field).
1242 _gnutls_check_key_usage (const gnutls_pcert_st
* cert
, gnutls_kx_algorithm_t alg
)
1244 unsigned int key_usage
= 0;
1250 return GNUTLS_E_INTERNAL_ERROR
;
1253 if (_gnutls_map_kx_get_cred (alg
, 1) == GNUTLS_CRD_CERTIFICATE
||
1254 _gnutls_map_kx_get_cred (alg
, 0) == GNUTLS_CRD_CERTIFICATE
)
1257 gnutls_pubkey_get_key_usage(cert
->pubkey
, &key_usage
);
1259 encipher_type
= _gnutls_kx_encipher_type (alg
);
1261 if (key_usage
!= 0 && encipher_type
!= CIPHER_IGN
)
1263 /* If key_usage has been set in the certificate
1266 if (encipher_type
== CIPHER_ENCRYPT
)
1268 /* If the key exchange method requires an encipher
1269 * type algorithm, and key's usage does not permit
1270 * encipherment, then fail.
1272 if (!(key_usage
& GNUTLS_KEY_KEY_ENCIPHERMENT
))
1275 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1279 if (encipher_type
== CIPHER_SIGN
)
1281 /* The same as above, but for sign only keys
1283 if (!(key_usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
))
1286 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1295 parse_pem_ca_mem (gnutls_certificate_credentials_t res
,
1296 const uint8_t * input_cert
, int input_cert_size
)
1298 gnutls_x509_crt_t
*x509_cert_list
;
1299 unsigned int x509_ncerts
;
1303 tmp
.data
= (void*)input_cert
;
1304 tmp
.size
= input_cert_size
;
1306 ret
= gnutls_x509_crt_list_import2( &x509_cert_list
, &x509_ncerts
, &tmp
,
1307 GNUTLS_X509_FMT_PEM
, 0);
1314 if ((ret
= add_new_crt_to_rdn_seq (res
, x509_cert_list
, x509_ncerts
)) < 0)
1320 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, x509_cert_list
, x509_ncerts
, 0);
1328 gnutls_free(x509_cert_list
);
1332 /* Reads a DER encoded certificate list from memory and stores it to a
1333 * gnutls_cert structure. Returns the number of certificates parsed.
1336 parse_der_ca_mem (gnutls_certificate_credentials_t res
,
1337 const void *input_cert
, int input_cert_size
)
1339 gnutls_x509_crt_t crt
;
1343 tmp
.data
= (void*)input_cert
;
1344 tmp
.size
= input_cert_size
;
1346 ret
= gnutls_x509_crt_init( &crt
);
1353 ret
= gnutls_x509_crt_import( crt
, &tmp
, GNUTLS_X509_FMT_DER
);
1360 if ((ret
= add_new_crt_to_rdn_seq (res
, &crt
, 1)) < 0)
1366 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, &crt
, 1, 0);
1376 gnutls_x509_crt_deinit(crt
);
1381 * gnutls_certificate_set_x509_trust_mem:
1382 * @res: is a #gnutls_certificate_credentials_t structure.
1383 * @ca: is a list of trusted CAs or a DER certificate
1384 * @type: is DER or PEM
1386 * This function adds the trusted CAs in order to verify client or
1387 * server certificates. In case of a client this is not required to be
1388 * called if the certificates are not verified using
1389 * gnutls_certificate_verify_peers2(). This function may be called
1392 * In case of a server the CAs set here will be sent to the client if
1393 * a certificate request is sent. This can be disabled using
1394 * gnutls_certificate_send_x509_rdn_sequence().
1396 * Returns: the number of certificates processed or a negative error code
1400 gnutls_certificate_set_x509_trust_mem (gnutls_certificate_credentials_t res
,
1401 const gnutls_datum_t
* ca
,
1402 gnutls_x509_crt_fmt_t type
)
1406 if (type
== GNUTLS_X509_FMT_DER
)
1407 ret
= parse_der_ca_mem (res
,
1408 ca
->data
, ca
->size
);
1410 ret
= parse_pem_ca_mem (res
,
1411 ca
->data
, ca
->size
);
1413 if (ret
== GNUTLS_E_NO_CERTIFICATE_FOUND
)
1420 * gnutls_certificate_set_x509_trust:
1421 * @res: is a #gnutls_certificate_credentials_t structure.
1422 * @ca_list: is a list of trusted CAs
1423 * @ca_list_size: holds the size of the CA list
1425 * This function adds the trusted CAs in order to verify client
1426 * or server certificates. In case of a client this is not required
1427 * to be called if the certificates are not verified using
1428 * gnutls_certificate_verify_peers2().
1429 * This function may be called multiple times.
1431 * In case of a server the CAs set here will be sent to the client if
1432 * a certificate request is sent. This can be disabled using
1433 * gnutls_certificate_send_x509_rdn_sequence().
1435 * Returns: the number of certificates processed or a negative error code
1441 gnutls_certificate_set_x509_trust (gnutls_certificate_credentials_t res
,
1442 gnutls_x509_crt_t
* ca_list
,
1446 gnutls_x509_crt_t new_list
[ca_list_size
];
1448 for (i
= 0; i
< ca_list_size
; i
++)
1450 ret
= gnutls_x509_crt_init (&new_list
[i
]);
1457 ret
= _gnutls_x509_crt_cpy (new_list
[i
], ca_list
[i
]);
1465 if ((ret
= add_new_crt_to_rdn_seq (res
, new_list
, ca_list_size
)) < 0)
1471 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, new_list
, ca_list_size
, 0);
1482 gnutls_x509_crt_deinit(new_list
[j
]);
1489 * gnutls_certificate_set_x509_trust_file:
1490 * @cred: is a #gnutls_certificate_credentials_t structure.
1491 * @cafile: is a file containing the list of trusted CAs (DER or PEM list)
1492 * @type: is PEM or DER
1494 * This function adds the trusted CAs in order to verify client or
1495 * server certificates. In case of a client this is not required to
1496 * be called if the certificates are not verified using
1497 * gnutls_certificate_verify_peers2(). This function may be called
1500 * In case of a server the names of the CAs set here will be sent to
1501 * the client if a certificate request is sent. This can be disabled
1502 * using gnutls_certificate_send_x509_rdn_sequence().
1504 * This function can also accept URLs. In that case it
1505 * will import all certificates that are marked as trusted. Note
1506 * that the supported URLs are the ones indicated by gnutls_url_is_supported().
1508 * Returns: number of certificates processed, or a negative error code on
1512 gnutls_certificate_set_x509_trust_file (gnutls_certificate_credentials_t cred
,
1514 gnutls_x509_crt_fmt_t type
)
1520 #ifdef ENABLE_PKCS11
1521 if (strncmp (cafile
, "pkcs11:", 7) == 0)
1523 return read_cas_url (cred
, cafile
);
1527 cas
.data
= (void*)read_binary_file (cafile
, &size
);
1528 if (cas
.data
== NULL
)
1531 return GNUTLS_E_FILE_ERROR
;
1536 ret
= gnutls_certificate_set_x509_trust_mem(cred
, &cas
, type
);
1550 * gnutls_certificate_set_x509_system_trust:
1551 * @cred: is a #gnutls_certificate_credentials_t structure.
1553 * This function adds the system's default trusted CAs in order to
1554 * verify client or server certificates.
1556 * In the case the system is currently unsupported %GNUTLS_E_UNIMPLEMENTED_FEATURE
1559 * Returns: the number of certificates processed or a negative error code
1565 gnutls_certificate_set_x509_system_trust (gnutls_certificate_credentials_t cred
)
1567 return gnutls_x509_trust_list_add_system_trust(cred
->tlist
, 0, 0);
1571 parse_pem_crl_mem (gnutls_x509_trust_list_t tlist
,
1572 const char * input_crl
, unsigned int input_crl_size
)
1574 gnutls_x509_crl_t
*x509_crl_list
;
1575 unsigned int x509_ncrls
;
1579 tmp
.data
= (void*)input_crl
;
1580 tmp
.size
= input_crl_size
;
1582 ret
= gnutls_x509_crl_list_import2( &x509_crl_list
, &x509_ncrls
, &tmp
,
1583 GNUTLS_X509_FMT_PEM
, 0);
1590 ret
= gnutls_x509_trust_list_add_crls(tlist
, x509_crl_list
, x509_ncrls
, 0, 0);
1598 gnutls_free(x509_crl_list
);
1602 /* Reads a DER encoded certificate list from memory and stores it to a
1603 * gnutls_cert structure. Returns the number of certificates parsed.
1606 parse_der_crl_mem (gnutls_x509_trust_list_t tlist
,
1607 const void *input_crl
, unsigned int input_crl_size
)
1609 gnutls_x509_crl_t crl
;
1613 tmp
.data
= (void*)input_crl
;
1614 tmp
.size
= input_crl_size
;
1616 ret
= gnutls_x509_crl_init( &crl
);
1623 ret
= gnutls_x509_crl_import( crl
, &tmp
, GNUTLS_X509_FMT_DER
);
1630 ret
= gnutls_x509_trust_list_add_crls(tlist
, &crl
, 1, 0, 0);
1640 gnutls_x509_crl_deinit(crl
);
1646 /* Reads a DER or PEM CRL from memory
1649 read_crl_mem (gnutls_certificate_credentials_t res
, const void *crl
,
1650 int crl_size
, gnutls_x509_crt_fmt_t type
)
1654 if (type
== GNUTLS_X509_FMT_DER
)
1655 ret
= parse_der_crl_mem (res
->tlist
, crl
, crl_size
);
1657 ret
= parse_pem_crl_mem (res
->tlist
, crl
, crl_size
);
1668 * gnutls_certificate_set_x509_crl_mem:
1669 * @res: is a #gnutls_certificate_credentials_t structure.
1670 * @CRL: is a list of trusted CRLs. They should have been verified before.
1671 * @type: is DER or PEM
1673 * This function adds the trusted CRLs in order to verify client or
1674 * server certificates. In case of a client this is not required to
1675 * be called if the certificates are not verified using
1676 * gnutls_certificate_verify_peers2(). This function may be called
1679 * Returns: number of CRLs processed, or a negative error code on error.
1682 gnutls_certificate_set_x509_crl_mem (gnutls_certificate_credentials_t res
,
1683 const gnutls_datum_t
* CRL
,
1684 gnutls_x509_crt_fmt_t type
)
1686 return read_crl_mem (res
, CRL
->data
, CRL
->size
, type
);
1690 * gnutls_certificate_set_x509_crl:
1691 * @res: is a #gnutls_certificate_credentials_t structure.
1692 * @crl_list: is a list of trusted CRLs. They should have been verified before.
1693 * @crl_list_size: holds the size of the crl_list
1695 * This function adds the trusted CRLs in order to verify client or
1696 * server certificates. In case of a client this is not required to
1697 * be called if the certificates are not verified using
1698 * gnutls_certificate_verify_peers2(). This function may be called
1701 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1706 gnutls_certificate_set_x509_crl (gnutls_certificate_credentials_t res
,
1707 gnutls_x509_crl_t
* crl_list
,
1711 gnutls_x509_crl_t new_crl
[crl_list_size
];
1713 for (i
= 0; i
< crl_list_size
; i
++)
1715 ret
= gnutls_x509_crl_init (&new_crl
[i
]);
1722 ret
= _gnutls_x509_crl_cpy (new_crl
[i
], crl_list
[i
]);
1730 ret
= gnutls_x509_trust_list_add_crls(res
->tlist
, new_crl
, crl_list_size
, 0, 0);
1741 gnutls_x509_crl_deinit(new_crl
[j
]);
1747 * gnutls_certificate_set_x509_crl_file:
1748 * @res: is a #gnutls_certificate_credentials_t structure.
1749 * @crlfile: is a file containing the list of verified CRLs (DER or PEM list)
1750 * @type: is PEM or DER
1752 * This function adds the trusted CRLs in order to verify client or server
1753 * certificates. In case of a client this is not required
1754 * to be called if the certificates are not verified using
1755 * gnutls_certificate_verify_peers2().
1756 * This function may be called multiple times.
1758 * Returns: number of CRLs processed or a negative error code on error.
1761 gnutls_certificate_set_x509_crl_file (gnutls_certificate_credentials_t res
,
1762 const char *crlfile
,
1763 gnutls_x509_crt_fmt_t type
)
1767 char *data
= (void*)read_binary_file (crlfile
, &size
);
1772 return GNUTLS_E_FILE_ERROR
;
1775 if (type
== GNUTLS_X509_FMT_DER
)
1776 ret
= parse_der_crl_mem (res
->tlist
, data
, size
);
1778 ret
= parse_pem_crl_mem (res
->tlist
, data
, size
);
1791 #include <gnutls/pkcs12.h>
1795 * gnutls_certificate_set_x509_simple_pkcs12_file:
1796 * @res: is a #gnutls_certificate_credentials_t structure.
1797 * @pkcs12file: filename of file containing PKCS#12 blob.
1798 * @type: is PEM or DER of the @pkcs12file.
1799 * @password: optional password used to decrypt PKCS#12 file, bags and keys.
1801 * This function sets a certificate/private key pair and/or a CRL in
1802 * the gnutls_certificate_credentials_t structure. This function may
1803 * be called more than once (in case multiple keys/certificates exist
1806 * PKCS#12 files with a MAC, encrypted bags and PKCS #8
1807 * private keys are supported. However,
1808 * only password based security, and the same password for all
1809 * operations, are supported.
1811 * PKCS#12 file may contain many keys and/or certificates, and there
1812 * is no way to identify which key/certificate pair you want. You
1813 * should make sure the PKCS#12 file only contain one key/certificate
1814 * pair and/or one CRL.
1816 * It is believed that the limitations of this function is acceptable
1817 * for most usage, and that any more flexibility would introduce
1818 * complexity that would make it harder to use this functionality at
1821 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1824 gnutls_certificate_set_x509_simple_pkcs12_file
1825 (gnutls_certificate_credentials_t res
, const char *pkcs12file
,
1826 gnutls_x509_crt_fmt_t type
, const char *password
)
1828 gnutls_datum_t p12blob
;
1832 p12blob
.data
= (void*)read_binary_file (pkcs12file
, &size
);
1833 p12blob
.size
= (unsigned int) size
;
1834 if (p12blob
.data
== NULL
)
1837 return GNUTLS_E_FILE_ERROR
;
1841 gnutls_certificate_set_x509_simple_pkcs12_mem (res
, &p12blob
, type
,
1843 free (p12blob
.data
);
1849 * gnutls_certificate_set_x509_simple_pkcs12_mem:
1850 * @res: is a #gnutls_certificate_credentials_t structure.
1851 * @p12blob: the PKCS#12 blob.
1852 * @type: is PEM or DER of the @pkcs12file.
1853 * @password: optional password used to decrypt PKCS#12 file, bags and keys.
1855 * This function sets a certificate/private key pair and/or a CRL in
1856 * the gnutls_certificate_credentials_t structure. This function may
1857 * be called more than once (in case multiple keys/certificates exist
1860 * Encrypted PKCS#12 bags and PKCS#8 private keys are supported. However,
1861 * only password based security, and the same password for all
1862 * operations, are supported.
1864 * PKCS#12 file may contain many keys and/or certificates, and there
1865 * is no way to identify which key/certificate pair you want. You
1866 * should make sure the PKCS#12 file only contain one key/certificate
1867 * pair and/or one CRL.
1869 * It is believed that the limitations of this function is acceptable
1870 * for most usage, and that any more flexibility would introduce
1871 * complexity that would make it harder to use this functionality at
1874 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1879 gnutls_certificate_set_x509_simple_pkcs12_mem
1880 (gnutls_certificate_credentials_t res
, const gnutls_datum_t
* p12blob
,
1881 gnutls_x509_crt_fmt_t type
, const char *password
)
1883 gnutls_pkcs12_t p12
;
1884 gnutls_x509_privkey_t key
= NULL
;
1885 gnutls_x509_crt_t
*chain
= NULL
;
1886 gnutls_x509_crl_t crl
= NULL
;
1887 unsigned int chain_size
= 0, i
;
1890 ret
= gnutls_pkcs12_init (&p12
);
1897 ret
= gnutls_pkcs12_import (p12
, p12blob
, type
, 0);
1901 gnutls_pkcs12_deinit (p12
);
1907 ret
= gnutls_pkcs12_verify_mac (p12
, password
);
1911 gnutls_pkcs12_deinit (p12
);
1916 ret
= gnutls_pkcs12_simple_parse (p12
, password
, &key
, &chain
, &chain_size
,
1917 NULL
, NULL
, &crl
, 0);
1918 gnutls_pkcs12_deinit (p12
);
1927 ret
= gnutls_certificate_set_x509_key (res
, chain
, chain_size
, key
);
1937 ret
= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1943 ret
= gnutls_certificate_set_x509_crl (res
, &crl
, 1);
1956 for (i
=0;i
<chain_size
;i
++)
1957 gnutls_x509_crt_deinit (chain
[i
]);
1961 gnutls_x509_privkey_deinit (key
);
1963 gnutls_x509_crl_deinit (crl
);
1971 * gnutls_certificate_free_crls:
1972 * @sc: is a #gnutls_certificate_credentials_t structure.
1974 * This function will delete all the CRLs associated
1975 * with the given credentials.
1978 gnutls_certificate_free_crls (gnutls_certificate_credentials_t sc
)
1980 /* do nothing for now */
1985 * gnutls_certificate_credentials_t:
1986 * @cred: is a #gnutls_certificate_credentials_t structure.
1987 * @fn: A PIN callback
1988 * @userdata: Data to be passed in the callback
1990 * This function will set a callback function to be used when
1991 * required to access a protected object. This function overrides any other
1992 * global PIN functions.
1994 * Note that this function must be called right after initialization
1999 void gnutls_certificate_set_pin_function (gnutls_certificate_credentials_t cred
,
2000 gnutls_pin_callback_t fn
, void *userdata
)
2003 cred
->pin
.data
= userdata
;