2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <gnutls_int.h>
26 #include "gnutls_auth_int.h"
27 #include "gnutls_errors.h"
28 #include <gnutls_cert.h>
29 #include <auth_cert.h>
30 #include "gnutls_dh.h"
31 #include "gnutls_num.h"
33 #include "gnutls_datum.h"
34 #include <gnutls_pk.h>
35 #include <gnutls_algorithms.h>
36 #include <gnutls_global.h>
37 #include <gnutls_record.h>
38 #include <gnutls_sig.h>
39 #include <gnutls_state.h>
40 #include <gnutls_pk.h>
41 #include <gnutls_str.h>
44 #include <gnutls_x509.h>
45 #include "x509/common.h"
46 #include "x509/x509.h"
47 #include "x509/verify.h"
49 #include "x509/pkcs7.h"
50 #include "x509/privkey.h"
51 #include "read-file.h"
54 * some x509 certificate parsing functions.
57 /* Check if the number of bits of the key in the certificate
61 check_bits (gnutls_x509_crt_t crt
, unsigned int max_bits
)
66 ret
= gnutls_x509_crt_get_pk_algorithm (crt
, &bits
);
76 return GNUTLS_E_CONSTRAINT_ERROR
;
83 #define CLEAR_CERTS for(x=0;x<peer_certificate_list_size;x++) { \
84 if (peer_certificate_list[x]) \
85 gnutls_x509_crt_deinit(peer_certificate_list[x]); \
87 gnutls_free( peer_certificate_list)
90 * _gnutls_x509_cert_verify_peers - This function returns the peer's certificate status
91 * @session: is a gnutls session
93 * This function will try to verify the peer's certificate and return its status (TRUSTED, REVOKED etc.).
94 * The return value (status) should be one of the gnutls_certificate_status_t enumerated elements.
95 * However you must also check the peer's name in order to check if the verified certificate belongs to the
96 * actual peer. Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
100 _gnutls_x509_cert_verify_peers (gnutls_session_t session
,
101 unsigned int *status
)
103 cert_auth_info_t info
;
104 gnutls_certificate_credentials_t cred
;
105 gnutls_x509_crt_t
*peer_certificate_list
;
106 int peer_certificate_list_size
, i
, x
, ret
;
108 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
110 info
= _gnutls_get_auth_info (session
);
114 return GNUTLS_E_INVALID_REQUEST
;
117 cred
= (gnutls_certificate_credentials_t
)
118 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
122 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
125 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
126 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
128 if (info
->ncerts
> cred
->verify_depth
)
131 return GNUTLS_E_CONSTRAINT_ERROR
;
134 /* generate a list of gnutls_certs based on the auth info
137 peer_certificate_list_size
= info
->ncerts
;
138 peer_certificate_list
=
140 peer_certificate_list_size
* sizeof (gnutls_x509_crt_t
));
141 if (peer_certificate_list
== NULL
)
144 return GNUTLS_E_MEMORY_ERROR
;
147 for (i
= 0; i
< peer_certificate_list_size
; i
++)
149 ret
= gnutls_x509_crt_init (&peer_certificate_list
[i
]);
158 gnutls_x509_crt_import (peer_certificate_list
[i
],
159 &info
->raw_certificate_list
[i
],
160 GNUTLS_X509_FMT_DER
);
168 ret
= check_bits (peer_certificate_list
[i
], cred
->verify_bits
);
178 /* Verify certificate
181 gnutls_x509_crt_list_verify (peer_certificate_list
,
182 peer_certificate_list_size
,
183 cred
->x509_ca_list
, cred
->x509_ncas
,
184 cred
->x509_crl_list
, cred
->x509_ncrls
,
185 cred
->verify_flags
, status
);
199 * Read certificates and private keys, from files, memory etc.
202 /* returns error if the certificate has different algorithm than
203 * the given key parameters.
206 _gnutls_check_key_cert_match (gnutls_certificate_credentials_t res
)
210 unsigned pk
= res
->cert_list
[res
->ncerts
- 1][0].subject_pk_algorithm
;
212 if (res
->pkey
[res
->ncerts
- 1].pk_algorithm
!= pk
)
215 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH
;
218 if (pk
== GNUTLS_PK_RSA
)
220 _gnutls_x509_write_rsa_params (res
->pkey
[res
->ncerts
- 1].params
,
221 res
->pkey
[res
->ncerts
-
222 1].params_size
, &kid
);
225 _gnutls_x509_write_rsa_params (res
->cert_list
[res
->ncerts
- 1][0].
227 res
->cert_list
[res
->ncerts
-
228 1][0].params_size
, &cid
);
230 else if (pk
== GNUTLS_PK_DSA
)
233 _gnutls_x509_write_dsa_params (res
->pkey
[res
->ncerts
- 1].params
,
234 res
->pkey
[res
->ncerts
-
235 1].params_size
, &kid
);
237 _gnutls_x509_write_dsa_params (res
->cert_list
[res
->ncerts
- 1][0].
239 res
->cert_list
[res
->ncerts
-
240 1][0].params_size
, &cid
);
243 if (cid
.size
!= kid
.size
)
246 _gnutls_free_datum (&kid
);
247 _gnutls_free_datum (&cid
);
248 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH
;
251 if (memcmp (kid
.data
, cid
.data
, kid
.size
) != 0)
254 _gnutls_free_datum (&kid
);
255 _gnutls_free_datum (&cid
);
256 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH
;
259 _gnutls_free_datum (&kid
);
260 _gnutls_free_datum (&cid
);
264 /* Reads a DER encoded certificate list from memory and stores it to
265 * a gnutls_cert structure. This is only called if PKCS7 read fails.
266 * returns the number of certificates parsed (1)
269 parse_crt_mem (gnutls_cert
** cert_list
, unsigned *ncerts
,
270 gnutls_x509_crt_t cert
)
278 (gnutls_cert
*) gnutls_realloc_fast (*cert_list
,
279 i
* sizeof (gnutls_cert
));
281 if (*cert_list
== NULL
)
284 return GNUTLS_E_MEMORY_ERROR
;
287 ret
= _gnutls_x509_crt_to_gcert (&cert_list
[0][i
- 1], cert
, 0);
296 return 1; /* one certificate parsed */
299 /* Reads a DER encoded certificate list from memory and stores it to
300 * a gnutls_cert structure. This is only called if PKCS7 read fails.
301 * returns the number of certificates parsed (1)
304 parse_der_cert_mem (gnutls_cert
** cert_list
, unsigned *ncerts
,
305 const void *input_cert
, int input_cert_size
)
308 gnutls_x509_crt_t cert
;
311 ret
= gnutls_x509_crt_init (&cert
);
318 tmp
.data
= (opaque
*) input_cert
;
319 tmp
.size
= input_cert_size
;
321 ret
= gnutls_x509_crt_import (cert
, &tmp
, GNUTLS_X509_FMT_DER
);
325 gnutls_x509_crt_deinit (cert
);
329 ret
= parse_crt_mem (cert_list
, ncerts
, cert
);
330 gnutls_x509_crt_deinit (cert
);
338 /* Reads a PKCS7 base64 encoded certificate list from memory and stores it to
339 * a gnutls_cert structure.
340 * returns the number of certificate parsed
343 parse_pkcs7_cert_mem (gnutls_cert
** cert_list
, unsigned *ncerts
, const
344 void *input_cert
, int input_cert_size
, int flags
)
348 gnutls_datum_t tmp
, tmp2
;
350 opaque
*pcert
= NULL
;
352 gnutls_pkcs7_t pkcs7
;
354 ret
= gnutls_pkcs7_init (&pkcs7
);
361 if (flags
& CERT_PEM
)
362 ret
= gnutls_pkcs7_import (pkcs7
, &tmp
, GNUTLS_X509_FMT_PEM
);
364 ret
= gnutls_pkcs7_import (pkcs7
, &tmp
, GNUTLS_X509_FMT_DER
);
367 /* if we failed to read the structure,
368 * then just try to decode a plain DER
372 gnutls_pkcs7_deinit (pkcs7
);
374 return parse_der_cert_mem (cert_list
, ncerts
,
375 input_cert
, input_cert_size
);
381 /* tmp now contains the decoded certificate list */
382 tmp
.data
= (opaque
*) input_cert
;
383 tmp
.size
= input_cert_size
;
385 ret
= gnutls_pkcs7_get_crt_count (pkcs7
);
390 gnutls_pkcs7_deinit (pkcs7
);
399 ret
= gnutls_pkcs7_get_crt_raw (pkcs7
, j
, NULL
, &pcert_size
);
400 if (ret
!= GNUTLS_E_MEMORY_ERROR
)
406 pcert
= gnutls_malloc (pcert_size
);
407 if (ret
== GNUTLS_E_MEMORY_ERROR
)
414 /* read the certificate
416 ret
= gnutls_pkcs7_get_crt_raw (pkcs7
, j
, pcert
, &pcert_size
);
423 (gnutls_cert
*) gnutls_realloc_fast (*cert_list
,
424 i
* sizeof (gnutls_cert
));
426 if (*cert_list
== NULL
)
430 return GNUTLS_E_MEMORY_ERROR
;
434 tmp2
.size
= pcert_size
;
437 _gnutls_x509_raw_cert_to_gcert (&cert_list
[0][i
- 1], &tmp2
, 0);
442 gnutls_pkcs7_deinit (pkcs7
);
453 while (ret
>= 0 && j
>= 0);
457 gnutls_pkcs7_deinit (pkcs7
);
462 /* Reads a base64 encoded certificate list from memory and stores it to
463 * a gnutls_cert structure. Returns the number of certificate parsed.
466 parse_pem_cert_mem (gnutls_cert
** cert_list
, unsigned *ncerts
,
467 const char *input_cert
, int input_cert_size
)
476 if ((ptr
= memmem (input_cert
, input_cert_size
,
477 PEM_PKCS7_SEP
, sizeof (PEM_PKCS7_SEP
) - 1)) != NULL
)
481 ret
= parse_pkcs7_cert_mem (cert_list
, ncerts
, ptr
, size
, CERT_PEM
);
487 /* move to the certificate
489 ptr
= memmem (input_cert
, input_cert_size
,
490 PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
492 ptr
= memmem (input_cert
, input_cert_size
,
493 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
498 return GNUTLS_E_BASE64_DECODING_ERROR
;
500 size
= input_cert_size
- (ptr
- input_cert
);
508 siz2
= _gnutls_fbase64_decode (NULL
, ptr
, size
, &ptr2
);
513 return GNUTLS_E_BASE64_DECODING_ERROR
;
517 (gnutls_cert
*) gnutls_realloc_fast (*cert_list
,
518 i
* sizeof (gnutls_cert
));
520 if (*cert_list
== NULL
)
523 return GNUTLS_E_MEMORY_ERROR
;
529 ret
= _gnutls_x509_raw_cert_to_gcert (&cert_list
[0][i
- 1], &tmp
, 0);
535 _gnutls_free_datum (&tmp
); /* free ptr2 */
537 /* now we move ptr after the pem header
540 /* find the next certificate (if any)
542 size
= input_cert_size
- (ptr
- input_cert
);
548 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
550 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP2
,
551 sizeof (PEM_CERT_SEP2
) - 1);
571 /* Reads a DER or PEM certificate from memory
574 read_cert_mem (gnutls_certificate_credentials_t res
, const void *cert
,
575 int cert_size
, gnutls_x509_crt_fmt_t type
)
579 /* allocate space for the certificate to add
581 res
->cert_list
= gnutls_realloc_fast (res
->cert_list
,
584 sizeof (gnutls_cert
*));
585 if (res
->cert_list
== NULL
)
588 return GNUTLS_E_MEMORY_ERROR
;
591 res
->cert_list_length
= gnutls_realloc_fast (res
->cert_list_length
,
593 res
->ncerts
) * sizeof (int));
594 if (res
->cert_list_length
== NULL
)
597 return GNUTLS_E_MEMORY_ERROR
;
600 res
->cert_list
[res
->ncerts
] = NULL
; /* for realloc */
601 res
->cert_list_length
[res
->ncerts
] = 0;
603 if (type
== GNUTLS_X509_FMT_DER
)
604 ret
= parse_pkcs7_cert_mem (&res
->cert_list
[res
->ncerts
],
605 &res
->cert_list_length
[res
->ncerts
],
609 parse_pem_cert_mem (&res
->cert_list
[res
->ncerts
],
610 &res
->cert_list_length
[res
->ncerts
], cert
,
624 _gnutls_x509_privkey_to_gkey (gnutls_privkey
* dest
,
625 gnutls_x509_privkey_t src
)
629 memset (dest
, 0, sizeof (gnutls_privkey
));
631 for (i
= 0; i
< src
->params_size
; i
++)
633 dest
->params
[i
] = _gnutls_mpi_copy (src
->params
[i
]);
634 if (dest
->params
[i
] == NULL
)
637 ret
= GNUTLS_E_MEMORY_ERROR
;
642 dest
->pk_algorithm
= src
->pk_algorithm
;
643 dest
->params_size
= src
->params_size
;
649 for (i
= 0; i
< src
->params_size
; i
++)
651 _gnutls_mpi_release (&dest
->params
[i
]);
657 _gnutls_gkey_deinit (gnutls_privkey
* key
)
663 for (i
= 0; i
< key
->params_size
; i
++)
665 _gnutls_mpi_release (&key
->params
[i
]);
670 _gnutls_x509_raw_privkey_to_gkey (gnutls_privkey
* privkey
,
671 const gnutls_datum_t
* raw_key
,
672 gnutls_x509_crt_fmt_t type
)
674 gnutls_x509_privkey_t tmpkey
;
677 ret
= gnutls_x509_privkey_init (&tmpkey
);
684 ret
= gnutls_x509_privkey_import (tmpkey
, raw_key
, type
);
688 gnutls_x509_privkey_deinit (tmpkey
);
692 ret
= _gnutls_x509_privkey_to_gkey (privkey
, tmpkey
);
696 gnutls_x509_privkey_deinit (tmpkey
);
700 gnutls_x509_privkey_deinit (tmpkey
);
705 /* Reads a PEM encoded PKCS-1 RSA private key from memory
706 * 2002-01-26: Added ability to read DSA keys.
707 * type indicates the certificate format.
710 read_key_mem (gnutls_certificate_credentials_t res
,
711 const void *key
, int key_size
, gnutls_x509_crt_fmt_t type
)
716 /* allocate space for the pkey list
719 gnutls_realloc_fast (res
->pkey
,
720 (res
->ncerts
+ 1) * sizeof (gnutls_privkey
));
721 if (res
->pkey
== NULL
)
724 return GNUTLS_E_MEMORY_ERROR
;
727 tmp
.data
= (opaque
*) key
;
731 _gnutls_x509_raw_privkey_to_gkey (&res
->pkey
[res
->ncerts
], &tmp
, type
);
741 /* Reads a certificate file
744 read_cert_file (gnutls_certificate_credentials_t res
,
745 const char *certfile
, gnutls_x509_crt_fmt_t type
)
749 char *data
= read_binary_file (certfile
, &size
);
754 return GNUTLS_E_FILE_ERROR
;
757 ret
= read_cert_mem (res
, data
, size
, type
);
766 /* Reads PKCS-1 RSA private key file or a DSA file (in the format openssl
770 read_key_file (gnutls_certificate_credentials_t res
,
771 const char *keyfile
, gnutls_x509_crt_fmt_t type
)
775 char *data
= read_binary_file (keyfile
, &size
);
780 return GNUTLS_E_FILE_ERROR
;
783 ret
= read_key_mem (res
, data
, size
, type
);
790 * gnutls_certificate_set_x509_key_mem - Used to set keys in a gnutls_certificate_credentials_t structure
791 * @res: is an #gnutls_certificate_credentials_t structure.
792 * @cert: contains a certificate list (path) for the specified private key
793 * @key: is the private key
794 * @type: is PEM or DER
796 * This function sets a certificate/private key pair in the
797 * gnutls_certificate_credentials_t structure. This function may be called
798 * more than once (in case multiple keys/certificates exist for the
801 * Currently are supported: RSA PKCS-1 encoded private keys,
804 * DSA private keys are encoded the OpenSSL way, which is an ASN.1
805 * DER sequence of 6 INTEGERs - version, p, q, g, pub, priv.
807 * Note that the keyUsage (2.5.29.15) PKIX extension in X.509 certificates
808 * is supported. This means that certificates intended for signing cannot
809 * be used for ciphersuites that require encryption.
811 * If the certificate and the private key are given in PEM encoding
812 * then the strings that hold their values must be null terminated.
816 gnutls_certificate_set_x509_key_mem (gnutls_certificate_credentials_t
817 res
, const gnutls_datum_t
* cert
,
818 const gnutls_datum_t
* key
,
819 gnutls_x509_crt_fmt_t type
)
823 /* this should be first
825 if ((ret
= read_key_mem (res
, key
->data
, key
->size
, type
)) < 0)
828 if ((ret
= read_cert_mem (res
, cert
->data
, cert
->size
, type
)) < 0)
833 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
843 * gnutls_certificate_set_x509_key - Used to set keys in a gnutls_certificate_credentials_t structure
844 * @res: is an #gnutls_certificate_credentials_t structure.
845 * @cert_list: contains a certificate list (path) for the specified private key
846 * @cert_list_size: holds the size of the certificate list
847 * @key: is a gnutls_x509_privkey_t key
849 * This function sets a certificate/private key pair in the
850 * gnutls_certificate_credentials_t structure. This function may be called
851 * more than once (in case multiple keys/certificates exist for the
856 gnutls_certificate_set_x509_key (gnutls_certificate_credentials_t res
,
857 gnutls_x509_crt_t
* cert_list
,
859 gnutls_x509_privkey_t key
)
863 /* this should be first
867 gnutls_realloc_fast (res
->pkey
,
868 (res
->ncerts
+ 1) * sizeof (gnutls_privkey
));
869 if (res
->pkey
== NULL
)
872 return GNUTLS_E_MEMORY_ERROR
;
875 ret
= _gnutls_x509_privkey_to_gkey (&res
->pkey
[res
->ncerts
], key
);
882 res
->cert_list
= gnutls_realloc_fast (res
->cert_list
,
885 sizeof (gnutls_cert
*));
886 if (res
->cert_list
== NULL
)
889 return GNUTLS_E_MEMORY_ERROR
;
892 res
->cert_list_length
= gnutls_realloc_fast (res
->cert_list_length
,
894 res
->ncerts
) * sizeof (int));
895 if (res
->cert_list_length
== NULL
)
898 return GNUTLS_E_MEMORY_ERROR
;
901 res
->cert_list
[res
->ncerts
] = NULL
; /* for realloc */
902 res
->cert_list_length
[res
->ncerts
] = 0;
905 for (i
= 0; i
< cert_list_size
; i
++)
907 ret
= parse_crt_mem (&res
->cert_list
[res
->ncerts
],
908 &res
->cert_list_length
[res
->ncerts
], cert_list
[i
]);
917 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
927 * gnutls_certificate_set_x509_key_file - Used to set keys in a gnutls_certificate_credentials_t structure
928 * @res: is an #gnutls_certificate_credentials_t structure.
929 * @CERTFILE: is a file that containing the certificate list (path) for
930 * the specified private key, in PKCS7 format, or a list of certificates
931 * @KEYFILE: is a file that contains the private key
932 * @type: is PEM or DER
934 * This function sets a certificate/private key pair in the
935 * gnutls_certificate_credentials_t structure. This function may be called
936 * more than once (in case multiple keys/certificates exist for the
939 * Currently only PKCS-1 encoded RSA and DSA private keys are accepted by
944 gnutls_certificate_set_x509_key_file (gnutls_certificate_credentials_t
945 res
, const char *CERTFILE
,
947 gnutls_x509_crt_fmt_t type
)
951 /* this should be first
953 if ((ret
= read_key_file (res
, KEYFILE
, type
)) < 0)
956 if ((ret
= read_cert_file (res
, CERTFILE
, type
)) < 0)
961 if ((ret
= _gnutls_check_key_cert_match (res
)) < 0)
971 generate_rdn_seq (gnutls_certificate_credentials_t res
)
978 /* Generate the RDN sequence
979 * This will be sent to clients when a certificate
980 * request message is sent.
983 /* FIXME: in case of a client it is not needed
984 * to do that. This would save time and memory.
985 * However we don't have that information available
990 for (i
= 0; i
< res
->x509_ncas
; i
++)
992 if ((ret
= gnutls_x509_crt_get_raw_dn (res
->x509_ca_list
[i
], &tmp
)) < 0)
997 size
+= (2 + tmp
.size
);
998 _gnutls_free_datum (&tmp
);
1001 if (res
->x509_rdn_sequence
.data
!= NULL
)
1002 gnutls_free (res
->x509_rdn_sequence
.data
);
1004 res
->x509_rdn_sequence
.data
= gnutls_malloc (size
);
1005 if (res
->x509_rdn_sequence
.data
== NULL
)
1008 return GNUTLS_E_MEMORY_ERROR
;
1010 res
->x509_rdn_sequence
.size
= size
;
1012 pdata
= res
->x509_rdn_sequence
.data
;
1014 for (i
= 0; i
< res
->x509_ncas
; i
++)
1016 if ((ret
= gnutls_x509_crt_get_raw_dn (res
->x509_ca_list
[i
], &tmp
)) < 0)
1018 _gnutls_free_datum (&res
->x509_rdn_sequence
);
1023 _gnutls_write_datum16 (pdata
, tmp
);
1024 pdata
+= (2 + tmp
.size
);
1025 _gnutls_free_datum (&tmp
);
1034 /* Returns 0 if it's ok to use the gnutls_kx_algorithm_t with this
1035 * certificate (uses the KeyUsage field).
1038 _gnutls_check_key_usage (const gnutls_cert
* cert
, gnutls_kx_algorithm_t alg
)
1040 unsigned int key_usage
= 0;
1046 return GNUTLS_E_INTERNAL_ERROR
;
1049 if (_gnutls_map_kx_get_cred (alg
, 1) == GNUTLS_CRD_CERTIFICATE
||
1050 _gnutls_map_kx_get_cred (alg
, 0) == GNUTLS_CRD_CERTIFICATE
)
1053 key_usage
= cert
->key_usage
;
1055 encipher_type
= _gnutls_kx_encipher_type (alg
);
1057 if (key_usage
!= 0 && encipher_type
!= CIPHER_IGN
)
1059 /* If key_usage has been set in the certificate
1062 if (encipher_type
== CIPHER_ENCRYPT
)
1064 /* If the key exchange method requires an encipher
1065 * type algorithm, and key's usage does not permit
1066 * encipherment, then fail.
1068 if (!(key_usage
& KEY_KEY_ENCIPHERMENT
))
1071 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1075 if (encipher_type
== CIPHER_SIGN
)
1077 /* The same as above, but for sign only keys
1079 if (!(key_usage
& KEY_DIGITAL_SIGNATURE
))
1082 return GNUTLS_E_KEY_USAGE_VIOLATION
;
1093 parse_pem_ca_mem (gnutls_x509_crt_t
** cert_list
, unsigned *ncerts
,
1094 const opaque
* input_cert
, int input_cert_size
)
1101 /* move to the certificate
1103 ptr
= memmem (input_cert
, input_cert_size
,
1104 PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
1106 ptr
= memmem (input_cert
, input_cert_size
,
1107 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
1112 return GNUTLS_E_BASE64_DECODING_ERROR
;
1114 size
= input_cert_size
- (ptr
- input_cert
);
1123 (gnutls_x509_crt_t
*) gnutls_realloc_fast (*cert_list
,
1126 (gnutls_x509_crt_t
));
1128 if (*cert_list
== NULL
)
1131 return GNUTLS_E_MEMORY_ERROR
;
1134 ret
= gnutls_x509_crt_init (&cert_list
[0][i
- 1]);
1141 tmp
.data
= (opaque
*) ptr
;
1145 gnutls_x509_crt_import (cert_list
[0][i
- 1],
1146 &tmp
, GNUTLS_X509_FMT_PEM
);
1153 /* now we move ptr after the pem header
1157 /* find the next certificate (if any)
1164 ptr3
= memmem (ptr
, size
, PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
1166 ptr3
= memmem (ptr
, size
,
1167 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
1170 size
= input_cert_size
- (ptr
- input_cert
);
1179 while (ptr
!= NULL
);
1186 /* Reads a DER encoded certificate list from memory and stores it to
1187 * a gnutls_cert structure. This is only called if PKCS7 read fails.
1188 * returns the number of certificates parsed (1)
1191 parse_der_ca_mem (gnutls_x509_crt_t
** cert_list
, unsigned *ncerts
,
1192 const void *input_cert
, int input_cert_size
)
1201 (gnutls_x509_crt_t
*) gnutls_realloc_fast (*cert_list
,
1203 sizeof (gnutls_x509_crt_t
));
1205 if (*cert_list
== NULL
)
1208 return GNUTLS_E_MEMORY_ERROR
;
1211 tmp
.data
= (opaque
*) input_cert
;
1212 tmp
.size
= input_cert_size
;
1214 ret
= gnutls_x509_crt_init (&cert_list
[0][i
- 1]);
1222 gnutls_x509_crt_import (cert_list
[0][i
- 1], &tmp
, GNUTLS_X509_FMT_DER
);
1231 return 1; /* one certificate parsed */
1235 * gnutls_certificate_set_x509_trust_mem - Used to add trusted CAs in a gnutls_certificate_credentials_t structure
1236 * @res: is an #gnutls_certificate_credentials_t structure.
1237 * @ca: is a list of trusted CAs or a DER certificate
1238 * @type: is DER or PEM
1240 * This function adds the trusted CAs in order to verify client
1241 * or server certificates. In case of a client this is not required
1242 * to be called if the certificates are not verified using
1243 * gnutls_certificate_verify_peers2().
1244 * This function may be called multiple times.
1246 * In case of a server the CAs set here will be sent to the client if
1247 * a certificate request is sent. This can be disabled using
1248 * gnutls_certificate_send_x509_rdn_sequence().
1250 * Returns the number of certificates processed or a negative
1255 gnutls_certificate_set_x509_trust_mem (gnutls_certificate_credentials_t
1256 res
, const gnutls_datum_t
* ca
,
1257 gnutls_x509_crt_fmt_t type
)
1261 if (type
== GNUTLS_X509_FMT_DER
)
1262 ret
= parse_der_ca_mem (&res
->x509_ca_list
, &res
->x509_ncas
,
1263 ca
->data
, ca
->size
);
1265 ret
= parse_pem_ca_mem (&res
->x509_ca_list
, &res
->x509_ncas
,
1266 ca
->data
, ca
->size
);
1268 if ((ret2
= generate_rdn_seq (res
)) < 0)
1275 * gnutls_certificate_set_x509_trust - Used to add trusted CAs in a gnutls_certificate_credentials_t structure
1276 * @res: is an #gnutls_certificate_credentials_t structure.
1277 * @ca_list: is a list of trusted CAs
1278 * @ca_list_size: holds the size of the CA list
1280 * This function adds the trusted CAs in order to verify client
1281 * or server certificates. In case of a client this is not required
1282 * to be called if the certificates are not verified using
1283 * gnutls_certificate_verify_peers2().
1284 * This function may be called multiple times.
1286 * In case of a server the CAs set here will be sent to the client if
1287 * a certificate request is sent. This can be disabled using
1288 * gnutls_certificate_send_x509_rdn_sequence().
1290 * Returns 0 on success.
1294 gnutls_certificate_set_x509_trust (gnutls_certificate_credentials_t res
,
1295 gnutls_x509_crt_t
* ca_list
,
1300 res
->x509_ca_list
= gnutls_realloc_fast (res
->x509_ca_list
,
1303 sizeof (gnutls_x509_crt_t
));
1304 if (res
->x509_ca_list
== NULL
)
1307 return GNUTLS_E_MEMORY_ERROR
;
1310 for (i
= 0; i
< ca_list_size
; i
++)
1312 ret
= gnutls_x509_crt_init (&res
->x509_ca_list
[ res
->x509_ncas
]);
1319 ret
= _gnutls_x509_crt_cpy (res
->x509_ca_list
[ res
->x509_ncas
],
1324 gnutls_x509_crt_deinit (res
->x509_ca_list
[ res
->x509_ncas
]);
1330 if ((ret2
= generate_rdn_seq (res
)) < 0)
1337 * gnutls_certificate_set_x509_trust_file - Used to add trusted CAs in a gnutls_certificate_credentials_t structure
1338 * @res: is an #gnutls_certificate_credentials_t structure.
1339 * @cafile: is a file containing the list of trusted CAs (DER or PEM list)
1340 * @type: is PEM or DER
1342 * This function adds the trusted CAs in order to verify client
1343 * or server certificates. In case of a client this is not required
1344 * to be called if the certificates are not verified using
1345 * gnutls_certificate_verify_peers2().
1346 * This function may be called multiple times.
1348 * In case of a server the names of the CAs set here will be sent to the
1349 * client if a certificate request is sent. This can be disabled using
1350 * gnutls_certificate_send_x509_rdn_sequence().
1352 * Returns the number of certificates processed or a negative
1357 gnutls_certificate_set_x509_trust_file (gnutls_certificate_credentials_t
1358 res
, const char *cafile
,
1359 gnutls_x509_crt_fmt_t type
)
1363 char *data
= read_binary_file (cafile
, &size
);
1368 return GNUTLS_E_FILE_ERROR
;
1371 if (type
== GNUTLS_X509_FMT_DER
)
1372 ret
= parse_der_ca_mem (&res
->x509_ca_list
, &res
->x509_ncas
,
1375 ret
= parse_pem_ca_mem (&res
->x509_ca_list
, &res
->x509_ncas
,
1386 if ((ret2
= generate_rdn_seq (res
)) < 0)
1395 parse_pem_crl_mem (gnutls_x509_crl_t
** crl_list
, unsigned *ncrls
,
1396 const opaque
* input_crl
, int input_crl_size
)
1403 /* move to the certificate
1405 ptr
= memmem (input_crl
, input_crl_size
,
1406 PEM_CRL_SEP
, sizeof (PEM_CRL_SEP
) - 1);
1410 return GNUTLS_E_BASE64_DECODING_ERROR
;
1413 size
= input_crl_size
- (ptr
- input_crl
);
1422 (gnutls_x509_crl_t
*) gnutls_realloc_fast (*crl_list
,
1425 (gnutls_x509_crl_t
));
1427 if (*crl_list
== NULL
)
1430 return GNUTLS_E_MEMORY_ERROR
;
1433 ret
= gnutls_x509_crl_init (&crl_list
[0][i
- 1]);
1440 tmp
.data
= (char *) ptr
;
1444 gnutls_x509_crl_import (crl_list
[0][i
- 1],
1445 &tmp
, GNUTLS_X509_FMT_PEM
);
1452 /* now we move ptr after the pem header
1455 /* find the next certificate (if any)
1458 size
= input_crl_size
- (ptr
- input_crl
);
1461 ptr
= memmem (ptr
, size
, PEM_CRL_SEP
, sizeof (PEM_CRL_SEP
) - 1);
1468 while (ptr
!= NULL
);
1475 /* Reads a DER encoded certificate list from memory and stores it to
1476 * a gnutls_cert structure. This is only called if PKCS7 read fails.
1477 * returns the number of certificates parsed (1)
1480 parse_der_crl_mem (gnutls_x509_crl_t
** crl_list
, unsigned *ncrls
,
1481 const void *input_crl
, int input_crl_size
)
1490 (gnutls_x509_crl_t
*) gnutls_realloc_fast (*crl_list
,
1492 sizeof (gnutls_x509_crl_t
));
1494 if (*crl_list
== NULL
)
1497 return GNUTLS_E_MEMORY_ERROR
;
1500 tmp
.data
= (opaque
*) input_crl
;
1501 tmp
.size
= input_crl_size
;
1503 ret
= gnutls_x509_crl_init (&crl_list
[0][i
- 1]);
1511 gnutls_x509_crl_import (crl_list
[0][i
- 1], &tmp
, GNUTLS_X509_FMT_DER
);
1520 return 1; /* one certificate parsed */
1524 /* Reads a DER or PEM CRL from memory
1527 read_crl_mem (gnutls_certificate_credentials_t res
, const void *crl
,
1528 int crl_size
, gnutls_x509_crt_fmt_t type
)
1532 /* allocate space for the certificate to add
1534 res
->x509_crl_list
= gnutls_realloc_fast (res
->x509_crl_list
,
1537 sizeof (gnutls_x509_crl_t
));
1538 if (res
->x509_crl_list
== NULL
)
1541 return GNUTLS_E_MEMORY_ERROR
;
1544 if (type
== GNUTLS_X509_FMT_DER
)
1545 ret
= parse_der_crl_mem (&res
->x509_crl_list
,
1546 &res
->x509_ncrls
, crl
, crl_size
);
1548 ret
= parse_pem_crl_mem (&res
->x509_crl_list
,
1549 &res
->x509_ncrls
, crl
, crl_size
);
1561 * gnutls_certificate_set_x509_crl_mem - Used to add CRLs in a gnutls_certificate_credentials_t structure
1562 * @res: is an #gnutls_certificate_credentials_t structure.
1563 * @CRL: is a list of trusted CRLs. They should have been verified before.
1564 * @type: is DER or PEM
1566 * This function adds the trusted CRLs in order to verify client or server
1567 * certificates. In case of a client this is not required
1568 * to be called if the certificates are not verified using
1569 * gnutls_certificate_verify_peers2().
1570 * This function may be called multiple times.
1572 * Returns the number of CRLs processed or a negative value on error.
1576 gnutls_certificate_set_x509_crl_mem (gnutls_certificate_credentials_t
1577 res
, const gnutls_datum_t
* CRL
,
1578 gnutls_x509_crt_fmt_t type
)
1582 if ((ret
= read_crl_mem (res
, CRL
->data
, CRL
->size
, type
)) < 0)
1589 * gnutls_certificate_set_x509_crl - Used to add CRLs in a gnutls_certificate_credentials_t structure
1590 * @res: is an #gnutls_certificate_credentials_t structure.
1591 * @crl_list: is a list of trusted CRLs. They should have been verified before.
1592 * @crl_list_size: holds the size of the crl_list
1594 * This function adds the trusted CRLs in order to verify client or server
1595 * certificates. In case of a client this is not required
1596 * to be called if the certificates are not verified using
1597 * gnutls_certificate_verify_peers2().
1598 * This function may be called multiple times.
1600 * Returns 0 on success.
1604 gnutls_certificate_set_x509_crl (gnutls_certificate_credentials_t res
,
1605 gnutls_x509_crl_t
* crl_list
,
1610 res
->x509_crl_list
= gnutls_realloc_fast (res
->x509_crl_list
,
1613 sizeof (gnutls_x509_crl_t
));
1614 if (res
->x509_crl_list
== NULL
)
1617 return GNUTLS_E_MEMORY_ERROR
;
1620 for (i
= 0; i
< crl_list_size
; i
++)
1622 ret
= gnutls_x509_crl_init (&res
->x509_crl_list
[res
->x509_ncrls
]);
1629 ret
= _gnutls_x509_crl_cpy (res
->x509_crl_list
[res
->x509_ncrls
],
1643 * gnutls_certificate_set_x509_crl_file - Used to add CRLs in a gnutls_certificate_credentials_t structure
1644 * @res: is an #gnutls_certificate_credentials_t structure.
1645 * @crlfile: is a file containing the list of verified CRLs (DER or PEM list)
1646 * @type: is PEM or DER
1648 * This function adds the trusted CRLs in order to verify client or server
1649 * certificates. In case of a client this is not required
1650 * to be called if the certificates are not verified using
1651 * gnutls_certificate_verify_peers2().
1652 * This function may be called multiple times.
1654 * Returns the number of CRLs processed or a negative value on error.
1658 gnutls_certificate_set_x509_crl_file (gnutls_certificate_credentials_t
1659 res
, const char *crlfile
,
1660 gnutls_x509_crt_fmt_t type
)
1664 char *data
= read_binary_file (crlfile
, &size
);
1669 return GNUTLS_E_FILE_ERROR
;
1672 if (type
== GNUTLS_X509_FMT_DER
)
1673 ret
= parse_der_crl_mem (&res
->x509_crl_list
, &res
->x509_ncrls
,
1676 ret
= parse_pem_crl_mem (&res
->x509_crl_list
, &res
->x509_ncrls
,
1690 #include <gnutls/pkcs12.h>
1693 parse_pkcs12 (gnutls_certificate_credentials_t res
,
1694 gnutls_pkcs12_t p12
,
1695 const char *password
,
1696 gnutls_x509_privkey
* key
,
1697 gnutls_x509_crt_t
* cert
, gnutls_x509_crl_t
* crl
)
1699 gnutls_pkcs12_bag bag
= NULL
;
1705 int elements_in_bag
;
1708 ret
= gnutls_pkcs12_bag_init (&bag
);
1716 ret
= gnutls_pkcs12_get_bag (p12
, index
, bag
);
1717 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
1725 ret
= gnutls_pkcs12_bag_get_type (bag
, 0);
1732 if (ret
== GNUTLS_BAG_ENCRYPTED
)
1734 ret
= gnutls_pkcs12_bag_decrypt (bag
, password
);
1742 elements_in_bag
= gnutls_pkcs12_bag_get_count (bag
);
1743 if (elements_in_bag
< 0)
1749 for (i
= 0; i
< elements_in_bag
; i
++)
1754 type
= gnutls_pkcs12_bag_get_type (bag
, i
);
1761 ret
= gnutls_pkcs12_bag_get_data (bag
, i
, &data
);
1770 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY
:
1771 case GNUTLS_BAG_PKCS8_KEY
:
1772 ret
= gnutls_x509_privkey_init (key
);
1779 ret
= gnutls_x509_privkey_import_pkcs8
1780 (*key
, &data
, GNUTLS_X509_FMT_DER
, password
,
1781 type
== GNUTLS_BAG_PKCS8_KEY
? GNUTLS_PKCS_PLAIN
: 0);
1789 case GNUTLS_BAG_CERTIFICATE
:
1790 ret
= gnutls_x509_crt_init (cert
);
1798 gnutls_x509_crt_import (*cert
, &data
, GNUTLS_X509_FMT_DER
);
1806 case GNUTLS_BAG_CRL
:
1807 ret
= gnutls_x509_crl_init (crl
);
1814 ret
= gnutls_x509_crl_import (*crl
, &data
, GNUTLS_X509_FMT_DER
);
1822 case GNUTLS_BAG_ENCRYPTED
:
1823 /* XXX Bother to recurse one level down? Unlikely to
1824 use the same password anyway. */
1825 case GNUTLS_BAG_EMPTY
:
1832 gnutls_pkcs12_bag_deinit (bag
);
1839 gnutls_pkcs12_bag_deinit (bag
);
1845 * gnutls_certificate_set_x509_simple_pkcs12_file:
1846 * @res: is an #gnutls_certificate_credentials_t structure.
1847 * @pkcs12file: filename of file containing PKCS#12 blob.
1848 * @type: is PEM or DER of the @pkcs12file.
1849 * @password: optional password used to decrypt PKCS#12 file, bags and keys.
1851 * This function sets a certificate/private key pair and/or a CRL in
1852 * the gnutls_certificate_credentials_t structure. This function may
1853 * be called more than once (in case multiple keys/certificates exist
1856 * MAC:ed PKCS#12 files are supported. Encrypted PKCS#12 bags are
1857 * supported. Encrypted PKCS#8 private keys are supported. However,
1858 * only password based security, and the same password for all
1859 * operations, are supported.
1861 * The private keys may be RSA PKCS#1 or DSA private keys encoded in
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 * Return value: Returns 0 on success, or an error code.
1877 gnutls_certificate_set_x509_simple_pkcs12_file
1878 (gnutls_certificate_credentials_t res
, const char *pkcs12file
,
1879 gnutls_x509_crt_fmt_t type
, const char *password
)
1881 gnutls_pkcs12_t p12
;
1882 gnutls_datum_t p12blob
;
1883 gnutls_x509_privkey key
= NULL
;
1884 gnutls_x509_crt_t cert
= NULL
;
1885 gnutls_x509_crl_t crl
= NULL
;
1888 ret
= gnutls_pkcs12_init (&p12
);
1895 p12blob
.data
= read_binary_file (pkcs12file
, &p12blob
.size
);
1896 if (p12blob
.data
== NULL
)
1899 gnutls_pkcs12_deinit (p12
);
1900 return GNUTLS_E_FILE_ERROR
;
1903 ret
= gnutls_pkcs12_import (p12
, &p12blob
, type
, 0);
1904 free (p12blob
.data
);
1908 gnutls_pkcs12_deinit (p12
);
1914 ret
= gnutls_pkcs12_verify_mac (p12
, password
);
1918 gnutls_pkcs12_deinit (p12
);
1923 ret
= parse_pkcs12 (res
, p12
, password
, &key
, &cert
, &crl
);
1924 gnutls_pkcs12_deinit (p12
);
1933 ret
= gnutls_certificate_set_x509_key (res
, &cert
, 1, key
);
1943 ret
= gnutls_certificate_set_x509_crl (res
, &crl
, 1);
1955 gnutls_x509_crt_deinit (cert
);
1957 gnutls_x509_privkey_deinit (key
);
1959 gnutls_x509_crl_deinit (crl
);
1966 * gnutls_certificate_free_crls - Used to free all the CRLs from a gnutls_certificate_credentials_t structure
1967 * @sc: is an #gnutls_certificate_credentials_t structure.
1969 * This function will delete all the CRLs associated
1970 * with the given credentials.
1974 gnutls_certificate_free_crls (gnutls_certificate_credentials_t sc
)
1978 for (j
= 0; j
< sc
->x509_ncrls
; j
++)
1980 gnutls_x509_crl_deinit (sc
->x509_crl_list
[j
]);
1985 gnutls_free (sc
->x509_crl_list
);
1986 sc
->x509_crl_list
= NULL
;