diff options
Diffstat (limited to 'src/lib/libcrypto/x509/x509_cmp.c')
-rw-r--r-- | src/lib/libcrypto/x509/x509_cmp.c | 138 |
1 files changed, 87 insertions, 51 deletions
diff --git a/src/lib/libcrypto/x509/x509_cmp.c b/src/lib/libcrypto/x509/x509_cmp.c index f9d9510ac5..9a93bae3ff 100644 --- a/src/lib/libcrypto/x509/x509_cmp.c +++ b/src/lib/libcrypto/x509/x509_cmp.c | |||
@@ -60,13 +60,11 @@ | |||
60 | #include <sys/types.h> | 60 | #include <sys/types.h> |
61 | #include <sys/stat.h> | 61 | #include <sys/stat.h> |
62 | #include "cryptlib.h" | 62 | #include "cryptlib.h" |
63 | #include "asn1.h" | 63 | #include <openssl/asn1.h> |
64 | #include "objects.h" | 64 | #include <openssl/objects.h> |
65 | #include "x509.h" | 65 | #include <openssl/x509.h> |
66 | 66 | ||
67 | int X509_issuer_and_serial_cmp(a,b) | 67 | int X509_issuer_and_serial_cmp(X509 *a, X509 *b) |
68 | X509 *a; | ||
69 | X509 *b; | ||
70 | { | 68 | { |
71 | int i; | 69 | int i; |
72 | X509_CINF *ai,*bi; | 70 | X509_CINF *ai,*bi; |
@@ -79,8 +77,7 @@ X509 *b; | |||
79 | } | 77 | } |
80 | 78 | ||
81 | #ifndef NO_MD5 | 79 | #ifndef NO_MD5 |
82 | unsigned long X509_issuer_and_serial_hash(a) | 80 | unsigned long X509_issuer_and_serial_hash(X509 *a) |
83 | X509 *a; | ||
84 | { | 81 | { |
85 | unsigned long ret=0; | 82 | unsigned long ret=0; |
86 | MD5_CTX ctx; | 83 | MD5_CTX ctx; |
@@ -101,70 +98,59 @@ X509 *a; | |||
101 | } | 98 | } |
102 | #endif | 99 | #endif |
103 | 100 | ||
104 | int X509_issuer_name_cmp(a, b) | 101 | int X509_issuer_name_cmp(X509 *a, X509 *b) |
105 | X509 *a; | ||
106 | X509 *b; | ||
107 | { | 102 | { |
108 | return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); | 103 | return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); |
109 | } | 104 | } |
110 | 105 | ||
111 | int X509_subject_name_cmp(a, b) | 106 | int X509_subject_name_cmp(X509 *a, X509 *b) |
112 | X509 *a; | ||
113 | X509 *b; | ||
114 | { | 107 | { |
115 | return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); | 108 | return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); |
116 | } | 109 | } |
117 | 110 | ||
118 | int X509_CRL_cmp(a, b) | 111 | int X509_CRL_cmp(X509_CRL *a, X509_CRL *b) |
119 | X509_CRL *a; | ||
120 | X509_CRL *b; | ||
121 | { | 112 | { |
122 | return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); | 113 | return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); |
123 | } | 114 | } |
124 | 115 | ||
125 | X509_NAME *X509_get_issuer_name(a) | 116 | X509_NAME *X509_get_issuer_name(X509 *a) |
126 | X509 *a; | ||
127 | { | 117 | { |
128 | return(a->cert_info->issuer); | 118 | return(a->cert_info->issuer); |
129 | } | 119 | } |
130 | 120 | ||
131 | unsigned long X509_issuer_name_hash(x) | 121 | unsigned long X509_issuer_name_hash(X509 *x) |
132 | X509 *x; | ||
133 | { | 122 | { |
134 | return(X509_NAME_hash(x->cert_info->issuer)); | 123 | return(X509_NAME_hash(x->cert_info->issuer)); |
135 | } | 124 | } |
136 | 125 | ||
137 | X509_NAME *X509_get_subject_name(a) | 126 | X509_NAME *X509_get_subject_name(X509 *a) |
138 | X509 *a; | ||
139 | { | 127 | { |
140 | return(a->cert_info->subject); | 128 | return(a->cert_info->subject); |
141 | } | 129 | } |
142 | 130 | ||
143 | ASN1_INTEGER *X509_get_serialNumber(a) | 131 | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
144 | X509 *a; | ||
145 | { | 132 | { |
146 | return(a->cert_info->serialNumber); | 133 | return(a->cert_info->serialNumber); |
147 | } | 134 | } |
148 | 135 | ||
149 | unsigned long X509_subject_name_hash(x) | 136 | unsigned long X509_subject_name_hash(X509 *x) |
150 | X509 *x; | ||
151 | { | 137 | { |
152 | return(X509_NAME_hash(x->cert_info->subject)); | 138 | return(X509_NAME_hash(x->cert_info->subject)); |
153 | } | 139 | } |
154 | 140 | ||
155 | int X509_NAME_cmp(a, b) | 141 | int X509_NAME_cmp(X509_NAME *a, X509_NAME *b) |
156 | X509_NAME *a; | ||
157 | X509_NAME *b; | ||
158 | { | 142 | { |
159 | int i,j; | 143 | int i,j; |
160 | X509_NAME_ENTRY *na,*nb; | 144 | X509_NAME_ENTRY *na,*nb; |
161 | 145 | ||
162 | if (sk_num(a->entries) != sk_num(b->entries)) | 146 | if (sk_X509_NAME_ENTRY_num(a->entries) |
163 | return(sk_num(a->entries)-sk_num(b->entries)); | 147 | != sk_X509_NAME_ENTRY_num(b->entries)) |
164 | for (i=sk_num(a->entries)-1; i>=0; i--) | 148 | return sk_X509_NAME_ENTRY_num(a->entries) |
149 | -sk_X509_NAME_ENTRY_num(b->entries); | ||
150 | for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) | ||
165 | { | 151 | { |
166 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | 152 | na=sk_X509_NAME_ENTRY_value(a->entries,i); |
167 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | 153 | nb=sk_X509_NAME_ENTRY_value(b->entries,i); |
168 | j=na->value->length-nb->value->length; | 154 | j=na->value->length-nb->value->length; |
169 | if (j) return(j); | 155 | if (j) return(j); |
170 | j=memcmp(na->value->data,nb->value->data, | 156 | j=memcmp(na->value->data,nb->value->data, |
@@ -177,10 +163,10 @@ X509_NAME *b; | |||
177 | /* We will check the object types after checking the values | 163 | /* We will check the object types after checking the values |
178 | * since the values will more often be different than the object | 164 | * since the values will more often be different than the object |
179 | * types. */ | 165 | * types. */ |
180 | for (i=sk_num(a->entries)-1; i>=0; i--) | 166 | for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) |
181 | { | 167 | { |
182 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | 168 | na=sk_X509_NAME_ENTRY_value(a->entries,i); |
183 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | 169 | nb=sk_X509_NAME_ENTRY_value(b->entries,i); |
184 | j=OBJ_cmp(na->object,nb->object); | 170 | j=OBJ_cmp(na->object,nb->object); |
185 | if (j) return(j); | 171 | if (j) return(j); |
186 | } | 172 | } |
@@ -190,8 +176,7 @@ X509_NAME *b; | |||
190 | #ifndef NO_MD5 | 176 | #ifndef NO_MD5 |
191 | /* I now DER encode the name and hash it. Since I cache the DER encoding, | 177 | /* I now DER encode the name and hash it. Since I cache the DER encoding, |
192 | * this is reasonably effiecent. */ | 178 | * this is reasonably effiecent. */ |
193 | unsigned long X509_NAME_hash(x) | 179 | unsigned long X509_NAME_hash(X509_NAME *x) |
194 | X509_NAME *x; | ||
195 | { | 180 | { |
196 | unsigned long ret=0; | 181 | unsigned long ret=0; |
197 | unsigned char md[16]; | 182 | unsigned char md[16]; |
@@ -217,10 +202,8 @@ X509_NAME *x; | |||
217 | #endif | 202 | #endif |
218 | 203 | ||
219 | /* Search a stack of X509 for a match */ | 204 | /* Search a stack of X509 for a match */ |
220 | X509 *X509_find_by_issuer_and_serial(sk,name,serial) | 205 | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, |
221 | STACK *sk; | 206 | ASN1_INTEGER *serial) |
222 | X509_NAME *name; | ||
223 | ASN1_INTEGER *serial; | ||
224 | { | 207 | { |
225 | int i; | 208 | int i; |
226 | X509_CINF cinf; | 209 | X509_CINF cinf; |
@@ -230,28 +213,81 @@ ASN1_INTEGER *serial; | |||
230 | cinf.serialNumber=serial; | 213 | cinf.serialNumber=serial; |
231 | cinf.issuer=name; | 214 | cinf.issuer=name; |
232 | 215 | ||
233 | for (i=0; i<sk_num(sk); i++) | 216 | for (i=0; i<sk_X509_num(sk); i++) |
234 | { | 217 | { |
235 | x509=(X509 *)sk_value(sk,i); | 218 | x509=sk_X509_value(sk,i); |
236 | if (X509_issuer_and_serial_cmp(x509,&x) == 0) | 219 | if (X509_issuer_and_serial_cmp(x509,&x) == 0) |
237 | return(x509); | 220 | return(x509); |
238 | } | 221 | } |
239 | return(NULL); | 222 | return(NULL); |
240 | } | 223 | } |
241 | 224 | ||
242 | X509 *X509_find_by_subject(sk,name) | 225 | X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name) |
243 | STACK *sk; | ||
244 | X509_NAME *name; | ||
245 | { | 226 | { |
246 | X509 *x509; | 227 | X509 *x509; |
247 | int i; | 228 | int i; |
248 | 229 | ||
249 | for (i=0; i<sk_num(sk); i++) | 230 | for (i=0; i<sk_X509_num(sk); i++) |
250 | { | 231 | { |
251 | x509=(X509 *)sk_value(sk,i); | 232 | x509=sk_X509_value(sk,i); |
252 | if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) | 233 | if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) |
253 | return(x509); | 234 | return(x509); |
254 | } | 235 | } |
255 | return(NULL); | 236 | return(NULL); |
256 | } | 237 | } |
257 | 238 | ||
239 | EVP_PKEY *X509_get_pubkey(X509 *x) | ||
240 | { | ||
241 | if ((x == NULL) || (x->cert_info == NULL)) | ||
242 | return(NULL); | ||
243 | return(X509_PUBKEY_get(x->cert_info->key)); | ||
244 | } | ||
245 | |||
246 | int X509_check_private_key(X509 *x, EVP_PKEY *k) | ||
247 | { | ||
248 | EVP_PKEY *xk=NULL; | ||
249 | int ok=0; | ||
250 | |||
251 | xk=X509_get_pubkey(x); | ||
252 | if (xk->type != k->type) | ||
253 | { | ||
254 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH); | ||
255 | goto err; | ||
256 | } | ||
257 | switch (k->type) | ||
258 | { | ||
259 | #ifndef NO_RSA | ||
260 | case EVP_PKEY_RSA: | ||
261 | if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0 | ||
262 | || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) | ||
263 | { | ||
264 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | ||
265 | goto err; | ||
266 | } | ||
267 | break; | ||
268 | #endif | ||
269 | #ifndef NO_DSA | ||
270 | case EVP_PKEY_DSA: | ||
271 | if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0) | ||
272 | { | ||
273 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | ||
274 | goto err; | ||
275 | } | ||
276 | break; | ||
277 | #endif | ||
278 | #ifndef NO_DH | ||
279 | case EVP_PKEY_DH: | ||
280 | /* No idea */ | ||
281 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY); | ||
282 | goto err; | ||
283 | #endif | ||
284 | default: | ||
285 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE); | ||
286 | goto err; | ||
287 | } | ||
288 | |||
289 | ok=1; | ||
290 | err: | ||
291 | EVP_PKEY_free(xk); | ||
292 | return(ok); | ||
293 | } | ||