added support for Blowfish-128
[public/netxms.git] / src / libnetxms / crypto.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** NetXMS Foundation Library
7aad6641 4** Copyright (C) 2003-2013 Victor Kirhenshtein
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
68f384ea
VK
7** it under the terms of the GNU Lesser General Public License as published
8** by the Free Software Foundation; either version 3 of the License, or
5039dede
AK
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
68f384ea 16** You should have received a copy of the GNU Lesser General Public License
5039dede
AK
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: crypto.cpp
21**
22**/
23
24#include "libnetxms.h"
4e2debcc 25#include "ice.h"
5039dede 26
b1e9b6b3
VK
27/**
28 * Constants
29 */
5039dede
AK
30#define KEY_BUFFER_SIZE 4096
31
b1e9b6b3
VK
32/**
33 * Supported ciphers. By default, we support all ciphers compiled
34 * into OpenSSL library.
35 */
967893bb 36static UINT32 m_dwSupportedCiphers =
5039dede
AK
37#ifdef _WITH_ENCRYPTION
38#ifndef OPENSSL_NO_AES
39 CSCP_SUPPORT_AES_256 |
9e9d631e 40 CSCP_SUPPORT_AES_128 |
5039dede
AK
41#endif
42#ifndef OPENSSL_NO_BF
d772d0dd
VK
43 CSCP_SUPPORT_BLOWFISH_256 |
44 CSCP_SUPPORT_BLOWFISH_128 |
5039dede
AK
45#endif
46#ifndef OPENSSL_NO_IDEA
47 CSCP_SUPPORT_IDEA |
48#endif
49#ifndef OPENSSL_NO_DES
50 CSCP_SUPPORT_3DES |
51#endif
52#endif /* _WITH_ENCRYPTION */
53 0;
54
b1e9b6b3
VK
55/**
56 * Static data
57 */
5039dede
AK
58#ifdef _WITH_ENCRYPTION
59
9e9d631e 60typedef OPENSSL_CONST EVP_CIPHER * (*CIPHER_FUNC)();
5039dede
AK
61static CIPHER_FUNC m_pfCipherList[NETXMS_MAX_CIPHERS] =
62{
63#ifndef OPENSSL_NO_AES
64 EVP_aes_256_cbc,
65#else
66 NULL,
67#endif
68#ifndef OPENSSL_NO_BF
69 EVP_bf_cbc,
70#else
71 NULL,
72#endif
73#ifndef OPENSSL_NO_IDEA
74 EVP_idea_cbc,
75#else
76 NULL,
77#endif
78#ifndef OPENSSL_NO_DES
9e9d631e
VK
79 EVP_des_ede3_cbc,
80#else
81 NULL,
82#endif
83#ifndef OPENSSL_NO_AES
d772d0dd
VK
84 EVP_aes_128_cbc,
85#else
86 NULL,
87#endif
88#ifndef OPENSSL_NO_BF
89 EVP_bf_cbc
5039dede
AK
90#else
91 NULL
92#endif
93};
94static WORD m_wNoEncryptionFlag = 0;
95static MUTEX *m_pCryptoMutexList = NULL;
96
b1e9b6b3
VK
97/**
98 * Locking callback for CRYPTO library
99 */
5039dede
AK
100static void CryptoLockingCallback(int nMode, int nLock, const char *pszFile, int nLine)
101{
102 if (nMode & CRYPTO_LOCK)
c17f6cbc 103 MutexLock(m_pCryptoMutexList[nLock]);
5039dede
AK
104 else
105 MutexUnlock(m_pCryptoMutexList[nLock]);
106}
107
b1e9b6b3
VK
108/**
109 * ID callback for CRYPTO library
110 */
5039dede
AK
111#ifndef _WIN32
112
9e9d631e 113static unsigned long CryptoIdCallback()
5039dede
AK
114{
115 return (unsigned long)GetCurrentThreadId();
116}
117
118#endif
119
120#endif /* _WITH_ENCRYPTION */
121
b1e9b6b3
VK
122/**
123 * Initialize OpenSSL library
124 */
967893bb 125BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers)
5039dede
AK
126{
127#ifdef _WITH_ENCRYPTION
128 BYTE random[8192];
129 int i;
130
131 CRYPTO_malloc_init();
132 ERR_load_CRYPTO_strings();
133 OpenSSL_add_all_algorithms();
134 RAND_seed(random, 8192);
135 m_dwSupportedCiphers &= dwEnabledCiphers;
136 m_wNoEncryptionFlag = htons(MF_DONT_ENCRYPT);
137 m_pCryptoMutexList = (MUTEX *)malloc(sizeof(MUTEX) * CRYPTO_num_locks());
138 for(i = 0; i < CRYPTO_num_locks(); i++)
139 m_pCryptoMutexList[i] = MutexCreate();
140 CRYPTO_set_locking_callback(CryptoLockingCallback);
141#ifndef _WIN32
142 CRYPTO_set_id_callback(CryptoIdCallback);
143#endif /* _WIN32 */
144#endif /* _WITH_ENCRYPTION */
145 return TRUE;
146}
147
b1e9b6b3
VK
148/**
149 * Get supported ciphers
150 */
967893bb 151UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers()
5039dede
AK
152{
153 return m_dwSupportedCiphers;
154}
155
b1e9b6b3
VK
156/**
157 * Encrypt message
158 */
98abc9f1 159CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg)
5039dede
AK
160{
161#ifdef _WITH_ENCRYPTION
162 CSCP_ENCRYPTED_MESSAGE *pEnMsg;
163 CSCP_ENCRYPTED_PAYLOAD_HEADER header;
164 int nSize;
165 EVP_CIPHER_CTX cipher;
967893bb 166 UINT32 dwMsgSize;
5039dede
AK
167
168 if (pMsg->wFlags & m_wNoEncryptionFlag)
169 return (CSCP_ENCRYPTED_MESSAGE *)nx_memdup(pMsg, ntohl(pMsg->dwSize));
170
98abc9f1 171 if (m_pfCipherList[pCtx->getCipher()] == NULL)
5039dede
AK
172 return NULL; // Unsupported cipher
173
98abc9f1
VK
174 EVP_EncryptInit(&cipher, m_pfCipherList[pCtx->getCipher()](), pCtx->getSessionKey(), pCtx->getIV());
175 EVP_CIPHER_CTX_set_key_length(&cipher, pCtx->getKeyLength());
5039dede
AK
176
177 dwMsgSize = ntohl(pMsg->dwSize);
178 pEnMsg = (CSCP_ENCRYPTED_MESSAGE *)malloc(dwMsgSize + CSCP_ENCRYPTION_HEADER_SIZE +
179 EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(&cipher)) + 8);
180 pEnMsg->wCode = htons(CMD_ENCRYPTED_MESSAGE);
181 pEnMsg->nReserved = 0;
182
183 header.dwChecksum = htonl(CalculateCRC32((BYTE *)pMsg, dwMsgSize, 0));
184 header.dwReserved = 0;
185 EVP_EncryptUpdate(&cipher, pEnMsg->data, &nSize, (BYTE *)&header, CSCP_EH_ENCRYPTED_BYTES);
186 dwMsgSize = nSize;
187 EVP_EncryptUpdate(&cipher, pEnMsg->data + dwMsgSize, &nSize, (BYTE *)pMsg, ntohl(pMsg->dwSize));
188 dwMsgSize += nSize;
189 EVP_EncryptFinal(&cipher, pEnMsg->data + dwMsgSize, &nSize);
190 dwMsgSize += nSize + CSCP_EH_UNENCRYPTED_BYTES;
191 EVP_CIPHER_CTX_cleanup(&cipher);
192
193 if (dwMsgSize % 8 != 0)
194 {
195 pEnMsg->nPadding = (BYTE)(8 - (dwMsgSize % 8));
196 dwMsgSize += pEnMsg->nPadding;
197 }
198 else
199 {
200 pEnMsg->nPadding = 0;
201 }
202 pEnMsg->dwSize = htonl(dwMsgSize);
203
204 return pEnMsg;
205#else /* _WITH_ENCRYPTION */
206 return NULL;
207#endif
208}
209
7aad6641
VK
210/**
211 * Decrypt message
212 */
98abc9f1 213BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
5039dede
AK
214 CSCP_ENCRYPTED_MESSAGE *pMsg,
215 BYTE *pDecryptionBuffer)
216{
217#ifdef _WITH_ENCRYPTION
218 int nSize;
219 EVP_CIPHER_CTX cipher;
967893bb 220 UINT32 dwChecksum, dwMsgSize;
5039dede
AK
221 CSCP_MESSAGE *pClearMsg;
222
98abc9f1 223 if (m_pfCipherList[pCtx->getCipher()] == NULL)
5039dede
AK
224 return FALSE; // Unsupported cipher
225
226 pMsg->dwSize = ntohl(pMsg->dwSize);
98abc9f1
VK
227 EVP_DecryptInit(&cipher, m_pfCipherList[pCtx->getCipher()](), pCtx->getSessionKey(), pCtx->getIV());
228 EVP_CIPHER_CTX_set_key_length(&cipher, pCtx->getKeyLength());
5039dede
AK
229 EVP_DecryptUpdate(&cipher, pDecryptionBuffer, &nSize, pMsg->data,
230 pMsg->dwSize - CSCP_EH_UNENCRYPTED_BYTES - pMsg->nPadding);
231 EVP_DecryptFinal(&cipher, pDecryptionBuffer + nSize, &nSize);
232 EVP_CIPHER_CTX_cleanup(&cipher);
233
234 pClearMsg = (CSCP_MESSAGE *)(pDecryptionBuffer + CSCP_EH_ENCRYPTED_BYTES);
235 dwMsgSize = ntohl(pClearMsg->dwSize);
236 if (dwMsgSize > pMsg->dwSize)
237 return FALSE; // Message decrypted incorrectly, because it can't be larger than encrypted
238 dwChecksum = CalculateCRC32((BYTE *)pClearMsg, dwMsgSize, 0);
239 if (dwChecksum != ntohl(((CSCP_ENCRYPTED_PAYLOAD_HEADER *)pDecryptionBuffer)->dwChecksum))
240 return FALSE; // Bad checksum
241
242 memcpy(pMsg, pClearMsg, dwMsgSize);
243 return TRUE;
244#else /* _WITH_ENCRYPTION */
245 return FALSE;
246#endif
247}
248
b1e9b6b3
VK
249/**
250 * Setup encryption context
251 * Function will determine is it called on initiator or responder side
252 * by message code. Initiator should provide it's private key,
253 * and responder should provide pointer to response message.
254 */
967893bb 255UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 256 NXCPEncryptionContext **ppCtx,
5039dede
AK
257 CSCPMessage **ppResponse,
258 RSA *pPrivateKey, int nNXCPVersion)
259{
967893bb 260 UINT32 dwResult = RCC_NOT_IMPLEMENTED;
5039dede 261
98abc9f1 262 *ppCtx = NULL;
5039dede
AK
263#ifdef _WITH_ENCRYPTION
264 if (pMsg->GetCode() == CMD_REQUEST_SESSION_KEY)
265 {
967893bb 266 UINT32 dwCiphers;
5039dede
AK
267
268 *ppResponse = new CSCPMessage(nNXCPVersion);
269 (*ppResponse)->SetCode(CMD_SESSION_KEY);
270 (*ppResponse)->SetId(pMsg->GetId());
4af351c7 271 (*ppResponse)->disableEncryption();
5039dede
AK
272
273 dwCiphers = pMsg->GetVariableLong(VID_SUPPORTED_ENCRYPTION) & m_dwSupportedCiphers;
274 if (dwCiphers == 0)
275 {
276 (*ppResponse)->SetVariable(VID_RCC, RCC_NO_CIPHERS);
277 dwResult = RCC_NO_CIPHERS;
278 }
279 else
280 {
98abc9f1
VK
281 BYTE *pBufPos, ucKeyBuffer[KEY_BUFFER_SIZE];
282 RSA *pServerKey;
967893bb 283 UINT32 dwKeySize;
5039dede 284
98abc9f1 285 *ppCtx = NXCPEncryptionContext::create(dwCiphers);
5039dede
AK
286
287 // Encrypt key
288 dwKeySize = pMsg->GetVariableBinary(VID_PUBLIC_KEY, ucKeyBuffer, KEY_BUFFER_SIZE);
289 pBufPos = ucKeyBuffer;
290 pServerKey = d2i_RSAPublicKey(NULL, (OPENSSL_CONST BYTE **)&pBufPos, dwKeySize);
291 if (pServerKey != NULL)
292 {
293 (*ppResponse)->SetVariable(VID_RCC, RCC_SUCCESS);
98abc9f1 294 dwKeySize = RSA_public_encrypt((*ppCtx)->getKeyLength(), (*ppCtx)->getSessionKey(),
5039dede
AK
295 ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
296 (*ppResponse)->SetVariable(VID_SESSION_KEY, ucKeyBuffer, dwKeySize);
98abc9f1 297 dwKeySize = RSA_public_encrypt(EVP_MAX_IV_LENGTH, (*ppCtx)->getIV(),
5039dede
AK
298 ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
299 (*ppResponse)->SetVariable(VID_SESSION_IV, ucKeyBuffer, dwKeySize);
98abc9f1
VK
300 (*ppResponse)->SetVariable(VID_CIPHER, (WORD)(*ppCtx)->getCipher());
301 (*ppResponse)->SetVariable(VID_KEY_LENGTH, (WORD)(*ppCtx)->getKeyLength());
5039dede
AK
302 (*ppResponse)->SetVariable(VID_IV_LENGTH, (WORD)EVP_MAX_IV_LENGTH);
303 RSA_free(pServerKey);
304 dwResult = RCC_SUCCESS;
305 }
306 else
307 {
308 (*ppResponse)->SetVariable(VID_RCC, RCC_INVALID_PUBLIC_KEY);
309 dwResult = RCC_INVALID_PUBLIC_KEY;
310 }
311 }
312 }
313 else if (pMsg->GetCode() == CMD_SESSION_KEY)
314 {
315 dwResult = pMsg->GetVariableLong(VID_RCC);
316 if (dwResult == RCC_SUCCESS)
317 {
98abc9f1
VK
318 *ppCtx = NXCPEncryptionContext::create(pMsg, pPrivateKey);
319 if (*ppCtx == NULL)
320 {
321 dwResult = RCC_INVALID_SESSION_KEY;
322 }
5039dede
AK
323 }
324 }
325
326 if ((dwResult != RCC_SUCCESS) && (*ppCtx != NULL))
327 {
98abc9f1 328 delete *ppCtx;
5039dede
AK
329 *ppCtx = NULL;
330 }
331#else
332 if (pMsg->GetCode() == CMD_REQUEST_SESSION_KEY)
333 {
334 *ppResponse = new CSCPMessage(nNXCPVersion);
335 (*ppResponse)->SetCode(CMD_SESSION_KEY);
336 (*ppResponse)->SetId(pMsg->GetId());
26de9040 337 (*ppResponse)->disableEncryption();
5039dede
AK
338 (*ppResponse)->SetVariable(VID_RCC, dwResult);
339 }
340#endif
341
342 return dwResult;
343}
344
b1e9b6b3
VK
345/**
346 * Prepare session key request message
347 */
9e9d631e 348void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format)
5039dede
AK
349{
350#ifdef _WITH_ENCRYPTION
351 int iLen;
352 BYTE *pKeyBuffer, *pBufPos;
353
354 pMsg->SetCode(CMD_REQUEST_SESSION_KEY);
355 pMsg->SetVariable(VID_SUPPORTED_ENCRYPTION, m_dwSupportedCiphers);
356
9e9d631e
VK
357 if (useX509Format)
358 {
359 iLen = i2d_RSA_PUBKEY(pServerKey, NULL);
360 pKeyBuffer = (BYTE *)malloc(iLen);
361 pBufPos = pKeyBuffer;
362 i2d_RSA_PUBKEY(pServerKey, &pBufPos);
363 }
364 else
365 {
366 iLen = i2d_RSAPublicKey(pServerKey, NULL);
367 pKeyBuffer = (BYTE *)malloc(iLen);
368 pBufPos = pKeyBuffer;
369 i2d_RSAPublicKey(pServerKey, &pBufPos);
370 }
371 pMsg->SetVariable(VID_PUBLIC_KEY, pKeyBuffer, iLen);
5039dede
AK
372 free(pKeyBuffer);
373#endif
374}
375
7aad6641
VK
376/**
377 * Load RSA key(s) from file
378 */
5039dede
AK
379RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile)
380{
381#ifdef _WITH_ENCRYPTION
382 FILE *fp;
383 BYTE *pKeyBuffer, *pBufPos, hash[SHA1_DIGEST_SIZE];
967893bb 384 UINT32 dwLen;
5039dede
AK
385 RSA *pKey = NULL;
386
387 fp = _tfopen(pszKeyFile, _T("rb"));
388 if (fp != NULL)
389 {
967893bb 390 if (fread(&dwLen, 1, sizeof(UINT32), fp) == sizeof(UINT32) && dwLen < 10 * 1024)
5039dede
AK
391 {
392 pKeyBuffer = (BYTE *)malloc(dwLen);
393 pBufPos = pKeyBuffer;
394 if (fread(pKeyBuffer, 1, dwLen, fp) == dwLen)
395 {
396 BYTE hash2[SHA1_DIGEST_SIZE];
397
7aad6641 398 if (fread(hash, 1, SHA1_DIGEST_SIZE, fp) == SHA1_DIGEST_SIZE)
5039dede 399 {
7aad6641
VK
400 CalculateSHA1Hash(pKeyBuffer, dwLen, hash2);
401 if (!memcmp(hash, hash2, SHA1_DIGEST_SIZE))
5039dede 402 {
7aad6641
VK
403 pKey = d2i_RSAPublicKey(NULL, (OPENSSL_CONST BYTE **)&pBufPos, dwLen);
404 if (pKey != NULL)
5039dede 405 {
7aad6641 406 if (d2i_RSAPrivateKey(&pKey, (OPENSSL_CONST BYTE **)&pBufPos,
967893bb 407 dwLen - CAST_FROM_POINTER((pBufPos - pKeyBuffer), UINT32)) == NULL)
7aad6641
VK
408 {
409 RSA_free(pKey);
410 pKey = NULL;
411 }
5039dede
AK
412 }
413 }
414 }
415 }
416 free(pKeyBuffer);
417 }
418 fclose(fp);
419 }
420
421 return pKey;
422#else
423 return NULL;
424#endif
425}
426
7aad6641
VK
427/**
428 * Create signature for message using certificate (MS CAPI version)
429 * Paraeters:
430 * pMsg and dwMsgLen - message to sign and it's length
431 * pCert - certificate
432 * pBuffer - output buffer
433 * dwBufSize - buffer size
434 * pdwSigLen - actual signature size
435 */
5039dede
AK
436#ifdef _WIN32
437
967893bb
VK
438BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
439 BYTE *pBuffer, UINT32 dwBufSize, UINT32 *pdwSigLen)
5039dede
AK
440{
441 BOOL bFreeProv, bRet = FALSE;
442 DWORD i, j, dwLen, dwKeySpec;
443 HCRYPTPROV hProv;
444 HCRYPTHASH hHash;
445 BYTE *pTemp;
446
447 if (CryptAcquireCertificatePrivateKey(pCert, CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
448 NULL, &hProv, &dwKeySpec, &bFreeProv))
449 {
450 if (CryptCreateHash(hProv, CALG_SHA1, NULL, 0, &hHash))
451 {
452 if (CryptHashData(hHash, pMsg, dwMsgLen, 0))
453 {
454 dwLen = dwBufSize;
455 pTemp = (BYTE *)malloc(dwBufSize);;
456 bRet = CryptSignHash(hHash, dwKeySpec, NULL, 0, pTemp, &dwLen);
457 *pdwSigLen = dwLen;
458 // we have to reverse the byte-order in the result from CryptSignHash()
459 for(i = 0, j = dwLen - 1; i < dwLen; i++, j--)
460 pBuffer[i] = pTemp[j];
461 }
462 CryptDestroyHash(hHash);
463 }
464
465 if (bFreeProv)
466 CryptReleaseContext(hProv, 0);
467 }
468 return bRet;
469}
470
471#endif
4e2debcc 472
7aad6641
VK
473/**
474 * Encrypt data block with ICE
475 */
4e2debcc
VK
476void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key)
477{
478 ICE_KEY *ice = ice_key_create(1);
479 ice_key_set(ice, key);
480
481 int stopPos = inLen - (inLen % 8);
4e2debcc
VK
482 for(int pos = 0; pos < stopPos; pos += 8)
483 ice_key_encrypt(ice, &in[pos], &out[pos]);
484
485 if (stopPos < inLen)
486 {
487 BYTE plainText[8], encrypted[8];
488
489 memcpy(plainText, &in[stopPos], inLen - stopPos);
490 ice_key_encrypt(ice, plainText, encrypted);
491 memcpy(&out[stopPos], encrypted, inLen - stopPos);
492 }
493
494 ice_key_destroy(ice);
495}
496
7aad6641
VK
497/**
498 * Decrypt data block with ICE
499 */
4e2debcc
VK
500void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key)
501{
502 ICE_KEY *ice = ice_key_create(1);
503 ice_key_set(ice, key);
504
505 int stopPos = inLen - (inLen % 8);
4e2debcc
VK
506 for(int pos = 0; pos < stopPos; pos += 8)
507 ice_key_decrypt(ice, &in[pos], &out[pos]);
508
509 if (stopPos < inLen)
510 {
511 BYTE plainText[8], encrypted[8];
512
513 memcpy(encrypted, &in[stopPos], inLen - stopPos);
514 ice_key_decrypt(ice, encrypted, plainText);
515 memcpy(&out[stopPos], plainText, inLen - stopPos);
516 }
517
518 ice_key_destroy(ice);
519}
98abc9f1 520
7aad6641
VK
521/**
522 * Encryption context constructor
523 */
98abc9f1
VK
524NXCPEncryptionContext::NXCPEncryptionContext()
525{
526 m_sessionKey = NULL;
527}
528
7aad6641
VK
529/**
530 * Encryption context destructor
531 */
98abc9f1
VK
532NXCPEncryptionContext::~NXCPEncryptionContext()
533{
534 safe_free(m_sessionKey);
535}
536
7aad6641
VK
537/**
538 * Create encryption context from CMD_SESSION_KEY NXCP message
539 */
98abc9f1
VK
540NXCPEncryptionContext *NXCPEncryptionContext::create(CSCPMessage *msg, RSA *privateKey)
541{
e4cba530 542#ifdef _WITH_ENCRYPTION
98abc9f1 543 BYTE ucKeyBuffer[KEY_BUFFER_SIZE], ucSessionKey[KEY_BUFFER_SIZE];
967893bb 544 UINT32 dwKeySize;
98abc9f1
VK
545 int nSize, nIVLen;
546 NXCPEncryptionContext *ctx = new NXCPEncryptionContext;
547
548 ctx->m_cipher = msg->GetVariableShort(VID_CIPHER);
549 ctx->m_keyLength = msg->GetVariableShort(VID_KEY_LENGTH);
550 ctx->m_sessionKey = (BYTE *)malloc(ctx->m_keyLength);
551
552 // Decrypt session key
553 dwKeySize = msg->GetVariableBinary(VID_SESSION_KEY, ucKeyBuffer, KEY_BUFFER_SIZE);
554 nSize = RSA_private_decrypt(dwKeySize, ucKeyBuffer, ucSessionKey, privateKey, RSA_PKCS1_OAEP_PADDING);
555 if (nSize == ctx->m_keyLength)
556 {
557 memcpy(ctx->m_sessionKey, ucSessionKey, nSize);
558
559 // Decrypt session IV
560 nIVLen = msg->GetVariableShort(VID_IV_LENGTH);
561 if (nIVLen == 0) // Versions prior to 0.2.13 don't send IV length, assume 16
562 nIVLen = 16;
563 dwKeySize = msg->GetVariableBinary(VID_SESSION_IV, ucKeyBuffer, KEY_BUFFER_SIZE);
564 nSize = RSA_private_decrypt(dwKeySize, ucKeyBuffer, ucSessionKey, privateKey, RSA_PKCS1_OAEP_PADDING);
565 if ((nSize == nIVLen) &&
566 (nIVLen <= EVP_CIPHER_iv_length(m_pfCipherList[ctx->m_cipher]())))
567 {
568 memcpy(ctx->m_iv, ucSessionKey, min(EVP_MAX_IV_LENGTH, nIVLen));
569 }
570 else
571 {
572 delete_and_null(ctx);
573 }
574 }
575 else
576 {
577 delete_and_null(ctx);
578 }
579 return ctx;
e4cba530
VK
580#else
581 return new NXCPEncryptionContext;
582#endif
98abc9f1
VK
583}
584
7aad6641
VK
585/**
586 * Create encryption context from CMD_REQUEST_SESSION_KEY NXCP message
587 */
967893bb 588NXCPEncryptionContext *NXCPEncryptionContext::create(UINT32 ciphers)
98abc9f1
VK
589{
590 NXCPEncryptionContext *ctx = new NXCPEncryptionContext;
591
e4cba530 592#ifdef _WITH_ENCRYPTION
98abc9f1
VK
593 // Select cipher
594 if (ciphers & CSCP_SUPPORT_AES_256)
595 {
596 ctx->m_cipher = CSCP_CIPHER_AES_256;
597 ctx->m_keyLength = 32;
598 }
d772d0dd 599 else if (ciphers & CSCP_SUPPORT_BLOWFISH_256)
98abc9f1 600 {
d772d0dd 601 ctx->m_cipher = CSCP_CIPHER_BLOWFISH_256;
98abc9f1
VK
602 ctx->m_keyLength = 32;
603 }
9e9d631e
VK
604 else if (ciphers & CSCP_SUPPORT_AES_128)
605 {
606 ctx->m_cipher = CSCP_CIPHER_AES_128;
607 ctx->m_keyLength = 16;
608 }
d772d0dd
VK
609 else if (ciphers & CSCP_SUPPORT_BLOWFISH_128)
610 {
611 ctx->m_cipher = CSCP_CIPHER_BLOWFISH_128;
612 ctx->m_keyLength = 16;
613 }
98abc9f1
VK
614 else if (ciphers & CSCP_SUPPORT_IDEA)
615 {
616 ctx->m_cipher = CSCP_CIPHER_IDEA;
617 ctx->m_keyLength = 16;
618 }
619 else if (ciphers & CSCP_SUPPORT_3DES)
620 {
621 ctx->m_cipher = CSCP_CIPHER_3DES;
622 ctx->m_keyLength = 24;
623 }
624
625 // Generate key
626 ctx->m_sessionKey = (BYTE *)malloc(ctx->m_keyLength);
627 RAND_bytes(ctx->m_sessionKey, ctx->m_keyLength);
628 RAND_bytes(ctx->m_iv, EVP_MAX_IV_LENGTH);
e4cba530 629#endif
98abc9f1
VK
630
631 return ctx;
632}