Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
This includes a very skeleton provider I started.
Two operations are somewhat functional
Exporting public keys:
openssl pkey -in "pkcs11:type=public" -pubin -pubout -out testkey.pub
Signing a hash:
openssl pkeyutl -sign -in testhash.txt -inkey pkcs11:type=private -out sig

Signed-off-by: Simo Sorce <[email protected]>
  • Loading branch information
simo5 committed Jul 3, 2022
0 parents commit 4feaea0
Show file tree
Hide file tree
Showing 19 changed files with 5,885 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
INSTALL
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
compile
config.guess
config.log
config.status
config.sub
configure
depcomp
install-sh
libtool
ltmain.sh
missing
src/.deps/
src/Makefile
src/Makefile.in
src/config.h
src/config.h.in
src/stamp-h1
cscope.out
src/.libs/
src/*.la
src/*.lo
src/*.o
Empty file added AUTHORS
Empty file.
2 changes: 2 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The license of use has not been decided yet.
It will be a Free Software/Open Source license.
Empty file added ChangeLog
Empty file.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUBDIRS = src
dist_doc_DATA = README
Empty file added NEWS
Empty file.
8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is an Openssl 3.x provider to access Hardware or Software Tokens
using the PKCS#11 Cryptographic Token Interface

This code targets version 3.0 of the interface but should be backwards
compatible to previous versions as well.

Spec:
https://docs.oasis-open.org/pkcs11/pkcs11-base/v3.0/os/pkcs11-base-v3.0-os.html
39 changes: 39 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.71])
AC_INIT([pkcs11prov], [0.1], [[email protected]])
AC_CONFIG_SRCDIR([src/provider.c])
AC_CONFIG_HEADERS([src/config.h])
AM_INIT_AUTOMAKE

# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
PKG_PROG_PKG_CONFIG

# Checks for libraries.
PKG_CHECK_MODULES(
[OPENSSL],
[libcrypto >= 3.0.0],
,
[AC_MSG_ERROR([libcrypto >= 3.0.0 is required])]
)
AC_SUBST([SHARED_EXT], $(eval echo "${shrext_cmds}"))

# Checks for header files.
AC_CHECK_HEADERS([string.h dlfcn.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_CHECK_FUNCS([strpbrk])

AC_CONFIG_FILES([
Makefile
src/Makefile
])
AC_OUTPUT
19 changes: 19 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

noinst_HEADERS = pkcs11.h provider.h
lib_LTLIBRARIES = pkcs11prov.la

SHARED_EXT=@SHARED_EXT@

pkcs11prov_la_SOURCES = \
provider.c \
provider_store.c \
provider_keymgmt.c \
provider_signature.c

pkcs11prov_la_CFLAGS = $(AM_CFLAGS) $(OPENSSL_CFLAGS)
pkcs11prov_la_LIBADD = $(OPENSSL_LIBS)

pkcs11prov_la_LDFLAGS = $(AM_LDFLAGS) -module -shared -shrext $(SHARED_EXT) \
-avoid-version -export-symbols "$(srcdir)/provider.exports"


243 changes: 243 additions & 0 deletions src/oasis/pkcs11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/* Copyright (c) OASIS Open 2016-2019. All Rights Reserved.
* Distributed under the terms of the OASIS IPR Policy,
* [http://www.oasis-open.org/policies-guidelines/ipr], AS-IS, WITHOUT ANY
* IMPLIED OR EXPRESS WARRANTY; there is no warranty of MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE or NONINFRINGEMENT of the rights of others.
*/

#ifndef _PKCS11_H_
#define _PKCS11_H_ 1

