replaced get_rsa_private_key() by rsa_signature() in order restrict the distribution...
[strongswan.git] / src / libstrongswan / library.c
1 /**
2 * @file library.c
3 *
4 * @brief Helper functions and definitions.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <string.h>
25 #include <time.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29
30 #include "library.h"
31
32 #include <printf_hook.h>
33
34 ENUM(status_names, SUCCESS, DESTROY_ME,
35 "SUCCESS",
36 "FAILED",
37 "OUT_OF_RES",
38 "ALREADY_DONE",
39 "NOT_SUPPORTED",
40 "INVALID_ARG",
41 "NOT_FOUND",
42 "PARSE_ERROR",
43 "VERIFY_ERROR",
44 "INVALID_STATE",
45 "DESTROY_ME",
46 "NEED_MORE",
47 );
48
49 /**
50 * Described in header.
51 */
52 void *clalloc(void * pointer, size_t size)
53 {
54 void *data;
55 data = malloc(size);
56
57 memcpy(data, pointer,size);
58
59 return (data);
60 }
61
62 /**
63 * Described in header.
64 */
65 void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
66 {
67 size_t i;
68 for (i = 0; i < n; i++)
69 {
70 dest[i] ^= src[i];
71 }
72 }
73
74 /**
75 * We use a single mutex for all refcount variables. This
76 * is not optimal for performance, but the critical section
77 * is not that long...
78 * TODO: Consider to include a mutex in each refcount_t variable.
79 */
80 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
81
82 /**
83 * Described in header.
84 *
85 * TODO: May be implemented with atomic CPU instructions
86 * instead of a mutex.
87 */
88 void ref_get(refcount_t *ref)
89 {
90 pthread_mutex_lock(&ref_mutex);
91 (*ref)++;
92 pthread_mutex_unlock(&ref_mutex);
93 }
94
95 /**
96 * Described in header.
97 *
98 * TODO: May be implemented with atomic CPU instructions
99 * instead of a mutex.
100 */
101 bool ref_put(refcount_t *ref)
102 {
103 bool more_refs;
104
105 pthread_mutex_lock(&ref_mutex);
106 more_refs = --(*ref);
107 pthread_mutex_unlock(&ref_mutex);
108 return !more_refs;
109 }
110
111 /**
112 * output handler in printf() for time_t
113 */
114 static int print_time(FILE *stream, const struct printf_info *info,
115 const void *const *args)
116 {
117 static const char* months[] = {
118 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
119 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
120 };
121 time_t *time = *((time_t**)(args[0]));
122 bool utc = TRUE;
123 struct tm t;
124
125 if (info->alt)
126 {
127 utc = *((bool*)(args[1]));
128 }
129 if (time == UNDEFINED_TIME)
130 {
131 return fprintf(stream, "--- -- --:--:--%s----",
132 info->alt ? " UTC " : " ");
133 }
134 if (utc)
135 {
136 gmtime_r(time, &t);
137 }
138 else
139 {
140 localtime_r(time, &t);
141 }
142 return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
143 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
144 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
145 }
146
147 /**
148 * output handler in printf() for time deltas
149 */
150 static int print_time_delta(FILE *stream, const struct printf_info *info,
151 const void *const *args)
152 {
153 time_t *start = *((time_t**)(args[0]));
154 time_t *end = *((time_t**)(args[1]));
155 u_int delta = abs(*end - *start);
156
157 char* unit = "second";
158
159 if (delta > 2 * 60 * 60 * 24)
160 {
161 delta /= 60 * 60 * 24;
162 unit = "day";
163 }
164 else if (delta > 2 * 60 * 60)
165 {
166 delta /= 60 * 60;
167 unit = "hour";
168 }
169 else if (delta > 2 * 60)
170 {
171 delta /= 60;
172 unit = "minute";
173 }
174 return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
175 }
176
177 /**
178 * register printf() handlers for time_t
179 */
180 static void __attribute__ ((constructor))print_register()
181 {
182 register_printf_function(PRINTF_TIME, print_time, arginfo_ptr_alt_ptr_int);
183 register_printf_function(PRINTF_TIME_DELTA, print_time_delta, arginfo_ptr_ptr);
184 }