Blame nss/lib/pk11wrap/pk11sdr.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
#include "seccomon.h"
Packit 40b132
#include "secoid.h"
Packit 40b132
#include "secasn1.h"
Packit 40b132
#include "pkcs11.h"
Packit 40b132
#include "pk11func.h"
Packit 40b132
#include "pk11sdr.h"
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Data structure and template for encoding the result of an SDR operation
Packit 40b132
 *  This is temporary.  It should include the algorithm ID of the encryption mechanism
Packit 40b132
 */
Packit 40b132
struct SDRResult
Packit 40b132
{
Packit 40b132
  SECItem keyid;
Packit 40b132
  SECAlgorithmID alg;
Packit 40b132
  SECItem data;
Packit 40b132
};
Packit 40b132
typedef struct SDRResult SDRResult;
Packit 40b132
Packit 40b132
SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
Packit 40b132
Packit 40b132
static SEC_ASN1Template template[] = {
Packit 40b132
  { SEC_ASN1_SEQUENCE, 0, NULL, sizeof (SDRResult) },
Packit 40b132
  { SEC_ASN1_OCTET_STRING, offsetof(SDRResult, keyid) },
Packit 40b132
  { SEC_ASN1_INLINE | SEC_ASN1_XTRN, offsetof(SDRResult, alg),
Packit 40b132
    SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
Packit 40b132
  { SEC_ASN1_OCTET_STRING, offsetof(SDRResult, data) },
Packit 40b132
  { 0 }
Packit 40b132
};
Packit 40b132
Packit 40b132
static unsigned char keyID[] = {
Packit 40b132
  0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Packit 40b132
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
Packit 40b132
};
Packit 40b132
Packit 40b132
static SECItem keyIDItem = {
Packit 40b132
  0,
Packit 40b132
  keyID,
Packit 40b132
  sizeof keyID
Packit 40b132
};
Packit 40b132
Packit 40b132
/* local utility function for padding an incoming data block
Packit 40b132
 * to the mechanism block size.
Packit 40b132
 */
Packit 40b132
static SECStatus
Packit 40b132
padBlock(SECItem *data, int blockSize, SECItem *result)
Packit 40b132
{
Packit 40b132
  SECStatus rv = SECSuccess;
Packit 40b132
  int padLength;
Packit 40b132
  unsigned int i;
Packit 40b132
Packit 40b132
  result->data = 0;
Packit 40b132
  result->len = 0;
Packit 40b132
Packit 40b132
  /* This algorithm always adds to the block (to indicate the number
Packit 40b132
   * of pad bytes).  So allocate a block large enough.
Packit 40b132
   */
Packit 40b132
  padLength = blockSize - (data->len % blockSize);
Packit 40b132
  result->len = data->len + padLength;
Packit 40b132
  result->data = (unsigned char *)PORT_Alloc(result->len);
Packit 40b132
Packit 40b132
  /* Copy the data */
Packit 40b132
  PORT_Memcpy(result->data, data->data, data->len);
Packit 40b132
Packit 40b132
  /* Add the pad values */
Packit 40b132
  for(i = data->len; i < result->len; i++)
Packit 40b132
    result->data[i] = (unsigned char)padLength;
Packit 40b132
Packit 40b132
  return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
static SECStatus
Packit 40b132
unpadBlock(SECItem *data, int blockSize, SECItem *result)
Packit 40b132
{
Packit 40b132
  SECStatus rv = SECSuccess;
Packit 40b132
  int padLength;
Packit 40b132
  unsigned int i;
Packit 40b132
Packit 40b132
  result->data = 0;
Packit 40b132
  result->len = 0;
Packit 40b132
Packit 40b132
  /* Remove the padding from the end if the input data */
Packit 40b132
  if (data->len == 0 || data->len % blockSize  != 0) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  padLength = data->data[data->len-1];
Packit 40b132
  if (padLength > blockSize) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  /* verify padding */
Packit 40b132
  for (i=data->len - padLength; i < data->len; i++) {
Packit 40b132
    if (data->data[i] != padLength) {
Packit 40b132
	rv = SECFailure;
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
  }
Packit 40b132
Packit 40b132
  result->len = data->len - padLength;
Packit 40b132
  result->data = (unsigned char *)PORT_Alloc(result->len);
Packit 40b132
  if (!result->data) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  PORT_Memcpy(result->data, data->data, result->len);
Packit 40b132
Packit 40b132
  if (padLength < 2) {
Packit 40b132
    return SECWouldBlock;
Packit 40b132
  }
Packit 40b132
Packit 40b132
loser:
Packit 40b132
  return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
static PRLock *pk11sdrLock = NULL;
Packit 40b132
Packit 40b132
void
Packit 40b132
pk11sdr_Init (void)
Packit 40b132
{
Packit 40b132
   pk11sdrLock = PR_NewLock();
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
pk11sdr_Shutdown(void)
Packit 40b132
{
Packit 40b132
    if (pk11sdrLock) {
Packit 40b132
	PR_DestroyLock(pk11sdrLock);
Packit 40b132
	pk11sdrLock = NULL;
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * PK11SDR_Encrypt
Packit 40b132
 *  Encrypt a block of data using the symmetric key identified.  The result
Packit 40b132
 *  is an ASN.1 (DER) encoded block of keyid, params and data.
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11SDR_Encrypt(SECItem *keyid, SECItem *data, SECItem *result, void *cx)
Packit 40b132
{
Packit 40b132
  SECStatus rv = SECSuccess;
Packit 40b132
  PK11SlotInfo *slot = 0;
Packit 40b132
  PK11SymKey *key = 0;
Packit 40b132
  SECItem *params = 0;
Packit 40b132
  PK11Context *ctx = 0;
Packit 40b132
  CK_MECHANISM_TYPE type;
Packit 40b132
  SDRResult sdrResult;
Packit 40b132
  SECItem paddedData;
Packit 40b132
  SECItem *pKeyID;
Packit 40b132
  PLArenaPool *arena = 0;
Packit 40b132
Packit 40b132
  /* Initialize */
Packit 40b132
  paddedData.len = 0;
Packit 40b132
  paddedData.data = 0;
Packit 40b132
Packit 40b132
  arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
Packit 40b132
  if (!arena) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  /* 1. Locate the requested keyid, or the default key (which has a keyid)
Packit 40b132
   * 2. Create an encryption context
Packit 40b132
   * 3. Encrypt
Packit 40b132
   * 4. Encode the results (using ASN.1)
Packit 40b132
   */
Packit 40b132
Packit 40b132
  slot = PK11_GetInternalKeySlot();
Packit 40b132
  if (!slot) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  /* Use triple-DES */
Packit 40b132
  type = CKM_DES3_CBC;
Packit 40b132
Packit 40b132
  /*
Packit 40b132
   * Login to the internal token before we look for the key, otherwise we
Packit 40b132
   * won't find it.
Packit 40b132
   */
Packit 40b132
  rv = PK11_Authenticate(slot, PR_TRUE, cx);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  /* Find the key to use */
Packit 40b132
  pKeyID = keyid;
Packit 40b132
  if (pKeyID->len == 0) {
Packit 40b132
	  pKeyID = &keyIDItem;  /* Use default value */
Packit 40b132
Packit 40b132
	  /* put in a course lock to prevent a race between not finding the 
Packit 40b132
	   * key and creating  one.
Packit 40b132
	   */
Packit 40b132
Packit 40b132
	  if (pk11sdrLock) PR_Lock(pk11sdrLock);
Packit 40b132
Packit 40b132
	  /* Try to find the key */
Packit 40b132
	  key = PK11_FindFixedKey(slot, type, pKeyID, cx);
Packit 40b132
	  
Packit 40b132
	  /* If the default key doesn't exist yet, try to create it */
Packit 40b132
	  if (!key) key = PK11_GenDES3TokenKey(slot, pKeyID, cx);
Packit 40b132
	  if (pk11sdrLock) PR_Unlock(pk11sdrLock);
Packit 40b132
  } else {
Packit 40b132
	  key = PK11_FindFixedKey(slot, type, pKeyID, cx);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  if (!key) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  params = PK11_GenerateNewParam(type, key);
Packit 40b132
  if (!params) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  ctx = PK11_CreateContextBySymKey(type, CKA_ENCRYPT, key, params);
Packit 40b132
  if (!ctx) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  rv = padBlock(data, PK11_GetBlockSize(type, 0), &paddedData);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  sdrResult.data.len = paddedData.len;
Packit 40b132
  sdrResult.data.data = (unsigned char *)PORT_ArenaAlloc(arena, sdrResult.data.len);
Packit 40b132
Packit 40b132
  rv = PK11_CipherOp(ctx, sdrResult.data.data, (int*)&sdrResult.data.len, sdrResult.data.len,
Packit 40b132
                     paddedData.data, paddedData.len);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  PK11_Finalize(ctx);
Packit 40b132
Packit 40b132
  sdrResult.keyid = *pKeyID;
Packit 40b132
Packit 40b132
  rv = PK11_ParamToAlgid(SEC_OID_DES_EDE3_CBC, params, arena, &sdrResult.alg);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  if (!SEC_ASN1EncodeItem(0, result, &sdrResult, template)) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
loser:
Packit 40b132
  SECITEM_ZfreeItem(&paddedData, PR_FALSE);
Packit 40b132
  if (arena) PORT_FreeArena(arena, PR_TRUE);
Packit 40b132
  if (ctx) PK11_DestroyContext(ctx, PR_TRUE);
Packit 40b132
  if (params) SECITEM_ZfreeItem(params, PR_TRUE);
Packit 40b132
  if (key) PK11_FreeSymKey(key);
Packit 40b132
  if (slot) PK11_FreeSlot(slot);
Packit 40b132
Packit 40b132
  return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/* decrypt a block */
Packit 40b132
static SECStatus
Packit 40b132
pk11Decrypt(PK11SlotInfo *slot, PLArenaPool *arena, 
Packit 40b132
	    CK_MECHANISM_TYPE type, PK11SymKey *key, 
Packit 40b132
	    SECItem *params, SECItem *in, SECItem *result)
Packit 40b132
{
Packit 40b132
  PK11Context *ctx = 0;
Packit 40b132
  SECItem paddedResult;
Packit 40b132
  SECStatus rv;
Packit 40b132
Packit 40b132
  paddedResult.len = 0;
Packit 40b132
  paddedResult.data = 0;
Packit 40b132
Packit 40b132
  ctx = PK11_CreateContextBySymKey(type, CKA_DECRYPT, key, params);
Packit 40b132
  if (!ctx) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  paddedResult.len = in->len;
Packit 40b132
  paddedResult.data = PORT_ArenaAlloc(arena, paddedResult.len);
Packit 40b132
Packit 40b132
  rv = PK11_CipherOp(ctx, paddedResult.data, 
Packit 40b132
			(int*)&paddedResult.len, paddedResult.len,
Packit 40b132
			in->data, in->len);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  PK11_Finalize(ctx);
Packit 40b132
Packit 40b132
  /* Remove the padding */
Packit 40b132
  rv = unpadBlock(&paddedResult, PK11_GetBlockSize(type, 0), result);
Packit 40b132
  if (rv) goto loser;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
  if (ctx) PK11_DestroyContext(ctx, PR_TRUE);
Packit 40b132
  return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * PK11SDR_Decrypt
Packit 40b132
 *  Decrypt a block of data produced by PK11SDR_Encrypt.  The key used is identified
Packit 40b132
 *  by the keyid field within the input.
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11SDR_Decrypt(SECItem *data, SECItem *result, void *cx)
Packit 40b132
{
Packit 40b132
  SECStatus rv = SECSuccess;
Packit 40b132
  PK11SlotInfo *slot = 0;
Packit 40b132
  PK11SymKey *key = 0;
Packit 40b132
  CK_MECHANISM_TYPE type;
Packit 40b132
  SDRResult sdrResult;
Packit 40b132
  SECItem *params = 0;
Packit 40b132
  SECItem possibleResult = { 0, NULL, 0 };
Packit 40b132
  PLArenaPool *arena = 0;
Packit 40b132
Packit 40b132
  arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
Packit 40b132
  if (!arena) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  /* Decode the incoming data */
Packit 40b132
  memset(&sdrResult, 0, sizeof sdrResult);
Packit 40b132
  rv = SEC_QuickDERDecodeItem(arena, &sdrResult, template, data);
Packit 40b132
  if (rv != SECSuccess) goto loser;  /* Invalid format */
Packit 40b132
Packit 40b132
  /* Find the slot and key for the given keyid */
Packit 40b132
  slot = PK11_GetInternalKeySlot();
Packit 40b132
  if (!slot) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  rv = PK11_Authenticate(slot, PR_TRUE, cx);
Packit 40b132
  if (rv != SECSuccess) goto loser;
Packit 40b132
Packit 40b132
  /* Get the parameter values from the data */
Packit 40b132
  params = PK11_ParamFromAlgid(&sdrResult.alg);
Packit 40b132
  if (!params) { rv = SECFailure; goto loser; }
Packit 40b132
Packit 40b132
  /* Use triple-DES (Should look up the algorithm) */
Packit 40b132
  type = CKM_DES3_CBC;
Packit 40b132
  key = PK11_FindFixedKey(slot, type, &sdrResult.keyid, cx);
Packit 40b132
  if (!key) { 
Packit 40b132
	rv = SECFailure;  
Packit 40b132
  } else {
Packit 40b132
	rv = pk11Decrypt(slot, arena, type, key, params, 
Packit 40b132
			&sdrResult.data, result);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  /*
Packit 40b132
   * if the pad value was too small (1 or 2), then it's statistically
Packit 40b132
   * 'likely' that (1 in 256) that we may not have the correct key.
Packit 40b132
   * Check the other keys for a better match. If we find none, use
Packit 40b132
   * this result.
Packit 40b132
   */
Packit 40b132
  if (rv == SECWouldBlock) {
Packit 40b132
	possibleResult = *result;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  /*
Packit 40b132
   * handle the case where your key indicies may have been broken
Packit 40b132
   */
Packit 40b132
  if (rv != SECSuccess) {
Packit 40b132
	PK11SymKey *keyList = PK11_ListFixedKeysInSlot(slot, NULL, cx);
Packit 40b132
	PK11SymKey *testKey = NULL;
Packit 40b132
	PK11SymKey *nextKey = NULL;
Packit 40b132
Packit 40b132
	for (testKey = keyList; testKey; 
Packit 40b132
				testKey = PK11_GetNextSymKey(testKey)) {
Packit 40b132
	    rv = pk11Decrypt(slot, arena, type, testKey, params, 
Packit 40b132
			     &sdrResult.data, result);
Packit 40b132
	    if (rv == SECSuccess) {
Packit 40b132
		break;
Packit 40b132
	    } 
Packit 40b132
	    /* found a close match. If it's our first remember it */
Packit 40b132
	    if (rv == SECWouldBlock) {
Packit 40b132
		if (possibleResult.data) {
Packit 40b132
		    /* this is unlikely but possible. If we hit this condition,
Packit 40b132
		     * we have no way of knowing which possibility to prefer.
Packit 40b132
		     * in this case we just match the key the application
Packit 40b132
		     * thought was the right one */
Packit 40b132
		    SECITEM_ZfreeItem(result, PR_FALSE);
Packit 40b132
		} else {
Packit 40b132
		    possibleResult = *result;
Packit 40b132
		}
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/* free the list */
Packit 40b132
	for (testKey = keyList; testKey; testKey = nextKey) {
Packit 40b132
	    nextKey = PK11_GetNextSymKey(testKey);
Packit 40b132
	    PK11_FreeSymKey(testKey);
Packit 40b132
	}
Packit 40b132
  }
Packit 40b132
Packit 40b132
  /* we didn't find a better key, use the one with a small pad value */
Packit 40b132
  if ((rv != SECSuccess) && (possibleResult.data)) {
Packit 40b132
	*result = possibleResult;
Packit 40b132
	possibleResult.data = NULL;
Packit 40b132
	rv = SECSuccess;
Packit 40b132
  }
Packit 40b132
Packit 40b132
loser:
Packit 40b132
  if (arena) PORT_FreeArena(arena, PR_TRUE);
Packit 40b132
  if (key) PK11_FreeSymKey(key);
Packit 40b132
  if (params) SECITEM_ZfreeItem(params, PR_TRUE);
Packit 40b132
  if (slot) PK11_FreeSlot(slot);
Packit 40b132
  if (possibleResult.data) SECITEM_ZfreeItem(&possibleResult, PR_FALSE);
Packit 40b132
Packit 40b132
  return rv;
Packit 40b132
}