⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgplicensenumber.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
📖 第 1 页 / 共 3 页
字号:
  }

  if( IsntPGPError(err) )
	*outlen = curpos - *out; /* real length */
  else  {
	PGPFreeData( *out );
	*out = NULL;
  }

  return err;
}

#ifdef PGP_LICENSE_SERVER

// AES encrypt LN 
static PGPError pgpEncryptLicenseNumber(PGPContextRef context,
										const LICENSENUMBER *ln, PGPLicenseNumberRef ln_ref)
{
	PGPError err;
	PGPSymmetricCipherContextRef AES256context;
	PGPByte *ln_encr = ((PGPLicenseNumber *)ln_ref)->ln_encr;
	
	err=kPGPError_NoErr;
	
	err=PGPNewSymmetricCipherContext(context,kPGPCipherAlgorithm_AES256,
		&AES256context);
	
	if(IsntPGPError(err))
	{
		err=PGPInitSymmetricCipher(AES256context,AES256key);
		
		if(IsntPGPError(err))
		{
			PGPByte ln_decr_buf[LICENSENUMBER_BIN_SIZE];
			lnToStream( ln, ln_decr_buf );
			
			err=PGPSymmetricCipherEncrypt( AES256context,
				ln_decr_buf+1, ln_encr+1 );
			
			ln_encr[0]=ln->version;
		}
		
		PGPFreeSymmetricCipherContext(AES256context);
	}
	
	return err;
}

#endif

// AES decrypt LN and check hash
static PGPError pgpDecryptLicenseNumber(PGPContextRef context,
	PGPLicenseNumberRef ln_ref, LICENSENUMBER *ln)
{
	PGPError err;
	PGPSymmetricCipherContextRef AES256context;
	PGPHashContextRef SHAContext;
	PGPByte HashedLN[20];
	const PGPByte *ln_encr;
	
	if (!context || !ln_ref || !ln)
		return kPGPError_BadParams;
	
	ln_encr = ((PGPLicenseNumber*)ln_ref)->ln_encr;
	
	err=kPGPError_NoErr;
	
	err=PGPNewSymmetricCipherContext(context,kPGPCipherAlgorithm_AES256,
		&AES256context);
	
	if(IsntPGPError(err))
	{
		err=PGPInitSymmetricCipher( AES256context,AES256key);
		
		if(IsntPGPError(err))
		{
			PGPByte ln_decr_buf[LICENSENUMBER_BIN_SIZE];

			err=PGPSymmetricCipherDecrypt( AES256context, ln_encr+1, ln_decr_buf+1);
			ln_decr_buf[0] = ln_encr[0];
			
			lnFromStream( ln_decr_buf, ln );
			
			// Hash the LN
			err=PGPNewHashContext(context,
				kPGPHashAlgorithm_SHA,&SHAContext);
			
			if(IsntPGPError(err))
			{
				// Hash everything except the hash field
 				err=PGPContinueHash(SHAContext, ln_decr_buf, LICENSENUMBER_BIN_SIZE-2);
				
				if(IsntPGPError(err))
				{
					err=PGPFinalizeHash(SHAContext,HashedLN);
					
					if(ln->shaHash[0]==HashedLN[0] && ln->shaHash[1]==HashedLN[1])
						err=kPGPError_NoErr; // Its OK
					else
						err=kPGPError_LNCorrupt;
				}
				
				PGPFreeHashContext(SHAContext);
			}
			
		}
		
		PGPFreeSymmetricCipherContext(AES256context);
	}
	
	return err;
}

#ifdef PGP_LICENSE_SERVER

