Blame SOURCES/Use-imported-soft-pkcs11-for-tests.patch

92593c
From 43f5837eecd5022c525efcfb3605af16958dc59a Mon Sep 17 00:00:00 2001
92593c
From: Greg Hudson <ghudson@mit.edu>
92593c
Date: Thu, 20 Jun 2019 13:41:57 -0400
92593c
Subject: [PATCH] Use imported soft-pkcs11 for tests
92593c
92593c
Update the soft-pkcs11 code for OpenSSL 1.1, fix some warnings,
92593c
integrate it into the build system, and use it for the PKINIT tests.
92593c
92593c
(cherry picked from commit e5ef7b69765353ea62ad8712a229ed4e90a8fe17)
92593c
(cherry picked from commit 47e66724b9d5cfef84965d99c83d29e4739932e3)
92593c
---
92593c
 src/configure.in                        |   1 +
92593c
 src/tests/Makefile.in                   |   2 +-
92593c
 src/tests/softpkcs11/Makefile.in        |  21 ++++
92593c
 src/tests/softpkcs11/deps               |   6 ++
92593c
 src/tests/softpkcs11/main.c             | 124 +++++++++++++++++-------
92593c
 src/tests/softpkcs11/softpkcs11.exports |  39 ++++++++
92593c
 src/tests/t_pkinit.py                   |  18 +---
92593c
 7 files changed, 162 insertions(+), 49 deletions(-)
92593c
 create mode 100644 src/tests/softpkcs11/Makefile.in
92593c
 create mode 100644 src/tests/softpkcs11/deps
92593c
 create mode 100644 src/tests/softpkcs11/softpkcs11.exports
92593c
92593c
diff --git a/src/configure.in b/src/configure.in
92593c
index 93aec682e..9f6b67b44 100644
92593c
--- a/src/configure.in
92593c
+++ b/src/configure.in
92593c
@@ -1086,6 +1086,7 @@ int i = 1;
92593c
 fi
92593c
 if test "$k5_cv_openssl_version_okay" = yes && (test "$enable_pkinit" = yes || test "$enable_pkinit" = try); then
92593c
   K5_GEN_MAKEFILE(plugins/preauth/pkinit)
92593c
+  K5_GEN_MAKEFILE(tests/softpkcs11)
92593c
   PKINIT=yes
92593c
   AC_CHECK_LIB(crypto, CMS_get0_content, [AC_DEFINE([HAVE_OPENSSL_CMS], 1, [Define if OpenSSL supports cms.])])
92593c
 elif test "$k5_cv_openssl_version_okay" = no && test "$enable_pkinit" = yes; then
92593c
diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in
92593c
index e27617ee2..ab958eb4c 100644
92593c
--- a/src/tests/Makefile.in
92593c
+++ b/src/tests/Makefile.in
92593c
@@ -1,7 +1,7 @@
92593c
 mydir=tests
92593c
 BUILDTOP=$(REL)..
92593c
 SUBDIRS = resolve asn.1 create hammer verify gssapi dejagnu shlib \
92593c
-	gss-threads misc threads
92593c
+	gss-threads misc threads softpkcs11
92593c
 
92593c
 RUN_DB_TEST = $(RUN_SETUP) KRB5_KDC_PROFILE=kdc.conf KRB5_CONFIG=krb5.conf \
92593c
 	LC_ALL=C $(VALGRIND)
