2 * Copyright (C) 2003-2012 Free Software Foundation, Inc.
3 * Author: Nikos Mavrogiannopoulos, Simon Josefsson, Howard Chu
5 * This file is part of GnuTLS.
7 * The GnuTLS is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 3 of
10 * the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 /* Functions on X.509 Certificate parsing
25 #include <gnutls_int.h>
26 #include <gnutls_datum.h>
27 #include <gnutls_global.h>
28 #include <gnutls_errors.h>
30 #include <gnutls_x509.h>
34 #include <gnutls_pk.h>
37 * gnutls_x509_crt_init:
38 * @cert: The structure to be initialized
40 * This function will initialize an X.509 certificate structure.
42 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
43 * negative error value.
46 gnutls_x509_crt_init (gnutls_x509_crt_t
* cert
)
48 gnutls_x509_crt_t tmp
= gnutls_calloc (1, sizeof (gnutls_x509_crt_int
));
52 return GNUTLS_E_MEMORY_ERROR
;
54 result
= asn1_create_element (_gnutls_get_pkix (),
55 "PKIX1.Certificate", &tmp
->cert
);
56 if (result
!= ASN1_SUCCESS
)
60 return _gnutls_asn2err (result
);
63 /* If you add anything here, be sure to check if it has to be added
64 to gnutls_x509_crt_import as well. */
68 return 0; /* success */
72 * _gnutls_x509_crt_cpy - This function copies a gnutls_x509_crt_t structure
73 * @dest: The structure where to copy
74 * @src: The structure to be copied
76 * This function will copy an X.509 certificate structure.
78 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
79 * negative error value.
82 _gnutls_x509_crt_cpy (gnutls_x509_crt_t dest
, gnutls_x509_crt_t src
)
89 ret
= gnutls_x509_crt_export (src
, GNUTLS_X509_FMT_DER
, NULL
, &der_size
);
90 if (ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
96 der
= gnutls_malloc (der_size
);
100 return GNUTLS_E_MEMORY_ERROR
;
103 ret
= gnutls_x509_crt_export (src
, GNUTLS_X509_FMT_DER
, der
, &der_size
);
113 ret
= gnutls_x509_crt_import (dest
, &tmp
, GNUTLS_X509_FMT_DER
);
127 * gnutls_x509_crt_deinit:
128 * @cert: The structure to be deinitialized
130 * This function will deinitialize a certificate structure.
133 gnutls_x509_crt_deinit (gnutls_x509_crt_t cert
)
139 asn1_delete_structure (&cert
->cert
);
145 * gnutls_x509_crt_import:
146 * @cert: The structure to store the parsed certificate.
147 * @data: The DER or PEM encoded certificate.
148 * @format: One of DER or PEM
150 * This function will convert the given DER or PEM encoded Certificate
151 * to the native gnutls_x509_crt_t format. The output will be stored
154 * If the Certificate is PEM encoded it should have a header of "X509
155 * CERTIFICATE", or "CERTIFICATE".
157 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
158 * negative error value.
161 gnutls_x509_crt_import (gnutls_x509_crt_t cert
,
162 const gnutls_datum_t
* data
,
163 gnutls_x509_crt_fmt_t format
)
165 int result
= 0, need_free
= 0;
166 gnutls_datum_t _data
;
171 return GNUTLS_E_INVALID_REQUEST
;
174 _data
.data
= data
->data
;
175 _data
.size
= data
->size
;
177 /* If the Certificate is in PEM format then decode it
179 if (format
== GNUTLS_X509_FMT_PEM
)
181 /* Try the first header */
183 _gnutls_fbase64_decode (PEM_X509_CERT2
, data
->data
, data
->size
, &_data
);
187 /* try for the second header */
189 _gnutls_fbase64_decode (PEM_X509_CERT
, data
->data
,
204 /* Any earlier asn1_der_decoding will modify the ASN.1
205 structure, so we need to replace it with a fresh
207 asn1_delete_structure (&cert
->cert
);
209 result
= asn1_create_element (_gnutls_get_pkix (),
210 "PKIX1.Certificate", &cert
->cert
);
211 if (result
!= ASN1_SUCCESS
)
213 result
= _gnutls_asn2err (result
);
219 result
= asn1_der_decoding (&cert
->cert
, _data
.data
, _data
.size
, NULL
);
220 if (result
!= ASN1_SUCCESS
)
222 result
= _gnutls_asn2err (result
);
229 /* Since we do not want to disable any extension
231 cert
->use_extensions
= 1;
233 _gnutls_free_datum (&_data
);
239 _gnutls_free_datum (&_data
);
245 * gnutls_x509_crt_get_issuer_dn:
246 * @cert: should contain a #gnutls_x509_crt_t structure
247 * @buf: a pointer to a structure to hold the name (may be null)
248 * @buf_size: initially holds the size of @buf
250 * This function will copy the name of the Certificate issuer in the
251 * provided buffer. The name will be in the form
252 * "C=xxxx,O=yyyy,CN=zzzz" as described in RFC4514. The output string
253 * will be ASCII or UTF-8 encoded, depending on the certificate data.
255 * If @buf is null then only the size will be filled.
257 * Returns: GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
258 * long enough, and in that case the @buf_size will be updated with
259 * the required size. On success 0 is returned.
262 gnutls_x509_crt_get_issuer_dn (gnutls_x509_crt_t cert
, char *buf
,
268 return GNUTLS_E_INVALID_REQUEST
;
271 return _gnutls_x509_parse_dn (cert
->cert
,
272 "tbsCertificate.issuer.rdnSequence", buf
,
277 * gnutls_x509_crt_get_issuer_dn_by_oid:
278 * @cert: should contain a #gnutls_x509_crt_t structure
279 * @oid: holds an Object Identified in null terminated string
280 * @indx: In case multiple same OIDs exist in the RDN, this specifies which to send. Use (0) to get the first one.
281 * @raw_flag: If non-zero returns the raw DER data of the DN part.
282 * @buf: a pointer to a structure to hold the name (may be null)
283 * @buf_size: initially holds the size of @buf
285 * This function will extract the part of the name of the Certificate
286 * issuer specified by the given OID. The output, if the raw flag is not
287 * used, will be encoded as described in RFC4514. Thus a string that is
288 * ASCII or UTF-8 encoded, depending on the certificate data.
290 * Some helper macros with popular OIDs can be found in gnutls/x509.h
291 * If raw flag is (0), this function will only return known OIDs as
292 * text. Other OIDs will be DER encoded, as described in RFC4514 --
293 * in hex format with a '#' prefix. You can check about known OIDs
294 * using gnutls_x509_dn_oid_known().
296 * If @buf is null then only the size will be filled. If the @raw_flag
297 * is not specified the output is always null terminated, although the
298 * @buf_size will not include the null character.
300 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
301 * long enough, and in that case the @buf_size will be updated with
302 * the required size. %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE if there
303 * are no data in the current index. On success 0 is returned.
306 gnutls_x509_crt_get_issuer_dn_by_oid (gnutls_x509_crt_t cert
,
307 const char *oid
, int indx
,
308 unsigned int raw_flag
, void *buf
,
317 return GNUTLS_E_INVALID_REQUEST
;
320 ret
= _gnutls_x509_parse_dn_oid (cert
->cert
,
321 "tbsCertificate.issuer.rdnSequence",
322 oid
, indx
, raw_flag
, &td
);
324 return gnutls_assert_val(ret
);
326 return _gnutls_strdatum_to_buf (&td
, buf
, buf_size
);
330 * gnutls_x509_crt_get_issuer_dn_oid:
331 * @cert: should contain a #gnutls_x509_crt_t structure
332 * @indx: This specifies which OID to return. Use (0) to get the first one.
333 * @oid: a pointer to a buffer to hold the OID (may be null)
334 * @oid_size: initially holds the size of @oid
336 * This function will extract the OIDs of the name of the Certificate
337 * issuer specified by the given index.
339 * If @oid is null then only the size will be filled. The @oid
340 * returned will be null terminated, although @oid_size will not
341 * account for the trailing null.
343 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
344 * long enough, and in that case the @buf_size will be updated with
345 * the required size. %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE if there
346 * are no data in the current index. On success 0 is returned.
349 gnutls_x509_crt_get_issuer_dn_oid (gnutls_x509_crt_t cert
,
350 int indx
, void *oid
, size_t * oid_size
)
355 return GNUTLS_E_INVALID_REQUEST
;
358 return _gnutls_x509_get_dn_oid (cert
->cert
,
359 "tbsCertificate.issuer.rdnSequence",
360 indx
, oid
, oid_size
);
364 * gnutls_x509_crt_get_dn:
365 * @cert: should contain a #gnutls_x509_crt_t structure
366 * @buf: a pointer to a structure to hold the name (may be null)
367 * @buf_size: initially holds the size of @buf
369 * This function will copy the name of the Certificate in the provided
370 * buffer. The name will be in the form "C=xxxx,O=yyyy,CN=zzzz" as
371 * described in RFC4514. The output string will be ASCII or UTF-8
372 * encoded, depending on the certificate data.
374 * If @buf is null then only the size will be filled.
376 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
377 * long enough, and in that case the @buf_size will be updated
378 * with the required size. On success 0 is returned.
381 gnutls_x509_crt_get_dn (gnutls_x509_crt_t cert
, char *buf
,
387 return GNUTLS_E_INVALID_REQUEST
;
390 return _gnutls_x509_parse_dn (cert
->cert
,
391 "tbsCertificate.subject.rdnSequence", buf
,
396 * gnutls_x509_crt_get_dn_by_oid:
397 * @cert: should contain a #gnutls_x509_crt_t structure
398 * @oid: holds an Object Identified in null terminated string
399 * @indx: In case multiple same OIDs exist in the RDN, this specifies which to send. Use (0) to get the first one.
400 * @raw_flag: If non-zero returns the raw DER data of the DN part.
401 * @buf: a pointer where the DN part will be copied (may be null).
402 * @buf_size: initially holds the size of @buf
404 * This function will extract the part of the name of the Certificate
405 * subject specified by the given OID. The output, if the raw flag is
406 * not used, will be encoded as described in RFC4514. Thus a string
407 * that is ASCII or UTF-8 encoded, depending on the certificate data.
409 * Some helper macros with popular OIDs can be found in gnutls/x509.h
410 * If raw flag is (0), this function will only return known OIDs as
411 * text. Other OIDs will be DER encoded, as described in RFC4514 --
412 * in hex format with a '#' prefix. You can check about known OIDs
413 * using gnutls_x509_dn_oid_known().
415 * If @buf is null then only the size will be filled. If the @raw_flag
416 * is not specified the output is always null terminated, although the
417 * @buf_size will not include the null character.
419 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
420 * long enough, and in that case the @buf_size will be updated with
421 * the required size. %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE if there
422 * are no data in the current index. On success 0 is returned.
425 gnutls_x509_crt_get_dn_by_oid (gnutls_x509_crt_t cert
, const char *oid
,
426 int indx
, unsigned int raw_flag
,
427 void *buf
, size_t * buf_size
)
435 return GNUTLS_E_INVALID_REQUEST
;
438 ret
= _gnutls_x509_parse_dn_oid (cert
->cert
,
439 "tbsCertificate.subject.rdnSequence",
440 oid
, indx
, raw_flag
, &td
);
442 return gnutls_assert_val(ret
);
444 return _gnutls_strdatum_to_buf (&td
, buf
, buf_size
);
448 * gnutls_x509_crt_get_dn_oid:
449 * @cert: should contain a #gnutls_x509_crt_t structure
450 * @indx: This specifies which OID to return. Use (0) to get the first one.
451 * @oid: a pointer to a buffer to hold the OID (may be null)
452 * @oid_size: initially holds the size of @oid
454 * This function will extract the OIDs of the name of the Certificate
455 * subject specified by the given index.
457 * If @oid is null then only the size will be filled. The @oid
458 * returned will be null terminated, although @oid_size will not
459 * account for the trailing null.
461 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not
462 * long enough, and in that case the @buf_size will be updated with
463 * the required size. %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE if there
464 * are no data in the current index. On success 0 is returned.
467 gnutls_x509_crt_get_dn_oid (gnutls_x509_crt_t cert
,
468 int indx
, void *oid
, size_t * oid_size
)
473 return GNUTLS_E_INVALID_REQUEST
;
476 return _gnutls_x509_get_dn_oid (cert
->cert
,
477 "tbsCertificate.subject.rdnSequence",
478 indx
, oid
, oid_size
);
482 * gnutls_x509_crt_get_signature_algorithm:
483 * @cert: should contain a #gnutls_x509_crt_t structure
485 * This function will return a value of the #gnutls_sign_algorithm_t
486 * enumeration that is the signature algorithm that has been used to
487 * sign this certificate.
489 * Returns: a #gnutls_sign_algorithm_t value, or a negative error code on
493 gnutls_x509_crt_get_signature_algorithm (gnutls_x509_crt_t cert
)
495 return _gnutls_x509_get_signature_algorithm(cert
->cert
, "signatureAlgorithm.algorithm");
499 * gnutls_x509_crt_get_signature:
500 * @cert: should contain a #gnutls_x509_crt_t structure
501 * @sig: a pointer where the signature part will be copied (may be null).
502 * @sizeof_sig: initially holds the size of @sig
504 * This function will extract the signature field of a certificate.
506 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
507 * negative error value. and a negative error code on error.
510 gnutls_x509_crt_get_signature (gnutls_x509_crt_t cert
,
511 char *sig
, size_t * sizeof_sig
)
520 return GNUTLS_E_INVALID_REQUEST
;
524 result
= asn1_read_value (cert
->cert
, "signature", NULL
, &len
);
525 if (result
!= ASN1_MEM_ERROR
)
528 return _gnutls_asn2err (result
);
535 return GNUTLS_E_CERTIFICATE_ERROR
;
540 if (*sizeof_sig
< (unsigned int) len
)
543 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
546 result
= asn1_read_value (cert
->cert
, "signature", sig
, &len
);
547 if (result
!= ASN1_SUCCESS
)
550 return _gnutls_asn2err (result
);
557 * gnutls_x509_crt_get_version:
558 * @cert: should contain a #gnutls_x509_crt_t structure
560 * This function will return the version of the specified Certificate.
562 * Returns: version of certificate, or a negative error code on error.
565 gnutls_x509_crt_get_version (gnutls_x509_crt_t cert
)
573 return GNUTLS_E_INVALID_REQUEST
;
576 len
= sizeof (version
);
578 asn1_read_value (cert
->cert
, "tbsCertificate.version", version
,
579 &len
)) != ASN1_SUCCESS
)
582 if (result
== ASN1_ELEMENT_NOT_FOUND
)
583 return 1; /* the DEFAULT version */
585 return _gnutls_asn2err (result
);
588 return (int) version
[0] + 1;
592 * gnutls_x509_crt_get_activation_time:
593 * @cert: should contain a #gnutls_x509_crt_t structure
595 * This function will return the time this Certificate was or will be
598 * Returns: activation time, or (time_t)-1 on error.
601 gnutls_x509_crt_get_activation_time (gnutls_x509_crt_t cert
)
609 return _gnutls_x509_get_time (cert
->cert
,
610 "tbsCertificate.validity.notBefore", 0);
614 * gnutls_x509_crt_get_expiration_time:
615 * @cert: should contain a #gnutls_x509_crt_t structure
617 * This function will return the time this Certificate was or will be
620 * Returns: expiration time, or (time_t)-1 on error.
623 gnutls_x509_crt_get_expiration_time (gnutls_x509_crt_t cert
)
631 return _gnutls_x509_get_time (cert
->cert
,
632 "tbsCertificate.validity.notAfter", 0);
636 * gnutls_x509_crt_get_private_key_usage_period:
637 * @cert: should contain a #gnutls_x509_crt_t structure
638 * @activation: The activation time
639 * @expiration: The expiration time
640 * @critical: the extension status
642 * This function will return the expiration and activation
643 * times of the private key of the certificate. It relies on
644 * the PKIX extension 2.5.29.16 being present.
646 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
647 * if the extension is not present, otherwise a negative error value.
650 gnutls_x509_crt_get_private_key_usage_period (gnutls_x509_crt_t cert
, time_t* activation
, time_t* expiration
,
651 unsigned int *critical
)
654 gnutls_datum_t der
= {NULL
, 0};
655 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
660 return GNUTLS_E_INVALID_REQUEST
;
664 _gnutls_x509_crt_get_extension (cert
, "2.5.29.16", 0, &der
,
667 return gnutls_assert_val(ret
);
669 if (der
.size
== 0 || der
.data
== NULL
)
670 return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
);
672 result
= asn1_create_element
673 (_gnutls_get_pkix (), "PKIX1.PrivateKeyUsagePeriod", &c2
);
674 if (result
!= ASN1_SUCCESS
)
677 ret
= _gnutls_asn2err (result
);
681 result
= asn1_der_decoding (&c2
, der
.data
, der
.size
, NULL
);
682 if (result
!= ASN1_SUCCESS
)
685 ret
= _gnutls_asn2err (result
);
690 *activation
= _gnutls_x509_get_time (c2
,
694 *expiration
= _gnutls_x509_get_time (c2
,
700 _gnutls_free_datum(&der
);
701 asn1_delete_structure (&c2
);
708 * gnutls_x509_crt_get_serial:
709 * @cert: should contain a #gnutls_x509_crt_t structure
710 * @result: The place where the serial number will be copied
711 * @result_size: Holds the size of the result field.
713 * This function will return the X.509 certificate's serial number.
714 * This is obtained by the X509 Certificate serialNumber field. Serial
715 * is not always a 32 or 64bit number. Some CAs use large serial
716 * numbers, thus it may be wise to handle it as something uint8_t.
718 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
719 * negative error value.
722 gnutls_x509_crt_get_serial (gnutls_x509_crt_t cert
, void *result
,
723 size_t * result_size
)
730 return GNUTLS_E_INVALID_REQUEST
;
735 asn1_read_value (cert
->cert
, "tbsCertificate.serialNumber", result
, &len
);
738 if (ret
!= ASN1_SUCCESS
)
741 return _gnutls_asn2err (ret
);
748 * gnutls_x509_crt_get_subject_key_id:
749 * @cert: should contain a #gnutls_x509_crt_t structure
750 * @ret: The place where the identifier will be copied
751 * @ret_size: Holds the size of the result field.
752 * @critical: will be non-zero if the extension is marked as critical (may be null)
754 * This function will return the X.509v3 certificate's subject key
755 * identifier. This is obtained by the X.509 Subject Key identifier
756 * extension field (2.5.29.14).
758 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
759 * if the extension is not present, otherwise a negative error value.
762 gnutls_x509_crt_get_subject_key_id (gnutls_x509_crt_t cert
, void *ret
,
763 size_t * ret_size
, unsigned int *critical
)
767 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
772 return GNUTLS_E_INVALID_REQUEST
;
777 memset (ret
, 0, *ret_size
);
782 _gnutls_x509_crt_get_extension (cert
, "2.5.29.14", 0, &id
,
788 if (id
.size
== 0 || id
.data
== NULL
)
791 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
794 result
= asn1_create_element
795 (_gnutls_get_pkix (), "PKIX1.SubjectKeyIdentifier", &c2
);
796 if (result
!= ASN1_SUCCESS
)
799 _gnutls_free_datum (&id
);
800 return _gnutls_asn2err (result
);
803 result
= asn1_der_decoding (&c2
, id
.data
, id
.size
, NULL
);
804 _gnutls_free_datum (&id
);
806 if (result
!= ASN1_SUCCESS
)
809 asn1_delete_structure (&c2
);
810 return _gnutls_asn2err (result
);
814 result
= asn1_read_value (c2
, "", ret
, &len
);
817 asn1_delete_structure (&c2
);
819 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
821 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
824 if (result
!= ASN1_SUCCESS
)
826 if (result
!= ASN1_MEM_ERROR
)
828 return _gnutls_asn2err (result
);
835 _get_authority_key_id (gnutls_x509_crt_t cert
, ASN1_TYPE
*c2
,
836 unsigned int *critical
)
841 *c2
= ASN1_TYPE_EMPTY
;
846 return GNUTLS_E_INVALID_REQUEST
;
850 _gnutls_x509_crt_get_extension (cert
, "2.5.29.35", 0, &id
,
853 return gnutls_assert_val(ret
);
856 if (id
.size
== 0 || id
.data
== NULL
)
859 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
862 ret
= asn1_create_element
863 (_gnutls_get_pkix (), "PKIX1.AuthorityKeyIdentifier", c2
);
864 if (ret
!= ASN1_SUCCESS
)
867 _gnutls_free_datum (&id
);
868 return _gnutls_asn2err (ret
);
871 ret
= asn1_der_decoding (c2
, id
.data
, id
.size
, NULL
);
872 _gnutls_free_datum (&id
);
874 if (ret
!= ASN1_SUCCESS
)
877 asn1_delete_structure (c2
);
878 return _gnutls_asn2err (ret
);
885 * gnutls_x509_crt_get_authority_key_gn_serial:
886 * @cert: should contain a #gnutls_x509_crt_t structure
887 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
888 * @alt: is the place where the alternative name will be copied to
889 * @alt_size: holds the size of alt.
890 * @alt_type: holds the type of the alternative name (one of gnutls_x509_subject_alt_name_t).
891 * @serial: buffer to store the serial number (may be null)
892 * @serial_size: Holds the size of the serial field (may be null)
893 * @critical: will be non-zero if the extension is marked as critical (may be null)
895 * This function will return the X.509 authority key
896 * identifier when stored as a general name (authorityCertIssuer)
899 * Because more than one general names might be stored
900 * @seq can be used as a counter to request them all until
901 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
903 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
904 * if the extension is not present, otherwise a negative error value.
909 gnutls_x509_crt_get_authority_key_gn_serial (gnutls_x509_crt_t cert
, unsigned int seq
, void *alt
,
910 size_t * alt_size
, unsigned int *alt_type
,
911 void* serial
, size_t *serial_size
,
912 unsigned int *critical
)
914 int ret
, result
, len
;
917 ret
= _get_authority_key_id(cert
, &c2
, critical
);
919 return gnutls_assert_val(ret
);
922 _gnutls_parse_general_name (c2
, "authorityCertIssuer", seq
, alt
, alt_size
, alt_type
,
926 ret
= gnutls_assert_val(ret
);
933 result
= asn1_read_value (c2
, "authorityCertSerialNumber", serial
, &len
);
939 ret
= _gnutls_asn2err(result
);
948 asn1_delete_structure (&c2
);
954 * gnutls_x509_crt_get_authority_key_id:
955 * @cert: should contain a #gnutls_x509_crt_t structure
956 * @id: The place where the identifier will be copied
957 * @id_size: Holds the size of the id field.
958 * @critical: will be non-zero if the extension is marked as critical (may be null)
960 * This function will return the X.509v3 certificate authority's key
961 * identifier. This is obtained by the X.509 Authority Key
962 * identifier extension field (2.5.29.35). Note that this function
963 * only returns the keyIdentifier field of the extension and
964 * %GNUTLS_E_X509_UNSUPPORTED_EXTENSION, if the extension contains
965 * the name and serial number of the certificate. In that case
966 * gnutls_x509_crt_get_authority_key_gn_serial() may be used.
968 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
969 * if the extension is not present, otherwise a negative error value.
972 gnutls_x509_crt_get_authority_key_id (gnutls_x509_crt_t cert
, void *id
,
974 unsigned int *critical
)
976 int ret
, result
, len
;
979 ret
= _get_authority_key_id(cert
, &c2
, critical
);
981 return gnutls_assert_val(ret
);
984 result
= asn1_read_value (c2
, "keyIdentifier", id
, &len
);
987 asn1_delete_structure (&c2
);
989 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
990 return gnutls_assert_val(GNUTLS_E_X509_UNSUPPORTED_EXTENSION
);
992 if (result
!= ASN1_SUCCESS
)
994 if (result
!= ASN1_MEM_ERROR
)
996 return _gnutls_asn2err (result
);
1003 * gnutls_x509_crt_get_pk_algorithm:
1004 * @cert: should contain a #gnutls_x509_crt_t structure
1005 * @bits: if bits is non null it will hold the size of the parameters' in bits
1007 * This function will return the public key algorithm of an X.509
1010 * If bits is non null, it should have enough size to hold the parameters
1011 * size in bits. For RSA the bits returned is the modulus.
1012 * For DSA the bits returned are of the public
1015 * Returns: a member of the #gnutls_pk_algorithm_t enumeration on
1016 * success, or a negative error code on error.
1019 gnutls_x509_crt_get_pk_algorithm (gnutls_x509_crt_t cert
, unsigned int *bits
)
1026 return GNUTLS_E_INVALID_REQUEST
;
1033 _gnutls_x509_get_pk_algorithm (cert
->cert
,
1034 "tbsCertificate.subjectPublicKeyInfo",
1048 is_type_printable (int type
)
1050 if (type
== GNUTLS_SAN_DNSNAME
|| type
== GNUTLS_SAN_RFC822NAME
||
1051 type
== GNUTLS_SAN_URI
)
1057 #define XMPP_OID "1.3.6.1.5.5.7.8.5"
1059 /* returns the type and the name on success.
1060 * Type is also returned as a parameter in case of an error.
1063 _gnutls_parse_general_name (ASN1_TYPE src
, const char *src_name
,
1064 int seq
, void *name
, size_t * name_size
,
1065 unsigned int *ret_type
, int othername_oid
)
1068 char nptr
[ASN1_MAX_NAME_SIZE
];
1070 char choice_type
[128];
1071 gnutls_x509_subject_alt_name_t type
;
1073 seq
++; /* 0->1, 1->2 etc */
1075 if (src_name
[0] != 0)
1076 snprintf (nptr
, sizeof (nptr
), "%s.?%u", src_name
, seq
);
1078 snprintf (nptr
, sizeof (nptr
), "?%u", seq
);
1080 len
= sizeof (choice_type
);
1081 result
= asn1_read_value (src
, nptr
, choice_type
, &len
);
1083 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
1085 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1088 if (result
!= ASN1_SUCCESS
)
1091 return _gnutls_asn2err (result
);
1095 type
= _gnutls_x509_san_find_type (choice_type
);
1096 if (type
== (gnutls_x509_subject_alt_name_t
) - 1)
1099 return GNUTLS_E_X509_UNKNOWN_SAN
;
1105 if (type
== GNUTLS_SAN_OTHERNAME
)
1108 _gnutls_str_cat (nptr
, sizeof (nptr
), ".otherName.type-id");
1110 _gnutls_str_cat (nptr
, sizeof (nptr
), ".otherName.value");
1113 result
= asn1_read_value (src
, nptr
, name
, &len
);
1116 if (result
== ASN1_MEM_ERROR
)
1117 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1119 if (result
!= ASN1_SUCCESS
)
1122 return _gnutls_asn2err (result
);
1127 if ((unsigned)len
> strlen (XMPP_OID
) && strcmp (name
, XMPP_OID
) == 0)
1128 type
= GNUTLS_SAN_OTHERNAME_XMPP
;
1134 if (src_name
[0] != 0)
1135 snprintf (nptr
, sizeof (nptr
), "%s.?%u.otherName.type-id",
1138 snprintf (nptr
, sizeof (nptr
), "?%u.otherName.type-id", seq
);
1141 result
= asn1_read_value (src
, nptr
, oid
, &len
);
1142 if (result
!= ASN1_SUCCESS
)
1145 return _gnutls_asn2err (result
);
1148 if ((unsigned)len
> strlen (XMPP_OID
) && strcmp (oid
, XMPP_OID
) == 0)
1150 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
1151 size_t orig_name_size
= *name_size
;
1153 result
= asn1_create_element
1154 (_gnutls_get_pkix (), "PKIX1.UTF8String", &c2
);
1155 if (result
!= ASN1_SUCCESS
)
1158 return _gnutls_asn2err (result
);
1161 result
= asn1_der_decoding (&c2
, name
, *name_size
, NULL
);
1162 if (result
!= ASN1_SUCCESS
)
1165 asn1_delete_structure (&c2
);
1166 return _gnutls_asn2err (result
);
1170 result
= asn1_read_value (c2
, "", name
, &len
);
1171 if (result
!= ASN1_SUCCESS
)
1174 asn1_delete_structure (&c2
);
1175 *name_size
= len
+ 1;
1176 return _gnutls_asn2err (result
);
1178 asn1_delete_structure (&c2
);
1180 if ((unsigned)len
+ 1 > orig_name_size
)
1183 *name_size
= len
+ 1;
1184 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1188 /* null terminate it */
1189 ((char *) name
)[*name_size
] = 0;
1193 else if (type
== GNUTLS_SAN_DN
)
1195 _gnutls_str_cat (nptr
, sizeof (nptr
), ".directoryName");
1196 result
= _gnutls_x509_parse_dn (src
, nptr
, name
, name_size
);
1203 else if (othername_oid
)
1204 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1207 size_t orig_name_size
= *name_size
;
1209 _gnutls_str_cat (nptr
, sizeof (nptr
), ".");
1210 _gnutls_str_cat (nptr
, sizeof (nptr
), choice_type
);
1213 result
= asn1_read_value (src
, nptr
, name
, &len
);
1216 if (result
== ASN1_MEM_ERROR
)
1218 if (is_type_printable (type
))
1220 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1223 if (result
!= ASN1_SUCCESS
)
1226 return _gnutls_asn2err (result
);
1229 if (is_type_printable (type
))
1232 if ((unsigned)len
+ 1 > orig_name_size
)
1236 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
1239 /* null terminate it */
1240 ((char *) name
)[*name_size
] = 0;
1249 get_alt_name (gnutls_x509_crt_t cert
, const char *extension_id
,
1250 unsigned int seq
, void *alt
,
1251 size_t * alt_size
, unsigned int *alt_type
,
1252 unsigned int *critical
, int othername_oid
)
1255 gnutls_datum_t dnsname
;
1256 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
1261 return GNUTLS_E_INVALID_REQUEST
;
1265 memset (alt
, 0, *alt_size
);
1270 _gnutls_x509_crt_get_extension (cert
, extension_id
, 0, &dnsname
,
1276 if (dnsname
.size
== 0 || dnsname
.data
== NULL
)
1279 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1282 if (strcmp ("2.5.29.17", extension_id
) == 0)
1283 result
= asn1_create_element (_gnutls_get_pkix (),
1284 "PKIX1.SubjectAltName", &c2
);
1285 else if (strcmp ("2.5.29.18", extension_id
) == 0)
1286 result
= asn1_create_element (_gnutls_get_pkix (),
1287 "PKIX1.IssuerAltName", &c2
);
1291 return GNUTLS_E_INTERNAL_ERROR
;
1294 if (result
!= ASN1_SUCCESS
)
1297 _gnutls_free_datum (&dnsname
);
1298 return _gnutls_asn2err (result
);
1301 result
= asn1_der_decoding (&c2
, dnsname
.data
, dnsname
.size
, NULL
);
1302 _gnutls_free_datum (&dnsname
);
1304 if (result
!= ASN1_SUCCESS
)
1307 asn1_delete_structure (&c2
);
1308 return _gnutls_asn2err (result
);
1312 _gnutls_parse_general_name (c2
, "", seq
, alt
, alt_size
, alt_type
,
1315 asn1_delete_structure (&c2
);
1327 * gnutls_x509_crt_get_subject_alt_name:
1328 * @cert: should contain a #gnutls_x509_crt_t structure
1329 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1330 * @san: is the place where the alternative name will be copied to
1331 * @san_size: holds the size of san.
1332 * @critical: will be non-zero if the extension is marked as critical (may be null)
1334 * This function retrieves the Alternative Name (2.5.29.17), contained
1335 * in the given certificate in the X509v3 Certificate Extensions.
1337 * When the SAN type is otherName, it will extract the data in the
1338 * otherName's value field, and %GNUTLS_SAN_OTHERNAME is returned.
1339 * You may use gnutls_x509_crt_get_subject_alt_othername_oid() to get
1340 * the corresponding OID and the "virtual" SAN types (e.g.,
1341 * %GNUTLS_SAN_OTHERNAME_XMPP).
1343 * If an otherName OID is known, the data will be decoded. Otherwise
1344 * the returned data will be DER encoded, and you will have to decode
1345 * it yourself. Currently, only the RFC 3920 id-on-xmppAddr SAN is
1348 * Returns: the alternative subject name type on success, one of the
1349 * enumerated #gnutls_x509_subject_alt_name_t. It will return
1350 * %GNUTLS_E_SHORT_MEMORY_BUFFER if @san_size is not large enough to
1351 * hold the value. In that case @san_size will be updated with the
1352 * required size. If the certificate does not have an Alternative
1353 * name with the specified sequence number then
1354 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1357 gnutls_x509_crt_get_subject_alt_name (gnutls_x509_crt_t cert
,
1358 unsigned int seq
, void *san
,
1360 unsigned int *critical
)
1362 return get_alt_name (cert
, "2.5.29.17", seq
, san
, san_size
, NULL
, critical
,
1367 * gnutls_x509_crt_get_issuer_alt_name:
1368 * @cert: should contain a #gnutls_x509_crt_t structure
1369 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1370 * @ian: is the place where the alternative name will be copied to
1371 * @ian_size: holds the size of ian.
1372 * @critical: will be non-zero if the extension is marked as critical (may be null)
1374 * This function retrieves the Issuer Alternative Name (2.5.29.18),
1375 * contained in the given certificate in the X509v3 Certificate
1378 * When the SAN type is otherName, it will extract the data in the
1379 * otherName's value field, and %GNUTLS_SAN_OTHERNAME is returned.
1380 * You may use gnutls_x509_crt_get_subject_alt_othername_oid() to get
1381 * the corresponding OID and the "virtual" SAN types (e.g.,
1382 * %GNUTLS_SAN_OTHERNAME_XMPP).
1384 * If an otherName OID is known, the data will be decoded. Otherwise
1385 * the returned data will be DER encoded, and you will have to decode
1386 * it yourself. Currently, only the RFC 3920 id-on-xmppAddr Issuer
1387 * AltName is recognized.
1389 * Returns: the alternative issuer name type on success, one of the
1390 * enumerated #gnutls_x509_subject_alt_name_t. It will return
1391 * %GNUTLS_E_SHORT_MEMORY_BUFFER if @ian_size is not large enough
1392 * to hold the value. In that case @ian_size will be updated with
1393 * the required size. If the certificate does not have an
1394 * Alternative name with the specified sequence number then
1395 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1400 gnutls_x509_crt_get_issuer_alt_name (gnutls_x509_crt_t cert
,
1401 unsigned int seq
, void *ian
,
1403 unsigned int *critical
)
1405 return get_alt_name (cert
, "2.5.29.18", seq
, ian
, ian_size
, NULL
, critical
,
1410 * gnutls_x509_crt_get_subject_alt_name2:
1411 * @cert: should contain a #gnutls_x509_crt_t structure
1412 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1413 * @san: is the place where the alternative name will be copied to
1414 * @san_size: holds the size of ret.
1415 * @san_type: holds the type of the alternative name (one of gnutls_x509_subject_alt_name_t).
1416 * @critical: will be non-zero if the extension is marked as critical (may be null)
1418 * This function will return the alternative names, contained in the
1419 * given certificate. It is the same as
1420 * gnutls_x509_crt_get_subject_alt_name() except for the fact that it
1421 * will return the type of the alternative name in @san_type even if
1422 * the function fails for some reason (i.e. the buffer provided is
1425 * Returns: the alternative subject name type on success, one of the
1426 * enumerated #gnutls_x509_subject_alt_name_t. It will return
1427 * %GNUTLS_E_SHORT_MEMORY_BUFFER if @san_size is not large enough
1428 * to hold the value. In that case @san_size will be updated with
1429 * the required size. If the certificate does not have an
1430 * Alternative name with the specified sequence number then
1431 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1434 gnutls_x509_crt_get_subject_alt_name2 (gnutls_x509_crt_t cert
,
1435 unsigned int seq
, void *san
,
1437 unsigned int *san_type
,
1438 unsigned int *critical
)
1440 return get_alt_name (cert
, "2.5.29.17", seq
, san
, san_size
, san_type
,
1445 * gnutls_x509_crt_get_issuer_alt_name2:
1446 * @cert: should contain a #gnutls_x509_crt_t structure
1447 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1448 * @ian: is the place where the alternative name will be copied to
1449 * @ian_size: holds the size of ret.
1450 * @ian_type: holds the type of the alternative name (one of gnutls_x509_subject_alt_name_t).
1451 * @critical: will be non-zero if the extension is marked as critical (may be null)
1453 * This function will return the alternative names, contained in the
1454 * given certificate. It is the same as
1455 * gnutls_x509_crt_get_issuer_alt_name() except for the fact that it
1456 * will return the type of the alternative name in @ian_type even if
1457 * the function fails for some reason (i.e. the buffer provided is
1460 * Returns: the alternative issuer name type on success, one of the
1461 * enumerated #gnutls_x509_subject_alt_name_t. It will return
1462 * %GNUTLS_E_SHORT_MEMORY_BUFFER if @ian_size is not large enough
1463 * to hold the value. In that case @ian_size will be updated with
1464 * the required size. If the certificate does not have an
1465 * Alternative name with the specified sequence number then
1466 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1472 gnutls_x509_crt_get_issuer_alt_name2 (gnutls_x509_crt_t cert
,
1473 unsigned int seq
, void *ian
,
1475 unsigned int *ian_type
,
1476 unsigned int *critical
)
1478 return get_alt_name (cert
, "2.5.29.18", seq
, ian
, ian_size
, ian_type
,
1483 * gnutls_x509_crt_get_subject_alt_othername_oid:
1484 * @cert: should contain a #gnutls_x509_crt_t structure
1485 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1486 * @oid: is the place where the otherName OID will be copied to
1487 * @oid_size: holds the size of ret.
1489 * This function will extract the type OID of an otherName Subject
1490 * Alternative Name, contained in the given certificate, and return
1491 * the type as an enumerated element.
1493 * This function is only useful if
1494 * gnutls_x509_crt_get_subject_alt_name() returned
1495 * %GNUTLS_SAN_OTHERNAME.
1497 * If @oid is null then only the size will be filled. The @oid
1498 * returned will be null terminated, although @oid_size will not
1499 * account for the trailing null.
1501 * Returns: the alternative subject name type on success, one of the
1502 * enumerated gnutls_x509_subject_alt_name_t. For supported OIDs, it
1503 * will return one of the virtual (GNUTLS_SAN_OTHERNAME_*) types,
1504 * e.g. %GNUTLS_SAN_OTHERNAME_XMPP, and %GNUTLS_SAN_OTHERNAME for
1505 * unknown OIDs. It will return %GNUTLS_E_SHORT_MEMORY_BUFFER if
1506 * @ian_size is not large enough to hold the value. In that case
1507 * @ian_size will be updated with the required size. If the
1508 * certificate does not have an Alternative name with the specified
1509 * sequence number and with the otherName type then
1510 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1513 gnutls_x509_crt_get_subject_alt_othername_oid (gnutls_x509_crt_t cert
,
1515 void *oid
, size_t * oid_size
)
1517 return get_alt_name (cert
, "2.5.29.17", seq
, oid
, oid_size
, NULL
, NULL
, 1);
1521 * gnutls_x509_crt_get_issuer_alt_othername_oid:
1522 * @cert: should contain a #gnutls_x509_crt_t structure
1523 * @seq: specifies the sequence number of the alt name (0 for the first one, 1 for the second etc.)
1524 * @ret: is the place where the otherName OID will be copied to
1525 * @ret_size: holds the size of ret.
1527 * This function will extract the type OID of an otherName Subject
1528 * Alternative Name, contained in the given certificate, and return
1529 * the type as an enumerated element.
1531 * If @oid is null then only the size will be filled. The @oid
1532 * returned will be null terminated, although @oid_size will not
1533 * account for the trailing null.
1535 * This function is only useful if
1536 * gnutls_x509_crt_get_issuer_alt_name() returned
1537 * %GNUTLS_SAN_OTHERNAME.
1539 * Returns: the alternative issuer name type on success, one of the
1540 * enumerated gnutls_x509_subject_alt_name_t. For supported OIDs, it
1541 * will return one of the virtual (GNUTLS_SAN_OTHERNAME_*) types,
1542 * e.g. %GNUTLS_SAN_OTHERNAME_XMPP, and %GNUTLS_SAN_OTHERNAME for
1543 * unknown OIDs. It will return %GNUTLS_E_SHORT_MEMORY_BUFFER if
1544 * @ret_size is not large enough to hold the value. In that case
1545 * @ret_size will be updated with the required size. If the
1546 * certificate does not have an Alternative name with the specified
1547 * sequence number and with the otherName type then
1548 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
1553 gnutls_x509_crt_get_issuer_alt_othername_oid (gnutls_x509_crt_t cert
,
1555 void *ret
, size_t * ret_size
)
1557 return get_alt_name (cert
, "2.5.29.18", seq
, ret
, ret_size
, NULL
, NULL
, 1);
1561 * gnutls_x509_crt_get_basic_constraints:
1562 * @cert: should contain a #gnutls_x509_crt_t structure
1563 * @critical: will be non-zero if the extension is marked as critical
1564 * @ca: pointer to output integer indicating CA status, may be NULL,
1565 * value is 1 if the certificate CA flag is set, 0 otherwise.
1566 * @pathlen: pointer to output integer indicating path length (may be
1567 * NULL), non-negative error codes indicate a present pathLenConstraint
1568 * field and the actual value, -1 indicate that the field is absent.
1570 * This function will read the certificate's basic constraints, and
1571 * return the certificates CA status. It reads the basicConstraints
1572 * X.509 extension (2.5.29.19).
1574 * Returns: If the certificate is a CA a positive value will be
1575 * returned, or (0) if the certificate does not have CA flag set. A
1576 * negative error code may be returned in case of errors. If the
1577 * certificate does not contain the basicConstraints extension
1578 * GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be returned.
1581 gnutls_x509_crt_get_basic_constraints (gnutls_x509_crt_t cert
,
1582 unsigned int *critical
,
1583 unsigned int *ca
, int *pathlen
)
1586 gnutls_datum_t basicConstraints
;
1587 unsigned int tmp_ca
;
1592 return GNUTLS_E_INVALID_REQUEST
;
1596 _gnutls_x509_crt_get_extension (cert
, "2.5.29.19", 0,
1597 &basicConstraints
, critical
)) < 0)
1602 if (basicConstraints
.size
== 0 || basicConstraints
.data
== NULL
)
1605 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1609 _gnutls_x509_ext_extract_basicConstraints (&tmp_ca
,
1611 basicConstraints
.data
,
1612 basicConstraints
.size
);
1615 _gnutls_free_datum (&basicConstraints
);
1627 * gnutls_x509_crt_get_ca_status:
1628 * @cert: should contain a #gnutls_x509_crt_t structure
1629 * @critical: will be non-zero if the extension is marked as critical
1631 * This function will return certificates CA status, by reading the
1632 * basicConstraints X.509 extension (2.5.29.19). If the certificate is
1633 * a CA a positive value will be returned, or (0) if the certificate
1634 * does not have CA flag set.
1636 * Use gnutls_x509_crt_get_basic_constraints() if you want to read the
1637 * pathLenConstraint field too.
1639 * Returns: A negative error code may be returned in case of parsing error.
1640 * If the certificate does not contain the basicConstraints extension
1641 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be returned.
1644 gnutls_x509_crt_get_ca_status (gnutls_x509_crt_t cert
, unsigned int *critical
)
1648 return gnutls_x509_crt_get_basic_constraints (cert
, critical
, &ca
,
1653 * gnutls_x509_crt_get_key_usage:
1654 * @cert: should contain a #gnutls_x509_crt_t structure
1655 * @key_usage: where the key usage bits will be stored
1656 * @critical: will be non-zero if the extension is marked as critical
1658 * This function will return certificate's key usage, by reading the
1659 * keyUsage X.509 extension (2.5.29.15). The key usage value will ORed
1660 * values of the: %GNUTLS_KEY_DIGITAL_SIGNATURE,
1661 * %GNUTLS_KEY_NON_REPUDIATION, %GNUTLS_KEY_KEY_ENCIPHERMENT,
1662 * %GNUTLS_KEY_DATA_ENCIPHERMENT, %GNUTLS_KEY_KEY_AGREEMENT,
1663 * %GNUTLS_KEY_KEY_CERT_SIGN, %GNUTLS_KEY_CRL_SIGN,
1664 * %GNUTLS_KEY_ENCIPHER_ONLY, %GNUTLS_KEY_DECIPHER_ONLY.
1666 * Returns: the certificate key usage, or a negative error code in case of
1667 * parsing error. If the certificate does not contain the keyUsage
1668 * extension %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be
1672 gnutls_x509_crt_get_key_usage (gnutls_x509_crt_t cert
,
1673 unsigned int *key_usage
,
1674 unsigned int *critical
)
1677 gnutls_datum_t keyUsage
;
1683 return GNUTLS_E_INVALID_REQUEST
;
1687 _gnutls_x509_crt_get_extension (cert
, "2.5.29.15", 0, &keyUsage
,
1693 if (keyUsage
.size
== 0 || keyUsage
.data
== NULL
)
1696 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1699 result
= _gnutls_x509_ext_extract_keyUsage (&_usage
, keyUsage
.data
,
1701 _gnutls_free_datum (&keyUsage
);
1703 *key_usage
= _usage
;
1715 * gnutls_x509_crt_get_proxy:
1716 * @cert: should contain a #gnutls_x509_crt_t structure
1717 * @critical: will be non-zero if the extension is marked as critical
1718 * @pathlen: pointer to output integer indicating path length (may be
1719 * NULL), non-negative error codes indicate a present pCPathLenConstraint
1720 * field and the actual value, -1 indicate that the field is absent.
1721 * @policyLanguage: output variable with OID of policy language
1722 * @policy: output variable with policy data
1723 * @sizeof_policy: output variable size of policy data
1725 * This function will get information from a proxy certificate. It
1726 * reads the ProxyCertInfo X.509 extension (1.3.6.1.5.5.7.1.14).
1728 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
1729 * otherwise a negative error code is returned.
1732 gnutls_x509_crt_get_proxy (gnutls_x509_crt_t cert
,
1733 unsigned int *critical
,
1735 char **policyLanguage
,
1736 char **policy
, size_t * sizeof_policy
)
1739 gnutls_datum_t proxyCertInfo
;
1744 return GNUTLS_E_INVALID_REQUEST
;
1748 _gnutls_x509_crt_get_extension (cert
, "1.3.6.1.5.5.7.1.14", 0,
1749 &proxyCertInfo
, critical
)) < 0)
1754 if (proxyCertInfo
.size
== 0 || proxyCertInfo
.data
== NULL
)
1757 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1760 result
= _gnutls_x509_ext_extract_proxyCertInfo (pathlen
,
1765 proxyCertInfo
.size
);
1766 _gnutls_free_datum (&proxyCertInfo
);
1777 * gnutls_x509_policy_release:
1778 * @policy: a certificate policy
1780 * This function will deinitialize all memory associated with the provided
1781 * @policy. The policy is allocated using gnutls_x509_crt_get_policy().
1785 void gnutls_x509_policy_release(struct gnutls_x509_policy_st
* policy
)
1789 gnutls_free(policy
->oid
);
1790 for (i
=0;i
<policy
->qualifiers
;i
++)
1791 gnutls_free(policy
->qualifier
[i
].data
);
1794 static int decode_user_notice(const void* data
, size_t size
, gnutls_datum_t
*txt
)
1796 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
1798 char choice_type
[64];
1800 gnutls_datum_t td
, utd
;
1802 ret
= asn1_create_element
1803 (_gnutls_get_pkix (), "PKIX1.UserNotice", &c2
);
1804 if (ret
!= ASN1_SUCCESS
)
1807 ret
= GNUTLS_E_PARSING_ERROR
;
1811 ret
= asn1_der_decoding (&c2
, data
, size
, NULL
);
1812 if (ret
!= ASN1_SUCCESS
)
1815 ret
= GNUTLS_E_PARSING_ERROR
;
1819 len
= sizeof(choice_type
);
1820 ret
= asn1_read_value(c2
, "explicitText", choice_type
, &len
);
1821 if (ret
!= ASN1_SUCCESS
)
1824 ret
= GNUTLS_E_PARSING_ERROR
;
1828 if (strcmp(choice_type
, "utf8String") != 0 && strcmp(choice_type
, "IA5String") != 0 &&
1829 strcmp(choice_type
, "bmpString") != 0 && strcmp(choice_type
, "visibleString") != 0)
1832 ret
= GNUTLS_E_PARSING_ERROR
;
1836 snprintf (name
, sizeof (name
), "explicitText.%s", choice_type
);
1838 ret
= _gnutls_x509_read_value(c2
, name
, &td
);
1845 if (strcmp(choice_type
, "bmpString") == 0)
1846 { /* convert to UTF-8 */
1847 ret
= _gnutls_ucs2_to_utf8(td
.data
, td
.size
, &utd
);
1848 _gnutls_free_datum(&td
);
1860 /* _gnutls_x509_read_value allows that */
1861 td
.data
[td
.size
] = 0;
1864 txt
->data
= (void*)td
.data
;
1865 txt
->size
= td
.size
;
1869 asn1_delete_structure (&c2
);
1875 * gnutls_x509_crt_get_policy:
1876 * @cert: should contain a #gnutls_x509_crt_t structure
1877 * @indx: This specifies which policy to return. Use (0) to get the first one.
1878 * @policy: A pointer to a policy structure.
1879 * @critical: will be non-zero if the extension is marked as critical
1881 * This function will extract the certificate policy (extension 2.5.29.32)
1882 * specified by the given index.
1884 * The policy returned by this function must be deinitialized by using
1885 * gnutls_x509_policy_release().
1887 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
1888 * if the extension is not present, otherwise a negative error value.
1893 gnutls_x509_crt_get_policy (gnutls_x509_crt_t crt
, int indx
,
1894 struct gnutls_x509_policy_st
* policy
,
1895 unsigned int *critical
)
1897 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
1899 char tmpoid
[MAX_OID_SIZE
];
1900 gnutls_datum_t tmpd
= {NULL
, 0};
1907 return GNUTLS_E_INVALID_REQUEST
;
1910 memset(policy
, 0, sizeof(*policy
));
1913 _gnutls_x509_crt_get_extension (crt
, "2.5.29.32", 0, &tmpd
,
1919 if (tmpd
.size
== 0 || tmpd
.data
== NULL
)
1922 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1925 ret
= asn1_create_element
1926 (_gnutls_get_pkix (), "PKIX1.certificatePolicies", &c2
);
1927 if (ret
!= ASN1_SUCCESS
)
1930 ret
= _gnutls_asn2err (ret
);
1934 ret
= asn1_der_decoding (&c2
, tmpd
.data
, tmpd
.size
, NULL
);
1935 if (ret
!= ASN1_SUCCESS
)
1938 ret
= _gnutls_asn2err (ret
);
1941 _gnutls_free_datum (&tmpd
);
1944 /* create a string like "?1"
1946 snprintf (tmpstr
, sizeof (tmpstr
), "?%u.policyIdentifier", indx
);
1948 ret
= _gnutls_x509_read_value(c2
, tmpstr
, &tmpd
);
1950 if (ret
== GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
)
1951 ret
= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
1958 policy
->oid
= (void*)tmpd
.data
;
1961 for (i
=0;i
<GNUTLS_MAX_QUALIFIERS
;i
++)
1965 snprintf (tmpstr
, sizeof (tmpstr
), "?%u.policyQualifiers.?%u.policyQualifierId", indx
, i
+1);
1967 len
= sizeof(tmpoid
);
1968 ret
= asn1_read_value(c2
, tmpstr
, tmpoid
, &len
);
1970 if (ret
== ASN1_ELEMENT_NOT_FOUND
)
1971 break; /* finished */
1973 if (ret
!= ASN1_SUCCESS
)
1976 ret
= _gnutls_asn2err (ret
);
1980 if (strcmp(tmpoid
, "1.3.6.1.5.5.7.2.1") == 0)
1982 snprintf (tmpstr
, sizeof (tmpstr
), "?%u.policyQualifiers.?%u.qualifier", indx
, i
+1);
1984 ret
= _gnutls_x509_read_string(c2
, tmpstr
, &td
, ASN1_ETYPE_IA5_STRING
);
1991 policy
->qualifier
[i
].data
= (void*)td
.data
;
1992 policy
->qualifier
[i
].size
= td
.size
;
1994 policy
->qualifier
[i
].type
= GNUTLS_X509_QUALIFIER_URI
;
1996 else if (strcmp(tmpoid
, "1.3.6.1.5.5.7.2.2") == 0)
2000 snprintf (tmpstr
, sizeof (tmpstr
), "?%u.policyQualifiers.?%u.qualifier", indx
, i
+1);
2002 ret
= _gnutls_x509_read_value(c2
, tmpstr
, &td
);
2009 ret
= decode_user_notice(td
.data
, td
.size
, &txt
);
2010 gnutls_free(td
.data
);
2019 policy
->qualifier
[i
].data
= (void*)txt
.data
;
2020 policy
->qualifier
[i
].size
= txt
.size
;
2021 policy
->qualifier
[i
].type
= GNUTLS_X509_QUALIFIER_NOTICE
;
2024 policy
->qualifier
[i
].type
= GNUTLS_X509_QUALIFIER_UNKNOWN
;
2026 policy
->qualifiers
++;
2034 gnutls_x509_policy_release(policy
);
2037 _gnutls_free_datum (&tmpd
);
2038 asn1_delete_structure (&c2
);
2044 * gnutls_x509_crt_get_extension_by_oid:
2045 * @cert: should contain a #gnutls_x509_crt_t structure
2046 * @oid: holds an Object Identified in null terminated string
2047 * @indx: In case multiple same OIDs exist in the extensions, this specifies which to send. Use (0) to get the first one.
2048 * @buf: a pointer to a structure to hold the name (may be null)
2049 * @buf_size: initially holds the size of @buf
2050 * @critical: will be non-zero if the extension is marked as critical
2052 * This function will return the extension specified by the OID in the
2053 * certificate. The extensions will be returned as binary data DER
2054 * encoded, in the provided buffer.
2056 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
2057 * otherwise a negative error code is returned. If the certificate does not
2058 * contain the specified extension
2059 * GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be returned.
2062 gnutls_x509_crt_get_extension_by_oid (gnutls_x509_crt_t cert
,
2063 const char *oid
, int indx
,
2064 void *buf
, size_t * buf_size
,
2065 unsigned int *critical
)
2068 gnutls_datum_t output
;
2073 return GNUTLS_E_INVALID_REQUEST
;
2077 _gnutls_x509_crt_get_extension (cert
, oid
, indx
, &output
,
2084 if (output
.size
== 0 || output
.data
== NULL
)
2087 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
2090 if (output
.size
> (unsigned int) *buf_size
)
2092 *buf_size
= output
.size
;
2093 _gnutls_free_datum (&output
);
2094 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
2097 *buf_size
= output
.size
;
2100 memcpy (buf
, output
.data
, output
.size
);
2102 _gnutls_free_datum (&output
);
2109 * gnutls_x509_crt_get_extension_oid:
2110 * @cert: should contain a #gnutls_x509_crt_t structure
2111 * @indx: Specifies which extension OID to send. Use (0) to get the first one.
2112 * @oid: a pointer to a structure to hold the OID (may be null)
2113 * @oid_size: initially holds the size of @oid
2115 * This function will return the requested extension OID in the certificate.
2116 * The extension OID will be stored as a string in the provided buffer.
2118 * The @oid returned will be null terminated, although @oid_size will not
2119 * account for the trailing null.
2121 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
2122 * otherwise a negative error code is returned. If you have reached the
2123 * last extension available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
2127 gnutls_x509_crt_get_extension_oid (gnutls_x509_crt_t cert
, int indx
,
2128 void *oid
, size_t * oid_size
)
2135 return GNUTLS_E_INVALID_REQUEST
;
2138 result
= _gnutls_x509_crt_get_extension_oid (cert
, indx
, oid
, oid_size
);
2149 * gnutls_x509_crt_get_extension_info:
2150 * @cert: should contain a #gnutls_x509_crt_t structure
2151 * @indx: Specifies which extension OID to send. Use (0) to get the first one.
2152 * @oid: a pointer to a structure to hold the OID
2153 * @oid_size: initially holds the maximum size of @oid, on return
2154 * holds actual size of @oid.
2155 * @critical: output variable with critical flag, may be NULL.
2157 * This function will return the requested extension OID in the
2158 * certificate, and the critical flag for it. The extension OID will
2159 * be stored as a string in the provided buffer. Use
2160 * gnutls_x509_crt_get_extension_data() to extract the data.
2162 * If the buffer provided is not long enough to hold the output, then
2163 * @oid_size is updated and %GNUTLS_E_SHORT_MEMORY_BUFFER will be
2164 * returned. The @oid returned will be null terminated, although
2165 * @oid_size will not account for the trailing null.
2167 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
2168 * otherwise a negative error code is returned. If you have reached the
2169 * last extension available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
2173 gnutls_x509_crt_get_extension_info (gnutls_x509_crt_t cert
, int indx
,
2174 void *oid
, size_t * oid_size
,
2175 unsigned int *critical
)
2178 char str_critical
[10];
2179 char name
[ASN1_MAX_NAME_SIZE
];
2185 return GNUTLS_E_INVALID_REQUEST
;
2188 snprintf (name
, sizeof (name
), "tbsCertificate.extensions.?%u.extnID",
2192 result
= asn1_read_value (cert
->cert
, name
, oid
, &len
);
2195 if (result
== ASN1_ELEMENT_NOT_FOUND
)
2196 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
2197 else if (result
!= ASN1_SUCCESS
)
2200 return _gnutls_asn2err (result
);
2203 snprintf (name
, sizeof (name
), "tbsCertificate.extensions.?%u.critical",
2205 len
= sizeof (str_critical
);
2206 result
= asn1_read_value (cert
->cert
, name
, str_critical
, &len
);
2207 if (result
!= ASN1_SUCCESS
)
2210 return _gnutls_asn2err (result
);
2215 if (str_critical
[0] == 'T')
2226 * gnutls_x509_crt_get_extension_data:
2227 * @cert: should contain a #gnutls_x509_crt_t structure
2228 * @indx: Specifies which extension OID to send. Use (0) to get the first one.
2229 * @data: a pointer to a structure to hold the data (may be null)
2230 * @sizeof_data: initially holds the size of @oid
2232 * This function will return the requested extension data in the
2233 * certificate. The extension data will be stored as a string in the
2236 * Use gnutls_x509_crt_get_extension_info() to extract the OID and
2237 * critical flag. Use gnutls_x509_crt_get_extension_by_oid() instead,
2238 * if you want to get data indexed by the extension OID rather than
2241 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
2242 * otherwise a negative error code is returned. If you have reached the
2243 * last extension available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
2247 gnutls_x509_crt_get_extension_data (gnutls_x509_crt_t cert
, int indx
,
2248 void *data
, size_t * sizeof_data
)
2251 char name
[ASN1_MAX_NAME_SIZE
];
2256 return GNUTLS_E_INVALID_REQUEST
;
2259 snprintf (name
, sizeof (name
), "tbsCertificate.extensions.?%u.extnValue",
2263 result
= asn1_read_value (cert
->cert
, name
, data
, &len
);
2266 if (result
== ASN1_ELEMENT_NOT_FOUND
)
2267 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
2268 else if (result
< 0)
2271 return _gnutls_asn2err (result
);
2278 _gnutls_x509_crt_get_raw_dn2 (gnutls_x509_crt_t cert
,
2279 const char *whom
, gnutls_datum_t
* start
)
2281 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
2284 gnutls_datum_t signed_data
= { NULL
, 0 };
2286 /* get the issuer of 'cert'
2289 asn1_create_element (_gnutls_get_pkix (), "PKIX1.TBSCertificate",
2290 &c2
)) != ASN1_SUCCESS
)
2293 return _gnutls_asn2err (result
);
2297 _gnutls_x509_get_signed_data (cert
->cert
, "tbsCertificate", &signed_data
);
2304 result
= asn1_der_decoding (&c2
, signed_data
.data
, signed_data
.size
, NULL
);
2305 if (result
!= ASN1_SUCCESS
)
2308 asn1_delete_structure (&c2
);
2309 result
= _gnutls_asn2err (result
);
2314 asn1_der_decoding_startEnd (c2
, signed_data
.data
, signed_data
.size
,
2315 whom
, &start1
, &end1
);
2317 if (result
!= ASN1_SUCCESS
)
2320 result
= _gnutls_asn2err (result
);
2324 len1
= end1
- start1
+ 1;
2326 _gnutls_set_datum (start
, &signed_data
.data
[start1
], len1
);
2331 asn1_delete_structure (&c2
);
2332 _gnutls_free_datum (&signed_data
);
2337 * gnutls_x509_crt_get_raw_issuer_dn:
2338 * @cert: should contain a #gnutls_x509_crt_t structure
2339 * @start: will hold the starting point of the DN
2341 * This function will return a pointer to the DER encoded DN structure
2342 * and the length. This points to allocated data that must be free'd using gnutls_free().
2344 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
2345 * negative error value.or a negative error code on error.
2349 gnutls_x509_crt_get_raw_issuer_dn (gnutls_x509_crt_t cert
,
2350 gnutls_datum_t
* start
)
2352 return _gnutls_x509_crt_get_raw_dn2 (cert
, "issuer", start
);
2356 * gnutls_x509_crt_get_raw_dn:
2357 * @cert: should contain a #gnutls_x509_crt_t structure
2358 * @start: will hold the starting point of the DN
2360 * This function will return a pointer to the DER encoded DN structure and
2361 * the length. This points to allocated data that must be free'd using gnutls_free().
2363 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
2364 * negative error value. or a negative error code on error.
2368 gnutls_x509_crt_get_raw_dn (gnutls_x509_crt_t cert
, gnutls_datum_t
* start
)
2370 return _gnutls_x509_crt_get_raw_dn2 (cert
, "subject", start
);
2374 get_dn (gnutls_x509_crt_t cert
, const char *whom
, gnutls_x509_dn_t
* dn
)
2376 *dn
= asn1_find_node (cert
->cert
, whom
);
2378 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2383 * gnutls_x509_crt_get_subject:
2384 * @cert: should contain a #gnutls_x509_crt_t structure
2385 * @dn: output variable with pointer to uint8_t DN.
2387 * Return the Certificate's Subject DN as a %gnutls_x509_dn_t data type,
2388 * that can be decoded using gnutls_x509_dn_get_rdn_ava().
2390 * Note that @dn should be treated as constant. Because it points
2391 * into the @cert object, you should not use @dn after @cert is
2394 * Returns: Returns 0 on success, or an error code.
2397 gnutls_x509_crt_get_subject (gnutls_x509_crt_t cert
, gnutls_x509_dn_t
* dn
)
2399 return get_dn (cert
, "tbsCertificate.subject.rdnSequence", dn
);
2403 * gnutls_x509_crt_get_issuer:
2404 * @cert: should contain a #gnutls_x509_crt_t structure
2405 * @dn: output variable with pointer to uint8_t DN
2407 * Return the Certificate's Issuer DN as a %gnutls_x509_dn_t data type,
2408 * that can be decoded using gnutls_x509_dn_get_rdn_ava().
2410 * Note that @dn should be treated as constant. Because it points
2411 * into the @cert object, you should not use @dn after @cert is
2414 * Returns: Returns 0 on success, or an error code.
2417 gnutls_x509_crt_get_issuer (gnutls_x509_crt_t cert
, gnutls_x509_dn_t
* dn
)
2419 return get_dn (cert
, "tbsCertificate.issuer.rdnSequence", dn
);
2423 * gnutls_x509_dn_get_rdn_ava:
2424 * @dn: a pointer to DN
2425 * @irdn: index of RDN
2426 * @iava: index of AVA.
2427 * @ava: Pointer to structure which will hold output information.
2429 * Get pointers to data within the DN. The format of the @ava structure
2432 * struct gnutls_x509_ava_st {
2433 * gnutls_datum_t oid;
2434 * gnutls_datum_t value;
2435 * unsigned long value_tag;
2438 * The X.509 distinguished name is a sequence of sequences of strings
2439 * and this is what the @irdn and @iava indexes model.
2441 * Note that @ava will contain pointers into the @dn structure which
2442 * in turns points to the original certificate. Thus you should not
2443 * modify any data or deallocate any of those.
2445 * This is a low-level function that requires the caller to do the
2446 * value conversions when necessary (e.g. from UCS-2).
2448 * Returns: Returns 0 on success, or an error code.
2451 gnutls_x509_dn_get_rdn_ava (gnutls_x509_dn_t dn
,
2452 int irdn
, int iava
, gnutls_x509_ava_st
* ava
)
2454 ASN1_TYPE rdn
, elem
;
2455 ASN1_DATA_NODE vnode
;
2457 int lenlen
, remlen
, ret
;
2458 char rbuf
[ASN1_MAX_NAME_SIZE
];
2460 const unsigned char *ptr
;
2463 irdn
++; /* 0->1, 1->2 etc */
2465 snprintf (rbuf
, sizeof (rbuf
), "rdnSequence.?%d.?%d", irdn
, iava
);
2466 rdn
= asn1_find_node (dn
, rbuf
);
2470 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2473 snprintf (rbuf
, sizeof (rbuf
), "?%d.type", iava
);
2474 elem
= asn1_find_node (rdn
, rbuf
);
2478 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2481 ret
= asn1_read_node_value(elem
, &vnode
);
2482 if (ret
!= ASN1_SUCCESS
)
2485 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2488 ava
->oid
.data
= (void*)vnode
.value
;
2489 ava
->oid
.size
= vnode
.value_len
;
2491 snprintf (rbuf
, sizeof (rbuf
), "?%d.value", iava
);
2492 elem
= asn1_find_node (rdn
, rbuf
);
2496 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2499 ret
= asn1_read_node_value(elem
, &vnode
);
2500 if (ret
!= ASN1_SUCCESS
)
2503 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
;
2505 /* The value still has the previous tag's length bytes, plus the
2506 * current value's tag and length bytes. Decode them.
2510 remlen
= vnode
.value_len
;
2511 len
= asn1_get_length_der (ptr
, remlen
, &lenlen
);
2515 return GNUTLS_E_ASN1_DER_ERROR
;
2520 ret
= asn1_get_tag_der (ptr
, remlen
, &cls
, &lenlen
, &ava
->value_tag
);
2524 return _gnutls_asn2err (ret
);
2533 tmp
= asn1_get_length_der (ptr
, remlen
, &lenlen
);
2537 return GNUTLS_E_ASN1_DER_ERROR
;
2539 ava
->value
.size
= tmp
;
2541 ava
->value
.data
= (void*)(ptr
+ lenlen
);
2547 * gnutls_x509_crt_get_fingerprint:
2548 * @cert: should contain a #gnutls_x509_crt_t structure
2549 * @algo: is a digest algorithm
2550 * @buf: a pointer to a structure to hold the fingerprint (may be null)
2551 * @buf_size: initially holds the size of @buf
2553 * This function will calculate and copy the certificate's fingerprint
2554 * in the provided buffer.
2556 * If the buffer is null then only the size will be filled.
2558 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is
2559 * not long enough, and in that case the *buf_size will be updated
2560 * with the required size. On success 0 is returned.
2563 gnutls_x509_crt_get_fingerprint (gnutls_x509_crt_t cert
,
2564 gnutls_digest_algorithm_t algo
,
2565 void *buf
, size_t * buf_size
)
2572 if (buf_size
== 0 || cert
== NULL
)
2574 return GNUTLS_E_INVALID_REQUEST
;
2578 asn1_der_coding (cert
->cert
, "", NULL
, &cert_buf_size
, NULL
);
2580 cert_buf
= gnutls_malloc (cert_buf_size
);
2581 if (cert_buf
== NULL
)
2584 return GNUTLS_E_MEMORY_ERROR
;
2587 result
= asn1_der_coding (cert
->cert
, "", cert_buf
, &cert_buf_size
, NULL
);
2589 if (result
!= ASN1_SUCCESS
)
2592 gnutls_free (cert_buf
);
2593 return _gnutls_asn2err (result
);
2596 tmp
.data
= cert_buf
;
2597 tmp
.size
= cert_buf_size
;
2599 result
= gnutls_fingerprint (algo
, &tmp
, buf
, buf_size
);
2600 gnutls_free (cert_buf
);
2606 * gnutls_x509_crt_export:
2607 * @cert: Holds the certificate
2608 * @format: the format of output params. One of PEM or DER.
2609 * @output_data: will contain a certificate PEM or DER encoded
2610 * @output_data_size: holds the size of output_data (and will be
2611 * replaced by the actual size of parameters)
2613 * This function will export the certificate to DER or PEM format.
2615 * If the buffer provided is not long enough to hold the output, then
2616 * *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
2619 * If the structure is PEM encoded, it will have a header
2620 * of "BEGIN CERTIFICATE".
2622 * Returns: In case of failure a negative error code will be
2623 * returned, and 0 on success.
2626 gnutls_x509_crt_export (gnutls_x509_crt_t cert
,
2627 gnutls_x509_crt_fmt_t format
, void *output_data
,
2628 size_t * output_data_size
)
2633 return GNUTLS_E_INVALID_REQUEST
;
2636 return _gnutls_x509_export_int (cert
->cert
, format
, "CERTIFICATE",
2637 output_data
, output_data_size
);
2641 * gnutls_x509_crt_export2:
2642 * @cert: Holds the certificate
2643 * @format: the format of output params. One of PEM or DER.
2644 * @out: will contain a certificate PEM or DER encoded
2646 * This function will export the certificate to DER or PEM format.
2647 * The output buffer is allocated using gnutls_malloc().
2649 * If the structure is PEM encoded, it will have a header
2650 * of "BEGIN CERTIFICATE".
2652 * Returns: In case of failure a negative error code will be
2653 * returned, and 0 on success.
2658 gnutls_x509_crt_export2 (gnutls_x509_crt_t cert
,
2659 gnutls_x509_crt_fmt_t format
, gnutls_datum_t
* out
)
2664 return GNUTLS_E_INVALID_REQUEST
;
2667 return _gnutls_x509_export_int2 (cert
->cert
, format
, "CERTIFICATE", out
);
2671 _gnutls_get_key_id (gnutls_pk_algorithm_t pk
, gnutls_pk_params_st
* params
,
2672 unsigned char *output_data
,
2673 size_t * output_data_size
)
2676 gnutls_datum_t der
= { NULL
, 0 };
2677 const gnutls_digest_algorithm_t hash
= GNUTLS_DIG_SHA1
;
2678 unsigned int digest_len
= _gnutls_hash_get_algo_len(hash
);
2680 if (output_data
== NULL
|| *output_data_size
< digest_len
)
2683 *output_data_size
= digest_len
;
2684 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
2687 ret
= _gnutls_x509_encode_PKI_params(&der
, pk
, params
);
2689 return gnutls_assert_val(ret
);
2691 ret
= _gnutls_hash_fast(hash
, der
.data
, der
.size
, output_data
);
2697 *output_data_size
= digest_len
;
2703 _gnutls_free_datum (&der
);
2708 * gnutls_x509_crt_get_key_id:
2709 * @crt: Holds the certificate
2710 * @flags: should be 0 for now
2711 * @output_data: will contain the key ID
2712 * @output_data_size: holds the size of output_data (and will be
2713 * replaced by the actual size of parameters)
2715 * This function will return a unique ID that depends on the public
2716 * key parameters. This ID can be used in checking whether a
2717 * certificate corresponds to the given private key.
2719 * If the buffer provided is not long enough to hold the output, then
2720 * *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
2721 * be returned. The output will normally be a SHA-1 hash output,
2722 * which is 20 bytes.
2724 * Returns: In case of failure a negative error code will be
2725 * returned, and 0 on success.
2728 gnutls_x509_crt_get_key_id (gnutls_x509_crt_t crt
, unsigned int flags
,
2729 unsigned char *output_data
,
2730 size_t * output_data_size
)
2733 gnutls_pk_params_st params
;
2738 return GNUTLS_E_INVALID_REQUEST
;
2741 pk
= gnutls_x509_crt_get_pk_algorithm (crt
, NULL
);
2748 ret
= _gnutls_x509_crt_get_mpis (crt
, ¶ms
);
2755 ret
= _gnutls_get_key_id(pk
, ¶ms
, output_data
, output_data_size
);
2757 gnutls_pk_params_release(¶ms
);
2763 /* This is exactly as gnutls_x509_crt_check_revocation() except that
2767 _gnutls_x509_crt_check_revocation (gnutls_x509_crt_t cert
,
2768 const gnutls_x509_crl_t
* crl_list
,
2769 int crl_list_length
,
2770 gnutls_verify_output_function func
)
2772 uint8_t serial
[128];
2773 uint8_t cert_serial
[128];
2774 size_t serial_size
, cert_serial_size
;
2775 int ncerts
, ret
, i
, j
;
2776 gnutls_datum_t dn1
, dn2
;
2781 return GNUTLS_E_INVALID_REQUEST
;
2784 for (j
= 0; j
< crl_list_length
; j
++)
2785 { /* do for all the crls */
2787 /* Step 1. check if issuer's DN match
2789 ret
= gnutls_x509_crl_get_raw_issuer_dn (crl_list
[j
], &dn1
);
2796 ret
= gnutls_x509_crt_get_raw_issuer_dn (cert
, &dn2
);
2803 ret
= _gnutls_x509_compare_raw_dn (&dn1
, &dn2
);
2804 _gnutls_free_datum (&dn1
);
2805 _gnutls_free_datum (&dn2
);
2808 /* issuers do not match so don't even
2814 /* Step 2. Read the certificate's serial number
2816 cert_serial_size
= sizeof (cert_serial
);
2817 ret
= gnutls_x509_crt_get_serial (cert
, cert_serial
, &cert_serial_size
);
2824 /* Step 3. cycle through the CRL serials and compare with
2825 * certificate serial we have.
2828 ncerts
= gnutls_x509_crl_get_crt_count (crl_list
[j
]);
2835 for (i
= 0; i
< ncerts
; i
++)
2837 serial_size
= sizeof (serial
);
2839 gnutls_x509_crl_get_crt_serial (crl_list
[j
], i
, serial
,
2840 &serial_size
, NULL
);
2848 if (serial_size
== cert_serial_size
)
2850 if (memcmp (serial
, cert_serial
, serial_size
) == 0)
2853 if (func
) func(cert
, NULL
, crl_list
[j
], GNUTLS_CERT_REVOKED
|GNUTLS_CERT_INVALID
);
2854 return 1; /* revoked! */
2858 if (func
) func(cert
, NULL
, crl_list
[j
], 0);
2861 return 0; /* not revoked. */
2866 * gnutls_x509_crt_check_revocation:
2867 * @cert: should contain a #gnutls_x509_crt_t structure
2868 * @crl_list: should contain a list of gnutls_x509_crl_t structures
2869 * @crl_list_length: the length of the crl_list
2871 * This function will return check if the given certificate is
2872 * revoked. It is assumed that the CRLs have been verified before.
2874 * Returns: 0 if the certificate is NOT revoked, and 1 if it is. A
2875 * negative error code is returned on error.
2878 gnutls_x509_crt_check_revocation (gnutls_x509_crt_t cert
,
2879 const gnutls_x509_crl_t
* crl_list
,
2880 int crl_list_length
)
2882 return _gnutls_x509_crt_check_revocation(cert
, crl_list
, crl_list_length
, NULL
);
2886 * gnutls_x509_crt_get_verify_algorithm:
2887 * @crt: Holds the certificate
2888 * @signature: contains the signature
2889 * @hash: The result of the call with the hash algorithm used for signature
2891 * This function will read the certifcate and the signed data to
2892 * determine the hash algorithm used to generate the signature.
2894 * Deprecated: Use gnutls_pubkey_get_verify_algorithm() instead.
2896 * Returns: the 0 if the hash algorithm is found. A negative error code is
2897 * returned on error.
2902 gnutls_x509_crt_get_verify_algorithm (gnutls_x509_crt_t crt
,
2903 const gnutls_datum_t
* signature
,
2904 gnutls_digest_algorithm_t
* hash
)
2906 gnutls_pk_params_st issuer_params
;
2912 return GNUTLS_E_INVALID_REQUEST
;
2915 ret
= _gnutls_x509_crt_get_mpis (crt
, &issuer_params
);
2922 ret
= _gnutls_x509_verify_algorithm (hash
,
2924 gnutls_x509_crt_get_pk_algorithm (crt
,
2928 /* release allocated mpis */
2929 gnutls_pk_params_release(&issuer_params
);
2937 * gnutls_x509_crt_get_preferred_hash_algorithm:
2938 * @crt: Holds the certificate
2939 * @hash: The result of the call with the hash algorithm used for signature
2940 * @mand: If non-zero it means that the algorithm MUST use this hash. May be NULL.
2942 * This function will read the certifcate and return the appropriate digest
2943 * algorithm to use for signing with this certificate. Some certificates (i.e.
2944 * DSA might not be able to sign without the preferred algorithm).
2946 * Deprecated: Please use gnutls_pubkey_get_preferred_hash_algorithm().
2948 * Returns: the 0 if the hash algorithm is found. A negative error code is
2949 * returned on error.
2954 gnutls_x509_crt_get_preferred_hash_algorithm (gnutls_x509_crt_t crt
,
2955 gnutls_digest_algorithm_t
*
2956 hash
, unsigned int *mand
)
2958 gnutls_pk_params_st issuer_params
;
2964 return GNUTLS_E_INVALID_REQUEST
;
2967 ret
= _gnutls_x509_crt_get_mpis (crt
, &issuer_params
);
2975 _gnutls_pk_get_hash_algorithm (gnutls_x509_crt_get_pk_algorithm
2976 (crt
, NULL
), &issuer_params
,
2979 /* release allocated mpis */
2980 gnutls_pk_params_release(&issuer_params
);
2986 * gnutls_x509_crt_verify_data:
2987 * @crt: Holds the certificate
2988 * @flags: should be 0 for now
2989 * @data: holds the data to be signed
2990 * @signature: contains the signature
2992 * This function will verify the given signed data, using the
2993 * parameters from the certificate.
2995 * Deprecated. Please use gnutls_pubkey_verify_data().
2997 * Returns: In case of a verification failure %GNUTLS_E_PK_SIG_VERIFY_FAILED
2998 * is returned, and zero or positive code on success.
3001 gnutls_x509_crt_verify_data (gnutls_x509_crt_t crt
, unsigned int flags
,
3002 const gnutls_datum_t
* data
,
3003 const gnutls_datum_t
* signature
)
3010 return GNUTLS_E_INVALID_REQUEST
;
3013 result
= _gnutls_x509_verify_data (GNUTLS_DIG_UNKNOWN
, data
, signature
, crt
);
3024 * gnutls_x509_crt_verify_hash:
3025 * @crt: Holds the certificate
3026 * @flags: should be 0 for now
3027 * @hash: holds the hash digest to be verified
3028 * @signature: contains the signature
3030 * This function will verify the given signed digest, using the
3031 * parameters from the certificate.
3033 * Deprecated. Please use gnutls_pubkey_verify_data2() or gnutls_pubkey_verify_hash2().
3035 * Returns: In case of a verification failure %GNUTLS_E_PK_SIG_VERIFY_FAILED
3036 * is returned, and zero or positive code on success.
3039 gnutls_x509_crt_verify_hash (gnutls_x509_crt_t crt
, unsigned int flags
,
3040 const gnutls_datum_t
* hash
,
3041 const gnutls_datum_t
* signature
)
3043 gnutls_pk_params_st params
;
3044 gnutls_digest_algorithm_t algo
;
3050 return GNUTLS_E_INVALID_REQUEST
;
3053 ret
= gnutls_x509_crt_get_verify_algorithm (crt
, signature
, &algo
);
3055 return gnutls_assert_val(ret
);
3057 /* Read the MPI parameters from the issuer's certificate.
3060 _gnutls_x509_crt_get_mpis (crt
, ¶ms
);
3068 pubkey_verify_hashed_data (gnutls_x509_crt_get_pk_algorithm (crt
, NULL
), algo
,
3069 hash
, signature
, ¶ms
);
3075 /* release all allocated MPIs
3077 gnutls_pk_params_release(¶ms
);
3083 * gnutls_x509_crt_get_crl_dist_points:
3084 * @cert: should contain a #gnutls_x509_crt_t structure
3085 * @seq: specifies the sequence number of the distribution point (0 for the first one, 1 for the second etc.)
3086 * @ret: is the place where the distribution point will be copied to
3087 * @ret_size: holds the size of ret.
3088 * @reason_flags: Revocation reasons. An ORed sequence of flags from %gnutls_x509_crl_reason_flags_t.
3089 * @critical: will be non-zero if the extension is marked as critical (may be null)
3091 * This function retrieves the CRL distribution points (2.5.29.31),
3092 * contained in the given certificate in the X509v3 Certificate
3095 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER and updates @ret_size if
3096 * @ret_size is not enough to hold the distribution point, or the
3097 * type of the distribution point if everything was ok. The type is
3098 * one of the enumerated %gnutls_x509_subject_alt_name_t. If the
3099 * certificate does not have an Alternative name with the specified
3100 * sequence number then %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is
3104 gnutls_x509_crt_get_crl_dist_points (gnutls_x509_crt_t cert
,
3105 unsigned int seq
, void *ret
,
3107 unsigned int *reason_flags
,
3108 unsigned int *critical
)
3111 gnutls_datum_t dist_points
= { NULL
, 0 };
3112 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
3113 char name
[ASN1_MAX_NAME_SIZE
];
3115 gnutls_x509_subject_alt_name_t type
;
3121 return GNUTLS_E_INVALID_REQUEST
;
3124 if (*ret_size
> 0 && ret
)
3125 memset (ret
, 0, *ret_size
);
3133 _gnutls_x509_crt_get_extension (cert
, "2.5.29.31", 0, &dist_points
,
3140 if (dist_points
.size
== 0 || dist_points
.data
== NULL
)
3143 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
3146 result
= asn1_create_element
3147 (_gnutls_get_pkix (), "PKIX1.CRLDistributionPoints", &c2
);
3148 if (result
!= ASN1_SUCCESS
)
3151 _gnutls_free_datum (&dist_points
);
3152 return _gnutls_asn2err (result
);
3155 result
= asn1_der_decoding (&c2
, dist_points
.data
, dist_points
.size
, NULL
);
3156 _gnutls_free_datum (&dist_points
);
3158 if (result
!= ASN1_SUCCESS
)
3161 asn1_delete_structure (&c2
);
3162 return _gnutls_asn2err (result
);
3165 /* Return the different names from the first CRLDistr. point.
3166 * The whole thing is a mess.
3168 _gnutls_str_cpy (name
, sizeof (name
), "?1.distributionPoint.fullName");
3170 result
= _gnutls_parse_general_name (c2
, name
, seq
, ret
, ret_size
, NULL
, 0);
3173 asn1_delete_structure (&c2
);
3180 /* Read the CRL reasons.
3184 _gnutls_str_cpy (name
, sizeof (name
), "?1.reasons");
3186 reasons
[0] = reasons
[1] = 0;
3188 len
= sizeof (reasons
);
3189 result
= asn1_read_value (c2
, name
, reasons
, &len
);
3191 if (result
!= ASN1_VALUE_NOT_FOUND
&& result
!= ASN1_SUCCESS
)
3194 asn1_delete_structure (&c2
);
3195 return _gnutls_asn2err (result
);
3198 *reason_flags
= reasons
[0] | (reasons
[1] << 8);
3201 asn1_delete_structure (&c2
);
3207 * gnutls_x509_crt_get_key_purpose_oid:
3208 * @cert: should contain a #gnutls_x509_crt_t structure
3209 * @indx: This specifies which OID to return. Use (0) to get the first one.
3210 * @oid: a pointer to a buffer to hold the OID (may be null)
3211 * @oid_size: initially holds the size of @oid
3212 * @critical: output flag to indicate criticality of extension
3214 * This function will extract the key purpose OIDs of the Certificate
3215 * specified by the given index. These are stored in the Extended Key
3216 * Usage extension (2.5.29.37) See the GNUTLS_KP_* definitions for
3217 * human readable names.
3219 * If @oid is null then only the size will be filled. The @oid
3220 * returned will be null terminated, although @oid_size will not
3221 * account for the trailing null.
3223 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is
3224 * not long enough, and in that case the *oid_size will be updated
3225 * with the required size. On success 0 is returned.
3228 gnutls_x509_crt_get_key_purpose_oid (gnutls_x509_crt_t cert
,
3229 int indx
, void *oid
, size_t * oid_size
,
3230 unsigned int *critical
)
3232 char tmpstr
[ASN1_MAX_NAME_SIZE
];
3235 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
3240 return GNUTLS_E_INVALID_REQUEST
;
3244 memset (oid
, 0, *oid_size
);
3249 _gnutls_x509_crt_get_extension (cert
, "2.5.29.37", 0, &id
,
3255 if (id
.size
== 0 || id
.data
== NULL
)
3258 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
3261 result
= asn1_create_element
3262 (_gnutls_get_pkix (), "PKIX1.ExtKeyUsageSyntax", &c2
);
3263 if (result
!= ASN1_SUCCESS
)
3266 _gnutls_free_datum (&id
);
3267 return _gnutls_asn2err (result
);
3270 result
= asn1_der_decoding (&c2
, id
.data
, id
.size
, NULL
);
3271 _gnutls_free_datum (&id
);
3273 if (result
!= ASN1_SUCCESS
)
3276 asn1_delete_structure (&c2
);
3277 return _gnutls_asn2err (result
);
3281 /* create a string like "?1"
3283 snprintf (tmpstr
, sizeof (tmpstr
), "?%u", indx
);
3286 result
= asn1_read_value (c2
, tmpstr
, oid
, &len
);
3289 asn1_delete_structure (&c2
);
3291 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
3293 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
3296 if (result
!= ASN1_SUCCESS
)
3299 return _gnutls_asn2err (result
);
3307 * gnutls_x509_crt_get_pk_rsa_raw:
3308 * @crt: Holds the certificate
3309 * @m: will hold the modulus
3310 * @e: will hold the public exponent
3312 * This function will export the RSA public key's parameters found in
3313 * the given structure. The new parameters will be allocated using
3314 * gnutls_malloc() and will be stored in the appropriate datum.
3316 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
3319 gnutls_x509_crt_get_pk_rsa_raw (gnutls_x509_crt_t crt
,
3320 gnutls_datum_t
* m
, gnutls_datum_t
* e
)
3323 gnutls_pk_params_st params
;
3328 return GNUTLS_E_INVALID_REQUEST
;
3331 ret
= gnutls_x509_crt_get_pk_algorithm (crt
, NULL
);
3332 if (ret
!= GNUTLS_PK_RSA
)
3335 return GNUTLS_E_INVALID_REQUEST
;
3338 ret
= _gnutls_x509_crt_get_mpis (crt
, ¶ms
);
3345 ret
= _gnutls_mpi_dprint_lz (params
.params
[0], m
);
3352 ret
= _gnutls_mpi_dprint_lz (params
.params
[1], e
);
3356 _gnutls_free_datum (m
);
3363 gnutls_pk_params_release(¶ms
);
3368 * gnutls_x509_crt_get_pk_dsa_raw:
3369 * @crt: Holds the certificate
3370 * @p: will hold the p
3371 * @q: will hold the q
3372 * @g: will hold the g
3373 * @y: will hold the y
3375 * This function will export the DSA public key's parameters found in
3376 * the given certificate. The new parameters will be allocated using
3377 * gnutls_malloc() and will be stored in the appropriate datum.
3379 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
3382 gnutls_x509_crt_get_pk_dsa_raw (gnutls_x509_crt_t crt
,
3383 gnutls_datum_t
* p
, gnutls_datum_t
* q
,
3384 gnutls_datum_t
* g
, gnutls_datum_t
* y
)
3387 gnutls_pk_params_st params
;
3392 return GNUTLS_E_INVALID_REQUEST
;
3395 ret
= gnutls_x509_crt_get_pk_algorithm (crt
, NULL
);
3396 if (ret
!= GNUTLS_PK_DSA
)
3399 return GNUTLS_E_INVALID_REQUEST
;
3402 ret
= _gnutls_x509_crt_get_mpis (crt
, ¶ms
);
3411 ret
= _gnutls_mpi_dprint_lz (params
.params
[0], p
);
3419 ret
= _gnutls_mpi_dprint_lz (params
.params
[1], q
);
3423 _gnutls_free_datum (p
);
3429 ret
= _gnutls_mpi_dprint_lz (params
.params
[2], g
);
3433 _gnutls_free_datum (p
);
3434 _gnutls_free_datum (q
);
3440 ret
= _gnutls_mpi_dprint_lz (params
.params
[3], y
);
3444 _gnutls_free_datum (p
);
3445 _gnutls_free_datum (g
);
3446 _gnutls_free_datum (q
);
3453 gnutls_pk_params_release(¶ms
);
3459 * gnutls_x509_crt_list_import2:
3460 * @certs: The structures to store the parsed certificate. Must not be initialized.
3461 * @size: It will contain the size of the list.
3462 * @data: The PEM encoded certificate.
3463 * @format: One of DER or PEM.
3464 * @flags: must be (0) or an OR'd sequence of gnutls_certificate_import_flags.
3466 * This function will convert the given PEM encoded certificate list
3467 * to the native gnutls_x509_crt_t format. The output will be stored
3468 * in @certs which will be initialized.
3470 * If the Certificate is PEM encoded it should have a header of "X509
3471 * CERTIFICATE", or "CERTIFICATE".
3473 * Returns: the number of certificates read or a negative error value.
3478 gnutls_x509_crt_list_import2 (gnutls_x509_crt_t
** certs
,
3479 unsigned int * size
,
3480 const gnutls_datum_t
* data
,
3481 gnutls_x509_crt_fmt_t format
, unsigned int flags
)
3483 unsigned int init
= 1024;
3486 *certs
= gnutls_malloc(sizeof(gnutls_x509_crt_t
)*init
);
3490 return GNUTLS_E_MEMORY_ERROR
;
3493 ret
= gnutls_x509_crt_list_import(*certs
, &init
, data
, format
, GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED
);
3494 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
)
3496 *certs
= gnutls_realloc_fast(*certs
, sizeof(gnutls_x509_crt_t
)*init
);
3500 return GNUTLS_E_MEMORY_ERROR
;
3503 ret
= gnutls_x509_crt_list_import(*certs
, &init
, data
, format
, flags
);
3508 gnutls_free(*certs
);
3517 static int check_if_sorted(gnutls_x509_crt_t
* crt
, int nr
)
3519 char prev_dn
[MAX_DN
];
3521 size_t prev_dn_size
, dn_size
;
3524 /* check if the X.509 list is ordered */
3532 dn_size
= sizeof(dn
);
3533 ret
= gnutls_x509_crt_get_dn(crt
[i
], dn
, &dn_size
);
3536 ret
= gnutls_assert_val(ret
);
3540 if (dn_size
!= prev_dn_size
|| memcmp(dn
, prev_dn
, dn_size
) != 0)
3542 ret
= gnutls_assert_val(GNUTLS_E_CERTIFICATE_LIST_UNSORTED
);
3547 prev_dn_size
= sizeof(prev_dn
);
3548 ret
= gnutls_x509_crt_get_issuer_dn(crt
[i
], prev_dn
, &prev_dn_size
);
3551 ret
= gnutls_assert_val(ret
);
3565 * gnutls_x509_crt_list_import:
3566 * @certs: The structures to store the parsed certificate. Must not be initialized.
3567 * @cert_max: Initially must hold the maximum number of certs. It will be updated with the number of certs available.
3568 * @data: The PEM encoded certificate.
3569 * @format: One of DER or PEM.
3570 * @flags: must be (0) or an OR'd sequence of gnutls_certificate_import_flags.
3572 * This function will convert the given PEM encoded certificate list
3573 * to the native gnutls_x509_crt_t format. The output will be stored
3574 * in @certs. They will be automatically initialized.
3576 * The flag %GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED will cause
3577 * import to fail if the certificates in the provided buffer are more
3578 * than the available structures. The %GNUTLS_X509_CRT_LIST_FAIL_IF_UNSORTED
3579 * flag will cause the function to fail if the provided list is not
3580 * sorted from subject to issuer.
3582 * If the Certificate is PEM encoded it should have a header of "X509
3583 * CERTIFICATE", or "CERTIFICATE".
3585 * Returns: the number of certificates read or a negative error value.
3588 gnutls_x509_crt_list_import (gnutls_x509_crt_t
* certs
,
3589 unsigned int *cert_max
,
3590 const gnutls_datum_t
* data
,
3591 gnutls_x509_crt_fmt_t format
, unsigned int flags
)
3596 int ret
, nocopy
= 0;
3597 unsigned int count
= 0, j
;
3599 if (format
== GNUTLS_X509_FMT_DER
)
3604 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
3607 count
= 1; /* import only the first one */
3609 ret
= gnutls_x509_crt_init (&certs
[0]);
3616 ret
= gnutls_x509_crt_import (certs
[0], data
, format
);
3627 /* move to the certificate
3629 ptr
= memmem (data
->data
, data
->size
,
3630 PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
3632 ptr
= memmem (data
->data
, data
->size
,
3633 PEM_CERT_SEP2
, sizeof (PEM_CERT_SEP2
) - 1);
3636 return gnutls_assert_val(GNUTLS_E_NO_CERTIFICATE_FOUND
);
3642 if (count
>= *cert_max
)
3644 if (!(flags
& GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED
))
3652 ret
= gnutls_x509_crt_init (&certs
[count
]);
3659 tmp
.data
= (void *) ptr
;
3660 tmp
.size
= data
->size
- (ptr
- (char *) data
->data
);
3663 gnutls_x509_crt_import (certs
[count
], &tmp
, GNUTLS_X509_FMT_PEM
);
3671 /* now we move ptr after the pem header
3674 /* find the next certificate (if any)
3676 size
= data
->size
- (ptr
- (char *) data
->data
);
3682 ptr2
= memmem (ptr
, size
, PEM_CERT_SEP
, sizeof (PEM_CERT_SEP
) - 1);
3684 ptr2
= memmem (ptr
, size
, PEM_CERT_SEP2
,
3685 sizeof (PEM_CERT_SEP2
) - 1);
3694 while (ptr
!= NULL
);
3698 if (flags
& GNUTLS_X509_CRT_LIST_FAIL_IF_UNSORTED
)
3700 ret
= check_if_sorted(certs
, *cert_max
);
3711 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
3714 for (j
= 0; j
< count
; j
++)
3715 gnutls_x509_crt_deinit (certs
[j
]);
3720 * gnutls_x509_crt_get_subject_unique_id:
3721 * @crt: Holds the certificate
3722 * @buf: user allocated memory buffer, will hold the unique id
3723 * @buf_size: size of user allocated memory buffer (on input), will hold
3724 * actual size of the unique ID on return.
3726 * This function will extract the subjectUniqueID value (if present) for
3727 * the given certificate.
3729 * If the user allocated memory buffer is not large enough to hold the
3730 * full subjectUniqueID, then a GNUTLS_E_SHORT_MEMORY_BUFFER error will be
3731 * returned, and buf_size will be set to the actual length.
3733 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
3736 gnutls_x509_crt_get_subject_unique_id (gnutls_x509_crt_t crt
, char *buf
,
3740 gnutls_datum_t datum
= { NULL
, 0 };
3743 _gnutls_x509_read_value (crt
->cert
, "tbsCertificate.subjectUniqueID",
3746 if (datum
.size
> *buf_size
)
3747 { /* then we're not going to fit */
3748 *buf_size
= datum
.size
;
3750 result
= GNUTLS_E_SHORT_MEMORY_BUFFER
;
3754 *buf_size
= datum
.size
;
3755 memcpy (buf
, datum
.data
, datum
.size
);
3758 _gnutls_free_datum (&datum
);
3764 * gnutls_x509_crt_get_issuer_unique_id:
3765 * @crt: Holds the certificate
3766 * @buf: user allocated memory buffer, will hold the unique id
3767 * @buf_size: size of user allocated memory buffer (on input), will hold
3768 * actual size of the unique ID on return.
3770 * This function will extract the issuerUniqueID value (if present) for
3771 * the given certificate.
3773 * If the user allocated memory buffer is not large enough to hold the
3774 * full subjectUniqueID, then a GNUTLS_E_SHORT_MEMORY_BUFFER error will be
3775 * returned, and buf_size will be set to the actual length.
3777 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
3782 gnutls_x509_crt_get_issuer_unique_id (gnutls_x509_crt_t crt
, char *buf
,
3786 gnutls_datum_t datum
= { NULL
, 0 };
3789 _gnutls_x509_read_value (crt
->cert
, "tbsCertificate.issuerUniqueID",
3792 if (datum
.size
> *buf_size
)
3793 { /* then we're not going to fit */
3794 *buf_size
= datum
.size
;
3796 result
= GNUTLS_E_SHORT_MEMORY_BUFFER
;
3800 *buf_size
= datum
.size
;
3801 memcpy (buf
, datum
.data
, datum
.size
);
3804 _gnutls_free_datum (&datum
);
3810 _gnutls_parse_aia (ASN1_TYPE src
,
3813 gnutls_datum_t
* data
)
3816 char nptr
[ASN1_MAX_NAME_SIZE
];
3819 const char *oid
= NULL
;
3821 seq
++; /* 0->1, 1->2 etc */
3824 case GNUTLS_IA_ACCESSMETHOD_OID
:
3825 snprintf (nptr
, sizeof (nptr
), "?%u.accessMethod", seq
);
3828 case GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE
:
3829 snprintf (nptr
, sizeof (nptr
), "?%u.accessLocation", seq
);
3832 case GNUTLS_IA_CAISSUERS_URI
:
3833 oid
= GNUTLS_OID_AD_CAISSUERS
;
3836 case GNUTLS_IA_OCSP_URI
:
3838 oid
= GNUTLS_OID_AD_OCSP
;
3841 snprintf (nptr
, sizeof (nptr
), "?%u.accessMethod", seq
);
3842 len
= sizeof (tmpoid
);
3843 result
= asn1_read_value (src
, nptr
, tmpoid
, &len
);
3845 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
3846 return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
);
3848 if (result
!= ASN1_SUCCESS
)
3851 return _gnutls_asn2err (result
);
3853 if ((unsigned)len
!= strlen (oid
) + 1 || memcmp (tmpoid
, oid
, len
) != 0)
3854 return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM
);
3859 snprintf (nptr
, sizeof (nptr
),
3860 "?%u.accessLocation.uniformResourceIdentifier", seq
);
3864 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
3868 result
= asn1_read_value (src
, nptr
, NULL
, &len
);
3869 if (result
== ASN1_VALUE_NOT_FOUND
|| result
== ASN1_ELEMENT_NOT_FOUND
)
3870 return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
);
3872 if (result
!= ASN1_MEM_ERROR
)
3875 return _gnutls_asn2err (result
);
3880 d
.data
= gnutls_malloc (d
.size
);
3882 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
3884 result
= asn1_read_value (src
, nptr
, d
.data
, &len
);
3885 if (result
!= ASN1_SUCCESS
)
3888 gnutls_free (d
.data
);
3889 return _gnutls_asn2err (result
);
3894 data
->data
= d
.data
;
3895 data
->size
= d
.size
;
3898 gnutls_free (d
.data
);
3904 * gnutls_x509_crt_get_authority_info_access:
3905 * @crt: Holds the certificate
3906 * @seq: specifies the sequence number of the access descriptor (0 for the first one, 1 for the second etc.)
3907 * @what: what data to get, a #gnutls_info_access_what_t type.
3908 * @data: output data to be freed with gnutls_free().
3909 * @critical: pointer to output integer that is set to non-0 if the extension is marked as critical (may be %NULL)
3911 * This function extracts the Authority Information Access (AIA)
3912 * extension, see RFC 5280 section 4.2.2.1 for more information. The
3913 * AIA extension holds a sequence of AccessDescription (AD) data:
3915 * <informalexample><programlisting>
3916 * AuthorityInfoAccessSyntax ::=
3917 * SEQUENCE SIZE (1..MAX) OF AccessDescription
3919 * AccessDescription ::= SEQUENCE {
3920 * accessMethod OBJECT IDENTIFIER,
3921 * accessLocation GeneralName }
3922 * </programlisting></informalexample>
3924 * The @seq input parameter is used to indicate which member of the
3925 * sequence the caller is interested in. The first member is 0, the
3926 * second member 1 and so on. When the @seq value is out of bounds,
3927 * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned.
3929 * The type of data returned in @data is specified via @what which
3930 * should be #gnutls_info_access_what_t values.
3932 * If @what is %GNUTLS_IA_ACCESSMETHOD_OID then @data will hold the
3933 * accessMethod OID (e.g., "1.3.6.1.5.5.7.48.1").
3935 * If @what is %GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE, @data will
3936 * hold the accessLocation GeneralName type (e.g.,
3937 * "uniformResourceIdentifier").
3939 * If @what is %GNUTLS_IA_URI, @data will hold the accessLocation URI
3940 * data. Requesting this @what value leads to an error if the
3941 * accessLocation is not of the "uniformResourceIdentifier" type.
3943 * If @what is %GNUTLS_IA_OCSP_URI, @data will hold the OCSP URI.
3944 * Requesting this @what value leads to an error if the accessMethod
3945 * is not 1.3.6.1.5.5.7.48.1 aka OSCP, or if accessLocation is not of
3946 * the "uniformResourceIdentifier" type.
3948 * If @what is %GNUTLS_IA_CAISSUERS_URI, @data will hold the caIssuers
3949 * URI. Requesting this @what value leads to an error if the
3950 * accessMethod is not 1.3.6.1.5.5.7.48.2 aka caIssuers, or if
3951 * accessLocation is not of the "uniformResourceIdentifier" type.
3953 * More @what values may be allocated in the future as needed.
3955 * If @data is NULL, the function does the same without storing the
3956 * output data, that is, it will set @critical and do error checking
3959 * The value of the critical flag is returned in *@critical. Supply a
3960 * NULL @critical if you want the function to make sure the extension
3961 * is non-critical, as required by RFC 5280.
3963 * Returns: %GNUTLS_E_SUCCESS on success, %GNUTLS_E_INVALID_REQUEST on
3964 * invalid @crt, %GNUTLS_E_CONSTRAINT_ERROR if the extension is
3965 * incorrectly marked as critical (use a non-NULL @critical to
3966 * override), %GNUTLS_E_UNKNOWN_ALGORITHM if the requested OID does
3967 * not match (e.g., when using %GNUTLS_IA_OCSP_URI), otherwise a
3968 * negative error code.
3973 gnutls_x509_crt_get_authority_info_access (gnutls_x509_crt_t crt
,
3976 gnutls_datum_t
* data
,
3977 unsigned int *critical
)
3981 ASN1_TYPE c2
= ASN1_TYPE_EMPTY
;
3986 return GNUTLS_E_INVALID_REQUEST
;
3989 if ((ret
= _gnutls_x509_crt_get_extension (crt
, GNUTLS_OID_AIA
, 0, &aia
,
3993 if (aia
.size
== 0 || aia
.data
== NULL
)
3996 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
;
3999 if (critical
&& *critical
)
4000 return GNUTLS_E_CONSTRAINT_ERROR
;
4002 ret
= asn1_create_element (_gnutls_get_pkix (),
4003 "PKIX1.AuthorityInfoAccessSyntax", &c2
);
4004 if (ret
!= ASN1_SUCCESS
)
4007 _gnutls_free_datum (&aia
);
4008 return _gnutls_asn2err (ret
);
4011 ret
= asn1_der_decoding (&c2
, aia
.data
, aia
.size
, NULL
);
4012 /* asn1_print_structure (stdout, c2, "", ASN1_PRINT_ALL); */
4013 _gnutls_free_datum (&aia
);
4014 if (ret
!= ASN1_SUCCESS
)
4017 asn1_delete_structure (&c2
);
4018 return _gnutls_asn2err (ret
);
4021 ret
= _gnutls_parse_aia (c2
, seq
, what
, data
);
4023 asn1_delete_structure (&c2
);
4031 * gnutls_x509_crt_set_pin_function:
4032 * @crt: The certificate structure
4034 * @userdata: data associated with the callback
4036 * This function will set a callback function to be used when
4037 * it is required to access a protected object. This function overrides
4038 * the global function set using gnutls_pkcs11_set_pin_function().
4040 * Note that this callback is currently used only during the import
4041 * of a PKCS #11 certificate with gnutls_x509_crt_import_pkcs11_url().
4046 void gnutls_x509_crt_set_pin_function (gnutls_x509_crt_t crt
,
4047 gnutls_pin_callback_t fn
, void *userdata
)
4050 crt
->pin
.data
= userdata
;