1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is the Netscape security libraries.
16
* The Initial Developer of the Original Code is
17
* Netscape Communications Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 1994-2000
19
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
46
* Data structure and template for encoding the result of an SDR operation
47
* This is temporary. It should include the algorithm ID of the encryption mechanism
55
typedef struct SDRResult SDRResult;
57
static SEC_ASN1Template template[] = {
58
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof (SDRResult) },
59
{ SEC_ASN1_OCTET_STRING, offsetof(SDRResult, keyid) },
60
{ SEC_ASN1_INLINE, offsetof(SDRResult, alg), SECOID_AlgorithmIDTemplate },
61
{ SEC_ASN1_OCTET_STRING, offsetof(SDRResult, data) },
65
static unsigned char keyID[] = {
66
0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
70
static SECItem keyIDItem = {
76
/* local utility function for padding an incoming data block
77
* to the mechanism block size.
80
padBlock(SECItem *data, int blockSize, SECItem *result)
82
SECStatus rv = SECSuccess;
89
/* This algorithm always adds to the block (to indicate the number
90
* of pad bytes). So allocate a block large enough.
92
padLength = blockSize - (data->len % blockSize);
93
result->len = data->len + padLength;
94
result->data = (unsigned char *)PORT_Alloc(result->len);
97
PORT_Memcpy(result->data, data->data, data->len);
99
/* Add the pad values */
100
for(i = data->len; i < result->len; i++)
101
result->data[i] = (unsigned char)padLength;
107
unpadBlock(SECItem *data, int blockSize, SECItem *result)
109
SECStatus rv = SECSuccess;
115
/* Remove the padding from the end if the input data */
116
if (data->len == 0 || data->len % blockSize != 0) { rv = SECFailure; goto loser; }
118
padLength = data->data[data->len-1];
119
if (padLength > blockSize) { rv = SECFailure; goto loser; }
121
result->len = data->len - padLength;
122
result->data = (unsigned char *)PORT_Alloc(result->len);
123
if (!result->data) { rv = SECFailure; goto loser; }
125
PORT_Memcpy(result->data, data->data, result->len);
131
static PRLock *pk11sdrLock = NULL;
136
pk11sdrLock = PR_NewLock();
140
pk11sdr_Shutdown(void)
143
PR_DestroyLock(pk11sdrLock);
150
* Encrypt a block of data using the symmetric key identified. The result
151
* is an ASN.1 (DER) encoded block of keyid, params and data.
154
PK11SDR_Encrypt(SECItem *keyid, SECItem *data, SECItem *result, void *cx)
156
SECStatus rv = SECSuccess;
157
PK11SlotInfo *slot = 0;
160
PK11Context *ctx = 0;
161
CK_MECHANISM_TYPE type;
165
PLArenaPool *arena = 0;
171
arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
172
if (!arena) { rv = SECFailure; goto loser; }
174
/* 1. Locate the requested keyid, or the default key (which has a keyid)
175
* 2. Create an encryption context
177
* 4. Encode the results (using ASN.1)
180
slot = PK11_GetInternalKeySlot();
181
if (!slot) { rv = SECFailure; goto loser; }
187
* Login to the internal token before we look for the key, otherwise we
190
rv = PK11_Authenticate(slot, PR_TRUE, cx);
191
if (rv != SECSuccess) goto loser;
193
/* Find the key to use */
195
if (pKeyID->len == 0) {
196
pKeyID = &keyIDItem; /* Use default value */
198
/* put in a course lock to prevent a race between not finding the
199
* key and creating one.
202
if (pk11sdrLock) PR_Lock(pk11sdrLock);
204
/* Try to find the key */
205
key = PK11_FindFixedKey(slot, type, pKeyID, cx);
207
/* If the default key doesn't exist yet, try to create it */
208
if (!key) key = PK11_GenDES3TokenKey(slot, pKeyID, cx);
209
if (pk11sdrLock) PR_Unlock(pk11sdrLock);
211
key = PK11_FindFixedKey(slot, type, pKeyID, cx);
214
if (!key) { rv = SECFailure; goto loser; }
216
params = PK11_GenerateNewParam(type, key);
217
if (!params) { rv = SECFailure; goto loser; }
219
ctx = PK11_CreateContextBySymKey(type, CKA_ENCRYPT, key, params);
220
if (!ctx) { rv = SECFailure; goto loser; }
222
rv = padBlock(data, PK11_GetBlockSize(type, 0), &paddedData);
223
if (rv != SECSuccess) goto loser;
225
sdrResult.data.len = paddedData.len;
226
sdrResult.data.data = (unsigned char *)PORT_ArenaAlloc(arena, sdrResult.data.len);
228
rv = PK11_CipherOp(ctx, sdrResult.data.data, (int*)&sdrResult.data.len, sdrResult.data.len,
229
paddedData.data, paddedData.len);
230
if (rv != SECSuccess) goto loser;
234
sdrResult.keyid = *pKeyID;
236
rv = PK11_ParamToAlgid(SEC_OID_DES_EDE3_CBC, params, arena, &sdrResult.alg);
237
if (rv != SECSuccess) goto loser;
239
if (!SEC_ASN1EncodeItem(0, result, &sdrResult, template)) { rv = SECFailure; goto loser; }
242
SECITEM_ZfreeItem(&paddedData, PR_FALSE);
243
if (arena) PORT_FreeArena(arena, PR_TRUE);
244
if (ctx) PK11_DestroyContext(ctx, PR_TRUE);
245
if (params) SECITEM_ZfreeItem(params, PR_TRUE);
246
if (key) PK11_FreeSymKey(key);
247
if (slot) PK11_FreeSlot(slot);
254
* Decrypt a block of data produced by PK11SDR_Encrypt. The key used is identified
255
* by the keyid field within the input.
258
PK11SDR_Decrypt(SECItem *data, SECItem *result, void *cx)
260
SECStatus rv = SECSuccess;
261
PK11SlotInfo *slot = 0;
263
PK11Context *ctx = 0;
264
CK_MECHANISM_TYPE type;
267
SECItem paddedResult;
268
PLArenaPool *arena = 0;
270
paddedResult.len = 0;
271
paddedResult.data = 0;
273
arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
274
if (!arena) { rv = SECFailure; goto loser; }
276
/* Decode the incoming data */
277
memset(&sdrResult, 0, sizeof sdrResult);
278
rv = SEC_QuickDERDecodeItem(arena, &sdrResult, template, data);
279
if (rv != SECSuccess) goto loser; /* Invalid format */
281
/* Find the slot and key for the given keyid */
282
slot = PK11_GetInternalKeySlot();
283
if (!slot) { rv = SECFailure; goto loser; }
285
rv = PK11_Authenticate(slot, PR_TRUE, cx);
286
if (rv != SECSuccess) goto loser;
288
/* Use triple-DES (Should look up the algorithm) */
290
key = PK11_FindFixedKey(slot, type, &sdrResult.keyid, cx);
291
if (!key) { rv = SECFailure; goto loser; }
293
/* Get the parameter values from the data */
294
params = PK11_ParamFromAlgid(&sdrResult.alg);
295
if (!params) { rv = SECFailure; goto loser; }
297
ctx = PK11_CreateContextBySymKey(type, CKA_DECRYPT, key, params);
298
if (!ctx) { rv = SECFailure; goto loser; }
300
paddedResult.len = sdrResult.data.len;
301
paddedResult.data = PORT_ArenaAlloc(arena, paddedResult.len);
303
rv = PK11_CipherOp(ctx, paddedResult.data, (int*)&paddedResult.len, paddedResult.len,
304
sdrResult.data.data, sdrResult.data.len);
305
if (rv != SECSuccess) goto loser;
309
/* Remove the padding */
310
rv = unpadBlock(&paddedResult, PK11_GetBlockSize(type, 0), result);
314
/* SECITEM_ZfreeItem(&paddedResult, PR_FALSE); */
315
if (arena) PORT_FreeArena(arena, PR_TRUE);
316
if (ctx) PK11_DestroyContext(ctx, PR_TRUE);
317
if (key) PK11_FreeSymKey(key);
318
if (params) SECITEM_ZfreeItem(params, PR_TRUE);
319
if (slot) PK11_FreeSlot(slot);