aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/crypto/ca.c
blob: b40d244cc6564d685e5daab81a13fd558b504be1 (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
/**
 * @file ca.c
 * 
 * @brief Implementation of ca_info_t.
 * 
 */

/*
 * Copyright (C) 2007 Andreas Steffen
 * Hochschule fuer Technik Rapperswil
 *
 * 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.
 */

#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <printf.h>

#include "ca.h"

#include <library.h>
#include <debug.h>
#include <utils/linked_list.h>
#include <utils/identification.h>

typedef struct private_ca_info_t private_ca_info_t;

/**
 * Private data of a ca_info_t object.
 */
struct private_ca_info_t {
	/**
	 * Public interface for this ca info record
	 */
	ca_info_t public;
	
	/**
	 * Name of the ca info record
	 */
	char *name;

	/**
	 * Time when ca info record was installed
	 */
	time_t installed;

	/**
	 * Distinguished Name of the CA
	 */
	identification_t *authName;
	
	/**
	 * Authority Key Identifier
	 */
	chunk_t authKeyID;

	/**
	 * Authority Key Serial Number
	 */
	chunk_t authKeySerialNumber;
	
	/**
	 * List of crlDistributionPoints
	 */
	linked_list_t *crlDistributionPoints;

	/**
	 * List of ocspAccessPoints
	 */
	linked_list_t *ocspAccessPoints;
};

/**
 * Implements ca_info_t.add_crluri
 */
static void add_crluri(private_ca_info_t *this, const char* uri)
{
	if (uri == NULL)
	{
		return;
	}
	if (!strncasecmp(uri, "http", 4)
    &&  !strncasecmp(uri, "ldap", 4)
    &&  !strncasecmp(uri, "file", 4)
	&&  !strncasecmp(uri, "ftp",  3))
	{
		DBG1("  invalid CRL URI: '%s'", uri);
		return;
	}
}

/**
 * Implements ca_info_t.add_ocspuri
 */
static void add_ocspuri(private_ca_info_t *this, const char* uri)
{
	if (uri == NULL)
	{
		return;
	}
	if (!strncasecmp(uri, "http", 4))
	{
		DBG1("  invalid OCSP URI: '%s'", uri);
		return;
	}
}

/**
 * Implements ca_info_t.destroy
 */
static void destroy(private_ca_info_t *this)
{
	this->crlDistributionPoints->destroy_offset(this->crlDistributionPoints,
												offsetof(identification_t, destroy));
	this->ocspAccessPoints->destroy_offset(this->ocspAccessPoints,
												offsetof(identification_t, destroy));
	DESTROY_IF(this->authName);
	free(this->authKeyID.ptr);
	free(this->authKeySerialNumber.ptr);
	free(this->name);
	free(this);
}

/**
 * output handler in printf()
 */
static int print(FILE *stream, const struct printf_info *info,
				 const void *const *args)
{
	private_ca_info_t *this = *((private_ca_info_t**)(args[0]));
	bool utc = TRUE;
	int written = 0;
	time_t now;
	
	if (info->alt)
	{
		utc = *((bool*)args[1]);
	}
	
	if (this == NULL)
	{
		return fprintf(stream, "(null)");
	}
	
	now = time(NULL);
	
	written += fprintf(stream, "%#T, ", &this->installed, utc);
	written += fprintf(stream, "\"%s\"\n", this->name);
	written += fprintf(stream, "    authname:  '%D'\n", this->authName);

	return written;
}

/**
 * register printf() handlers
 */
static void __attribute__ ((constructor))print_register()
{
	register_printf_function(PRINTF_CAINFO, print, arginfo_ptr_alt_ptr_int);
}

/*
 * Described in header.
 */
ca_info_t *ca_info_create(const char *name, const x509_t *cacert)
{
	private_ca_info_t *this = malloc_thing(private_ca_info_t);
	
	/* initialize */
	this->name = strdup(name);
	this->authName = NULL;
	this->authKeyID = chunk_empty;
	this->authKeySerialNumber = chunk_empty;
	this->crlDistributionPoints = linked_list_create();
	this->ocspAccessPoints = linked_list_create();
	
	/* public functions */
	this->public.add_crluri = (void (*) (ca_info_t*,const char*))add_crluri;
	this->public.add_ocspuri = (void (*) (ca_info_t*,const char*))add_ocspuri;
	this->public.destroy = (void (*) (ca_info_t*))destroy;

	return &this->public;
}