1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  under this license.
7  *
8  *  Copyright (c) Microsoft Corporation
9  *
10  *  All rights reserved.
11  *
12  *  BSD License
13  *
14  *  Redistribution and use in source and binary forms, with or without modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or
22  *  other materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 //** Description
36 //
37 //    This file contains the NV read and write access methods.  This implementation
38 //    uses RAM/file and does not manage the RAM/file as NV blocks.
39 //    The implementation may become more sophisticated over time.
40 //
41 
42 //** Includes and Local
43 #include <stdio.h>
44 #include <time.h>
45 #include "Platform.h"
46 
47 #if CERTIFYX509_DEBUG
48 
49 const char      *debugFileName = "DebugFile.txt";
50 
51 //*** fileOpen()
52 // This exists to allow use of the 'safe' version of fopen() with a MS runtime.
53 static FILE *
fileOpen(const char * fn,const char * mode)54 fileOpen(
55     const char      *fn,
56     const char      *mode
57 )
58 {
59     FILE        *f;
60 #   if defined _MSC_VER
61     if(fopen_s(&f, fn, mode) != 0)
62         f = NULL;
63 #   else
64     f = fopen(fn, mode);
65 #   endif
66     return f;
67 }
68 
69 //*** DebugFileInit()
70 // This function initializes the file containing the debug data with the time of the
71 // file creation.
72 //  Return Type: int
73 //   0              success
74 //  != 0            error
75 int
DebugFileInit(void)76 DebugFileInit(
77     void
78 )
79 {
80     FILE            *f = NULL;
81     time_t           t = time(NULL);
82 //
83     // Get current date and time.
84 #   if defined _MSC_VER
85     char                 timeString[100];
86     ctime_s(timeString, (size_t)sizeof(timeString), &t);
87 #   else
88     char                *timeString;
89     timeString = ctime(&t);
90 #   endif
91     // Try to open the debug file
92     f = fileOpen(debugFileName, "w");
93     if(f)
94     {
95         // Initialize the contents with the time.
96         fprintf(f, "%s\n", timeString);
97         fclose(f);
98         return 0;
99     }
100     return -1;
101 }
102 
103 //*** DebugDumpBuffer()
104 void
DebugDumpBuffer(int size,unsigned char * buf,const char * identifier)105 DebugDumpBuffer(
106     int             size,
107     unsigned char   *buf,
108     const char      *identifier
109 )
110 {
111     int              i;
112 //
113     FILE *f = fileOpen(debugFileName, "a");
114     if(!f)
115         return;
116     if(identifier)
117         fprintf(f, "%s\n", identifier);
118     if(buf)
119     {
120         for(i = 0; i < size; i++)
121         {
122             if(((i % 16) == 0) && (i))
123                 fprintf(f, "\n");
124             fprintf(f, " %02X", buf[i]);
125         }
126         if((size % 16) != 0)
127             fprintf(f, "\n");
128     }
129     fclose(f);
130 }
131 
132 #endif // CERTIFYX509_DEBUG
133