Browse Source

OpenSSL >= 1.1.0 compatibility

OpenSSL did some otclient-breaking changes around 1.1.0, 
this patch should work on both < and >= 1.1.0
divinity76 1 year ago
parent
commit
79e31cb041
No account linked to committer's email address
1 changed files with 76 additions and 16 deletions
  1. 76
    16
      src/framework/util/crypt.cpp

+ 76
- 16
src/framework/util/crypt.cpp View File

@@ -326,22 +326,62 @@ void Crypt::rsaGenerateKey(int bits, int e)
326 326
 
327 327
 void Crypt::rsaSetPublicKey(const std::string& n, const std::string& e)
328 328
 {
329
-    BN_dec2bn(&m_rsa->n, n.c_str());
330
-    BN_dec2bn(&m_rsa->e, e.c_str());
331
-
332
-    // clear rsa cache
333
-    if(m_rsa->_method_mod_n) { BN_MONT_CTX_free(m_rsa->_method_mod_n); m_rsa->_method_mod_n = NULL; }
329
+#if OPENSSL_VERSION_NUMBER < 0x10100005L
330
+	BN_dec2bn(&m_rsa->n, n.c_str());
331
+	BN_dec2bn(&m_rsa->e, e.c_str());
332
+	// clear rsa cache
333
+	if (m_rsa->_method_mod_n)
334
+	{
335
+		BN_MONT_CTX_free(m_rsa->_method_mod_n);
336
+		m_rsa->_method_mod_n = NULL;
337
+	}
338
+#else
339
+	{
340
+		BIGNUM *bn=NULL;
341
+		BIGNUM *be=NULL;
342
+		BN_dec2bn(&bn, n.c_str());
343
+		BN_dec2bn(&be, e.c_str());
344
+		RSA_set0_key(m_rsa,bn,be,NULL);
345
+		// note, not supposed to free bn/be here, that's m_rsa's destructor's job
346
+	}
347
+#endif
334 348
 }
335 349
 
336 350
 void Crypt::rsaSetPrivateKey(const std::string& p, const std::string& q, const std::string& d)
337 351
 {
338
-    BN_dec2bn(&m_rsa->p, p.c_str());
339
-    BN_dec2bn(&m_rsa->q, q.c_str());
340
-    BN_dec2bn(&m_rsa->d, d.c_str());
341
-
342
-    // clear rsa cache
343
-    if(m_rsa->_method_mod_p) { BN_MONT_CTX_free(m_rsa->_method_mod_p); m_rsa->_method_mod_p = NULL; }
344
-    if(m_rsa->_method_mod_q) { BN_MONT_CTX_free(m_rsa->_method_mod_q); m_rsa->_method_mod_q = NULL; }
352
+#if OPENSSL_VERSION_NUMBER < 0x10100005L
353
+	BN_dec2bn(&m_rsa->p, p.c_str());
354
+	BN_dec2bn(&m_rsa->q, q.c_str());
355
+	BN_dec2bn(&m_rsa->d, d.c_str());
356
+	// clear rsa cache
357
+	if (m_rsa->_method_mod_p)
358
+	{
359
+		BN_MONT_CTX_free(m_rsa->_method_mod_p);
360
+		m_rsa->_method_mod_p = NULL;
361
+	}
362
+	if (m_rsa->_method_mod_q)
363
+	{
364
+		BN_MONT_CTX_free(m_rsa->_method_mod_q);
365
+		m_rsa->_method_mod_q = NULL;
366
+	}
367
+#else
368
+	{
369
+
370
+		if(d.length()> 0)
371
+		{
372
+			BIGNUM *bd=NULL;
373
+			BN_dec2bn(&bd, d.c_str());
374
+			RSA_set0_key(m_rsa,NULL,NULL,bd);
375
+		}
376
+		BIGNUM *bp=NULL;
377
+		BIGNUM *bq=NULL;
378
+		BN_dec2bn(&bp, p.c_str());
379
+		BN_dec2bn(&bq, q.c_str());
380
+		RSA_set0_factors(m_rsa,bp,bq);
381
+		// note, not supposed to free bp/bq/bd here, that's m_rsa's destructor's job
382
+
383
+	}
384
+#endif
345 385
 }
346 386
 
347 387
 bool Crypt::rsaCheckKey()
@@ -352,10 +392,30 @@ bool Crypt::rsaCheckKey()
352 392
         BN_CTX_start(ctx);
353 393
 
354 394
         BIGNUM *r1 = BN_CTX_get(ctx), *r2 = BN_CTX_get(ctx);
355
-        BN_mod(m_rsa->dmp1, m_rsa->d, r1, ctx);
356
-        BN_mod(m_rsa->dmq1, m_rsa->d, r2, ctx);
357
-
358
-        BN_mod_inverse(m_rsa->iqmp, m_rsa->q, m_rsa->p, ctx);
395
+#if OPENSSL_VERSION_NUMBER < 0x10100005L
396
+		BN_mod(m_rsa->dmp1, m_rsa->d, r1, ctx);
397
+		BN_mod(m_rsa->dmq1, m_rsa->d, r2, ctx);
398
+		BN_mod_inverse(m_rsa->iqmp, m_rsa->q, m_rsa->p, ctx);
399
+#else
400
+		{
401
+			const BIGNUM *dmp1_c=NULL;
402
+			const BIGNUM *d=NULL;
403
+			const BIGNUM *dmq1_c=NULL;
404
+			const BIGNUM *iqmp_c=NULL;
405
+			const BIGNUM *q=NULL;
406
+			const BIGNUM *p=NULL;
407
+			RSA_get0_key(m_rsa,NULL, NULL, &d);
408
+			RSA_get0_factors(m_rsa, &p, &q);
409
+			RSA_get0_crt_params(m_rsa,&dmp1_c,&dmq1_c,&iqmp_c);
410
+			BIGNUM *dmp1=BN_dup(dmp1_c);
411
+			BIGNUM *dmq1=BN_dup(dmq1_c);
412
+			BIGNUM *iqmp=BN_dup(iqmp_c);
413
+			BN_mod(dmp1, d, r1, ctx);
414
+			BN_mod(dmq1, d, r2, ctx);
415
+			BN_mod_inverse(iqmp, q, p, ctx);
416
+			RSA_set0_crt_params(m_rsa, dmp1, dmq1, iqmp);
417
+		}
418
+#endif
359 419
         return true;
360 420
     }
361 421
     else {

Loading…
Cancel
Save