summaryrefslogtreecommitdiff
path: root/src/pluto/certs.c
blob: 43976a913b566496ae194a267d21bb5e7e78c0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Certificate support for IKE authentication
 * Copyright (C) 2002-2004 Andreas Steffen, Zuercher Hochschule Winterthur
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * RCSID $Id: certs.c 3252 2007-10-06 21:24:50Z andreas $
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <freeswan.h>
#include <ipsec_policy.h>

#include "constants.h"
#include "defs.h"
#include "log.h"
#include "asn1.h"
#include "id.h"
#include "x509.h"
#include "pgp.h"
#include "pem.h"
#include "certs.h"
#include "pkcs1.h"

/*
 * used for initializatin of certs
 */
const cert_t empty_cert = {CERT_NONE, {NULL}};

/*
 * extracts the certificate to be sent to the peer
 */
chunk_t
get_mycert(cert_t cert)
{
    switch (cert.type)
    {
    case CERT_PGP:
	return cert.u.pgp->certificate;
    case CERT_X509_SIGNATURE:
	return cert.u.x509->certificate;
    default:
	return empty_chunk;
    }
}

/* load a coded key or certificate file with autodetection
 * of binary DER or base64 PEM ASN.1 formats and armored PGP format
 */
bool
load_coded_file(const char *filename, prompt_pass_t *pass, const char *type
, chunk_t *blob, bool *pgp)
{
    err_t ugh = NULL;

    FILE *fd = fopen(filename, "r");

    if (fd)
    {
	int bytes;
	fseek(fd, 0, SEEK_END );
	blob->len = ftell(fd);
	rewind(fd);
	blob->ptr = alloc_bytes(blob->len, type);
	bytes = fread(blob->ptr, 1, blob->len, fd);
	fclose(fd);
	plog("  loaded %s file '%s' (%d bytes)", type, filename, bytes);

	*pgp = FALSE;

	/* try DER format */
	if (is_asn1(*blob))
	{
	    DBG(DBG_PARSING,
		DBG_log("  file coded in DER format");
	    )
	    return TRUE;
	}

	/* try PEM format */
	ugh = pemtobin(blob, pass, filename, pgp);

	if (ugh == NULL)
	{
	    if (*pgp)
	    {
                DBG(DBG_PARSING,
                    DBG_log("  file coded in armored PGP format");
                )
                return TRUE;
	    }
	    if (is_asn1(*blob))
	    {
		DBG(DBG_PARSING,
		    DBG_log("  file coded in PEM format");
		)
		return TRUE;
	    }
	    ugh = "file coded in unknown format, discarded";
	}

	/* a conversion error has occured */
	plog("  %s", ugh);
	pfree(blob->ptr);
	*blob = empty_chunk;
    }
    else
    {
	plog("  could not open %s file '%s'", type, filename);
    }
    return FALSE;
}

/*
 *  Loads a PKCS#1 or PGP private RSA key file
 */
err_t
load_rsa_private_key(const char* filename, prompt_pass_t *pass
, RSA_private_key_t *key)
{
    err_t ugh = NULL;
    bool pgp = FALSE;
    chunk_t blob = empty_chunk;

    const char *path = concatenate_paths(PRIVATE_KEY_PATH, filename);

    if (load_coded_file(path, pass, "private key", &blob, &pgp))
    {
	if (pgp)
	{
	    if (!parse_pgp(blob, NULL, key))
		ugh = "syntax error in PGP private key file";
	}
	else
	{
	    if (!pkcs1_parse_private_key(blob, key))
		ugh = "syntax error in PKCS#1 private key file";
	}
	pfree(blob.ptr);
    }
    else
	ugh = "error loading RSA private key file";

    return ugh;
}
/*
 *  Loads a X.509 or OpenPGP certificate
 */
bool
load_cert(const char *filename, const char *label, cert_t *cert)
{
    bool pgp = FALSE;
    chunk_t blob = empty_chunk;

    /* initialize cert struct */
    cert->type = CERT_NONE;
    cert->u.x509 = NULL;

    if (load_coded_file(filename, NULL, label, &blob, &pgp))
    {
	if (pgp)
	{
	    pgpcert_t *pgpcert = alloc_thing(pgpcert_t, "pgpcert");
	    *pgpcert = empty_pgpcert;
	    if (parse_pgp(blob, pgpcert, NULL))
	    {
		cert->type = CERT_PGP;
		cert->u.pgp = pgpcert;
		return TRUE;
	    }
	    else
	    {
		plog("  error in OpenPGP certificate");
		free_pgpcert(pgpcert);
		return FALSE;
	    }
	}
	else
	{
	    x509cert_t *x509cert = alloc_thing(x509cert_t, "x509cert");
	    *x509cert = empty_x509cert;
	    if (parse_x509cert(blob, 0, x509cert))
	    {
		cert->type = CERT_X509_SIGNATURE;
		cert->u.x509 = x509cert;
		return TRUE;
	    }
	    else
	    {
		plog("  error in X.509 certificate");
		free_x509cert(x509cert);
		return FALSE;
	    }
	}
    }
    return FALSE;
}

/*
 *  Loads a host certificate
 */
bool
load_host_cert(const char *filename, cert_t *cert)
{
    const char *path = concatenate_paths(HOST_CERT_PATH, filename);

    return load_cert(path, "host cert", cert);
}

/*
 *  Loads a CA certificate
 */
bool
load_ca_cert(const char *filename, cert_t *cert)
{
    const char *path = concatenate_paths(CA_CERT_PATH, filename);

    return load_cert(path, "CA cert", cert);
}

/*
 * establish equality of two certificates
 */
bool
same_cert(const cert_t *a, const cert_t *b)
{
    return a->type == b->type && a->u.x509 == b->u.x509;
}

/*  for each link pointing to the certif icate
 "  increase the count by one
 */
void
share_cert(cert_t cert)
{
    switch (cert.type)
    {
    case CERT_PGP:
	share_pgpcert(cert.u.pgp);
	break;
    case CERT_X509_SIGNATURE:
	share_x509cert(cert.u.x509);
	break;
    default:
        break;
    }
}

/*  release of a certificate decreases the count by one
 "  the certificate is freed when the counter reaches zero
 */
void
release_cert(cert_t cert)
{
   switch (cert.type)
    {
    case CERT_PGP:
	release_pgpcert(cert.u.pgp);
	break;
    case CERT_X509_SIGNATURE:
	release_x509cert(cert.u.x509);
	break;
    default:
        break;
    }
}

/*
 *  list all X.509 and OpenPGP end certificates
 */
void
list_certs(bool utc)
{
    list_x509_end_certs(utc);
    list_pgp_end_certs(utc);
}