aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/mpt/crypto/exception.hpp
blob: 6e7de510325f0ac0f251f9ec00f6ef5b452e70d5 (plain) (blame)
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
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */

#ifndef MPT_CRYPTO_EXCEPTION_HPP
#define MPT_CRYPTO_EXCEPTION_HPP



#include "mpt/base/detect.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/format/simple.hpp"
#include "mpt/out_of_memory/out_of_memory.hpp"

#include <stdexcept>
#include <string>

#if MPT_OS_WINDOWS
#include <windows.h>  // must be before wincrypt.h for clang-cl
#include <wincrypt.h> // must be before ncrypt.h
#include <ncrypt.h>
#endif // MPT_OS_WINDOWS


namespace mpt {
inline namespace MPT_INLINE_NS {


namespace crypto {



#if MPT_OS_WINDOWS

class exception
	: public std::runtime_error {
private:
	NTSTATUS m_Status;

public:
	exception(NTSTATUS status)
		: std::runtime_error(std::string("crypto error: NTSTATUS ") + mpt::format<std::string>::hex0<8>(static_cast<DWORD>(status)))
		, m_Status(status) {
		return;
	}

	exception(NTSTATUS status, const std::string & function)
		: std::runtime_error(std::string("crypto error: ") + function + std::string(" NTSTATUS ") + mpt::format<std::string>::hex0<8>(static_cast<DWORD>(status)))
		, m_Status(status) {
		return;
	}

public:
	NTSTATUS code() const noexcept {
		return m_Status;
	}
};


class security_exception
	: public std::runtime_error {
private:
	SECURITY_STATUS m_Status;

public:
	security_exception(SECURITY_STATUS status)
		: std::runtime_error(std::string("crypto error: SECURITY_STATUS ") + mpt::format<std::string>::hex0<8>(static_cast<DWORD>(status)))
		, m_Status(status) {
		return;
	}

	security_exception(SECURITY_STATUS status, const std::string & function)
		: std::runtime_error(std::string("crypto error: ") + function + std::string(" SECURITY_STATUS ") + mpt::format<std::string>::hex0<8>(static_cast<DWORD>(status)))
		, m_Status(status) {
		return;
	}

public:
	SECURITY_STATUS code() const noexcept {
		return m_Status;
	}
};


inline void CheckNTSTATUS(NTSTATUS status) {
	if (status >= 0) {
		return;
	} else if (static_cast<DWORD>(status) == STATUS_NO_MEMORY) {
		mpt::throw_out_of_memory();
	} else {
		throw exception(status);
	}
}


inline void CheckNTSTATUS(NTSTATUS status, const std::string & function) {
	if (status >= 0) {
		return;
	} else if (static_cast<DWORD>(status) == STATUS_NO_MEMORY) {
		mpt::throw_out_of_memory();
	} else {
		throw exception(status, function);
	}
}


inline void CheckSECURITY_STATUS(SECURITY_STATUS status) {
	if (status == ERROR_SUCCESS) {
		return;
	} else if (status == NTE_NO_MEMORY) {
		mpt::throw_out_of_memory();
	} else {
		throw security_exception(status);
	}
}


inline void CheckSECURITY_STATUS(SECURITY_STATUS status, const std::string & function) {
	if (status == ERROR_SUCCESS) {
		return;
	} else if (status == NTE_NO_MEMORY) {
		mpt::throw_out_of_memory();
	} else {
		throw security_exception(status, function);
	}
}


#endif // MPT_OS_WINDOWS



} // namespace crypto


} // namespace MPT_INLINE_NS
} // namespace mpt



#endif // MPT_CRYPTO_EXCEPTION_HPP