Skip to content

Commit 4efe34b

Browse files
committed
Issue 65: Bring patches from amlogic and realtek into secapi2-adapter
Reason for change: Bring patches to secapi2-adapter Risks: None Priority: P1
1 parent 6dfd27f commit 4efe34b

File tree

3 files changed

+140
-18
lines changed

3 files changed

+140
-18
lines changed

include/sec_security.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ Sec_Result SecCipher_ProcessOpaqueWithMapAndPattern(Sec_CipherHandle* cipherHand
997997
SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE numEncryptedBlocks,
998998
SEC_SIZE numClearBlocks, Sec_OpaqueBufferHandle** opaqueBufferHandle, SEC_SIZE* bytesWritten);
999999

1000+
Sec_Result SecCipher_ProcessOpaqueWithMapAndHandle(Sec_CipherHandle* cipherHandle, SEC_BYTE* iv, uint32_t secureBufferHandle,
1001+
SEC_BYTE* input, SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE* bytesWritten);
1002+
10001003
#ifdef __cplusplus
10011004
}
10021005
#endif

src/sec_adapter_cipher.c

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "sa_cenc.h"
2222
#include <stdbool.h>
2323

24+
2425
#define RSA_OAEP_PADDING_SIZE 42
2526

2627
struct Sec_CipherHandle_struct {
@@ -951,7 +952,7 @@ Sec_Result SecCipher_ProcessOpaqueWithMap(Sec_CipherHandle* cipherHandle, SEC_BY
951952
if (result != SEC_RESULT_SUCCESS) {
952953
SEC_LOG_ERROR("SecOpaqueBuffer_Malloc failed");
953954
return SEC_RESULT_FAILURE;
954-
}
955+
}
955956

956957
sa_subsample_length* subsample_lengths = malloc(mapLength * sizeof(sa_subsample_length));
957958
for (size_t i = 0; i < mapLength; i++) {
@@ -991,6 +992,7 @@ Sec_Result SecCipher_ProcessOpaqueWithMap(Sec_CipherHandle* cipherHandle, SEC_BY
991992
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) 1, &sample);
992993
SEC_FREE(subsample_lengths);
993994
if (status != SA_STATUS_OK) {
995+
SEC_LOG_ERROR("Failed process encryption");
994996
SecOpaqueBuffer_Free(*opaqueBufferHandle);
995997
*opaqueBufferHandle = NULL;
996998
*bytesWritten = 0;
@@ -1095,6 +1097,111 @@ Sec_Result SecCipher_ProcessOpaqueWithMapAndPattern(Sec_CipherHandle* cipherHand
10951097
return SEC_RESULT_SUCCESS;
10961098
}
10971099

1100+
Sec_Result SecCipher_ProcessOpaqueWithMapAndHandle(Sec_CipherHandle* cipherHandle, SEC_BYTE* iv, uint32_t secureBufferHandle,
1101+
SEC_BYTE* input, SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE* bytesWritten) {
1102+
1103+
if (cipherHandle == NULL) {
1104+
SEC_LOG_ERROR("NULL cipherHandle");
1105+
return SEC_RESULT_FAILURE;
1106+
}
1107+
1108+
if (iv == NULL) {
1109+
SEC_LOG_ERROR("NULL iv");
1110+
return SEC_RESULT_FAILURE;
1111+
}
1112+
1113+
if (secureBufferHandle == 0) {
1114+
SEC_LOG_ERROR("Buffer Handle is 0");
1115+
return SEC_RESULT_FAILURE;
1116+
}
1117+
1118+
if (map == NULL) {
1119+
SEC_LOG_ERROR("NULL map");
1120+
return SEC_RESULT_FAILURE;
1121+
}
1122+
1123+
if (bytesWritten == NULL) {
1124+
SEC_LOG_ERROR("NULL bytesWritten");
1125+
return SEC_RESULT_FAILURE;
1126+
}
1127+
1128+
// wrap in a secapi2 adapter buffer
1129+
Sec_OpaqueBufferHandle* opaqueBufferHandle = NULL;
1130+
Sec_Result result = SecOpaqueBuffer_Create(&opaqueBufferHandle, (void*) secureBufferHandle, inputSize);
1131+
if (result != SEC_RESULT_SUCCESS) {
1132+
SEC_LOG_ERROR("SecOpaqueBuffer_Create failed with preallocated memory: %" PRIu32 "", secureBufferHandle);
1133+
return SEC_RESULT_FAILURE;
1134+
}
1135+
1136+
sa_subsample_length* subsample_lengths = malloc(mapLength * sizeof(sa_subsample_length));
1137+
for (size_t i = 0; i < mapLength; i++) {
1138+
subsample_lengths[i].bytes_of_clear_data = map[i].clear;
1139+
subsample_lengths[i].bytes_of_protected_data = map[i].encrypted;
1140+
}
1141+
1142+
sa_buffer out_buffer;
1143+
out_buffer.buffer_type = SA_BUFFER_TYPE_SVP;
1144+
out_buffer.context.svp.offset = 0;
1145+
out_buffer.context.svp.buffer = get_svp_buffer(cipherHandle->processorHandle, opaqueBufferHandle);
1146+
if (out_buffer.context.svp.buffer == INVALID_HANDLE) {
1147+
free(subsample_lengths);
1148+
SEC_LOG_ERROR("Failed create buffer from handle: %" PRIu32 "", secureBufferHandle);
1149+
1150+
// Memory is pre-allocated elsewhere so we don't wan't to free it, just
1151+
// release our handle to it.
1152+
Sec_ProtectedMemHandle* h = NULL;
1153+
SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1154+
opaqueBufferHandle = NULL;
1155+
*bytesWritten = 0;
1156+
return SEC_RESULT_FAILURE;
1157+
}
1158+
1159+
sa_buffer in_buffer;
1160+
in_buffer.buffer_type = SA_BUFFER_TYPE_CLEAR;
1161+
in_buffer.context.clear.buffer = input;
1162+
in_buffer.context.clear.length = inputSize;
1163+
in_buffer.context.clear.offset = 0;
1164+
1165+
sa_sample sample;
1166+
sample.iv = iv;
1167+
sample.iv_length = SEC_AES_BLOCK_SIZE;
1168+
sample.crypt_byte_block = 0;
1169+
sample.skip_byte_block = 0;
1170+
sample.subsample_count = mapLength;
1171+
sample.subsample_lengths = subsample_lengths;
1172+
sample.context = cipherHandle->cipher.context;
1173+
sample.out = &out_buffer;
1174+
sample.in = &in_buffer;
1175+
1176+
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) 1, &sample);
1177+
free(subsample_lengths);
1178+
1179+
if (status != SA_STATUS_OK) {
1180+
SEC_LOG_ERROR("Failed process encryption");
1181+
// Memory is pre-allocated elsewhere so we don't wan't to free it, just
1182+
// release our handle to it.
1183+
Sec_ProtectedMemHandle* h = NULL;
1184+
SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1185+
1186+
*bytesWritten = 0;
1187+
CHECK_STATUS(status)
1188+
}
1189+
else
1190+
{
1191+
// Higher layers will use the pre-allocated memory directly, not our buffer.
1192+
Sec_ProtectedMemHandle* h = NULL;
1193+
const Sec_Result r = SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1194+
1195+
if (r != SEC_RESULT_SUCCESS)
1196+
{
1197+
SEC_LOG_ERROR("Failed to release buffer from pre-allocated memory");
1198+
}
1199+
}
1200+
1201+
*bytesWritten = out_buffer.context.svp.offset;
1202+
return SEC_RESULT_SUCCESS;
1203+
}
1204+
10981205
Sec_Result get_cipher_algorithm(const Sec_CipherAlgorithm algorithm, SEC_BOOL is_unwrap,
10991206
sa_cipher_algorithm* cipher_algorithm, void** parameters, void* iv, SEC_SIZE key_length, SEC_SIZE key_offset) {
11001207
*parameters = NULL;

src/sec_adapter_engine.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static SEC_BOOL g_sec_openssl_inited = SEC_FALSE;
2929
static RSA_METHOD* rsa_method = NULL;
3030
#endif
3131

32+
static ENGINE* engine = NULL;
33+
3234
static void Sec_ShutdownOpenSSL() {
3335
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3436
if (rsa_method != NULL) {
@@ -37,11 +39,10 @@ static void Sec_ShutdownOpenSSL() {
3739
}
3840
#endif
3941

40-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
4142
if (engine != NULL) {
42-
ENGINE_remove(engine);
4343
ENGINE_finish(engine);
4444
ENGINE_free(engine);
45+
engine = NULL;
4546
}
4647
}
4748

@@ -62,7 +63,8 @@ static int Sec_OpenSSLPrivSign(int type, const unsigned char* m, unsigned int m_
6263
SEC_LOG_ERROR("Unknown type %d", type);
6364
return -1;
6465
}
65-
66+
67+
SEC_PRINT("Calling Sec_OpenSSLPrivSign with %s", OPENSSL_VERSION_TEXT);
6668
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
6769
if (keyHandle == NULL) {
6870
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -96,6 +98,7 @@ static int Sec_OpenSSLPubVerify(int type, const unsigned char* m, unsigned int m
9698
return -1;
9799
}
98100

101+
SEC_PRINT("Calling Sec_OpenSSLPubVerify with %s", OPENSSL_VERSION_TEXT);
99102
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
100103
if (keyHandle == NULL) {
101104
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -129,6 +132,7 @@ static int Sec_OpenSSLPubEncrypt(int flen, const unsigned char* from, unsigned c
129132
return -1;
130133
}
131134

135+
SEC_PRINT("Calling Sec_OpenSSLPubEncrypt with %s", OPENSSL_VERSION_TEXT);
132136
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
133137
if (keyHandle == NULL) {
134138
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -162,6 +166,7 @@ static int Sec_OpenSSLPrivDecrypt(int flen, const unsigned char* from, unsigned
162166
return -1;
163167
}
164168

169+
SEC_PRINT("Calling Sec_OpenSSLPrivDecrypt with %s", OPENSSL_VERSION_TEXT);
165170
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
166171
if (keyHandle == NULL) {
167172
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -199,7 +204,8 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
199204
#endif
200205

201206
static void ENGINE_load_securityapi(void) {
202-
ENGINE* engine = ENGINE_new();
207+
engine = ENGINE_new();
208+
SEC_PRINT("*****ENGINE_load_securityapi***** \n");
203209
if (engine == NULL) {
204210
SEC_LOG_ERROR("ENGINE_new failed");
205211
return;
@@ -208,28 +214,34 @@ static void ENGINE_load_securityapi(void) {
208214
if (!ENGINE_set_id(engine, SECAPI_ENGINE_ID)) {
209215
SEC_LOG_ERROR("ENGINE_set_id failed");
210216
ENGINE_free(engine);
217+
engine = NULL;
211218
return;
212219
}
213220
if (!ENGINE_set_name(engine, "SecurityApi engine")) {
214221
SEC_LOG_ERROR("ENGINE_set_name failed");
215222
ENGINE_free(engine);
223+
engine = NULL;
216224
return;
217225
}
218226

219227
if (!ENGINE_init(engine)) {
220228
SEC_LOG_ERROR("ENGINE_init failed");
221229
ENGINE_free(engine);
230+
engine = NULL;
222231
return;
223232
}
224233

225234
#if OPENSSL_VERSION_NUMBER < 0x10100000L
235+
SEC_PRINT("******Calling ENGINE_set_RSA 1 **** \n");
226236
if (!ENGINE_set_RSA(engine, &g_sec_openssl_rsamethod)) {
227237
#else
228238
if (rsa_method == NULL) {
239+
SEC_PRINT("*****Sec_InitOpenSSL creating RSA method****** \n");
229240
rsa_method = RSA_meth_new("securityapi RSA method", RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY);
230241
if (rsa_method == NULL) {
231242
SEC_LOG_ERROR("RSA_meth_new failed");
232243
ENGINE_free(engine);
244+
engine = NULL;
233245
return;
234246
}
235247

@@ -238,26 +250,27 @@ static void ENGINE_load_securityapi(void) {
238250
RSA_meth_set_sign(rsa_method, Sec_OpenSSLPrivSign);
239251
RSA_meth_set_verify(rsa_method, Sec_OpenSSLPubVerify);
240252
}
241-
253+
254+
SEC_PRINT("*****Calling ENGINE_set_RSA 2***** \n");
242255
if (!ENGINE_set_RSA(engine, rsa_method)) {
243256
#endif
244-
ENGINE_remove(engine);
245257
ENGINE_free(engine);
258+
engine = NULL;
246259
return;
247260
}
248261

249-
ENGINE_add(engine);
250-
ENGINE_free(engine);
251262
ERR_clear_error();
252263
}
253264

254265
void Sec_InitOpenSSL() {
255266
static pthread_mutex_t init_openssl_mutex = PTHREAD_MUTEX_INITIALIZER;
256267

257268
pthread_mutex_lock(&init_openssl_mutex);
269+
SEC_PRINT("***** Sec_InitOpenSSL****** \n");
258270

259271
if (g_sec_openssl_inited != SEC_TRUE) {
260272
#if OPENSSL_VERSION_NUMBER < 0x10100000L
273+
SEC_PRINT("*****Sec_InitOpenSSL OPenssl < 1.1.0****** \n");
261274
ERR_load_crypto_strings();
262275
OpenSSL_add_all_algorithms();
263276
OpenSSL_add_all_ciphers();
@@ -276,9 +289,9 @@ void Sec_InitOpenSSL() {
276289

277290
ENGINE_set_default(engine, ENGINE_METHOD_ALL);
278291
ENGINE_free(engine);
292+
engine = NULL;
279293
}
280294

281-
ENGINE_load_securityapi();
282295

283296
if (atexit(Sec_ShutdownOpenSSL) != 0) {
284297
SEC_LOG_ERROR("atexit failed");
@@ -288,6 +301,10 @@ void Sec_InitOpenSSL() {
288301
g_sec_openssl_inited = SEC_TRUE;
289302
}
290303

304+
if (engine == NULL) {
305+
ENGINE_load_securityapi();
306+
}
307+
291308
pthread_mutex_unlock(&init_openssl_mutex);
292309
}
293310

@@ -300,21 +317,19 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
300317
Sec_RSARawPublicKey pubKey;
301318
RSA* rsa = NULL;
302319

303-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
304320
if (engine == NULL) {
305321
SEC_LOG_ERROR("ENGINE_by_id failed");
322+
SEC_LOG_ERROR("engine not initialized");
306323
return NULL;
307324
}
308325

309326
if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey(keyHandle, &pubKey)) {
310-
ENGINE_free(engine);
311327
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
312328
return NULL;
313329
}
314330

315331
rsa = RSA_new_method(engine);
316332
if (rsa == NULL) {
317-
ENGINE_free(engine);
318333
SEC_LOG_ERROR("RSA_new_method failed");
319334
return NULL;
320335
}
@@ -329,28 +344,26 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
329344

330345
RSA_set_app_data(rsa, keyHandle);
331346
ENGINE_free(engine);
347+
engine = NULL;
332348
return rsa;
333349
}
334350

335351
RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle* certificateHandle) {
336352
Sec_RSARawPublicKey pubKey;
337353
RSA* rsa = NULL;
338354

339-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
340355
if (engine == NULL) {
341356
SEC_LOG_ERROR("ENGINE_by_id failed");
342357
return NULL;
343358
}
344359

345360
if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey(certificateHandle, &pubKey)) {
346-
ENGINE_free(engine);
347361
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
348362
return NULL;
349363
}
350364

351365
rsa = RSA_new_method(engine);
352366
if (rsa == NULL) {
353-
ENGINE_free(engine);
354367
SEC_LOG_ERROR("RSA_new_method failed");
355368
return NULL;
356369
}
@@ -362,9 +375,8 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
362375
RSA_set0_key(rsa, BN_bin2bn(pubKey.n, (int) Sec_BEBytesToUint32(pubKey.modulus_len_be), NULL),
363376
BN_bin2bn(pubKey.e, 4, NULL), NULL);
364377
#endif
365-
378+
SEC_PRINT("Calling SecKey_ToEngineRSA with %s", OPENSSL_VERSION_TEXT);
366379
RSA_set_app_data(rsa, keyHandle);
367-
ENGINE_free(engine);
368380
return rsa;
369381
}
370382

0 commit comments

Comments
 (0)