Skip to content

Commit e60a335

Browse files
authored
Update error handling and debug logging (#92)
* Add RSA fallback test * Update error function codes to match refactor * Surface SymCrypt errors from provider * Allow logging configuration * Add debug logging to keysinuse init * Add RSA and ECC keysinuse debug logs * PR comments * Add FIPS compliance logging to unpadded RSA encrypt/decrypt * PR comments * Create separate provider loggin calls without func code
1 parent a8e9f21 commit e60a335

38 files changed

+836
-623
lines changed

Diff for: ScosslCommon/inc/scossl_helpers.h

+122-70
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef _Return_type_success_(return >= 0) int SCOSSL_RETURNLENGTH; // For funct
4343
// pointer before using it with SymCrypt.
4444
//
4545
// In the SCOSSL provider, it is our responsibility to perform the allocation and free ourselves.
46-
// Here we allocate SYMCRYPT_ALIGN_VALUE extra bytes, and store the offset into our allocation in the byte before the aligned
46+
// Here we allocate SYMCRYPT_ALIGN_VALUE extra bytes, and store the offset into our allocation in the byte before the aligned
4747
// pointer we use in SymCrypt. On free, we look at the byte before the aligned pointer we have been using, to determine the start
4848
// of the allocation and free it correctly.
4949
//
@@ -91,62 +91,94 @@ void scossl_destroy_logging();
9191

9292
// SCOSSL function codes
9393
typedef enum {
94+
// ScosslCommon
9495
SCOSSL_ERR_F_ENUM_START= 100,
9596
SCOSSL_ERR_F_AES_CCM_CIPHER,
96-
SCOSSL_ERR_F_AES_CCM_CTRL,
97+
SCOSSL_ERR_F_AES_CCM_SET_IV_FIXED,
98+
SCOSSL_ERR_F_AES_CCM_SET_IV_LEN,
99+
SCOSSL_ERR_F_AES_CCM_SET_TLS1_AAD,
97100
SCOSSL_ERR_F_AES_CCM_TLS,
98-
SCOSSL_ERR_F_AES_GCM_CTRL,
101+
SCOSSL_ERR_F_AES_GCM_CIPHER,
102+
SCOSSL_ERR_F_AES_GCM_IV_GEN,
103+
SCOSSL_ERR_F_AES_GCM_SET_IV_FIXED,
104+
SCOSSL_ERR_F_AES_GCM_SET_IV_INV,
105+
SCOSSL_ERR_F_AES_GCM_SET_IV_LEN,
106+
SCOSSL_ERR_F_AES_GCM_SET_TLS1_AAD,
99107
SCOSSL_ERR_F_AES_GCM_TLS,
100-
SCOSSL_ERR_F_AES_XTS_CIPHER,
101-
SCOSSL_ERR_F_AES_XTS_CTRL,
102-
SCOSSL_ERR_F_DH_COMPUTE_KEY,
103-
SCOSSL_ERR_F_DH_GENERATE_KEY,
104108
SCOSSL_ERR_F_DH_GENERATE_KEYPAIR,
109+
SCOSSL_ERR_F_DH_GET_GROUP_BY_NID,
105110
SCOSSL_ERR_F_DH_IMPORT_KEYPAIR,
106-
SCOSSL_ERR_F_DIGESTS,
107-
SCOSSL_ERR_F_ECC_GENERATE_KEYPAIR,
108-
SCOSSL_ERR_F_ECC_IMPORT_KEYPAIR,
111+
SCOSSL_ERR_F_ECC_GROUP_TO_SYMCRYPT_CURVE,
112+
SCOSSL_ERR_F_ECC_POINT_TO_PUBKEY,
109113
SCOSSL_ERR_F_ECDSA_APPLY_DER,
110114
SCOSSL_ERR_F_ECDSA_DER_CHECK_TAG_AND_GET_VALUE_AND_LENGTH,
111115
SCOSSL_ERR_F_ECDSA_REMOVE_DER,
112-
SCOSSL_ERR_F_ECKEY_COMPUTE_KEY,
113-
SCOSSL_ERR_F_ECKEY_KEYGEN,
114-
SCOSSL_ERR_F_ECKEY_SIGN,
115-
SCOSSL_ERR_F_ECKEY_SIGN_SETUP,
116-
SCOSSL_ERR_F_ECKEY_SIGN_SIG,
117-
SCOSSL_ERR_F_ECKEY_VERIFY,
118-
SCOSSL_ERR_F_ECKEY_VERIFY_SIG,
119-
SCOSSL_ERR_F_GET_DH_CONTEXT_EX,
120-
SCOSSL_ERR_F_GET_ECC_CONTEXT_EX,
116+
SCOSSL_ERR_F_ECDSA_SIGN,
117+
SCOSSL_ERR_F_ECDSA_VERIFY,
121118
SCOSSL_ERR_F_GET_SYMCRYPT_HASH_ALGORITHM,
122119
SCOSSL_ERR_F_GET_SYMCRYPT_MAC_ALGORITHM,
123-
SCOSSL_ERR_F_HKDF_CTRL,
124120
SCOSSL_ERR_F_HKDF_DERIVE,
125-
SCOSSL_ERR_F_HKDF_INIT,
126-
SCOSSL_ERR_F_INITIALIZE_RSA_KEY,
127-
SCOSSL_ERR_F_PKEY_METHODS,
128-
SCOSSL_ERR_F_PKEY_RSA_SIGN,
129-
SCOSSL_ERR_F_PKEY_RSA_VERIFY,
130-
SCOSSL_ERR_F_RSA_INIT,
131-
SCOSSL_ERR_F_RSA_KEYGEN,
132-
SCOSSL_ERR_F_RSA_PRIV_DEC,
133-
SCOSSL_ERR_F_RSA_PRIV_ENC,
134-
SCOSSL_ERR_F_RSA_PUB_DEC,
135-
SCOSSL_ERR_F_RSA_PUB_ENC,
136-
SCOSSL_ERR_F_RSA_SIGN,
137-
SCOSSL_ERR_F_RSA_VERIFY,
121+
SCOSSL_ERR_F_MAC_INIT,
122+
SCOSSL_ERR_F_MAC_SET_HMAC_MD,
123+
SCOSSL_ERR_F_RSA_DECRYPT,
124+
SCOSSL_ERR_F_RSA_ENCRYPT,
125+
SCOSSL_ERR_F_RSA_EXPORT_KEY,
126+
SCOSSL_ERR_F_RSA_NEW_EXPORT_PARAMS,
127+
SCOSSL_ERR_F_RSA_PKCS1_SIGN,
128+
SCOSSL_ERR_F_RSA_PKCS1_VERIFY,
138129
SCOSSL_ERR_F_RSAPSS_SIGN,
139130
SCOSSL_ERR_F_RSAPSS_VERIFY,
140-
SCOSSL_ERR_F_TLS1PRF_CTRL,
141-
SCOSSL_ERR_F_TLS1PRF_DERIVE,
142-
SCOSSL_ERR_F_TLS1PRF_INIT,
143-
SCOSSL_ERR_F_HMAC_INIT,
144-
SCOSSL_ERR_F_HMAC_CTRL,
145-
SCOSSL_ERR_F_HMAC_CTRL_STR,
146-
SCOSSL_ERR_F_SSHKDF_NEW,
147-
SCOSSL_ERR_F_SSHKDF_CTRL,
148-
SCOSSL_ERR_F_SSHKDF_CTRL_STR,
149131
SCOSSL_ERR_F_SSHKDF_DERIVE,
132+
SCOSSL_ERR_F_TLS1PRF_DERIVE,
133+
// SymCryptEngine
134+
SCOSSL_ERR_F_ENG_AES_CCM_CTRL,
135+
SCOSSL_ERR_F_ENG_AES_GCM_CTRL,
136+
SCOSSL_ERR_F_ENG_AES_XTS_CIPHER,
137+
SCOSSL_ERR_F_ENG_AES_XTS_CTRL,
138+
SCOSSL_ERR_F_ENG_DH_COMPUTE_KEY,
139+
SCOSSL_ERR_F_ENG_DH_GENERATE_KEY,
140+
SCOSSL_ERR_F_ENG_DH_GENERATE_KEYPAIR,
141+
SCOSSL_ERR_F_ENG_DH_IMPORT_KEYPAIR,
142+
SCOSSL_ERR_F_ENG_DIGESTS,
143+
SCOSSL_ERR_F_ENG_ECC_GENERATE_KEYPAIR,
144+
SCOSSL_ERR_F_ENG_ECC_IMPORT_KEYPAIR,
145+
SCOSSL_ERR_F_ENG_ECKEY_COMPUTE_KEY,
146+
SCOSSL_ERR_F_ENG_ECKEY_KEYGEN,
147+
SCOSSL_ERR_F_ENG_ECKEY_SIGN,
148+
SCOSSL_ERR_F_ENG_ECKEY_SIGN_SETUP,
149+
SCOSSL_ERR_F_ENG_ECKEY_SIGN_SIG,
150+
SCOSSL_ERR_F_ENG_ECKEY_VERIFY,
151+
SCOSSL_ERR_F_ENG_ECKEY_VERIFY_SIG,
152+
SCOSSL_ERR_F_ENG_GET_DH_CONTEXT_EX,
153+
SCOSSL_ERR_F_ENG_GET_ECC_CONTEXT_EX,
154+
SCOSSL_ERR_F_ENG_GET_SYMCRYPT_HASH_ALGORITHM,
155+
SCOSSL_ERR_F_ENG_HKDF_CTRL,
156+
SCOSSL_ERR_F_ENG_HKDF_DERIVE,
157+
SCOSSL_ERR_F_ENG_HKDF_INIT,
158+
SCOSSL_ERR_F_ENG_HMAC_COPY,
159+
SCOSSL_ERR_F_ENG_HMAC_CTRL,
160+
SCOSSL_ERR_F_ENG_HMAC_INIT,
161+
SCOSSL_ERR_F_ENG_INITIALIZE_RSA_KEY,
162+
SCOSSL_ERR_F_ENG_PKEY_METHODS,
163+
SCOSSL_ERR_F_ENG_PKEY_RSA_SIGN,
164+
SCOSSL_ERR_F_ENG_PKEY_RSA_VERIFY,
165+
SCOSSL_ERR_F_ENG_PKEY_RSAPSS_VERIFY,
166+
SCOSSL_ERR_F_ENG_RSA_INIT,
167+
SCOSSL_ERR_F_ENG_RSA_KEYGEN,
168+
SCOSSL_ERR_F_ENG_RSA_PRIV_DEC,
169+
SCOSSL_ERR_F_ENG_RSA_PRIV_ENC,
170+
SCOSSL_ERR_F_ENG_RSA_PUB_DEC,
171+
SCOSSL_ERR_F_ENG_RSA_PUB_ENC,
172+
SCOSSL_ERR_F_ENG_RSA_SIGN,
173+
SCOSSL_ERR_F_ENG_RSA_VERIFY,
174+
SCOSSL_ERR_F_ENG_RSAPSS_SIGN,
175+
SCOSSL_ERR_F_ENG_RSAPSS_VERIFY,
176+
SCOSSL_ERR_F_ENG_SSHKDF_CTRL,
177+
SCOSSL_ERR_F_ENG_SSHKDF_CTRL_STR,
178+
SCOSSL_ERR_F_ENG_SSHKDF_DERIVE,
179+
SCOSSL_ERR_F_ENG_SSHKDF_NEW,
180+
SCOSSL_ERR_F_ENG_TLS1PRF_CTRL,
181+
SCOSSL_ERR_F_ENG_TLS1PRF_INIT,
150182
SCOSSL_ERR_F_ENUM_END
151183
} SCOSSL_ERR_FUNC;
152184

@@ -158,6 +190,7 @@ typedef enum {
158190
SCOSSL_ERR_R_NOT_FIPS_ALGORITHM,
159191
SCOSSL_ERR_R_OPENSSL_FALLBACK,
160192
SCOSSL_ERR_R_SYMCRYPT_FAILURE,
193+
SCOSSL_ERR_R_KEYSINUSE_FAILURE,
161194
SCOSSL_ERR_R_ENUM_END
162195
} SCOSSL_ERR_REASON;
163196

@@ -179,19 +212,9 @@ void _scossl_log_bytes(
179212
int len,
180213
const char *format, ...);
181214

182-
void _scossl_log_bignum(
183-
int trace_level,
184-
SCOSSL_ERR_FUNC func_code,
185-
SCOSSL_ERR_REASON reason_code,
186-
const char *file,
187-
int line,
188-
char *description,
189-
BIGNUM *bn);
190-
191215
void _scossl_log_SYMCRYPT_ERROR(
192216
int trace_level,
193217
SCOSSL_ERR_FUNC func_code,
194-
SCOSSL_ERR_REASON reason_code,
195218
const char *file,
196219
int line,
197220
char *description,
@@ -211,26 +234,18 @@ void _scossl_log_SYMCRYPT_ERROR(
211234
#define SCOSSL_LOG_BYTES_INFO(func_code, reason_code, description, s, len) \
212235
_scossl_log_bytes(SCOSSL_LOG_LEVEL_INFO, func_code, reason_code, __FILE__, __LINE__, (const char*) s, len, description)
213236

214-
#define SCOSSL_LOG_BIGNUM_DEBUG(func_code, reason_code, description, bn) \
215-
_scossl_log_bignum(SCOSSL_LOG_LEVEL_DEBUG, func_code, reason_code, __FILE__, __LINE__, description, bn)
216-
217-
#define SCOSSL_LOG_BIGNUM_INFO(func_code, reason_code, description, s, len) \
218-
_scossl_log_bignum(SCOSSL_LOG_LEVEL_INFO, func_code, reason_code, __FILE__, __LINE__, description, bn)
237+
#define SCOSSL_LOG_SYMCRYPT_DEBUG(func_code, description, scError) \
238+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_DEBUG, func_code, __FILE__, __LINE__, description, scError)
219239

220-
#define SCOSSL_LOG_SYMCRYPT_DEBUG(func_code, reason_code, description, scError) \
221-
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_DEBUG, func_code, reason_code, __FILE__, __LINE__, description, scError)
222-
223-
#define SCOSSL_LOG_SYMCRYPT_INFO(func_code, reason_code, description, scError) \
224-
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_INFO, func_code, reason_code, __FILE__, __LINE__, description, scError)
240+
#define SCOSSL_LOG_SYMCRYPT_INFO(func_code, description, scError) \
241+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_INFO, func_code, __FILE__, __LINE__, description, scError)
225242
#else
226243
#define SCOSSL_LOG_DEBUG(func_code, reason_code, ...)
227244
#define SCOSSL_LOG_INFO(func_code, reason_code, ...)
228245
#define SCOSSL_LOG_BYTES_DEBUG(func_code, reason_code, description, s, len)
229246
#define SCOSSL_LOG_BYTES_INFO(func_code, reason_code, description, s, len)
230-
#define SCOSSL_LOG_BIGNUM_DEBUG(func_code, reason_code, description, bn)
231-
#define SCOSSL_LOG_BIGNUM_INFO(func_code, reason_code, description, s, len)
232-
#define SCOSSL_LOG_SYMCRYPT_DEBUG(func_code, reason_code, description, scError)
233-
#define SCOSSL_LOG_SYMCRYPT_INFO(func_code, reason_code, description, scError)
247+
#define SCOSSL_LOG_SYMCRYPT_DEBUG(func_code, description, scError)
248+
#define SCOSSL_LOG_SYMCRYPT_INFO(func_code, description, scError)
234249
#endif
235250

