2 * @file sipe-crypt-nss.c
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 pier11 <pier11@operamail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Cypher routines implementation based on NSS.
33 * Work around a compiler error in NSS 3.13.x. Let's hope they fix it for
34 * 3.14.x. See also: https://bugzilla.mozilla.org/show_bug.cgi?id=702090
36 #if (NSS_VMAJOR == 3) && (NSS_VMINOR == 13)
37 #define __GNUC_MINOR __GNUC_MINOR__
41 #include "sipe-common.h"
42 #include "sipe-backend.h"
43 #include "sipe-crypt.h"
45 /* NSS specific initialization/shutdown */
46 void sipe_crypto_init(SIPE_UNUSED_PARAMETER gboolean production_mode
)
48 if (!NSS_IsInitialized()) {
50 * I have a bad feeling about this: according to the NSS
51 * documentation, NSS can only be initialized once.
52 * Unfortunately there seems to be no way to initialize a
53 * "NSS context" that could then be used by the SIPE code
54 * to avoid colliding with other NSS users.
56 * This seems to work, so it'll have to do for now.
58 * It might also be required to move this to the backend
59 * so that the backend code can decide when it is OK to
63 SIPE_DEBUG_INFO_NOFORMAT("NSS initialised");
67 void sipe_crypto_shutdown(void)
69 /* do nothing for NSS.
70 * We don't want accedently switch off NSS possibly used by other plugin -
71 * ssl-nss in Pidgin for example.
78 sipe_crypt_ctx_create(CK_MECHANISM_TYPE cipherMech
, const guchar
*key
, gsize key_length
)
85 PK11Context
* EncContext
;
88 slot
= PK11_GetBestSlot(cipherMech
, NULL
);
90 keyItem
.type
= siBuffer
;
91 keyItem
.data
= (unsigned char *)key
;
92 keyItem
.len
= key_length
;
94 SymKey
= PK11_ImportSymKey(slot
, cipherMech
, PK11_OriginUnwrap
, CKA_ENCRYPT
, &keyItem
, NULL
);
96 /* Parameter for crypto context */
97 ivItem
.type
= siBuffer
;
100 SecParam
= PK11_ParamFromIV(cipherMech
, &ivItem
);
102 EncContext
= PK11_CreateContextBySymKey(cipherMech
, CKA_ENCRYPT
, SymKey
, SecParam
);
104 PK11_FreeSymKey(SymKey
);
105 SECITEM_FreeItem(SecParam
, PR_TRUE
);
112 sipe_crypt_ctx_encrypt(PK11Context
* EncContext
, const guchar
*in
, gsize length
, guchar
*out
)
116 PK11_CipherOp(EncContext
, out
, &tmp1_outlen
, length
, (unsigned char *)in
, length
);
120 sipe_crypt_ctx_destroy(PK11Context
* EncContext
)
122 PK11_DestroyContext(EncContext
, PR_TRUE
);
126 sipe_crypt(CK_MECHANISM_TYPE cipherMech
,
127 const guchar
*key
, gsize key_length
,
128 const guchar
*plaintext
, gsize plaintext_length
,
129 guchar
*encrypted_text
)
133 EncContext
= sipe_crypt_ctx_create(cipherMech
, key
, key_length
);
134 sipe_crypt_ctx_encrypt(EncContext
, plaintext
, plaintext_length
, encrypted_text
);
135 sipe_crypt_ctx_destroy(EncContext
);
142 sipe_crypt_des(const guchar
*key
,
143 const guchar
*plaintext
, gsize plaintext_length
,
144 guchar
*encrypted_text
)
146 sipe_crypt(CKM_DES_ECB
, key
, 8, plaintext
, plaintext_length
, encrypted_text
);
150 sipe_crypt_rc4(const guchar
*key
, gsize key_length
,
151 const guchar
*plaintext
, gsize plaintext_length
,
152 guchar
*encrypted_text
)
154 sipe_crypt(CKM_RC4
, key
, key_length
, plaintext
, plaintext_length
, encrypted_text
);
158 sipe_crypt_rsa_encrypt(gpointer
public, gsize modulus_length
,
159 const guchar
*plaintext
,
160 guchar
*encrypted_text
)
162 SECStatus result
= PK11_PubEncryptRaw(public,
163 encrypted_text
, (guchar
*) plaintext
,
164 modulus_length
, NULL
);
165 return(result
== SECSuccess
);
169 sipe_crypt_rsa_decrypt(gpointer
private, gsize modulus_length
,
170 const guchar
*encrypted_text
,
174 SECStatus result
= PK11_PubDecryptRaw(private,
175 (guchar
*) encrypted_text
, &length
, modulus_length
,
176 plaintext
, modulus_length
);
177 return((result
== SECSuccess
) && (length
== modulus_length
));
180 guchar
*sipe_crypt_rsa_sign(gpointer
private,
181 const guchar
*digest
, gsize digest_length
,
182 gsize
*signature_length
)
188 length
= PK11_SignatureLen(private);
189 if (length
< 0) return(NULL
);
191 /* digest to sign (= encrypt) with private key */
192 digItem
.data
= (guchar
*) digest
;
193 digItem
.len
= digest_length
;
196 sigItem
.data
= g_malloc(length
);
197 sigItem
.len
= length
;
199 length
= PK11_Sign(private, &sigItem
, &digItem
);
200 if (length
!= SECSuccess
) {
201 g_free(sigItem
.data
);
205 *signature_length
= sigItem
.len
;
206 return(sigItem
.data
);
209 gboolean
sipe_crypt_verify_rsa(gpointer
public,
210 const guchar
*digest
, gsize digest_length
,
211 const guchar
*signature
, gsize signature_length
)
216 /* digest to verify against */
217 digItem
.data
= (guchar
*) digest
;
218 digItem
.len
= digest_length
;
220 /* signature to decrypt with public key -> digest to compare */
221 sigItem
.data
= (guchar
*) signature
;
222 sigItem
.len
= signature_length
;
224 return(PK11_Verify(public, &sigItem
, &digItem
, NULL
) == SECSuccess
);
228 /* Stream RC4 cipher for file transfer */
230 sipe_crypt_ft_start(const guchar
*key
)
232 return sipe_crypt_ctx_create(CKM_RC4
, key
, 16);
236 sipe_crypt_ft_stream(gpointer context
,
237 const guchar
*in
, gsize length
,
240 sipe_crypt_ctx_encrypt(context
, in
, length
, out
);
244 sipe_crypt_ft_destroy(gpointer context
)
246 sipe_crypt_ctx_destroy(context
);
250 * Stream RC4 cipher for TLS
252 * basically the same as for FT, but with variable key length
254 gpointer
sipe_crypt_tls_start(const guchar
*key
, gsize key_length
)
256 return sipe_crypt_ctx_create(CKM_RC4
, key
, key_length
);
259 void sipe_crypt_tls_stream(gpointer context
,
260 const guchar
*in
, gsize length
,
263 sipe_crypt_ctx_encrypt(context
, in
, length
, out
);
266 void sipe_crypt_tls_destroy(gpointer context
)
268 sipe_crypt_ctx_destroy(context
);