~ubuntu-branches/ubuntu/hardy/openswan/hardy-updates

« back to all changes in this revision

Viewing changes to programs/pluto/x509keys.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2005-01-27 16:10:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050127161011-idgybmyz3vwhpfiq
Tags: 2.3.0-2
Urgency HIGH due to security issue and problems with build-deps in sarge.
* Fix the security issue. Please see
  http://www.idefense.com/application/poi/display?id=190&
      type=vulnerabilities&flashstatus=false
  for more details. Thanks to Martin Schulze for informing me about
  this issue.
  Closes: #292458: Openswan XAUTH/PAM Buffer Overflow Vulnerability
* Added a Build-Dependency to lynx.
  Closes: #291143: openswan: FTBFS: Missing build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Support of X.509 keys
 
2
 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
 
3
 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
 
4
 * Copyright (C) 2002 Mario Strasser
 
5
 * Copyright (C) 2000-2004 Andreas Steffen, Zuercher Hochschule Winterthur
 
6
 * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License as published by the
 
10
 * Free Software Foundation; either version 2 of the License, or (at your
 
11
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
16
 * for more details.
 
17
 *
 
18
 * RCSID $Id: x509keys.c,v 1.4 2004/06/27 22:42:14 mcr Exp $
 
19
 */
 
20
 
 
21
#include <stdlib.h>
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
#include <unistd.h>
 
25
#include <dirent.h>
 
26
#include <time.h>
 
27
#include <sys/types.h>
 
28
 
 
29
#include <openswan.h>
 
30
#include <openswan/ipsec_policy.h>
 
31
 
 
32
#include <sys/queue.h>
 
33
 
 
34
#include "constants.h"
 
35
#include "oswlog.h"
 
36
 
 
37
#include "defs.h"
 
38
#include "log.h"
 
39
#include "id.h"
 
40
#include "asn1.h"
 
41
#include "oid.h"
 
42
#include "x509.h"
 
43
#include "pgp.h"
 
44
#include "certs.h"
 
45
#include "keys.h"
 
46
#include "packet.h"
 
47
#include "demux.h"      /* needs packet.h */
 
48
#include "connections.h"
 
49
#include "state.h"
 
50
#include "md2.h"
 
51
#include "md5.h"
 
52
#include "sha1.h"
 
53
#include "whack.h"
 
54
#include "fetch.h"
 
55
#include "ocsp.h"
 
56
#include "pkcs.h"
 
57
#include "x509more.h"
 
58
#include "paths.h"
 
59
 
 
60
/* extract id and public key from x.509 certificate and
 
61
 * insert it into a pubkeyrec
 
62
 */
 
63
void
 
64
add_x509_public_key(x509cert_t *cert , time_t until
 
65
    , enum dns_auth_level dns_auth_level)
 
66
{
 
67
    generalName_t *gn;
 
68
    struct pubkey *pk;
 
69
    cert_t c = { FALSE, CERT_X509_SIGNATURE, {cert} };
 
70
 
 
71
    /* we support RSA only */
 
72
    if (cert->subjectPublicKeyAlgorithm != PUBKEY_ALG_RSA) return;
 
73
 
 
74
    /* ID type: ID_DER_ASN1_DN  (X.509 subject field) */
 
75
    pk = allocate_RSA_public_key(c);
 
76
    passert(pk != NULL);
 
77
    pk->id.kind = ID_DER_ASN1_DN;
 
78
    pk->id.name = cert->subject;
 
79
    pk->dns_auth_level = dns_auth_level;
 
80
    pk->until_time = until;
 
81
    pk->issuer = cert->issuer;
 
82
    delete_public_keys(&pk->id, pk->alg);
 
83
    install_public_key(pk, &pubkeys);
 
84
 
 
85
    gn = cert->subjectAltName;
 
86
 
 
87
    while (gn != NULL) /* insert all subjectAltNames */
 
88
    {
 
89
        struct id id = empty_id;
 
90
 
 
91
        gntoid(&id, gn);
 
92
        if (id.kind != ID_NONE)
 
93
        {
 
94
            pk = allocate_RSA_public_key(c);
 
95
            pk->id = id;
 
96
            pk->dns_auth_level = dns_auth_level;
 
97
            pk->until_time = until;
 
98
            pk->issuer = cert->issuer;
 
99
            delete_public_keys(&pk->id, pk->alg);
 
100
            install_public_key(pk, &pubkeys);
 
101
        }
 
102
        gn = gn->next;
 
103
    }
 
104
}
 
105
 
 
106
 
 
107
/*  when a X.509 certificate gets revoked, all instances of
 
108
 *  the corresponding public key must be removed
 
109
 */
 
110
void
 
111
remove_x509_public_key(/*const*/ x509cert_t *cert)
 
112
{
 
113
    const cert_t c = {FALSE, CERT_X509_SIGNATURE, {cert}};
 
114
    struct pubkey_list *p, **pp;
 
115
    struct pubkey *revoked_pk;
 
116
 
 
117
    revoked_pk = allocate_RSA_public_key(c);
 
118
    p          = pubkeys;
 
119
    pp         = &pubkeys;
 
120
 
 
121
    while(p != NULL)
 
122
   {
 
123
        if (same_RSA_public_key(&p->key->u.rsa, &revoked_pk->u.rsa))
 
124
        {
 
125
            /* remove p from list and free memory */
 
126
            *pp = free_public_keyentry(p);
 
127
            loglog(RC_LOG_SERIOUS,
 
128
                "invalid RSA public key deleted");
 
129
        }
 
130
        else
 
131
        {
 
132
            pp = &p->next;
 
133
        }
 
134
        p =*pp;
 
135
    }
 
136
    free_public_key(revoked_pk);
 
137
}
 
138
 
 
139
/*
 
140
 * Decode the CERT payload of Phase 1.
 
141
 */
 
142
void
 
143
decode_cert(struct msg_digest *md)
 
144
{
 
145
    struct payload_digest *p;
 
146
 
 
147
    for (p = md->chain[ISAKMP_NEXT_CERT]; p != NULL; p = p->next)
 
148
    {
 
149
        struct isakmp_cert *const cert = &p->payload.cert;
 
150
        chunk_t blob;
 
151
        time_t valid_until;
 
152
        blob.ptr = p->pbs.cur;
 
153
        blob.len = pbs_left(&p->pbs);
 
154
        if (cert->isacert_type == CERT_X509_SIGNATURE)
 
155
        {
 
156
            x509cert_t cert = empty_x509cert;
 
157
            if (parse_x509cert(blob, 0, &cert))
 
158
            {
 
159
                if (verify_x509cert(&cert, strict_crl_policy, &valid_until))
 
160
                {
 
161
                    DBG(DBG_X509 | DBG_PARSING,
 
162
                        DBG_log("Public key validated")
 
163
                    )
 
164
                    add_x509_public_key(&cert, valid_until, DAL_SIGNED);
 
165
                }
 
166
                else
 
167
                {
 
168
                    plog("X.509 certificate rejected");
 
169
                }
 
170
                free_generalNames(cert.subjectAltName, FALSE);
 
171
                free_generalNames(cert.crlDistributionPoints, FALSE);
 
172
            }
 
173
            else
 
174
                plog("Syntax error in X.509 certificate");
 
175
        }
 
176
        else if (cert->isacert_type == CERT_PKCS7_WRAPPED_X509)
 
177
        {
 
178
            x509cert_t *cert = NULL;
 
179
 
 
180
            if (parse_pkcs7_cert(blob, &cert))
 
181
                store_x509certs(&cert, strict_crl_policy);
 
182
            else
 
183
                plog("Syntax error in PKCS#7 wrapped X.509 certificates");
 
184
        }
 
185
        else
 
186
        {
 
187
            loglog(RC_LOG_SERIOUS, "ignoring %s certificate payload",
 
188
                   enum_show(&cert_type_names, cert->isacert_type));
 
189
            DBG_cond_dump_chunk(DBG_PARSING, "CERT:\n", blob);
 
190
        }
 
191
    }
 
192
}
 
193
 
 
194
/*
 
195
 * Decode the CR payload of Phase 1.
 
196
 */
 
197
void
 
198
decode_cr(struct msg_digest *md, generalName_t **requested_ca)
 
199
{
 
200
    struct payload_digest *p;
 
201
 
 
202
    for (p = md->chain[ISAKMP_NEXT_CR]; p != NULL; p = p->next)
 
203
    {
 
204
        struct isakmp_cr *const cr = &p->payload.cr;
 
205
        chunk_t ca_name;
 
206
        
 
207
        ca_name.len = pbs_left(&p->pbs);
 
208
        ca_name.ptr = (ca_name.len > 0)? p->pbs.cur : NULL;
 
209
 
 
210
        DBG_cond_dump_chunk(DBG_PARSING, "CR", ca_name);
 
211
 
 
212
        if (cr->isacr_type == CERT_X509_SIGNATURE)
 
213
        {
 
214
            char buf[IDTOA_BUF];
 
215
 
 
216
            if (ca_name.len > 0)
 
217
            {
 
218
                generalName_t *gn;
 
219
                
 
220
                if (!is_asn1(ca_name))
 
221
                    continue;
 
222
 
 
223
                gn = alloc_thing(generalName_t, "generalName");
 
224
                clonetochunk(ca_name, ca_name.ptr,ca_name.len, "ca name");
 
225
                gn->kind = GN_DIRECTORY_NAME;
 
226
                gn->name = ca_name;
 
227
                gn->next = *requested_ca;
 
228
                *requested_ca = gn;
 
229
            }
 
230
 
 
231
            DBG(DBG_PARSING | DBG_CONTROL,
 
232
                dntoa_or_null(buf, IDTOA_BUF, ca_name, "%any");
 
233
                DBG_log("requested CA: '%s'", buf);
 
234
            )
 
235
        }
 
236
        else
 
237
            loglog(RC_LOG_SERIOUS, "ignoring %s certificate request payload",
 
238
                   enum_show(&cert_type_names, cr->isacr_type));
 
239
    }
 
240
}
 
241
 
 
242
bool
 
243
build_and_ship_CR(u_int8_t type, chunk_t ca, pb_stream *outs, u_int8_t np)
 
244
{
 
245
    pb_stream cr_pbs;
 
246
    struct isakmp_cr cr_hd;
 
247
    cr_hd.isacr_np = np;
 
248
    cr_hd.isacr_type = type;
 
249
 
 
250
    /* build CR header */
 
251
    if (!out_struct(&cr_hd, &isakmp_ipsec_cert_req_desc, outs, &cr_pbs))
 
252
        return FALSE;
 
253
 
 
254
    if (ca.ptr != NULL)
 
255
    {
 
256
        /* build CR body containing the distinguished name of the CA */
 
257
        if (!out_chunk(ca, &cr_pbs, "CA"))
 
258
            return FALSE;
 
259
    }
 
260
    close_output_pbs(&cr_pbs);
 
261
    return TRUE;
 
262
}
 
263
 
 
264
bool
 
265
collect_rw_ca_candidates(struct msg_digest *md, generalName_t **top)
 
266
{
 
267
    struct connection *d = find_host_connection(&md->iface->addr
 
268
        , pluto_port, (ip_address*)NULL, md->sender_port);
 
269
 
 
270
    for (; d != NULL; d = d->hp_next)
 
271
    {
 
272
        /* must be a road warrior connection */
 
273
        if (d->kind == CK_TEMPLATE && !(d->policy & POLICY_OPPO)
 
274
        && d->spd.that.ca.ptr != NULL)
 
275
        {
 
276
            generalName_t *gn;
 
277
            bool new_entry = TRUE;
 
278
 
 
279
            for (gn = *top; gn != NULL; gn = gn->next)
 
280
            {
 
281
                if (same_dn(gn->name, d->spd.that.ca))
 
282
                {
 
283
                    new_entry = FALSE;
 
284
                    break;
 
285
                }
 
286
            }
 
287
            if (new_entry)
 
288
            {
 
289
                gn = alloc_thing(generalName_t, "generalName");
 
290
                gn->kind = GN_DIRECTORY_NAME;
 
291
                gn->name = d->spd.that.ca;
 
292
                gn->next = *top;
 
293
                *top = gn;
 
294
            }
 
295
        }
 
296
    }
 
297
    return *top != NULL;
 
298
}
 
299
 
 
300
/*
 
301
 * Local Variables:
 
302
 * c-basic-offset:4
 
303
 * c-style: pluto
 
304
 * End:
 
305
 */