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

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