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_session_t session
, gnutls_x509_crt_t crt
, unsigned int max_bits
)
62 ret
= gnutls_x509_crt_get_pk_algorithm (crt
, &bits
);
70 if (bits
> max_bits
&& max_bits
> 0)
73 return GNUTLS_E_CONSTRAINT_ERROR
;
76 if (gnutls_pk_bits_to_sec_param(pk
, bits
) == GNUTLS_SEC_PARAM_INSECURE
)
79 _gnutls_audit_log(session
, "The security level of the certificate (%s: %u) is weak\n", gnutls_pk_get_name(pk
), bits
);
80 if (session
->internals
.priorities
.allow_weak_keys
== 0)
81 return gnutls_assert_val(GNUTLS_E_CERTIFICATE_ERROR
);
88 #define CLEAR_CERTS for(x=0;x<peer_certificate_list_size;x++) { \
89 if (peer_certificate_list[x]) \
90 gnutls_x509_crt_deinit(peer_certificate_list[x]); \
92 gnutls_free( peer_certificate_list)
95 * _gnutls_x509_cert_verify_peers - return the peer's certificate status
96 * @session: is a gnutls session
98 * This function will try to verify the peer's certificate and return its status (TRUSTED, REVOKED etc.).
99 * The return value (status) should be one of the gnutls_certificate_status_t enumerated elements.
100 * However you must also check the peer's name in order to check if the verified certificate belongs to the
101 * actual peer. Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
104 _gnutls_x509_cert_verify_peers (gnutls_session_t session
,
105 unsigned int *status
)
107 cert_auth_info_t info
;
108 gnutls_certificate_credentials_t cred
;
109 gnutls_x509_crt_t
*peer_certificate_list
;
110 int peer_certificate_list_size
, i
, x
, ret
;
112 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
114 info
= _gnutls_get_auth_info (session
);
118 return GNUTLS_E_INVALID_REQUEST
;
121 cred
= (gnutls_certificate_credentials_t
)
122 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
126 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
129 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
130 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
132 if (info
->ncerts
> cred
->verify_depth
&& cred
->verify_depth
> 0)
135 return GNUTLS_E_CONSTRAINT_ERROR
;
138 /* generate a list of gnutls_certs based on the auth info
141 peer_certificate_list_size
= info
->ncerts
;
142 peer_certificate_list
=
143 gnutls_calloc (peer_certificate_list_size
, sizeof (gnutls_x509_crt_t
));
144 if (peer_certificate_list
== NULL
)
147 return GNUTLS_E_MEMORY_ERROR
;
150 for (i
= 0; i
< peer_certificate_list_size
; i
++)
152 ret
= gnutls_x509_crt_init (&peer_certificate_list
[i
]);
161 gnutls_x509_crt_import (peer_certificate_list
[i
],
162 &info
->raw_certificate_list
[i
],
163 GNUTLS_X509_FMT_DER
);
171 ret
= check_bits (session
, peer_certificate_list
[i
], cred
->verify_bits
);
181 /* Verify certificate
184 ret
= gnutls_x509_trust_list_verify_crt (cred
->tlist
, peer_certificate_list
,
185 peer_certificate_list_size
,
186 cred
->verify_flags
| session
->internals
.
187 priorities
.additional_verify_flags
,
202 * Read certificates and private keys, from files, memory etc.
206 /* Returns the name of the certificate of a null name
208 static int get_x509_name(gnutls_x509_crt_t crt
, gnutls_str_array_t
*names
)
211 int i
, ret
= 0, ret2
;
214 for (i
= 0; !(ret
< 0); i
++)
216 max_size
= sizeof(name
);
218 ret
= gnutls_x509_crt_get_subject_alt_name(crt
, i
, name
, &max_size
, NULL
);
219 if (ret
== GNUTLS_SAN_DNSNAME
)
221 ret2
= _gnutls_str_array_append(names
, name
, max_size
);
224 _gnutls_str_array_clear(names
);
225 return gnutls_assert_val(ret2
);
230 max_size
= sizeof(name
);
231 ret
= gnutls_x509_crt_get_dn_by_oid (crt
, OID_X520_COMMON_NAME
, 0, 0, name
, &max_size
);
234 ret
= _gnutls_str_array_append(names
, name
, max_size
);
237 _gnutls_str_array_clear(names
);
238 return gnutls_assert_val(ret
);
245 static int get_x509_name_raw(gnutls_datum_t
*raw
, gnutls_x509_crt_fmt_t type
, gnutls_str_array_t
*names
)
248 gnutls_x509_crt_t crt
;
250 ret
= gnutls_x509_crt_init (&crt
);
257 ret
= gnutls_x509_crt_import (crt
, raw
, type
);
261 gnutls_x509_crt_deinit (crt
);
265 ret
= get_x509_name(crt
, names
);
266 gnutls_x509_crt_deinit (crt
);
270 /* Reads a DER encoded certificate list from memory and stores it to a
271 * gnutls_cert structure. Returns the number of certificates parsed.
274 parse_der_cert_mem (gnutls_certificate_credentials_t res
,
275 const void *input_cert
, int input_cert_size
)
278 gnutls_x509_crt_t crt
;
279 gnutls_pcert_st
*ccert
;
281 gnutls_str_array_t names
;
283 _gnutls_str_array_init(&names
);
285 ccert
= gnutls_malloc (sizeof (*ccert
));
289 return GNUTLS_E_MEMORY_ERROR
;
292 ret
= gnutls_x509_crt_init (&crt
);
299 tmp
.data
= (uint8_t *) input_cert
;
300 tmp
.size
= input_cert_size
;
302 ret
= gnutls_x509_crt_import (crt
, &tmp
, GNUTLS_X509_FMT_DER
);
306 gnutls_x509_crt_deinit (crt
);
310 ret
= get_x509_name(crt
, &names
);
314 gnutls_x509_crt_deinit (crt
);
318 ret
= gnutls_pcert_import_x509 (ccert
, crt
, 0);
319 gnutls_x509_crt_deinit (crt
);
327 ret
= certificate_credential_append_crt_list (res
, names
, ccert
, 1);
337 _gnutls_str_array_clear(&names
);
342 /* Reads a base64 encoded certificate list from memory and stores it to
343 * a gnutls_cert structure. Returns the number of certificate parsed.
346 parse_pem_cert_mem (gnutls_certificate_credentials_t res
,
347 const char *input_cert
, int input_cert_size
)
353 gnutls_pcert_st
*certs
= NULL
;
354 gnutls_str_array_t names
;
356 _gnutls_str_array_init(&names
);
358 /* move to the certificate
360 ptr
= memmem (input_cert
, input_cert_size
,
361 PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
363 ptr
= memmem (input_cert
, input_cert_size
,
364 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
369 return GNUTLS_E_BASE64_DECODING_ERROR
;
371 size
= input_cert_size
- (ptr
- input_cert
);
377 certs
= gnutls_realloc_fast (certs
, (count
+ 1) * sizeof (gnutls_pcert_st
));
382 ret
= GNUTLS_E_MEMORY_ERROR
;
386 tmp
.data
= (void*)ptr
;
391 ret
= get_x509_name_raw(&tmp
, GNUTLS_X509_FMT_PEM
, &names
);
399 ret
= gnutls_pcert_import_x509_raw (&certs
[count
], &tmp
, GNUTLS_X509_FMT_PEM
, 0);
406 /* now we move ptr after the pem header
409 /* find the next certificate (if any)
411 size
= input_cert_size
- (ptr
- input_cert
);
417 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
419 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP2
,
420 sizeof (PEM_CERT_SEP2
) - 1);
432 ret
= certificate_credential_append_crt_list (res
, names
, certs
, count
);
442 _gnutls_str_array_clear(&names
);
443 for (i
=0;i
<count
;i
++)
444 gnutls_pcert_deinit(&certs
[i
]);
451 /* Reads a DER or PEM certificate from memory
454 read_cert_mem (gnutls_certificate_credentials_t res
, const void *cert
,
455 int cert_size
, gnutls_x509_crt_fmt_t type
)
459 if (type
== GNUTLS_X509_FMT_DER
)
460 ret
= parse_der_cert_mem (res
, cert
, cert_size
);
462 ret
= parse_pem_cert_mem (res
, cert
, cert_size
);
473 /* Reads a PEM encoded PKCS-1 RSA/DSA private key from memory. Type
474 * indicates the certificate format. KEY can be NULL, to indicate
475 * that GnuTLS doesn't know the private key.
478 read_key_mem (gnutls_certificate_credentials_t res
,
479 const void *key
, int key_size
, gnutls_x509_crt_fmt_t type
)
483 gnutls_privkey_t privkey
;
487 tmp
.data
= (uint8_t *) key
;
490 ret
= gnutls_privkey_init(&privkey
);
498 gnutls_privkey_set_pin_function(privkey
, res
->pin
.cb
, res
->pin
.data
);
500 ret
= gnutls_privkey_import_x509_raw (privkey
, &tmp
, type
, NULL
, 0);
507 ret
= certificate_credentials_append_pkey (res
, privkey
);
511 gnutls_privkey_deinit (privkey
);
519 return GNUTLS_E_INVALID_REQUEST
;
527 /* Reads a private key from a token.
530 read_key_url (gnutls_certificate_credentials_t res
, const char *url
)
533 gnutls_privkey_t pkey
= NULL
;
535 /* allocate space for the pkey list
537 ret
= gnutls_privkey_init (&pkey
);
545 gnutls_privkey_set_pin_function(pkey
, res
->pin
.cb
, res
->pin
.data
);
547 ret
= gnutls_privkey_import_url (pkey
, url
, 0);
554 ret
= certificate_credentials_append_pkey (res
, pkey
);
565 gnutls_privkey_deinit (pkey
);
571 /* Reads a private key from a token.
574 read_cas_url (gnutls_certificate_credentials_t res
, const char *url
)
577 gnutls_x509_crt_t
*xcrt_list
= NULL
;
578 gnutls_pkcs11_obj_t
*pcrt_list
= NULL
;
579 unsigned int pcrt_list_size
= 0;
581 /* FIXME: should we use login? */
583 gnutls_pkcs11_obj_list_import_url (NULL
, &pcrt_list_size
, url
,
584 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED
, 0);
585 if (ret
< 0 && ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
591 if (pcrt_list_size
== 0)
597 pcrt_list
= gnutls_malloc (sizeof (*pcrt_list
) * pcrt_list_size
);
598 if (pcrt_list
== NULL
)
601 return GNUTLS_E_MEMORY_ERROR
;
605 gnutls_pkcs11_obj_list_import_url (pcrt_list
, &pcrt_list_size
, url
,
606 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED
, 0);
613 xcrt_list
= gnutls_malloc (sizeof (*xcrt_list
) * pcrt_list_size
);
614 if (xcrt_list
== NULL
)
617 ret
= GNUTLS_E_MEMORY_ERROR
;
622 gnutls_x509_crt_list_import_pkcs11 (xcrt_list
, pcrt_list_size
, pcrt_list
,
630 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, xcrt_list
, pcrt_list_size
, 0);
638 gnutls_free (xcrt_list
);
639 gnutls_free (pcrt_list
);
646 /* Reads a certificate key from a token.
649 read_cert_url (gnutls_certificate_credentials_t res
, const char *url
)
652 gnutls_x509_crt_t crt
;
653 gnutls_pcert_st
*ccert
;
654 gnutls_str_array_t names
;
656 _gnutls_str_array_init(&names
);
658 ccert
= gnutls_malloc (sizeof (*ccert
));
662 return GNUTLS_E_MEMORY_ERROR
;
665 ret
= gnutls_x509_crt_init (&crt
);
673 gnutls_x509_crt_set_pin_function(crt
, res
->pin
.cb
, res
->pin
.data
);
675 ret
= gnutls_x509_crt_import_pkcs11_url (crt
, url
, 0);
676 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
678 gnutls_x509_crt_import_pkcs11_url (crt
, url
,
679 GNUTLS_PKCS11_OBJ_FLAG_LOGIN
);
684 gnutls_x509_crt_deinit (crt
);
688 ret
= get_x509_name(crt
, &names
);
692 gnutls_x509_crt_deinit (crt
);
696 ret
= gnutls_pcert_import_x509 (ccert
, crt
, 0);
697 gnutls_x509_crt_deinit (crt
);
705 ret
= certificate_credential_append_crt_list (res
, names
, ccert
, 1);
715 _gnutls_str_array_clear(&names
);
721 /* Reads a certificate file
724 read_cert_file (gnutls_certificate_credentials_t res
,
725 const char *certfile
, gnutls_x509_crt_fmt_t type
)
732 if (strncmp (certfile
, "pkcs11:", 7) == 0)
734 return read_cert_url (res
, certfile
);
736 #endif /* ENABLE_PKCS11 */
738 data
= read_binary_file (certfile
, &size
);
743 return GNUTLS_E_FILE_ERROR
;
746 ret
= read_cert_mem (res
, data
, size
, type
);
755 /* Reads PKCS-1 RSA private key file or a DSA file (in the format openssl
759 read_key_file (gnutls_certificate_credentials_t res
,
760 const char *keyfile
, gnutls_x509_crt_fmt_t type
)
766 if (gnutls_url_is_supported(keyfile
))
768 return read_key_url (res
, keyfile
);
771 data
= read_binary_file (keyfile
, &size
);
776 return GNUTLS_E_FILE_ERROR
;
779 ret
= read_key_mem (res
, data
, size
, type
);
786 * gnutls_certificate_set_x509_key_mem:
787 * @res: is a #gnutls_certificate_credentials_t structure.
788 * @cert: contains a certificate list (path) for the specified private key
789 * @key: is the private key, or %NULL
790 * @type: is PEM or DER
792 * This function sets a certificate/private key pair in the
793 * gnutls_certificate_credentials_t structure. This function may be called
794 * more than once, in case multiple keys/certificates exist for the
797 * Note that the keyUsage (2.5.29.15) PKIX extension in X.509 certificates
798 * is supported. This means that certificates intended for signing cannot
799 * be used for ciphersuites that require encryption.
801 * If the certificate and the private key are given in PEM encoding
802 * then the strings that hold their values must be null terminated.
804 * The @key may be %NULL if you are using a sign callback, see
805 * gnutls_sign_callback_set().
807 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
810 gnutls_certificate_set_x509_key_mem (gnutls_certificate_credentials_t res
,
811 const gnutls_datum_t
* cert
,
812 const gnutls_datum_t
* key
,
813 gnutls_x509_crt_fmt_t type
)
817 /* this should be first
819 if ((ret
= read_key_mem (res
, key
? key
->data
: NULL
,
820 key
? key
->size
: 0, type
)) < 0)
823 if ((ret
= read_cert_mem (res
, cert
->data
, cert
->size
, type
)) < 0)
828 if (key
&& (ret
= _gnutls_check_key_cert_match (res
)) < 0)
837 static int check_if_sorted(gnutls_pcert_st
* crt
, int nr
)
839 gnutls_x509_crt_t x509
;
840 char prev_dn
[MAX_DN
];
842 size_t prev_dn_size
, dn_size
;
845 /* check if the X.509 list is ordered */
846 if (nr
> 1 && crt
[0].type
== GNUTLS_CRT_X509
)
851 ret
= gnutls_x509_crt_init(&x509
);
853 return gnutls_assert_val(ret
);
855 ret
= gnutls_x509_crt_import(x509
, &crt
[i
].cert
, GNUTLS_X509_FMT_DER
);
858 ret
= gnutls_assert_val(ret
);
864 dn_size
= sizeof(dn
);
865 ret
= gnutls_x509_crt_get_dn(x509
, dn
, &dn_size
);
868 ret
= gnutls_assert_val(ret
);
872 if (dn_size
!= prev_dn_size
|| memcmp(dn
, prev_dn
, dn_size
) != 0)
874 ret
= gnutls_assert_val(GNUTLS_E_CERTIFICATE_LIST_UNSORTED
);
879 prev_dn_size
= sizeof(prev_dn
);
880 ret
= gnutls_x509_crt_get_issuer_dn(x509
, prev_dn
, &prev_dn_size
);
883 ret
= gnutls_assert_val(ret
);
887 gnutls_x509_crt_deinit(x509
);
894 gnutls_x509_crt_deinit(x509
);
899 certificate_credential_append_crt_list (gnutls_certificate_credentials_t res
,
900 gnutls_str_array_t names
, gnutls_pcert_st
* crt
, int nr
)
904 ret
= check_if_sorted(crt
, nr
);
906 return gnutls_assert_val(ret
);
908 res
->certs
= gnutls_realloc_fast (res
->certs
,
911 if (res
->certs
== NULL
)
914 return GNUTLS_E_MEMORY_ERROR
;
917 res
->certs
[res
->ncerts
].cert_list
= crt
;
918 res
->certs
[res
->ncerts
].cert_list_length
= nr
;
919 res
->certs
[res
->ncerts
].names
= names
;
926 certificate_credentials_append_pkey (gnutls_certificate_credentials_t res
,
927 gnutls_privkey_t pkey
)
929 res
->pkey
= gnutls_realloc_fast (res
->pkey
,
931 sizeof (gnutls_privkey_t
));
932 if (res
->pkey
== NULL
)
935 return GNUTLS_E_MEMORY_ERROR
;
937 res
->pkey
[res
->ncerts
] = pkey
;
943 * gnutls_certificate_set_x509_key:
944 * @res: is a #gnutls_certificate_credentials_t structure.
945 * @cert_list: contains a certificate list (path) for the specified private key
946 * @cert_list_size: holds the size of the certificate list
947 * @key: is a #gnutls_x509_privkey_t key
949 * This function sets a certificate/private key pair in the
950 * gnutls_certificate_credentials_t structure. This function may be
951 * called more than once, in case multiple keys/certificates exist for
952 * the server. For clients that wants to send more than their own end
953 * entity certificate (e.g., also an intermediate CA cert) then put
954 * the certificate chain in @cert_list.
956 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
961 gnutls_certificate_set_x509_key (gnutls_certificate_credentials_t res
,
962 gnutls_x509_crt_t
* cert_list
,
964 gnutls_x509_privkey_t key
)
967 gnutls_privkey_t pkey
;
968 gnutls_pcert_st
*pcerts
= NULL
;
969 gnutls_str_array_t names
;
971 _gnutls_str_array_init(&names
);
973 /* this should be first
975 ret
= gnutls_privkey_init (&pkey
);
983 gnutls_privkey_set_pin_function(pkey
, res
->pin
.cb
, res
->pin
.data
);
985 ret
= gnutls_privkey_import_x509 (pkey
, key
, GNUTLS_PRIVKEY_IMPORT_COPY
);
992 ret
= certificate_credentials_append_pkey (res
, pkey
);
999 /* load certificates */
1000 pcerts
= gnutls_malloc (sizeof (gnutls_pcert_st
) * cert_list_size
);
1004 return GNUTLS_E_MEMORY_ERROR
;
1007 ret
= get_x509_name(cert_list
[0], &names
);
1009 return gnutls_assert_val(ret
);
1011 for (i
= 0; i
< cert_list_size
; i
++)
1013 ret
= gnutls_pcert_import_x509 (&pcerts
[i
], cert_list
[i
], 0);
1021 ret
= certificate_credential_append_crt_list (res
, names
, pcerts
, cert_list_size
);
1030 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1039 _gnutls_str_array_clear(&names
);
1044 * gnutls_certificate_set_key:
1045 * @res: is a #gnutls_certificate_credentials_t structure.
1046 * @names: is an array of DNS name of the certificate (NULL if none)
1047 * @names_size: holds the size of the names list
1048 * @pcert_list: contains a certificate list (path) for the specified private key
1049 * @pcert_list_size: holds the size of the certificate list
1050 * @key: is a #gnutls_privkey_t key
1052 * This function sets a certificate/private key pair in the
1053 * gnutls_certificate_credentials_t structure. This function may be
1054 * called more than once, in case multiple keys/certificates exist for
1055 * the server. For clients that wants to send more than its own end
1056 * entity certificate (e.g., also an intermediate CA cert) then put
1057 * the certificate chain in @pcert_list. The @pcert_list and @key will
1058 * become part of the credentials structure and must not
1059 * be deallocated. They will be automatically deallocated when @res
1062 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1067 gnutls_certificate_set_key (gnutls_certificate_credentials_t res
,
1070 gnutls_pcert_st
* pcert_list
,
1071 int pcert_list_size
,
1072 gnutls_privkey_t key
)
1075 gnutls_str_array_t str_names
;
1077 _gnutls_str_array_init(&str_names
);
1079 if (names
!= NULL
&& names_size
> 0)
1081 for (i
=0;i
<names_size
;i
++)
1083 ret
= _gnutls_str_array_append(&str_names
, names
[i
], strlen(names
[i
]));
1086 ret
= gnutls_assert_val(ret
);
1093 gnutls_privkey_set_pin_function(key
, res
->pin
.cb
, res
->pin
.data
);
1095 ret
= certificate_credentials_append_pkey (res
, key
);
1102 ret
= certificate_credential_append_crt_list (res
, str_names
, pcert_list
, pcert_list_size
);
1111 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1120 _gnutls_str_array_clear(&str_names
);
1125 * gnutls_certificate_set_x509_key_file:
1126 * @res: is a #gnutls_certificate_credentials_t structure.
1127 * @certfile: is a file that containing the certificate list (path) for
1128 * the specified private key, in PKCS7 format, or a list of certificates
1129 * @keyfile: is a file that contains the private key
1130 * @type: is PEM or DER
1132 * This function sets a certificate/private key pair in the
1133 * gnutls_certificate_credentials_t structure. This function may be
1134 * called more than once, in case multiple keys/certificates exist for
1135 * the server. For clients that need to send more than its own end
1136 * entity certificate, e.g., also an intermediate CA cert, then the
1137 * @certfile must contain the ordered certificate chain.
1139 * This function can also accept URLs at @keyfile and @certfile. In that case it
1140 * will import the private key and certificate indicated by the URLs. Note
1141 * that the supported URLs are the ones indicated by gnutls_url_is_supported().
1143 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1146 gnutls_certificate_set_x509_key_file (gnutls_certificate_credentials_t res
,
1147 const char *certfile
,
1148 const char *keyfile
,
1149 gnutls_x509_crt_fmt_t type
)
1153 /* this should be first
1155 if ((ret
= read_key_file (res
, keyfile
, type
)) < 0)
1158 if ((ret
= read_cert_file (res
, certfile
, type
)) < 0)
1163 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
1173 add_new_crt_to_rdn_seq (gnutls_certificate_credentials_t res
, gnutls_x509_crt_t
* crts
,
1174 unsigned int crt_size
)
1179 unsigned char *newdata
;
1182 /* Add DN of the last added CAs to the RDN sequence
1183 * This will be sent to clients when a certificate
1184 * request message is sent.
1187 /* FIXME: in case of a client it is not needed
1188 * to do that. This would save time and memory.
1189 * However we don't have that information available
1191 * Further, this function is now much more efficient,
1192 * so optimizing that is less important.
1195 for (i
= 0; i
< crt_size
; i
++)
1197 if ((ret
= gnutls_x509_crt_get_raw_dn (crts
[i
], &tmp
)) < 0)
1203 newsize
= res
->x509_rdn_sequence
.size
+ 2 + tmp
.size
;
1204 if (newsize
< res
->x509_rdn_sequence
.size
)
1207 _gnutls_free_datum (&tmp
);
1208 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1211 newdata
= gnutls_realloc (res
->x509_rdn_sequence
.data
, newsize
);
1212 if (newdata
== NULL
)
1215 _gnutls_free_datum (&tmp
);
1216 return GNUTLS_E_MEMORY_ERROR
;
1219 _gnutls_write_datum16 (newdata
+ res
->x509_rdn_sequence
.size
, tmp
);
1220 _gnutls_free_datum (&tmp
);
1222 res
->x509_rdn_sequence
.size
= newsize
;
1223 res
->x509_rdn_sequence
.data
= newdata
;
1229 /* Returns 0 if it's ok to use the gnutls_kx_algorithm_t with this
1230 * certificate (uses the KeyUsage field).
1233 _gnutls_check_key_usage (const gnutls_pcert_st
* cert
, gnutls_kx_algorithm_t alg
)
1235 unsigned int key_usage
= 0;
1241 return GNUTLS_E_INTERNAL_ERROR
;
1244 if (_gnutls_map_kx_get_cred (alg
, 1) == GNUTLS_CRD_CERTIFICATE
||
1245 _gnutls_map_kx_get_cred (alg
, 0) == GNUTLS_CRD_CERTIFICATE
)
1248 gnutls_pubkey_get_key_usage(cert
->pubkey
, &key_usage
);
1250 encipher_type
= _gnutls_kx_encipher_type (alg
);
1252 if (key_usage
!= 0 && encipher_type
!= CIPHER_IGN
)
1254 /* If key_usage has been set in the certificate
1257 if (encipher_type
== CIPHER_ENCRYPT
)
1259 /* If the key exchange method requires an encipher
1260 * type algorithm, and key's usage does not permit
1261 * encipherment, then fail.
1263 if (!(key_usage
& GNUTLS_KEY_KEY_ENCIPHERMENT
))
1266 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1270 if (encipher_type
== CIPHER_SIGN
)
1272 /* The same as above, but for sign only keys
1274 if (!(key_usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
))
1277 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1286 parse_pem_ca_mem (gnutls_certificate_credentials_t res
,
1287 const uint8_t * input_cert
, int input_cert_size
)
1289 gnutls_x509_crt_t
*x509_cert_list
;
1290 unsigned int x509_ncerts
;
1294 tmp
.data
= (void*)input_cert
;
1295 tmp
.size
= input_cert_size
;
1297 ret
= gnutls_x509_crt_list_import2( &x509_cert_list
, &x509_ncerts
, &tmp
,
1298 GNUTLS_X509_FMT_PEM
, 0);
1305 if ((ret
= add_new_crt_to_rdn_seq (res
, x509_cert_list
, x509_ncerts
)) < 0)
1311 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, x509_cert_list
, x509_ncerts
, 0);
1319 gnutls_free(x509_cert_list
);
1323 /* Reads a DER encoded certificate list from memory and stores it to a
1324 * gnutls_cert structure. Returns the number of certificates parsed.
1327 parse_der_ca_mem (gnutls_certificate_credentials_t res
,
1328 const void *input_cert
, int input_cert_size
)
1330 gnutls_x509_crt_t crt
;
1334 tmp
.data
= (void*)input_cert
;
1335 tmp
.size
= input_cert_size
;
1337 ret
= gnutls_x509_crt_init( &crt
);
1344 ret
= gnutls_x509_crt_import( crt
, &tmp
, GNUTLS_X509_FMT_DER
);
1351 if ((ret
= add_new_crt_to_rdn_seq (res
, &crt
, 1)) < 0)
1357 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, &crt
, 1, 0);
1367 gnutls_x509_crt_deinit(crt
);
1372 * gnutls_certificate_set_x509_trust_mem:
1373 * @res: is a #gnutls_certificate_credentials_t structure.
1374 * @ca: is a list of trusted CAs or a DER certificate
1375 * @type: is DER or PEM
1377 * This function adds the trusted CAs in order to verify client or
1378 * server certificates. In case of a client this is not required to be
1379 * called if the certificates are not verified using
1380 * gnutls_certificate_verify_peers2(). This function may be called
1383 * In case of a server the CAs set here will be sent to the client if
1384 * a certificate request is sent. This can be disabled using
1385 * gnutls_certificate_send_x509_rdn_sequence().
1387 * Returns: the number of certificates processed or a negative error code
1391 gnutls_certificate_set_x509_trust_mem (gnutls_certificate_credentials_t res
,
1392 const gnutls_datum_t
* ca
,
1393 gnutls_x509_crt_fmt_t type
)
1397 if (type
== GNUTLS_X509_FMT_DER
)
1398 ret
= parse_der_ca_mem (res
,
1399 ca
->data
, ca
->size
);
1401 ret
= parse_pem_ca_mem (res
,
1402 ca
->data
, ca
->size
);
1404 if (ret
== GNUTLS_E_NO_CERTIFICATE_FOUND
)
1411 * gnutls_certificate_set_x509_trust:
1412 * @res: is a #gnutls_certificate_credentials_t structure.
1413 * @ca_list: is a list of trusted CAs
1414 * @ca_list_size: holds the size of the CA list
1416 * This function adds the trusted CAs in order to verify client
1417 * or server certificates. In case of a client this is not required
1418 * to be called if the certificates are not verified using
1419 * gnutls_certificate_verify_peers2().
1420 * This function may be called multiple times.
1422 * In case of a server the CAs set here will be sent to the client if
1423 * a certificate request is sent. This can be disabled using
1424 * gnutls_certificate_send_x509_rdn_sequence().
1426 * Returns: the number of certificates processed or a negative error code
1432 gnutls_certificate_set_x509_trust (gnutls_certificate_credentials_t res
,
1433 gnutls_x509_crt_t
* ca_list
,
1437 gnutls_x509_crt_t new_list
[ca_list_size
];
1439 for (i
= 0; i
< ca_list_size
; i
++)
1441 ret
= gnutls_x509_crt_init (&new_list
[i
]);
1448 ret
= _gnutls_x509_crt_cpy (new_list
[i
], ca_list
[i
]);
1456 if ((ret
= add_new_crt_to_rdn_seq (res
, new_list
, ca_list_size
)) < 0)
1462 ret
= gnutls_x509_trust_list_add_cas(res
->tlist
, new_list
, ca_list_size
, 0);
1473 gnutls_x509_crt_deinit(new_list
[j
]);
1480 * gnutls_certificate_set_x509_trust_file:
1481 * @cred: is a #gnutls_certificate_credentials_t structure.
1482 * @cafile: is a file containing the list of trusted CAs (DER or PEM list)
1483 * @type: is PEM or DER
1485 * This function adds the trusted CAs in order to verify client or
1486 * server certificates. In case of a client this is not required to
1487 * be called if the certificates are not verified using
1488 * gnutls_certificate_verify_peers2(). This function may be called
1491 * In case of a server the names of the CAs set here will be sent to
1492 * the client if a certificate request is sent. This can be disabled
1493 * using gnutls_certificate_send_x509_rdn_sequence().
1495 * This function can also accept URLs. In that case it
1496 * will import all certificates that are marked as trusted. Note
1497 * that the supported URLs are the ones indicated by gnutls_url_is_supported().
1499 * Returns: number of certificates processed, or a negative error code on
1503 gnutls_certificate_set_x509_trust_file (gnutls_certificate_credentials_t cred
,
1505 gnutls_x509_crt_fmt_t type
)
1511 #ifdef ENABLE_PKCS11
1512 if (strncmp (cafile
, "pkcs11:", 7) == 0)
1514 return read_cas_url (cred
, cafile
);
1518 cas
.data
= (void*)read_binary_file (cafile
, &size
);
1519 if (cas
.data
== NULL
)
1522 return GNUTLS_E_FILE_ERROR
;
1527 ret
= gnutls_certificate_set_x509_trust_mem(cred
, &cas
, type
);
1541 * gnutls_certificate_set_x509_system_trust:
1542 * @cred: is a #gnutls_certificate_credentials_t structure.
1544 * This function adds the system's default trusted CAs in order to
1545 * verify client or server certificates.
1547 * In the case the system is currently unsupported %GNUTLS_E_UNIMPLEMENTED_FEATURE
1550 * Returns: the number of certificates processed or a negative error code
1556 gnutls_certificate_set_x509_system_trust (gnutls_certificate_credentials_t cred
)
1558 return gnutls_x509_trust_list_add_system_trust(cred
->tlist
, 0, 0);
1562 parse_pem_crl_mem (gnutls_x509_trust_list_t tlist
,
1563 const char * input_crl
, unsigned int input_crl_size
)
1565 gnutls_x509_crl_t
*x509_crl_list
;
1566 unsigned int x509_ncrls
;
1570 tmp
.data
= (void*)input_crl
;
1571 tmp
.size
= input_crl_size
;
1573 ret
= gnutls_x509_crl_list_import2( &x509_crl_list
, &x509_ncrls
, &tmp
,
1574 GNUTLS_X509_FMT_PEM
, 0);
1581 ret
= gnutls_x509_trust_list_add_crls(tlist
, x509_crl_list
, x509_ncrls
, 0, 0);
1589 gnutls_free(x509_crl_list
);
1593 /* Reads a DER encoded certificate list from memory and stores it to a
1594 * gnutls_cert structure. Returns the number of certificates parsed.
1597 parse_der_crl_mem (gnutls_x509_trust_list_t tlist
,
1598 const void *input_crl
, unsigned int input_crl_size
)
1600 gnutls_x509_crl_t crl
;
1604 tmp
.data
= (void*)input_crl
;
1605 tmp
.size
= input_crl_size
;
1607 ret
= gnutls_x509_crl_init( &crl
);
1614 ret
= gnutls_x509_crl_import( crl
, &tmp
, GNUTLS_X509_FMT_DER
);
1621 ret
= gnutls_x509_trust_list_add_crls(tlist
, &crl
, 1, 0, 0);
1631 gnutls_x509_crl_deinit(crl
);
1637 /* Reads a DER or PEM CRL from memory
1640 read_crl_mem (gnutls_certificate_credentials_t res
, const void *crl
,
1641 int crl_size
, gnutls_x509_crt_fmt_t type
)
1645 if (type
== GNUTLS_X509_FMT_DER
)
1646 ret
= parse_der_crl_mem (res
->tlist
, crl
, crl_size
);
1648 ret
= parse_pem_crl_mem (res
->tlist
, crl
, crl_size
);
1659 * gnutls_certificate_set_x509_crl_mem:
1660 * @res: is a #gnutls_certificate_credentials_t structure.
1661 * @CRL: is a list of trusted CRLs. They should have been verified before.
1662 * @type: is DER or PEM
1664 * This function adds the trusted CRLs in order to verify client or
1665 * server certificates. In case of a client this is not required to
1666 * be called if the certificates are not verified using
1667 * gnutls_certificate_verify_peers2(). This function may be called
1670 * Returns: number of CRLs processed, or a negative error code on error.
1673 gnutls_certificate_set_x509_crl_mem (gnutls_certificate_credentials_t res
,
1674 const gnutls_datum_t
* CRL
,
1675 gnutls_x509_crt_fmt_t type
)
1677 return read_crl_mem (res
, CRL
->data
, CRL
->size
, type
);
1681 * gnutls_certificate_set_x509_crl:
1682 * @res: is a #gnutls_certificate_credentials_t structure.
1683 * @crl_list: is a list of trusted CRLs. They should have been verified before.
1684 * @crl_list_size: holds the size of the crl_list
1686 * This function adds the trusted CRLs in order to verify client or
1687 * server certificates. In case of a client this is not required to
1688 * be called if the certificates are not verified using
1689 * gnutls_certificate_verify_peers2(). This function may be called
1692 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1697 gnutls_certificate_set_x509_crl (gnutls_certificate_credentials_t res
,
1698 gnutls_x509_crl_t
* crl_list
,
1702 gnutls_x509_crl_t new_crl
[crl_list_size
];
1704 for (i
= 0; i
< crl_list_size
; i
++)
1706 ret
= gnutls_x509_crl_init (&new_crl
[i
]);
1713 ret
= _gnutls_x509_crl_cpy (new_crl
[i
], crl_list
[i
]);
1721 ret
= gnutls_x509_trust_list_add_crls(res
->tlist
, new_crl
, crl_list_size
, 0, 0);
1732 gnutls_x509_crl_deinit(new_crl
[j
]);
1738 * gnutls_certificate_set_x509_crl_file:
1739 * @res: is a #gnutls_certificate_credentials_t structure.
1740 * @crlfile: is a file containing the list of verified CRLs (DER or PEM list)
1741 * @type: is PEM or DER
1743 * This function adds the trusted CRLs in order to verify client or server
1744 * certificates. In case of a client this is not required
1745 * to be called if the certificates are not verified using
1746 * gnutls_certificate_verify_peers2().
1747 * This function may be called multiple times.
1749 * Returns: number of CRLs processed or a negative error code on error.
1752 gnutls_certificate_set_x509_crl_file (gnutls_certificate_credentials_t res
,
1753 const char *crlfile
,
1754 gnutls_x509_crt_fmt_t type
)
1758 char *data
= (void*)read_binary_file (crlfile
, &size
);
1763 return GNUTLS_E_FILE_ERROR
;
1766 if (type
== GNUTLS_X509_FMT_DER
)
1767 ret
= parse_der_crl_mem (res
->tlist
, data
, size
);
1769 ret
= parse_pem_crl_mem (res
->tlist
, data
, size
);
1782 #include <gnutls/pkcs12.h>
1786 * gnutls_certificate_set_x509_simple_pkcs12_file:
1787 * @res: is a #gnutls_certificate_credentials_t structure.
1788 * @pkcs12file: filename of file containing PKCS#12 blob.
1789 * @type: is PEM or DER of the @pkcs12file.
1790 * @password: optional password used to decrypt PKCS#12 file, bags and keys.
1792 * This function sets a certificate/private key pair and/or a CRL in
1793 * the gnutls_certificate_credentials_t structure. This function may
1794 * be called more than once (in case multiple keys/certificates exist
1797 * PKCS#12 files with a MAC, encrypted bags and PKCS #8
1798 * private keys are supported. However,
1799 * only password based security, and the same password for all
1800 * operations, are supported.
1802 * PKCS#12 file may contain many keys and/or certificates, and there
1803 * is no way to identify which key/certificate pair you want. You
1804 * should make sure the PKCS#12 file only contain one key/certificate
1805 * pair and/or one CRL.
1807 * It is believed that the limitations of this function is acceptable
1808 * for most usage, and that any more flexibility would introduce
1809 * complexity that would make it harder to use this functionality at
1812 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1815 gnutls_certificate_set_x509_simple_pkcs12_file
1816 (gnutls_certificate_credentials_t res
, const char *pkcs12file
,
1817 gnutls_x509_crt_fmt_t type
, const char *password
)
1819 gnutls_datum_t p12blob
;
1823 p12blob
.data
= (void*)read_binary_file (pkcs12file
, &size
);
1824 p12blob
.size
= (unsigned int) size
;
1825 if (p12blob
.data
== NULL
)
1828 return GNUTLS_E_FILE_ERROR
;
1832 gnutls_certificate_set_x509_simple_pkcs12_mem (res
, &p12blob
, type
,
1834 free (p12blob
.data
);
1840 * gnutls_certificate_set_x509_simple_pkcs12_mem:
1841 * @res: is a #gnutls_certificate_credentials_t structure.
1842 * @p12blob: the PKCS#12 blob.
1843 * @type: is PEM or DER of the @pkcs12file.
1844 * @password: optional password used to decrypt PKCS#12 file, bags and keys.
1846 * This function sets a certificate/private key pair and/or a CRL in
1847 * the gnutls_certificate_credentials_t structure. This function may
1848 * be called more than once (in case multiple keys/certificates exist
1851 * Encrypted PKCS#12 bags and PKCS#8 private keys are supported. However,
1852 * only password based security, and the same password for all
1853 * operations, are supported.
1855 * PKCS#12 file may contain many keys and/or certificates, and there
1856 * is no way to identify which key/certificate pair you want. You
1857 * should make sure the PKCS#12 file only contain one key/certificate
1858 * pair and/or one CRL.
1860 * It is believed that the limitations of this function is acceptable
1861 * for most usage, and that any more flexibility would introduce
1862 * complexity that would make it harder to use this functionality at
1865 * Returns: %GNUTLS_E_SUCCESS (0) on success, or a negative error code.
1870 gnutls_certificate_set_x509_simple_pkcs12_mem
1871 (gnutls_certificate_credentials_t res
, const gnutls_datum_t
* p12blob
,
1872 gnutls_x509_crt_fmt_t type
, const char *password
)
1874 gnutls_pkcs12_t p12
;
1875 gnutls_x509_privkey_t key
= NULL
;
1876 gnutls_x509_crt_t
*chain
= NULL
;
1877 gnutls_x509_crl_t crl
= NULL
;
1878 unsigned int chain_size
= 0, i
;
1881 ret
= gnutls_pkcs12_init (&p12
);
1888 ret
= gnutls_pkcs12_import (p12
, p12blob
, type
, 0);
1892 gnutls_pkcs12_deinit (p12
);
1898 ret
= gnutls_pkcs12_verify_mac (p12
, password
);
1902 gnutls_pkcs12_deinit (p12
);
1907 ret
= gnutls_pkcs12_simple_parse (p12
, password
, &key
, &chain
, &chain_size
,
1908 NULL
, NULL
, &crl
, 0);
1909 gnutls_pkcs12_deinit (p12
);
1918 ret
= gnutls_certificate_set_x509_key (res
, chain
, chain_size
, key
);
1928 ret
= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1934 ret
= gnutls_certificate_set_x509_crl (res
, &crl
, 1);
1947 for (i
=0;i
<chain_size
;i
++)
1948 gnutls_x509_crt_deinit (chain
[i
]);
1952 gnutls_x509_privkey_deinit (key
);
1954 gnutls_x509_crl_deinit (crl
);
1962 * gnutls_certificate_free_crls:
1963 * @sc: is a #gnutls_certificate_credentials_t structure.
1965 * This function will delete all the CRLs associated
1966 * with the given credentials.
1969 gnutls_certificate_free_crls (gnutls_certificate_credentials_t sc
)
1971 /* do nothing for now */
1976 * gnutls_certificate_credentials_t:
1977 * @cred: is a #gnutls_certificate_credentials_t structure.
1978 * @fn: A PIN callback
1979 * @userdata: Data to be passed in the callback
1981 * This function will set a callback function to be used when
1982 * required to access a protected object. This function overrides any other
1983 * global PIN functions.
1985 * Note that this function must be called right after initialization
1990 void gnutls_certificate_set_pin_function (gnutls_certificate_credentials_t cred
,
1991 gnutls_pin_callback_t fn
, void *userdata
)
1994 cred
->pin
.data
= userdata
;