Coverage Report

Created: 2025-07-11 07:00

/src/ibmswtpm2/src/TPMCmds.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************************/
2
/*                    */
3
/*       Main Simulator Entry Point           */
4
/*           Written by Ken Goldman       */
5
/*           IBM Thomas J. Watson Research Center     */
6
/*            $Id: TPMCmds.c 1259 2018-07-10 19:11:09Z kgoldman $   */
7
/*                    */
8
/*  Licenses and Notices              */
9
/*                    */
10
/*  1. Copyright Licenses:              */
11
/*                    */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in  */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,    */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create  */
15
/*    derivative works, distribute, display and perform the Source Code and */
16
/*    derivative works thereof, and to grant others the rights granted herein.  */
17
/*                    */
18
/*  - The TCG grants to the user of the other parts of the specification  */
19
/*    (other than the Source Code) the rights to reproduce, distribute,   */
20
/*    display, and perform the specification solely for the purpose of    */
21
/*    developing products based on such documents.        */
22
/*                    */
23
/*  2. Source Code Distribution Conditions:         */
24
/*                    */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,  */
26
/*    this list of conditions and the following disclaimers.      */
27
/*                    */
28
/*  - Redistributions in binary form must reproduce the above copyright   */
29
/*    licenses, this list of conditions and the following disclaimers in the  */
30
/*    documentation and/or other materials provided with the distribution.  */
31
/*                    */
32
/*  3. Disclaimers:               */
33
/*                    */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.   */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for    */
39
/*  information on specification licensing rights available through TCG   */
40
/*  membership agreements.              */
41
/*                    */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED   */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR   */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR    */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY    */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.   */
47
/*                    */
48
/*  - Without limitation, TCG and its members and licensors disclaim all  */
49
/*    liability, including liability for infringement of any proprietary  */
50
/*    rights, relating to use of information in this specification and to the */
51
/*    implementation of this specification, and TCG disclaims all liability for */
52
/*    cost of procurement of substitute goods or services, lost profits, loss   */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,  */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,  */
55
/*    arising in any way out of use or reliance upon this specification or any  */
56
/*    information herein.             */
57
/*                    */
58
/*  (c) Copyright IBM Corp. and others, 2016 - 2018       */
59
/*                    */
60
/********************************************************************************/
61
62
/* D.5 TPMCmds.c */
63
/* D.5.1. Description */
64
/* This file contains the entry point for the simulator. */
65
/* D.5.2. Includes, Defines, Data Definitions, and Function Prototypes */
66
#include "TpmBuildSwitches.h"
67
#include <stdlib.h>
68
#include <stdio.h>
69
#include <stdint.h>
70
#include <ctype.h>
71
#include <string.h>
72
#ifdef TPM_WINDOWS
73
#include <windows.h>
74
#include <winsock.h>
75
#endif
76
#include "Implementation.h" /* kgold */
77
#include "TpmTcpProtocol.h"
78
#include "Manufacture_fp.h"
79
#include "Platform_fp.h"
80
#include "Simulator_fp.h"
81
#ifdef TPM_WINDOWS
82
#include "TcpServer_fp.h"
83
#endif
84
#ifdef TPM_POSIX
85
#include "TcpServerPosix_fp.h"
86
#endif
87
#define PURPOSE             \
88
0
    "TPM Reference Simulator.\nCopyright Microsoft Corp.\n"
89
0
#define DEFAULT_TPM_PORT 2321
90
void* MainPointer;
91
/* D.5.3. Functions */
92
/* D.5.3.1. Usage() */
93
/* This function prints the proper calling sequence for the simulator. */
94
static void
95
Usage(
96
      char                *pszProgramName
97
      )
98
0
{
99
0
    fprintf(stderr, "%s", PURPOSE);
100
0
    fprintf(stderr, "Usage:\n");
101
0
    fprintf(stderr, "%s         - Starts the TPM server listening on port %d, %d\n",
102
0
      pszProgramName, DEFAULT_TPM_PORT, DEFAULT_TPM_PORT+1);
103
0
    fprintf(stderr,  "%s -port PortNum - Starts the TPM server listening on port PortNum, PortNum+1\n",
104
0
      pszProgramName);
105
0
    fprintf(stderr,  "%s -rm remanufacture the TPM before starting\n", pszProgramName);
106
0
    fprintf(stderr,  "%s -h       - This message\n", pszProgramName);
107
0
    exit(1);
108
0
}
109
/* D.5.3.2. main() */
110
/* This is the main entry point for the simulator. */
111
/* It registers the interface and starts listening for clients */
112
int
113
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
114
main
115
#else
116
tpm_server_main
117
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
118
(
119
     int              argc,
120
     char            *argv[]
121
     )
122
0
{
123
0
    int   i;        /* argc iterator */
124
0
    int   manufacture = 0;
125
    
126
    /* command line argument defaults */
127
0
    int portNum = DEFAULT_TPM_PORT;
128
0
    int portNumPlat;
129
130
0
    for (i=1 ; i<argc ; i++) {
131
0
  if (strcmp(argv[i],"-rm") == 0) {
132
0
      manufacture = 1;
133
0
  }
134
0
  else if (strcmp(argv[i],"-port") == 0) {
135
0
      i++;
136
0
      if (i < argc) {
137
0
    portNum = atoi(argv[i]);
138
0
    if(portNum <=0 || portNum>65535) {
139
0
        Usage(argv[0]);
140
0
    }
141
0
      }
142
0
      else {
143
0
    printf("Missing parameter for -port\n");
144
0
    Usage(argv[0]);
145
0
      }
146
0
  }
147
0
  else if (strcmp(argv[i],"-h") == 0) {
148
0
      Usage(argv[0]);
149
0
  }
150
0
  else {
151
0
      printf("\n%s is not a valid option\n", argv[i]);
152
0
      Usage(argv[0]);
153
0
  }
154
0
    }
155
    /* test to see if the NVChip files exists */
156
0
    {
157
0
  FILE *file = fopen("NVChip", "r+b");
158
0
  if (file == NULL) { /* if not, remanufacture */
159
0
      manufacture = 1;
160
0
  }
161
0
  else {
162
0
      fclose(file );
163
0
  }
164
0
    }
165
0
    _plat__NVEnable(NULL);
166
0
    if (manufacture) {
167
0
  if(TPM_Manufacture(1) != 0)
168
0
      {
169
0
    exit(1);
170
0
      }
171
  // Coverage test - repeated manufacturing attempt
172
0
  if(TPM_Manufacture(0) != 1)
173
0
      {
174
0
    exit(2);
175
0
      }
176
  // Coverage test - re-manufacturing
177
0
  TPM_TearDown();
178
0
  if(TPM_Manufacture(1) != 0)
179
0
      {
180
0
    exit(3);
181
0
      }
182
0
    }
183
    // Disable NV memory
184
0
    _plat__NVDisable();
185
    /* power on the TPM kgold MS simulator comes up powered off */
186
0
    _rpc__Signal_PowerOn(FALSE);
187
0
    _rpc__Signal_NvOn();
188
0
    portNumPlat = portNum + 1;
189
0
    StartTcpServer(&portNum, &portNumPlat);
190
0
    return EXIT_SUCCESS;
191
0
}