summaryrefslogtreecommitdiff
path: root/src/manager/common/openssl-error-handler.cpp
blob: e8649c104c02852c5cd52c432aa47df54e214863 (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
/*
 *  Copyright (c) 2017 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *  @file   openssl-error-handler.cpp
 *  @author Pawel Kowalski (p.kowalski2@partner.samsung.com)
 */

#include <string.h>

#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/dsa.h>

#include <ckm/ckm-error.h>
#include <dpl/log/log.h>

#include "openssl-error-handler.h"
#include <exception>

#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define OPENSSL_SUCCESS 1

namespace CKM {

static const int GENERIC_REASON_MAX = 99;

#define ERRORDESCRIBE(name) case name: return #name
const char *ckm_debug_translate_error(int err)
{
	switch (err) {
	ERRORDESCRIBE(CKM_API_SUCCESS);
	ERRORDESCRIBE(CKM_API_ERROR_INPUT_PARAM);
	ERRORDESCRIBE(CKM_API_ERROR_OUT_OF_MEMORY);
	ERRORDESCRIBE(CKM_API_ERROR_SERVER_ERROR);
	ERRORDESCRIBE(CKM_API_ERROR_AUTHENTICATION_FAILED);
	ERRORDESCRIBE(CKM_API_ERROR_VERIFICATION_FAILED);
	default: return "Error not defined";
	}
}
#undef ERRORDESCRIBE

void errorDump()
{
	typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr;
	BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
	if (!bio.get())
		return;

	ERR_print_errors(bio.get());

	std::vector<char> message(1024);
	int len = BIO_read(bio.get(), message.data(), message.size());
	if (len > 0) {
		message.resize(len);
		LogError(std::string(message.begin(), message.end()));
	}
}

void errorHandle(const char *file, int line, const char *function, int openssl_ret)
{
	if (openssl_ret >= OPENSSL_SUCCESS)
		return;

	int ret = CKM_API_SUCCESS;

	unsigned long err = ERR_peek_error();

	if (err == 0)
		ret = CKM_API_ERROR_SERVER_ERROR;

	/* known errors */
	switch (err) {
#if OPENSSL_VERSION_NUMBER > 0x10100000L
	case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
	case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
#else /* OPENSSL_VERSION_NUMBER > 0x10100000L */
	case ERR_PACK(ERR_LIB_RSA, RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS):
	case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
	case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
	case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
#endif /* OPENSSL_VERSION_NUMBER > 0x10100000L */
	case ERR_PACK(ERR_LIB_RSA, RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION):
	case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE):
	case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS):
	case ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE):
	case ERR_PACK(ERR_LIB_DSA, DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE):
	case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG):
	case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG):
	case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT):
	case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT):
	case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ):
	case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ):
	case ERR_PACK(ERR_LIB_PEM, PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ):
		ret = CKM_API_ERROR_INPUT_PARAM;
		break;
	case ERR_PACK(ERR_LIB_X509, X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY):
	case ERR_PACK(ERR_LIB_X509, X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_VERIFYFINAL, EVP_R_WRONG_PUBLIC_KEY_TYPE):
	case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_VERIFYFINAL, EVP_R_NO_VERIFY_FUNCTION_CONFIGURED):
		ret = CKM_API_ERROR_VERIFICATION_FAILED;
		break;
	}

	/* known rsa padding errors */
	if (ret == CKM_API_SUCCESS && ERR_GET_LIB(err) == ERR_LIB_RSA) {
		switch (ERR_GET_FUNC(err)) {
		case RSA_F_CHECK_PADDING_MD:
		case RSA_F_RSA_PADDING_CHECK_NONE:
		case RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP:
		case RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1:
		case RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1:
		case RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2:
		case RSA_F_RSA_PADDING_CHECK_SSLV23:
		case RSA_F_RSA_PADDING_CHECK_X931:
		case RSA_F_RSA_PADDING_ADD_NONE:
		case RSA_F_RSA_PADDING_ADD_PKCS1_OAEP:
		case RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1:
		case RSA_F_RSA_PADDING_ADD_PKCS1_PSS:
		case RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1:
		case RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1:
		case RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2:
		case RSA_F_RSA_PADDING_ADD_SSLV23:
		case RSA_F_RSA_PADDING_ADD_X931:
			ret = CKM_API_ERROR_INPUT_PARAM;
			break;
		}
	}

	/* fatal errors */
	int reason = ERR_GET_REASON(err);
	if (ret == CKM_API_SUCCESS && reason <= GENERIC_REASON_MAX && (err & ERR_R_FATAL) > 0) {
		switch (reason) {
		case ERR_R_MALLOC_FAILURE:
			ret = CKM_API_ERROR_OUT_OF_MEMORY;
			break;
		case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
		case ERR_R_PASSED_NULL_PARAMETER:
			ret = CKM_API_ERROR_INPUT_PARAM;
			break;
		case ERR_R_INTERNAL_ERROR:
		case ERR_R_DISABLED:
			ret = CKM_API_ERROR_SERVER_ERROR;
			break;
		}
	}

	/* neither known nor fatal, unknown */
	if (ret == CKM_API_SUCCESS) {
		errorDump();
	}

	/* remove all errors from queue */
	ERR_clear_error();

	switch(ret) {
	case CKM_API_ERROR_INPUT_PARAM:
		throw CKM::Exc::InputParam(file, function, line, "");
	case CKM_API_ERROR_OUT_OF_MEMORY:
		throw CKM::Exc::InternalError(file, function, line, "Out of memory");
	case CKM_API_ERROR_SERVER_ERROR:
		throw CKM::Exc::InternalError(file, function, line, "");
	case CKM_API_ERROR_AUTHENTICATION_FAILED:
		throw CKM::Exc::AuthenticationFailed(file, function, line, "");
	case CKM_API_ERROR_VERIFICATION_FAILED:
		throw CKM::Exc::VerificationFailed(file, function, line, "");
	default:
		throw CKM::Exc::InternalError(file, function, line, "Error not described");
	}

}

} // namespace CKM