b6b438
From f20d681243aed9c4e2c1a669cb04964b380413f3 Mon Sep 17 00:00:00 2001
b6b438
From: Andreas Schneider <asn@samba.org>
b6b438
Date: Fri, 22 Feb 2019 12:59:13 +0100
b6b438
Subject: [PATCH 056/187] lib:crypto: Use GnuTLS RC4 in py_crypto
b6b438
b6b438
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14031
b6b438
b6b438
Signed-off-by: Andreas Schneider <asn@samba.org>
b6b438
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
b6b438
(cherry picked from commit fc4ae06001fbb0045318a8cec7af6af81241c60e)
b6b438
---
b6b438
 lib/crypto/py_crypto.c   | 34 +++++++++++++++++++++++++++++-----
b6b438
 lib/crypto/wscript_build |  7 +++----
b6b438
 2 files changed, 32 insertions(+), 9 deletions(-)
b6b438
b6b438
diff --git a/lib/crypto/py_crypto.c b/lib/crypto/py_crypto.c
b6b438
index 13e2569945d..c85cd2c13d2 100644
b6b438
--- a/lib/crypto/py_crypto.c
b6b438
+++ b/lib/crypto/py_crypto.c
b6b438
@@ -21,13 +21,18 @@
b6b438
 #include <Python.h>
b6b438
 #include "includes.h"
b6b438
 #include "python/py3compat.h"
b6b438
-#include "lib/crypto/arcfour.h"
b6b438
+
b6b438
+#include <gnutls/gnutls.h>
b6b438
+#include <gnutls/crypto.h>
b6b438
 
b6b438
 static PyObject *py_crypto_arcfour_crypt_blob(PyObject *module, PyObject *args)
b6b438
 {
b6b438
-	DATA_BLOB data, key;
b6b438
+	DATA_BLOB data;
b6b438
 	PyObject *py_data, *py_key, *result;
b6b438
 	TALLOC_CTX *ctx;
b6b438
+	gnutls_cipher_hd_t cipher_hnd = NULL;
b6b438
+	gnutls_datum_t key;
b6b438
+	int rc;
b6b438
 
b6b438
 	if (!PyArg_ParseTuple(args, "OO", &py_data, &py_key))
b6b438
 		return NULL;
b6b438
@@ -51,10 +56,29 @@ static PyObject *py_crypto_arcfour_crypt_blob(PyObject *module, PyObject *args)
b6b438
 		return PyErr_NoMemory();
b6b438
 	}
b6b438
 
b6b438
-	key.data = (uint8_t *)PyBytes_AsString(py_key);
b6b438
-	key.length = PyBytes_Size(py_key);
b6b438
+	key = (gnutls_datum_t) {
b6b438
+		.data = (uint8_t *)PyBytes_AsString(py_key),
b6b438
+		.size = PyBytes_Size(py_key),
b6b438
+	};
b6b438
 
b6b438
-	arcfour_crypt_blob(data.data, data.length, &key);
b6b438
+	rc = gnutls_cipher_init(&cipher_hnd,
b6b438
+				GNUTLS_CIPHER_ARCFOUR_128,
b6b438
+				&key,
b6b438
+				NULL);
b6b438
+	if (rc < 0) {
b6b438
+		talloc_free(ctx);
b6b438
+		PyErr_Format(PyExc_OSError, "encryption failed");
b6b438
+		return NULL;
b6b438
+	}
b6b438
+	rc = gnutls_cipher_encrypt(cipher_hnd,
b6b438
+				   data.data,
b6b438
+				   data.length);
b6b438
+	gnutls_cipher_deinit(cipher_hnd);
b6b438
+	if (rc < 0) {
b6b438
+		talloc_free(ctx);
b6b438
+		PyErr_Format(PyExc_OSError, "encryption failed");
b6b438
+		return NULL;
b6b438
+	}
b6b438
 
b6b438
 	result = PyBytes_FromStringAndSize((const char*) data.data, data.length);
b6b438
 	talloc_free(ctx);
b6b438
diff --git a/lib/crypto/wscript_build b/lib/crypto/wscript_build
b6b438
index 2ad8dfe2cd0..46b0e084328 100644
b6b438
--- a/lib/crypto/wscript_build
b6b438
+++ b/lib/crypto/wscript_build
b6b438
@@ -28,7 +28,6 @@ bld.SAMBA_SUBSYSTEM('TORTURE_LIBCRYPTO',
b6b438
         )
b6b438
 
b6b438
 bld.SAMBA_PYTHON('python_crypto',
b6b438
-		 source='py_crypto.c',
b6b438
-		 deps='LIBCRYPTO',
b6b438
-		 realname='samba/crypto.so'
b6b438
-		)
b6b438
+                 source='py_crypto.c',
b6b438
+                 deps='gnutls talloc',
b6b438
+                 realname='samba/crypto.so')
b6b438
-- 
b6b438
2.23.0
b6b438