92593c
diff --git a/src/tests/softpkcs11/Makefile.in b/src/tests/softpkcs11/Makefile.in
92593c
new file mode 100644
92593c
index 000000000..e89678154
92593c
--- /dev/null
92593c
+++ b/src/tests/softpkcs11/Makefile.in
92593c
@@ -0,0 +1,21 @@
92593c
+mydir=tests$(S)softpkcs11
92593c
+BUILDTOP=$(REL)..$(S)..
92593c
+
92593c
+LOCALINCLUDES = -I$(top_srcdir)/plugins/preauth/pkinit
92593c
+
92593c
+LIBBASE=softpkcs11
92593c
+LIBMAJOR=0
92593c
+LIBMINOR=0
92593c
+
92593c
+SHLIB_EXPLIBS=$(SUPPORT_LIB) -lcrypto
92593c
+SHLIB_EXPDEPS=$(SUPPORT_DEPLIB)
92593c
+
92593c
+STLIBOBJS=main.o
92593c
+
92593c
+SRCS=$(srcdir)/main.c
92593c
+
92593c
+all-unix: all-libs
92593c
+clean-unix:: clean-libs clean-libobjs
92593c
+
92593c
+@libnover_frag@
92593c
+@libobj_frag@
92593c
diff --git a/src/tests/softpkcs11/deps b/src/tests/softpkcs11/deps
92593c
new file mode 100644
92593c
index 000000000..1e82d9572
92593c
--- /dev/null
92593c
+++ b/src/tests/softpkcs11/deps
92593c
@@ -0,0 +1,6 @@
92593c
+#
92593c
+# Generated makefile dependencies follow.
92593c
+#
92593c
+main.so main.po $(OUTPRE)main.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
92593c
+  $(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
92593c
+  $(top_srcdir)/plugins/preauth/pkinit/pkcs11.h main.c
92593c
diff --git a/src/tests/softpkcs11/main.c b/src/tests/softpkcs11/main.c
92593c
index 2acec5169..5255323d3 100644
92593c
--- a/src/tests/softpkcs11/main.c
92593c
+++ b/src/tests/softpkcs11/main.c
92593c
@@ -1,3 +1,4 @@
92593c
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
92593c
 /*
92593c
  * Copyright (c) 2004-2006, Stockholms universitet
92593c
  * (Stockholm University, Stockholm Sweden)
92593c
@@ -31,7 +32,57 @@
92593c
  * POSSIBILITY OF SUCH DAMAGE.
92593c
  */
92593c
 
92593c
-#include "locl.h"
92593c
+#include "k5-platform.h"
92593c
+
92593c
+#include <openssl/err.h>
92593c
+#include <openssl/evp.h>
92593c
+#include <openssl/pem.h>
92593c
+#include <openssl/rand.h>
92593c
+#include <openssl/x509.h>
92593c
+
92593c
+#include <ctype.h>
92593c
+#include <pwd.h>
92593c
+
92593c
+#include <pkcs11.h>
92593c
+
92593c
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
92593c
+#define EVP_PKEY_get0_RSA(key) ((key)->pkey.rsa)
92593c
+#define RSA_PKCS1_OpenSSL RSA_PKCS1_SSLeay
92593c
+#define RSA_get0_key compat_rsa_get0_key
92593c
+static void
92593c
+compat_rsa_get0_key(const RSA *rsa, const BIGNUM **n, const BIGNUM **e,
92593c
+                    const BIGNUM **d)
92593c
+{
92593c
+    if (n != NULL)
92593c
+        *n = rsa->n;
92593c
+    if (e != NULL)
92593c
+        *e = rsa->e;
92593c
+    if (d != NULL)
92593c
+        *d = rsa->d;
92593c
+}
92593c
+#endif
92593c
+
92593c
+#define OPENSSL_ASN1_MALLOC_ENCODE(T, B, BL, S, R)      \
92593c
+    {                                                   \
92593c
+        unsigned char *p;                               \
92593c
+        (BL) = i2d_##T((S), NULL);                      \
92593c
+        if ((BL) <= 0) {                                \
92593c
+            (R) = EINVAL;                               \
92593c
+        } else {                                        \
92593c
+            (B) = malloc((BL));                         \
92593c
+            if ((B) == NULL) {                          \
92593c
+                (R) = ENOMEM;                           \
92593c
+            } else {                                    \
92593c
+                p = (B);                                \
92593c
+                (R) = 0;                                \
92593c
+                (BL) = i2d_##T((S), &p);                \
92593c
+                if ((BL) <= 0) {                        \
92593c
+                    free((B));                          \
92593c
+                    (R) = EINVAL;                       \
92593c
+                }                                       \
92593c
+            }                                           \
92593c
+        }                                               \
92593c
+    }
92593c
 
92593c
 /* RCSID("$Id: main.c,v 1.24 2006/01/11 12:42:53 lha Exp $"); */
92593c
 
92593c
@@ -124,7 +175,7 @@ st_logf(const char *fmt, ...)
92593c
 }
