xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/log.c (revision 2d5f7628c5531eb583b9313ac2fd1cf8582b4479)
1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2004-2006
8  *
9  */
10 
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <syslog.h>
15 
16 #include "trousers/tss.h"
17 #include "tcslog.h"
18 
19 #ifdef TSS_DEBUG
20 
21 /*
22  * LogBlobData()
23  *
24  *   Log a blob's data to the debugging stream
25  *
26  * szDescriptor - The APPID tag found in the caller's environment at build time
27  * sizeOfBlob - The size of the data to log
28  * blob - the data to log
29  *
30  */
31 
32 void
LogBlobData(char * szDescriptor,unsigned long sizeOfBlob,unsigned char * blob)33 LogBlobData(char *szDescriptor, unsigned long sizeOfBlob, unsigned char *blob)
34 {
35 	char temp[64];
36 	unsigned int i;
37 
38 
39 	if (getenv("TCSD_FOREGROUND") == NULL)
40 		openlog(szDescriptor, LOG_NDELAY|LOG_PID, TSS_SYSLOG_LVL);
41 	memset(temp, 0, sizeof(temp));
42 
43 	for (i = 0; (unsigned long)i < sizeOfBlob; i++) {
44 		if ((i > 0) && ((i % 16) == 0)) {
45 			if (getenv("TCSD_FOREGROUND") != NULL)
46 				fprintf(stdout, "%s %s\n", szDescriptor, temp);
47 			else
48 				syslog(LOG_DEBUG, "%s", temp);
49 			memset(temp, 0, sizeof(temp));
50 		}
51 		snprintf(&temp[(i%16)*3], 4, "%.2X ", blob[i]);
52 	}
53 
54 	if (i == sizeOfBlob) {
55 		if (getenv("TCSD_FOREGROUND") != NULL)
56 			fprintf(stdout, "%s %s\n", szDescriptor, temp);
57 		else
58 			syslog(LOG_DEBUG, "%s", temp);
59 	}
60 }
61 
62 void
LogTPMERR(TSS_RESULT result,char * file,int line)63 LogTPMERR(TSS_RESULT result, char *file, int line)
64 {
65 	if (getenv("TSS_DEBUG_OFF") == NULL)
66 		fprintf(stderr, "%s %s %s:%d: 0x%x\n", "LOG_RETERR", "TPM", file, line, result);
67 }
68 
69 TSS_RESULT
LogTDDLERR(TSS_RESULT result,char * file,int line)70 LogTDDLERR(TSS_RESULT result, char *file, int line)
71 {
72 	if (getenv("TSS_DEBUG_OFF") == NULL)
73 		fprintf(stderr, "%s %s %s:%d: 0x%x\n", "LOG_RETERR", APPID, file, line, result);
74 
75 	return (result | TSS_LAYER_TDDL);
76 }
77 
78 TSS_RESULT
LogTCSERR(TSS_RESULT result,char * file,int line)79 LogTCSERR(TSS_RESULT result, char *file, int line)
80 {
81 	if (getenv("TSS_DEBUG_OFF") == NULL)
82 		fprintf(stderr, "%s %s %s:%d: 0x%x\n", "LOG_RETERR", APPID, file, line, result);
83 
84 	return (result | TSS_LAYER_TCS);
85 }
86 
87 #endif
88