// Creates a LN based on entry fields (using hardcoded private key).
// For use by web tools.
PGPError PGPLNCreate(PGPContextRef context,
							PGPUInt8 productID,
							PGPUInt16 evalPeriod,
							PGPUInt32 numLicenses, // 24 bit
							PGPUInt8 flags,
							PGPUInt32 serialNumber,
							PGPLicenseNumberRef *buffer)
{
	LICENSENUMBER pf;
	PGPByte HashedLN[20];
	PGPError err;
	PGPHashContextRef SHAContext;

	err=kPGPError_NoErr;
	*buffer = NULL;

	CLEAR_LICENSENUMBER(&pf);
	
	pf.version=kPGPLicenseNumberVersion;
	
	pf.productID=productID;
	pf.evalPeriod =		evalPeriod;
	pf.numLicenses =	numLicenses;
	pf.flags =			flags;
	pf.serialNumber =	serialNumber;
	
	// Hash the LN
	err=PGPNewHashContext(context,
		kPGPHashAlgorithm_SHA,&SHAContext);
	
	if(IsntPGPError(err))
	{
		PGPByte ln_buf[LICENSENUMBER_BIN_SIZE];

		// Hash everything except the hash field
		lnToStream( &pf, ln_buf );
		err=PGPContinueHash(SHAContext, ln_buf, LICENSENUMBER_BIN_SIZE-2);
		
		if(IsntPGPError(err))
		{
			err=PGPFinalizeHash(SHAContext,HashedLN);
			
			pf.shaHash[1]=HashedLN[1];
			pf.shaHash[0]=HashedLN[0];
			
		 	*buffer=(PGPLicenseNumberRef)PGPNewData(
				PGPPeekContextMemoryMgr(context),sizeof(PGPLicenseNumber),0);

			if( *buffer == NULL)
				err = kPGPError_OutOfMemory;

			if( IsntPGPError(err) )
				err=pgpEncryptLicenseNumber(context, &pf, *buffer);

			if( IsPGPError(err) )  {
				PGPFreeData( *buffer );
				*buffer = NULL;
			}
		}
		
		PGPFreeHashContext(SHAContext);
	}
	
	CLEAR_LICENSENUMBER( &pf );
	
	return err;
}

#endif

// Takes a base32 LN and imports it into internal opaque structure
PGPError PGPLNImportBase32(PGPContextRef context,
						const PGPByte *licenseNumber32, PGPLicenseNumberRef *buffer)
{
	PGPError err;
	PGPByte ln_buf[kPGPLicenseNumberMaxSize];
	PGPSize ln_buf_size;

	err=kPGPError_NoErr;

	if (!licenseNumber32 || !buffer)
		return kPGPError_BadParams;

	*buffer = NULL;

	/* remove '-' */
	{
		PGPByte ln_buf1[kPGPLicenseNumberMaxSize];
		PGPByte *p = ln_buf1;
		PGPUInt32 i;

		for( i=0; i<strlen(licenseNumber32) && i<sizeof(ln_buf1)-1; i++ )  {
			if( licenseNumber32[i] != '-' )
				*(p++) = licenseNumber32[i];
		}
		*p = '\0';
		
		err=pgpDecode32(ln_buf1, ln_buf, &ln_buf_size);

#ifdef PGP_DEBUG
	{
		PGPByte ln_buf2[kPGPLicenseNumberMaxSize];
		pgpEncode32( ln_buf, ln_buf_size, ln_buf2 );
		pgpAssert( strcmp(ln_buf2, ln_buf1)==0 );
	}
#endif
	}
	
	if(IsntPGPError(err))
	{
		LICENSENUMBER pn;
		PGPLicenseNumber ln_encr;
		
		if(ln_buf_size < LICENSENUMBER_BIN_SIZE) 
			return kPGPError_LNCorrupt;

		/* build LN opaque structure */
		pgpAssert( ln_buf_size <= 0xffff );
		memcpy( ln_encr.ln_encr, ln_buf, LICENSENUMBER_BIN_SIZE );
		
		err=pgpDecryptLicenseNumber(context, &ln_encr, &pn);
		
		if(IsntPGPError(err))
		{
			CLEAR_LICENSENUMBER( &pn );

			*buffer=(PGPLicenseNumberRef)PGPNewData( PGPPeekContextMemoryMgr(context), 
				sizeof(ln_encr), 0 );
			if( *buffer )  
				memcpy( *buffer, &ln_encr, sizeof(ln_encr) );
			else
				err = kPGPError_OutOfMemory;
		}
	}

	return err;
}

