safe renegotiation tests only run under valgrind in the devel environment.
[gnutls.git] / tests / dn.c
blob3c40b98052c64874d10b02509fb75253d097cb23
1 /*
2 * Copyright (C) 2006-2012 Free Software Foundation, Inc.
3 * Author: Simon Josefsson, Howard Chu
5 * This file is part of GnuTLS.
7 * GnuTLS is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuTLS 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 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GnuTLS; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
30 #include "utils.h"
32 static char pem[] =
33 "-----BEGIN CERTIFICATE-----\n"
34 "MIIE8jCCAtqgAwIBAgIDAkQVMA0GCSqGSIb3DQEBBQUAMHkxEDAOBgNVBAoTB1Jv\n"
35 "b3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEiMCAGA1UEAxMZ\n"
36 "Q0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJARYSc3VwcG9y\n"
37 "dEBjYWNlcnQub3JnMB4XDTA2MDUxNTE1MjEzMVoXDTA3MDUxNTE1MjEzMVowPjEY\n"
38 "MBYGA1UEAxMPQ0FjZXJ0IFdvVCBVc2VyMSIwIAYJKoZIhvcNAQkBFhNzaW1vbkBq\n"
39 "b3NlZnNzb24ub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuddO\n"
40 "vxr7gfof8Ejtk/EOC16m0UdymQhwQwfPM5wWKJCJK9l5G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TRA50hmLxwt5D8Vs2v55n1kl6\n"
56 "7PBo6H2ZMfbQcws731k4RpOqQcU+2yl/wBlDChOOO95mbJ31tqMh27yIjIemgD6Z\n"
57 "jxL92AgHPzSFy/nyqmZ1ADcnB5fC5WsEYyr9tPM1gpjJEsi95YIBrO7Uyt4tj5U3\n"
58 "dYDvbU+Mg1r0gJi61wciuyAllwKfu9aqkCjJKQGHrTimWzRa6RPygaojWIEmap89\n"
59 "bHarWgDg9CKVP1DggVkcD838s//kE1Vl2DReyfAtEQ1agSXLFncgxL+yOi1o3lcq\n"
60 "+dmDgpDn168TY1Iug80uVKg7AfkLrA==\n" "-----END CERTIFICATE-----\n";
62 static void
63 print_dn (gnutls_x509_dn_t dn)
65 int i, j, ret = 0;
66 gnutls_x509_ava_st ava;
68 for (i = 0; ret == 0; i++)
69 for (j = 0; ret == 0; j++)
71 ret = gnutls_x509_dn_get_rdn_ava (dn, i, j, &ava);
72 if (ret == GNUTLS_E_ASN1_ELEMENT_NOT_FOUND)
74 if (j > 0)
76 j = 0;
77 ret = 0;
79 break;
81 if (ret < 0)
82 fail ("get_rdn_ava %d\n", ret);
83 if (debug)
84 printf ("dn[%d][%d] OID=%.*s\n\tDATA=%.*s\n", i, j,
85 ava.oid.size, ava.oid.data, ava.value.size, ava.value.data);
89 void
90 doit (void)
92 int ret;
93 gnutls_datum_t pem_cert = { (unsigned char*)pem, sizeof (pem) };
94 gnutls_x509_crt_t cert;
95 gnutls_x509_dn_t xdn;
97 ret = gnutls_global_init ();
98 if (ret < 0)
99 fail ("init %d\n", ret);
101 ret = gnutls_x509_crt_init (&cert);
102 if (ret < 0)
103 fail ("crt_init %d\n", ret);
105 ret = gnutls_x509_crt_import (cert, &pem_cert, GNUTLS_X509_FMT_PEM);
106 if (ret < 0)
107 fail ("crt_import %d\n", ret);
109 ret = gnutls_x509_crt_get_issuer (cert, &xdn);
110 if (ret < 0)
111 fail ("get_issuer %d\n", ret);
113 if (debug)
115 printf ("Issuer:\n");
116 print_dn (xdn);
119 ret = gnutls_x509_crt_get_subject (cert, &xdn);
120 if (ret < 0)
121 fail ("get_subject %d\n", ret);
123 if (debug)
125 printf ("Subject:\n");
126 print_dn (xdn);
129 if (debug)
130 success ("done\n");
132 gnutls_x509_crt_deinit (cert);
133 gnutls_global_deinit ();