/*

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is a portion of the Path Development API developed
by CygnaCom Solutions, Inc.

Portions created by CygnaCom Solutions, Inc. are Copyright (C) CygnaCom 
Solutions, Inc. All Rights Reserved.

Contributor(s): ______________________________________.

http://www.cygnacom.com

*/
/*------------------------------------------------------------------------*\
|  Project Name  :   Path Development API
|  File Name     :   utils.h
|  Creation Date :   May 17, 1999
|  Author        :   Matt Cooper (mcooper@cygnacom.com)
|  Last Mode Date:   May 17, 1999
|  Modification History:
|--------------------------------------------------------------------------
|
|     Known Bugs:   None
|                   
|--------------------------------------------------------------------------
| Cryptographic devices and technical data regarding them are subject to
| Federal Government export controls as specified in Title 22, Code of
| Federal Regulations, Parts 121 through 128.  Cryptographic devices
| implementing the Data Encryption Standard (DES) and technical data
| regarding them must comply with these Federal regulations
|--------------------------------------------------------------------------
| Copyright (c) 1999, CygnaCom Solutions, Inc.
| All Rights Reserved.
\*------------------------------------------------------------------------*/

#ifndef UTIL_HEADER_INCLD
#define UTIL_HEADER_INCLD

// Error codes from AtoB and BtoA
#define ATOB_EMPTY_INPUT				4226
#define ATOB_INVALID_INPUT_CHAR			4227
#define ATOB_INVALID_INPUT_LENGTH		4228

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string.h>
#include "cmapi.h"
#include "PathRec.h"
#include "CertsRec.h"
#include "Defines.h"


#if __cplusplus
extern "C" {
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// Simple Utility Functions
/////////////////////////////////////////////////////////////////////////////////////////
// Bin to ASCII and vice versa
void CPLI_btoa(char* byteval, char* ascval, unsigned int blength);
int CPLI_atob(char* byteval, char* ascval, unsigned int *alength);

long CPLI_TranslateErrorNumber(long errcode);
int IsInErrRange( long rv, long errbase );
int IsBadCert( Cert_struct *pCert );

long CPLI_CopyBytesStruct( Bytes_struct* src, Bytes_struct* dest );
long CPLI_IsSameBytesStruct( Bytes_struct *a, Bytes_struct* b );

/////////////////////////////////////////////////////////////////////////////////////////
// Cache specific funtions
/////////////////////////////////////////////////////////////////////////////////////////
// Builds record entry for output file
long CPLI_CreatePathRecFromCerts( CPathRec &pathRec, Cert_struct *subject, Cert_struct *issuer = NULL, unsigned long flag = 0 );
// For sorting certs in mem cache
long CPLI_CompareCertPriority(LPCERTCACHE_ENTRY_STRUCT cert1, LPCERTCACHE_ENTRY_STRUCT cert2, char* trustedRootDN, char** acceptablePolicySet );


/////////////////////////////////////////////////////////////////////////////////////////
// Distinguished Name Manipulation / Comparison
/////////////////////////////////////////////////////////////////////////////////////////
void CPLI_SquashSpacesInDN( char *pDN );
int CPLI_CountRDNs( char* dn );
int CPLI_CountMatchingRDNs( char* dn1, char* dn2 );
long CPLI_IsSameDN( char *dn1, char *dn2 );
long CPLI_IsTrustedDN( char **rootDNs, char *dn2 );


/////////////////////////////////////////////////////////////////////////////////////////
// bit mask manipulations
/////////////////////////////////////////////////////////////////////////////////////////
void CPLI_set_bit(unsigned long *flag, unsigned long bit);
void CPLI_unset_bit(unsigned long *flag, unsigned long bit);
int CPLI_is_bit_set(unsigned long flag, unsigned long bit);
unsigned long CPLI_get_bit_mask( short OneBasedBitToSet );
ULONG CPLI_invert_mask(unsigned long bits);


/////////////////////////////////////////////////////////////////////////////////////////
// General Path Building Helpers
/////////////////////////////////////////////////////////////////////////////////////////
LPLOCAL_SESSION_INFO CPLI_GetSessionInfo( long session );
long CPLI_CreateNewSession(		long* session, 
								char* subjectDN, 
								CPL_SESSION_INFO *sessionInfo,
								CertPath_struct *partialPath = NULL,
								int inhibitPolicyMapping = 0,
								int requireExplicitPolicy = 0,
								char** desiredAlgOIDArray = NULL, 
								unsigned short keyUsage = 0, 
								Bytes_struct* skid = NULL				);

long CPLI_DestroySession( long session );

long CPLI_IsValidTargetCert( LPLOCAL_SESSION_INFO pLocalInfo, LPCPL_SESSION_INFO sessionInfo, Cert_struct *pCert );
//long CPLI_FreeLocalPathList( LPPATH_ENTRY_LIST *pList ); //not used

// Parses CrossCertificatePair ASN structure, returns forward cert ptr and len.. 
// *** DOES NOT COPY DATA!! (do NOT free pForwardCert->data!!) ***
long CPLI_GetForwardCrossCert( Bytes_struct *pCrossCertPair, Bytes_struct *pForwardCert );


/////////////////////////////////////////////////////////////////////////////////////////
// String Array Manipulation
/////////////////////////////////////////////////////////////////////////////////////////
long CPLI_CopyStringArray( char** src, char*** dest );
long CPLI_IsStringInStringArray( char *str, char** strArray );
long CPLI_FreeStringArray( char*** arr );
long CPLI_IntersectStringArrays( char** arr1, char** arr2, char*** dest );
long CPLI_GrowStringArray( char*** arr, int numEntriesToAdd );

/////////////////////////////////////////////////////////////////////////////////////////
// Policy validation and mapping helpers
/////////////////////////////////////////////////////////////////////////////////////////
long CPLI_AddPolicyMappingsToPolicySet(char*** policySet, Pol_maps_struct* pMaps );
long CPLI_IsValidPolicySetChain( char** policySet, CertPath_struct *pHead, int inhibitPolicyMapping, int RequireExplicitPolicy );
// Intersects policy list with policy array and preserves original array
long CPLI_IntersectPolicyArrayAndPolicySet( char** arr1, Policy_struct* pPols, char*** dest );
// Intersects policy list with a policy array - DESTROYS THE ORIGINAL ARRAY - DO NOT PRESERVE PTR!
long CPLI_IntersectPolicyArrayAndPolicySetInPlace( char*** policyArray, Policy_struct* pPols );


#if __cplusplus
}
#endif


#endif
