1ebfedea0SLionel Sambuc /* crypto/x509/x509_cmp.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <ctype.h>
61ebfedea0SLionel Sambuc #include "cryptlib.h"
62ebfedea0SLionel Sambuc #include <openssl/asn1.h>
63ebfedea0SLionel Sambuc #include <openssl/objects.h>
64ebfedea0SLionel Sambuc #include <openssl/x509.h>
65ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
66ebfedea0SLionel Sambuc
X509_issuer_and_serial_cmp(const X509 * a,const X509 * b)67ebfedea0SLionel Sambuc int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
68ebfedea0SLionel Sambuc {
69ebfedea0SLionel Sambuc int i;
70ebfedea0SLionel Sambuc X509_CINF *ai, *bi;
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc ai = a->cert_info;
73ebfedea0SLionel Sambuc bi = b->cert_info;
74ebfedea0SLionel Sambuc i = M_ASN1_INTEGER_cmp(ai->serialNumber, bi->serialNumber);
75*0a6a1f1dSLionel Sambuc if (i)
76*0a6a1f1dSLionel Sambuc return (i);
77ebfedea0SLionel Sambuc return (X509_NAME_cmp(ai->issuer, bi->issuer));
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc
80ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_MD5
X509_issuer_and_serial_hash(X509 * a)81ebfedea0SLionel Sambuc unsigned long X509_issuer_and_serial_hash(X509 *a)
82ebfedea0SLionel Sambuc {
83ebfedea0SLionel Sambuc unsigned long ret = 0;
84ebfedea0SLionel Sambuc EVP_MD_CTX ctx;
85ebfedea0SLionel Sambuc unsigned char md[16];
86ebfedea0SLionel Sambuc char *f;
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc EVP_MD_CTX_init(&ctx);
89ebfedea0SLionel Sambuc f = X509_NAME_oneline(a->cert_info->issuer, NULL, 0);
90ebfedea0SLionel Sambuc if (!EVP_DigestInit_ex(&ctx, EVP_md5(), NULL))
91ebfedea0SLionel Sambuc goto err;
92ebfedea0SLionel Sambuc if (!EVP_DigestUpdate(&ctx, (unsigned char *)f, strlen(f)))
93ebfedea0SLionel Sambuc goto err;
94ebfedea0SLionel Sambuc OPENSSL_free(f);
95*0a6a1f1dSLionel Sambuc if (!EVP_DigestUpdate
96*0a6a1f1dSLionel Sambuc (&ctx, (unsigned char *)a->cert_info->serialNumber->data,
97ebfedea0SLionel Sambuc (unsigned long)a->cert_info->serialNumber->length))
98ebfedea0SLionel Sambuc goto err;
99ebfedea0SLionel Sambuc if (!EVP_DigestFinal_ex(&ctx, &(md[0]), NULL))
100ebfedea0SLionel Sambuc goto err;
101ebfedea0SLionel Sambuc ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
102ebfedea0SLionel Sambuc ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
103ebfedea0SLionel Sambuc ) & 0xffffffffL;
104ebfedea0SLionel Sambuc err:
105ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&ctx);
106ebfedea0SLionel Sambuc return (ret);
107ebfedea0SLionel Sambuc }
108ebfedea0SLionel Sambuc #endif
109ebfedea0SLionel Sambuc
X509_issuer_name_cmp(const X509 * a,const X509 * b)110ebfedea0SLionel Sambuc int X509_issuer_name_cmp(const X509 *a, const X509 *b)
111ebfedea0SLionel Sambuc {
112ebfedea0SLionel Sambuc return (X509_NAME_cmp(a->cert_info->issuer, b->cert_info->issuer));
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc
X509_subject_name_cmp(const X509 * a,const X509 * b)115ebfedea0SLionel Sambuc int X509_subject_name_cmp(const X509 *a, const X509 *b)
116ebfedea0SLionel Sambuc {
117ebfedea0SLionel Sambuc return (X509_NAME_cmp(a->cert_info->subject, b->cert_info->subject));
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc
X509_CRL_cmp(const X509_CRL * a,const X509_CRL * b)120ebfedea0SLionel Sambuc int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
121ebfedea0SLionel Sambuc {
122ebfedea0SLionel Sambuc return (X509_NAME_cmp(a->crl->issuer, b->crl->issuer));
123ebfedea0SLionel Sambuc }
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA
X509_CRL_match(const X509_CRL * a,const X509_CRL * b)126ebfedea0SLionel Sambuc int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
127ebfedea0SLionel Sambuc {
128ebfedea0SLionel Sambuc return memcmp(a->sha1_hash, b->sha1_hash, 20);
129ebfedea0SLionel Sambuc }
130ebfedea0SLionel Sambuc #endif
131ebfedea0SLionel Sambuc
X509_get_issuer_name(X509 * a)132ebfedea0SLionel Sambuc X509_NAME *X509_get_issuer_name(X509 *a)
133ebfedea0SLionel Sambuc {
134ebfedea0SLionel Sambuc return (a->cert_info->issuer);
135ebfedea0SLionel Sambuc }
136ebfedea0SLionel Sambuc
X509_issuer_name_hash(X509 * x)137ebfedea0SLionel Sambuc unsigned long X509_issuer_name_hash(X509 *x)
138ebfedea0SLionel Sambuc {
139ebfedea0SLionel Sambuc return (X509_NAME_hash(x->cert_info->issuer));
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_MD5
X509_issuer_name_hash_old(X509 * x)143ebfedea0SLionel Sambuc unsigned long X509_issuer_name_hash_old(X509 *x)
144ebfedea0SLionel Sambuc {
145ebfedea0SLionel Sambuc return (X509_NAME_hash_old(x->cert_info->issuer));
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc #endif
148ebfedea0SLionel Sambuc
X509_get_subject_name(X509 * a)149ebfedea0SLionel Sambuc X509_NAME *X509_get_subject_name(X509 *a)
150ebfedea0SLionel Sambuc {
151ebfedea0SLionel Sambuc return (a->cert_info->subject);
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc
X509_get_serialNumber(X509 * a)154ebfedea0SLionel Sambuc ASN1_INTEGER *X509_get_serialNumber(X509 *a)
155ebfedea0SLionel Sambuc {
156ebfedea0SLionel Sambuc return (a->cert_info->serialNumber);
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc
X509_subject_name_hash(X509 * x)159ebfedea0SLionel Sambuc unsigned long X509_subject_name_hash(X509 *x)
160ebfedea0SLionel Sambuc {
161ebfedea0SLionel Sambuc return (X509_NAME_hash(x->cert_info->subject));
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_MD5
X509_subject_name_hash_old(X509 * x)165ebfedea0SLionel Sambuc unsigned long X509_subject_name_hash_old(X509 *x)
166ebfedea0SLionel Sambuc {
167ebfedea0SLionel Sambuc return (X509_NAME_hash_old(x->cert_info->subject));
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc #endif
170ebfedea0SLionel Sambuc
171ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SHA
172*0a6a1f1dSLionel Sambuc /*
173*0a6a1f1dSLionel Sambuc * Compare two certificates: they must be identical for this to work. NB:
174*0a6a1f1dSLionel Sambuc * Although "cmp" operations are generally prototyped to take "const"
175*0a6a1f1dSLionel Sambuc * arguments (eg. for use in STACKs), the way X509 handling is - these
176*0a6a1f1dSLionel Sambuc * operations may involve ensuring the hashes are up-to-date and ensuring
177*0a6a1f1dSLionel Sambuc * certain cert information is cached. So this is the point where the
178*0a6a1f1dSLionel Sambuc * "depth-first" constification tree has to halt with an evil cast.
179ebfedea0SLionel Sambuc */
X509_cmp(const X509 * a,const X509 * b)180ebfedea0SLionel Sambuc int X509_cmp(const X509 *a, const X509 *b)
181ebfedea0SLionel Sambuc {
182ebfedea0SLionel Sambuc /* ensure hash is valid */
183ebfedea0SLionel Sambuc X509_check_purpose((X509 *)a, -1, 0);
184ebfedea0SLionel Sambuc X509_check_purpose((X509 *)b, -1, 0);
185ebfedea0SLionel Sambuc
186ebfedea0SLionel Sambuc return memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
187ebfedea0SLionel Sambuc }
188ebfedea0SLionel Sambuc #endif
189ebfedea0SLionel Sambuc
X509_NAME_cmp(const X509_NAME * a,const X509_NAME * b)190ebfedea0SLionel Sambuc int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
191ebfedea0SLionel Sambuc {
192ebfedea0SLionel Sambuc int ret;
193ebfedea0SLionel Sambuc
194ebfedea0SLionel Sambuc /* Ensure canonical encoding is present and up to date */
195ebfedea0SLionel Sambuc
196*0a6a1f1dSLionel Sambuc if (!a->canon_enc || a->modified) {
197ebfedea0SLionel Sambuc ret = i2d_X509_NAME((X509_NAME *)a, NULL);
198ebfedea0SLionel Sambuc if (ret < 0)
199ebfedea0SLionel Sambuc return -2;
200ebfedea0SLionel Sambuc }
201ebfedea0SLionel Sambuc
202*0a6a1f1dSLionel Sambuc if (!b->canon_enc || b->modified) {
203ebfedea0SLionel Sambuc ret = i2d_X509_NAME((X509_NAME *)b, NULL);
204ebfedea0SLionel Sambuc if (ret < 0)
205ebfedea0SLionel Sambuc return -2;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc ret = a->canon_enclen - b->canon_enclen;
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc if (ret)
211ebfedea0SLionel Sambuc return ret;
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
214ebfedea0SLionel Sambuc
215ebfedea0SLionel Sambuc }
216ebfedea0SLionel Sambuc
X509_NAME_hash(X509_NAME * x)217ebfedea0SLionel Sambuc unsigned long X509_NAME_hash(X509_NAME *x)
218ebfedea0SLionel Sambuc {
219ebfedea0SLionel Sambuc unsigned long ret = 0;
220ebfedea0SLionel Sambuc unsigned char md[SHA_DIGEST_LENGTH];
221ebfedea0SLionel Sambuc
222ebfedea0SLionel Sambuc /* Make sure X509_NAME structure contains valid cached encoding */
223ebfedea0SLionel Sambuc i2d_X509_NAME(x, NULL);
224ebfedea0SLionel Sambuc if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
225ebfedea0SLionel Sambuc NULL))
226ebfedea0SLionel Sambuc return 0;
227ebfedea0SLionel Sambuc
228ebfedea0SLionel Sambuc ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
229ebfedea0SLionel Sambuc ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
230ebfedea0SLionel Sambuc ) & 0xffffffffL;
231ebfedea0SLionel Sambuc return (ret);
232ebfedea0SLionel Sambuc }
233ebfedea0SLionel Sambuc
234ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_MD5
235*0a6a1f1dSLionel Sambuc /*
236*0a6a1f1dSLionel Sambuc * I now DER encode the name and hash it. Since I cache the DER encoding,
237*0a6a1f1dSLionel Sambuc * this is reasonably efficient.
238*0a6a1f1dSLionel Sambuc */
239ebfedea0SLionel Sambuc
X509_NAME_hash_old(X509_NAME * x)240ebfedea0SLionel Sambuc unsigned long X509_NAME_hash_old(X509_NAME *x)
241ebfedea0SLionel Sambuc {
242ebfedea0SLionel Sambuc EVP_MD_CTX md_ctx;
243ebfedea0SLionel Sambuc unsigned long ret = 0;
244ebfedea0SLionel Sambuc unsigned char md[16];
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc /* Make sure X509_NAME structure contains valid cached encoding */
247ebfedea0SLionel Sambuc i2d_X509_NAME(x, NULL);
248ebfedea0SLionel Sambuc EVP_MD_CTX_init(&md_ctx);
249ebfedea0SLionel Sambuc EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
250ebfedea0SLionel Sambuc if (EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL)
251ebfedea0SLionel Sambuc && EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length)
252ebfedea0SLionel Sambuc && EVP_DigestFinal_ex(&md_ctx, md, NULL))
253ebfedea0SLionel Sambuc ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
254ebfedea0SLionel Sambuc ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
255ebfedea0SLionel Sambuc ) & 0xffffffffL;
256ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&md_ctx);
257ebfedea0SLionel Sambuc
258ebfedea0SLionel Sambuc return (ret);
259ebfedea0SLionel Sambuc }
260ebfedea0SLionel Sambuc #endif
261ebfedea0SLionel Sambuc
262ebfedea0SLionel Sambuc /* Search a stack of X509 for a match */
X509_find_by_issuer_and_serial(STACK_OF (X509)* sk,X509_NAME * name,ASN1_INTEGER * serial)263ebfedea0SLionel Sambuc X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
264ebfedea0SLionel Sambuc ASN1_INTEGER *serial)
265ebfedea0SLionel Sambuc {
266ebfedea0SLionel Sambuc int i;
267ebfedea0SLionel Sambuc X509_CINF cinf;
268ebfedea0SLionel Sambuc X509 x, *x509 = NULL;
269ebfedea0SLionel Sambuc
270*0a6a1f1dSLionel Sambuc if (!sk)
271*0a6a1f1dSLionel Sambuc return NULL;
272ebfedea0SLionel Sambuc
273ebfedea0SLionel Sambuc x.cert_info = &cinf;
274ebfedea0SLionel Sambuc cinf.serialNumber = serial;
275ebfedea0SLionel Sambuc cinf.issuer = name;
276ebfedea0SLionel Sambuc
277*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_num(sk); i++) {
278ebfedea0SLionel Sambuc x509 = sk_X509_value(sk, i);
279ebfedea0SLionel Sambuc if (X509_issuer_and_serial_cmp(x509, &x) == 0)
280ebfedea0SLionel Sambuc return (x509);
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc return (NULL);
283ebfedea0SLionel Sambuc }
284ebfedea0SLionel Sambuc
X509_find_by_subject(STACK_OF (X509)* sk,X509_NAME * name)285ebfedea0SLionel Sambuc X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
286ebfedea0SLionel Sambuc {
287ebfedea0SLionel Sambuc X509 *x509;
288ebfedea0SLionel Sambuc int i;
289ebfedea0SLionel Sambuc
290*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_num(sk); i++) {
291ebfedea0SLionel Sambuc x509 = sk_X509_value(sk, i);
292ebfedea0SLionel Sambuc if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
293ebfedea0SLionel Sambuc return (x509);
294ebfedea0SLionel Sambuc }
295ebfedea0SLionel Sambuc return (NULL);
296ebfedea0SLionel Sambuc }
297ebfedea0SLionel Sambuc
X509_get_pubkey(X509 * x)298ebfedea0SLionel Sambuc EVP_PKEY *X509_get_pubkey(X509 *x)
299ebfedea0SLionel Sambuc {
300ebfedea0SLionel Sambuc if ((x == NULL) || (x->cert_info == NULL))
301ebfedea0SLionel Sambuc return (NULL);
302ebfedea0SLionel Sambuc return (X509_PUBKEY_get(x->cert_info->key));
303ebfedea0SLionel Sambuc }
304ebfedea0SLionel Sambuc
X509_get0_pubkey_bitstr(const X509 * x)305ebfedea0SLionel Sambuc ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
306ebfedea0SLionel Sambuc {
307*0a6a1f1dSLionel Sambuc if (!x)
308*0a6a1f1dSLionel Sambuc return NULL;
309ebfedea0SLionel Sambuc return x->cert_info->key->public_key;
310ebfedea0SLionel Sambuc }
311ebfedea0SLionel Sambuc
X509_check_private_key(X509 * x,EVP_PKEY * k)312ebfedea0SLionel Sambuc int X509_check_private_key(X509 *x, EVP_PKEY *k)
313ebfedea0SLionel Sambuc {
314ebfedea0SLionel Sambuc EVP_PKEY *xk;
315ebfedea0SLionel Sambuc int ret;
316ebfedea0SLionel Sambuc
317ebfedea0SLionel Sambuc xk = X509_get_pubkey(x);
318ebfedea0SLionel Sambuc
319ebfedea0SLionel Sambuc if (xk)
320ebfedea0SLionel Sambuc ret = EVP_PKEY_cmp(xk, k);
321ebfedea0SLionel Sambuc else
322ebfedea0SLionel Sambuc ret = -2;
323ebfedea0SLionel Sambuc
324*0a6a1f1dSLionel Sambuc switch (ret) {
325ebfedea0SLionel Sambuc case 1:
326ebfedea0SLionel Sambuc break;
327ebfedea0SLionel Sambuc case 0:
328ebfedea0SLionel Sambuc X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
329ebfedea0SLionel Sambuc break;
330ebfedea0SLionel Sambuc case -1:
331ebfedea0SLionel Sambuc X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
332ebfedea0SLionel Sambuc break;
333ebfedea0SLionel Sambuc case -2:
334ebfedea0SLionel Sambuc X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
335ebfedea0SLionel Sambuc }
336ebfedea0SLionel Sambuc if (xk)
337ebfedea0SLionel Sambuc EVP_PKEY_free(xk);
338ebfedea0SLionel Sambuc if (ret > 0)
339ebfedea0SLionel Sambuc return 1;
340ebfedea0SLionel Sambuc return 0;
341ebfedea0SLionel Sambuc }
342