E-Mail Encryption for Outlook 2000 Using COM Add-in
Introduction
Microsoft Office 2000, which is one of the widely used desktop applications, provides the solution provider the privilege for extending the capabilities of Office applications. Outlook 2000 supports COM Add-in, which extends the capabilities of an application in a tightly integrated way by taking advantage of that application’s extensibility model, for this purpose. The security measures like digital ID or private /public-key pair that are implemented for the E-mail security is cumbersome to setup and is complicate for an average user to understand. So to make the task more users friendly, the encryption of the E-mails using only public key, which can be a password, is recommended.
In this article we have covered how a COM Add-in can be written so that it enhance the security of emails send from the outlook 2000 by providing a security option which enables the user to encrypt and decrypt the emails using password as the key. These passwords may be decided between the parties over the phone or some communication channels.
COM Add-in
COM Add-in is a technology introduced by Microsoft to help the developers to extend the already rich functionality found in the office applications. COM add-ins are DLLs or EXEs that are specially registered so they can be loaded by Office 2000 applications at startup. More tech-nically, a COM add-in for Office 2000 is any in-process or out-of-process COM object that implements the IDT-Exten-sibility2 interface and is set up properly in the registry. COM add-ins are compatible across all Microsoft Office products. This support across all Office products means you can write your add-in once, and then use the add-in in not only Outlook but also other Office products.
COM Add-in Architecture
In this COM Add-in architecture, the outlook application serves as the host for the Add-in and the Add-in that we write acts as the in-process COM server. Here we have used COM Add-in as a compiled DLL that has been registered so that Outlook knows how to load and communicate with the Add-in. Writing the add-in as a DLL, enhances the speed by running the code in-process with the host application and provides performance benefits. Since the Office application loads and connects to the add-in it controls the lifetime of the add-in.
Add-in Registration
The COM add-in technology also relies heavily on the system registry to determine which add-ins are available for the different Office products. The registry also tells the Outlook application how to load the add-in by putting certain configuration information into the registry. When writing registration information to the registry for the add-in, there are three values we need to write. They are 1)FriendlyName which specifies the name of the Add-in that will appear to the user in the Add-in Manager. 2)Description, this property contains the string that will appear at the bottom of the Add-in Manager when the user selects the add-in.3)LoadBehavior,which specifies the way the COM add-in should be loaded. The registry also specifies the users for whom the add-in should load. If we register the add-in under HKEY_LOCAL_MACHINE, it's made available to every user on the machine. And if it’s under HKEY_CURRENT_USER, the add-in will be available to that particular user only. Office now stores the list of regi stered and connected add-ins in a sub key under the Office root. Office now uses the registry strictly to list all add-ins in the COM add-ins collection and uses the registry to store connected/disconnected state as well as boot or demand loaded information.
Initially, once the Dll is made for the COM Add-in it is registered manually byCreating the keys for the respective office application (say ,outlook) and set the values like FriendlyName, Description and LoadBehavior for the key. But Visual Studio 6.0 introduces an add-in registration mechanism that improves the add-in user's experience.Visual Basic and Visual Studio improve the new registration mechanism by creating add-ins that auto-register via the DllRegisterServer entry point. Outlook 2000 uses the same solution for the Outlook 2000 add-ins.
The Add-In Designer, included with Visual Basic 6.0, Visual Studio 6.0, and Outlook2000 Developer, will register Addin by itself under:
HKEY_LOCAL_MACHINE\Software\Microsoft\Shared Tools\Addin Designer.Each application thereafter registered on the machine registers an Application Name key under the Addin Designer. The value under the Application Name key is used in the UI of the Add-in Designer in Visual Basic 6.0. When a designer is added to a Visual Basic 6.0 project, the user selects which application the add-in should be registered for and what the load behavior is. If the add-in works for multiple applications, more than one designer instance is added to the Visual Basic project, and each designer project item is set to a specific application.
In the case of COM Add-ins in VC++ the registration is done manually by running regsvr32 which calls DllRegisterServer for creating the key and setting the properties.
COM Add-in Event Procedures
To work with Outlook 2000 ,the COM Add-ins must implement IDTExtensibility2 interface. The IDTExtensibility2 interface provides five event procedures that we must implement in our add-ins. The host, the outlook application, communicate with the add-in through these interface to tell it when the host application is done initializing, when the add-in has been loaded and disconnected etc. Outlook calls the methods of the interface, which are implemented when an add-in is connected to the application, whether through the Add-Ins dialog box or some other manner.
Event procedures that we must implement in the COM add-in are
- OnConnection-Occurs when the Add-in is connected to the host application
- OnAddinsUpdate-fires whenever any changes are made to add-ins in the Add-in Manager.
- OnStarupComplete-fires when the start up of the host application is complete.
- OnBeginShutdown-fires before the host application begins its unloading process.
- OnDisconnection-fires when the add-in is disconnected from the host application.
Add-In Object Model
Add-in object Model has been used for the design of COM Add-in. Hierarchy of this model has been shown below.
Hierarchy
Application
|
+---COMAddIns
|
+---COMAddIn
|
+---Application
|
+---Connect
|
+---Creator
|
+---Description
|
+---Guid
|
+---Object
|
+---Parent
|
+---ProgID
Outlook Object Model
Outlook Object Model exposes many Objects with its properties, Collections and methods that can be made use of by any COM Add-ins for the Outlook. The COM Add-in that we have made for adding the security option for the emails sent from outlook 2000, makes use of the following objects of the Outlook object model.
Application Object-which is the root object in the object model, represents the entire Microsoft Outlook application and allows access to other objects in the Outlook hierarchy. NameSpace Object- represents an abstract root object for any data source. The object itself provides methods for logging in and out, accessing storage objects directly by ID, accessing certain special default folders directly, and accessing data sources owned by other users. Items Collection Object –which comes under the Folders collection object of the NameSpace Object, represents a collection of Outlook item objects in a folder. Use items(index),where index is the name or index number, to return a single Outlook item. Explorers Collection Objects-which comes under the application Object, contains a set of explorer objects representing all explorers. Explorers property is used to return the Explorers collection from the Application object. Explorer Object- represents the window in which the contents of a folder are displayed. ActiveExplorer is used to return the currently active explorer. CommandBars Collection Objects-which comes under the explorers collection objects, contains a collection of CommandBar objects presented in the container application. This is used to get the toolbar or the menubar present in the container application. By using CommandBars (index), we can get the toolbar or the menubar. Then apply the CommandBar property to the pop-up control to return the command bar that represents that menu. InspectorsCollection Object-which comes under the application object, represents all set of inspector objects representing all inspectors. Inspectors collection is returned from the Application object by using Inspectors property. Inspector Object-represents the window in which an Outlook is displayed . The Active Inspector method is used to return the handle for currently active child window.
Encryption And Decryption Functionalities
Crypto API functions, which can make use of password as the public, key for the encryption and decryption is used here for the purpose. Here we have chosen RC4-Stream cipher algorithm. The two crypto API functions used for these purpose are.
CryptEncrypt -which encrypts a block or stream of data using the specified encryption key and CryptDecrypt.-which decrypts a block or stream of data using the specified encryption key.
Encrypting a Message
The steps involved in the encryption of message are :
Creating a Key
The symmetric key(session key) used for encryption is obtained by using the following functions.
- CryptAcquireContext-This function is used to get a handle to a particular key container within a particular cryptographic service provider(CSP),which is needed for the function that creates the symmetric key for encryption by using the password.
- phProv parameter of this function gives the handle to cryptographic service provider.
- To use password for creating the symmetric key, first hashing of the password is to be done. This is done by the following two functions. CryptCreateHash- It creates and returns to the calling application a handle to a CSP hash object.
- CryptCreateHash- It creates and returns to the calling application a handle to a CSP hash object.
CryptHashData- This function adds password to the hash object,returned by the above function.
- CryptDeriveKey-This function transforms the password that has been fed to the hash object to the symmetric key, which is used for encryption. Here through the parameter Algid we have to specify the type of symmetric algorithm i.e. RC4.
- The parameter phKey returns the symmetric key that is used for encryption.
Message Encryption
CryptEncrypt-This function performs encryption operation on the message using the symmetric key returned by the above function.
CryptEncrypt
( hKey, hHash, Final, dwFlags, &pbData, &pdwDataLen,dwBufLen)
The parameter pbData returns the encrypted message.

