Exported gnutls_cpuid() and gnutls_have_cpuid().
[gnutls:gnutls.git] / extra / openssl_compat.c
1 /*
2  * Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS-EXTRA.
8  *
9  * GnuTLS-extra is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *               
14  * GnuTLS-extra is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *                               
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 /* This file includes all functions that were in the 0.5.x and 0.8.x
24  * gnutls API. They are now implemented over the new certificate parsing
25  * API.
26  */
27
28 #include "gnutls_int.h"
29
30 #include <gnutls_global.h>
31 #include <gnutls_errors.h>
32 #include <string.h>             /* memset */
33 #include <x509/x509_int.h>
34 #include <libtasn1.h>
35 #include <gnutls/x509.h>
36 #include <openssl_compat.h>
37
38 /*-
39  * gnutls_x509_extract_certificate_dn:
40  * @cert: should contain an X.509 DER encoded certificate
41  * @ret: a pointer to a structure to hold the peer's name
42  *
43  * This function will return the name of the certificate holder. The name is gnutls_x509_dn structure and
44  * is a obtained by the peer's certificate. If the certificate send by the
45  * peer is invalid, or in any other failure this function returns error.
46  * Returns a negative error code in case of an error.
47  -*/
48 int
49 gnutls_x509_extract_certificate_dn (const gnutls_datum_t * cert,
50                                     gnutls_x509_dn * ret)
51 {
52   gnutls_x509_crt_t xcert;
53   int result;
54   size_t len;
55
56   result = gnutls_x509_crt_init (&xcert);
57   if (result < 0)
58     return result;
59
60   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
61   if (result < 0)
62     {
63       gnutls_x509_crt_deinit (xcert);
64       return result;
65     }
66
67   len = sizeof (ret->country);
68   gnutls_x509_crt_get_dn_by_oid (xcert, GNUTLS_OID_X520_COUNTRY_NAME, 0,
69                                  0, ret->country, &len);
70
71   len = sizeof (ret->organization);
72   gnutls_x509_crt_get_dn_by_oid (xcert, GNUTLS_OID_X520_ORGANIZATION_NAME,
73                                  0, 0, ret->organization, &len);
74
75   len = sizeof (ret->organizational_unit_name);
76   gnutls_x509_crt_get_dn_by_oid (xcert,
77                                  GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
78                                  0, 0, ret->organizational_unit_name, &len);
79
80   len = sizeof (ret->common_name);
81   gnutls_x509_crt_get_dn_by_oid (xcert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
82                                  ret->common_name, &len);
83
84   len = sizeof (ret->locality_name);
85   gnutls_x509_crt_get_dn_by_oid (xcert, GNUTLS_OID_X520_LOCALITY_NAME, 0,
86                                  0, ret->locality_name, &len);
87
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, &len);
92
93   len = sizeof (ret->email);
94   gnutls_x509_crt_get_dn_by_oid (xcert, GNUTLS_OID_PKCS9_EMAIL, 0, 0,
95                                  ret->email, &len);
96
97   gnutls_x509_crt_deinit (xcert);
98
99   return 0;
100 }
101
102 /*-
103  * gnutls_x509_extract_certificate_issuer_dn:
104  * @cert: should contain an X.509 DER encoded certificate
105  * @ret: a pointer to a structure to hold the issuer's name
106  *
107  * This function will return the name of the issuer stated in the certificate. The name is a gnutls_x509_dn structure and
108  * is a obtained by the peer's certificate. If the certificate send by the
109  * peer is invalid, or in any other failure this function returns error.
110  * Returns a negative error code in case of an error.
111  -*/
112 int
113 gnutls_x509_extract_certificate_issuer_dn (const gnutls_datum_t * cert,
114                                            gnutls_x509_dn * ret)
115 {
116   gnutls_x509_crt_t xcert;
117   int result;
118   size_t len;
119
120   result = gnutls_x509_crt_init (&xcert);
121   if (result < 0)
122     return result;
123
124   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
125   if (result < 0)
126     {
127       gnutls_x509_crt_deinit (xcert);
128       return result;
129     }
130
131   len = sizeof (ret->country);
132   gnutls_x509_crt_get_issuer_dn_by_oid (xcert,
133                                         GNUTLS_OID_X520_COUNTRY_NAME, 0,
134                                         0, ret->country, &len);
135
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, &len);
140
141   len = sizeof (ret->organizational_unit_name);
142   gnutls_x509_crt_get_issuer_dn_by_oid (xcert,
143                                         GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
144                                         0, 0,
145                                         ret->organizational_unit_name, &len);
146
147   len = sizeof (ret->common_name);
148   gnutls_x509_crt_get_issuer_dn_by_oid (xcert,
149                                         GNUTLS_OID_X520_COMMON_NAME, 0, 0,
150                                         ret->common_name, &len);
151
152   len = sizeof (ret->locality_name);
153   gnutls_x509_crt_get_issuer_dn_by_oid (xcert,
154                                         GNUTLS_OID_X520_LOCALITY_NAME, 0,
155                                         0, ret->locality_name, &len);
156
157   len = sizeof (ret->state_or_province_name);
158   gnutls_x509_crt_get_issuer_dn_by_oid (xcert,
159                                         GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME,
160                                         0, 0, ret->state_or_province_name,
161                                         &len);
162
163   len = sizeof (ret->email);
164   gnutls_x509_crt_get_issuer_dn_by_oid (xcert, GNUTLS_OID_PKCS9_EMAIL, 0,
165                                         0, ret->email, &len);
166
167   gnutls_x509_crt_deinit (xcert);
168
169   return 0;
170 }
171
172