// Exports internal opaque structure to base32
PGPError PGPLNExportBase32( PGPContextRef context,
										  const PGPLicenseNumberRef licenseNumber,
										  const PGPByte *templ, 
										  PGPByte base32[kPGPLicenseNumberMaxSize] )  
{
	PGPError err;
	const PGPLicenseNumber *ln = (PGPLicenseNumber *)licenseNumber;
	PGPByte b32[kPGPLicenseNumberMaxSize];
	PGPByte *pb32 = b32;
	PGPUInt32 i;

	(void)context;

	if( templ == NULL )
		return pgpEncode32(ln->ln_encr, LICENSENUMBER_BIN_SIZE, base32);

	err=pgpEncode32(ln->ln_encr, LICENSENUMBER_BIN_SIZE, b32);

	if( IsPGPError(err) )
		return err;

	if( strlen(templ) >= sizeof(b32) )  {
		base32[0] = '\0';
		return kPGPError_BufferTooSmall;
	}

	for( i=0; i<strlen( templ ); i++ )  {
		if( templ[i]=='x' || templ[i]=='X' )
			base32[i] = *(pb32++);
		else
			base32[i] = templ[i];
	}
	base32[i] = '\0';

	return err;
}

#ifdef PGP_LICENSE_SERVER // [
// Exports internal opaque structure into application/form-urlencoded format
PGPError pgpLNExportUrlEncoded(PGPContextRef context,
	const PGPLicenseNumberRef ln_ref, PGPByte urlEncoded[kPGPLicenseNumberMaxSize])
{
	PGPError err = kPGPError_NoErr;
	LICENSENUMBER ln;
	PGPByte flags_str[80] = { '\0', '\0' };
	PGPByte *f = flags_str+1;

	*urlEncoded = '\0';

	err=pgpDecryptLicenseNumber(context,ln_ref,&ln);
	if( IsPGPError(err) )
		return err;

	/* we know that all fields consist of valid urlencoded characters, so we don't use
	   addFormUrlEncodedParam 
	*/

	if( ln.flags & kPGPLicenseNumberFlag_Evaluation )
		strcat( flags_str, ",eval" );
	if( ln.flags & kPGPLicenseNumberFlag_Enterprise )
		strcat( flags_str, ",enterprise" );
	if( ln.flags & kPGPLicenseNumberFlag_Disk )
		strcat( flags_str, ",disk");
	if( ln.flags & kPGPLicenseNumberFlag_Mail )
		strcat( flags_str, ",mail" );
	if( ln.flags & kPGPLicenseNumberFlag_Desktop )
		strcat( flags_str, ",desktop" );

	sprintf( urlEncoded, "version=%d&prodid=%d&evaldays=%d&licenses=%d&flags=%s&sn=%d",
		ln.version, ln.productID, ln.evalPeriod, ln.numLicenses, f, ln.serialNumber );

	CLEAR_LICENSENUMBER(&ln);

	return err;
}
#endif // PGP_LICENSE_SERVER ]


// Functions for retrieving internal values
PGPError PGPLNGetProductID(PGPContextRef context,PGPLicenseNumberRef licenseNumber,
							PGPUInt8 *productID)
{
	PGPError err;
	LICENSENUMBER pn;

	err=pgpDecryptLicenseNumber(context,licenseNumber,&pn);

	if(IsntPGPError(err))
	{
		*productID=pn.productID;

		CLEAR_LICENSENUMBER(&pn);
	}

	return err;
}


PGPError			PGPLNGetEvalPeriod(PGPContextRef context,PGPLicenseNumberRef licenseNumber,
							PGPUInt16 *evalPeriod)
{
	PGPError err;
	LICENSENUMBER pn;

	err=pgpDecryptLicenseNumber(context,licenseNumber,&pn);

	if(IsntPGPError(err))  {
		*evalPeriod=pn.evalPeriod;
		CLEAR_LICENSENUMBER(&pn);
	}
	return err;
}

PGPError			PGPLNGetNumLicenses(PGPContextRef context,PGPLicenseNumberRef licenseNumber,
							PGPUInt32 *numLicenses)
{
	PGPError err;
	LICENSENUMBER pn;

	err=pgpDecryptLicenseNumber(context,licenseNumber,&pn);

	if(IsntPGPError(err))  {
		*numLicenses=pn.numLicenses;
		CLEAR_LICENSENUMBER(&pn);
	}

	return err;
}

PGPError			PGPLNGetFlags(PGPContextRef context,PGPLicenseNumberRef licenseNumber,
							PGPUInt8 *flags)
{
	PGPError err;
	LICENSENUMBER pn;

	err=pgpDecryptLicenseNumber(context,licenseNumber,&pn);

	if(IsntPGPError(err))
	{
		*flags=pn.flags;

		CLEAR_LICENSENUMBER(&pn);
	}

	return err;
}

