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

#include "config.h"

#include "p11-tests.h"

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

static const char *the_prefix = NULL;

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_msg_print("unknown or invalid CK_RV value: 0x%08X", rv);
		return "";
	}
}

void
p11t_msg_va(const char *message, va_list va)
{
	int len;

	if(the_prefix)
		fprintf(stderr, "%s: ", the_prefix);
	vfprintf(stderr, message, va);
	len = strlen(message);
	if(len && message[len - 1] != '\n')
		fputc('\n', stderr);
	fflush(stderr);
}

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

void
p11t_msg_fatal(const char *message, ...)
{
	va_list va;
	va_start(va, message);
	p11t_msg_va(message, va);
	va_end(va);
	exit(1);
}

void
p11t_msg_prefix(const char *prefix)
{
	the_prefix = prefix;
}