Fix type definitions, module init return values, and unicode/bytes conversions
diff --git a/OpenSSL/crypto/crl.c b/OpenSSL/crypto/crl.c
index c9abbab..83691fb 100644
--- a/OpenSSL/crypto/crl.c
+++ b/OpenSSL/crypto/crl.c
@@ -232,7 +232,7 @@
 }
 
 PyTypeObject crypto_CRL_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "CRL",
     sizeof(crypto_CRLObj),
     0,
diff --git a/OpenSSL/crypto/crypto.c b/OpenSSL/crypto/crypto.c
index d71e3ec..87f3e75 100644
--- a/OpenSSL/crypto/crypto.c
+++ b/OpenSSL/crypto/crypto.c
@@ -805,7 +805,7 @@
 #endif
 
     if (module == NULL) {
-        return NULL;
+        PyOpenSSL_MODRETURN(NULL);
     }
 
 #ifndef PY3
@@ -863,14 +863,9 @@
     if (!init_crypto_revoked(module))
         goto error;
 
-#ifdef PY3
-    return module;
-#endif
+    PyOpenSSL_MODRETURN(module);
 
 error:
-#ifdef PY3
-    return NULL;
-#else
+    PyOpenSSL_MODRETURN(NULL);
     ;
-#endif
 }
diff --git a/OpenSSL/crypto/netscape_spki.c b/OpenSSL/crypto/netscape_spki.c
index ab3d463..ff40962 100644
--- a/OpenSSL/crypto/netscape_spki.c
+++ b/OpenSSL/crypto/netscape_spki.c
@@ -244,7 +244,7 @@
 #undef ADD_METHOD
 
 PyTypeObject crypto_NetscapeSPKI_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "NetscapeSPKI",
     sizeof(crypto_NetscapeSPKIObj),
     0,
diff --git a/OpenSSL/crypto/pkcs12.c b/OpenSSL/crypto/pkcs12.c
index 5483e42..18c0970 100644
--- a/OpenSSL/crypto/pkcs12.c
+++ b/OpenSSL/crypto/pkcs12.c
@@ -498,7 +498,7 @@
 }
 
 PyTypeObject crypto_PKCS12_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "PKCS12",
     sizeof(crypto_PKCS12Obj),
     0,
diff --git a/OpenSSL/crypto/pkcs7.c b/OpenSSL/crypto/pkcs7.c
index 8bfddb3..fff95e2 100644
--- a/OpenSSL/crypto/pkcs7.c
+++ b/OpenSSL/crypto/pkcs7.c
@@ -161,7 +161,7 @@
 }
 
 PyTypeObject crypto_PKCS7_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "PKCS7",
     sizeof(crypto_PKCS7Obj),
     0,
diff --git a/OpenSSL/crypto/pkey.c b/OpenSSL/crypto/pkey.c
index 82b9e06..6494d2a 100644
--- a/OpenSSL/crypto/pkey.c
+++ b/OpenSSL/crypto/pkey.c
@@ -197,7 +197,7 @@
 }
 
 PyTypeObject crypto_PKey_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "OpenSSL.crypto.PKey",
     sizeof(crypto_PKeyObj),
     0,
diff --git a/OpenSSL/crypto/revoked.c b/OpenSSL/crypto/revoked.c
index 95cfcf4..08c1542 100644
--- a/OpenSSL/crypto/revoked.c
+++ b/OpenSSL/crypto/revoked.c
@@ -387,7 +387,7 @@
 }
 
 PyTypeObject crypto_Revoked_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "Revoked",
     sizeof(crypto_RevokedObj),
     0,
diff --git a/OpenSSL/crypto/x509.c b/OpenSSL/crypto/x509.c
index 73f5e08..a426466 100644
--- a/OpenSSL/crypto/x509.c
+++ b/OpenSSL/crypto/x509.c
@@ -110,12 +110,20 @@
         goto err;
     }
 
+#ifdef PY3
+    {
+        PyObject *hexbytes = PyUnicode_AsASCIIString(hex);
+        Py_DECREF(hex);
+        hex = hexbytes;
+    }
+#endif
+
     /**
      * BN_hex2bn stores the result in &bignum.  Unless it doesn't feel like
      * it.  If bignum is still NULL after this call, then the return value
      * is actually the result.  I hope.  -exarkun
      */
-    small_serial = BN_hex2bn(&bignum, _PyUnicode_AsString(hex));
+    small_serial = BN_hex2bn(&bignum, PyBytes_AsString(hex));
 
     Py_DECREF(hex);
     hex = NULL;
@@ -764,7 +772,7 @@
 }
 
 PyTypeObject crypto_X509_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "X509",
     sizeof(crypto_X509Obj),
     0,
diff --git a/OpenSSL/crypto/x509ext.c b/OpenSSL/crypto/x509ext.c
index 995a079..28e8805 100644
--- a/OpenSSL/crypto/x509ext.c
+++ b/OpenSSL/crypto/x509ext.c
@@ -240,7 +240,7 @@
 }
 
 PyTypeObject crypto_X509Extension_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "X509Extension",
     sizeof(crypto_X509ExtensionObj),
     0,
diff --git a/OpenSSL/crypto/x509name.c b/OpenSSL/crypto/x509name.c
index 83f3c21..7cb7fed 100644
--- a/OpenSSL/crypto/x509name.c
+++ b/OpenSSL/crypto/x509name.c
@@ -152,7 +152,11 @@
 {
     int nid, len;
     char *utf8string;
-    char *name = _PyUnicode_AsString(nameobj);
+    char *name;
+#ifdef PY3
+    nameobj = PyUnicode_AsASCIIString(nameobj);
+#endif
+    name = PyBytes_AsString(nameobj);
 
     if ((nid = OBJ_txt2nid(name)) == NID_undef) {
         /*
@@ -456,7 +460,7 @@
 #undef ADD_METHOD
 
 PyTypeObject crypto_X509Name_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "X509Name",
     sizeof(crypto_X509NameObj),
     0,
diff --git a/OpenSSL/crypto/x509req.c b/OpenSSL/crypto/x509req.c
index 4237a45..23c0218 100644
--- a/OpenSSL/crypto/x509req.c
+++ b/OpenSSL/crypto/x509req.c
@@ -358,7 +358,7 @@
 
 
 PyTypeObject crypto_X509Req_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "X509Req",
     sizeof(crypto_X509ReqObj),
     0,
diff --git a/OpenSSL/crypto/x509store.c b/OpenSSL/crypto/x509store.c
index 1452df6..30ae508 100644
--- a/OpenSSL/crypto/x509store.c
+++ b/OpenSSL/crypto/x509store.c
@@ -93,7 +93,7 @@
 
 
 PyTypeObject crypto_X509Store_Type = {
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
     "X509Store",
     sizeof(crypto_X509StoreObj),
     0,