Decrypting a Message
Here also the symmetric key need to be generated for decryption. The steps involved in the decryption of message are :
Creating a Key
The symmetric key(session key) used for decryption is obtained by using the following functions.
- CryptAcquireContext-This function is used to get a handle to a particular key container within a particular cryptographic service provider(CSP),which is needed for the function that creates the symmetric key for decryption by using the password.
phProv parameter of this function gives the handle to cryptographic service provider.
- To use password for creating the symmetric key, first hashing of the password is to be done. This is done by the following two functions.
CryptCreateHash- It creates and returns to the calling application a handle to a CSP hash object.
CryptHashData-This function adds password to the hash object, returned by the above function.
- CryptDeriveKey-This function transforms the password that has been fed to the hash object to the symmetric key, which is used for decryption.
The parameter phKey returns the symmetric key that is used for decryption.
Message Decryption
CryptDecrypt-This function performs decryption operation on the message that is encrypted by CryptEncrypt, by using the symmetric key returned by the above function.
The parameter pbData returns the decrypted message.

When a large amount of data needs to be encrypted/decrypted, it can be done in sections. This is done by calling CryptEncrypt / CryptDecrypt repeatedly. The Final parameter should be set to TRUE only on the last invocation of CryptEncrypt/CryptDecrypt, so the encryption/decryption engine can properly finish the encryption/decryption process. The following extra actions are performed when Final is TRUE:
- If the key is a block cipher key, the data will be padded to a multiple of the block size of the cipher. To find the block size of a cipher, use CryptGetKeyParam to get the KP_BLOCKLEN parameter of the key.
- If the cipher is operating in a chaining mode, the next CryptEncrypt/CryptDecrypt operation will reset the cipher's feedback register to the KP_IV value of the key.
- If the cipher is a stream cipher, the next CryptEncrypt/CryptDecrypt call will reset the cipher to its initial state.
Working Model Of E-mail Encryption
Basic Architecture
The architecture of encryption process comprises the following parts.
- Host Application
- COM Add-in
- Encryption/Decryption DLL

