jump to article
...intermittent thoughts

Existence of a Notes Encryption Key

Today I was enhancing an existing application which requires an encryption key to work correctly. One of the problems to fix was how the application responded if that required encryption key was missing: the application stopped with an error message showing a callstack. While this was very bad, I was not that much concerned, because the application is used only by some of my developer colleagues. Well, thats no excuse for a runtime error, but I know they are not that shocked like an enduser if they see an error message.

Tho fix the problem I simply had to check, whether the current user has the required encryption key or not. To accomplish that simple sounding demand I checked the Designer Help but did not find a solution. So I asked the Google oracle but it came up with tons of PGP stuff and I got bored very fast. I then asked Daniel but he he also had no prompt answer (but an idea). While chatting about it, I came to a solution - I want to share with you. It's a simple function you may use in your own Scripts. With that function, it is possible to ask if the current user has the encryption key given by the only on eparameter of the function. It retunrs only true or false - so it is really simple to use:


Private Function IsEncryptionKeyAvailable( Byval key$ ) As Boolean
'/**
' * Checks whether the current user has an Encryption Key or not
' * check results are cached during runtime of the module, so multiple requests to the same key are very performant
' * @param key the name of the Key to check
' * @error Every Error is thrown by the repeater callstack technique
' * @author Michael Gollmick
' * @Version 1.0
' */
On Error Goto Repeater

Static doc As NotesDocument
Static encryptionkeyList List As Boolean
Dim it As NotesItem
Dim keyIndex$
Dim s As NotesSession
Dim testValue As Boolean

key = Trim( key )
If Len( key ) = 0 Then
IsEncryptionKeyAvailable = True
Exit Function
End If

keyIndex = Ucase( Key )
If Not Iselement( encryptionkeyList( keyIndex ) ) Then
' we did not check this key yet, so we do it now!
If doc Is Nothing Then
Set s = New NotesSession
Set doc = New NotesDocument( s.CurrentDatabase )
Set it = doc.ReplaceItemValue( "Key", "" )
it.IsProtected = True
it.IsSigned = True
it.IsEncrypted = True
End If
doc.EncryptionKeys = key
Call doc.Encrypt
' if the previous line did not throw an error, we have the key,
' so we set the test value top true (which defaults to false)
testValue = True
KeyNotAvailableForUser:
' if we have run into an error, testValue is still on false and we can store that value      
encryptionkeyList( keyIndex ) = testValue
End If

' now that we surely have the requested key in the list, we return it from the list
' that's slightly slower than returning from within the code, but is better readable and
' maintainable
IsEncryptionKeyAvailable = encryptionkeyList( keyIndex )

Exit Function
Repeater:
If Err = 4000 Then Resume KeyNotAvailableForUser
Error Err, Error & {
::}
& Getthreadinfo(1) & {():} & Erl
End Function


The function is quite handy - results are even cached, so calling it several times with the same key is quite performant (lists used the right way are sooo performant). Well, some parts could possibly be even more performant (we speak about just a few CPU ticks) but I have chosen the current coding style to keep it readable for others.

Simply copy, paste and use it - but drop me a note then.

Add Comment
 
Subject:
   
Name:
E-mail:
Web Site:
 
Comment:  (No HTML - Links will be converted if prefixed http://)
 
Remember Me?