corrected copyright notices
[gnutls.git] / lib / x509 / x509.c
blob4ece36232787f9e23bb6f06a1563b150dd8dfad2
1 /*
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>
29 #include <common.h>
30 #include <gnutls_x509.h>
31 #include <x509_b64.h>
32 #include <x509_int.h>
33 #include <libtasn1.h>
34 #include <gnutls_pk.h>
36 /**
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.
44 **/
45 int
46 gnutls_x509_crt_init (gnutls_x509_crt_t * cert)
48 gnutls_x509_crt_t tmp = gnutls_calloc (1, sizeof (gnutls_x509_crt_int));
49 int result;
51 if (!tmp)
52 return GNUTLS_E_MEMORY_ERROR;
54 result = asn1_create_element (_gnutls_get_pkix (),
55 "PKIX1.Certificate", &tmp->cert);
56 if (result != ASN1_SUCCESS)
58 gnutls_assert ();
59 gnutls_free (tmp);
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. */
66 *cert = tmp;
68 return 0; /* success */
71 /*-
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.
80 -*/
81 int
82 _gnutls_x509_crt_cpy (gnutls_x509_crt_t dest, gnutls_x509_crt_t src)
84 int ret;
85 size_t der_size=0;
86 uint8_t *der;
87 gnutls_datum_t tmp;
89 ret = gnutls_x509_crt_export (src, GNUTLS_X509_FMT_DER, NULL, &der_size);
90 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
92 gnutls_assert ();
93 return ret;
96 der = gnutls_malloc (der_size);
97 if (der == NULL)
99 gnutls_assert ();
100 return GNUTLS_E_MEMORY_ERROR;
103 ret = gnutls_x509_crt_export (src, GNUTLS_X509_FMT_DER, der, &der_size);
104 if (ret < 0)
106 gnutls_assert ();
107 gnutls_free (der);
108 return ret;
111 tmp.data = der;
112 tmp.size = der_size;
113 ret = gnutls_x509_crt_import (dest, &tmp, GNUTLS_X509_FMT_DER);
115 gnutls_free (der);
117 if (ret < 0)
119 gnutls_assert ();
120 return ret;
123 return 0;
127 * gnutls_x509_crt_deinit:
128 * @cert: The structure to be deinitialized
130 * This function will deinitialize a certificate structure.
132 void
133 gnutls_x509_crt_deinit (gnutls_x509_crt_t cert)
135 if (!cert)
136 return;
138 if (cert->cert)
139 asn1_delete_structure (&cert->cert);
141 gnutls_free (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
152 * in @cert.
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;
168 if (cert == NULL)
170 gnutls_assert ();
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 */
182 result =
183 _gnutls_fbase64_decode (PEM_X509_CERT2, data->data, data->size, &_data);
185 if (result <= 0)
187 /* try for the second header */
188 result =
189 _gnutls_fbase64_decode (PEM_X509_CERT, data->data,
190 data->size, &_data);
192 if (result < 0)
194 gnutls_assert ();
195 return result;
199 need_free = 1;
202 if (cert->expanded)
204 /* Any earlier asn1_der_decoding will modify the ASN.1
205 structure, so we need to replace it with a fresh
206 structure. */
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);
214 gnutls_assert ();
215 goto cleanup;
219 result = asn1_der_decoding (&cert->cert, _data.data, _data.size, NULL);
220 if (result != ASN1_SUCCESS)
222 result = _gnutls_asn2err (result);
223 gnutls_assert ();
224 goto cleanup;
227 cert->expanded = 1;
229 /* Since we do not want to disable any extension
231 cert->use_extensions = 1;
232 if (need_free)
233 _gnutls_free_datum (&_data);
235 return 0;
237 cleanup:
238 if (need_free)
239 _gnutls_free_datum (&_data);
240 return result;
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,
263 size_t * buf_size)
265 if (cert == NULL)
267 gnutls_assert ();
268 return GNUTLS_E_INVALID_REQUEST;
271 return _gnutls_x509_parse_dn (cert->cert,
272 "tbsCertificate.issuer.rdnSequence", buf,
273 buf_size);
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,
309 size_t * buf_size)
311 gnutls_datum_t td;
312 int ret;
314 if (cert == NULL)
316 gnutls_assert ();
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);
323 if (ret < 0)
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)
352 if (cert == NULL)
354 gnutls_assert ();
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,
382 size_t * buf_size)
384 if (cert == NULL)
386 gnutls_assert ();
387 return GNUTLS_E_INVALID_REQUEST;
390 return _gnutls_x509_parse_dn (cert->cert,
391 "tbsCertificate.subject.rdnSequence", buf,
392 buf_size);
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)
429 gnutls_datum_t td;
430 int ret;
432 if (cert == NULL)
434 gnutls_assert ();
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);
441 if (ret < 0)
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)
470 if (cert == NULL)
472 gnutls_assert ();
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
490 * error.
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)
513 int result;
514 unsigned int bits;
515 int len;
517 if (cert == NULL)
519 gnutls_assert ();
520 return GNUTLS_E_INVALID_REQUEST;
523 len = 0;
524 result = asn1_read_value (cert->cert, "signature", NULL, &len);
525 if (result != ASN1_MEM_ERROR)
527 gnutls_assert ();
528 return _gnutls_asn2err (result);
531 bits = len;
532 if (bits % 8 != 0)
534 gnutls_assert ();
535 return GNUTLS_E_CERTIFICATE_ERROR;
538 len = bits / 8;
540 if (*sizeof_sig < (unsigned int) len)
542 *sizeof_sig = len;
543 return GNUTLS_E_SHORT_MEMORY_BUFFER;
546 result = asn1_read_value (cert->cert, "signature", sig, &len);
547 if (result != ASN1_SUCCESS)
549 gnutls_assert ();
550 return _gnutls_asn2err (result);
553 return 0;
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)
567 uint8_t version[8];
568 int len, result;
570 if (cert == NULL)
572 gnutls_assert ();
573 return GNUTLS_E_INVALID_REQUEST;
576 len = sizeof (version);
577 if ((result =
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 */
584 gnutls_assert ();
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
596 * activated.
598 * Returns: activation time, or (time_t)-1 on error.
600 time_t
601 gnutls_x509_crt_get_activation_time (gnutls_x509_crt_t cert)
603 if (cert == NULL)
605 gnutls_assert ();
606 return (time_t) - 1;
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
618 * expired.
620 * Returns: expiration time, or (time_t)-1 on error.
622 time_t
623 gnutls_x509_crt_get_expiration_time (gnutls_x509_crt_t cert)
625 if (cert == NULL)
627 gnutls_assert ();
628 return (time_t) - 1;
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)
653 int result, ret;
654 gnutls_datum_t der = {NULL, 0};
655 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
657 if (cert == NULL)
659 gnutls_assert ();
660 return GNUTLS_E_INVALID_REQUEST;
663 ret =
664 _gnutls_x509_crt_get_extension (cert, "2.5.29.16", 0, &der,
665 critical);
666 if (ret < 0)
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)
676 gnutls_assert ();
677 ret = _gnutls_asn2err (result);
678 goto cleanup;
681 result = asn1_der_decoding (&c2, der.data, der.size, NULL);
682 if (result != ASN1_SUCCESS)
684 gnutls_assert ();
685 ret = _gnutls_asn2err (result);
686 goto cleanup;
689 if (activation)
690 *activation = _gnutls_x509_get_time (c2,
691 "notBefore", 1);
693 if (expiration)
694 *expiration = _gnutls_x509_get_time (c2,
695 "notAfter", 1);
697 ret = 0;
699 cleanup:
700 _gnutls_free_datum(&der);
701 asn1_delete_structure (&c2);
703 return ret;
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)
725 int ret, len;
727 if (cert == NULL)
729 gnutls_assert ();
730 return GNUTLS_E_INVALID_REQUEST;
733 len = *result_size;
734 ret =
735 asn1_read_value (cert->cert, "tbsCertificate.serialNumber", result, &len);
736 *result_size = len;
738 if (ret != ASN1_SUCCESS)
740 gnutls_assert ();
741 return _gnutls_asn2err (ret);
744 return 0;
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)
765 int result, len;
766 gnutls_datum_t id;
767 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
769 if (cert == NULL)
771 gnutls_assert ();
772 return GNUTLS_E_INVALID_REQUEST;
776 if (ret)
777 memset (ret, 0, *ret_size);
778 else
779 *ret_size = 0;
781 if ((result =
782 _gnutls_x509_crt_get_extension (cert, "2.5.29.14", 0, &id,
783 critical)) < 0)
785 return result;
788 if (id.size == 0 || id.data == NULL)
790 gnutls_assert ();
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)
798 gnutls_assert ();
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)
808 gnutls_assert ();
809 asn1_delete_structure (&c2);
810 return _gnutls_asn2err (result);
813 len = *ret_size;
814 result = asn1_read_value (c2, "", ret, &len);
816 *ret_size = 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)
827 gnutls_assert ();
828 return _gnutls_asn2err (result);
831 return 0;
834 static int
835 _get_authority_key_id (gnutls_x509_crt_t cert, ASN1_TYPE *c2,
836 unsigned int *critical)
838 int ret;
839 gnutls_datum_t id;
841 *c2 = ASN1_TYPE_EMPTY;
843 if (cert == NULL)
845 gnutls_assert ();
846 return GNUTLS_E_INVALID_REQUEST;
849 if ((ret =
850 _gnutls_x509_crt_get_extension (cert, "2.5.29.35", 0, &id,
851 critical)) < 0)
853 return gnutls_assert_val(ret);
856 if (id.size == 0 || id.data == NULL)
858 gnutls_assert ();
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)
866 gnutls_assert ();
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)
876 gnutls_assert ();
877 asn1_delete_structure (c2);
878 return _gnutls_asn2err (ret);
881 return 0;
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)
897 * and serial number.
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.
906 * Since: 3.0
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;
915 ASN1_TYPE c2;
917 ret = _get_authority_key_id(cert, &c2, critical);
918 if (ret < 0)
919 return gnutls_assert_val(ret);
921 ret =
922 _gnutls_parse_general_name (c2, "authorityCertIssuer", seq, alt, alt_size, alt_type,
924 if (ret < 0)
926 ret = gnutls_assert_val(ret);
927 goto fail;
930 if (serial)
932 len = *serial_size;
933 result = asn1_read_value (c2, "authorityCertSerialNumber", serial, &len);
935 *serial_size = len;
937 if (result < 0)
939 ret = _gnutls_asn2err(result);
940 goto fail;
945 ret = 0;
947 fail:
948 asn1_delete_structure (&c2);
950 return ret;
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,
973 size_t * id_size,
974 unsigned int *critical)
976 int ret, result, len;
977 ASN1_TYPE c2;
979 ret = _get_authority_key_id(cert, &c2, critical);
980 if (ret < 0)
981 return gnutls_assert_val(ret);
983 len = *id_size;
984 result = asn1_read_value (c2, "keyIdentifier", id, &len);
986 *id_size = 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)
995 gnutls_assert ();
996 return _gnutls_asn2err (result);
999 return 0;
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
1008 * certificate.
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
1013 * exponent.
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)
1021 int result;
1023 if (cert == NULL)
1025 gnutls_assert ();
1026 return GNUTLS_E_INVALID_REQUEST;
1029 if (bits)
1030 *bits = 0;
1032 result =
1033 _gnutls_x509_get_pk_algorithm (cert->cert,
1034 "tbsCertificate.subjectPublicKeyInfo",
1035 bits);
1037 if (result < 0)
1039 gnutls_assert ();
1040 return result;
1043 return result;
1047 inline static int
1048 is_type_printable (int type)
1050 if (type == GNUTLS_SAN_DNSNAME || type == GNUTLS_SAN_RFC822NAME ||
1051 type == GNUTLS_SAN_URI)
1052 return 1;
1053 else
1054 return 0;
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)
1067 int len;
1068 char nptr[ASN1_MAX_NAME_SIZE];
1069 int result;
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);
1077 else
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)
1090 gnutls_assert ();
1091 return _gnutls_asn2err (result);
1095 type = _gnutls_x509_san_find_type (choice_type);
1096 if (type == (gnutls_x509_subject_alt_name_t) - 1)
1098 gnutls_assert ();
1099 return GNUTLS_E_X509_UNKNOWN_SAN;
1102 if (ret_type)
1103 *ret_type = type;
1105 if (type == GNUTLS_SAN_OTHERNAME)
1107 if (othername_oid)
1108 _gnutls_str_cat (nptr, sizeof (nptr), ".otherName.type-id");
1109 else
1110 _gnutls_str_cat (nptr, sizeof (nptr), ".otherName.value");
1112 len = *name_size;
1113 result = asn1_read_value (src, nptr, name, &len);
1114 *name_size = len;
1116 if (result == ASN1_MEM_ERROR)
1117 return GNUTLS_E_SHORT_MEMORY_BUFFER;
1119 if (result != ASN1_SUCCESS)
1121 gnutls_assert ();
1122 return _gnutls_asn2err (result);
1125 if (othername_oid)
1127 if ((unsigned)len > strlen (XMPP_OID) && strcmp (name, XMPP_OID) == 0)
1128 type = GNUTLS_SAN_OTHERNAME_XMPP;
1130 else
1132 char oid[42];
1134 if (src_name[0] != 0)
1135 snprintf (nptr, sizeof (nptr), "%s.?%u.otherName.type-id",
1136 src_name, seq);
1137 else
1138 snprintf (nptr, sizeof (nptr), "?%u.otherName.type-id", seq);
1140 len = sizeof (oid);
1141 result = asn1_read_value (src, nptr, oid, &len);
1142 if (result != ASN1_SUCCESS)
1144 gnutls_assert ();
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)
1157 gnutls_assert ();
1158 return _gnutls_asn2err (result);
1161 result = asn1_der_decoding (&c2, name, *name_size, NULL);
1162 if (result != ASN1_SUCCESS)
1164 gnutls_assert ();
1165 asn1_delete_structure (&c2);
1166 return _gnutls_asn2err (result);
1169 len = *name_size;
1170 result = asn1_read_value (c2, "", name, &len);
1171 if (result != ASN1_SUCCESS)
1173 gnutls_assert ();
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)
1182 gnutls_assert ();
1183 *name_size = len + 1;
1184 return GNUTLS_E_SHORT_MEMORY_BUFFER;
1187 *name_size = len;
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);
1197 if (result < 0)
1199 gnutls_assert ();
1200 return result;
1203 else if (othername_oid)
1204 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1205 else
1207 size_t orig_name_size = *name_size;
1209 _gnutls_str_cat (nptr, sizeof (nptr), ".");
1210 _gnutls_str_cat (nptr, sizeof (nptr), choice_type);
1212 len = *name_size;
1213 result = asn1_read_value (src, nptr, name, &len);
1214 *name_size = len;
1216 if (result == ASN1_MEM_ERROR)
1218 if (is_type_printable (type))
1219 (*name_size)++;
1220 return GNUTLS_E_SHORT_MEMORY_BUFFER;
1223 if (result != ASN1_SUCCESS)
1225 gnutls_assert ();
1226 return _gnutls_asn2err (result);
1229 if (is_type_printable (type))
1232 if ((unsigned)len + 1 > orig_name_size)
1234 gnutls_assert ();
1235 (*name_size)++;
1236 return GNUTLS_E_SHORT_MEMORY_BUFFER;
1239 /* null terminate it */
1240 ((char *) name)[*name_size] = 0;
1245 return type;
1248 static int
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)
1254 int result;
1255 gnutls_datum_t dnsname;
1256 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
1258 if (cert == NULL)
1260 gnutls_assert ();
1261 return GNUTLS_E_INVALID_REQUEST;
1264 if (alt)
1265 memset (alt, 0, *alt_size);
1266 else
1267 *alt_size = 0;
1269 if ((result =
1270 _gnutls_x509_crt_get_extension (cert, extension_id, 0, &dnsname,
1271 critical)) < 0)
1273 return result;
1276 if (dnsname.size == 0 || dnsname.data == NULL)
1278 gnutls_assert ();
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);
1288 else
1290 gnutls_assert ();
1291 return GNUTLS_E_INTERNAL_ERROR;
1294 if (result != ASN1_SUCCESS)
1296 gnutls_assert ();
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)
1306 gnutls_assert ();
1307 asn1_delete_structure (&c2);
1308 return _gnutls_asn2err (result);
1311 result =
1312 _gnutls_parse_general_name (c2, "", seq, alt, alt_size, alt_type,
1313 othername_oid);
1315 asn1_delete_structure (&c2);
1317 if (result < 0)
1319 gnutls_assert ();
1320 return result;
1323 return result;
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
1346 * recognized.
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,
1359 size_t * san_size,
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
1376 * Extensions.
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.
1397 * Since: 2.10.0
1400 gnutls_x509_crt_get_issuer_alt_name (gnutls_x509_crt_t cert,
1401 unsigned int seq, void *ian,
1402 size_t * ian_size,
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
1423 * not enough).
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,
1436 size_t * san_size,
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,
1441 critical, 0);
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
1458 * not enough).
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.
1468 * Since: 2.10.0
1472 gnutls_x509_crt_get_issuer_alt_name2 (gnutls_x509_crt_t cert,
1473 unsigned int seq, void *ian,
1474 size_t * ian_size,
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,
1479 critical, 0);
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,
1514 unsigned int seq,
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.
1550 * Since: 2.10.0
1553 gnutls_x509_crt_get_issuer_alt_othername_oid (gnutls_x509_crt_t cert,
1554 unsigned int seq,
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)
1585 int result;
1586 gnutls_datum_t basicConstraints;
1587 unsigned int tmp_ca;
1589 if (cert == NULL)
1591 gnutls_assert ();
1592 return GNUTLS_E_INVALID_REQUEST;
1595 if ((result =
1596 _gnutls_x509_crt_get_extension (cert, "2.5.29.19", 0,
1597 &basicConstraints, critical)) < 0)
1599 return result;
1602 if (basicConstraints.size == 0 || basicConstraints.data == NULL)
1604 gnutls_assert ();
1605 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1608 result =
1609 _gnutls_x509_ext_extract_basicConstraints (&tmp_ca,
1610 pathlen,
1611 basicConstraints.data,
1612 basicConstraints.size);
1613 if (ca)
1614 *ca = tmp_ca;
1615 _gnutls_free_datum (&basicConstraints);
1617 if (result < 0)
1619 gnutls_assert ();
1620 return result;
1623 return tmp_ca;
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)
1646 int pathlen;
1647 unsigned int ca;
1648 return gnutls_x509_crt_get_basic_constraints (cert, critical, &ca,
1649 &pathlen);
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
1669 * returned.
1672 gnutls_x509_crt_get_key_usage (gnutls_x509_crt_t cert,
1673 unsigned int *key_usage,
1674 unsigned int *critical)
1676 int result;
1677 gnutls_datum_t keyUsage;
1678 uint16_t _usage;
1680 if (cert == NULL)
1682 gnutls_assert ();
1683 return GNUTLS_E_INVALID_REQUEST;
1686 if ((result =
1687 _gnutls_x509_crt_get_extension (cert, "2.5.29.15", 0, &keyUsage,
1688 critical)) < 0)
1690 return result;
1693 if (keyUsage.size == 0 || keyUsage.data == NULL)
1695 gnutls_assert ();
1696 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1699 result = _gnutls_x509_ext_extract_keyUsage (&_usage, keyUsage.data,
1700 keyUsage.size);
1701 _gnutls_free_datum (&keyUsage);
1703 *key_usage = _usage;
1705 if (result < 0)
1707 gnutls_assert ();
1708 return result;
1711 return 0;
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,
1734 int *pathlen,
1735 char **policyLanguage,
1736 char **policy, size_t * sizeof_policy)
1738 int result;
1739 gnutls_datum_t proxyCertInfo;
1741 if (cert == NULL)
1743 gnutls_assert ();
1744 return GNUTLS_E_INVALID_REQUEST;
1747 if ((result =
1748 _gnutls_x509_crt_get_extension (cert, "1.3.6.1.5.5.7.1.14", 0,
1749 &proxyCertInfo, critical)) < 0)
1751 return result;
1754 if (proxyCertInfo.size == 0 || proxyCertInfo.data == NULL)
1756 gnutls_assert ();
1757 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1760 result = _gnutls_x509_ext_extract_proxyCertInfo (pathlen,
1761 policyLanguage,
1762 policy,
1763 sizeof_policy,
1764 proxyCertInfo.data,
1765 proxyCertInfo.size);
1766 _gnutls_free_datum (&proxyCertInfo);
1767 if (result < 0)
1769 gnutls_assert ();
1770 return result;
1773 return 0;
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().
1783 * Since: 3.1.5
1785 void gnutls_x509_policy_release(struct gnutls_x509_policy_st* policy)
1787 unsigned i;
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;
1797 int ret, len;
1798 char choice_type[64];
1799 char name[128];
1800 gnutls_datum_t td, utd;
1802 ret = asn1_create_element
1803 (_gnutls_get_pkix (), "PKIX1.UserNotice", &c2);
1804 if (ret != ASN1_SUCCESS)
1806 gnutls_assert ();
1807 ret = GNUTLS_E_PARSING_ERROR;
1808 goto cleanup;
1811 ret = asn1_der_decoding (&c2, data, size, NULL);
1812 if (ret != ASN1_SUCCESS)
1814 gnutls_assert ();
1815 ret = GNUTLS_E_PARSING_ERROR;
1816 goto cleanup;
1819 len = sizeof(choice_type);
1820 ret = asn1_read_value(c2, "explicitText", choice_type, &len);
1821 if (ret != ASN1_SUCCESS)
1823 gnutls_assert ();
1824 ret = GNUTLS_E_PARSING_ERROR;
1825 goto cleanup;
1828 if (strcmp(choice_type, "utf8String") != 0 && strcmp(choice_type, "IA5String") != 0 &&
1829 strcmp(choice_type, "bmpString") != 0 && strcmp(choice_type, "visibleString") != 0)
1831 gnutls_assert();
1832 ret = GNUTLS_E_PARSING_ERROR;
1833 goto cleanup;
1836 snprintf (name, sizeof (name), "explicitText.%s", choice_type);
1838 ret = _gnutls_x509_read_value(c2, name, &td);
1839 if (ret < 0)
1841 gnutls_assert ();
1842 goto cleanup;
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);
1849 if (ret < 0)
1851 gnutls_assert();
1852 goto cleanup;
1855 td.data = utd.data;
1856 td.size = utd.size;
1858 else
1860 /* _gnutls_x509_read_value allows that */
1861 td.data[td.size] = 0;
1864 txt->data = (void*)td.data;
1865 txt->size = td.size;
1866 ret = 0;
1868 cleanup:
1869 asn1_delete_structure (&c2);
1870 return ret;
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.
1890 * Since: 3.1.5
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;
1898 char tmpstr[128];
1899 char tmpoid[MAX_OID_SIZE];
1900 gnutls_datum_t tmpd = {NULL, 0};
1901 int ret, len;
1902 unsigned i;
1904 if (crt == NULL)
1906 gnutls_assert ();
1907 return GNUTLS_E_INVALID_REQUEST;
1910 memset(policy, 0, sizeof(*policy));
1912 if ((ret =
1913 _gnutls_x509_crt_get_extension (crt, "2.5.29.32", 0, &tmpd,
1914 critical)) < 0)
1916 return ret;
1919 if (tmpd.size == 0 || tmpd .data == NULL)
1921 gnutls_assert ();
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)
1929 gnutls_assert ();
1930 ret = _gnutls_asn2err (ret);
1931 goto cleanup;
1934 ret = asn1_der_decoding (&c2, tmpd.data, tmpd.size, NULL);
1935 if (ret != ASN1_SUCCESS)
1937 gnutls_assert ();
1938 ret = _gnutls_asn2err (ret);
1939 goto cleanup;
1941 _gnutls_free_datum (&tmpd);
1943 indx++;
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;
1953 if (ret < 0)
1955 gnutls_assert();
1956 goto cleanup;
1958 policy->oid = (void*)tmpd.data;
1959 tmpd.data = NULL;
1961 for (i=0;i<GNUTLS_MAX_QUALIFIERS;i++)
1963 gnutls_datum_t td;
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)
1975 gnutls_assert();
1976 ret = _gnutls_asn2err (ret);
1977 goto cleanup;
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);
1985 if (ret < 0)
1987 gnutls_assert();
1988 goto full_cleanup;
1991 policy->qualifier[i].data = (void*)td.data;
1992 policy->qualifier[i].size = td.size;
1993 td.data = NULL;
1994 policy->qualifier[i].type = GNUTLS_X509_QUALIFIER_URI;
1996 else if (strcmp(tmpoid, "1.3.6.1.5.5.7.2.2") == 0)
1998 gnutls_datum_t txt;
2000 snprintf (tmpstr, sizeof (tmpstr), "?%u.policyQualifiers.?%u.qualifier", indx, i+1);
2002 ret = _gnutls_x509_read_value(c2, tmpstr, &td);
2003 if (ret < 0)
2005 gnutls_assert();
2006 goto full_cleanup;
2009 ret = decode_user_notice(td.data, td.size, &txt);
2010 gnutls_free(td.data);
2011 td.data = NULL;
2013 if (ret < 0)
2015 gnutls_assert();
2016 goto full_cleanup;
2019 policy->qualifier[i].data = (void*)txt.data;
2020 policy->qualifier[i].size = txt.size;
2021 policy->qualifier[i].type = GNUTLS_X509_QUALIFIER_NOTICE;
2023 else
2024 policy->qualifier[i].type = GNUTLS_X509_QUALIFIER_UNKNOWN;
2026 policy->qualifiers++;
2030 ret = 0;
2031 goto cleanup;
2033 full_cleanup:
2034 gnutls_x509_policy_release(policy);
2036 cleanup:
2037 _gnutls_free_datum (&tmpd);
2038 asn1_delete_structure (&c2);
2039 return ret;
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)
2067 int result;
2068 gnutls_datum_t output;
2070 if (cert == NULL)
2072 gnutls_assert ();
2073 return GNUTLS_E_INVALID_REQUEST;
2076 if ((result =
2077 _gnutls_x509_crt_get_extension (cert, oid, indx, &output,
2078 critical)) < 0)
2080 gnutls_assert ();
2081 return result;
2084 if (output.size == 0 || output.data == NULL)
2086 gnutls_assert ();
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;
2099 if (buf)
2100 memcpy (buf, output.data, output.size);
2102 _gnutls_free_datum (&output);
2104 return 0;
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
2124 * will be returned.
2127 gnutls_x509_crt_get_extension_oid (gnutls_x509_crt_t cert, int indx,
2128 void *oid, size_t * oid_size)
2130 int result;
2132 if (cert == NULL)
2134 gnutls_assert ();
2135 return GNUTLS_E_INVALID_REQUEST;
2138 result = _gnutls_x509_crt_get_extension_oid (cert, indx, oid, oid_size);
2139 if (result < 0)
2141 return result;
2144 return 0;
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
2170 * will be returned.
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)
2177 int result;
2178 char str_critical[10];
2179 char name[ASN1_MAX_NAME_SIZE];
2180 int len;
2182 if (!cert)
2184 gnutls_assert ();
2185 return GNUTLS_E_INVALID_REQUEST;
2188 snprintf (name, sizeof (name), "tbsCertificate.extensions.?%u.extnID",
2189 indx + 1);
2191 len = *oid_size;
2192 result = asn1_read_value (cert->cert, name, oid, &len);
2193 *oid_size = len;
2195 if (result == ASN1_ELEMENT_NOT_FOUND)
2196 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
2197 else if (result != ASN1_SUCCESS)
2199 gnutls_assert ();
2200 return _gnutls_asn2err (result);
2203 snprintf (name, sizeof (name), "tbsCertificate.extensions.?%u.critical",
2204 indx + 1);
2205 len = sizeof (str_critical);
2206 result = asn1_read_value (cert->cert, name, str_critical, &len);
2207 if (result != ASN1_SUCCESS)
2209 gnutls_assert ();
2210 return _gnutls_asn2err (result);
2213 if (critical)
2215 if (str_critical[0] == 'T')
2216 *critical = 1;
2217 else
2218 *critical = 0;
2221 return 0;
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
2234 * provided buffer.
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
2239 * sequence.
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
2244 * will be returned.
2247 gnutls_x509_crt_get_extension_data (gnutls_x509_crt_t cert, int indx,
2248 void *data, size_t * sizeof_data)
2250 int result, len;
2251 char name[ASN1_MAX_NAME_SIZE];
2253 if (!cert)
2255 gnutls_assert ();
2256 return GNUTLS_E_INVALID_REQUEST;
2259 snprintf (name, sizeof (name), "tbsCertificate.extensions.?%u.extnValue",
2260 indx + 1);
2262 len = *sizeof_data;
2263 result = asn1_read_value (cert->cert, name, data, &len);
2264 *sizeof_data = len;
2266 if (result == ASN1_ELEMENT_NOT_FOUND)
2267 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
2268 else if (result < 0)
2270 gnutls_assert ();
2271 return _gnutls_asn2err (result);
2274 return 0;
2277 static int
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;
2282 int result, len1;
2283 int start1, end1;
2284 gnutls_datum_t signed_data = { NULL, 0 };
2286 /* get the issuer of 'cert'
2288 if ((result =
2289 asn1_create_element (_gnutls_get_pkix (), "PKIX1.TBSCertificate",
2290 &c2)) != ASN1_SUCCESS)
2292 gnutls_assert ();
2293 return _gnutls_asn2err (result);
2296 result =
2297 _gnutls_x509_get_signed_data (cert->cert, "tbsCertificate", &signed_data);
2298 if (result < 0)
2300 gnutls_assert ();
2301 goto cleanup;
2304 result = asn1_der_decoding (&c2, signed_data.data, signed_data.size, NULL);
2305 if (result != ASN1_SUCCESS)
2307 gnutls_assert ();
2308 asn1_delete_structure (&c2);
2309 result = _gnutls_asn2err (result);
2310 goto cleanup;
2313 result =
2314 asn1_der_decoding_startEnd (c2, signed_data.data, signed_data.size,
2315 whom, &start1, &end1);
2317 if (result != ASN1_SUCCESS)
2319 gnutls_assert ();
2320 result = _gnutls_asn2err (result);
2321 goto cleanup;
2324 len1 = end1 - start1 + 1;
2326 _gnutls_set_datum (start, &signed_data.data[start1], len1);
2328 result = 0;
2330 cleanup:
2331 asn1_delete_structure (&c2);
2332 _gnutls_free_datum (&signed_data);
2333 return result;
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);
2373 static int
2374 get_dn (gnutls_x509_crt_t cert, const char *whom, gnutls_x509_dn_t * dn)
2376 *dn = asn1_find_node (cert->cert, whom);
2377 if (!*dn)
2378 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
2379 return 0;
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
2392 * deallocated.
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
2412 * deallocated.
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
2430 * is shown below.
2432 * struct gnutls_x509_ava_st {
2433 * gnutls_datum_t oid;
2434 * gnutls_datum_t value;
2435 * unsigned long value_tag;
2436 * };
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;
2456 long len;
2457 int lenlen, remlen, ret;
2458 char rbuf[ASN1_MAX_NAME_SIZE];
2459 unsigned char cls;
2460 const unsigned char *ptr;
2462 iava++;
2463 irdn++; /* 0->1, 1->2 etc */
2465 snprintf (rbuf, sizeof (rbuf), "rdnSequence.?%d.?%d", irdn, iava);
2466 rdn = asn1_find_node (dn, rbuf);
2467 if (!rdn)
2469 gnutls_assert ();
2470 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
2473 snprintf (rbuf, sizeof (rbuf), "?%d.type", iava);
2474 elem = asn1_find_node (rdn, rbuf);
2475 if (!elem)
2477 gnutls_assert ();
2478 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
2481 ret = asn1_read_node_value(elem, &vnode);
2482 if (ret != ASN1_SUCCESS)
2484 gnutls_assert ();
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);
2493 if (!elem)
2495 gnutls_assert ();
2496 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
2499 ret = asn1_read_node_value(elem, &vnode);
2500 if (ret != ASN1_SUCCESS)
2502 gnutls_assert ();
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.
2509 ptr = vnode.value;
2510 remlen = vnode.value_len;
2511 len = asn1_get_length_der (ptr, remlen, &lenlen);
2512 if (len < 0)
2514 gnutls_assert ();
2515 return GNUTLS_E_ASN1_DER_ERROR;
2518 ptr += lenlen;
2519 remlen -= lenlen;
2520 ret = asn1_get_tag_der (ptr, remlen, &cls, &lenlen, &ava->value_tag);
2521 if (ret)
2523 gnutls_assert ();
2524 return _gnutls_asn2err (ret);
2527 ptr += lenlen;
2528 remlen -= lenlen;
2531 signed long tmp;
2533 tmp = asn1_get_length_der (ptr, remlen, &lenlen);
2534 if (tmp < 0)
2536 gnutls_assert ();
2537 return GNUTLS_E_ASN1_DER_ERROR;
2539 ava->value.size = tmp;
2541 ava->value.data = (void*)(ptr + lenlen);
2543 return 0;
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)
2567 uint8_t *cert_buf;
2568 int cert_buf_size;
2569 int result;
2570 gnutls_datum_t tmp;
2572 if (buf_size == 0 || cert == NULL)
2574 return GNUTLS_E_INVALID_REQUEST;
2577 cert_buf_size = 0;
2578 asn1_der_coding (cert->cert, "", NULL, &cert_buf_size, NULL);
2580 cert_buf = gnutls_malloc (cert_buf_size);
2581 if (cert_buf == NULL)
2583 gnutls_assert ();
2584 return GNUTLS_E_MEMORY_ERROR;
2587 result = asn1_der_coding (cert->cert, "", cert_buf, &cert_buf_size, NULL);
2589 if (result != ASN1_SUCCESS)
2591 gnutls_assert ();
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);
2602 return result;
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
2617 * be returned.
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)
2630 if (cert == NULL)
2632 gnutls_assert ();
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.
2655 * Since: 3.1
2658 gnutls_x509_crt_export2 (gnutls_x509_crt_t cert,
2659 gnutls_x509_crt_fmt_t format, gnutls_datum_t * out)
2661 if (cert == NULL)
2663 gnutls_assert ();
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)
2675 int ret = 0;
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)
2682 gnutls_assert ();
2683 *output_data_size = digest_len;
2684 return GNUTLS_E_SHORT_MEMORY_BUFFER;
2687 ret = _gnutls_x509_encode_PKI_params(&der, pk, params);
2688 if (ret < 0)
2689 return gnutls_assert_val(ret);
2691 ret = _gnutls_hash_fast(hash, der.data, der.size, output_data);
2692 if (ret < 0)
2694 gnutls_assert ();
2695 goto cleanup;
2697 *output_data_size = digest_len;
2699 ret = 0;
2701 cleanup:
2703 _gnutls_free_datum (&der);
2704 return ret;
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)
2732 int pk, ret = 0;
2733 gnutls_pk_params_st params;
2735 if (crt == NULL)
2737 gnutls_assert ();
2738 return GNUTLS_E_INVALID_REQUEST;
2741 pk = gnutls_x509_crt_get_pk_algorithm (crt, NULL);
2742 if (pk < 0)
2744 gnutls_assert ();
2745 return pk;
2748 ret = _gnutls_x509_crt_get_mpis (crt, &params);
2749 if (ret < 0)
2751 gnutls_assert ();
2752 return ret;
2755 ret = _gnutls_get_key_id(pk, &params, output_data, output_data_size);
2757 gnutls_pk_params_release(&params);
2759 return ret;
2763 /* This is exactly as gnutls_x509_crt_check_revocation() except that
2764 * it calls func.
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;
2778 if (cert == NULL)
2780 gnutls_assert ();
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);
2790 if (ret < 0)
2792 gnutls_assert ();
2793 return ret;
2796 ret = gnutls_x509_crt_get_raw_issuer_dn (cert, &dn2);
2797 if (ret < 0)
2799 gnutls_assert ();
2800 return ret;
2803 ret = _gnutls_x509_compare_raw_dn (&dn1, &dn2);
2804 _gnutls_free_datum (&dn1);
2805 _gnutls_free_datum (&dn2);
2806 if (ret == 0)
2808 /* issuers do not match so don't even
2809 * bother checking.
2811 continue;
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);
2818 if (ret < 0)
2820 gnutls_assert ();
2821 return ret;
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]);
2829 if (ncerts < 0)
2831 gnutls_assert ();
2832 return ncerts;
2835 for (i = 0; i < ncerts; i++)
2837 serial_size = sizeof (serial);
2838 ret =
2839 gnutls_x509_crl_get_crt_serial (crl_list[j], i, serial,
2840 &serial_size, NULL);
2842 if (ret < 0)
2844 gnutls_assert ();
2845 return ret;
2848 if (serial_size == cert_serial_size)
2850 if (memcmp (serial, cert_serial, serial_size) == 0)
2852 /* serials match */
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.
2899 * Since: 2.8.0
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;
2907 int ret;
2909 if (crt == NULL)
2911 gnutls_assert ();
2912 return GNUTLS_E_INVALID_REQUEST;
2915 ret = _gnutls_x509_crt_get_mpis (crt, &issuer_params);
2916 if (ret < 0)
2918 gnutls_assert ();
2919 return ret;
2922 ret = _gnutls_x509_verify_algorithm (hash,
2923 signature,
2924 gnutls_x509_crt_get_pk_algorithm (crt,
2925 NULL),
2926 &issuer_params);
2928 /* release allocated mpis */
2929 gnutls_pk_params_release(&issuer_params);
2931 return ret;
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.
2951 * Since: 2.12.0
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;
2959 int ret;
2961 if (crt == NULL)
2963 gnutls_assert ();
2964 return GNUTLS_E_INVALID_REQUEST;
2967 ret = _gnutls_x509_crt_get_mpis (crt, &issuer_params);
2968 if (ret < 0)
2970 gnutls_assert ();
2971 return ret;
2974 ret =
2975 _gnutls_pk_get_hash_algorithm (gnutls_x509_crt_get_pk_algorithm
2976 (crt, NULL), &issuer_params,
2977 hash, mand);
2979 /* release allocated mpis */
2980 gnutls_pk_params_release(&issuer_params);
2982 return ret;
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)
3005 int result;
3007 if (crt == NULL)
3009 gnutls_assert ();
3010 return GNUTLS_E_INVALID_REQUEST;
3013 result = _gnutls_x509_verify_data (GNUTLS_DIG_UNKNOWN, data, signature, crt);
3014 if (result < 0)
3016 gnutls_assert ();
3017 return result;
3020 return result;
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;
3045 int ret;
3047 if (crt == NULL)
3049 gnutls_assert ();
3050 return GNUTLS_E_INVALID_REQUEST;
3053 ret = gnutls_x509_crt_get_verify_algorithm (crt, signature, &algo);
3054 if (ret < 0)
3055 return gnutls_assert_val(ret);
3057 /* Read the MPI parameters from the issuer's certificate.
3059 ret =
3060 _gnutls_x509_crt_get_mpis (crt, &params);
3061 if (ret < 0)
3063 gnutls_assert ();
3064 return ret;
3067 ret =
3068 pubkey_verify_hashed_data (gnutls_x509_crt_get_pk_algorithm (crt, NULL), algo,
3069 hash, signature, &params);
3070 if (ret < 0)
3072 gnutls_assert ();
3075 /* release all allocated MPIs
3077 gnutls_pk_params_release(&params);
3079 return ret;
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
3093 * Extensions.
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
3101 * returned.
3104 gnutls_x509_crt_get_crl_dist_points (gnutls_x509_crt_t cert,
3105 unsigned int seq, void *ret,
3106 size_t * ret_size,
3107 unsigned int *reason_flags,
3108 unsigned int *critical)
3110 int result;
3111 gnutls_datum_t dist_points = { NULL, 0 };
3112 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
3113 char name[ASN1_MAX_NAME_SIZE];
3114 int len;
3115 gnutls_x509_subject_alt_name_t type;
3116 uint8_t reasons[2];
3118 if (cert == NULL)
3120 gnutls_assert ();
3121 return GNUTLS_E_INVALID_REQUEST;
3124 if (*ret_size > 0 && ret)
3125 memset (ret, 0, *ret_size);
3126 else
3127 *ret_size = 0;
3129 if (reason_flags)
3130 *reason_flags = 0;
3132 result =
3133 _gnutls_x509_crt_get_extension (cert, "2.5.29.31", 0, &dist_points,
3134 critical);
3135 if (result < 0)
3137 return result;
3140 if (dist_points.size == 0 || dist_points.data == NULL)
3142 gnutls_assert ();
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)
3150 gnutls_assert ();
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)
3160 gnutls_assert ();
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);
3171 if (result < 0)
3173 asn1_delete_structure (&c2);
3174 return result;
3177 type = result;
3180 /* Read the CRL reasons.
3182 if (reason_flags)
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)
3193 gnutls_assert ();
3194 asn1_delete_structure (&c2);
3195 return _gnutls_asn2err (result);
3198 *reason_flags = reasons[0] | (reasons[1] << 8);
3201 asn1_delete_structure (&c2);
3203 return type;
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];
3233 int result, len;
3234 gnutls_datum_t id;
3235 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
3237 if (cert == NULL)
3239 gnutls_assert ();
3240 return GNUTLS_E_INVALID_REQUEST;
3243 if (oid)
3244 memset (oid, 0, *oid_size);
3245 else
3246 *oid_size = 0;
3248 if ((result =
3249 _gnutls_x509_crt_get_extension (cert, "2.5.29.37", 0, &id,
3250 critical)) < 0)
3252 return result;
3255 if (id.size == 0 || id.data == NULL)
3257 gnutls_assert ();
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)
3265 gnutls_assert ();
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)
3275 gnutls_assert ();
3276 asn1_delete_structure (&c2);
3277 return _gnutls_asn2err (result);
3280 indx++;
3281 /* create a string like "?1"
3283 snprintf (tmpstr, sizeof (tmpstr), "?%u", indx);
3285 len = *oid_size;
3286 result = asn1_read_value (c2, tmpstr, oid, &len);
3288 *oid_size = 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)
3298 gnutls_assert ();
3299 return _gnutls_asn2err (result);
3302 return 0;
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)
3322 int ret;
3323 gnutls_pk_params_st params;
3325 if (crt == NULL)
3327 gnutls_assert ();
3328 return GNUTLS_E_INVALID_REQUEST;
3331 ret = gnutls_x509_crt_get_pk_algorithm (crt, NULL);
3332 if (ret != GNUTLS_PK_RSA)
3334 gnutls_assert ();
3335 return GNUTLS_E_INVALID_REQUEST;
3338 ret = _gnutls_x509_crt_get_mpis (crt, &params);
3339 if (ret < 0)
3341 gnutls_assert ();
3342 return ret;
3345 ret = _gnutls_mpi_dprint_lz (params.params[0], m);
3346 if (ret < 0)
3348 gnutls_assert ();
3349 goto cleanup;
3352 ret = _gnutls_mpi_dprint_lz (params.params[1], e);
3353 if (ret < 0)
3355 gnutls_assert ();
3356 _gnutls_free_datum (m);
3357 goto cleanup;
3360 ret = 0;
3362 cleanup:
3363 gnutls_pk_params_release(&params);
3364 return ret;
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)
3386 int ret;
3387 gnutls_pk_params_st params;
3389 if (crt == NULL)
3391 gnutls_assert ();
3392 return GNUTLS_E_INVALID_REQUEST;
3395 ret = gnutls_x509_crt_get_pk_algorithm (crt, NULL);
3396 if (ret != GNUTLS_PK_DSA)
3398 gnutls_assert ();
3399 return GNUTLS_E_INVALID_REQUEST;
3402 ret = _gnutls_x509_crt_get_mpis (crt, &params);
3403 if (ret < 0)
3405 gnutls_assert ();
3406 return ret;
3410 /* P */
3411 ret = _gnutls_mpi_dprint_lz (params.params[0], p);
3412 if (ret < 0)
3414 gnutls_assert ();
3415 goto cleanup;
3418 /* Q */
3419 ret = _gnutls_mpi_dprint_lz (params.params[1], q);
3420 if (ret < 0)
3422 gnutls_assert ();
3423 _gnutls_free_datum (p);
3424 goto cleanup;
3428 /* G */
3429 ret = _gnutls_mpi_dprint_lz (params.params[2], g);
3430 if (ret < 0)
3432 gnutls_assert ();
3433 _gnutls_free_datum (p);
3434 _gnutls_free_datum (q);
3435 goto cleanup;
3439 /* Y */
3440 ret = _gnutls_mpi_dprint_lz (params.params[3], y);
3441 if (ret < 0)
3443 gnutls_assert ();
3444 _gnutls_free_datum (p);
3445 _gnutls_free_datum (g);
3446 _gnutls_free_datum (q);
3447 goto cleanup;
3450 ret = 0;
3452 cleanup:
3453 gnutls_pk_params_release(&params);
3454 return ret;
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.
3475 * Since: 3.0
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;
3484 int ret;
3486 *certs = gnutls_malloc(sizeof(gnutls_x509_crt_t)*init);
3487 if (*certs == NULL)
3489 gnutls_assert();
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);
3497 if (*certs == NULL)
3499 gnutls_assert();
3500 return GNUTLS_E_MEMORY_ERROR;
3503 ret = gnutls_x509_crt_list_import(*certs, &init, data, format, flags);
3506 if (ret < 0)
3508 gnutls_free(*certs);
3509 *certs = NULL;
3510 return ret;
3513 *size = init;
3514 return 0;
3517 static int check_if_sorted(gnutls_x509_crt_t * crt, int nr)
3519 char prev_dn[MAX_DN];
3520 char dn[MAX_DN];
3521 size_t prev_dn_size, dn_size;
3522 int i, ret;
3524 /* check if the X.509 list is ordered */
3525 if (nr > 1)
3528 for (i=0;i<nr;i++)
3530 if (i>0)
3532 dn_size = sizeof(dn);
3533 ret = gnutls_x509_crt_get_dn(crt[i], dn, &dn_size);
3534 if (ret < 0)
3536 ret = gnutls_assert_val(ret);
3537 goto cleanup;
3540 if (dn_size != prev_dn_size || memcmp(dn, prev_dn, dn_size) != 0)
3542 ret = gnutls_assert_val(GNUTLS_E_CERTIFICATE_LIST_UNSORTED);
3543 goto cleanup;
3547 prev_dn_size = sizeof(prev_dn);
3548 ret = gnutls_x509_crt_get_issuer_dn(crt[i], prev_dn, &prev_dn_size);
3549 if (ret < 0)
3551 ret = gnutls_assert_val(ret);
3552 goto cleanup;
3557 ret = 0;
3559 cleanup:
3560 return 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)
3593 int size;
3594 const char *ptr;
3595 gnutls_datum_t tmp;
3596 int ret, nocopy = 0;
3597 unsigned int count = 0, j;
3599 if (format == GNUTLS_X509_FMT_DER)
3601 if (*cert_max < 1)
3603 *cert_max = 1;
3604 return GNUTLS_E_SHORT_MEMORY_BUFFER;
3607 count = 1; /* import only the first one */
3609 ret = gnutls_x509_crt_init (&certs[0]);
3610 if (ret < 0)
3612 gnutls_assert ();
3613 goto error;
3616 ret = gnutls_x509_crt_import (certs[0], data, format);
3617 if (ret < 0)
3619 gnutls_assert ();
3620 goto error;
3623 *cert_max = 1;
3624 return 1;
3627 /* move to the certificate
3629 ptr = memmem (data->data, data->size,
3630 PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
3631 if (ptr == NULL)
3632 ptr = memmem (data->data, data->size,
3633 PEM_CERT_SEP2, sizeof (PEM_CERT_SEP2) - 1);
3635 if (ptr == NULL)
3636 return gnutls_assert_val(GNUTLS_E_NO_CERTIFICATE_FOUND);
3638 count = 0;
3642 if (count >= *cert_max)
3644 if (!(flags & GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED))
3645 break;
3646 else
3647 nocopy = 1;
3650 if (!nocopy)
3652 ret = gnutls_x509_crt_init (&certs[count]);
3653 if (ret < 0)
3655 gnutls_assert ();
3656 goto error;
3659 tmp.data = (void *) ptr;
3660 tmp.size = data->size - (ptr - (char *) data->data);
3662 ret =
3663 gnutls_x509_crt_import (certs[count], &tmp, GNUTLS_X509_FMT_PEM);
3664 if (ret < 0)
3666 gnutls_assert ();
3667 goto error;
3671 /* now we move ptr after the pem header
3673 ptr++;
3674 /* find the next certificate (if any)
3676 size = data->size - (ptr - (char *) data->data);
3678 if (size > 0)
3680 char *ptr2;
3682 ptr2 = memmem (ptr, size, PEM_CERT_SEP, sizeof (PEM_CERT_SEP) - 1);
3683 if (ptr2 == NULL)
3684 ptr2 = memmem (ptr, size, PEM_CERT_SEP2,
3685 sizeof (PEM_CERT_SEP2) - 1);
3687 ptr = ptr2;
3689 else
3690 ptr = NULL;
3692 count++;
3694 while (ptr != NULL);
3696 *cert_max = count;
3698 if (flags & GNUTLS_X509_CRT_LIST_FAIL_IF_UNSORTED)
3700 ret = check_if_sorted(certs, *cert_max);
3701 if (ret < 0)
3703 gnutls_assert();
3704 goto error;
3708 if (nocopy == 0)
3709 return count;
3710 else
3711 return GNUTLS_E_SHORT_MEMORY_BUFFER;
3713 error:
3714 for (j = 0; j < count; j++)
3715 gnutls_x509_crt_deinit (certs[j]);
3716 return ret;
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,
3737 size_t * buf_size)
3739 int result;
3740 gnutls_datum_t datum = { NULL, 0 };
3742 result =
3743 _gnutls_x509_read_value (crt->cert, "tbsCertificate.subjectUniqueID",
3744 &datum);
3746 if (datum.size > *buf_size)
3747 { /* then we're not going to fit */
3748 *buf_size = datum.size;
3749 buf[0] = '\0';
3750 result = GNUTLS_E_SHORT_MEMORY_BUFFER;
3752 else
3754 *buf_size = datum.size;
3755 memcpy (buf, datum.data, datum.size);
3758 _gnutls_free_datum (&datum);
3760 return result;
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.
3779 * Since: 2.12.0
3782 gnutls_x509_crt_get_issuer_unique_id (gnutls_x509_crt_t crt, char *buf,
3783 size_t * buf_size)
3785 int result;
3786 gnutls_datum_t datum = { NULL, 0 };
3788 result =
3789 _gnutls_x509_read_value (crt->cert, "tbsCertificate.issuerUniqueID",
3790 &datum);
3792 if (datum.size > *buf_size)
3793 { /* then we're not going to fit */
3794 *buf_size = datum.size;
3795 buf[0] = '\0';
3796 result = GNUTLS_E_SHORT_MEMORY_BUFFER;
3798 else
3800 *buf_size = datum.size;
3801 memcpy (buf, datum.data, datum.size);
3804 _gnutls_free_datum (&datum);
3806 return result;
3809 static int
3810 _gnutls_parse_aia (ASN1_TYPE src,
3811 unsigned int seq,
3812 int what,
3813 gnutls_datum_t * data)
3815 int len;
3816 char nptr[ASN1_MAX_NAME_SIZE];
3817 int result;
3818 gnutls_datum_t d;
3819 const char *oid = NULL;
3821 seq++; /* 0->1, 1->2 etc */
3822 switch (what)
3824 case GNUTLS_IA_ACCESSMETHOD_OID:
3825 snprintf (nptr, sizeof (nptr), "?%u.accessMethod", seq);
3826 break;
3828 case GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE:
3829 snprintf (nptr, sizeof (nptr), "?%u.accessLocation", seq);
3830 break;
3832 case GNUTLS_IA_CAISSUERS_URI:
3833 oid = GNUTLS_OID_AD_CAISSUERS;
3834 /* fall through */
3836 case GNUTLS_IA_OCSP_URI:
3837 if (oid == NULL)
3838 oid = GNUTLS_OID_AD_OCSP;
3840 char tmpoid[20];
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)
3850 gnutls_assert ();
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);
3856 /* fall through */
3858 case GNUTLS_IA_URI:
3859 snprintf (nptr, sizeof (nptr),
3860 "?%u.accessLocation.uniformResourceIdentifier", seq);
3861 break;
3863 default:
3864 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3867 len = 0;
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)
3874 gnutls_assert ();
3875 return _gnutls_asn2err (result);
3878 d.size = len;
3880 d.data = gnutls_malloc (d.size);
3881 if (d.data == NULL)
3882 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
3884 result = asn1_read_value (src, nptr, d.data, &len);
3885 if (result != ASN1_SUCCESS)
3887 gnutls_assert ();
3888 gnutls_free (d.data);
3889 return _gnutls_asn2err (result);
3892 if (data)
3894 data->data = d.data;
3895 data->size = d.size;
3897 else
3898 gnutls_free (d.data);
3900 return 0;
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
3957 * as usual.
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.
3970 * Since: 3.0
3973 gnutls_x509_crt_get_authority_info_access (gnutls_x509_crt_t crt,
3974 unsigned int seq,
3975 int what,
3976 gnutls_datum_t * data,
3977 unsigned int *critical)
3979 int ret;
3980 gnutls_datum_t aia;
3981 ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
3983 if (crt == NULL)
3985 gnutls_assert ();
3986 return GNUTLS_E_INVALID_REQUEST;
3989 if ((ret = _gnutls_x509_crt_get_extension (crt, GNUTLS_OID_AIA, 0, &aia,
3990 critical)) < 0)
3991 return ret;
3993 if (aia.size == 0 || aia.data == NULL)
3995 gnutls_assert ();
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)
4006 gnutls_assert ();
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)
4016 gnutls_assert ();
4017 asn1_delete_structure (&c2);
4018 return _gnutls_asn2err (ret);
4021 ret = _gnutls_parse_aia (c2, seq, what, data);
4023 asn1_delete_structure (&c2);
4024 if (ret < 0)
4025 gnutls_assert ();
4027 return ret;
4031 * gnutls_x509_crt_set_pin_function:
4032 * @crt: The certificate structure
4033 * @fn: the callback
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().
4043 * Since: 3.1.0
4046 void gnutls_x509_crt_set_pin_function (gnutls_x509_crt_t crt,
4047 gnutls_pin_callback_t fn, void *userdata)
4049 crt->pin.cb = fn;
4050 crt->pin.data = userdata;