Host Application - Any Office 2000 application can act as a host application. Here, Outlook 2000 acts as the host application. All the COM Add-ins that are registered under this gets loaded whenever this application starts. And the host application uses the methods of IDTExtensibility2 interface, which are implemented in the COM Add-in to communicate with the same.
After registering the COM Add-in, the host application will have the following appearance when it is loaded.

Once we select the option whether to encrypt the mail body or the attachment ,it will show a dialogbox (as shown below) asking to enter the password, which will be used as the public key for encryption/decryption .

The message will be encrypted using this password and the encrypted message will be set back to the mail body as shown below.

To decrypt the encrypted message ,again the user will be asked to enter the password as in encryption and the decrypted message will be set back to the mail body.
COM Add-in -The COM Add-in, a DLL registered for Outlook 2000, is used for adding security option in the menu bar of the Outlook 2000.The COM Add-in makes use of the objects that are exposed by Outlook 2000 Object model for this purpose. The encryption and decryption options in the security calls another DLL which implements the crypto API functions for encryption and decryption to perform the task, whenever the respective options are selected.
The steps involved in extending the mail message window of Outlook 2000 to handle the encryption process in a simpler way, are as follows.
- Sink event operations for the new mail message window of Outlook 2000 to identify it.
- Provide Security custom options to the mail message for encryption / decryption process.
- Sink event operations for the Security option.
- Retrieve the mail message/attachment and perform encryption /decryption.
Sink event operations for the new mail message window
To trap invocation of the new mail message window a new class is derived from the IDISPATCH class providing the event operations. Calling the user-defined function of this class by passing the inspectors object does such mapping .
All the events are exposed as objects by Outlook object model and every such object has GUID. By making use of this GUID, respective events could be mapped. When the event occurs, the callback method invoke() gets fired as a result of mapping.
The equivalent code in VB is as follows:
And then override the new mail message event function.
Provide Security custom options for the mail message
Through the above invoke() function ,specific callback functions are called for doing operations like providing Security custom buttons. Encrypt/Decrypt buttons are created on the Standard Bar for providing security option in VC .
The VB equivalent of the above is as follows.
A Popup item Security with Encrypt/Decrypt sub items is created on the Menu Bar for the security purpose.
Write the following codes in the NewInspector event.
Sink event operations for the Security option
It is also done in the similar way as that of new mail message window. A class derived from the IDISPATCH provides functionalities to sink events. Calling the user-defined function of this class by passing the control object does such mapping.
The VB equivalent of this is as follows.
Declare a CommandBarButton object with withevent option.
Following is the event procedure for event operation of controls:
Retrieve the mail message and perform encryption /decryption
Using the reference of the currently active mail message window, mail item is obtained by the following code.
Finally mail message body is received by using the code below.
To perform encryption / decryption call the user defined function which in turn calls the Encryption/Decryption DLL to encrypt/decrypt the message retrieved from the mail message body.
The equivalent code in VB is as follows.
Using the reference of the currently active mail message window mail item is obtained by the following code.
Finally mail message body is received by using the code below.
To retrieve the data from the attachment use the following code to get the attachment name.
Encryption/Decryption DLL -This DLL have well defined methods, which can implement any symmetric encryption/decryption algorithms. All crypto API functions for encryption and decryption discussed above are implemented as DLL which exposes the functions messageEncrypt and messageDecrypt ,which takes three parameters such as the text to be encrypted/decrypted ,the password to be used as the public key and the output data, for encryption and decryption respectively, that are used by COM Add-in whenever encryption and decryption is selected. Any symmetric encryption algorithm can be used instead of RC4 stream cipher algorithm. To incorporate this change, implement the required algorithm in the above mentioned two functions.
Storage And Retrieval Of Password
All messages that are received in the inbox are uniquely identified by an entry id. To read an encrypted message for the first time, the user will be asked to enter the password which is already have been set between the sender and the receiver, using phone or some other communication medium and this password will be stored with the entry id which is unique with each message in the database . Database for this purpose is created in MS Access. By using this password (only if it matches)the encrypted message is decrypted to the original message. For all subsequent reading of the encrypted message the user will not be asked to enter the password again ,but password is retrieved from the database depending on the entry id of the message and is decrypted. In addition, authentication of the database access is done by prompting the user to enter password for the database for all subsequent readings.
Conclusion
The features of COM Add-in, for desktop applications has enabled the development of extensive solutions more easier .Especially, Outlook can possess much more customized functionalities like simple mail encryption, by means of having good interaction with COM Add-in.

