add a c++ crypto component, ported from bug 400742; with a specialized makefile to build in the extension (with the gecko sdk)

This commit is contained in:
Dan Mills 2008-04-25 18:28:31 -07:00
parent 63af6c5dbd
commit 2689bfa5b9
5 changed files with 617 additions and 0 deletions

View File

@ -0,0 +1,93 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 Weave code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dan Mills <thunder@mozilla.com> (original author)
* Honza Bambas <honzab@allpeers.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
[scriptable, uuid(d3b0f750-c976-46d0-be20-96b24f4684bc)]
interface IWeaveCrypto : nsISupports
{
/**
* Shortcuts for some algorithm SEC OIDs. Full list available here:
* http://lxr.mozilla.org/seamonkey/source/security/nss/lib/util/secoidt.h
*/
const unsigned long DES_EDE3_CBC = 156;
// Unsupported now...
const unsigned long AES_128_ECB = 183;
const unsigned long AES_128_CBC = 184;
const unsigned long AES_192_ECB = 185;
const unsigned long AES_192_CBC = 186;
const unsigned long AES_256_ECB = 187;
const unsigned long AES_256_CBC = 188;
/**
* One of the above constants. Assigning differnt value
* will fail cause the encrypt method fail.
* Default value is DES_EDE3_CBC.
*/
attribute unsigned long algorithm;
/**
* Encrypt data using a passphrase
* See algorithm attribute, it determines which mechanisms will be used
*
* @param pass
* Passphrase to encrypt with
* @param iter
* Number of iterations for key strengthening
* This parameter is currently ignored and is always 1
* @param clearText
* Data to be encrypted
* @returns Encrypted data, base64 encoded
*/
ACString encrypt(in ACString pass, in ACString clearText);
/**
* Decrypt data using a passphrase
*
* @param pass
* Passphrase to decrypt with
* @param cipherText
* Base64 encoded data to be decrypted
* @returns Decrypted data
*/
ACString decrypt(in ACString pass, in ACString cipherText);
};

179
services/crypto/Makefile Normal file
View File

@ -0,0 +1,179 @@
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# 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 Weave code.
#
# The Initial Developer of the Original Code is
# Mozilla Corporation
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Dan Mills <thunder@mozilla.com> (original author)
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
idl = IWeaveCrypto.idl
cpp_sources = WeaveCrypto.cpp WeaveCryptoModule.cpp
target = WeaveCrypto # will have .so / .dylib / .dll appended
sdkdir =
destdir = ..
platformdir = $(destdir)/platform/$(platform)
xpidl = $(sdkdir)/bin/xpidl
# FIXME: we don't actually require this for e.g. clean
ifndef sdkdir
$(warning No 'sdkdir' variable given)
$(warning It should point to the location of the Gecko SDK)
$(warning For example: "make sdkdir=/foo/bar/baz")
$(error )
endif
######################################################################
headers = -I$(sdkdir)/include \
-I$(sdkdir)/include/system_wrappers \
-I$(sdkdir)/include/nss \
-I$(sdkdir)/include/xpcom \
-I$(sdkdir)/include/string \
-I$(sdkdir)/include/pipnss \
-I$(sdkdir)/include/nspr \
-I$(sdkdir)/sdk/include
cppflags += -c -pipe -Os \
-fPIC -fno-rtti -fno-exceptions -fno-strict-aliasing \
-fpascal-strings -fno-common -fshort-wchar -pthread \
-Wall -Wconversion -Wpointer-arith -Woverloaded-virtual -Wsynth \
-Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -Wcast-align \
-Wno-long-long \
-include xpcom-config.h $(headers)
libdirs = -L$(sdkdir)/lib -L$(sdkdir)/bin
libs = -lxpcomglue_s -lxpcom \
-lcrmf -lsmime3 -lssl3 -lnss3 -lnssutil3 -lsoftokn3 \
-lplds4 -lplc4 -lnspr4
ldflags += -pthread -pipe -bundle \
-Wl,-executable_path,$(sdkdir)/bin \
-Wl,-dead_strip \
-Wl,-exported_symbol \
-Wl,_NSGetModule \
$(libdirs) $(libs)
######################################################################
# Platform detection
sys := $(shell uname -s)
ifeq ($(sys), Linux)
os = Linux
compiler = gcc3
cxx = c++
so = so
cppflags += -shared
else
ifeq ($(sys), Darwin)
os = Darwin
compiler = gcc3
cxx = c++
so = dylib
cppflags += -dynamiclib
else
ifeq ($(os), MINGW32_NT-5.1)
$(error Sorry, windows is not supported yet)
os = WINNT
compiler = msvc
cxx = c++ # fixme
so = dll
cppflags += -shared
else
$(error Sorry, your os is unknown/unsupported: $(os))
endif
endif
endif
machine := $(shell uname -m)
ifeq ($(machine), i386)
arch = x86
else
ifeq ($(machine), i586)
arch = x86
else
ifeq ($(machine), i686)
arch = x86
else
ifeq ($(machine), ppc) # FIXME: verify
arch = ppc
else
# FIXME: x86_64, ia64, sparc, Alpha
$(error Sorry, your arch is unknown/unsupported: $(machine))
endif
endif
endif
endif
platform = $(os)_$(arch)-$(compiler)
idl_headers = $(idl:.idl=.h)
idl_typelib = $(idl:.idl=.xpt)
cpp_objects = $(cpp_sources:.cpp=.o)
so_target = $(target:=.$(so))
######################################################################
all: build # default target
build: $(so_target) $(idl_typelib)
install: build
mkdir -p $(destdir)/components
mkdir -p $(platformdir)/components
cp $(idl_typelib) $(destdir)/components
cp $(so_target) $(platformdir)/components
clean:
rm -f $(so_target) $(cpp_objects) $(idl_typelib) $(idl_headers)
# rules to build the c headers and .xpt from idl
$(idl_headers): $(idl)
$(xpidl) -m header -I$(sdkdir)/idl $(@:.h=.idl)
$(idl_typelib): $(idl)
$(xpidl) -m typelib -I$(sdkdir)/idl $(@:.xpt=.idl)
# "main" (internal) rules, build sources and link the component
$(cpp_objects): $(cpp_sources)
$(cxx) -o $@ $(cppflags) $(@:.o=.cpp)
$(so_target): $(idl_headers) $(cpp_objects)
$(cxx) -o $@ $(ldflags) $(cpp_objects)
chmod +x $@
# strip $@

