[Box Backup-commit] COMMIT r3050 - in box/invisnet/vs2010/0.11/bin: . bbutil bbutil/win32

subversion at boxbackup.org subversion at boxbackup.org
Thu Jan 19 18:12:00 GMT 2012


Author: invisnet
Date: 2012-01-19 18:11:59 +0000 (Thu, 19 Jan 2012)
New Revision: 3050

Added:
   box/invisnet/vs2010/0.11/bin/bbutil/
   box/invisnet/vs2010/0.11/bin/bbutil/bbutil.cpp
   box/invisnet/vs2010/0.11/bin/bbutil/bbutil.h
   box/invisnet/vs2010/0.11/bin/bbutil/est.cpp
   box/invisnet/vs2010/0.11/bin/bbutil/est.h
   box/invisnet/vs2010/0.11/bin/bbutil/u2w.cpp
   box/invisnet/vs2010/0.11/bin/bbutil/u2w.h
   box/invisnet/vs2010/0.11/bin/bbutil/win32/
   box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.ico
   box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.rc
   box/invisnet/vs2010/0.11/bin/bbutil/win32/resource.h
Log:
bbutil - util to import *nix keys

Added: box/invisnet/vs2010/0.11/bin/bbutil/bbutil.cpp
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/bbutil.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/bbutil.cpp	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,30 @@
+#include "bbutil.h"
+
+
+int main(int argc, char *argv[])
+{
+	switch(argc) {
+	case 1:
+		cout << "bbutil" << endl
+			  << endl
+			  << "    est <path>" << endl
+			  << endl
+			  << "        Estimate disk usage." << endl
+			  << endl
+			  << "    u2w <KeysFile> <CertificateFile> <PrivateKeyFile>" << endl
+			  << endl
+			  << "        Imports Unix-style certificate and key files." << endl
+			  << endl;
+		break;
+	case 3:
+		if (!strcmp("est",argv[1]))
+			do_est(argv[2]);
+		break;
+	case 5:
+		if (!strcmp("u2w",argv[1]))
+			do_u2w(argv[2],argv[3],argv[4]);
+		break;
+	}
+
+	return 0;
+}

Added: box/invisnet/vs2010/0.11/bin/bbutil/bbutil.h
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/bbutil.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/bbutil.h	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,9 @@
+#include "Box.h"
+
+#include <iostream>
+#include <fstream>
+
+#include "est.h"
+#include "u2w.h"
+
+using namespace std;

Added: box/invisnet/vs2010/0.11/bin/bbutil/est.cpp
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/est.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/est.cpp	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,87 @@
+#include "bbutil.h"
+#include "zlib.h"
+
+
+static uint64_t rawTotal = 0;
+static uint64_t cmpTotal = 0;
+
+
+static void recurse(std::string &path)
+{
+	uint64_t c = 0;
+	struct dirent *de;
+	DIR *dir = opendir(path.c_str());
+
+	while(NULL != (de = readdir(dir))) {
+		if ('.' == de->d_name[0] && ('\0' == de->d_name[1] || ('.' == de->d_name[1] && '\0' == de->d_name[2])))
+			continue;
+
+		std::string str(path);
+		str.append("\\");
+		str.append(de->d_name);
+
+		if (de->d_type & DT_DIR) {
+			cout << str.c_str() << ": " << rawTotal << " => " << cmpTotal << endl;
+			recurse(str);
+		} else {
+			char		inBuf[4096];
+			Bytef		outBuf[4096];
+			z_stream	z = {	reinterpret_cast<Bytef*>(inBuf),
+								0,
+								0,
+								outBuf,
+								sizeof(outBuf),
+								0,
+								NULL,
+								NULL,
+								Z_NULL,
+								Z_NULL,
+								Z_NULL,
+								Z_BINARY,
+								0,
+								0};
+			ifstream	fs(str, ios_base::binary);
+
+			if (!fs.bad()) {
+				if (Z_OK == deflateInit(&z,Z_DEFAULT_COMPRESSION)) {
+					do {
+						fs.read(inBuf,sizeof(inBuf));
+
+						z.next_in = reinterpret_cast<Bytef*>(inBuf);
+						z.avail_in = fs.gcount();
+						do {
+							z.next_out = outBuf;
+							z.avail_out = sizeof(outBuf);
+							deflate(&z,Z_NO_FLUSH);
+						} while(z.avail_in > 0);
+					} while(fs.good());
+					do {
+						z.next_out = outBuf;
+						z.avail_out = sizeof(outBuf);
+					} while (Z_OK == deflate(&z,Z_FINISH));
+					deflateEnd(&z);
+
+					rawTotal += z.total_in;
+					cmpTotal += z.total_out;
+				} else {
+					cerr << "deflateInit failed" << endl;
+				}
+			} else {
+				cerr << "Failed to open '" << str << "'" << endl;
+			}
+			fs.close();
+		}
+	}
+	closedir(dir);
+}
+
+
+void do_est(const char *path)
+{
+	std::string rpath(path);
+
+	recurse(rpath);
+
+	cout << "Total data read:  " << rawTotal << endl;
+	cout << "Total compressed: " << cmpTotal << endl;
+}

