This is the first of two commits (didn't want to dump them all into the
[openssl.git] / crypto / x509v3 / v3_purp.c
1 /* v3_purp.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/x509v3.h>
62
63
64 static void x509v3_cache_extensions(X509 *x);
65
66 static int ca_check(X509 *x);
67 static int check_ssl_ca(X509 *x);
68 static int check_purpose_ssl_client(X509_PURPOSE *xp, X509 *x, int ca);
69 static int check_purpose_ssl_server(X509_PURPOSE *xp, X509 *x, int ca);
70 static int check_purpose_ns_ssl_server(X509_PURPOSE *xp, X509 *x, int ca);
71 static int purpose_smime(X509 *x, int ca);
72 static int check_purpose_smime_sign(X509_PURPOSE *xp, X509 *x, int ca);
73 static int check_purpose_smime_encrypt(X509_PURPOSE *xp, X509 *x, int ca);
74 static int check_purpose_crl_sign(X509_PURPOSE *xp, X509 *x, int ca);
75 static int no_check(X509_PURPOSE *xp, X509 *x, int ca);
76
77 static int xp_cmp(X509_PURPOSE **a, X509_PURPOSE **b);
78 static void xptable_free(X509_PURPOSE *p);
79
80 static X509_PURPOSE xstandard[] = {
81         {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL},
82         {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL},
83         {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
84         {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL},
85         {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
86         {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL},
87         {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", NULL},
88 };
89
90 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
91
92 IMPLEMENT_STACK_OF(X509_PURPOSE)
93
94 static STACK_OF(X509_PURPOSE) *xptable = NULL;
95
96 static int xp_cmp(X509_PURPOSE **a, X509_PURPOSE **b)
97 {
98         return (*a)->purpose - (*b)->purpose;
99 }
100
101 int X509_check_purpose(X509 *x, int id, int ca)
102 {
103         int idx;
104         X509_PURPOSE *pt;
105         if(!(x->ex_flags & EXFLAG_SET)) {
106                 CRYPTO_w_lock(CRYPTO_LOCK_X509);
107                 x509v3_cache_extensions(x);
108                 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
109         }
110         if(id == -1) return 1;
111         idx = X509_PURPOSE_get_by_id(id);
112         if(idx == -1) return -1;
113         pt = X509_PURPOSE_get0(idx);
114         return pt->check_purpose(pt, x, ca);
115 }
116
117 int X509_PURPOSE_get_count(void)
118 {
119         if(!xptable) return X509_PURPOSE_COUNT;
120         return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
121 }
122
123 X509_PURPOSE * X509_PURPOSE_get0(int idx)
124 {
125         if(idx < 0) return NULL;
126         if(idx < X509_PURPOSE_COUNT) return xstandard + idx;
127         return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
128 }
129
130 int X509_PURPOSE_get_by_sname(char *sname)
131 {
132         int i;
133         X509_PURPOSE *xptmp;
134         for(i = 0; i < X509_PURPOSE_get_count(); i++) {
135                 xptmp = X509_PURPOSE_get0(i);
136                 if(!strcmp(xptmp->sname, sname)) return i;
137         }
138         return -1;
139 }
140
141
142 int X509_PURPOSE_get_by_id(int purpose)
143 {
144         X509_PURPOSE tmp;
145         int idx;
146         if((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
147                 return purpose - X509_PURPOSE_MIN;
148         tmp.purpose = purpose;
149         if(!xptable) return -1;
150         idx = sk_X509_PURPOSE_find(xptable, &tmp);
151         if(idx == -1) return -1;
152         return idx + X509_PURPOSE_COUNT;
153 }
154
155 int X509_PURPOSE_add(int id, int trust, int flags,
156                         int (*ck)(X509_PURPOSE *, X509 *, int),
157                                         char *name, char *sname, void *arg)
158 {
159         int idx;
160         X509_PURPOSE *ptmp;
161         /* This is set according to what we change: application can't set it */
162         flags &= ~X509_PURPOSE_DYNAMIC;
163         /* This will always be set for application modified trust entries */
164         flags |= X509_PURPOSE_DYNAMIC_NAME;
165         /* Get existing entry if any */
166         idx = X509_PURPOSE_get_by_id(id);
167         /* Need a new entry */
168         if(idx == -1) {
169                 if(!(ptmp = Malloc(sizeof(X509_PURPOSE)))) {
170                         X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
171                         return 0;
172                 }
173                 ptmp->flags = X509_PURPOSE_DYNAMIC;
174         } else ptmp = X509_PURPOSE_get0(idx);
175
176         /* Free existing name if dynamic */
177         if(ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
178                 Free(ptmp->name);
179                 Free(ptmp->sname);
180         }
181         /* dup supplied name */
182         ptmp->name = BUF_strdup(name);
183         ptmp->sname = BUF_strdup(sname);
184         if(!ptmp->name || !ptmp->sname) {
185                 X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
186                 return 0;
187         }
188         /* Keep the dynamic flag of existing entry */
189         ptmp->flags &= X509_PURPOSE_DYNAMIC;
190         /* Set all other flags */
191         ptmp->flags |= flags;
192
193         ptmp->purpose = id;
194         ptmp->trust = trust;
195         ptmp->check_purpose = ck;
196         ptmp->usr_data = arg;
197
198         /* If its a new entry manage the dynamic table */
199         if(idx == -1) {
200                 if(!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
201                         X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
202                         return 0;
203                 }
204                 if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
205                         X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
206                         return 0;
207                 }
208         }
209         return 1;
210 }
211
212 static void xptable_free(X509_PURPOSE *p)
213         {
214         if(!p) return;
215         if (p->flags & X509_PURPOSE_DYNAMIC) 
216                 {
217                 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
218                         Free(p->name);
219                         Free(p->sname);
220                 }
221                 Free(p);
222                 }
223         }
224
225 void X509_PURPOSE_cleanup(void)
226 {
227         int i;
228         sk_X509_PURPOSE_pop_free(xptable, xptable_free);
229         for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
230         xptable = NULL;
231 }
232
233 int X509_PURPOSE_get_id(X509_PURPOSE *xp)
234 {
235         return xp->purpose;
236 }
237
238 char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
239 {
240         return xp->name;
241 }
242
243 char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
244 {
245         return xp->sname;
246 }
247
248 int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
249 {
250         return xp->trust;
251 }
252
253 #ifndef NO_SHA
254 static void x509v3_cache_extensions(X509 *x)
255 {
256         BASIC_CONSTRAINTS *bs;
257         ASN1_BIT_STRING *usage;
258         ASN1_BIT_STRING *ns;
259         STACK_OF(ASN1_OBJECT) *extusage;
260         int i;
261         if(x->ex_flags & EXFLAG_SET) return;
262         X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
263         /* Does subject name match issuer ? */
264         if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
265                          x->ex_flags |= EXFLAG_SS;
266         /* V1 should mean no extensions ... */
267         if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
268         /* Handle basic constraints */
269         if((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
270                 if(bs->ca) x->ex_flags |= EXFLAG_CA;
271                 if(bs->pathlen) {
272                         if((bs->pathlen->type == V_ASN1_NEG_INTEGER)
273                                                 || !bs->ca) {
274                                 x->ex_flags |= EXFLAG_INVALID;
275                                 x->ex_pathlen = 0;
276                         } else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
277                 } else x->ex_pathlen = -1;
278                 BASIC_CONSTRAINTS_free(bs);
279                 x->ex_flags |= EXFLAG_BCONS;
280         }
281         /* Handle key usage */
282         if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
283                 if(usage->length > 0) {
284                         x->ex_kusage = usage->data[0];
285                         if(usage->length > 1) 
286                                 x->ex_kusage |= usage->data[1] << 8;
287                 } else x->ex_kusage = 0;
288                 x->ex_flags |= EXFLAG_KUSAGE;
289                 ASN1_BIT_STRING_free(usage);
290         }
291         x->ex_xkusage = 0;
292         if((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
293                 x->ex_flags |= EXFLAG_XKUSAGE;
294                 for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
295                         switch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {
296                                 case NID_server_auth:
297                                 x->ex_xkusage |= XKU_SSL_SERVER;
298                                 break;
299
300                                 case NID_client_auth:
301                                 x->ex_xkusage |= XKU_SSL_CLIENT;
302                                 break;
303
304                                 case NID_email_protect:
305                                 x->ex_xkusage |= XKU_SMIME;
306                                 break;
307
308                                 case NID_code_sign:
309                                 x->ex_xkusage |= XKU_CODE_SIGN;
310                                 break;
311
312                                 case NID_ms_sgc:
313                                 case NID_ns_sgc:
314                                 x->ex_xkusage |= XKU_SGC;
315                         }
316                 }
317                 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
318         }
319
320         if((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
321                 if(ns->length > 0) x->ex_nscert = ns->data[0];
322                 else x->ex_nscert = 0;
323                 x->ex_flags |= EXFLAG_NSCERT;
324                 ASN1_BIT_STRING_free(ns);
325         }
326         x->ex_flags |= EXFLAG_SET;
327 }
328 #endif
329
330 /* CA checks common to all purposes
331  * return codes:
332  * 0 not a CA
333  * 1 is a CA
334  * 2 basicConstraints absent so "maybe" a CA
335  * 3 basicConstraints absent but self signed V1.
336  */
337
338 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
339 #define ku_reject(x, usage) \
340         (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
341 #define xku_reject(x, usage) \
342         (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
343 #define ns_reject(x, usage) \
344         (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
345
346 static int ca_check(X509 *x)
347 {
348         /* keyUsage if present should allow cert signing */
349         if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
350         if(x->ex_flags & EXFLAG_BCONS) {
351                 if(x->ex_flags & EXFLAG_CA) return 1;
352                 /* If basicConstraints says not a CA then say so */
353                 else return 0;
354         } else {
355                 if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
356                 else return 2;
357         }
358 }
359
360 /* Check SSL CA: common checks for SSL client and server */
361 static int check_ssl_ca(X509 *x)
362 {
363         int ca_ret;
364         ca_ret = ca_check(x);
365         if(!ca_ret) return 0;
366         /* check nsCertType if present */
367         if(x->ex_flags & EXFLAG_NSCERT) {
368                 if(x->ex_nscert & NS_SSL_CA) return ca_ret;
369                 return 0;
370         }
371         if(ca_ret != 2) return ca_ret;
372         else return 0;
373 }
374         
375
376 static int check_purpose_ssl_client(X509_PURPOSE *xp, X509 *x, int ca)
377 {
378         if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
379         if(ca) return check_ssl_ca(x);
380         /* We need to do digital signatures with it */
381         if(ku_reject(x,KU_DIGITAL_SIGNATURE)) return 0;
382         /* nsCertType if present should allow SSL client use */ 
383         if(ns_reject(x, NS_SSL_CLIENT)) return 0;
384         return 1;
385 }
386
387 static int check_purpose_ssl_server(X509_PURPOSE *xp, X509 *x, int ca)
388 {
389         if(xku_reject(x,XKU_SSL_SERVER|XKU_SGC)) return 0;
390         if(ca) return check_ssl_ca(x);
391
392         if(ns_reject(x, NS_SSL_SERVER)) return 0;
393         /* Now as for keyUsage: we'll at least need to sign OR encipher */
394         if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT)) return 0;
395         
396         return 1;
397
398 }
399
400 static int check_purpose_ns_ssl_server(X509_PURPOSE *xp, X509 *x, int ca)
401 {
402         int ret;
403         ret = check_purpose_ssl_server(xp, x, ca);
404         if(!ret || ca) return ret;
405         /* We need to encipher or Netscape complains */
406         if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
407         return ret;
408 }
409
410 /* common S/MIME checks */
411 static int purpose_smime(X509 *x, int ca)
412 {
413         if(xku_reject(x,XKU_SMIME)) return 0;
414         if(ca) {
415                 int ca_ret;
416                 ca_ret = ca_check(x);
417                 if(!ca_ret) return 0;
418                 /* check nsCertType if present */
419                 if(x->ex_flags & EXFLAG_NSCERT) {
420                         if(x->ex_nscert & NS_SMIME_CA) return ca_ret;
421                         return 0;
422                 }
423                 if(ca_ret != 2) return ca_ret;
424                 else return 0;
425         }
426         if(x->ex_flags & EXFLAG_NSCERT) {
427                 if(x->ex_nscert & NS_SMIME) return 1;
428                 /* Workaround for some buggy certificates */
429                 if(x->ex_nscert & NS_SSL_CLIENT) return 2;
430                 return 0;
431         }
432         return 1;
433 }
434
435 static int check_purpose_smime_sign(X509_PURPOSE *xp, X509 *x, int ca)
436 {
437         int ret;
438         ret = purpose_smime(x, ca);
439         if(!ret || ca) return ret;
440         if(ku_reject(x, KU_DIGITAL_SIGNATURE)) return 0;
441         return ret;
442 }
443
444 static int check_purpose_smime_encrypt(X509_PURPOSE *xp, X509 *x, int ca)
445 {
446         int ret;
447         ret = purpose_smime(x, ca);
448         if(!ret || ca) return ret;
449         if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
450         return ret;
451 }
452
453 static int check_purpose_crl_sign(X509_PURPOSE *xp, X509 *x, int ca)
454 {
455         if(ca) {
456                 int ca_ret;
457                 if((ca_ret = ca_check(x)) != 2) return ca_ret;
458                 else return 0;
459         }
460         if(ku_reject(x, KU_CRL_SIGN)) return 0;
461         return 1;
462 }
463
464 static int no_check(X509_PURPOSE *xp, X509 *x, int ca)
465 {
466         return 1;
467 }