236251
#define SCOSSL_LOG_ERROR(func_code, reason_code, ...) \
@@ -239,11 +254,48 @@ void _scossl_log_SYMCRYPT_ERROR(
239254
#define SCOSSL_LOG_BYTES_ERROR(func_code, reason_code, description, s, len) \
240255
_scossl_log_bytes(SCOSSL_LOG_LEVEL_ERROR, func_code, reason_code, __FILE__, __LINE__, (const char*) s, len, description)
241256

242-
#define SCOSSL_LOG_BIGNUM_ERROR(func_code, reason_code, description, s, len) \
243-
_scossl_log_bignum(SCOSSL_LOG_LEVEL_ERROR, func_code, reason_code, __FILE__, __LINE__, description, bn)
257+
#define SCOSSL_LOG_SYMCRYPT_ERROR(func_code, description, scError) \
258+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_ERROR, func_code, __FILE__, __LINE__, description, scError)
259+
260+
#if OPENSSL_API_LEVEL >= 30000
261+
// Enable debug and info messages in debug builds, but compile them out in release builds
262+
#if DBG
263+
#define SCOSSL_PROV_LOG_DEBUG(reason_code, ...) \
264+
_scossl_log(SCOSSL_LOG_LEVEL_DEBUG, 0, reason_code, __FILE__, __LINE__, __VA_ARGS__)
265+
266+
#define SCOSSL_PROV_LOG_INFO(reason_code, ...) \
267+
_scossl_log(SCOSSL_LOG_LEVEL_INFO, 0, reason_code, __FILE__, __LINE__, __VA_ARGS__)
268+
269+
#define SCOSSL_PROV_LOG_BYTES_DEBUG(reason_code, description, s, len) \
270+
_scossl_log_bytes(SCOSSL_LOG_LEVEL_DEBUG, 0, reason_code, __FILE__, __LINE__, (const char*) s, len, description)
271+
272+
#define SCOSSL_PROV_LOG_BYTES_INFO(reason_code, description, s, len) \
273+
_scossl_log_bytes(SCOSSL_LOG_LEVEL_INFO, 0, reason_code, __FILE__, __LINE__, (const char*) s, len, description)
274+
275+
#define SCOSSL_PROV_LOG_SYMCRYPT_DEBUG(description, scError) \
276+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_DEBUG, 0, __FILE__, __LINE__, description, scError)
244277

