summaryrefslogtreecommitdiff
path: root/src/msg.c
blob: a0a4d6f0ac93a1562e71f565fcf0c3be16e858f3 (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

#include "p11-tests.h"
#include "p11-tests-lib.h"

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

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x400
#include <windows.h>
#else
#include <errno.h>
#endif

#include <openssl/err.h>

/* Where we log to */
P11TestsLogFunc p11t_log_func = NULL;

static const char *the_prefix = "";

const char*
p11t_msg_rv(CK_RV rv)
{
	switch(rv) {
	#define CT(x)	case x: return #x;
	CT(CKR_OK)
	CT(CKR_CANCEL)
	CT(CKR_HOST_MEMORY)
	CT(CKR_SLOT_ID_INVALID)
	CT(CKR_GENERAL_ERROR)
	CT(CKR_FUNCTION_FAILED)
	CT(CKR_ARGUMENTS_BAD)
	CT(CKR_NO_EVENT)
	CT(CKR_NEED_TO_CREATE_THREADS)
	CT(CKR_CANT_LOCK)
	CT(CKR_ATTRIBUTE_READ_ONLY)
	CT(CKR_ATTRIBUTE_SENSITIVE)
	CT(CKR_ATTRIBUTE_TYPE_INVALID)
	CT(CKR_ATTRIBUTE_VALUE_INVALID)
	CT(CKR_DATA_INVALID)
	CT(CKR_DATA_LEN_RANGE)
	CT(CKR_DEVICE_ERROR)
	CT(CKR_DEVICE_MEMORY)
	CT(CKR_DEVICE_REMOVED)
	CT(CKR_ENCRYPTED_DATA_INVALID)
	CT(CKR_ENCRYPTED_DATA_LEN_RANGE)
	CT(CKR_FUNCTION_CANCELED)
	CT(CKR_FUNCTION_NOT_PARALLEL)
	CT(CKR_FUNCTION_NOT_SUPPORTED)
	CT(CKR_KEY_HANDLE_INVALID)
	CT(CKR_KEY_SIZE_RANGE)
	CT(CKR_KEY_TYPE_INCONSISTENT)
	CT(CKR_KEY_NOT_NEEDED)
	CT(CKR_KEY_CHANGED)
	CT(CKR_KEY_NEEDED)
	CT(CKR_KEY_INDIGESTIBLE)
	CT(CKR_KEY_FUNCTION_NOT_PERMITTED)
	CT(CKR_KEY_NOT_WRAPPABLE)
	CT(CKR_KEY_UNEXTRACTABLE)
	CT(CKR_MECHANISM_INVALID)
	CT(CKR_MECHANISM_PARAM_INVALID)
	CT(CKR_OBJECT_HANDLE_INVALID)
	CT(CKR_OPERATION_ACTIVE)
	CT(CKR_OPERATION_NOT_INITIALIZED)
	CT(CKR_PIN_INCORRECT)
	CT(CKR_PIN_INVALID)
	CT(CKR_PIN_LEN_RANGE)
	CT(CKR_PIN_EXPIRED)
	CT(CKR_PIN_LOCKED)
	CT(CKR_SESSION_CLOSED)
	CT(CKR_SESSION_COUNT)
	CT(CKR_SESSION_HANDLE_INVALID)
	CT(CKR_SESSION_PARALLEL_NOT_SUPPORTED)
	CT(CKR_SESSION_READ_ONLY)
	CT(CKR_SESSION_EXISTS)
	CT(CKR_SESSION_READ_ONLY_EXISTS)
	CT(CKR_SESSION_READ_WRITE_SO_EXISTS)
	CT(CKR_SIGNATURE_INVALID)
	CT(CKR_SIGNATURE_LEN_RANGE)
	CT(CKR_TEMPLATE_INCOMPLETE)
	CT(CKR_TEMPLATE_INCONSISTENT)
	CT(CKR_TOKEN_NOT_PRESENT)
	CT(CKR_TOKEN_NOT_RECOGNIZED)
	CT(CKR_TOKEN_WRITE_PROTECTED)
	CT(CKR_UNWRAPPING_KEY_HANDLE_INVALID)
	CT(CKR_UNWRAPPING_KEY_SIZE_RANGE)
	CT(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT)
	CT(CKR_USER_ALREADY_LOGGED_IN)
	CT(CKR_USER_NOT_LOGGED_IN)
	CT(CKR_USER_PIN_NOT_INITIALIZED)
	CT(CKR_USER_TYPE_INVALID)
	CT(CKR_USER_ANOTHER_ALREADY_LOGGED_IN)
	CT(CKR_USER_TOO_MANY_TYPES)
	CT(CKR_WRAPPED_KEY_INVALID)
	CT(CKR_WRAPPED_KEY_LEN_RANGE)
	CT(CKR_WRAPPING_KEY_HANDLE_INVALID)
	CT(CKR_WRAPPING_KEY_SIZE_RANGE)
	CT(CKR_WRAPPING_KEY_TYPE_INCONSISTENT)
	CT(CKR_RANDOM_SEED_NOT_SUPPORTED)
	CT(CKR_RANDOM_NO_RNG)
	CT(CKR_DOMAIN_PARAMS_INVALID)
	CT(CKR_BUFFER_TOO_SMALL)
	CT(CKR_SAVED_STATE_INVALID)
	CT(CKR_INFORMATION_SENSITIVE)
	CT(CKR_STATE_UNSAVEABLE)
	CT(CKR_CRYPTOKI_NOT_INITIALIZED)
	CT(CKR_CRYPTOKI_ALREADY_INITIALIZED)
	CT(CKR_MUTEX_BAD)
	CT(CKR_MUTEX_NOT_LOCKED)
	CT(CKR_FUNCTION_REJECTED)
	CT(CKR_VENDOR_DEFINED)
	#undef CT
	default:
		p11t_check_warn("unknown or invalid CK_RV value encountered: 0x%08X", rv);
		return "";
	}
}

#ifdef _WIN32

static char last_error[1024];

const char*
p11t_msg_os(void)
{
	LPVOID lpMsgBuf;
	DWORD error = GetLastError();
	DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
	                            FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, error,
	                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
								(LPTSTR) &lpMsgBuf, 0, NULL);
	if(dwRet && lpMsgBuf)
	{
		strncpy(last_error, lpMsgBuf, 1024);
		LocalFree(lpMsgBuf);
	}
	else
	{
		_snprintf(last_error, 1024, "unknown error: 0x%08x", (int)error);
	}

	return last_error;
}

