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