summaryrefslogtreecommitdiff
path: root/extra/openssl_compat.c
blob: da81b7131e4ba0f4174cf619aaf47ea145142c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS-EXTRA.
 *
 * GnuTLS-extra is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *               
 * GnuTLS-extra is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *                               
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* This file includes all functions that were in the 0.5.x and 0.8.x
 * gnutls API. They are now implemented over the new certificate parsing
 * API.
 */

#include "gnutls_int.h"

#include <string.h> /* memset */
#include <x509/x509_int.h>
#include <libtasn1.h>
#include <gnutls/x509.h>
#include <openssl_compat.h>

/*-
 * gnutls_x509_extract_certificate_dn:
 * @cert: should contain an X.509 DER encoded certificate
 * @ret: a pointer to a structure to hold the peer's name
 *
 * This function will return the name of the certificate holder. The name is gnutls_x509_dn structure and
 * is a obtained by the peer's certificate. If the certificate send by the
 * peer is invalid, or in any other failure this function returns error.
 * Returns a negative error code in case of an error.
 -*/
int gnutls_x509_extract_certificate_dn(const gnutls_datum_t *cert,
				       gnutls_x509_dn *ret)
{
	gnutls_x509_crt_t xcert;
	int result;
	size_t len;

	result = gnutls_x509_crt_init(&xcert);
	if (result < 0)
		return result;

	result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
	if (result < 0) {
		gnutls_x509_crt_deinit(xcert);
		return result;
	}

	len = sizeof(ret->country);
	gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_COUNTRY_NAME, 0, 0,
				      ret->country, &len);

	len = sizeof(ret->organization);
	gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_ORGANIZATION_NAME,
				      0, 0, ret->organization, &len);

	len = sizeof(ret->organizational_unit_name);
	gnutls_x509_crt_get_dn_by_oid(xcert,
				      GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
				      0, 0, ret->organizational_unit_name,
				      &len);

	len = sizeof(ret->common_name);
	gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
				      ret->common_name, &len);

	len = sizeof(ret->locality_name);
	gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_X520_LOCALITY_NAME, 0,
				      0, ret->locality_name, &len);

	len = sizeof(ret->state_or_province_name);
	gnutls_x509_crt_get_dn_by_oid(xcert,
				      GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, 0,
				      0, ret->state_or_province_name, &len);

	len = sizeof(ret->email);
	gnutls_x509_crt_get_dn_by_oid(xcert, GNUTLS_OID_PKCS9_EMAIL, 0, 0,
				      ret->email, &len);

	gnutls_x509_crt_deinit(xcert);

	return 0;
}

/*-
 * gnutls_x509_extract_certificate_issuer_dn:
 * @cert: should contain an X.509 DER encoded certificate
 * @ret: a pointer to a structure to hold the issuer's name
 *
 * This function will return the name of the issuer stated in the certificate. The name is a gnutls_x509_dn structure and
 * is a obtained by the peer's certificate. If the certificate send by the
 * peer is invalid, or in any other failure this function returns error.
 * Returns a negative error code in case of an error.
 -*/
int gnutls_x509_extract_certificate_issuer_dn(const gnutls_datum_t *cert,
					      gnutls_x509_dn *ret)
{
	gnutls_x509_crt_t xcert;
	int result;
	size_t len;

	result = gnutls_x509_crt_init(&xcert);
	if (result < 0)
		return result;

	result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
	if (result < 0) {
		gnutls_x509_crt_deinit(xcert);
		return result;
	}

	len = sizeof(ret->country);
	gnutls_x509_crt_get_issuer_dn_by_oid(
		xcert, GNUTLS_OID_X520_COUNTRY_NAME, 0, 0, ret->country, &len);

	len = sizeof(ret->organization);
	gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
					     GNUTLS_OID_X520_ORGANIZATION_NAME,
					     0, 0, ret->organization, &len);

	len = sizeof(ret->organizational_unit_name);
	gnutls_x509_crt_get_issuer_dn_by_oid(
		xcert, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, 0, 0,
		ret->organizational_unit_name, &len);

	len = sizeof(ret->common_name);
	gnutls_x509_crt_get_issuer_dn_by_oid(xcert, GNUTLS_OID_X520_COMMON_NAME,
					     0, 0, ret->common_name, &len);

	len = sizeof(ret->locality_name);
	gnutls_x509_crt_get_issuer_dn_by_oid(xcert,
					     GNUTLS_OID_X520_LOCALITY_NAME, 0,
					     0, ret->locality_name, &len);

	len = sizeof(ret->state_or_province_name);
	gnutls_x509_crt_get_issuer_dn_by_oid(
		xcert, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, 0, 0,
		ret->state_or_province_name, &len);

	len = sizeof(ret->email);
	gnutls_x509_crt_get_issuer_dn_by_oid(xcert, GNUTLS_OID_PKCS9_EMAIL, 0,
					     0, ret->email, &len);

	gnutls_x509_crt_deinit(xcert);

	return 0;
}