#else /* _WIN32 */

const char*
p11t_msg_os(void)
{
	return strerror(errno);
}

#endif /* _WIN32 */

const char*
p11t_msg_openssl(void)
{
	return ERR_error_string(ERR_get_error(), NULL);
}

static void
default_log_func (int level, const char *section, const char *message)
{
	const char *code;

	switch (level) {
	case P11_TESTS_NONE:
		code = NULL;
		break;
	case P11_TESTS_INFO:
		code = "INFO";
		break;
	case P11_TESTS_WARN:
		code = "WARN";
		break;
	case P11_TESTS_FAIL:
		code = "FAIL";
		break;
	default:
		assert (0 && "should not be reached");
	}

	if (code)
		fprintf (stdout, "%s: ", code);
	if (section && section[0])
		fprintf (stdout, "%s: ", section);
	fprintf (stdout, "%s\n", message);
	fflush(stdout);
}

void
p11t_msg_va (int level, const char *message, va_list va)
{
	char buffer[4096];

	vsnprintf (buffer, sizeof (buffer), message, va);
	if (p11t_log_func)
		p11t_log_func (level, the_prefix, buffer);
	else
		default_log_func (level, the_prefix, buffer);
}

void
p11t_msg_print (const char *message, ...)
{
	va_list va;
	va_start(va, message);
	p11t_msg_va (P11_TESTS_NONE, message, va);
	va_end(va);
}

const char*
p11t_msg_prefix (const char *prefix)
{
	const char *old = the_prefix;
	if (prefix == NULL)
		prefix = "";
	the_prefix = prefix;
	return old;
}