Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 7 Jul 2002 07:12:11 -0700 (PDT)
From:      Dag-Erling Smorgrav <des@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 13865 for review
Message-ID:  <200207071412.g67ECBqT048633@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://people.freebsd.org/~peter/p4db/chv.cgi?CH=13865

Change 13865 by des@des.at.des.thinksec.com on 2002/07/07 07:11:12

	Checkpoint commit: add the beginnings of a protocol stack, which
	currently supports USB slots and ISO7816-3 T=1 devices.  You
	can't actually talk to the device yet though.
	
	Sponsored by:	DARPA, NAI Labs

Affected files ...

.. //depot/projects/cryptoki/bin/slots/slots.c#3 edit
.. //depot/projects/cryptoki/lib/C_GetSlotInfo.c#7 edit
.. //depot/projects/cryptoki/lib/C_GetTokenInfo.c#5 edit
.. //depot/projects/cryptoki/lib/Makefile#6 edit
.. //depot/projects/cryptoki/lib/_ck_apdu.c#1 add
.. //depot/projects/cryptoki/lib/_ck_configure.c#2 edit
.. //depot/projects/cryptoki/lib/_ck_crc.c#1 add
.. //depot/projects/cryptoki/lib/_ck_hexdump.c#1 add
.. //depot/projects/cryptoki/lib/_ck_object.c#1 add
.. //depot/projects/cryptoki/lib/_ck_parallel.c#1 add
.. //depot/projects/cryptoki/lib/_ck_serial.c#1 add
.. //depot/projects/cryptoki/lib/_ck_tpdu_t0.c#1 add
.. //depot/projects/cryptoki/lib/_ck_tpdu_t1.c#1 add
.. //depot/projects/cryptoki/lib/_ck_ugen.c#1 add
.. //depot/projects/cryptoki/lib/_ck_usb.c#1 add
.. //depot/projects/cryptoki/lib/cryptoki_impl.h#4 edit

Differences ...

==== //depot/projects/cryptoki/bin/slots/slots.c#3 (text+ko) ====

@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/cryptoki/bin/slots/slots.c#2 $
+ * $P4: //depot/projects/cryptoki/bin/slots/slots.c#3 $
  */
 
 #include <ctype.h>
@@ -80,7 +80,7 @@
 		printf(" (unrecognized token)");
 		return;
 	default:
-		errx(1, "C_GetTokenInfo(%lu): 0x%x", ulSlotID, rv);
+		errx(1, "C_GetTokenInfo(%lu): 0x%lx", ulSlotID, rv);
 	}
 	TERMINATE(TokenInfo.label);
 	TERMINATE(TokenInfo.manufacturerID);
@@ -144,7 +144,7 @@
 
 	rv = C_GetSlotInfo(ulSlotID, &SlotInfo);
 	if (rv != CKR_OK)
-		errx(1, "C_GetSlotInfo(%lu): 0x%x", ulSlotID, rv);
+		errx(1, "C_GetSlotInfo(%lu): 0x%lx", ulSlotID, rv);
 	TERMINATE(SlotInfo.slotDescription);
 	TERMINATE(SlotInfo.manufacturerID);
 	if (v_flag) {
@@ -191,7 +191,7 @@
 			if (ulCount == 0 || pSlotList != NULL_PTR)
 				break;
 		} else if (rv != CKR_BUFFER_TOO_SMALL) {
-			errx(1, "C_GetSlotList(): 0x%x", rv);
+			errx(1, "C_GetSlotList(): 0x%lx", rv);
 		}
 		tmp = realloc(pSlotList, ulCount * sizeof *pSlotList);
 		if (tmp == NULL)
@@ -240,7 +240,7 @@
 
 	rv = C_Initialize(NULL_PTR);
 	if (rv != CKR_OK)
-		err(1, "C_Initialize(): %d", rv);
+		err(1, "C_Initialize(): %lx", rv);
 	slots();
 	C_Finalize(NULL_PTR);
 	exit(0);

==== //depot/projects/cryptoki/lib/C_GetSlotInfo.c#7 (text+ko) ====

@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/cryptoki/lib/C_GetSlotInfo.c#6 $
+ * $P4: //depot/projects/cryptoki/lib/C_GetSlotInfo.c#7 $
  */
 
 #include <string.h>
@@ -42,22 +42,15 @@
 C_GetSlotInfo(CK_SLOT_ID slotID,
 	CK_SLOT_INFO_PTR pInfo)
 {
+	_ck_slot_ptr slot;
+	int ret;
 
 	if (!_ck_Initialized)
 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
 	if (pInfo == NULL_PTR || slotID > _ck_NumSlots)
 		return (CKR_ARGUMENTS_BAD);
+	slot = &_ck_Slots[slotID];
 	memset(pInfo, 0, sizeof *pInfo);
-	memset(pInfo->slotDescription, ' ', sizeof pInfo->slotDescription);
-	strncpy(pInfo->slotDescription, _ck_Slots[slotID].cks_dev,
-	    strlen(_ck_Slots[slotID].cks_dev));
-	memset(pInfo->manufacturerID, ' ', sizeof pInfo->manufacturerID);
-	switch (_ck_Slots[slotID].cks_type) {
-	case serial:
-	case parallel:
-	case ugen:
-		pInfo->flags |= CKF_HW_SLOT;
-		break;
-	}
-	return (CKR_OK);
+	ret = _ck_call(sinfo, slot->cks_stack, pInfo);
+	return (ret == 0 ? CKR_OK : CKR_GENERAL_ERROR /* XXX */);
 }