#ifdef __cplusplus
extern "C" {
#endif

/* Before including this file (pkcs11.h) (or pkcs11t.h by
* itself), 5 platform-specific macros must be defined. These
* macros are described below, and typical definitions for them
* are also given. Be advised that these definitions can depend
* on both the platform and the compiler used (and possibly also
* on whether a Cryptoki library is linked statically or
* dynamically).
*
* In addition to defining these 5 macros, the packing convention
* for Cryptoki structures should be set. The Cryptoki
* convention on packing is that structures should be 1-byte
* aligned.
*
* If you're using Windows this might be done by using the following
* preprocessor directive before including pkcs11.h or pkcs11t.h:
*
* #pragma pack(push, cryptoki, 1)
*
* and using the following preprocessor directive after including
* pkcs11.h or pkcs11t.h:
*
* #pragma pack(pop, cryptoki)
*
* In a UNIX environment, you're on your own for this. You might
* not need to do (or be able to do!) anything.
*
*
* Now for the macros:
*
*
* 1. CK_PTR: The indirection string for making a pointer to an
* object. It can be used like this:
*
* typedef CK_BYTE CK_PTR CK_BYTE_PTR;
*
* If you're using windows, it might be defined by:
*
* #define CK_PTR *
*
* In a typical UNIX environment, it might be defined by:
*
* #define CK_PTR *
*
*
* 2. CK_DECLARE_FUNCTION(returnType, name): A macro which makes
* an importable Cryptoki library function declaration out of a
* return type and a function name. It should be used in the
* following fashion:
*
* extern CK_DECLARE_FUNCTION(CK_RV, C_Initialize)(
* CK_VOID_PTR pReserved
* );
*
* If you're using Windows to declare a function in a Win32 cryptoki .dll,
* it might be defined by:
*
* #define CK_DECLARE_FUNCTION(returnType, name) \
* returnType __declspec(dllimport) name
*
* In a UNIX environment, it might be defined by:
*
* #define CK_DECLARE_FUNCTION(returnType, name) \
* returnType name
*
*
* 3. CK_DECLARE_FUNCTION_POINTER(returnType, name): A macro
* which makes a Cryptoki API function pointer declaration or
* function pointer type declaration out of a return type and a
* function name. It should be used in the following fashion:
*
* // Define funcPtr to be a pointer to a Cryptoki API function
* // taking arguments args and returning CK_RV.
* CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtr)(args);
*
* or
*
* // Define funcPtrType to be the type of a pointer to a
* // Cryptoki API function taking arguments args and returning
* // CK_RV, and then define funcPtr to be a variable of type
* // funcPtrType.
* typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtrType)(args);
* funcPtrType funcPtr;
*
* If you're using Windows to access
* functions in a Win32 Cryptoki .dll, in might be defined by:
*
* #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \
* returnType __declspec(dllimport) (* name)
*
* In a UNIX environment, it might be defined by:
*
* #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \
* returnType (* name)
*
*
* 4. CK_CALLBACK_FUNCTION(returnType, name): A macro which makes
* a function pointer type for an application callback out of
* a return type for the callback and a name for the callback.
* It should be used in the following fashion:
*
* CK_CALLBACK_FUNCTION(CK_RV, myCallback)(args);
*
* to declare a function pointer, myCallback, to a callback
* which takes arguments args and returns a CK_RV. It can also
* be used like this:
*
* typedef CK_CALLBACK_FUNCTION(CK_RV, myCallbackType)(args);
* myCallbackType myCallback;
*
* If you're using Windows, it might be defined by:
*
* #define CK_CALLBACK_FUNCTION(returnType, name) \
* returnType (* name)
*
* In a UNIX environment, it might be defined by:
*
* #define CK_CALLBACK_FUNCTION(returnType, name) \
* returnType (* name)
*
*
* 5. NULL_PTR: This macro is the value of a NULL pointer.
*
* In any ANSI/ISO C environment (and in many others as well),
* this should best be defined by
*
* #ifndef NULL_PTR
* #define NULL_PTR 0
* #endif
*/


/* All the various Cryptoki types and #define'd values are in the
* file pkcs11t.h.
*/
#include "pkcs11t.h"

#define __PASTE(x,y) x##y


/* ==============================================================
* Define the "extern" form of all the entry points.
* ==============================================================
*/

#define CK_NEED_ARG_LIST 1
#define CK_PKCS11_FUNCTION_INFO(name) \
extern CK_DECLARE_FUNCTION(CK_RV, name)

/* pkcs11f.h has all the information about the Cryptoki
* function prototypes.
*/
#include "pkcs11f.h"

#undef CK_NEED_ARG_LIST
#undef CK_PKCS11_FUNCTION_INFO


/* ==============================================================
* Define the typedef form of all the entry points. That is, for
* each Cryptoki function C_XXX, define a type CK_C_XXX which is
* a pointer to that kind of function.
* ==============================================================
*/

#define CK_NEED_ARG_LIST 1
#define CK_PKCS11_FUNCTION_INFO(name) \
typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_,name))

/* pkcs11f.h has all the information about the Cryptoki
* function prototypes.
*/
#include "pkcs11f.h"

#undef CK_NEED_ARG_LIST
#undef CK_PKCS11_FUNCTION_INFO


/* ==============================================================
* Define structed vector of entry points. A CK_FUNCTION_LIST
* contains a CK_VERSION indicating a library's Cryptoki version
* and then a whole slew of function pointers to the routines in
* the library. This type was declared, but not defined, in
* pkcs11t.h.
* ==============================================================
*/

#define CK_PKCS11_FUNCTION_INFO(name) \
__PASTE(CK_,name) name;

/* Create the 3.0 Function list */
struct CK_FUNCTION_LIST_3_0 {

CK_VERSION version; /* Cryptoki version */

/* Pile all the function pointers into the CK_FUNCTION_LIST. */
/* pkcs11f.h has all the information about the Cryptoki
* function prototypes.
*/
#include "pkcs11f.h"

};

#define CK_PKCS11_2_0_ONLY 1

/* Continue to define the old CK_FUNCTION_LIST */
struct CK_FUNCTION_LIST {

CK_VERSION version; /* Cryptoki version */

/* Pile all the function pointers into the CK_FUNCTION_LIST. */
/* pkcs11f.h has all the information about the Cryptoki
* function prototypes.
*/
#include "pkcs11f.h"

};

#undef CK_PKCS11_FUNCTION_INFO
#undef CK_PKCS11_2_0_ONLY


#undef __PASTE

#ifdef __cplusplus
}
#endif

#endif /* _PKCS11_H_ */

Loading

0 comments on commit 4feaea0

Please sign in to comment.