From 7c0e5b51fe8bca5192638fa141dcc8e450ba4028 Mon Sep 17 00:00:00 2001
From: Kirill Volinsky <mataes2007@gmail.com>
Date: Sun, 28 Oct 2012 16:52:51 +0000
Subject: added utility for packing your *.crt file

git-svn-id: http://svn.miranda-ng.org/main/trunk@2102 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 protocols/Skype/keypacker/keypacker.cpp            | 207 +++++++++++++++++++++
 protocols/Skype/keypacker/keypacker.sln            |  23 +++
 protocols/Skype/keypacker/keypacker.vcxproj        |  83 +++++++++
 .../Skype/keypacker/keypacker.vcxproj.filters      |  24 +++
 protocols/Skype/keypacker/stdafx.h                 |   8 +
 5 files changed, 345 insertions(+)
 create mode 100644 protocols/Skype/keypacker/keypacker.cpp
 create mode 100644 protocols/Skype/keypacker/keypacker.sln
 create mode 100644 protocols/Skype/keypacker/keypacker.vcxproj
 create mode 100644 protocols/Skype/keypacker/keypacker.vcxproj.filters
 create mode 100644 protocols/Skype/keypacker/stdafx.h

diff --git a/protocols/Skype/keypacker/keypacker.cpp b/protocols/Skype/keypacker/keypacker.cpp
new file mode 100644
index 0000000000..6aa5b3e0c0
--- /dev/null
+++ b/protocols/Skype/keypacker/keypacker.cpp
@@ -0,0 +1,207 @@
+#include "stdafx.h"
+
+void *buf;
+
+static const char base64Fillchar = '='; // used to mark partial words at the end
+
+// this lookup table defines the base64 encoding
+char *base64EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+const unsigned char base64DecodeTable[] = {
+	99, 98, 98, 98, 98, 98, 98, 98, 98, 97,  97, 98, 98, 97, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  //00 -29
+	98, 98, 97, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 62, 98, 98, 98, 63, 52, 53,  54, 55, 56, 57, 58, 59, 60, 61, 98, 98,  //30 -59
+	98, 96, 98, 98, 98, 0, 1, 2, 3, 4,   5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  //60 -89
+	25, 98, 98, 98, 98, 98, 98, 26, 27, 28,  29, 30, 31, 32, 33, 34, 35, 36, 37, 38,  39, 40, 41, 42, 43, 44, 45, 46, 47, 48,  //90 -119
+	49, 50, 51, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  //120 -149
+	98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  //150 -179
+	98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  //180 -209
+	98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  //210 -239
+	98, 98, 98, 98, 98, 98, 98, 98, 98, 98,  98, 98, 98, 98, 98, 98                                               //240 -255
+};
+
+int encodeLength(int inlen, char formatted)
+{
+	unsigned int i = ((inlen-1)/3*4+4+1);
+	if (formatted) i+=inlen/54;
+	return i;
+}
+
+char * encode(unsigned char *inbuf, unsigned int inlen, char formatted)
+{
+	int i = encodeLength(inlen, formatted), k = 17, eLen = inlen/3, j;
+	malloc(i);
+	char * curr = (char *)buf;
+	for (i=0;i<eLen;i++)
+	{
+		// Copy next three bytes into lower 24 bits of int, paying attention to sign.
+		j = (inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
+		// Encode the int into four chars
+		*(curr++) = base64EncodeTable[ j>>18      ];
+		*(curr++) = base64EncodeTable[(j>>12)&0x3f];
+		*(curr++) = base64EncodeTable[(j>> 6)&0x3f];
+		*(curr++) = base64EncodeTable[(j)&0x3f];
+		if (formatted) { if ( !k) { *(curr++) = '\n'; k = 18; } k--; }
+	}
+	eLen = inlen-eLen*3; // 0 - 2.
+	if (eLen == 1)
+	{
+		*(curr++) = base64EncodeTable[ inbuf[0]>>2      ];
+		*(curr++) = base64EncodeTable[(inbuf[0]<<4)&0x3F];
+		*(curr++) = base64Fillchar;
+		*(curr++) = base64Fillchar;
+	} else if (eLen == 2)
+	{
+		j = (inbuf[0]<<8)|inbuf[1];
+		*(curr++) = base64EncodeTable[ j>>10      ];
+		*(curr++) = base64EncodeTable[(j>> 4)&0x3f];
+		*(curr++) = base64EncodeTable[(j<< 2)&0x3f];
+		*(curr++) = base64Fillchar;
+	}
+	*(curr++) = 0;
+	return (char *)buf;
+}
+
+unsigned int decodeSize(char * data)
+{
+	if ( !data) return 0;
+	int size = 0;
+	unsigned char c;
+	//skip any extra characters (e.g. newlines or spaces)
+	while (*data)
+	{
+		if (*data>255) { return 0; }
+		c = base64DecodeTable[(unsigned char)(*data)];
+		if (c<97) size++;
+		else if (c == 98) { return 0; }
+		data++;
+	}
+	if (size == 0) return 0;
+	do { data--; size--; } while (*data == base64Fillchar); size++;
+	return (unsigned int)((size*3)/4);
+}
+
+unsigned char decode(char * data, unsigned char *buf, int len)
+{
+	if ( !data) return 0;
+	int i=0, p = 0;
+	unsigned char d, c;
+	for (;;)
+	{
+
+	#define BASE64DECODE_READ_NEXT_CHAR(c)                                              \
+	do {                                                                        \
+	if (data[i]>255) { c = 98; break; }                                        \
+	c = base64DecodeTable[(unsigned char)data[i++]];                       \
+	}while (c == 97);                                                             \
+	if(c == 98) { return 0; }
+
+		BASE64DECODE_READ_NEXT_CHAR(c)
+			if (c == 99) { return 2; }
+			if (c == 96)
+			{
+				if (p == (int)len) return 2;
+				return 1;
+			}
+
+			BASE64DECODE_READ_NEXT_CHAR(d)
+				if ((d == 99) || (d == 96)) { return 1; }
+				if (p == (int)len) { return 0; }
+				buf[p++] = (unsigned char)((c<<2)|((d>>4)&0x3));
+
+				BASE64DECODE_READ_NEXT_CHAR(c)
+					if (c == 99) { return 1; }
+					if (p == (int)len)
+					{
+						if (c == 96) return 2;
+						return 0;
+					}
+					if (c == 96) { return 1; }
+					buf[p++] = (unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf));
+
+					BASE64DECODE_READ_NEXT_CHAR(d)
+						if (d == 99) { return 1; }
+						if (p == (int)len)
+						{
+							if (d == 96) return 2;
+							return 0;
+						}
+						if (d == 96) { return 1; }
+						buf[p++] = (unsigned char)(((c<<6)&0xc0)|d);
+	}
+}
+#undef BASE64DECODE_READ_NEXT_CHAR
+
+unsigned char *decode(char * data, int *outlen)
+{
+	if ( !data) { *outlen = 0; return (unsigned char*)""; }
+	unsigned int len = decodeSize(data);
+	if (outlen) *outlen = len;
+	if ( !len) return NULL;
+	malloc(len+1);
+	if( !decode(data, (unsigned char*)buf, len)) { return NULL; }
+	return (unsigned char*)buf;
+}
+
+int main()
+{
+	aes_context ctx;
+	FILE *fin, *fout;
+
+	aes_set_key( &ctx, (BYTE*)MY_KEY, 128);
+
+	//encrypt
+	fin = fopen("..\\..\\..\\..\\SkypeKit\\keypair.crt", "rb");
+	fseek(fin, 0, SEEK_END);
+	long fileSize =	ftell(fin);
+	rewind (fin);
+	int needbyte = 16 - fileSize%16;
+	int corsize = fileSize + needbyte;
+	unsigned char *inBuf = (unsigned char*)malloc(corsize + 1);
+	unsigned char *locbuf = (unsigned char*)malloc(corsize + 1);
+	fread(inBuf, fileSize, 1, fin);
+	fclose(fin);
+	memset(inBuf+fileSize, 0, needbyte);
+	inBuf[corsize] = 0;
+	for (int i = 0; i < corsize; i+=16) {
+		aes_encrypt(&ctx, inBuf+i, locbuf+i);
+	}
+	free(inBuf);
+	locbuf[corsize] = 0; //cert should be null terminated
+	int basecoded = encodeLength(corsize, false);
+	buf = malloc(basecoded + 1);
+	unsigned char *tmp = (unsigned char *)malloc(basecoded + 1);
+	tmp = (unsigned char *)encode(locbuf, corsize, false);
+	tmp[basecoded] = 0;
+	free(locbuf);
+	fout = fopen("..\\..\\..\\..\\SkypeKit\\keypair.bin", "wb");
+	fputs((const char*)tmp, fout);
+	fclose(fout);
+	free(buf);
+	//free(tmp); todo:fix
+
+	//decrypt
+	fin = fopen("..\\..\\..\\..\\SkypeKit\\keypair.bin", "rb");
+	fseek(fin, 0, SEEK_END);
+	long fileSizeD = ftell(fin);
+	rewind(fin);
+	unsigned char *inBufD = (unsigned char*)malloc(fileSizeD);
+	fread(inBufD, fileSizeD, 1, fin);
+	fclose(fin);
+	inBufD[fileSizeD] = 0;
+	int basedecoded = decodeSize((char*)inBufD);
+	unsigned char *bufD = (unsigned char*)malloc(basedecoded + 1);
+	unsigned char *tmpD = (unsigned char*)malloc(basedecoded + 1);
+	decode((char*)inBufD, tmpD, basedecoded);
+	for (int i = 0; i < basedecoded; i += 16) {
+		aes_decrypt(&ctx, tmpD+i, bufD+i);
+	}
+	bufD[basedecoded] = 0; //cert should be null terminated
+	//free(inBufD); todo:fix
+	free(tmpD);
+	fout = fopen("..\\..\\..\\..\\SkypeKit\\keypair.crt.decrypted", "wb");
+	fputs((const char*)bufD, fout);
+	fclose(fout);
+	free(bufD);
+            
+	return 0;
+}
\ No newline at end of file
diff --git a/protocols/Skype/keypacker/keypacker.sln b/protocols/Skype/keypacker/keypacker.sln
new file mode 100644
index 0000000000..fd93844de0
--- /dev/null
+++ b/protocols/Skype/keypacker/keypacker.sln
@@ -0,0 +1,23 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "keypacker", "keypacker.vcxproj", "{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}"
+EndProject
+Global
+	GlobalSection(DPCodeReviewSolutionGUID) = preSolution
+		DPCodeReviewSolutionGUID = {00000000-0000-0000-0000-000000000000}
+	EndGlobalSection
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}.Debug|Win32.ActiveCfg = Debug|Win32
+		{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}.Debug|Win32.Build.0 = Debug|Win32
+		{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}.Release|Win32.ActiveCfg = Release|Win32
+		{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal
diff --git a/protocols/Skype/keypacker/keypacker.vcxproj b/protocols/Skype/keypacker/keypacker.vcxproj
new file mode 100644
index 0000000000..4021fbd185
--- /dev/null
+++ b/protocols/Skype/keypacker/keypacker.vcxproj
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{826A98BD-CDC2-4D4F-BC20-310F415F9FFB}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>keypacker</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>false</LinkIncremental>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>Use</PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="stdafx.h" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="..\src\aes\aes.c" />
+    <ClCompile Include="keypacker.cpp" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
\ No newline at end of file
diff --git a/protocols/Skype/keypacker/keypacker.vcxproj.filters b/protocols/Skype/keypacker/keypacker.vcxproj.filters
new file mode 100644
index 0000000000..923bcc1f4b
--- /dev/null
+++ b/protocols/Skype/keypacker/keypacker.vcxproj.filters
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <ClCompile Include="..\src\aes\aes.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="keypacker.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+  <ItemGroup>
+    <Filter Include="Header Files">
+      <UniqueIdentifier>{66b7c02a-1ddc-42a1-9979-8b109c0d5e39}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{22eb2a43-df72-4aa2-9503-cb25b3faf955}</UniqueIdentifier>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="stdafx.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
+</Project>
\ No newline at end of file
diff --git a/protocols/Skype/keypacker/stdafx.h b/protocols/Skype/keypacker/stdafx.h
new file mode 100644
index 0000000000..4bd30a212f
--- /dev/null
+++ b/protocols/Skype/keypacker/stdafx.h
@@ -0,0 +1,8 @@
+#include <stdio.h>
+#include <windows.h>
+
+extern "C"
+{
+#include "..\src\aes\aes.h"
+}
+#include "..\..\..\..\SkypeKit\key.h"
-- 
cgit v1.2.3