==== //depot/projects/cryptoki/lib/C_GetTokenInfo.c#5 (text+ko) ====

@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/cryptoki/lib/C_GetTokenInfo.c#4 $
+ * $P4: //depot/projects/cryptoki/lib/C_GetTokenInfo.c#5 $
  */
 
 #include "cryptoki_impl.h"
@@ -46,5 +46,5 @@
 	if (pInfo == NULL_PTR || slotID > _ck_NumSlots)
 		return (CKR_ARGUMENTS_BAD);
 
-	return (CKR_OK);
+	return (CKR_TOKEN_NOT_PRESENT);
 }

==== //depot/projects/cryptoki/lib/Makefile#6 (text+ko) ====

@@ -31,7 +31,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $P4: //depot/projects/cryptoki/lib/Makefile#5 $
+# $P4: //depot/projects/cryptoki/lib/Makefile#6 $
 #
 
 LIB		 = cryptoki
@@ -111,9 +111,16 @@
 SRCS		+= C_WaitForSlotEvent.c
 SRCS		+= C_WrapKey.c
 SRCS		+= _ck_configure.c
+SRCS		+= _ck_crc.c
 SRCS		+= _ck_global.c
+SRCS		+= _ck_hexdump.c
+SRCS		+= _ck_parallel.c
+SRCS		+= _ck_serial.c
 SRCS		+= _ck_session_delete.c
 SRCS		+= _ck_session_find.c
 SRCS		+= _ck_session_new.c
+SRCS		+= _ck_tpdu_t0.c
+SRCS		+= _ck_tpdu_t1.c
+SRCS		+= _ck_ugen.c
 
 .include <bsd.lib.mk>

==== //depot/projects/cryptoki/lib/_ck_configure.c#2 (text+ko) ====

@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/cryptoki/lib/_ck_configure.c#1 $
+ * $P4: //depot/projects/cryptoki/lib/_ck_configure.c#2 $
  */
 
 #include <ctype.h>
@@ -56,7 +56,7 @@
 	_ck_slot_ptr slot, tmp;
 	const char **cfn, *line, *p, *q;
 	size_t len, size;
-	int lineno, plen;
+	int lineno, plen, ret;
 	FILE *f;
 
 	if (_ck_Slots != NULL) {
@@ -98,7 +98,7 @@
 		for (p = q = line; q < line + len; ++q)
 			if (isspace(*q))
 				break;
-#if 0
+#if 1
 		warnx("%s:%d: device '%.*s'", *cfn, lineno, (int)(q - p), p);
 		warnx("slots %p, slot %lu = %p", _ck_Slots, _ck_NumSlots, slot);
 #endif
@@ -115,15 +115,20 @@
 		for (p = q; q < line + len; ++q)
 			if (isspace(*q))
 				break;
+		slot->cks_stack = calloc(1, sizeof *slot->cks_stack);
 		if (strncmp(p, "serial", q - p) == 0) {
 			slot->cks_type = serial;
+			slot->cks_stack->cks_layer = &_ck_serial;
 		} else if (strncmp(p, "parallel", q - p) == 0) {
 			slot->cks_type = parallel;
+			slot->cks_stack->cks_layer = &_ck_parallel;
 		} else if (strncmp(p, "ugen", q - p) == 0) {
 			slot->cks_type = ugen;
+			slot->cks_stack->cks_layer = &_ck_ugen;
 		} else {
 			warnx("%s:%d: unknown device type '%.*s'",
 			    *cfn, lineno, (int)(q - p), p);
+			free(slot->cks_stack);
 			continue;
 		}
 
@@ -147,6 +152,15 @@
 			continue;
 		}
 
+		/* open the device */
+		ret = _ck_call(open, slot->cks_stack, slot->cks_dev, 0);
+		if (ret != 0) {
+			warnx("%s:%d: failed to open device '%s': 0x%x",
+			    *cfn, lineno, slot->cks_dev, ret);
+			free(slot->cks_stack);
+			continue;
+		}
+
 		++_ck_NumSlots;
 	}
 	fclose(f);

==== //depot/projects/cryptoki/lib/cryptoki_impl.h#4 (text+ko) ====