View File

@ -0,0 +1,219 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 Weave code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dan Mills <thunder@mozilla.com> (original author)
* Honza Bambas <honzab@allpeers.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "WeaveCrypto.h"
#include "nsStringAPI.h"
#include "nsAutoPtr.h"
#include "plbase64.h"
#include "secerr.h"
#include "secpkcs7.h"
NS_IMPL_ISUPPORTS1(WeaveCrypto, IWeaveCrypto)
WeaveCrypto::WeaveCrypto()
: mAlgorithm(SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC)
{
}
WeaveCrypto::~WeaveCrypto()
{
}
nsresult
WeaveCrypto::EncodeBase64(const nsACString& binary, nsACString& retval)
{
PRUint32 encodedLength = (binary.Length() * 4 + 2) / 3;
nsAutoArrayPtr<char> encoded;
encoded = new char[encodedLength + 2];
NS_ENSURE_TRUE(encoded, NS_ERROR_OUT_OF_MEMORY);
PromiseFlatCString fBinary(binary);
PL_Base64Encode(fBinary.get(), fBinary.Length(), encoded);
retval.Assign(encoded, encodedLength);
return NS_OK;
}
nsresult
WeaveCrypto::DecodeBase64(const nsACString& base64, nsACString& retval)
{
PromiseFlatCString flat(base64);
PRUint32 decodedLength = (flat.Length() * 3) / 4;
nsAutoArrayPtr<char> decoded;
decoded = new char[decodedLength];
NS_ENSURE_TRUE(decoded, NS_ERROR_OUT_OF_MEMORY);
if (!PL_Base64Decode(flat.get(), flat.Length(), decoded))
return NS_ERROR_ILLEGAL_VALUE;
retval.Assign(decoded, decodedLength);
return NS_OK;
}
// static
void
WeaveCrypto::StoreToStringCallback(void *arg, const char *buf, unsigned long len)
{
nsACString* aText = (nsACString*)arg;
aText->Append(buf, len);
}
// static
PK11SymKey *
WeaveCrypto::GetSymmetricKeyCallback(void *arg, SECAlgorithmID *algid)
{
SECItem *pwitem = (SECItem *)arg;
PK11SlotInfo *slot = PK11_GetInternalSlot();
if (!slot)
return nsnull;
PK11SymKey *key = PK11_PBEKeyGen(slot, algid, pwitem, PR_FALSE, nsnull);
PK11_FreeSlot(slot);
if (key)
PK11_SetSymKeyUserData(key, pwitem, nsnull);
return key;
}
// static
PRBool
WeaveCrypto::DecryptionAllowedCallback(SECAlgorithmID *algid, PK11SymKey *key)
{
return PR_TRUE;
}
//////////////////////////////////////////////////////////////////////////////
// nsIPBECipher
NS_IMETHODIMP
WeaveCrypto::GetAlgorithm(PRUint32 *aAlgorithm)
{
*aAlgorithm = mAlgorithm;
return NS_OK;
}
NS_IMETHODIMP
WeaveCrypto::SetAlgorithm(PRUint32 aAlgorithm)
{
mAlgorithm = (SECOidTag)aAlgorithm;
return NS_OK;
}
NS_IMETHODIMP
WeaveCrypto::Encrypt(const nsACString& aPass,
const nsACString& aClearText,
nsACString& aCipherText)
{
nsresult rv = NS_ERROR_FAILURE;
SEC_PKCS7ContentInfo *cinfo = SEC_PKCS7CreateEncryptedData(mAlgorithm, 0, nsnull, nsnull);
NS_ENSURE_TRUE(cinfo, NS_ERROR_FAILURE);
SECAlgorithmID* encalgid = SEC_PKCS7GetEncryptionAlgorithm(cinfo);
if (encalgid)
{
PromiseFlatCString fPass(aPass);
SECItem pwitem = {siBuffer, (unsigned char*)fPass.get(), fPass.Length()};
PK11SymKey *key = GetSymmetricKeyCallback(&pwitem, encalgid);
if (key)
{
nsCString result;
SEC_PKCS7EncoderContext *ecx = SEC_PKCS7EncoderStart(
cinfo, StoreToStringCallback, &result, key);
PK11_FreeSymKey(key);
if (ecx)
{
SECStatus srv;
PromiseFlatCString fClearText(aClearText);
srv = SEC_PKCS7EncoderUpdate(ecx, fClearText.get(), fClearText.Length());
SEC_PKCS7EncoderFinish(ecx, nsnull, nsnull);
if (SECSuccess == srv)
rv = EncodeBase64(result, aCipherText);
}
else
{
NS_WARNING("Could not create PKCS#7 encoder context");
}
}
}
SEC_PKCS7DestroyContentInfo(cinfo);
return rv;
}
NS_IMETHODIMP
WeaveCrypto::Decrypt(const nsACString& aPass,
const nsACString& aCipherText,
nsACString& aClearText)
{
nsresult rv;
nsCString fCipherText;
rv = DecodeBase64(aCipherText, fCipherText);
NS_ENSURE_SUCCESS(rv, rv);
aClearText.Truncate();
PromiseFlatCString fPass(aPass);
SECItem pwitem = {siBuffer, (unsigned char*)fPass.get(), fPass.Length()};
SEC_PKCS7DecoderContext *dcx = SEC_PKCS7DecoderStart(
StoreToStringCallback, &aClearText, nsnull, nsnull,
GetSymmetricKeyCallback, &pwitem,
DecryptionAllowedCallback);
NS_ENSURE_TRUE(dcx, NS_ERROR_FAILURE);
SECStatus srv = SEC_PKCS7DecoderUpdate(dcx, fCipherText.get(), fCipherText.Length());
rv = (SECSuccess == srv) ? NS_OK : NS_ERROR_FAILURE;
SEC_PKCS7ContentInfo *cinfo = SEC_PKCS7DecoderFinish(dcx);
if (cinfo)
SEC_PKCS7DestroyContentInfo(cinfo);
return rv;
}

View File

@ -0,0 +1,72 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 WeaveCrypto code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dan Mills <thunder@mozilla.com> (original author)
* Honza Bambas <honzab@allpeers.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef WeaveCrypto_h_
#define WeaveCrypto_h_
#include "IWeaveCrypto.h"
#include "pk11pub.h"
#define WEAVE_CRYPTO_CONTRACTID "@labs.mozilla.com/Weave/Crypto;1"
#define WEAVE_CRYPTO_CLASSNAME "A Simple XPCOM Sample"
#define WEAVE_CRYPTO_CID { 0xd3b0f750, 0xc976, 0x46d0, \
{ 0xbe, 0x20, 0x96, 0xb2, 0x4f, 0x46, 0x84, 0xbc } }
class WeaveCrypto : public IWeaveCrypto
{
public:
WeaveCrypto();
NS_DECL_ISUPPORTS
NS_DECL_IWEAVECRYPTO
private:
~WeaveCrypto();
SECOidTag mAlgorithm;
nsresult DecodeBase64(const nsACString& base64, nsACString& retval);
nsresult EncodeBase64(const nsACString& binary, nsACString& retval);
static void WeaveCrypto::StoreToStringCallback(void *arg, const char *buf, unsigned long len);
static PK11SymKey *GetSymmetricKeyCallback(void *arg, SECAlgorithmID *algid);
static PRBool DecryptionAllowedCallback(SECAlgorithmID *algid, PK11SymKey *key);
};
#endif // WeaveCrypto_h_

View File

@ -0,0 +1,54 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 Weave code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dan Mills <thunder@mozilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIGenericFactory.h"
#include "WeaveCrypto.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(WeaveCrypto)
static nsModuleComponentInfo components[] =
{
{
WEAVE_CRYPTO_CLASSNAME,
WEAVE_CRYPTO_CID,
WEAVE_CRYPTO_CONTRACTID,
WeaveCryptoConstructor,
}
};
NS_IMPL_NSGETMODULE("WeaveCryptoModule", components)