92593c
 
92593c
 static void
92593c
-snprintf_fill(char *str, size_t size, char fillchar, const char *fmt, ...)
92593c
+snprintf_fill(char *str, int size, char fillchar, const char *fmt, ...)
92593c
 {
92593c
     int len;
92593c
     va_list ap;
92593c
@@ -141,19 +192,19 @@ snprintf_fill(char *str, size_t size, char fillchar, const char *fmt, ...)
92593c
 #endif
92593c
 
92593c
 #define VERIFY_SESSION_HANDLE(s, state)                 \
92593c
-{                                                       \
92593c
-    CK_RV ret;                                          \
92593c
-    ret = verify_session_handle(s, state);              \
92593c
-    if (ret != CKR_OK) {                                \
92593c
-        /* return CKR_OK */;                            \
92593c
-    }                                                   \
92593c
-}
92593c
+    {                                                   \
92593c
+        CK_RV vshret;                                   \
92593c
+        vshret = verify_session_handle(s, state);       \
92593c
+        if (vshret != CKR_OK) {                         \
92593c
+            /* return CKR_OK */;                        \
92593c
+        }                                               \
92593c
+    }
92593c
 
92593c
 static CK_RV
92593c
 verify_session_handle(CK_SESSION_HANDLE hSession,
92593c
                       struct session_state **state)
92593c
 {
92593c
-    int i;
92593c
+    size_t i;
92593c
 
92593c
     for (i = 0; i < MAX_NUM_SESSION; i++){
92593c
         if (soft_token.state[i].session_handle == hSession)
92593c
@@ -361,16 +412,20 @@ add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
92593c
         CK_ULONG modulus_bits = 0;
92593c
         CK_BYTE *exponent = NULL;
92593c
         size_t exponent_len = 0;
92593c
+        RSA *rsa;
92593c
+        const BIGNUM *n, *e;
92593c
 
92593c
-        modulus_bits = BN_num_bits(key->pkey.rsa->n);
92593c
+        rsa = EVP_PKEY_get0_RSA(key);
92593c
+        RSA_get0_key(rsa, &n, &e, NULL);
92593c
+        modulus_bits = BN_num_bits(n);
92593c
 
92593c
-        modulus_len = BN_num_bytes(key->pkey.rsa->n);
92593c
+        modulus_len = BN_num_bytes(n);
92593c
         modulus = malloc(modulus_len);
92593c
-        BN_bn2bin(key->pkey.rsa->n, modulus);
92593c
+        BN_bn2bin(n, modulus);
92593c
 
92593c
-        exponent_len = BN_num_bytes(key->pkey.rsa->e);
92593c
+        exponent_len = BN_num_bytes(e);
92593c
         exponent = malloc(exponent_len);
92593c
-        BN_bn2bin(key->pkey.rsa->e, exponent);
92593c
+        BN_bn2bin(e, exponent);
92593c
 
92593c
         add_object_attribute(o, 0, CKA_MODULUS, modulus, modulus_len);
92593c
         add_object_attribute(o, 0, CKA_MODULUS_BITS,
92593c
@@ -378,7 +433,7 @@ add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
92593c
         add_object_attribute(o, 0, CKA_PUBLIC_EXPONENT,
92593c
                              exponent, exponent_len);
92593c
 
92593c
-        RSA_set_method(key->pkey.rsa, RSA_PKCS1_SSLeay());
92593c
+        RSA_set_method(rsa, RSA_PKCS1_OpenSSL());
92593c
 
92593c
         free(modulus);
92593c
         free(exponent);
92593c
@@ -474,7 +529,7 @@ add_certificate(char *label,
92593c
     o->u.cert = cert;
92593c
     public_key = X509_get_pubkey(o->u.cert);
92593c
 
92593c
-    switch (EVP_PKEY_type(public_key->type)) {
92593c
+    switch (EVP_PKEY_base_id(public_key)) {
92593c
     case EVP_PKEY_RSA:
92593c
         key_type = CKK_RSA;
92593c
         break;
92593c
@@ -604,8 +659,8 @@ add_certificate(char *label,
92593c
             /* XXX verify keytype */
92593c
 
92593c
             if (key_type == CKK_RSA)
92593c
-                RSA_set_method(o->u.private_key.key->pkey.rsa,
92593c
-                               RSA_PKCS1_SSLeay());
92593c
+                RSA_set_method(EVP_PKEY_get0_RSA(o->u.private_key.key),
92593c
+                               RSA_PKCS1_OpenSSL());
92593c
 
92593c
             if (X509_check_private_key(cert, o->u.private_key.key) != 1) {
92593c
                 EVP_PKEY_free(o->u.private_key.key);
92593c
@@ -755,8 +810,9 @@ CK_RV
92593c
 C_Initialize(CK_VOID_PTR a)
92593c
 {
92593c
     CK_C_INITIALIZE_ARGS_PTR args = a;
92593c
+    size_t i;
92593c
+
92593c
     st_logf("Initialize\n");
92593c
-    int i;
92593c
 
92593c
     OpenSSL_add_all_algorithms();
92593c
     ERR_load_crypto_strings();
92593c
@@ -825,7 +881,7 @@ C_Initialize(CK_VOID_PTR a)
92593c
 CK_RV
92593c
 C_Finalize(CK_VOID_PTR args)
92593c
 {
92593c
-    int i;
92593c
+    size_t i;
92593c
 
92593c
     st_logf("Finalize\n");
92593c
 
92593c
@@ -1008,7 +1064,7 @@ C_OpenSession(CK_SLOT_ID slotID,
92593c
               CK_NOTIFY Notify,
92593c
               CK_SESSION_HANDLE_PTR phSession)
92593c
 {
92593c
-    int i;
92593c
+    size_t i;
92593c
 
92593c
     st_logf("OpenSession: slot: %d\n", (int)slotID);
92593c
 
92593c
@@ -1050,7 +1106,7 @@ C_CloseSession(CK_SESSION_HANDLE hSession)
92593c
 CK_RV
92593c
 C_CloseAllSessions(CK_SLOT_ID slotID)
92593c
 {
92593c
-    int i;
92593c
+    size_t i;
92593c
 
92593c
     st_logf("CloseAllSessions\n");
92593c
 
92593c
@@ -1127,7 +1183,8 @@ C_Login(CK_SESSION_HANDLE hSession,
92593c
         }
92593c
 
92593c
         /* XXX check keytype */
92593c
-        RSA_set_method(o->u.private_key.key->pkey.rsa, RSA_PKCS1_SSLeay());
92593c
+        RSA_set_method(EVP_PKEY_get0_RSA(o->u.private_key.key),
92593c
+                       RSA_PKCS1_OpenSSL());
92593c
 
92593c
         if (X509_check_private_key(o->u.private_key.cert, o->u.private_key.key) != 1) {
92593c
             EVP_PKEY_free(o->u.private_key.key);
92593c
@@ -1226,7 +1283,6 @@ C_FindObjectsInit(CK_SESSION_HANDLE hSession,
92593c
     }
92593c
     if (ulCount) {
92593c
         CK_ULONG i;
92593c
-        size_t len;
92593c
 
92593c
         print_attributes(pTemplate, ulCount);
92593c
 
92593c
@@ -1415,7 +1471,7 @@ C_Encrypt(CK_SESSION_HANDLE hSession,
92593c
         return CKR_ARGUMENTS_BAD;
92593c
     }
92593c
 
92593c
-    rsa = o->u.public_key->pkey.rsa;
92593c
+    rsa = EVP_PKEY_get0_RSA(o->u.public_key);
92593c
 
92593c
     if (rsa == NULL)
92593c
         return CKR_ARGUMENTS_BAD;
92593c
@@ -1445,7 +1501,7 @@ C_Encrypt(CK_SESSION_HANDLE hSession,
92593c
         goto out;
92593c
     }
92593c
 
92593c
-    if (buffer_len + padding_len < ulDataLen) {
92593c
+    if ((CK_ULONG)buffer_len + padding_len < ulDataLen) {
92593c
         ret = CKR_ARGUMENTS_BAD;
92593c
         goto out;
92593c
     }
92593c
@@ -1566,7 +1622,7 @@ C_Decrypt(CK_SESSION_HANDLE hSession,
92593c
         return CKR_ARGUMENTS_BAD;
92593c
     }
92593c
 
92593c
-    rsa = o->u.private_key.key->pkey.rsa;
92593c
+    rsa = EVP_PKEY_get0_RSA(o->u.private_key.key);
92593c
 
92593c
     if (rsa == NULL)
92593c
         return CKR_ARGUMENTS_BAD;
92593c
@@ -1596,7 +1652,7 @@ C_Decrypt(CK_SESSION_HANDLE hSession,
92593c
         goto out;
92593c
     }
92593c
 
92593c
-    if (buffer_len + padding_len < ulEncryptedDataLen) {
92593c
+    if ((CK_ULONG)buffer_len + padding_len < ulEncryptedDataLen) {
92593c
         ret = CKR_ARGUMENTS_BAD;
92593c
         goto out;
92593c
     }
92593c
@@ -1725,7 +1781,7 @@ C_Sign(CK_SESSION_HANDLE hSession,
92593c
         return CKR_ARGUMENTS_BAD;
92593c
     }
92593c
 
92593c
-    rsa = o->u.private_key.key->pkey.rsa;
92593c
+    rsa = EVP_PKEY_get0_RSA(o->u.private_key.key);
92593c
 
92593c
     if (rsa == NULL)
92593c
         return CKR_ARGUMENTS_BAD;
92593c
@@ -1754,7 +1810,7 @@ C_Sign(CK_SESSION_HANDLE hSession,
92593c
         goto out;
92593c
     }
92593c
 
92593c
-    if (buffer_len < ulDataLen + padding_len) {
92593c
+    if ((CK_ULONG)buffer_len < ulDataLen + padding_len) {
92593c
         ret = CKR_ARGUMENTS_BAD;
92593c
         goto out;
92593c
     }
92593c
@@ -1872,7 +1928,7 @@ C_Verify(CK_SESSION_HANDLE hSession,
92593c
         return CKR_ARGUMENTS_BAD;
92593c
     }
92593c
 
92593c
-    rsa = o->u.public_key->pkey.rsa;
92593c
+    rsa = EVP_PKEY_get0_RSA(o->u.public_key);
92593c
 
92593c
     if (rsa == NULL)
92593c
         return CKR_ARGUMENTS_BAD;
92593c
@@ -1900,7 +1956,7 @@ C_Verify(CK_SESSION_HANDLE hSession,
92593c
         goto out;
92593c
     }
92593c
 
92593c
-    if (buffer_len < ulDataLen) {
92593c
+    if ((CK_ULONG)buffer_len < ulDataLen) {
92593c
         ret = CKR_ARGUMENTS_BAD;
92593c
         goto out;
92593c
     }
92593c
@@ -1926,7 +1982,7 @@ C_Verify(CK_SESSION_HANDLE hSession,
92593c
     if (len > buffer_len)
92593c
         abort();
92593c
 
92593c
-    if (len != ulSignatureLen) {
92593c
+    if ((CK_ULONG)len != ulSignatureLen) {
92593c
         ret = CKR_GENERAL_ERROR;
92593c
         goto out;
92593c
     }
92593c
diff --git a/src/tests/softpkcs11/softpkcs11.exports b/src/tests/softpkcs11/softpkcs11.exports
92593c
new file mode 100644
92593c
index 000000000..aa7284511
92593c
--- /dev/null
92593c
+++ b/src/tests/softpkcs11/softpkcs11.exports
92593c
@@ -0,0 +1,39 @@
92593c
+C_CloseAllSessions
92593c
+C_CloseSession
92593c
+C_Decrypt
92593c
+C_DecryptFinal
92593c
+C_DecryptInit
92593c
+C_DecryptUpdate
92593c
+C_DigestInit
92593c
+C_Encrypt
92593c
+C_EncryptFinal
92593c
+C_EncryptInit
92593c
+C_EncryptUpdate
92593c
+C_Finalize
92593c
+C_FindObjects
92593c
+C_FindObjectsFinal
92593c
+C_FindObjectsInit
92593c
+C_GenerateRandom
92593c
+C_GetAttributeValue
92593c
+C_GetFunctionList
92593c
+C_GetInfo
92593c
+C_GetMechanismInfo
92593c
+C_GetMechanismList
92593c
+C_GetObjectSize
92593c
+C_GetSessionInfo
92593c
+C_GetSlotInfo
92593c
+C_GetSlotList
92593c
+C_GetTokenInfo
92593c
+C_Initialize
92593c
+C_InitToken
92593c
+C_Login
92593c
+C_Logout
92593c
+C_OpenSession
92593c
+C_Sign
92593c
+C_SignFinal
92593c
+C_SignInit
92593c
+C_SignUpdate
92593c
+C_Verify
92593c
+C_VerifyFinal
92593c
+C_VerifyInit
92593c
+C_VerifyUpdate
92593c
diff --git a/src/tests/t_pkinit.py b/src/tests/t_pkinit.py
92593c
index 1dadb1b96..384bf1426 100755
92593c
--- a/src/tests/t_pkinit.py
92593c
+++ b/src/tests/t_pkinit.py
92593c
@@ -4,14 +4,7 @@ from k5test import *
92593c
 if not os.path.exists(os.path.join(plugins, 'preauth', 'pkinit.so')):
92593c
     skip_rest('PKINIT tests', 'PKINIT module not built')
92593c
 
92593c
-# Check if soft-pkcs11.so is available.
92593c
-try:
92593c
-    import ctypes
92593c
-    lib = ctypes.LibraryLoader(ctypes.CDLL).LoadLibrary('soft-pkcs11.so')
92593c
-    del lib
92593c
-    have_soft_pkcs11 = True
92593c
-except:
92593c
-    have_soft_pkcs11 = False
92593c
+soft_pkcs11 = os.path.join(buildtop, 'tests', 'softpkcs11', 'softpkcs11.so')
92593c
 
92593c
 # Construct a krb5.conf fragment configuring pkinit.
92593c
 certs = os.path.join(srctop, 'tests', 'dejagnu', 'pkinit-certs')
92593c
@@ -69,9 +62,9 @@ p12_upn2_identity = 'PKCS12:%s' % user_upn2_p12
92593c
 p12_upn3_identity = 'PKCS12:%s' % user_upn3_p12
92593c
 p12_generic_identity = 'PKCS12:%s' % generic_p12
92593c
 p12_enc_identity = 'PKCS12:%s' % user_enc_p12
92593c
-p11_identity = 'PKCS11:soft-pkcs11.so'
92593c
-p11_token_identity = ('PKCS11:module_name=soft-pkcs11.so:'
92593c
-                      'slotid=1:token=SoftToken (token)')
92593c
+p11_identity = 'PKCS11:' + soft_pkcs11
92593c
+p11_token_identity = ('PKCS11:module_name=' + soft_pkcs11 +
92593c
+                      ':slotid=1:token=SoftToken (token)')
92593c
 
92593c
 # Start a realm with the test kdb module for the following UPN SAN tests.
92593c
 realm = K5Realm(krb5_conf=pkinit_krb5_conf, kdc_conf=alias_kdc_conf,
92593c
@@ -398,9 +391,6 @@ realm.klist(realm.user_princ)
92593c
 realm.kinit(realm.user_princ, flags=['-X', 'X509_user_identity=,'],
92593c
             expected_code=1, expected_msg='Preauthentication failed while')
92593c
 
92593c
-if not have_soft_pkcs11:
92593c
-    skip_rest('PKINIT PKCS11 tests', 'soft-pkcs11.so not found')
92593c
-
92593c
 softpkcs11rc = os.path.join(os.getcwd(), 'testdir', 'soft-pkcs11.rc')
92593c
 realm.env['SOFTPKCS11RC'] = softpkcs11rc
92593c