2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS-EXTRA.
8 * GnuTLS-extra is free software: you can redistribute it and/or modify
9 * it 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-extra is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* This file includes all functions that were in the 0.5.x and 0.8.x
23 * gnutls API. They are now implemented over the new certificate parsing
27 #include "gnutls_int.h"
29 #include <gnutls_global.h>
30 #include <gnutls_errors.h>
31 #include <string.h> /* memset */
32 #include <x509/x509_int.h>
34 #include <gnutls/x509.h>
35 #include <openssl_compat.h>
38 * gnutls_x509_extract_certificate_dn:
39 * @cert: should contain an X.509 DER encoded certificate
40 * @ret: a pointer to a structure to hold the peer's name
42 * This function will return the name of the certificate holder. The name is gnutls_x509_dn structure and
43 * is a obtained by the peer's certificate. If the certificate send by the
44 * peer is invalid, or in any other failure this function returns error.
45 * Returns a negative error code in case of an error.
48 gnutls_x509_extract_certificate_dn(const gnutls_datum_t * cert,
51 gnutls_x509_crt_t xcert;
55 result = gnutls_x509_crt_init(&xcert);
59 result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
61 gnutls_x509_crt_deinit(xcert);
65 len = sizeof(ret->country);
66 gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_COUNTRY_NAME,
67 0, 0, ret->country, &len);
69 len = sizeof(ret->organization);
70 gnutls_x509_crt_get_dn_by_oid(xcert,
71 GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
72 0, ret->organization, &len);
74 len = sizeof(ret->organizational_unit_name);
75 gnutls_x509_crt_get_dn_by_oid(xcert,
76 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
77 0, 0, ret->organizational_unit_name,
80 len = sizeof(ret->common_name);
81 gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_COMMON_NAME,
82 0, 0, ret->common_name, &len);
84 len = sizeof(ret->locality_name);
85 gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_LOCALITY_NAME,
86 0, 0, ret->locality_name, &len);
88 len = sizeof(ret->state_or_province_name);
89 gnutls_x509_crt_get_dn_by_oid(xcert,
90 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME,
91 0, 0, ret->state_or_province_name,
94 len = sizeof(ret->email);
95 gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_PKCS9_EMAIL, 0, 0,
98 gnutls_x509_crt_deinit(xcert);
104 * gnutls_x509_extract_certificate_issuer_dn:
105 * @cert: should contain an X.509 DER encoded certificate
106 * @ret: a pointer to a structure to hold the issuer's name
108 * This function will return the name of the issuer stated in the certificate. The name is a gnutls_x509_dn structure and
109 * is a obtained by the peer's certificate. If the certificate send by the
110 * peer is invalid, or in any other failure this function returns error.
111 * Returns a negative error code in case of an error.
114 gnutls_x509_extract_certificate_issuer_dn(const gnutls_datum_t * cert,
115 gnutls_x509_dn * ret)
117 gnutls_x509_crt_t xcert;
121 result = gnutls_x509_crt_init(&xcert);
125 result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
127 gnutls_x509_crt_deinit(xcert);
131 len = sizeof(ret->country);
132 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
133 GNUTLS_OID_X520_COUNTRY_NAME,
134 0, 0, ret->country, &len);
136 len = sizeof(ret->organization);
137 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
138 GNUTLS_OID_X520_ORGANIZATION_NAME,
139 0, 0, ret->organization,
142 len = sizeof(ret->organizational_unit_name);
143 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
144 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
146 ret->organizational_unit_name,
149 len = sizeof(ret->common_name);
150 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
151 GNUTLS_OID_X520_COMMON_NAME,
152 0, 0, ret->common_name, &len);
154 len = sizeof(ret->locality_name);
155 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
156 GNUTLS_OID_X520_LOCALITY_NAME,
157 0, 0, ret->locality_name,
160 len = sizeof(ret->state_or_province_name);
161 gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
162 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME,
164 ret->state_or_province_name,
167 len = sizeof(ret->email);
168 gnutls_x509_crt_get_issuer_dn_by_oid(xcert, GNUTLS_OID_PKCS9_EMAIL,
169 0, 0, ret->email, &len);
171 gnutls_x509_crt_deinit(xcert);