PGPError			PGPLNGetSN(PGPContextRef context,PGPLicenseNumberRef licenseNumber,
							PGPUInt32 *serialNumber)
{
	PGPError err;
	LICENSENUMBER pn;

	err=pgpDecryptLicenseNumber(context,licenseNumber,&pn);

	if(IsntPGPError(err))  {
		*serialNumber=pn.serialNumber;
		CLEAR_LICENSENUMBER(&pn);
	}

	return err;
}
							
// Free internal opaque structure
void				PGPLNFree(PGPLicenseNumberRef licenseNumber)
{
	pgpClearMemory(licenseNumber,sizeof(PGPLicenseNumber));
	PGPFreeData(licenseNumber);
}

static PGPError calculateNameHash( PGPContextRef context, 
		const PGPByte *customerName, const PGPByte *companyName, PGPHashContextRef hashContext )
{
	PGPError err = kPGPError_NoErr;
	const PGPByte nullStr[] = "";
	PGPSize ucs2BufSize;
	PGPUInt16 *ucs2Buf = NULL; 
	PGPSize ucs2Size;
	PGPSize utf8charSize;
	PGPByte utf8charBuf[16];
	PGPUInt32 i;

	if( customerName==NULL && companyName==NULL )
		return kPGPError_LazyProgrammer;
		
	ucs2BufSize = strlen(customerName);
	if( ucs2BufSize < strlen(companyName) )
		ucs2BufSize = strlen(companyName);

	/* allocate large enough buffer to hold customerName or companyName */
	ucs2Buf = (PGPUInt16*)PGPNewData(
				PGPPeekContextMemoryMgr(context), (ucs2BufSize+1)*sizeof(PGPUInt16), 0);
	if( ucs2Buf == NULL )
		return kPGPError_OutOfMemory;

	/* hash customerName */
	if(customerName!=NULL && IsntPGPError(err))   {
     	pgpUTF8StringToUCS2( customerName, kPGPUnicodeNullTerminated, ucs2Buf, ucs2BufSize+1, (PGPUInt32 *)&ucs2Size );
		if( ucs2Size==0 )
			return kPGPError_BadParams;

		pgpUCS2StringToUpper( ucs2Buf, ucs2Size );
		for( i=0; i<ucs2Size; i++ )  {
			if( !pgpUnicodeCharIsWhitespace( ucs2Buf[i] ) && !pgpUnicodeCharIsPunctuation( ucs2Buf[i] ) )  
			{
				pgpUCS2StringToUTF8( ucs2Buf+i, 1, utf8charBuf, sizeof(utf8charBuf), (PGPUInt32 *)&utf8charSize );
				if( IsntPGPError(err) )
					err=PGPContinueHash(hashContext, utf8charBuf, utf8charSize);
			}
		}
	}
	if( IsntPGPError(err) )	/* terminator */
		err=PGPContinueHash(hashContext, nullStr, 1);
			
	/* hash companyName, similiar to customerName */
	if(companyName!=NULL && IsntPGPError(err))   {
     	pgpUTF8StringToUCS2( companyName, kPGPUnicodeNullTerminated, ucs2Buf, ucs2BufSize+1, (PGPUInt32 *)&ucs2Size );
		if( ucs2Size==0 )
			return kPGPError_BadParams;

		pgpUCS2StringToUpper( ucs2Buf, ucs2Size );
		for( i=0; i<ucs2Size; i++ )  {
			if( !pgpUnicodeCharIsWhitespace( ucs2Buf[i] ) && !pgpUnicodeCharIsPunctuation( ucs2Buf[i] ) )  
			{
				pgpUCS2StringToUTF8( ucs2Buf+i, 1, utf8charBuf, sizeof(utf8charBuf), (PGPUInt32 *)&utf8charSize );
				if( IsntPGPError(err) )
					err=PGPContinueHash(hashContext, utf8charBuf, utf8charSize);
			}
		}
	}
	if( IsntPGPError(err) )	/* terminator */
		err=PGPContinueHash(hashContext, nullStr, 1);

	/* free UCS2 buffer */
	if( ucs2Buf != NULL )
		PGPFreeData( ucs2Buf );

	return err;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -