Blame SOURCES/pyOpenSSL-0.13.1-exception.patch

50e473
diff -up pyOpenSSL-0.13.1/OpenSSL/crypto/crypto.c.exception pyOpenSSL-0.13.1/OpenSSL/crypto/crypto.c
50e473
--- pyOpenSSL-0.13.1/OpenSSL/crypto/crypto.c.exception	2018-05-04 18:56:35.946486574 +0200
50e473
+++ pyOpenSSL-0.13.1/OpenSSL/crypto/crypto.c	2018-05-04 18:56:35.950486667 +0200
50e473
@@ -45,26 +45,72 @@ global_passphrase_callback(char *buf, in
50e473
 
50e473
     func = (PyObject *)cb_arg;
50e473
     argv = Py_BuildValue("(i)", rwflag);
50e473
-    if (argv == NULL)
50e473
+    if (argv == NULL) {
50e473
         return 0;
50e473
+    }
50e473
     ret = PyEval_CallObject(func, argv);
50e473
     Py_DECREF(argv);
50e473
-    if (ret == NULL)
50e473
+    if (ret == NULL) {
50e473
         return 0;
50e473
-    if (!PyBytes_Check(ret))
50e473
-    {
50e473
+    }
50e473
+    if (!PyBytes_Check(ret)) {
50e473
         Py_DECREF(ret);
50e473
         PyErr_SetString(PyExc_ValueError, "String expected");
50e473
         return 0;
50e473
     }
50e473
     nchars = PyBytes_Size(ret);
50e473
-    if (nchars > len)
50e473
-        nchars = len;
50e473
+    if (nchars > len) {
50e473
+       Py_DECREF(ret);
50e473
+       PyErr_SetString(PyExc_ValueError,
50e473
+                        "passphrase returned by callback is too long");
50e473
+       return 0;
50e473
+    }
50e473
     strncpy(buf, PyBytes_AsString(ret), nchars);
50e473
     Py_DECREF(ret);
50e473
     return nchars;
50e473
 }
50e473
 
50e473
+static PyObject *
50e473
+raise_current_error(void)
50e473
+{
50e473
+    if (PyErr_Occurred()) {
50e473
+        /*
50e473
+         * The python exception from callback is more informative than
50e473
+         * OpenSSL's error.
50e473
+         */
50e473
+        flush_error_queue();
50e473
+        return NULL;
50e473
+    }
50e473
+    exception_from_error_queue(crypto_Error);
50e473
+    return NULL;
50e473
+}
50e473
+
50e473
+static int
50e473
+setup_callback(int type, PyObject *pw, pem_password_cb **cb, void **cb_arg) {
50e473
+    if (pw == NULL) {
50e473
+        *cb = NULL;
50e473
+        *cb_arg = NULL;
50e473
+        return 1;
50e473
+    }
50e473
+    if (type != X509_FILETYPE_PEM) {
50e473
+        PyErr_SetString(PyExc_ValueError,
50e473
+                        "only FILETYPE_PEM key format supports encryption");
50e473
+        return 0;
50e473
+    }
50e473
+    if (PyBytes_Check(pw)) {
50e473
+        *cb = NULL;
50e473
+        *cb_arg = PyBytes_AsString(pw);
50e473
+    } else if (PyCallable_Check(pw)) {
50e473
+        *cb = global_passphrase_callback;
50e473
+        *cb_arg = pw;
50e473
+    } else {
50e473
+        PyErr_SetString(PyExc_TypeError,
50e473
+                        "Last argument must be string or callable");
50e473
+        return 0;
50e473
+    }
50e473
+    return 1;
50e473
+}
50e473
+
50e473
 static char crypto_load_privatekey_doc[] = "\n\
50e473
 Load a private key from a buffer\n\
50e473
 \n\
50e473
@@ -89,31 +135,20 @@ crypto_load_privatekey(PyObject *spam, P
50e473
     BIO *bio;
50e473
     EVP_PKEY *pkey;
50e473
 
50e473
-    if (!PyArg_ParseTuple(args, "is#|O:load_privatekey", &type, &buffer, &len, &pw))
50e473
+    if (!PyArg_ParseTuple(args, "is#|O:load_privatekey",
50e473
+                          &type, &buffer, &len, &pw)) {
50e473
+        return NULL;
50e473
+    }
50e473
+    if (!setup_callback(type, pw, &cb, &cb_arg)) {
50e473
         return NULL;
50e473
-
50e473
-    if (pw != NULL)
50e473
-    {
50e473
-        if (PyBytes_Check(pw))
50e473
-        {
50e473
-            cb = NULL;
50e473
-            cb_arg = PyBytes_AsString(pw);
50e473
-        }
50e473
-        else if (PyCallable_Check(pw))
50e473
-        {
50e473
-            cb = global_passphrase_callback;
50e473
-            cb_arg = pw;
50e473
-        }
50e473
-        else
50e473
-        {
50e473
-            PyErr_SetString(PyExc_TypeError, "Last argument must be string or callable");
50e473
-            return NULL;
50e473
-        }
50e473
     }
50e473
 
50e473
     bio = BIO_new_mem_buf(buffer, len);
50e473
-    switch (type)
50e473
-    {
50e473
+    if (bio == NULL) {
50e473
+        exception_from_error_queue(crypto_Error);
50e473
+        return NULL;
50e473
+    }
50e473
+    switch (type) {
50e473
         case X509_FILETYPE_PEM:
50e473
             pkey = PEM_read_bio_PrivateKey(bio, NULL, cb, cb_arg);
50e473
             break;
50e473
@@ -129,10 +164,8 @@ crypto_load_privatekey(PyObject *spam, P
50e473
     }
50e473
     BIO_free(bio);
50e473
 
50e473
-    if (pkey == NULL)
50e473
-    {
50e473
-        exception_from_error_queue(crypto_Error);
50e473
-        return NULL;
50e473
+    if (pkey == NULL) {
50e473
+        return raise_current_error();
50e473
     }
50e473
 
50e473
     return (PyObject *)crypto_PKey_New(pkey, 1);
50e473
@@ -168,49 +201,32 @@ crypto_dump_privatekey(PyObject *spam, P
50e473
     crypto_PKeyObj *pkey;
50e473
 
50e473
     if (!PyArg_ParseTuple(args, "iO!|sO:dump_privatekey", &type,
50e473
-			  &crypto_PKey_Type, &pkey, &cipher_name, &pw))
50e473
+                          &crypto_PKey_Type, &pkey, &cipher_name, &pw)) {
50e473
         return NULL;
50e473
-
50e473
-    if (cipher_name != NULL && pw == NULL)
50e473
-    {
50e473
+    }
50e473
+    if (cipher_name != NULL && pw == NULL) {
50e473
         PyErr_SetString(PyExc_ValueError, "Illegal number of arguments");
50e473
         return NULL;
50e473
     }
50e473
-    if (cipher_name != NULL)
50e473
-    {
50e473
+    if (cipher_name != NULL) {
50e473
         cipher = EVP_get_cipherbyname(cipher_name);
50e473
-        if (cipher == NULL)
50e473
-        {
50e473
+        if (cipher == NULL) {
50e473
             PyErr_SetString(PyExc_ValueError, "Invalid cipher name");
50e473
             return NULL;
50e473
         }
50e473
-        if (PyBytes_Check(pw))
50e473
-        {
50e473
-            cb = NULL;
50e473
-            cb_arg = PyBytes_AsString(pw);
50e473
-        }
50e473
-        else if (PyCallable_Check(pw))
50e473
-        {
50e473
-            cb = global_passphrase_callback;
50e473
-            cb_arg = pw;
50e473
-        }
50e473
-        else
50e473
-        {
50e473
-            PyErr_SetString(PyExc_TypeError, "Last argument must be string or callable");
50e473
+        if (!setup_callback(type, pw, &cb, &cb_arg)) {
50e473
             return NULL;
50e473
         }
50e473
     }
50e473
 
50e473
     bio = BIO_new(BIO_s_mem());
50e473
-    switch (type)
50e473
-    {
50e473
+    if (bio == NULL) {
50e473
+        exception_from_error_queue(crypto_Error);
50e473
+        return NULL;
50e473
+    }
50e473
+    switch (type) {
50e473
         case X509_FILETYPE_PEM:
50e473
             ret = PEM_write_bio_PrivateKey(bio, pkey->pkey, cipher, NULL, 0, cb, cb_arg);
50e473
-            if (PyErr_Occurred())
50e473
-            {
50e473
-                BIO_free(bio);
50e473
-                return NULL;
50e473
-            }
50e473
             break;
50e473
 
50e473
         case X509_FILETYPE_ASN1:
50e473
@@ -219,8 +235,12 @@ crypto_dump_privatekey(PyObject *spam, P
50e473
 
50e473
         case X509_FILETYPE_TEXT:
50e473
             rsa = EVP_PKEY_get1_RSA(pkey->pkey);
50e473
+            if (rsa == NULL) {
50e473
+                ret = 0;
50e473
+                break;
50e473
+            }
50e473
             ret = RSA_print(bio, rsa, 0);
50e473
-            RSA_free(rsa); 
50e473
+            RSA_free(rsa);
50e473
             break;
50e473
 
50e473
         default:
50e473
@@ -229,11 +249,9 @@ crypto_dump_privatekey(PyObject *spam, P
50e473
             return NULL;
50e473
     }
50e473
 
50e473
-    if (ret == 0)
50e473
-    {
50e473
+    if (ret == 0) {
50e473
         BIO_free(bio);
50e473
-        exception_from_error_queue(crypto_Error);
50e473
-        return NULL;
50e473
+        return raise_current_error();
50e473
     }
50e473
 
50e473
     buf_len = BIO_get_mem_data(bio, &temp);
50e473
@@ -513,8 +531,8 @@ crypto_load_pkcs7_data(PyObject *spam, P
50e473
     if (!PyArg_ParseTuple(args, "is#:load_pkcs7_data", &type, &buffer, &len))
50e473
         return NULL;
50e473
 
50e473
-    /* 
50e473
-     * Try to read the pkcs7 data from the bio 
50e473
+    /*
50e473
+     * Try to read the pkcs7 data from the bio
50e473
      */
50e473
     bio = BIO_new_mem_buf(buffer, len);
50e473
     switch (type)
50e473
diff -up pyOpenSSL-0.13.1/OpenSSL/test/test_crypto.py.exception pyOpenSSL-0.13.1/OpenSSL/test/test_crypto.py
50e473
--- pyOpenSSL-0.13.1/OpenSSL/test/test_crypto.py.exception	2018-05-04 18:56:35.948486620 +0200
50e473
+++ pyOpenSSL-0.13.1/OpenSSL/test/test_crypto.py	2018-05-04 18:57:16.363420609 +0200
50e473
@@ -7,7 +7,7 @@ Unit tests for L{OpenSSL.crypto}.
50e473
 
50e473
 from unittest import main
50e473
 
50e473
-import os, re
50e473
+import os, re, sys
50e473
 from subprocess import PIPE, Popen
50e473
 from datetime import datetime, timedelta
50e473
 
50e473
@@ -2038,6 +2038,18 @@ class FunctionTests(TestCase):
50e473
             load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, b("quack"))
50e473
 
50e473
 
50e473
+    def test_load_privatekey_passphraseWrongType(self):
50e473
+        """
50e473
+        :py:obj:`load_privatekey` raises :py:obj:`ValueError` when it is passed a passphrase
50e473
+        with a private key encoded in a format, that doesn't support
50e473
+        encryption.
50e473
+        """
50e473
+        key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
50e473
+        blob = dump_privatekey(FILETYPE_ASN1, key)
50e473
+        self.assertRaises(ValueError,
50e473
+            load_privatekey, FILETYPE_ASN1, blob, "secret")
50e473
+
50e473
+
50e473
     def test_load_privatekey_passphrase(self):
50e473
         """
50e473
         L{load_privatekey} can create a L{PKey} object from an encrypted PEM
50e473
@@ -2058,7 +2070,7 @@ class FunctionTests(TestCase):
50e473
         called = []
50e473
         def cb(*a):
50e473
             called.append(None)
50e473
-            return "quack"
50e473
+            return b("quack")
50e473
         self.assertRaises(
50e473
             Error,
50e473
             load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, cb)
50e473
@@ -2083,25 +2095,36 @@ class FunctionTests(TestCase):
50e473
     def test_load_privatekey_passphrase_exception(self):
50e473
         """
50e473
         An exception raised by the passphrase callback passed to
50e473
-        L{load_privatekey} causes L{OpenSSL.crypto.Error} to be raised.
50e473
-
50e473
-        This isn't as nice as just letting the exception pass through.  The
50e473
-        behavior might be changed to that eventually.
50e473
+        L{load_privatekey} is propagated.
50e473
         """
50e473
         def broken(ignored):
50e473
             raise RuntimeError("This is not working.")
50e473
         self.assertRaises(
50e473
-            Error,
50e473
+            RuntimeError,
50e473
             load_privatekey,
50e473
             FILETYPE_PEM, encryptedPrivateKeyPEM, broken)
50e473
 
50e473
 
50e473
+    def test_load_privatekey_passphrase_wrong_return_type(self):
50e473
+        """
50e473
+        :py:obj:`load_privatekey` raises :py:obj:`ValueError` if the passphrase
50e473
+        callback returns something other than a byte string.
50e473
+        """
50e473
+        self.assertRaises(
50e473
+            ValueError,
50e473
+            load_privatekey,
50e473
+            FILETYPE_PEM, encryptedPrivateKeyPEM, lambda *args: 3)
50e473
+
50e473
+
50e473
     def test_dump_privatekey_wrong_args(self):
50e473
         """
50e473
         L{dump_privatekey} raises L{TypeError} if called with the wrong number
50e473
         of arguments.
50e473
         """
50e473
         self.assertRaises(TypeError, dump_privatekey)
50e473
+        # If cipher name is given, password is required.
50e473
+        self.assertRaises(
50e473
+            ValueError, dump_privatekey, FILETYPE_PEM, PKey(), "foo")
50e473
 
50e473
 
50e473
     def test_dump_privatekey_unknown_cipher(self):
50e473
@@ -2138,6 +2161,18 @@ class FunctionTests(TestCase):
50e473
         self.assertRaises(ValueError, dump_privatekey, 100, key)
50e473
 
50e473
 
50e473
+    def test_load_privatekey_passphraseCallbackLength(self):
50e473
+        """
50e473
+        :py:obj:`crypto.load_privatekey` should raise an error when the passphrase
50e473
+        provided by the callback is too long, not silently truncate it.
50e473
+        """
50e473
+        def cb(ignored):
50e473
+            return "a" * 1025
50e473
+
50e473
+        self.assertRaises(ValueError,
50e473
+            load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, cb)
50e473
+
50e473
+
50e473
     def test_dump_privatekey_passphrase(self):
50e473
         """
50e473
         L{dump_privatekey} writes an encrypted PEM when given a passphrase.
50e473
@@ -2152,6 +2187,17 @@ class FunctionTests(TestCase):
50e473
         self.assertEqual(loadedKey.bits(), key.bits())
50e473
 
50e473
 
50e473
+    def test_dump_privatekey_passphraseWrongType(self):
50e473
+        """
50e473
+        :py:obj:`dump_privatekey` raises :py:obj:`ValueError` when it is passed a passphrase
50e473
+        with a private key encoded in a format, that doesn't support
50e473
+        encryption.
50e473
+        """
50e473
+        key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
50e473
+        self.assertRaises(ValueError,
50e473
+            dump_privatekey, FILETYPE_ASN1, key, "blowfish", "secret")
50e473
+
50e473
+
50e473
     def test_dump_certificate(self):
50e473
         """
50e473
         L{dump_certificate} writes PEM, DER, and text.
50e473
@@ -2230,6 +2276,32 @@ class FunctionTests(TestCase):
50e473
         self.assertEqual(loadedKey.bits(), key.bits())
50e473
 
50e473
 
50e473
+    def test_dump_privatekey_passphrase_exception(self):
50e473
+        """
50e473
+        :py:obj:`dump_privatekey` should not overwrite the exception raised
50e473
+        by the passphrase callback.
50e473
+        """
50e473
+        def cb(ignored):
50e473
+            raise ArithmeticError
50e473
+
50e473
+        key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
50e473
+        self.assertRaises(ArithmeticError,
50e473
+            dump_privatekey, FILETYPE_PEM, key, "blowfish", cb)
50e473
+
50e473
+
50e473
+    def test_dump_privatekey_passphraseCallbackLength(self):
50e473
+        """
50e473
+        :py:obj:`crypto.dump_privatekey` should raise an error when the passphrase
50e473
+        provided by the callback is too long, not silently truncate it.
50e473
+        """
50e473
+        def cb(ignored):
50e473
+            return "a" * 1025
50e473
+
50e473
+        key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
50e473
+        self.assertRaises(ValueError,
50e473
+            dump_privatekey, FILETYPE_PEM, key, "blowfish", cb)
50e473
+
50e473
+
50e473
     def test_load_pkcs7_data(self):
50e473
         """
50e473
         L{load_pkcs7_data} accepts a PKCS#7 string and returns an instance of