📄 osptnep.c
字号:
/**########################################################################*########################################################################*########################################################################* * COPYRIGHT (c) 1998, 1999, 2000 by TransNexus, LLC * * This software contains proprietary and confidential information * of TransNexus, LLC. Except as may be set forth in the license * agreement under which this software is supplied, use, disclosure, * or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC. * *******#########################################################################*#########################################################################*#########################################################################*/ #include "ospcomm.h"#include "ospmsginfo.h"#include "osptnep.h"#include "ospb64.h"/* * Enroll this device. This will consist of: * o parsing command line options; * o retrieving the CA certificate from the enrollment server; * o generating a request to be sent to the enrollment server; * o initializing the communications manager; * o transmitting the request to the enrollment server; * o parsing the response for a status ( success, pending, failed ) and * a certificate ( if the request was successful ). * * Requesting a CA certificate and requesting the device's own certificate * are two separate functions and require that this program be invoked * twice. This is just a sample program; retrieving a CA certificate and * retrieving the device's certificate can probably linked together, but * some care and caution must be taken in how the communications manager(s) * are started, stopped, and configured. */ int main ( int argc, char* argv[] ){ /* The return value from each function; it's set to be a failure code, * just in case a function that fails doesn't set it. */ unsigned retVal = OSPC_ENROLL_FAILURE_DEFAULT; /* The enrollment parameters that are received on the command line, * as documented in the requirements and design: */ OSPTENROLLPARAMS enrollParams; /* These are the parameters that define the communication manager's * running parameters, but which are not defined as input for hte * OSP TNEP Client Requirements. These will most likely not be entered * by an end user in the same scope as an enrollment request, but as * part of some device configuration that may just rely on system- * generated defaults. They are only used for contacting an enrollment * server, so they will not be used for the initial setup that's done * for generating private keys, certificate requests, and so on. */ OSPTCOMMPARAMS commParams; /* The certificate retrieved from the enrollment server. This * may not be saved in stable storage, so it is possible that the * enrollment client will have to fetch it for every operation. */ unsigned char* localCert = OSPC_OSNULL; /* The length of the certificate returned from the enrollment server. */ unsigned localCertLen = 0; /* The status of an enrollment: 0 for success, 1 for pending, * >= 2 for failure: */ unsigned enrollStatus = OSPC_ENROLL_STATUS_FAILURE_DEFAULT; /* Set the communications parameters to some sample values; these * can be set on standard input ( along with all of the other * enrollment parameters ), but the list of values required on * the command line begins to get too long. */ /* Set an SSL lifetime of 5 minutes: */ commParams.SSLLifetime = 300; /* Max of 2 HTTP connections: */ commParams.HTTPMaxConnections = 2; /* 10 second persistence for HTTP connections: */ commParams.HTTPPersistence = 10; /* Wait 5 seconds before retrying a server: */ commParams.HTTPRetryDelay = 5; /* Then again, don't retry at all: */ commParams.HTTPRetryLimit = 0; /* 10 second HTTP timeout: */ commParams.HTTPTimeout = 10000; /* * initialize Winsock Library if necessary */ OSPM_INITWINSOCK(retVal); /* Initialize the enrollment parameters first: */ retVal = OSPPInitEnrollParams( &enrollParams ); /* * o Parse the parameter list; * o Check that the user chose a function or operation to perform * ( not necessarily valid - just that they entered a function ); * o Now choose an operation based on the function entered: * - retrieve a CA cert if the function was "cacert" * - request or retrieve the device's certificate if the function * was "request" or "retrieve" * * o Free up the memory in the enrollment parameters; this includes * everything but the certificate for the device. * o Free up the memory in the certificate generated, if one was generated */ if ( retVal == OSPC_ERR_NO_ERROR ) { retVal = OSPPEnrollParseParameters( argc, argv, &enrollParams ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "Unable to parse input parameters.\n" ); } } /* Now check that the user entered a function: */ if ( retVal == OSPC_ERR_NO_ERROR ) { /* The parameters could be read, so now check the * function's integrity. */ if ( ( enrollParams.Function == OSPC_OSNULL ) || ( OSPM_STRLEN( (const char*)enrollParams.Function ) <= 0 ) ) { retVal = OSPC_ERR_ENROLL_PARAMS_FUNCTION; OSPM_DBGERRORLOG( retVal, "No function was requested.\n" ); OSPPEnrollUsage( argv[0], OSPC_ENROLL_HELP_MSG ); } } /* Now execute whatever function the user entered, if it's a valid * operation request. */ if ( retVal == OSPC_ERR_NO_ERROR ) { /* If ( the function is for retrieving the CA certificate ) then * o retrieve the CA certificate * o if ( there was a problem ) then complain; * o else ( dump the hex encoding of the certificate's contents ) */ if ( OSPM_STRCMP( (const char*)enrollParams.Function, OSPC_ENROLL_CA_CERT_FUNCTION ) == 0 ) { retVal = OSPPRetrieveCACert( &enrollParams, &commParams ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "Unable to retrieve the CA certificate\n" ); } else { OSPM_PRINTF( "CA certificate received: %s\n", enrollParams.CACertB64 ); /* wbr: store the CA certificate in "CACertB64.dat" */ OSPPSave( "CACertB64.dat", enrollParams.CACertB64, enrollParams.CACertB64Len ); } } /* Else ( if the function is to request or retrieve a cert ) then * o enroll the device with the enrollment server */ else if ( ( OSPM_STRCMP( (const char*)enrollParams.Function, OSPC_ENROLL_REQUEST_FUNCTION ) == 0 ) || ( OSPM_STRCMP( (const char*)enrollParams.Function, OSPC_ENROLL_RETRIEVE_FUNCTION ) == 0 ) ) { retVal = OSPPEnroll( &enrollParams, &commParams, &localCert, &localCertLen, &enrollStatus ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "Error encountered with enrollment.\n" ); } else { OSPPPrintCertAndStatus( localCert, localCertLen, enrollStatus ); } } else { OSPM_DBGMISC(( "Invalid function parameter passed in: <%s>\n", enrollParams.Function )); OSPPEnrollUsage( argv[0], OSPC_ENROLL_HELP_MSG ); } } OSPPFreeEnrollParams( &enrollParams ); /* Free up the certificate generated: */ if ( localCert != OSPC_OSNULL ) { OSPM_FREE( localCert ); } OSPM_PRINTF( "Program ends" ); /* wbr changed */ /* OSPM_PRINTF( "Press any key to continue." ); getchar(); */ return retVal;}/* This is a convenience function for checking arguments. For each * character in the input string, change the character to lowercase * if it's A-Z. OSPM_TOLOWER will be used for the sake of compatibility. * * Input: String to be converted to lowercase, and the output string to * place it in. */int OSPPEnrollStringLowercase( const char* ospvStringIn, char* ospvStringLowercaseOut){ /* The length of the input string: */ int inputStringLen = 0; /* Index for parsing the input string: */ int inputStringIndex = 0; /* There is initially no error to be worried about; there are * only errors if the output string cannot be initialized. */ int retVal = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(( "ENTER: OSPPEnrollStringLowercase\n" )); /* If ( the input string and output string aren't null ) then * o set the input string length; * o if ( we can set the memory of the output string to 0's ) then * - for ( each character in the input string ) * o copy it to the output string. * o else ( we couldn't initialize the memory ) so * - record an error. */ if ( ( ospvStringIn != OSPC_OSNULL ) && ( ospvStringLowercaseOut != OSPC_OSNULL ) ) { inputStringLen = OSPM_STRLEN( ospvStringIn ); /* Now blank out the output string: */ OSPM_MEMSET( ospvStringLowercaseOut, 0, inputStringLen ); for ( inputStringIndex = 0; inputStringIndex < inputStringLen; inputStringIndex++ ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -