From d9a01b05bd37f1d7f1ab6f7e0dbac7d7c3f5df9c Mon Sep 17 00:00:00 2001 From: "dougt%netscape.com" Date: Tue, 14 Mar 2000 04:41:48 +0000 Subject: [PATCH] landing psm-glue code. --- extensions/psm-glue/makefile.win | 27 + extensions/psm-glue/public/Makefile.in | 40 + extensions/psm-glue/public/makefile.win | 47 ++ .../psm-glue/public/nsIPSMComponent.idl | 70 ++ .../psm-glue/public/nsIPSMUIHandler.idl | 36 + .../psm-glue/public/nsISSLSocketProvider.idl | 35 + .../psm-glue/public/nsISecureBrowserUI.idl | 43 + extensions/psm-glue/res/Makefile.in | 32 + extensions/psm-glue/res/content/MANIFEST | 0 extensions/psm-glue/res/content/Makefile.in | 37 + .../res/content/NavSecurityOverlay.xul | 14 + .../psm-glue/res/content/NavSecurityUI.js | 44 ++ extensions/psm-glue/res/content/makefile.win | 33 + extensions/psm-glue/res/makefile.win | 27 + extensions/psm-glue/res/skin/MANIFEST | 3 + extensions/psm-glue/res/skin/Makefile.in | 39 + .../psm-glue/res/skin/NavSecurityOverlay.css | 0 extensions/psm-glue/res/skin/broken.gif | Bin 0 -> 62 bytes extensions/psm-glue/res/skin/lock.gif | 0 extensions/psm-glue/res/skin/makefile.win | 40 + extensions/psm-glue/res/skin/unlock.gif | Bin 0 -> 38 bytes extensions/psm-glue/src/Makefile.in | 54 ++ extensions/psm-glue/src/makefile.win | 67 ++ extensions/psm-glue/src/nsPSMComponent.cpp | 739 ++++++++++++++++++ extensions/psm-glue/src/nsPSMComponent.h | 52 ++ extensions/psm-glue/src/nsPSMModule.cpp | 74 ++ extensions/psm-glue/src/nsPSMMutex.c | 74 ++ extensions/psm-glue/src/nsPSMMutex.h | 0 extensions/psm-glue/src/nsPSMShimLayer.c | 269 +++++++ extensions/psm-glue/src/nsPSMShimLayer.h | 67 ++ extensions/psm-glue/src/nsPSMUICallbacks.cpp | 305 ++++++++ extensions/psm-glue/src/nsPSMUICallbacks.h | 59 ++ extensions/psm-glue/src/nsSSLIOLayer.cpp | 315 ++++++++ extensions/psm-glue/src/nsSSLIOLayer.h | 37 + .../psm-glue/src/nsSSLSocketProvider.cpp | 79 ++ extensions/psm-glue/src/nsSSLSocketProvider.h | 0 .../psm-glue/src/nsSecureBrowserUIImpl.cpp | 481 ++++++++++++ .../psm-glue/src/nsSecureBrowserUIImpl.h | 94 +++ 38 files changed, 3333 insertions(+) create mode 100644 extensions/psm-glue/makefile.win create mode 100644 extensions/psm-glue/public/Makefile.in create mode 100644 extensions/psm-glue/public/makefile.win create mode 100644 extensions/psm-glue/public/nsIPSMComponent.idl create mode 100644 extensions/psm-glue/public/nsIPSMUIHandler.idl create mode 100644 extensions/psm-glue/public/nsISSLSocketProvider.idl create mode 100644 extensions/psm-glue/public/nsISecureBrowserUI.idl create mode 100644 extensions/psm-glue/res/Makefile.in create mode 100644 extensions/psm-glue/res/content/MANIFEST create mode 100644 extensions/psm-glue/res/content/Makefile.in create mode 100644 extensions/psm-glue/res/content/NavSecurityOverlay.xul create mode 100644 extensions/psm-glue/res/content/NavSecurityUI.js create mode 100644 extensions/psm-glue/res/content/makefile.win create mode 100644 extensions/psm-glue/res/makefile.win create mode 100644 extensions/psm-glue/res/skin/MANIFEST create mode 100644 extensions/psm-glue/res/skin/Makefile.in create mode 100644 extensions/psm-glue/res/skin/NavSecurityOverlay.css create mode 100644 extensions/psm-glue/res/skin/broken.gif create mode 100644 extensions/psm-glue/res/skin/lock.gif create mode 100644 extensions/psm-glue/res/skin/makefile.win create mode 100644 extensions/psm-glue/res/skin/unlock.gif create mode 100644 extensions/psm-glue/src/Makefile.in create mode 100644 extensions/psm-glue/src/makefile.win create mode 100644 extensions/psm-glue/src/nsPSMComponent.cpp create mode 100644 extensions/psm-glue/src/nsPSMComponent.h create mode 100644 extensions/psm-glue/src/nsPSMModule.cpp create mode 100644 extensions/psm-glue/src/nsPSMMutex.c create mode 100644 extensions/psm-glue/src/nsPSMMutex.h create mode 100644 extensions/psm-glue/src/nsPSMShimLayer.c create mode 100644 extensions/psm-glue/src/nsPSMShimLayer.h create mode 100644 extensions/psm-glue/src/nsPSMUICallbacks.cpp create mode 100644 extensions/psm-glue/src/nsPSMUICallbacks.h create mode 100644 extensions/psm-glue/src/nsSSLIOLayer.cpp create mode 100644 extensions/psm-glue/src/nsSSLIOLayer.h create mode 100644 extensions/psm-glue/src/nsSSLSocketProvider.cpp create mode 100644 extensions/psm-glue/src/nsSSLSocketProvider.h create mode 100644 extensions/psm-glue/src/nsSecureBrowserUIImpl.cpp create mode 100644 extensions/psm-glue/src/nsSecureBrowserUIImpl.h diff --git a/extensions/psm-glue/makefile.win b/extensions/psm-glue/makefile.win new file mode 100644 index 00000000000..04ed6ce1583 --- /dev/null +++ b/extensions/psm-glue/makefile.win @@ -0,0 +1,27 @@ +#!nmake +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): + +DEPTH=../.. +include <$(DEPTH)/config/config.mak> + +DIRS = public src res + +include <$(DEPTH)\config\rules.mak> diff --git a/extensions/psm-glue/public/Makefile.in b/extensions/psm-glue/public/Makefile.in new file mode 100644 index 00000000000..7527228a238 --- /dev/null +++ b/extensions/psm-glue/public/Makefile.in @@ -0,0 +1,40 @@ +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# Hubbie Shaw +# Doug Turner +# + +MODULE = psmglue + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +XPIDLSRCS = \ + nsIPSMComponent.idl \ + nsIPSMUIHandler.idl \ + nsISecureBrowserUI.idl \ + nsISSLSocketProvider.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/extensions/psm-glue/public/makefile.win b/extensions/psm-glue/public/makefile.win new file mode 100644 index 00000000000..df0e9744e19 --- /dev/null +++ b/extensions/psm-glue/public/makefile.win @@ -0,0 +1,47 @@ +#!nmake +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# Hubbie Shaw +# Doug Turner +# + +MODULE = psmglue + +DEPTH=..\..\.. +IGNORE_MANIFEST=1 + +DLLNAME = psmglue +PDBFILE = $(DLLNAME).pdb +MAPFILE = $(DLLNAME).map +DLL = .\$(OBJDIR)\$(DLLNAME).dll +MAKE_OBJ_TYPE = DLL + +include <$(DEPTH)/config/config.mak> + +XPIDL_INCLUDES=-I$(DEPTH)\..\mozilla\dist\idl + +XPIDLSRCS= \ + .\nsIPSMComponent.idl \ + .\nsIPSMUIHandler.idl \ + .\nsISecureBrowserUI.idl \ + .\nsISSLSocketProvider.idl \ + $(NULL) + +include <$(DEPTH)\config\rules.mak> \ No newline at end of file diff --git a/extensions/psm-glue/public/nsIPSMComponent.idl b/extensions/psm-glue/public/nsIPSMComponent.idl new file mode 100644 index 00000000000..0ca27b8cc01 --- /dev/null +++ b/extensions/psm-glue/public/nsIPSMComponent.idl @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsISupports.idl" +//#include "nsIPrincipal.idl" +interface nsIPrincipal; + +%{C++ +#include "cmtcmn.h" +%} + +[ptr] native nsCMTControlStar(CMT_CONTROL); + +[scriptable, uuid(9e482670-5412-11d3-bbc8-0000861d1237)] +interface nsIPSMComponent : nsISupports +{ + + [noscript] nsCMTControlStar GetControlConnection( ); + void DisplaySecurityAdvisor(); + + /* Secure Hashing functions */ + void hashBegin(in unsigned long alg, out unsigned long id); + void hashUpdate(in unsigned long id, in string buf, in unsigned long buflen); + void hashEnd(in unsigned long id, out string hash, out unsigned long hashlen, + in unsigned long maxLen); + + /* Signature Verification functions */ + void verifyRSABegin(out unsigned long id); + void verifyRSAUpdate(in unsigned long id, in string buf, + in unsigned long buflen); + void verifyRSAEnd(in unsigned long id, in string plaintext, + in unsigned long plaintextLen, + in boolean keepCert, out nsIPrincipal principal); +}; + +%{C++ +#define PSM_COMPONENT_PROGID "component://netscape/psm" +#define PSM_COMPONENT_CLASSNAME "Mozilla PSM Component" + +/* Hash Algorithms (based on cmtcmn.h) */ +#define PSM_HASH_MD2 1 +#define PSM_HASH_MD2_LENGTH 16 +#define PSM_HASH_MD5 2 +#define PSM_HASH_MD5_LENGTH 16 +#define PSM_HASH_SHA1 3 +#define PSM_HASH_SHA1_LENGTH 20 +#define PSM_HASH_MAX_LENGTH PSM_HASH_SHA1_LENGTH +%} + diff --git a/extensions/psm-glue/public/nsIPSMUIHandler.idl b/extensions/psm-glue/public/nsIPSMUIHandler.idl new file mode 100644 index 00000000000..0404a26ea8a --- /dev/null +++ b/extensions/psm-glue/public/nsIPSMUIHandler.idl @@ -0,0 +1,36 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 the Mozilla browser. + * + * The Initial Developer of the Original Code is Netscape + * Communications, Inc. Portions created by Netscape are + * Copyright (C) 1999, Mozilla. All Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner + */ + +#include "nsISupports.idl" + +[scriptable, uuid(d92be9b0-601b-11d3-8c4a-000064657374)] +interface nsIPSMUIHandler : nsISupports +{ + void DisplayURI(in long width, in long height, in string urlStr); + void PromptForFile(in string prompt, in string fileRegEx, in boolean shouldFileExist, out string outFile); +}; + +%{C++ +#define PSM_UI_HANLDER_PROGID "component://netscape/psm/ui" +#define PSM_UI_HANLDER_CLASSNAME "Mozilla PSM UI Handler" +%} diff --git a/extensions/psm-glue/public/nsISSLSocketProvider.idl b/extensions/psm-glue/public/nsISSLSocketProvider.idl new file mode 100644 index 00000000000..f5bbd1a70db --- /dev/null +++ b/extensions/psm-glue/public/nsISSLSocketProvider.idl @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsISocketProvider.idl" + +[noscript, uuid(856a93d0-5415-11d3-bbc8-0000861d1237)] +interface nsISSLSocketProvider : nsISocketProvider { +}; + +%{C++ +#define NS_ISSLSOCKETPROVIDER_PROGID NS_NETWORK_SOCKET_PROGID_PREFIX "ssl" +#define NS_ISSLSOCKETPROVIDER_CLASSNAME "Mozilla SSL Socket Provider Component" + +%} diff --git a/extensions/psm-glue/public/nsISecureBrowserUI.idl b/extensions/psm-glue/public/nsISecureBrowserUI.idl new file mode 100644 index 00000000000..5feed10fc97 --- /dev/null +++ b/extensions/psm-glue/public/nsISecureBrowserUI.idl @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsISupports.idl" + +interface nsIDOMWindow; +interface nsIDOMElement; + +[scriptable, uuid(081e31e0-a144-11d3-8c7c-00609792278c)] +interface nsSecureBrowserUI : nsISupports +{ + void init(in nsIDOMWindow window, in nsIDOMElement button); +}; + +%{C++ +#define NS_SECURE_BROWSER_UI_PROGID "component://netscape/secure_browser_ui" +#define NS_SECURE_BROWSER_UI_CLASSNAME "Mozilla Secure Browser UI Handler" + +#define NS_SECURE_BROWSER_UI_CID \ +{ 0x10fe7ea0, 0xa10a, 0x11d3, {0x8c, 0x7c, 0x00, 0x60, 0x97, 0x92, 0x27, 0x8c}} + +%} diff --git a/extensions/psm-glue/res/Makefile.in b/extensions/psm-glue/res/Makefile.in new file mode 100644 index 00000000000..083709f2a06 --- /dev/null +++ b/extensions/psm-glue/res/Makefile.in @@ -0,0 +1,32 @@ +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +DIRS = content skin + +include $(topsrcdir)/config/rules.mk + diff --git a/extensions/psm-glue/res/content/MANIFEST b/extensions/psm-glue/res/content/MANIFEST new file mode 100644 index 00000000000..e69de29bb2d diff --git a/extensions/psm-glue/res/content/Makefile.in b/extensions/psm-glue/res/content/Makefile.in new file mode 100644 index 00000000000..ea5f549fa7f --- /dev/null +++ b/extensions/psm-glue/res/content/Makefile.in @@ -0,0 +1,37 @@ +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk +include $(topsrcdir)/config/rules.mk + +CONTENT_DIR = $(DIST)/bin/chrome/navigator/content/default/ +EXPORT_CONTENT = \ + $(srcdir)/NavSecurityOverlay.xul \ + $(srcdir)/NavSecurityUI.js \ + $(NULL) + +install:: + $(INSTALL) $(EXPORT_CONTENT) $(CONTENT_DIR) diff --git a/extensions/psm-glue/res/content/NavSecurityOverlay.xul b/extensions/psm-glue/res/content/NavSecurityOverlay.xul new file mode 100644 index 00000000000..fdcc741608e --- /dev/null +++ b/extensions/psm-glue/res/content/NavSecurityOverlay.xul @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/extensions/psm-glue/res/content/NavSecurityUI.js b/extensions/psm-glue/res/content/NavSecurityUI.js new file mode 100644 index 00000000000..478b7cdc6b7 --- /dev/null +++ b/extensions/psm-glue/res/content/NavSecurityUI.js @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): +*/ + +window.addEventListener("load", SetSecurityButton, false); + +function SetSecurityButton() +{ + dump("in SetSecurityButton\n"); + + var ui = Components.classes["component://netscape/secure_browser_ui"].getService(); + ui = ui.QueryInterface(Components.interfaces.nsSecureBrowserUI); + + var button = document.getElementById('security-button'); + if (button && window.content) + ui.init(window.content, button); +} + +function displayPageInfo() +{ + var psm = Components.classes["component://netscape/psm"].getService(); + psm = psm.QueryInterface(Components.interfaces.nsIPSMComponent); + psm.DisplaySecurityAdvisor( null ); +} + + diff --git a/extensions/psm-glue/res/content/makefile.win b/extensions/psm-glue/res/content/makefile.win new file mode 100644 index 00000000000..6f64bdb14e4 --- /dev/null +++ b/extensions/psm-glue/res/content/makefile.win @@ -0,0 +1,33 @@ +#!nmake +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): + +DEPTH=..\..\..\.. +include <$(DEPTH)/config/config.mak> +include <$(DEPTH)\config\rules.mak> + +install:: $(DLL) + $(MAKE_INSTALL) NavSecurityOverlay.xul $(DIST)\bin\chrome\navigator\content\default + $(MAKE_INSTALL) NavSecurityUI.js $(DIST)\bin\chrome\navigator\content\default + +clobber:: + rm $(DIST)\bin\chrome\navigator\content\default\NavSecurityOverlay.xul + rm $(DIST)\bin\chrome\navigator\content\default\NavSecurityUI.js + \ No newline at end of file diff --git a/extensions/psm-glue/res/makefile.win b/extensions/psm-glue/res/makefile.win new file mode 100644 index 00000000000..0b3aee2ee26 --- /dev/null +++ b/extensions/psm-glue/res/makefile.win @@ -0,0 +1,27 @@ +#!nmake +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): + +DEPTH=../../.. +include <$(DEPTH)/config/config.mak> + +DIRS = skin content + +include <$(DEPTH)\config\rules.mak> diff --git a/extensions/psm-glue/res/skin/MANIFEST b/extensions/psm-glue/res/skin/MANIFEST new file mode 100644 index 00000000000..f662adddc0b --- /dev/null +++ b/extensions/psm-glue/res/skin/MANIFEST @@ -0,0 +1,3 @@ +lock.gif +unlock.gif +broken.gif \ No newline at end of file diff --git a/extensions/psm-glue/res/skin/Makefile.in b/extensions/psm-glue/res/skin/Makefile.in new file mode 100644 index 00000000000..dc6e73de257 --- /dev/null +++ b/extensions/psm-glue/res/skin/Makefile.in @@ -0,0 +1,39 @@ +# +# The contents of this file are subject to the Netscape 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/NPL/ +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk +include $(topsrcdir)/config/rules.mk + +SKIN_DIR = $(DIST)/bin/chrome/navigator/skin/default/ +EXPORT_SKINS = \ + $(srcdir)/broken.gif \ + $(srcdir)/lock.gif \ + $(srcdir)/unlock.gif \ + $(srcdir)/NavSecurityOverlay.css \ + $(NULL) + +install:: + $(INSTALL) $(EXPORT_SKINS) $(SKIN_DIR) diff --git a/extensions/psm-glue/res/skin/NavSecurityOverlay.css b/extensions/psm-glue/res/skin/NavSecurityOverlay.css new file mode 100644 index 00000000000..e69de29bb2d diff --git a/extensions/psm-glue/res/skin/broken.gif b/extensions/psm-glue/res/skin/broken.gif new file mode 100644 index 0000000000000000000000000000000000000000..c80ccd964922c0204c68116cecaa634a03194e19 GIT binary patch literal 62 zcmZ?wbhEHb + +include <$(DEPTH)\config\rules.mak> + +install:: + $(MAKE_INSTALL) broken.gif $(DIST)\bin\chrome\navigator\skin\default + $(MAKE_INSTALL) unlock.gif $(DIST)\bin\chrome\navigator\skin\default + $(MAKE_INSTALL) NavSecurityOverlay.css $(DIST)\bin\chrome\navigator\skin\default + +clobber:: + rm $(DIST)\bin\chrome\navigator\skin\default\broken.gif + rm $(DIST)\bin\chrome\navigator\skin\default\lock.gif + rm $(DIST)\bin\chrome\navigator\skin\default\unlock.gif + rm $(DIST)\bin\chrome\navigator\skin\default\NavSecurityOverlay.css + + + diff --git a/extensions/psm-glue/res/skin/unlock.gif b/extensions/psm-glue/res/skin/unlock.gif new file mode 100644 index 0000000000000000000000000000000000000000..48e9d1b7a37bb06273184a5ac30b55a05a58c8fe GIT binary patch literal 38 ncmZ?wbhEHb +# + +MODULE = psmglue + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = psmglue + +IS_COMPONENT = 1 +LIBRARY_NAME = psmglue + + +CPPSRCS = \ + nsPSMComponent.cpp \ + nsPSMModule.cpp \ + nsPSMUICallbacks.cpp \ + $(NULL) + +CSRCS = \ + nsPSMMutex.c \ + nsPSMShimLayer.c \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + +INCLUDES += \ + -I$(srcdir) \ + $(NULL) \ No newline at end of file diff --git a/extensions/psm-glue/src/makefile.win b/extensions/psm-glue/src/makefile.win new file mode 100644 index 00000000000..80681343ad3 --- /dev/null +++ b/extensions/psm-glue/src/makefile.win @@ -0,0 +1,67 @@ +#!nmake +# +# 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 mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# Hubbie Shaw +# Doug Turner +# + +MODULE = psmglue + +DEPTH=..\..\.. +IGNORE_MANIFEST=1 + +DLLNAME = psmglue +PDBFILE = $(DLLNAME).pdb +MAPFILE = $(DLLNAME).map +DLL = .\$(OBJDIR)\$(DLLNAME).dll +MAKE_OBJ_TYPE = DLL + +include <$(DEPTH)/config/config.mak> + +LINCS = $(LINCS) \ + -I$(PUBLIC) \ + -I$(PUBLIC)/security \ + $(NULL) + +LLIBS = \ + $(LIBNSPR) \ + $(DIST)\lib\neckobase_s.lib \ + $(DIST)\lib\xpcom.lib \ + $(DIST)\lib\js3250.lib \ + $(DIST)\lib\cmt.lib \ + $(DIST)\lib\protocol.lib \ + $(DIST)\lib\mozreg.lib \ + $(NULL) + +OBJS = \ + .\$(OBJDIR)\nsPSMMutex.obj \ + .\$(OBJDIR)\nsPSMShimLayer.obj \ + .\$(OBJDIR)\nsPSMComponent.obj \ + .\$(OBJDIR)\nsPSMUICallbacks.obj \ + .\$(OBJDIR)\nsPSMModule.obj \ + .\$(OBJDIR)\nsSecureBrowserUIImpl.obj \ + .\$(OBJDIR)\nsSSLIOLayer.obj \ + .\$(OBJDIR)\nsSSLSocketProvider.obj \ + $(NULL) + +include <$(DEPTH)\config\rules.mak> + +install:: $(DLL) + $(MAKE_INSTALL) .\$(OBJDIR)\$(DLLNAME).dll $(DIST)\bin\components diff --git a/extensions/psm-glue/src/nsPSMComponent.cpp b/extensions/psm-glue/src/nsPSMComponent.cpp new file mode 100644 index 00000000000..909bc06ea06 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMComponent.cpp @@ -0,0 +1,739 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsProxiedService.h" +#include "nsPSMUICallbacks.h" +#include "VerReg.h" + +#include "nspr.h" +#include "nsPSMComponent.h" + +#include "nsCRT.h" + +#include "nsIPref.h" +#include "nsIProfile.h" +#include "nsILocalFile.h" +#ifdef XP_MAC +#include "nsILocalFileMac.h" +#endif +#include "nsSpecialSystemDirectory.h" + +#include "nsPSMMutex.h" +#include "nsPSMShimLayer.h" +#include "nsPSMUICallbacks.h" + +#include "nsISecureBrowserUI.h" +#include "nsIDocumentLoaderObserver.h" +#include "nsIScriptSecurityManager.h" + +#define PSM_VERSION_REG_KEY "/Netscape/Personal Security Manager" + +#ifdef WIN32 +#define PSM_FILE_NAME "psm.exe" +#elif XP_UNIX +#define PSM_FILE_NAME "start-psm" +#define PSM_FILE_LOCATION "/opt/netscape/security/start-psm" +#else +#define PSM_FILE_NAME "psm" +#endif + + +static NS_DEFINE_CID(kProfileCID, NS_PROFILE_CID); + + +nsPSMComponent* nsPSMComponent::mInstance = nsnull; + +nsPSMComponent::nsPSMComponent() +{ + NS_INIT_REFCNT(); + mControl = nsnull; +} + +nsPSMComponent::~nsPSMComponent() +{ + if (mControl) + { + CMT_CloseControlConnection(mControl); + mControl = nsnull; + } +} + + +NS_IMETHODIMP +nsPSMComponent::CreatePSMComponent(nsISupports* aOuter, REFNSIID aIID, void **aResult) +{ + if (!aResult) { + return NS_ERROR_INVALID_POINTER; + } + if (aOuter) { + *aResult = nsnull; + return NS_ERROR_NO_AGGREGATION; + } + + if (mInstance == nsnull) + { + mInstance = new nsPSMComponent(); + } + + if (mInstance == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = mInstance->QueryInterface(aIID, aResult); + if (NS_FAILED(rv)) + { + *aResult = nsnull; + } + return rv; +} + +/* nsISupports Implementation for the class */ +NS_IMPL_ISUPPORTS1 (nsPSMComponent, nsIPSMComponent); + +#define INIT_NUM_PREFS 100 +/* preference types */ +#define STRING_PREF 0 +#define BOOL_PREF 1 +#define INT_PREF 2 + + +/* resizable list struct that contains pref items */ +typedef struct CMSetPrefList { + int n; /* number of filled items */ + int capacity; /* allocated memory */ + CMTSetPrefElement* list; /* actual list */ +} CMSetPrefList; + +static void get_pack_bool_pref(nsIPref *prefManager, char* key, CMTSetPrefElement* list, int* n) +{ + PRBool boolpref; + + list[*n].key = nsCRT::strdup(key); + list[*n].type = BOOL_PREF; + + if ((prefManager->GetBoolPref(key, &boolpref) != 0) || boolpref) + { + list[*n].value = nsCRT::strdup("true"); + } + else + { + list[*n].value = nsCRT::strdup("false"); + } + + (*n)++; /* increment the counter after done packing */ + + return; +} +static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); + +static void SaveAllPrefs(int number, CMTSetPrefElement* list) +{ + nsCOMPtr prefManager; + + nsresult res = nsServiceManager::GetService(kPrefCID, + nsIPref::GetIID(), + getter_AddRefs(prefManager)); + + if (NS_FAILED(res) || !prefManager) + { + return; + } + + + int i; + int intval; + + for (i = 0; i < number; i++) + { + if (list[i].key == nsnull) + { + /* misconfigured item: next */ + continue; + } + + switch (list[i].type) + { + case 0: /* string type */ + prefManager->SetCharPref(list[i].key, list[i].value); + break; + case 1: /* boolean type */ + if (strcmp(list[i].value, "true") == 0) { + prefManager->SetBoolPref(list[i].key, (PRBool)1); + } + else if (strcmp(list[i].value, "false") == 0) { + prefManager->SetBoolPref(list[i].key, (PRBool)0); + } + break; + case 2: + intval = atoi(list[i].value); + prefManager->SetIntPref(list[i].key, intval); + break; + default: + break; + } + } + + return; +} + +nsresult +nsPSMComponent::PassAllPrefs() +{ + int i; + nsresult rv = NS_ERROR_FAILURE; + char* strpref = NULL; + int intpref; + PRBool boolpref; + CMSetPrefList prefs = {0}; + CMTSetPrefElement* list = NULL; + + char* pickAuto = "Select Automatically"; + char* alwaysAsk = "Ask Every Time"; + + nsCOMPtr prefManager; + + nsresult res = nsServiceManager::GetService(kPrefCID, + nsIPref::GetIID(), + getter_AddRefs(prefManager)); + + if (NS_OK != res) + { + return NS_ERROR_FAILURE; + } + + /* allocate memory for list */ + prefs.n = 0; /* counter */ + prefs.capacity = INIT_NUM_PREFS; + prefs.list = (CMTSetPrefElement*) new char[(INIT_NUM_PREFS * sizeof(CMTSetPrefElement))]; + + if (prefs.list == NULL) + { + return rv; + } + + /* shorthand */ + list = prefs.list; + + /* get preferences */ + get_pack_bool_pref(prefManager, "security.enable_ssl2", (CMTSetPrefElement*)list, &(prefs.n)); + get_pack_bool_pref(prefManager, "security.enable_ssl3", (CMTSetPrefElement*)list, &(prefs.n)); + + /* this pref is a boolean pref in nature but a string pref for + * historical reason + */ + + list[prefs.n].key = nsCRT::strdup("security.default_personal_cert"); + list[prefs.n].type = STRING_PREF; + + if ((prefManager->CopyCharPref(list[prefs.n].key, &strpref) == 0) && (strcmp(strpref, pickAuto) == 0)) + { + list[prefs.n].value = nsCRT::strdup(pickAuto); + } + else + { + /* although one could choose a specific cert for client auth in + * Nova, that mode is deprecated with PSM and mapped to ASK + */ + list[prefs.n].value = nsCRT::strdup(alwaysAsk); + } + + prefs.n++; + if (strpref != NULL) + { + nsCRT::free(strpref); + } + + list[prefs.n].key = nsCRT::strdup("security.default_mail_cert"); + list[prefs.n].type = STRING_PREF; + if (prefManager->CopyCharPref(list[prefs.n].key, &list[prefs.n].value) != 0) + { + list[prefs.n].value = NULL; + } + prefs.n++; + + list[prefs.n].key = nsCRT::strdup("security.ask_for_password"); + list[prefs.n].type = INT_PREF; + if (prefManager->GetIntPref(list[prefs.n].key, &intpref) != 0) + { + intpref = 2; /* default */ + } + + list[prefs.n].value = PR_smprintf("%d", intpref); + prefs.n++; + + list[prefs.n].key = nsCRT::strdup("security.password_lifetime"); + list[prefs.n].type = INT_PREF; + if (prefManager->GetIntPref(list[prefs.n].key, &intpref) != 0) + { + intpref = 480; /* default */ + } + + list[prefs.n].value = PR_smprintf("%d", intpref); + prefs.n++; + + /* OCSP preferences */ + /* XXX since these are the new ones added by PSM, we will be more + * error-tolerant in fetching them + */ + if (prefManager->GetBoolPref("security.OCSP.enabled", &boolpref) == 0) + { + if (boolpref) + { + list[prefs.n].value = nsCRT::strdup("true"); + } + else + { + list[prefs.n].value = nsCRT::strdup("false"); + } + list[prefs.n].key = nsCRT::strdup("security.OCSP.enabled"); + list[prefs.n].type = BOOL_PREF; + prefs.n++; + } + + if (prefManager->GetBoolPref("security.OCSP.useDefaultResponder", &boolpref) == 0) + { + if (boolpref) + { + list[prefs.n].value = nsCRT::strdup("true"); + } + else + { + list[prefs.n].value = nsCRT::strdup("false"); + } + list[prefs.n].key = nsCRT::strdup("security.OCSP.useDefaultResponder"); + list[prefs.n].type = BOOL_PREF; + prefs.n++; + } + + if (prefManager->CopyCharPref("security.OCSP.URL", &strpref) == 0) + { + list[prefs.n].value = strpref; + list[prefs.n].key = nsCRT::strdup("security.OCSP.URL"); + list[prefs.n].type = STRING_PREF; + prefs.n++; + } + + if (prefManager->CopyCharPref("security.OCSP.signingCA", &strpref) == 0) + { + list[prefs.n].value = strpref; + list[prefs.n].key = nsCRT::strdup("security.OCSP.signingCA"); + list[prefs.n].type = STRING_PREF; + prefs.n++; + } + + /* now application-specific preferences */ + /* get navigator preferences */ + get_pack_bool_pref(prefManager, "security.warn_entering_secure", (CMTSetPrefElement*)list, &prefs.n); + get_pack_bool_pref(prefManager, "security.warn_leaving_secure", (CMTSetPrefElement*)list, &prefs.n); + get_pack_bool_pref(prefManager, "security.warn_viewing_mixed", (CMTSetPrefElement*)list, &prefs.n); + get_pack_bool_pref(prefManager, "security.warn_submit_insecure", (CMTSetPrefElement*)list, &prefs.n); + + // Add any other prefs here such as ldap or mail/news. + + CMT_SetSavePrefsCallback(mControl, (savePrefsCallback_fn)SaveAllPrefs); + + if (CMT_PassAllPrefs(mControl, prefs.n, (CMTSetPrefElement*)prefs.list) != CMTSuccess) + { + goto loser; + } + + rv = NS_OK; /* success */ +loser: + /* clean out memory for prefs */ + for (i = 0; i < prefs.n; i++) + { + if (prefs.list[i].key != NULL) + { + nsCRT::free(prefs.list[i].key); + } + + if (prefs.list[i].value != NULL) + { + nsCRT::free(prefs.list[i].value); + } + } + + if (prefs.list != NULL) + { + delete(prefs.list); + } + return rv; +} + +NS_IMETHODIMP +nsPSMComponent::GetControlConnection( CMT_CONTROL * *_retval ) +{ + nsresult rv; + *_retval = nsnull; + if (mControl) + { + *_retval = mControl; + return NS_OK; + } + else /* initialize mutex, sock table, etc. */ + { + + if (nsPSMMutexInit() != PR_SUCCESS) + return NS_ERROR_FAILURE; + + mControl = CMT_ControlConnect(&nsPSMMutexTbl, &nsPSMShimTbl); + + if (mControl == nsnull) + { + //Try to find it. + int err; + char filepath[MAXREGPATHLEN]; + + err = VR_GetPath(PSM_VERSION_REG_KEY, sizeof(filepath), filepath); + if ( err == REGERR_OK ) + { + nsFileSpec psmSpec(filepath); + psmSpec += PSM_FILE_NAME; + + if (psmSpec.Exists()) + { + mControl = CMT_EstablishControlConnection((char *)psmSpec.GetNativePathCString(), &nsPSMShimTbl, &nsPSMMutexTbl); + } + } + } + +#ifndef XP_MAC + if (mControl == nsnull) + { + nsSpecialSystemDirectory sysDir(nsSpecialSystemDirectory::OS_CurrentProcessDirectory); + nsFileSpec spec = sysDir; + + spec += "psm/"; + spec += PSM_FILE_NAME; + + if (spec.Exists()) + { + mControl = CMT_EstablishControlConnection((char *)spec.GetNativePathCString(), &nsPSMShimTbl, &nsPSMMutexTbl); + } + } +#else + if (mControl == nsnull) + { + // Attempt to locate "Personal Security Manager" in "Essential Files". + nsCOMPtr aPSMApp = do_CreateInstance(NS_LOCAL_FILE_PROGID, &rv); + if (NS_SUCCEEDED(rv)) + { + nsCOMPtr psmAppMacFile = do_QueryInterface(aPSMApp, &rv); + if (NS_SUCCEEDED(rv)) + { + rv = psmAppMacFile->InitFindingAppByCreatorCode('nPSM'); + if (NS_SUCCEEDED(rv)) + { + rv = psmAppMacFile->LaunchAppWithDoc(nsnull, PR_TRUE); + if (NS_SUCCEEDED(rv)) + { + const PRUint32 kMaxWaitTicks = 180; // max 3 seconds + PRUint32 endTicks = ::TickCount() + kMaxWaitTicks; + + do + { + EventRecord theEvent; + WaitNextEvent(0, &theEvent, 5, NULL); + mControl = CMT_ControlConnect(&nsPSMMutexTbl, &nsPSMShimTbl); + } while (!mControl && (::TickCount() < endTicks)); + + } + } + } + } + NS_ASSERTION(NS_SUCCEEDED(rv), "Launching Personal Security Manager failed"); + } + +#endif + +#ifdef XP_UNIX + if (mControl == nsnull) + { + nsFileSpec psmSpec(PSM_FILE_LOCATION); + if (psmSpec.Exists()) + { + mControl = CMT_EstablishControlConnection(PSM_FILE_LOCATION, &nsPSMShimTbl, &nsPSMMutexTbl); + } + } +#endif + + if (mControl == nsnull) + { + char* filePath = nsnull; + + NS_WITH_PROXIED_SERVICE(nsIPSMUIHandler, handler, nsPSMUIHandlerImpl::GetCID(), NS_UI_THREAD_EVENTQ, &rv); + if(NS_SUCCEEDED(rv)) + handler->PromptForFile("Please find the Personal Security Manager application", PSM_FILE_NAME, PR_TRUE, &filePath); + if (! filePath) + return NS_ERROR_FAILURE; + + mControl = CMT_EstablishControlConnection(filePath, &nsPSMShimTbl, &nsPSMMutexTbl); + } + + + if (!mControl || InitPSMUICallbacks(mControl) != PR_SUCCESS) + goto failure; + + nsFileSpec profileSpec; + char* profileName = nsnull; + + NS_WITH_SERVICE(nsIProfile, profile, kProfileCID, &rv); + if (NS_FAILED(rv)) goto failure; + + rv = profile->GetCurrentProfileDir(&profileSpec); + if (NS_FAILED(rv)) goto failure;; + + rv = profile->GetCurrentProfile(&profileName); + if (NS_FAILED(rv)) goto failure; + + CMTStatus psmStatus; + + psmStatus = CMT_Hello( mControl, + PROTOCOL_VERSION, + profileName, + (char*)profileSpec.GetNativePathCString()); + + if (psmStatus == CMTFailure) + { + PR_FREEIF(profileName); + goto failure; + } + + if (NS_FAILED(PassAllPrefs())) + { + PR_FREEIF(profileName); + goto failure; + } + + PR_FREEIF(profileName); + + *_retval = mControl; + return NS_OK; + } + +failure: + + printf("*** Failure setting up Cartman! \n"); + + if (mControl) + { + CMT_CloseControlConnection(mControl); + mControl = NULL; + } + + // TODO we need to unregister our UI callback BEFORE destroying our mutex. + // nsPSMMutexDestroy(); + + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsPSMComponent::DisplaySecurityAdvisor() +{ + CMT_CONTROL *controlConnection; + GetControlConnection( &controlConnection ); + if (DisplayPSMUIDialog(controlConnection, nsnull) == PR_SUCCESS) + return NS_OK; + return NS_ERROR_FAILURE; +} + +//----------------------------------------- +// Secure Hash Functions +//----------------------------------------- +NS_IMETHODIMP +nsPSMComponent::HashBegin(PRUint32 alg, PRUint32* id) +{ + CMT_CONTROL *controlConnection; + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + + if(CMT_HashCreate(controlConnection, alg, (CMUint32*)id) != CMTSuccess) + return NS_ERROR_FAILURE; + if(CMT_HASH_Begin(controlConnection, *id) != CMTSuccess) + return NS_ERROR_FAILURE; + + return NS_OK; +} + +NS_IMETHODIMP +nsPSMComponent::HashUpdate(PRUint32 id, const char* buf, PRUint32 buflen) +{ + CMT_CONTROL *controlConnection; + + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + if (CMT_HASH_Update(controlConnection, id, + (const unsigned char*)buf, buflen) != CMTSuccess) + return NS_ERROR_FAILURE; + + return NS_OK; +} + +NS_IMETHODIMP +nsPSMComponent::HashEnd(PRUint32 id, char** hash, PRUint32* hashlen, + PRUint32 maxLen) +{ + if (!hash) + return NS_ERROR_ILLEGAL_VALUE; + + CMT_CONTROL *controlConnection; + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + + if(CMT_HASH_End(controlConnection, id, (unsigned char*)*hash, + (CMUint32*)hashlen, maxLen) != CMTSuccess) + return NS_ERROR_FAILURE; + CMT_HASH_Destroy(controlConnection, id); + return NS_OK; +} + +//----------------------------------------- +// Signature Verification Functions +//----------------------------------------- +PR_STATIC_CALLBACK(void) +UselessPK7DataSink(void* arg, const char* buf, CMUint32 len) +{ +} + +NS_IMETHODIMP +nsPSMComponent::VerifyRSABegin(PRUint32* id) +{ + if (!id) + return NS_ERROR_ILLEGAL_VALUE; + + CMT_CONTROL *controlConnection; + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + + CMInt32* blah = nsnull; + CMTStatus result = CMT_PKCS7DecoderStart(controlConnection, nsnull /*?*/, + (CMUint32*)id, blah, + UselessPK7DataSink, nsnull); + if (result == CMTSuccess) + return NS_OK; + else + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsPSMComponent::VerifyRSAUpdate(PRUint32 id, const char* buf, PRUint32 buflen) +{ + CMT_CONTROL *controlConnection; + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + + CMTStatus result = CMT_PKCS7DecoderUpdate(controlConnection, id, buf, buflen); + if (result == CMTSuccess) + return NS_OK; + else + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsPSMComponent::VerifyRSAEnd(PRUint32 id, const char* plaintext, + PRUint32 plaintextLen, + PRBool keepCert, nsIPrincipal** aPrincipal) +{ + CMT_CONTROL *controlConnection; + if (NS_FAILED(GetControlConnection( &controlConnection ))) + return NS_ERROR_FAILURE; + + CMUint32 contentInfo; + CMTStatus result = CMT_PKCS7DecoderFinish(controlConnection, + id, &contentInfo); + if (result != CMTSuccess) + return NS_ERROR_FAILURE; + + //-- Make sure a signature is present + CMInt32 isSigned; + result = CMT_GetNumericAttribute(controlConnection, contentInfo, + SSM_FID_P7CINFO_IS_SIGNED, &isSigned); + if (result != CMTSuccess || !isSigned) + return NS_ERROR_FAILURE; + + // SHA1 hash the plaintext to compare it to the signature + CMUint32 hashId; + CMT_HashCreate(controlConnection, PSM_HASH_SHA1, &hashId); + CMT_HASH_Begin(controlConnection, hashId); + result = CMT_HASH_Update(controlConnection, hashId, + (const unsigned char*)plaintext, plaintextLen); + if (result != CMTSuccess) return NS_ERROR_FAILURE; + + unsigned char* hash = (unsigned char*)PR_MALLOC(PSM_HASH_SHA1_LENGTH); + if (!hash) return NS_ERROR_OUT_OF_MEMORY; + CMUint32 hashLen; + result = CMT_HASH_End(controlConnection, hashId, hash, + &hashLen, PSM_HASH_SHA1_LENGTH); + NS_ASSERTION(hashLen == PSM_HASH_SHA1_LENGTH, + "PSMComponent: Hash too short."); + CMT_HASH_Destroy(controlConnection, hashId); + if (result != CMTSuccess) + { + PR_FREEIF(hash); + return NS_ERROR_FAILURE; + } + //-- Verify signature + CMInt32 ok; + CMTItemStr item; + item.data = hash; + item.len = hashLen; + result = CMT_PKCS7VerifyDetachedSignature(controlConnection, contentInfo, + 6 /* =Object Sining Cert */, + 3 /* =SHA1 algorithm (MD5=2)*/, + (CMUint32)keepCert, + &item, &ok); + PR_FREEIF(hash); + if (result != CMTSuccess) return NS_ERROR_FAILURE; + //-- Did it verify? + + if (ok != 0) + *aPrincipal = nsnull; + else + { + //-- Read cert info + CMInt32 cert; + result = CMT_GetNumericAttribute(controlConnection, contentInfo, + SSM_FID_P7CINFO_SIGNER_CERT, &cert); + if (result != CMTSuccess) return NS_ERROR_FAILURE; + CMTItemStr issuer; + result = CMT_GetStringAttribute(controlConnection, cert, + SSM_FID_CERT_ISSUER_NAME, &issuer); + CMTItemStr serial; + result = CMT_GetStringAttribute(controlConnection, cert, + SSM_FID_CERT_SERIAL_NUMBER, &serial); + //-- Get a principal + nsresult rv; + NS_WITH_SERVICE(nsIScriptSecurityManager, secMan, + NS_SCRIPTSECURITYMANAGER_PROGID, &rv) + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + rv = secMan->GetCertificatePrincipal((char*)issuer.data, + (char*)serial.data, aPrincipal); + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + } + + + result = CMT_PKCS7DestroyContentInfo(controlConnection, contentInfo); + + if(result != CMTSuccess) return NS_ERROR_FAILURE; + return NS_OK; +} diff --git a/extensions/psm-glue/src/nsPSMComponent.h b/extensions/psm-glue/src/nsPSMComponent.h new file mode 100644 index 00000000000..77913180fc2 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMComponent.h @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nscore.h" +#include "nsIPSMComponent.h" + +#define NS_PSMCOMPONENT_CID {0xddcae170, 0x5412, 0x11d3, {0xbb, 0xc8, 0x00, 0x00, 0x86, 0x1d, 0x12, 0x37}} + +// Implementation of the PSM app shell component interface. +class nsPSMComponent : public nsIPSMComponent +{ +public: + NS_DEFINE_STATIC_CID_ACCESSOR( NS_PSMCOMPONENT_CID ); + + nsPSMComponent(); + virtual ~nsPSMComponent(); + + NS_DECL_ISUPPORTS + + NS_DECL_NSIPSMCOMPONENT + + static NS_METHOD CreatePSMComponent(nsISupports* aOuter, REFNSIID aIID, void **aResult); + +private: + + PCMT_CONTROL mControl; + + nsCOMPtr mSecureBrowserIU; + static nsPSMComponent* mInstance; + nsresult PassAllPrefs(); +}; diff --git a/extensions/psm-glue/src/nsPSMModule.cpp b/extensions/psm-glue/src/nsPSMModule.cpp new file mode 100644 index 00000000000..1a7b84f6a67 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMModule.cpp @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsIModule.h" +#include "nsIGenericFactory.h" + +#include "nsPSMUICallbacks.h" +#include "nsPSMComponent.h" + +#include "nsISecureBrowserUI.h" +#include "nsSecureBrowserUIImpl.h" + +#include "nsSSLSocketProvider.h" + +static nsModuleComponentInfo components[] = +{ + { + PSM_COMPONENT_CLASSNAME, + NS_PSMCOMPONENT_CID, + PSM_COMPONENT_PROGID, + nsPSMComponent::CreatePSMComponent + }, + + { + PSM_UI_HANLDER_CLASSNAME, + NS_PSMUIHANDLER_CID, + PSM_UI_HANLDER_PROGID, + nsPSMUIHandlerImpl::CreatePSMUIHandler + }, + + { + NS_SECURE_BROWSER_UI_CLASSNAME, + NS_SECURE_BROWSER_UI_CID, + NS_SECURE_BROWSER_UI_PROGID, + nsSecureBrowserUIImpl::CreateSecureBrowserUI + }, + + { + NS_SECURE_BROWSER_DOCOBSERVER_CLASSNAME, + NS_SECURE_BROWSER_DOCOBSERVER_CID, + NS_SECURE_BROWSER_DOCOBSERVER_PROGID, + nsSecureBrowserUIImpl::CreateSecureBrowserUI + }, + + { + NS_ISSLSOCKETPROVIDER_CLASSNAME, + NS_SSLSOCKETPROVIDER_CID, + NS_ISSLSOCKETPROVIDER_PROGID, + nsSSLSocketProvider::Create + } +}; + +NS_IMPL_NSGETMODULE("PSMComponent", components); diff --git a/extensions/psm-glue/src/nsPSMMutex.c b/extensions/psm-glue/src/nsPSMMutex.c new file mode 100644 index 00000000000..c628c35897f --- /dev/null +++ b/extensions/psm-glue/src/nsPSMMutex.c @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "prmon.h" +#include "prtypes.h" + +#include "nsPSMMutex.h" + +static PRMonitor *_nsPSMMutexVar; + +PRStatus +nsPSMMutexInit() +{ + if (!_nsPSMMutexVar) + _nsPSMMutexVar = PR_NewMonitor(); + else + printf("PSMMutex warning got called twice\n"); + + return _nsPSMMutexVar ? PR_SUCCESS : PR_FAILURE; +} + +PRStatus +nsPSMMutexDestroy() +{ + if (!_nsPSMMutexVar) + return PR_FAILURE; + + PR_Wait(_nsPSMMutexVar, PR_INTERVAL_NO_TIMEOUT); + + PR_DestroyMonitor(_nsPSMMutexVar); + return PR_SUCCESS; +} + +static void +nsPSMMutexLock(CMTMutexPointer *p) +{ + PR_EnterMonitor(*(PRMonitor **)p); + return; +} + +static void +nsPSMMutexUnlock(CMTMutexPointer *p) +{ + PR_ExitMonitor(*(PRMonitor **)p); + return; +} + +CMT_MUTEX nsPSMMutexTbl = +{ + &_nsPSMMutexVar, + (CMTMutexFunction)nsPSMMutexLock, + (CMTMutexFunction)nsPSMMutexUnlock +}; diff --git a/extensions/psm-glue/src/nsPSMMutex.h b/extensions/psm-glue/src/nsPSMMutex.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/extensions/psm-glue/src/nsPSMShimLayer.c b/extensions/psm-glue/src/nsPSMShimLayer.c new file mode 100644 index 00000000000..56ac3bacc62 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMShimLayer.c @@ -0,0 +1,269 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nspr.h" +#include "nsPSMShimLayer.h" + +#ifdef XP_UNIX +#include +#include +#endif + + +#define NSPSMSHIMMAXFD 50 + +CMT_SocketFuncs nsPSMShimTbl = +{ + nsPSMShimGetSocket, + nsPSMShimConnect, + nsPSMShimVerifyUnixSocket, + nsPSMShimSend, + nsPSMShimSelect, + nsPSMShimReceive, + nsPSMShimShutdown, + nsPSMShimClose +}; + + +CMTSocket +nsPSMShimGetSocket(int unixSock) +{ + PRStatus rv; + PRFileDesc *fd; + CMSocket *sock; + + + if (unixSock) + { +#ifndef XP_UNIX + return NULL; +#else + fd = PR_OpenTCPSocket(AF_UNIX); + PR_ASSERT(fd); +#endif + } + else + { + PRSocketOptionData sockopt; + + fd = PR_NewTCPSocket(); + PR_ASSERT(fd); + + /* disable Nagle algorithm delay for control sockets */ + sockopt.option = PR_SockOpt_NoDelay; + sockopt.value.no_delay = PR_TRUE; + + rv = PR_SetSocketOption(fd, &sockopt); + PR_ASSERT(PR_SUCCESS == rv); + } + + sock = (CMSocket *)PR_Malloc(sizeof(CMSocket)); + + if (sock == NULL) + return sock; + + sock->fd = fd; + sock->isUnix = unixSock; + + memset(&sock->netAddr, 0, sizeof(PRNetAddr)); + + return (CMTSocket)sock; +} + +CMTStatus +nsPSMShimConnect(CMTSocket sock, short port, char *path) +{ + PRStatus err; + PRErrorCode errcode; + CMTStatus rv = CMTSuccess; + CMSocket *cmSock = (CMSocket *)sock; + + if (cmSock->isUnix) + { +#ifndef XP_UNIX + return CMTFailure; +#else + int pathLen; + if (!path) + { + return CMTFailure; + } + + /* check buffer overrun */ + pathLen = strlen(path)+1; + + pathLen = pathLen < sizeof(cmSock->netAddr.local.path) + ? pathLen : sizeof(cmSock->netAddr.local.path); + + memcpy(&cmSock->netAddr.local.path, path, pathLen); + cmSock->netAddr.local.family = PR_AF_LOCAL; +#endif + } + else /* cmSock->isUnix */ + { + cmSock->netAddr.inet.family = PR_AF_INET; + cmSock->netAddr.inet.port = PR_htons(port); + cmSock->netAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); + } + + err = PR_Connect( cmSock->fd, &cmSock->netAddr, PR_INTERVAL_MAX ); + + if (err == PR_FAILURE) + { + errcode = PR_GetError(); + + /* TODO: verify PR_INVALID_ARGUMENT_ERROR continue with connect */ + + switch (errcode) + { + case PR_IS_CONNECTED_ERROR: + rv = CMTSuccess; + break; + + case PR_IN_PROGRESS_ERROR: + case PR_IO_TIMEOUT_ERROR: +#ifdef WIN32 + case PR_WOULD_BLOCK_ERROR: + case PR_INVALID_ARGUMENT_ERROR: +#endif + default: + rv = CMTFailure; + break; + } + } + + return rv; +} + +CMTStatus +nsPSMShimVerifyUnixSocket(CMTSocket sock) +{ +#ifndef XP_UNIX + return CMTFailure; +#else + + int rv; + CMSocket *cmSock = (CMSocket *)sock; + struct stat statbuf; + + if (!cmSock->isUnix) + return CMTFailure; + + rv = stat(cmSock->netAddr.local.path, &statbuf); + if (rv < 0 || statbuf.st_uid != geteuid() ) + { + PR_Close(cmSock->fd); + cmSock->fd = NULL; + PR_Free(cmSock); + return CMTFailure; + } + return CMTSuccess; +#endif +} + +size_t +nsPSMShimSend(CMTSocket sock, void *buffer, size_t length) +{ + PRInt32 total; + CMSocket *cmSock = (CMSocket *)sock; + + total = PR_Send(cmSock->fd, buffer, length, 0, PR_INTERVAL_NO_TIMEOUT); + + /* TODO: for now, return 0 if there's an error */ + return (total < 0) ? 0 : total; +} + + +CMTSocket +nsPSMShimSelect(CMTSocket *socks, int numsocks, int poll) +{ + CMSocket **sockArr = (CMSocket **)socks; + PRPollDesc readPDs[NSPSMSHIMMAXFD]; + PRIntervalTime timeout; + PRInt32 cnt; + int i; + + memset(readPDs, 0, sizeof(readPDs)); + + PR_ASSERT(NSPSMSHIMMAXFD >= numsocks); + + for (i=0; ifd; + readPDs[i].in_flags = PR_POLL_READ; + } + + timeout = poll ? PR_INTERVAL_NO_WAIT : PR_INTERVAL_NO_TIMEOUT; + + cnt = PR_Poll(readPDs, numsocks, timeout); + + /* Figure out which socket was selected */ + if (cnt > 0) + { + for (i=0; ifd, buffer, bufSize, 0, PR_INTERVAL_NO_TIMEOUT); + + /* TODO: for now, return 0 if there's an error */ + return (total < 0) ? 0 : total; +} + +CMTStatus +nsPSMShimShutdown(CMTSocket sock) +{ + CMSocket *cmSock = (CMSocket*)sock; + PRStatus rv = PR_Shutdown(cmSock->fd, PR_SHUTDOWN_SEND); + return (PR_SUCCESS == rv) ? CMTSuccess : CMTFailure; +} + +CMTStatus +nsPSMShimClose(CMTSocket sock) +{ + CMSocket *cmSock = (CMSocket*)sock; + PRStatus rv = PR_SUCCESS; + PR_ASSERT(cmSock); + + rv = PR_Close(cmSock->fd); + cmSock->fd = NULL; + + /* TODO: release ref on control connection */ + PR_Free(cmSock); + + return (PR_SUCCESS == rv) ? CMTSuccess : CMTFailure; +} diff --git a/extensions/psm-glue/src/nsPSMShimLayer.h b/extensions/psm-glue/src/nsPSMShimLayer.h new file mode 100644 index 00000000000..1c2a2ecd588 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMShimLayer.h @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#ifndef _NSPSMSHIMLAYER_H_ +#define _NSPSMSHIMLAYER_H_ + +#include "cmtcmn.h" + +PR_BEGIN_EXTERN_C + +/* TODO: make sure CMSocket is cleaned up */ +typedef struct CMSocket { + PRFileDesc *fd; + PRBool isUnix; + PRNetAddr netAddr; +} CMSocket; + +PR_EXTERN(CMT_SocketFuncs) nsPSMShimTbl; + +PR_EXTERN(CMTSocket) +nsPSMShimGetSocket(int unixSock); + +PR_EXTERN(CMTStatus) +nsPSMShimConnect(CMTSocket sock, short port, char *path); + +PR_EXTERN(CMTStatus) +nsPSMShimVerifyUnixSocket(CMTSocket sock); + +PR_EXTERN(size_t) +nsPSMShimSend(CMTSocket sock, void *buffer, size_t length); + +PR_EXTERN(CMTSocket) +nsPSMShimSelect(CMTSocket *socks, int numsocks, int poll); + +PR_EXTERN(size_t) +nsPSMShimReceive(CMTSocket sock, void *buffer, size_t bufSize); + +PR_EXTERN(CMTStatus) +nsPSMShimShutdown(CMTSocket sock); + +PR_EXTERN(CMTStatus) +nsPSMShimClose(CMTSocket sock); + +PR_END_EXTERN_C + +#endif /* _NSPSMSHIMLAYER_H_ */ diff --git a/extensions/psm-glue/src/nsPSMUICallbacks.cpp b/extensions/psm-glue/src/nsPSMUICallbacks.cpp new file mode 100644 index 00000000000..faa49fec4f8 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMUICallbacks.cpp @@ -0,0 +1,305 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsProxiedService.h" +#include "nsIEventQueueService.h" +#include "nsPSMUICallbacks.h" + +#include "nsINetSupportDialogService.h" +#include "nsIFileSpecWithUI.h" + + + +#include "nsAppShellCIDs.h" +#include "prprf.h" + +// Interfaces Needed +#include "nsIAppShellService.h" +#include "nsIDocShell.h" +#include "nsIDOMWindow.h" +#include "nsIInterfaceRequestor.h" +#include "nsIPrompt.h" +#include "nsIScriptGlobalObject.h" +#include "nsIURL.h" +#include "nsIXULWindow.h" + +static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID); +static NS_DEFINE_CID(kNetSupportDialogCID, NS_NETSUPPORTDIALOG_CID); + + +// Happy callbacks +static char * PromptUserCallback(void *arg, char *prompt, int isPasswd); +static char * FilePathPromptCallback(void *arg, char *prompt, char *fileRegEx, CMUint32 shouldFileExist); +static void ApplicationFreeCallback(char *userInput); +static void * CartmanUIHandler(uint32 resourceID, void* clientContext, uint32 width, uint32 height, char* urlStr, void *data); +extern "C" void CARTMAN_UIEventLoop(void *data); + + +/* nsISupports Implementation for the class */ +NS_IMPL_ISUPPORTS1(nsPSMUIHandlerImpl, nsIPSMUIHandler) + +NS_METHOD +nsPSMUIHandlerImpl::DisplayURI(PRInt32 width, PRInt32 height, const char *urlStr) +{ + nsresult rv; + + NS_WITH_SERVICE(nsIAppShellService, appShell, kAppShellServiceCID, &rv); + if (NS_SUCCEEDED(rv)) + { + // get a parent window for the new browser window + nsCOMPtr parent; + appShell->GetHiddenWindow(getter_AddRefs(parent)); + + // convert it to a DOMWindow + nsCOMPtr docShell; + if (parent) + { + parent->GetDocShell(getter_AddRefs(docShell)); + } + nsCOMPtr domParent(do_GetInterface(docShell)); + nsCOMPtr sgo(do_QueryInterface(domParent)); + + nsCOMPtr context; + if (sgo) + { + sgo->GetContext(getter_AddRefs(context)); + } + if (context) + { + JSContext *jsContext = (JSContext*)context->GetNativeContext(); + if (jsContext) + { + void *stackPtr; + + char buffer[256]; + PR_snprintf(buffer, + sizeof(buffer), + "menubar=no,height=%d,width=%d", + height, + width ); + + jsval *argv = JS_PushArguments(jsContext, &stackPtr, "sss", urlStr, "_blank", buffer); + if (argv) + { + // open the window + nsIDOMWindow *newWindow; + domParent->Open(jsContext, argv, 3, &newWindow); + newWindow->ResizeTo(width, height); + JS_PopArguments(jsContext, stackPtr); + } + } + } + } + return rv; +} + +NS_IMETHODIMP +nsPSMUIHandlerImpl::PromptForFile(const char *prompt, const char *fileRegEx, PRBool shouldFileExist, char **outFile) +{ + NS_ENSURE_ARG_POINTER(outFile); + nsIFileSpecWithUI* file = NS_CreateFileSpecWithUI(); + + if (file == nsnull) + return NS_ERROR_NULL_POINTER; + + nsresult rv = file->ChooseInputFile(prompt, + nsIFileSpecWithUI::eAllFiles | nsIFileSpecWithUI::eExtraFilter, + fileRegEx, // FIX name? + fileRegEx); + + if (NS_FAILED(rv)) + return rv; + + rv = file->GetNativePath(outFile); + + NS_RELEASE(file); + + return rv; +} + +NS_METHOD +nsPSMUIHandlerImpl::CreatePSMUIHandler(nsISupports* aOuter, REFNSIID aIID, void **aResult) +{ + nsresult rv = NS_OK; + if ( aResult ) + { + /* Allocate new find component object. */ + nsPSMUIHandlerImpl *component = new nsPSMUIHandlerImpl(); + if ( component ) + { + /* Allocated OK, do query interface to get proper */ + /* pointer and increment refcount. */ + rv = component->QueryInterface( aIID, aResult ); + if ( NS_FAILED( rv ) ) + { + /* refcount still at zero, delete it here. */ + delete component; + } + } + else + { + rv = NS_ERROR_OUT_OF_MEMORY; + } + } + else + { + rv = NS_ERROR_NULL_POINTER; + } + return rv; +} + + + +extern "C" void CARTMAN_UIEventLoop(void *data) +{ + CMT_EventLoop((PCMT_CONTROL)data); +} + +PRStatus InitPSMUICallbacks(PCMT_CONTROL control) +{ + if (!control) + return PR_FAILURE; + + CMT_SetPromptCallback(control, (promptCallback_fn)PromptUserCallback, nsnull); + CMT_SetAppFreeCallback(control, (applicationFreeCallback_fn) ApplicationFreeCallback); + CMT_SetFilePathPromptCallback(control, (filePathPromptCallback_fn) FilePathPromptCallback, nsnull); + + if (CMT_SetUIHandlerCallback(control, (uiHandlerCallback_fn) CartmanUIHandler, NULL) != CMTSuccess) + return PR_FAILURE; + + PR_CreateThread(PR_USER_THREAD, + CARTMAN_UIEventLoop, + control, + PR_PRIORITY_NORMAL, + PR_GLOBAL_THREAD, + PR_UNJOINABLE_THREAD, + 0); + + return PR_SUCCESS; +} + +PRStatus DisplayPSMUIDialog(PCMT_CONTROL control, void *arg) +{ + CMUint32 advRID = 0; + CMInt32 width = 0; + CMInt32 height = 0; + CMTItem urlItem = {0, NULL, 0}; + CMTStatus rv = CMTSuccess; + CMTItem advisorContext = {0, NULL, 0}; + void * pwin; + + CMTSecurityAdvisorData data; + memset(&data, '\0', sizeof(CMTSecurityAdvisorData)); + + /* Create a Security Advisor context object. */ + rv = CMT_SecurityAdvisor(control, &data, &advRID); + + if (rv != CMTSuccess) + return PR_FAILURE; + + /* Get the URL, width, height, etc. from the advisor context. */ + rv = CMT_GetStringAttribute(control, + advRID, + SSM_FID_SECADVISOR_URL, + &urlItem); + + if ((rv != CMTSuccess) || (!urlItem.data)) + return PR_FAILURE; + + rv = CMT_GetNumericAttribute(control, + advRID, + SSM_FID_SECADVISOR_WIDTH, + &width); + if (rv != CMTSuccess) + return PR_FAILURE; + + rv = CMT_GetNumericAttribute(control, + advRID, + SSM_FID_SECADVISOR_HEIGHT, + &height); + if (rv != CMTSuccess) + return PR_FAILURE; + + /* Fire the URL up in a window of its own. */ + pwin = CartmanUIHandler(advRID, arg, width, height, (char*)urlItem.data, NULL); + return PR_SUCCESS; +} + + + +void* CartmanUIHandler(uint32 resourceID, void* clientContext, uint32 width, uint32 height, char* urlStr, void *data) +{ + nsresult rv = NS_OK; + + NS_WITH_PROXIED_SERVICE(nsIPSMUIHandler, handler, nsPSMUIHandlerImpl::GetCID(), NS_UI_THREAD_EVENTQ, &rv); + + if(NS_SUCCEEDED(rv)) + handler->DisplayURI(width, height, urlStr); + + return nsnull; +} + + + +char * PromptUserCallback(void *arg, char *prompt, int isPasswd) +{ + + nsresult rv = NS_OK; + PRUnichar *password; + PRInt32 value; + + NS_WITH_PROXIED_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, NS_UI_THREAD_EVENTQ, &rv); + + if (NS_SUCCEEDED(rv)) { + rv = dialog->PromptPassword(nsString(prompt).GetUnicode(), nsnull /* window title */, &password, &value); + + if (NS_SUCCEEDED(rv)) { + nsString a(password); + char* str = a.ToNewCString(); + Recycle(password); + return str; + } + } + + return nsnull; +} + +void ApplicationFreeCallback(char *userInput) +{ + nsAllocator::Free(userInput); +} + +char * FilePathPromptCallback(void *arg, char *prompt, char *fileRegEx, CMUint32 shouldFileExist) +{ + nsresult rv = NS_OK; + + char* filePath = nsnull; + + NS_WITH_PROXIED_SERVICE(nsIPSMUIHandler, handler, nsPSMUIHandlerImpl::GetCID(), NS_UI_THREAD_EVENTQ, &rv); + + if(NS_SUCCEEDED(rv)) + handler->PromptForFile(prompt, fileRegEx, (PRBool)shouldFileExist, &filePath); + + return filePath; +} diff --git a/extensions/psm-glue/src/nsPSMUICallbacks.h b/extensions/psm-glue/src/nsPSMUICallbacks.h new file mode 100644 index 00000000000..b2bbd9f7626 --- /dev/null +++ b/extensions/psm-glue/src/nsPSMUICallbacks.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#ifndef _NSPSMUICALLBACKS_H +#define _NSPSMUICALLBACKS_H + +#include "prtypes.h" +#include "nsIPSMUIHandler.h" + +PR_BEGIN_EXTERN_C + +#include "cmtcmn.h" /* fix */ +#include "cmtjs.h" + +PRStatus InitPSMUICallbacks(PCMT_CONTROL gControl); +PRStatus DisplayPSMUIDialog(PCMT_CONTROL control, void *arg); + +PR_END_EXTERN_C + +#define NS_PSMUIHANDLER_CID {0x15944e30, 0x601e, 0x11d3, {0x8c, 0x4a, 0x00, 0x00, 0x64, 0x65, 0x73, 0x74}} + +class nsPSMUIHandlerImpl : public nsIPSMUIHandler +{ + public: + + NS_DEFINE_STATIC_CID_ACCESSOR( NS_PSMUIHANDLER_CID ); + + /* ctor/dtor */ + nsPSMUIHandlerImpl() { NS_INIT_REFCNT(); } + virtual ~nsPSMUIHandlerImpl() { } + + NS_DECL_ISUPPORTS + NS_DECL_NSIPSMUIHANDLER + + static NS_METHOD CreatePSMUIHandler(nsISupports* aOuter, REFNSIID aIID, void **aResult); +}; + +#endif diff --git a/extensions/psm-glue/src/nsSSLIOLayer.cpp b/extensions/psm-glue/src/nsSSLIOLayer.cpp new file mode 100644 index 00000000000..79c43367573 --- /dev/null +++ b/extensions/psm-glue/src/nsSSLIOLayer.cpp @@ -0,0 +1,315 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): +*/ + +#include "nspr.h" + +#include "cmtcmn.h" + +#include "nsIPSMComponent.h" +#include "nsIServiceManager.h" +#include "nsPSMShimLayer.h" +#include "nsSSLIOLayer.h" + +/* TODO: check for failures */ +static PRDescIdentity nsSSLIOLayerIdentity; +static PRIOMethods nsSSLIOLayerMethods; + +typedef struct nsSSLIOLayerSecretData +{ + PCMT_CONTROL control; + CMSocket *cmsock; +} nsSSLIOLayerSecretData; + +static nsIPSMComponent* psm = nsnull; + +static PRStatus PR_CALLBACK +nsSSLIOLayerConnect(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) +{ + nsSSLIOLayerSecretData *secret; + int i=1; + nsresult result; + PRStatus rv = PR_SUCCESS; + CMTStatus status; + + const char* hostName; + + /* Set the error in case of failure. */ + + PR_SetError(PR_UNKNOWN_ERROR, status); + + if (!fd || !addr) + return PR_FAILURE; + + secret = (nsSSLIOLayerSecretData *)PR_Malloc(sizeof(nsSSLIOLayerSecretData)); + if (!secret) return PR_FAILURE; + + memset(secret, 0, sizeof(nsSSLIOLayerSecretData)); + + /* TODO: this should be allocated from cmshim layer */ + secret->cmsock = (CMSocket *)PR_Malloc(sizeof(CMSocket)); + if (!secret->cmsock) + { + PR_Free(secret); + return PR_FAILURE; + } + memset(secret->cmsock, 0, sizeof(CMSocket)); + + if (psm == nsnull) + { + result = nsServiceManager::GetService( PSM_COMPONENT_PROGID, + NS_GET_IID(nsIPSMComponent), + (nsISupports**)&psm); + if (NS_FAILED(result)) + { + rv=PR_FAILURE; + goto fail; + } + } + + result = psm->GetControlConnection(&secret->control); + + if (result != PR_SUCCESS) + { + rv = PR_FAILURE; + goto fail; + } + + secret->cmsock->fd = fd->lower; + secret->cmsock->isUnix = PR_FALSE; + + /* TODO: XXX fix this RSN */ + { + PRSocketOptionData opt; + + // Make the socket non-blocking... + opt.option = PR_SockOpt_Nonblocking; + opt.value.non_blocking = PR_FALSE; + rv = PR_SetSocketOption(fd->lower, &opt); + if (PR_SUCCESS != rv) + { + goto fail; + } + } + + char ipBuffer[PR_NETDB_BUF_SIZE]; + rv = PR_NetAddrToString(addr, (char*)&ipBuffer, PR_NETDB_BUF_SIZE); + + if (rv != PR_SUCCESS) + { + goto fail; + } + + + if (fd->secret) // how do we know that this is a necko nsSocketTransportFDPrivate?? + { + hostName = (const char*)fd->secret; + } + else + { + // no hostname, use ip address. + hostName = ipBuffer; + } + + + fd->secret = (PRFilePrivate *)secret; + + status = CMT_OpenSSLConnection(secret->control, + secret->cmsock, + SSM_REQUEST_SSL_DATA_SSL, + PR_ntohs(addr->inet.port), + ipBuffer, + (char*)hostName, + CM_TRUE, + nsnull); + if (CMTSuccess == status) + { + // since our stuff can block, what we want to do is return PR_FAILURE, + // but set the nspr ERROR to BLOCK. This will put us into a select + // q. + PR_SetError(PR_WOULD_BLOCK_ERROR, status); + return PR_FAILURE; + } + +fail: + fd->secret = nsnull; + PR_FREEIF(secret->cmsock) + + secret->cmsock = nsnull; + PR_FREEIF(secret); + + secret = nsnull; + return rv; +} + + /* CMT_DestroyDataConnection(ctrl, sock); */ + /* need to strip our layer, pass result to DestroyDataConnection */ + /* which will clean up the CMT accounting of sock, then call our */ + /* shim layer to translate back to NSPR */ + +static PRStatus PR_CALLBACK +nsSSLIOLayerClose(PRFileDesc *fd) +{ + nsSSLIOLayerSecretData *secret = (nsSSLIOLayerSecretData *)fd->secret; + PRDescIdentity id = PR_GetLayersIdentity(fd); + + if (secret && id == nsSSLIOLayerIdentity) + { + CMInt32 errorCode = PR_FAILURE; + + if (CMT_GetSSLDataErrorCode(secret->control, secret->cmsock, &errorCode) == PR_SUCCESS) + { + CMT_DestroyDataConnection(secret->control, secret->cmsock); + + PR_Free(secret); + + fd->secret = NULL; + fd->identity = PR_INVALID_IO_LAYER; + } + return (PRStatus)errorCode; + } + + return PR_FAILURE; +} + +static PRInt32 PR_CALLBACK +nsSSLIOLayerRead( PRFileDesc *fd, void *buf, PRInt32 amount) +{ + if (!fd) + return PR_FAILURE; + + PRInt32 result = PR_Recv(fd, buf, amount, 0, PR_INTERVAL_MIN); + + if (result > 0) + return result; + + if (result == -1) + { + PRErrorCode code = PR_GetError(); + + if (code == PR_IO_TIMEOUT_ERROR ) + PR_SetError(PR_WOULD_BLOCK_ERROR, PR_WOULD_BLOCK_ERROR); + return PR_FAILURE; + } + + if (result == 0) + { + nsSSLIOLayerSecretData *secret = (nsSSLIOLayerSecretData *)fd->secret; + PRDescIdentity id = PR_GetLayersIdentity(fd); + + if (secret && id == nsSSLIOLayerIdentity) + { + CMInt32 errorCode = PR_FAILURE; + + CMT_GetSSLDataErrorCode(secret->control, secret->cmsock, &errorCode); + + if (errorCode == PR_IO_TIMEOUT_ERROR) + { + PR_SetError(PR_WOULD_BLOCK_ERROR, PR_WOULD_BLOCK_ERROR); + return PR_FAILURE; + } + + PR_SetError(0, 0); + return errorCode; + } + } + + return result; +} + +static PRInt32 PR_CALLBACK +nsSSLIOLayerWrite( PRFileDesc *fd, const void *buf, PRInt32 amount) +{ + if (!fd) + return PR_FAILURE; + + PRInt32 result = PR_Send(fd, buf, amount, 0, PR_INTERVAL_MIN); + + if (result > 0) + return result; + + if (result == -1) + { + PRErrorCode code = PR_GetError(); + + if (code == PR_IO_TIMEOUT_ERROR ) + PR_SetError(PR_WOULD_BLOCK_ERROR, PR_WOULD_BLOCK_ERROR); + return PR_FAILURE; + } + + if (result == 0) + { + nsSSLIOLayerSecretData *secret = (nsSSLIOLayerSecretData *)fd->secret; + PRDescIdentity id = PR_GetLayersIdentity(fd); + + if (secret && id == nsSSLIOLayerIdentity) + { + CMInt32 errorCode = PR_FAILURE; + + CMT_GetSSLDataErrorCode(secret->control, secret->cmsock, &errorCode); + PR_SetError(0, 0); + return errorCode; + } + } + + + return result; +} + +PRFileDesc * +nsSSLIOLayerNewSocket(const char* hostName) +{ + static PRBool firstTime = PR_TRUE; + PRFileDesc * sock; + PRFileDesc * layer; + PRStatus rv; + + /* Get a normal NSPR socket */ + sock = PR_NewTCPSocket(); PR_ASSERT(NULL != sock); + + if (! sock) return NULL; + + + if (firstTime) + { + nsSSLIOLayerIdentity = PR_GetUniqueIdentity("Cartman layer"); + nsSSLIOLayerMethods = *PR_GetDefaultIOMethods(); + + nsSSLIOLayerMethods.connect = nsSSLIOLayerConnect; + nsSSLIOLayerMethods.close = nsSSLIOLayerClose; + nsSSLIOLayerMethods.read = nsSSLIOLayerRead; + nsSSLIOLayerMethods.write = nsSSLIOLayerWrite; + firstTime = PR_FALSE; + } + + layer = PR_CreateIOLayerStub(nsSSLIOLayerIdentity, &nsSSLIOLayerMethods); + + if (layer) + { + layer->secret = (PRFilePrivate*)hostName; + rv = PR_PushIOLayer(sock, PR_GetLayersIdentity(sock), layer); + } + + if(PR_SUCCESS != rv) + return NULL; + + return sock; +} diff --git a/extensions/psm-glue/src/nsSSLIOLayer.h b/extensions/psm-glue/src/nsSSLIOLayer.h new file mode 100644 index 00000000000..dc832a120b9 --- /dev/null +++ b/extensions/psm-glue/src/nsSSLIOLayer.h @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): +*/ + +#ifndef _NSSSLIOLAYER_H +#define _NSSSLIOLAYER_H + +#include "prtypes.h" +#include "prio.h" + +PR_BEGIN_EXTERN_C + +//typedef PRFileDesc* (PR_CALLBACK *NSPRSocketFN)(void); ?? + +PR_EXTERN(PRFileDesc *) nsSSLIOLayerNewSocket(const char* hostName); + +PR_END_EXTERN_C + +#endif /* _NSSSLIOLAYER_H */ diff --git a/extensions/psm-glue/src/nsSSLSocketProvider.cpp b/extensions/psm-glue/src/nsSSLSocketProvider.cpp new file mode 100644 index 00000000000..48668160361 --- /dev/null +++ b/extensions/psm-glue/src/nsSSLSocketProvider.cpp @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): +*/ + +#include "nsIComponentManager.h" +#include "nsIServiceManager.h" +#include "nsSSLSocketProvider.h" +#include "nsSSLIOLayer.h" + +//////////////////////////////////////////////////////////////////////////////// + +nsSSLSocketProvider::nsSSLSocketProvider() +{ + NS_INIT_REFCNT(); +} + +nsresult +nsSSLSocketProvider::Init() +{ + nsresult rv = NS_OK; + return rv; +} + +nsSSLSocketProvider::~nsSSLSocketProvider() +{ +} + +NS_IMPL_ISUPPORTS2(nsSSLSocketProvider, nsISocketProvider, nsISSLSocketProvider); + +NS_METHOD +nsSSLSocketProvider::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) +{ + if (aOuter) + return NS_ERROR_NO_AGGREGATION; + + nsSSLSocketProvider* pSockProv = new nsSSLSocketProvider(); + + if (nsnull == pSockProv) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(pSockProv); + + nsresult rv = pSockProv->Init(); + + if (NS_SUCCEEDED(rv)) + { + rv = pSockProv->QueryInterface(aIID, aResult); + } + + NS_RELEASE(pSockProv); + + return rv; +} + +NS_IMETHODIMP +nsSSLSocketProvider::NewSocket(const char *hostName, PRFileDesc **_result) +{ + *_result = nsSSLIOLayerNewSocket(hostName); + + return (nsnull == *_result) ? NS_ERROR_SOCKET_CREATE_FAILED : NS_OK; +} diff --git a/extensions/psm-glue/src/nsSSLSocketProvider.h b/extensions/psm-glue/src/nsSSLSocketProvider.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/extensions/psm-glue/src/nsSecureBrowserUIImpl.cpp b/extensions/psm-glue/src/nsSecureBrowserUIImpl.cpp new file mode 100644 index 00000000000..f0394b9cb69 --- /dev/null +++ b/extensions/psm-glue/src/nsSecureBrowserUIImpl.cpp @@ -0,0 +1,481 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#include "nsISecureBrowserUI.h" +#include "nsSecureBrowserUIImpl.h" + +#include "nsCOMPtr.h" +#include "nsIServiceManager.h" + +#include "nsIScriptGlobalObject.h" +#include "nsIObserverService.h" +#include "nsIDocumentLoader.h" +#include "nsCURILoader.h" +#include "nsIDocShell.h" +#include "nsIDocumentViewer.h" +#include "nsCURILoader.h" +#include "nsIDocument.h" +#include "nsIDOMHTMLDocument.h" +#include "nsIDOMXULDocument.h" +#include "nsIDOMElement.h" +#include "nsIDOMWindow.h" +#include "nsIChannel.h" + +#include "nsIURI.h" + +#include "prmem.h" + +#include "nsINetSupportDialogService.h" +#include "nsIPrompt.h" +#include "nsIPref.h" + +static NS_DEFINE_CID(kNetSupportDialogCID, NS_NETSUPPORTDIALOG_CID); +static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); + + +#define ENTER_SITE_PREF "security.warn_entering_secure" +#define LEAVE_SITE_PREF "security.warn_leaving_secure" +#define MIXEDCONTENT_PREF "security.warn_viewing_mixed" +#define INSECURE_SUBMIT_PREF "security.warn_submit_insecure" + + +nsSecureBrowserUIImpl* nsSecureBrowserUIImpl::mInstance = nsnull; + +nsSecureBrowserUIImpl::nsSecureBrowserUIImpl() +{ + NS_INIT_REFCNT(); +} + +nsSecureBrowserUIImpl::~nsSecureBrowserUIImpl() +{ +} + +NS_IMPL_ISUPPORTS1(nsSecureBrowserUIImpl, nsSecureBrowserUI); + +NS_IMETHODIMP +nsSecureBrowserUIImpl::CreateSecureBrowserUI(nsISupports* aOuter, REFNSIID aIID, void **aResult) +{ + if (!aResult) { + return NS_ERROR_INVALID_POINTER; + } + if (aOuter) { + *aResult = nsnull; + return NS_ERROR_NO_AGGREGATION; + } + + if (mInstance == nsnull) + { + mInstance = new nsSecureBrowserUIImpl(); + } + + if (mInstance == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = mInstance->QueryInterface(aIID, aResult); + if (NS_FAILED(rv)) + { + *aResult = nsnull; + } + return rv; +} + +NS_IMETHODIMP +nsSecureBrowserUIImpl::Init(nsIDOMWindow *window, nsIDOMElement *button) +{ + + nsCOMPtr sgo = do_QueryInterface(window); + if (sgo) + { + nsCOMPtr docShell; + + sgo->GetDocShell(getter_AddRefs(docShell)); + if (docShell) + { + nsSecureBrowserObserver *sbo = new nsSecureBrowserObserver(); + if (sbo) + { + NS_ADDREF(sbo); + return sbo->Init(button, docShell); // does the window delete us when it close? + } + } + } + return NS_OK; +} + + +nsSecureBrowserObserver::nsSecureBrowserObserver() +{ + NS_INIT_REFCNT(); + mIsSecureDocument = mMixContentAlertShown = mIsDocumentBroken = PR_FALSE; + +} + +nsSecureBrowserObserver::~nsSecureBrowserObserver() +{ +} + +NS_IMPL_ISUPPORTS1(nsSecureBrowserObserver, nsIDocumentLoaderObserver); + + +nsresult +nsSecureBrowserObserver::Init(nsIDOMElement *button, nsIDocShell* content) +{ + if (!button || !content) + return NS_ERROR_NULL_POINTER; + + mSecurityButton = button; + content->GetDocLoaderObserver(getter_AddRefs(mOldWebShellObserver)); + content->SetDocLoaderObserver(this); + return NS_OK; +} + + +NS_IMETHODIMP +nsSecureBrowserObserver::OnStartDocumentLoad(nsIDocumentLoader* aLoader, + nsIURI* aURL, + const char* aCommand) +{ + nsresult res; + + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnStartDocumentLoad(aLoader, aURL, aCommand); + } + + + if (!mSecurityButton) + return NS_OK; + + if (!aURL || !aLoader) + return NS_ERROR_NULL_POINTER; + + + mIsSecureDocument = mMixContentAlertShown = mIsDocumentBroken = PR_FALSE; + + // check to see that we are going to load the same + // kind of URL (scheme) as we just loaded. + + + PRBool isOldSchemeSecure; + res = IsSecureDocumentLoad(aLoader, &isOldSchemeSecure); + if (NS_FAILED(res)) + return NS_OK; + + PRBool isNewSchemeSecure; + res = IsSecureUrl(PR_FALSE, aURL, &isNewSchemeSecure); + if (NS_FAILED(res)) + return NS_OK; + +#if DEBUG_dougt + printf("[StartPageLoad] isOldSchemeSecure = %d isNewSchemeSecure = %d\n", isOldSchemeSecure, isNewSchemeSecure); +#endif + // if we are going from a secure page to and insecure page + if ( !isNewSchemeSecure && isOldSchemeSecure) + { +#if DEBUG_dougt + printf("change lock icon to unlock - new document\n"); +#endif + mSecurityButton->RemoveAttribute( "level" ); + + + PRBool boolpref; + NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &res); + if (NS_FAILED(res)) + return res; + + if ((prefs->GetBoolPref(LEAVE_SITE_PREF, &boolpref) != 0)) + boolpref = PR_TRUE; + + if (boolpref) + { + NS_WITH_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, &res); + if (NS_FAILED(res)) + return res; + + dialog->Alert(nsString("You are leaving a secure document").GetUnicode()); // fix localize! + } + } + // if we are going from an insecure page to a secure one. + else if (isNewSchemeSecure && !isOldSchemeSecure) + { + PRBool boolpref; + NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &res); + if (NS_FAILED(res)) + return res; + + if ((prefs->GetBoolPref(ENTER_SITE_PREF, &boolpref) != 0)) + boolpref = PR_TRUE; + + if (boolpref) + { + NS_WITH_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, &res); + if (NS_FAILED(res)) + return res; + + dialog->Alert(nsString("You are entering a secure document").GetUnicode()); // fix localize! + } + } + + mIsSecureDocument = isNewSchemeSecure; + + return NS_OK; +} + +NS_IMETHODIMP +nsSecureBrowserObserver::OnEndDocumentLoad(nsIDocumentLoader* aLoader, + nsIChannel* channel, + nsresult aStatus) +{ + nsresult rv; + + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnEndDocumentLoad(aLoader, channel, aStatus); + } + + if (!mIsSecureDocument) + return NS_OK; + + if (!mSecurityButton) + return NS_ERROR_NULL_POINTER; + +#if DEBUG_dougt + printf("[EndPageLoad] mIsSecureDocument = %d aStatus = %d mIsDocumentBroken = %d\n", mIsSecureDocument, aStatus, mIsDocumentBroken); +#endif + + if ( NS_SUCCEEDED(aStatus) && !mIsDocumentBroken ) + { +#if DEBUG_dougt + printf("change lock icon to secure \n"); +#endif + rv = mSecurityButton->SetAttribute( "level", nsString("high") ); + mIsSecureDocument = PR_TRUE; + } + else + { +#if DEBUG_dougt + printf("change lock icon to broken\n"); +#endif + rv = mSecurityButton->SetAttribute( "level", nsString("broken") ); + mIsSecureDocument = PR_FALSE; + } + + return rv; +} + +NS_IMETHODIMP +nsSecureBrowserObserver::OnStartURLLoad(nsIDocumentLoader* loader, + nsIChannel* channel) +{ + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnStartURLLoad(loader, channel); + } + +#if DEBUG_dougt + printf("[StartURLLoad] mIsSecureDocument = %d\n", mIsSecureDocument); +#endif + + PRBool secure; + nsresult rv = IsSecureChannelLoad(channel, &secure); + if (NS_FAILED(rv)) + return rv; + + if (mIsSecureDocument && !secure) + { + mIsDocumentBroken = PR_TRUE; + +// nsCOMPtr uri; +// channel->GetURI(getter_AddRefs(uri)); + +// uri->SetSpec("chrome://navigator/skin/insecureLink.gif"); //fix + + nsresult res; + + PRBool boolpref; + NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &res); + if (NS_FAILED(res)) + return res; + + if ((prefs->GetBoolPref(MIXEDCONTENT_PREF, &boolpref) != 0)) + boolpref = PR_TRUE; + + if (boolpref && !mMixContentAlertShown) + { + NS_WITH_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, &res); + if (NS_FAILED(res)) + return res; + + dialog->Alert(nsString("There is mixed content on this page").GetUnicode()); // fix localize! + mMixContentAlertShown = PR_TRUE; + } + } + return NS_OK; +} + +NS_IMETHODIMP +nsSecureBrowserObserver::OnProgressURLLoad(nsIDocumentLoader* loader, + nsIChannel* channel, + PRUint32 aProgress, + PRUint32 aProgressMax) +{ + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnProgressURLLoad(loader, channel, aProgress, aProgressMax); + } + return NS_OK; +} + +NS_IMETHODIMP +nsSecureBrowserObserver::OnStatusURLLoad(nsIDocumentLoader* loader, + nsIChannel* channel, + nsString& aMsg) +{ + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnStatusURLLoad(loader, channel, aMsg); + } + return NS_OK; +} + + +NS_IMETHODIMP +nsSecureBrowserObserver::OnEndURLLoad(nsIDocumentLoader* loader, + nsIChannel* channel, + nsresult aStatus) +{ + if (mOldWebShellObserver) + { + mOldWebShellObserver->OnEndURLLoad(loader, channel, aStatus); + } + +#if DEBUG_dougt + printf("[OnEndURLLoad] mIsSecureDocument = %d aStatus = %d\n", mIsSecureDocument, aStatus); +#endif + + if ( mIsSecureDocument && NS_FAILED(aStatus)) + { +#if DEBUG_dougt + printf("change lock icon to broken\n"); +#endif + mSecurityButton->SetAttribute( "level", nsString("broken") ); + mIsDocumentBroken = PR_TRUE; + } + return NS_OK; +} + +// fileSecure flag determines if we should include file: and other local protocols. +nsresult +nsSecureBrowserObserver::IsSecureUrl(PRBool fileSecure, nsIURI* aURL, PRBool* value) +{ + *value = PR_FALSE; + + if (!aURL) + return NS_ERROR_NULL_POINTER; +#if DEBUG_dougt + char* string; + aURL->GetSpec(&string); + printf("[ensuring channel]: %s\n", string); + nsAllocator::Free(string); +#endif + + char* scheme; + aURL->GetScheme(&scheme); + + if (scheme == nsnull) + return NS_ERROR_NULL_POINTER; + + if ( (strncmp(scheme, "https", 5) == 0) || + (fileSecure && + (strncmp(scheme, "file", 4) == 0) )) + *value = PR_TRUE; + + nsAllocator::Free(scheme); + return NS_OK; + +} + + +nsresult nsSecureBrowserObserver::IsSecureDocumentLoad(nsIDocumentLoader* loader, PRBool *value) +{ + if (!loader) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr uri; + nsresult rv = GetURIFromDocumentLoader(loader, getter_AddRefs(uri)); + + if (NS_FAILED(rv)) + return rv; + + return IsSecureUrl(PR_FALSE, uri, value); +} + +nsresult nsSecureBrowserObserver::IsSecureChannelLoad(nsIChannel* channel, PRBool *value) +{ + if (!channel) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr uri; + nsresult rv = channel->GetURI(getter_AddRefs(uri)); + if (NS_FAILED(rv)) + return rv; + + return IsSecureUrl(PR_TRUE, uri, value); +} + +nsresult +nsSecureBrowserObserver::GetURIFromDocumentLoader(nsIDocumentLoader* aLoader, nsIURI** uri) +{ + nsresult rv; + + if (aLoader == nsnull) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr cont; + rv = aLoader->GetContainer(getter_AddRefs(cont)); + if (NS_FAILED(rv) || (cont == nsnull)) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr docShell(do_QueryInterface(cont)); + NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); + + nsCOMPtr cv; + rv = docShell->GetContentViewer(getter_AddRefs(cv)); + if (NS_FAILED(rv) || (cv == nsnull)) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr docViewer(do_QueryInterface(cv)); + NS_ENSURE_TRUE(docViewer, NS_ERROR_FAILURE); + + nsCOMPtr doc; + rv = docViewer->GetDocument(*getter_AddRefs(doc)); + if (NS_FAILED(rv) || (doc == nsnull)) + return NS_ERROR_NULL_POINTER; + + *uri = doc->GetDocumentURL(); + if (!*uri) + return NS_ERROR_NULL_POINTER; + + return NS_OK; +} diff --git a/extensions/psm-glue/src/nsSecureBrowserUIImpl.h b/extensions/psm-glue/src/nsSecureBrowserUIImpl.h new file mode 100644 index 00000000000..e3792c675bf --- /dev/null +++ b/extensions/psm-glue/src/nsSecureBrowserUIImpl.h @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Hubbie Shaw + * Doug Turner +*/ + +#ifndef nsSecureBrowserUIImpl_h_ +#define nsSecureBrowserUIImpl_h_ + +#include "nsCOMPtr.h" +#include "nsXPIDLString.h" +#include "nsString.h" +#include "nsIObserver.h" +#include "nsIDocumentLoaderObserver.h" +#include "nsIDOMElement.h" +#include "nsISecureBrowserUI.h" +#include "nsIDocShell.h" + +#define NS_SECURE_BROWSER_DOCOBSERVER_CLASSNAME "Mozilla Secure Browser Doc Observer" + +#define NS_SECURE_BROWSER_DOCOBSERVER_CID \ +{0x97c06c30, 0xa145, 0x11d3, \ +{0x8c, 0x7c, 0x00, 0x60, 0x97, 0x92, 0x27, 0x8c}} + +#define NS_SECURE_BROWSER_DOCOBSERVER_PROGID "component://netscape/secure_browser_docobserver" + + +class nsSecureBrowserObserver : public nsIDocumentLoaderObserver +{ +public: + + nsSecureBrowserObserver(); + virtual ~nsSecureBrowserObserver(); + + nsresult Init(nsIDOMElement *button, nsIDocShell* content); + + NS_DECL_ISUPPORTS + + // nsIDocumentLoaderObserver + NS_DECL_NSIDOCUMENTLOADEROBSERVER + + static nsresult IsSecureDocumentLoad(nsIDocumentLoader* loader, PRBool *value); + static nsresult IsSecureChannelLoad(nsIChannel* channel, PRBool *value); + static nsresult IsSecureUrl(PRBool fileSecure, nsIURI* aURL, PRBool *value); + static nsresult GetURIFromDocumentLoader(nsIDocumentLoader* aLoader, nsIURI** uri); + +protected: + + nsCOMPtr mSecurityButton; + nsCOMPtr mOldWebShellObserver; + + PRBool mIsSecureDocument; // is https loaded + PRBool mIsDocumentBroken; // + PRBool mMixContentAlertShown; + +}; + +class nsSecureBrowserUIImpl : public nsSecureBrowserUI +{ +public: + + nsSecureBrowserUIImpl(); + virtual ~nsSecureBrowserUIImpl(); + + NS_DECL_ISUPPORTS + NS_DECL_NSSECUREBROWSERUI + + static NS_METHOD CreateSecureBrowserUI(nsISupports* aOuter, REFNSIID aIID, void **aResult); + +protected: + + static nsSecureBrowserUIImpl* mInstance; +}; + + +#endif /* nsSecureBrowserUIImpl_h_ */