Added: box/invisnet/vs2010/0.11/bin/bbutil/est.h
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/est.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/est.h	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,2 @@
+
+extern void do_est(const char *path);

Added: box/invisnet/vs2010/0.11/bin/bbutil/u2w.cpp
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/u2w.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/u2w.cpp	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,230 @@
+#include "bbutil.h"
+
+#include <WinCrypt.h>
+
+
+
+static HCRYPTPROV getCryptProv() throw(Win32Exception)
+{
+	HCRYPTPROV	hCryptProv	= NULL;
+
+	// first delete any existing provider that might be associated with existing certificates
+	CryptAcquireContext(&hCryptProv,"BoxBackup",MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_DELETEKEYSET|CRYPT_MACHINE_KEYSET);
+
+	if (!CryptAcquireContext(&hCryptProv,"BoxBackup",MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_MACHINE_KEYSET)) {
+		if (!CryptAcquireContext(&hCryptProv,"BoxBackup",MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET)) {
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptAcquireContext, "Could not aquire context")
+		}
+	}
+	return hCryptProv;
+}
+
+static void importKeysFile(const char *KeysFile) throw(Win32Exception)
+{
+	ifstream	sKeysFile(KeysFile,ios_base::binary | ios_base::ate);
+
+	if (sKeysFile.bad())
+		THROW_EXCEPTION_MESSAGE(Win32Exception, Internal, "Bad KeysFile");
+	ifstream::pos_type lenKeysFile = sKeysFile.tellg();
+	sKeysFile.seekg(0);
+
+	unique_ptr<char[]> bufKeysFile(new char[lenKeysFile]);
+	sKeysFile.read(bufKeysFile.get(),lenKeysFile);
+	sKeysFile.close();
+
+	DWORD err;
+	HKEY hKey = NULL;
+	try {
+		if (ERROR_SUCCESS != (err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Box Backup",0,KEY_QUERY_VALUE|KEY_SET_VALUE,&hKey))) {
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_RegOpenKeyEx, "Cannot open SOFTWARE\\Box Backup");
+
+		} else {
+			if (ERROR_SUCCESS != (err = RegSetValueEx(hKey,
+																	"FileEncKeys",
+																	0,
+																	REG_BINARY,
+																	reinterpret_cast<BYTE*>(bufKeysFile.get()),
+																	static_cast<DWORD>(lenKeysFile))))
+			{
+				THROW_EXCEPTION_MESSAGE(Win32Exception, API_RegSetValueEx, "Cannot write FileEncKeys");
+			}
+
+			RegCloseKey(hKey);
+		}
+	} catch(...) {
+		if (hKey)
+			RegCloseKey(hKey);
+		throw;
+	}
+
+	cout << "Imported KeysFile (" << KeysFile << ") OK" << endl;
+}
+
+static void importCertificateFile(HCRYPTPROV hCryptProv, const char *CertificateFile) throw(Win32Exception)
+{
+	ifstream sCertificateFile(CertificateFile, ios_base::ate);
+
+	if (sCertificateFile.bad())
+		THROW_EXCEPTION_MESSAGE(Win32Exception, Internal, "Bad CertificateFile");
+	ifstream::pos_type lenCertificateFile = sCertificateFile.tellg();
+	sCertificateFile.seekg(0);
+
+	unique_ptr<char[]> bufCertificateFile(new char[lenCertificateFile]);
+	sCertificateFile.read(bufCertificateFile.get(),lenCertificateFile);
+	sCertificateFile.close();
+
+
+	HCERTSTORE		hSystemStore	= NULL;
+	PCCERT_CONTEXT	pCertContext	= NULL;
+	HCRYPTKEY		hPubKey			= NULL;
+
+	try {
+		DWORD lenBinary = static_cast<DWORD>(lenCertificateFile * 3 / 4);
+		unique_ptr<BYTE[]> binCertificateFile(new BYTE[lenBinary]);
+
+		if (!CryptStringToBinary(bufCertificateFile.get(),
+										 static_cast<DWORD>(lenCertificateFile),
+										 CRYPT_STRING_BASE64HEADER,
+										 binCertificateFile.get(),
+										 &lenBinary,
+										 NULL,
+										 NULL))
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptStringToBinary, "Failed to decode Certificate");
+		}
+
+
+		if (NULL == (hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY")))
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CertOpenStore, "Cannot open Local Machine certificate store");
+		
+		if (!CertAddEncodedCertificateToStore(hSystemStore,
+														  X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+														  binCertificateFile.get(),
+														  lenBinary,
+														  CERT_STORE_ADD_REPLACE_EXISTING,
+														  &pCertContext))
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CertAddEncodedCertificateToStore, "Failed to add certificate to store");
+		}
+
+		if (!CryptImportPublicKeyInfo(hCryptProv, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, &pCertContext->pCertInfo->SubjectPublicKeyInfo, &hPubKey))
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptImportPublicKeyInfo, "Failed to import Public Key");
+		CryptDestroyKey(hPubKey);
+
+		CRYPT_KEY_PROV_INFO cryptKeyProvInfo = { L"BoxBackup",MS_DEF_PROV_W,PROV_RSA_FULL,CRYPT_MACHINE_KEYSET,0,NULL,AT_KEYEXCHANGE };
+
+		if (!CertSetCertificateContextProperty(pCertContext,CERT_KEY_PROV_INFO_PROP_ID,0,&cryptKeyProvInfo))
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CertSetCertificateContextProperty, "Failed to set context property");
+
+		CertFreeCertificateContext(pCertContext);
+		
+	} catch(...) {
+		if (pCertContext)
+			CertFreeCertificateContext(pCertContext);
+		if (hSystemStore)
+			CertCloseStore(hSystemStore,CERT_CLOSE_STORE_FORCE_FLAG);
+		throw;
+	}
+
+	cout << "Imported CertificateFile (" << CertificateFile << ") OK" << endl;
+}
+
+void importPrivateKeyFile(HCRYPTPROV hCryptProv, const char *PrivateKeyFile) throw(Win32Exception)
+{
+	ifstream sPrivateKeyFile(PrivateKeyFile, ios_base::ate);
+
+	if (sPrivateKeyFile.bad())
+		THROW_EXCEPTION_MESSAGE(Win32Exception, Internal, "Bad PrivateKeyFile");
+	ifstream::pos_type lenPrivateKeyFile = sPrivateKeyFile.tellg();
+	sPrivateKeyFile.seekg(0);
+
+	unique_ptr<char[]> bufPrivateKeyFile(new char[lenPrivateKeyFile]);
+	sPrivateKeyFile.getline(bufPrivateKeyFile.get(),lenPrivateKeyFile); // eat the first line so decoding doesn't choke
+	sPrivateKeyFile.read(bufPrivateKeyFile.get(),lenPrivateKeyFile);
+	sPrivateKeyFile.close();
+
+
+	try {
+		DWORD lenBinaryPrivateKey = static_cast<DWORD>(lenPrivateKeyFile * 3 / 4);
+		unique_ptr<BYTE[]> binPrivateKeyFile(new BYTE[lenBinaryPrivateKey]);
+
+		if (!CryptStringToBinary(bufPrivateKeyFile.get(),
+										 static_cast<DWORD>(lenPrivateKeyFile),
+										 CRYPT_STRING_BASE64,
+										 binPrivateKeyFile.get(),
+										 &lenBinaryPrivateKey,
+										 NULL,
+										 NULL))
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptStringToBinary, "Cannot decode Private Key");
+		}
+
+		DWORD lenDecodedPrivateKey = 0;
+		if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+									  PKCS_RSA_PRIVATE_KEY,
+									  binPrivateKeyFile.get(),
+									  lenBinaryPrivateKey,
+									  0,
+									  NULL,
+									  &lenDecodedPrivateKey) && ERROR_MORE_DATA != GetLastError())
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptDecodeObject, "Failed to get buffer length to decode Private Key")
+		}
+
+		unique_ptr<BYTE[]> decodedPrivateKey(new BYTE[lenDecodedPrivateKey]);
+		if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+									  PKCS_RSA_PRIVATE_KEY,
+									  binPrivateKeyFile.get(),
+									  lenBinaryPrivateKey,
+									  0,
+									  decodedPrivateKey.get(),
+									  &lenDecodedPrivateKey))
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptDecodeObject, "Failed to decode Private Key")
+		}
+
+		HCRYPTKEY hPrivKey;
+		if (!CryptImportKey(hCryptProv,
+								  decodedPrivateKey.get(),
+								  lenDecodedPrivateKey,
+								  NULL,
+								  CRYPT_EXPORTABLE,
+								  &hPrivKey))
+		{
+			THROW_EXCEPTION_MESSAGE(Win32Exception, API_CryptImportKey, "Failed to import Private Key")
+		}
+		CryptDestroyKey(hPrivKey);
+
+	} catch(...) {
+		throw;
+	}
+
+	cout << "Imported PrivateKeyFile (" << PrivateKeyFile << ") OK" << endl;
+}
+
+
+/*
+ * This is a utility function that can only be called by an administrator.
+ * I'm assuming things are as they ought to be - there are far better
+ * and easier ways to do damage to a system than by abusing this code.
+ */
+void do_u2w(const char *KeysFile, const char *CertificateFile, const char *PrivateKeyFile) throw()
+{
+	HCRYPTPROV	hCryptProv = NULL;
+
+	try {
+		hCryptProv	= getCryptProv();
+
+		importKeysFile(KeysFile);
+		importCertificateFile(hCryptProv, CertificateFile);
+		importPrivateKeyFile(hCryptProv, PrivateKeyFile);
+
+	} catch(Win32Exception &e) {
+		cout << e.what() << ": " << e.GetMessage() << endl;
+	} catch(std::exception &e) {
+		cout << e.what() << endl;
+	}
+
+	if (hCryptProv)
+		CryptReleaseContext(hCryptProv,0);
+}

Added: box/invisnet/vs2010/0.11/bin/bbutil/u2w.h
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/u2w.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/u2w.h	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,3 @@
+
+
+extern void do_u2w(const char *KeysFile, const char *CertificateFile, const char *PrivateKeyFile);

Added: box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.ico
===================================================================
(Binary files differ)


Property changes on: box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.ico
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.rc
===================================================================
(Binary files differ)


Property changes on: box/invisnet/vs2010/0.11/bin/bbutil/win32/bbutil.rc
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: box/invisnet/vs2010/0.11/bin/bbutil/win32/resource.h
===================================================================
--- box/invisnet/vs2010/0.11/bin/bbutil/win32/resource.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/bin/bbutil/win32/resource.h	2012-01-19 18:11:59 UTC (rev 3050)
@@ -0,0 +1,14 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Visual C++ generated include file.
+// Used by bbutil.rc
+
+// Next default values for new objects
+// 
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE        101
+#define _APS_NEXT_COMMAND_VALUE         40001
+#define _APS_NEXT_CONTROL_VALUE         1001
+#define _APS_NEXT_SYMED_VALUE           101
+#endif
+#endif




More information about the Boxbackup-commit mailing list