@@ -31,45 +31,73 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/cryptoki/lib/cryptoki_impl.h#3 $
+ * $P4: //depot/projects/cryptoki/lib/cryptoki_impl.h#4 $
  */
 
 #ifndef _CRYPTOKI_IMPL_H_INCLUDED
 #define _CRYPTOKI_IMPL_H_INCLUDED
 
 #include <cryptoki.h>
+#include <stdint.h>
+
+/*
+ * Types
+ */
+typedef struct _ck_stack _ck_stack;
+typedef _ck_stack *_ck_stack_ptr;
+typedef struct _ck_layer _ck_layer;
+typedef _ck_layer *_ck_layer_ptr;
+typedef int (*_ck_open)(_ck_stack_ptr, const char *, int);
+typedef int (*_ck_close)(_ck_stack_ptr, int);
+typedef int (*_ck_sinfo)(_ck_stack_ptr, CK_SLOT_INFO_PTR);
+typedef int (*_ck_tinfo)(_ck_stack_ptr, CK_TOKEN_INFO_PTR);
+typedef int (*_ck_xfer)(_ck_stack_ptr, unsigned char *, int *);
 
-extern CK_BBOOL		_ck_Initialized;
-extern CK_CREATEMUTEX	_ck_CreateMutex;
-extern CK_DESTROYMUTEX	_ck_DestroyMutex;
-extern CK_LOCKMUTEX	_ck_LockMutex;
-extern CK_UNLOCKMUTEX	_ck_UnlockMutex;
+struct _ck_stack {
+	_ck_layer_ptr	cks_layer;
+	void		*cks_param;
+
+	_ck_stack_ptr	cks_down;
+	_ck_stack_ptr	cks_up;
+};
 
-extern CK_INFO		_ck_Info;
+struct _ck_layer {
+	const char	*ckl_name;
+	const char	*ckl_desc;
+	_ck_open	ckl_open;
+	_ck_close	ckl_close;
+	_ck_sinfo	ckl_sinfo;
+	_ck_tinfo	ckl_tinfo;
+	_ck_xfer	ckl_send;
+	_ck_xfer	ckl_recv;
+};
 
-extern CK_FUNCTION_LIST	_ck_FunctionList;
+#define _ck_call(func, stack, ...) \
+	((*stack->cks_layer->ckl_##func)(stack, __VA_ARGS__))
+#define _ck_call_down(func, stack, ...) \
+	_ck_call(func, stack->cks_down, __VA_ARGS__)
+#define _ck_call_up(func, stack, ...) \
+	_ck_call(func, stack->cks_up, __VA_ARGS__)
 
 typedef struct _ck_slot _ck_slot;
 typedef _ck_slot *_ck_slot_ptr;
 struct _ck_slot {
-	CK_ULONG	 cks_id;
-	char		 cks_dev[64];
+	CK_ULONG	cks_id;
+	char		cks_dev[64];
 	enum {
 		serial,
 		parallel,
 		ugen,
-	}		 cks_type;
+	}		cks_type;
 	enum {
 		autodetect,
 		tzero,
 		tone,
 		apdu,
-	}		 cks_protocol;
+	}		cks_protocol;
+	_ck_stack_ptr	cks_stack;
 };
 
-extern CK_ULONG		_ck_NumSlots;
-extern _ck_slot_ptr	_ck_Slots;
-
 typedef struct _ck_session _ck_session;
 typedef _ck_session *_ck_session_ptr;
 struct _ck_session {
@@ -79,13 +107,40 @@
 	_ck_session_ptr	cks_next;
 };
 
+/*
+ * Global variables
+ */
+extern CK_BBOOL		_ck_Initialized;
+extern CK_CREATEMUTEX	_ck_CreateMutex;
+extern CK_DESTROYMUTEX	_ck_DestroyMutex;
+extern CK_LOCKMUTEX	_ck_LockMutex;
+extern CK_UNLOCKMUTEX	_ck_UnlockMutex;
+
+extern CK_INFO		_ck_Info;
+
+extern CK_FUNCTION_LIST	_ck_FunctionList;
+
+extern CK_ULONG		_ck_NumSlots;
+extern _ck_slot_ptr	_ck_Slots;
+
 extern CK_ULONG		_ck_NextSession;
 extern _ck_session_ptr	_ck_Sessions;
 
+extern _ck_layer	_ck_parallel;
+extern _ck_layer	_ck_serial;
+extern _ck_layer	_ck_tpdu_t0;
+extern _ck_layer	_ck_tpdu_t1;
+extern _ck_layer	_ck_ugen;
 
+/*
+ * Functions
+ */
 int			_ck_configure(void);
 _ck_session_ptr		_ck_session_new(CK_SLOT_ID, CK_STATE, CK_FLAGS);
 int			_ck_session_delete(CK_SESSION_HANDLE);
 _ck_session_ptr		_ck_session_find(CK_SESSION_HANDLE);
 
+uint16_t		_ck_crc(uint16_t, uint8_t *, int);
+void			_ck_hexdump(uint8_t *, int, int);
+
 #endif

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe p4-projects" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200207071412.g67ECBqT048633>