245-
#define SCOSSL_LOG_SYMCRYPT_ERROR(func_code, reason_code, description, scError) \
246-
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_ERROR, func_code, reason_code, __FILE__, __LINE__, description, scError)
278+
#define SCOSSL_PROV_LOG_SYMCRYPT_INFO(description, scError) \
279+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_INFO, 0, __FILE__, __LINE__, description, scError)
280+
#else
281+
#define SCOSSL_PROV_LOG_DEBUG(reason_code, ...)
282+
#define SCOSSL_PROV_LOG_INFO(reason_code, ...)
283+
#define SCOSSL_PROV_LOG_BYTES_DEBUG(reason_code, description, s, len)
284+
#define SCOSSL_PROV_LOG_BYTES_INFO(reason_code, description, s, len)
285+
#define SCOSSL_PROV_LOG_SYMCRYPT_DEBUG(description, scError)
286+
#define SCOSSL_PROV_LOG_SYMCRYPT_INFO(description, scError)
287+
#endif
288+
289+
#define SCOSSL_PROV_LOG_ERROR(reason_code, ...) \
290+
_scossl_log(SCOSSL_LOG_LEVEL_ERROR, 0, reason_code, __FILE__, __LINE__, __VA_ARGS__)
291+
292+
#define SCOSSL_PROV_LOG_BYTES_ERROR(reason_code, description, s, len) \
293+
_scossl_log_bytes(SCOSSL_LOG_LEVEL_ERROR, 0, reason_code, __FILE__, __LINE__, (const char*) s, len, description)
294+
295+
#define SCOSSL_PROV_LOG_SYMCRYPT_ERROR(description, scError) \
296+
_scossl_log_SYMCRYPT_ERROR(SCOSSL_LOG_LEVEL_ERROR, 0, __FILE__, __LINE__, description, scError)
297+
298+
#endif
247299

248300
//
249301
// Common helper functions

0 commit comments

Comments
 (0)