]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* crypto/x509/x509_cmp.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
8 | * | |
9 | * This library is free for commercial and non-commercial use as long as | |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
15 | * | |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
38 | * the apps directory (application code) you must include an acknowledgement: | |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
53 | * The licence and distribution terms for any publically available version or | |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <sys/types.h> | |
61 | #include <sys/stat.h> | |
62 | #include "cryptlib.h" | |
63 | #include "asn1.h" | |
64 | #include "objects.h" | |
65 | #include "x509.h" | |
66 | ||
6b691a5c | 67 | int X509_issuer_and_serial_cmp(X509 *a, X509 *b) |
d02b48c6 RE |
68 | { |
69 | int i; | |
70 | X509_CINF *ai,*bi; | |
71 | ||
72 | ai=a->cert_info; | |
73 | bi=b->cert_info; | |
74 | i=ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber); | |
75 | if (i) return(i); | |
76 | return(X509_NAME_cmp(ai->issuer,bi->issuer)); | |
77 | } | |
78 | ||
79 | #ifndef NO_MD5 | |
6b691a5c | 80 | unsigned long X509_issuer_and_serial_hash(X509 *a) |
d02b48c6 RE |
81 | { |
82 | unsigned long ret=0; | |
83 | MD5_CTX ctx; | |
84 | unsigned char md[16]; | |
85 | char str[256]; | |
86 | ||
87 | X509_NAME_oneline(a->cert_info->issuer,str,256); | |
88 | ret=strlen(str); | |
89 | MD5_Init(&ctx); | |
90 | MD5_Update(&ctx,(unsigned char *)str,ret); | |
91 | MD5_Update(&ctx,(unsigned char *)a->cert_info->serialNumber->data, | |
92 | (unsigned long)a->cert_info->serialNumber->length); | |
93 | MD5_Final(&(md[0]),&ctx); | |
94 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| | |
95 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) | |
96 | )&0xffffffffL; | |
97 | return(ret); | |
98 | } | |
99 | #endif | |
100 | ||
6b691a5c | 101 | int X509_issuer_name_cmp(X509 *a, X509 *b) |
d02b48c6 RE |
102 | { |
103 | return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); | |
104 | } | |
105 | ||
6b691a5c | 106 | int X509_subject_name_cmp(X509 *a, X509 *b) |
d02b48c6 RE |
107 | { |
108 | return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); | |
109 | } | |
110 | ||
6b691a5c | 111 | int X509_CRL_cmp(X509_CRL *a, X509_CRL *b) |
d02b48c6 RE |
112 | { |
113 | return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); | |
114 | } | |
115 | ||
6b691a5c | 116 | X509_NAME *X509_get_issuer_name(X509 *a) |
d02b48c6 RE |
117 | { |
118 | return(a->cert_info->issuer); | |
119 | } | |
120 | ||
6b691a5c | 121 | unsigned long X509_issuer_name_hash(X509 *x) |
d02b48c6 RE |
122 | { |
123 | return(X509_NAME_hash(x->cert_info->issuer)); | |
124 | } | |
125 | ||
6b691a5c | 126 | X509_NAME *X509_get_subject_name(X509 *a) |
d02b48c6 RE |
127 | { |
128 | return(a->cert_info->subject); | |
129 | } | |
130 | ||
6b691a5c | 131 | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
d02b48c6 RE |
132 | { |
133 | return(a->cert_info->serialNumber); | |
134 | } | |
135 | ||
6b691a5c | 136 | unsigned long X509_subject_name_hash(X509 *x) |
d02b48c6 RE |
137 | { |
138 | return(X509_NAME_hash(x->cert_info->subject)); | |
139 | } | |
140 | ||
6b691a5c | 141 | int X509_NAME_cmp(X509_NAME *a, X509_NAME *b) |
d02b48c6 RE |
142 | { |
143 | int i,j; | |
144 | X509_NAME_ENTRY *na,*nb; | |
145 | ||
146 | if (sk_num(a->entries) != sk_num(b->entries)) | |
147 | return(sk_num(a->entries)-sk_num(b->entries)); | |
148 | for (i=sk_num(a->entries)-1; i>=0; i--) | |
149 | { | |
150 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | |
151 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | |
152 | j=na->value->length-nb->value->length; | |
153 | if (j) return(j); | |
154 | j=memcmp(na->value->data,nb->value->data, | |
155 | na->value->length); | |
156 | if (j) return(j); | |
157 | j=na->set-nb->set; | |
158 | if (j) return(j); | |
159 | } | |
160 | ||
161 | /* We will check the object types after checking the values | |
162 | * since the values will more often be different than the object | |
163 | * types. */ | |
164 | for (i=sk_num(a->entries)-1; i>=0; i--) | |
165 | { | |
166 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | |
167 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | |
168 | j=OBJ_cmp(na->object,nb->object); | |
169 | if (j) return(j); | |
170 | } | |
171 | return(0); | |
172 | } | |
173 | ||
174 | #ifndef NO_MD5 | |
58964a49 RE |
175 | /* I now DER encode the name and hash it. Since I cache the DER encoding, |
176 | * this is reasonably effiecent. */ | |
6b691a5c | 177 | unsigned long X509_NAME_hash(X509_NAME *x) |
d02b48c6 RE |
178 | { |
179 | unsigned long ret=0; | |
180 | unsigned char md[16]; | |
58964a49 RE |
181 | unsigned char str[256],*p,*pp; |
182 | int i; | |
183 | ||
184 | i=i2d_X509_NAME(x,NULL); | |
185 | if (i > sizeof(str)) | |
186 | p=Malloc(i); | |
187 | else | |
188 | p=str; | |
189 | ||
190 | pp=p; | |
191 | i2d_X509_NAME(x,&pp); | |
192 | MD5((unsigned char *)p,i,&(md[0])); | |
193 | if (p != str) Free(p); | |
d02b48c6 | 194 | |
d02b48c6 RE |
195 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| |
196 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) | |
197 | )&0xffffffffL; | |
198 | return(ret); | |
199 | } | |
200 | #endif | |
201 | ||
202 | /* Search a stack of X509 for a match */ | |
6b691a5c UM |
203 | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, |
204 | ASN1_INTEGER *serial) | |
d02b48c6 RE |
205 | { |
206 | int i; | |
207 | X509_CINF cinf; | |
208 | X509 x,*x509=NULL; | |
209 | ||
210 | x.cert_info= &cinf; | |
211 | cinf.serialNumber=serial; | |
212 | cinf.issuer=name; | |
213 | ||
f73e07cf | 214 | for (i=0; i<sk_X509_num(sk); i++) |
d02b48c6 | 215 | { |
f73e07cf | 216 | x509=sk_X509_value(sk,i); |
d02b48c6 RE |
217 | if (X509_issuer_and_serial_cmp(x509,&x) == 0) |
218 | return(x509); | |
219 | } | |
58964a49 | 220 | return(NULL); |
d02b48c6 RE |
221 | } |
222 | ||
6b691a5c | 223 | X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name) |
d02b48c6 RE |
224 | { |
225 | X509 *x509; | |
226 | int i; | |
227 | ||
f73e07cf | 228 | for (i=0; i<sk_X509_num(sk); i++) |
d02b48c6 | 229 | { |
f73e07cf | 230 | x509=sk_X509_value(sk,i); |
d02b48c6 RE |
231 | if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) |
232 | return(x509); | |
233 | } | |
234 | return(NULL); | |
235 | } | |
236 | ||
6b691a5c | 237 | EVP_PKEY *X509_get_pubkey(X509 *x) |
dfeab068 RE |
238 | { |
239 | if ((x == NULL) || (x->cert_info == NULL)) | |
240 | return(NULL); | |
241 | return(X509_PUBKEY_get(x->cert_info->key)); | |
242 | } | |
243 | ||
6b691a5c | 244 | int X509_check_private_key(X509 *x, EVP_PKEY *k) |
dfeab068 RE |
245 | { |
246 | EVP_PKEY *xk=NULL; | |
247 | int ok=0; | |
248 | ||
249 | xk=X509_get_pubkey(x); | |
4004dbb7 BL |
250 | if (xk->type != k->type) |
251 | { | |
252 | SSLerr(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH); | |
253 | goto err; | |
254 | } | |
dfeab068 RE |
255 | switch (k->type) |
256 | { | |
257 | #ifndef NO_RSA | |
258 | case EVP_PKEY_RSA: | |
4004dbb7 BL |
259 | if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0 |
260 | || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) | |
261 | { | |
262 | SSLerr(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | |
263 | goto err; | |
264 | } | |
dfeab068 RE |
265 | break; |
266 | #endif | |
267 | #ifndef NO_DSA | |
268 | case EVP_PKEY_DSA: | |
269 | if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0) | |
4004dbb7 BL |
270 | { |
271 | SSLerr(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | |
272 | goto err; | |
273 | } | |
dfeab068 RE |
274 | break; |
275 | #endif | |
276 | #ifndef NO_DH | |
277 | case EVP_PKEY_DH: | |
278 | /* No idea */ | |
4004dbb7 | 279 | SSLerr(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY); |
dfeab068 RE |
280 | goto err; |
281 | #endif | |
282 | default: | |
4004dbb7 | 283 | SSLerr(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE); |
dfeab068 RE |
284 | goto err; |
285 | } | |
286 | ||
287 | ok=1; | |
288 | err: | |
10061c7c | 289 | EVP_PKEY_free(xk); |
dfeab068 RE |
290 | return(ok); |
291 | } |