More enum docs.
[gnutls.git] / tests / moredn.c
blob396accb5f1d9c4d9ce5669c599edf7896d7c380d
1 /*
2 * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
4 * Author: Joe Orton
6 * This file is part of GNUTLS.
8 * GNUTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GNUTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNUTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Parts copied from GnuTLS example programs. */
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
35 #include <gnutls/gnutls.h>
36 #include <gnutls/x509.h>
38 #include "utils.h"
40 static const char cert_pem[] =
41 "-----BEGIN CERTIFICATE-----\n"
42 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
43 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
44 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
45 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
46 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
47 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
48 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
49 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
50 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
51 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
52 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
53 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
54 static const gnutls_datum_t cert_datum = { (char *) cert_pem,
55 sizeof (cert_pem)
58 void
59 doit (void)
61 gnutls_x509_crt_t cert;
62 gnutls_x509_dn_t sdn, dn2;
63 unsigned char buf[8192], buf2[8192];
64 size_t buflen, buf2len;
65 gnutls_datum_t datum;
66 int rv;
68 gnutls_global_init ();
70 if (gnutls_x509_crt_init (&cert) == 0)
71 success ("success: cert init\n");
72 else
73 fail ("cert init failure\n");
75 if (gnutls_x509_crt_import (cert, &cert_datum, GNUTLS_X509_FMT_PEM) == 0)
76 success ("success: imported PEM cert\n");
77 else
78 fail ("FAIL: could not import PEM cert\n");
80 if (gnutls_x509_crt_get_subject (cert, &sdn) == 0)
81 success ("success: got subject DN.\n");
82 else
83 fail ("FAIL: could not get subject DN.\n");
85 buflen = sizeof buf;
86 rv = gnutls_x509_dn_export (sdn, GNUTLS_X509_FMT_DER, buf, &buflen);
87 if (rv == 0)
88 success ("success: exported subject DN.\n");
89 else
90 fail ("FAIL: could not export subject DN: %s\n", gnutls_strerror (rv));
92 if (gnutls_x509_dn_init (&dn2) == 0)
93 success ("success: init DN.\n");
94 else
95 fail ("FAIL: DN init.\n");
97 datum.data = buf;
98 datum.size = buflen;
100 if (gnutls_x509_dn_import (dn2, &datum) == 0)
101 success ("success: re-import subject DN.\n");
102 else
103 fail ("FAIL: re-import subject DN.\n");
105 buf2len = sizeof buf2;
106 rv = gnutls_x509_dn_export (dn2, GNUTLS_X509_FMT_DER, buf2, &buf2len);
107 if (rv == 0)
108 success ("success: exported subject DN.\n");
109 else
110 fail ("FAIL: could not export subject DN: %s\n", gnutls_strerror (rv));
112 if (buflen == buf2len && memcmp (buf, buf2, buflen) == 0)
113 success ("success: export/import/export match.\n");
114 else
115 fail ("FAIL: export/import/export differ.\n");
117 gnutls_x509_dn_deinit (dn2);
119 gnutls_x509_crt_deinit (cert);
121 gnutls_global_deinit ();