Blame SOURCES/pyOpenSSL-0.13.1-exception.patch

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