Tizen 2.0 Release
[external/libgnutls26.git] / tests / dn.c
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
3  * Author: Simon Josefsson, Howard Chu
4  *
5  * This file is part of GnuTLS.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
30 #include "utils.h"
31
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/EOC16m0UdymQhwQwfPM5wWKJCJK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4eO5TRA50hmLxwt5D8Vs2v55n1kl6\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";
61
62 static void
63 print_dn (gnutls_x509_dn_t dn)
64 {
65   int i, j, ret = 0;
66   gnutls_x509_ava_st ava;
67
68   for (i = 0; ret == 0; i++)
69     for (j = 0; ret == 0; j++)
70       {
71         ret = gnutls_x509_dn_get_rdn_ava (dn, i, j, &ava);
72         if (ret == GNUTLS_E_ASN1_ELEMENT_NOT_FOUND)
73           {
74             if (j > 0)
75               {
76                 j = 0;
77                 ret = 0;
78               }
79             break;
80           }
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);
86       }
87 }
88
89 void
90 doit (void)
91 {
92   int ret;
93   gnutls_datum_t derCert = { pem, sizeof (pem) };
94   gnutls_x509_crt_t cert;
95   gnutls_x509_dn_t xdn;
96
97   ret = gnutls_global_init ();
98   if (ret < 0)
99     fail ("init %d\n", ret);
100
101   ret = gnutls_x509_crt_init (&cert);
102   if (ret < 0)
103     fail ("crt_init %d\n", ret);
104
105   ret = gnutls_x509_crt_import (cert, &derCert, GNUTLS_X509_FMT_PEM);
106   if (ret < 0)
107     fail ("crt_import %d\n", ret);
108
109   ret = gnutls_x509_crt_get_issuer (cert, &xdn);
110   if (ret < 0)
111     fail ("get_issuer %d\n", ret);
112
113   if (debug)
114     {
115       printf ("Issuer:\n");
116       print_dn (xdn);
117     }
118
119   ret = gnutls_x509_crt_get_subject (cert, &xdn);
120   if (ret < 0)
121     fail ("get_subject %d\n", ret);
122
123   if (debug)
124     {
125       printf ("Subject:\n");
126       print_dn (xdn);
127     }
128
129   if (debug)
130     success ("done\n");
131
132   gnutls_x509_crt_deinit (cert);
133   gnutls_global_deinit ();
134 }