Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2006 Pekka Riikonen
+ Copyright (C) 1997 - 2007 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
{
SilcUInt32 i, len;
unsigned char *data = NULL;
- SilcBool ret;
+ int ret;
SILC_LOG_DEBUG(("Parsing SILC public key file"));
ret_public_key);
silc_free(data);
- return ret;
+ return ret ? TRUE : FALSE;
}
/* Imports SILC protocol style public key */
-SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
- SilcUInt32 key_len,
- void **ret_public_key)
+int silc_pkcs_silc_import_public_key(unsigned char *key,
+ SilcUInt32 key_len,
+ void **ret_public_key)
{
const SilcPKCSAlgorithm *pkcs;
SilcBufferStruct buf, alg_key;
SILC_LOG_DEBUG(("Parsing SILC public key"));
if (!ret_public_key)
- return FALSE;
+ return 0;
silc_buffer_set(&buf, key, key_len);
if (ret == -1)
goto err;
+ /* Backwards compatibility */
+ if (totlen == key_len)
+ totlen -= 4;
+
if (totlen + 4 != key_len)
goto err;
}
/* Import PKCS algorithm public key */
- if (pkcs->import_public_key)
- if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key),
- &silc_pubkey->public_key))
- goto err;
+ if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key),
+ &silc_pubkey->public_key))
+ goto err;
silc_free(pkcs_name);
silc_free(ident);
*ret_public_key = silc_pubkey;
- return TRUE;
+ return key_len;
err:
silc_free(pkcs_name);
silc_free(silc_pubkey);
if (asn1)
silc_asn1_free(asn1);
- return FALSE;
+ return 0;
}
/* Exports public key as SILC protocol style public key file */
SilcUInt32 blocklen;
unsigned char tmp[32], keymat[64], *data = NULL;
SilcUInt32 i, len, magic, mac_len;
- SilcBool ret;
+ int ret;
SILC_LOG_DEBUG(("Parsing SILC private key file"));
/* Check file magic */
SILC_GET32_MSB(magic, filedata);
if (magic != SILC_PKCS_PRIVATE_KEY_MAGIC) {
- SILC_LOG_ERROR(("Private key does not have correct magic"));
+ SILC_LOG_DEBUG(("Private key does not have correct magic"));
return FALSE;
}
silc_hash_final(sha1, keymat + 16);
/* Set the key to the cipher */
- silc_cipher_set_key(aes, keymat, 256);
+ silc_cipher_set_key(aes, keymat, 256, FALSE);
/* First, verify the MAC of the private key data */
mac_len = silc_hmac_len(sha1hmac);
silc_free(data);
- return ret;
+ return ret ? TRUE : FALSE;
}
/* Private key version */
/* Imports SILC implementation style private key */
-SilcBool silc_pkcs_silc_import_private_key(unsigned char *key,
- SilcUInt32 key_len,
- void **ret_private_key)
+int silc_pkcs_silc_import_private_key(unsigned char *key,
+ SilcUInt32 key_len,
+ void **ret_private_key)
{
SilcBufferStruct buf;
const SilcPKCSAlgorithm *pkcs;
SILC_LOG_DEBUG(("Parsing SILC private key"));
if (!ret_private_key)
- return FALSE;
+ return 0;
silc_buffer_set(&buf, key, key_len);
/* Parse the RSA SILC private key */
SilcBufferStruct k;
SilcMPInt n, e, d, dp, dq, qp, p, q;
- SilcMPInt version;
unsigned char *tmp;
SilcUInt32 len, ver;
}
/* Encode to PKCS #1 format */
- silc_mp_init(&version);
- silc_mp_set_ui(&version, 0);
memset(&alg_key, 0, sizeof(alg_key));
if (!silc_asn1_encode(asn1, &alg_key,
SILC_ASN1_SEQUENCE,
- SILC_ASN1_INT(&version),
+ SILC_ASN1_SHORT_INT(0),
SILC_ASN1_INT(&n),
SILC_ASN1_INT(&e),
SILC_ASN1_INT(&d),
SILC_ASN1_END, SILC_ASN1_END))
goto err;
- silc_mp_uninit(&version);
silc_mp_uninit(&n);
silc_mp_uninit(&e);
silc_mp_uninit(&e);
}
/* Import PKCS algorithm private key */
- if (pkcs->import_private_key)
- if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key),
- &silc_privkey->private_key))
- goto err;
+ if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key),
+ &silc_privkey->private_key))
+ goto err;
silc_free(pkcs_name);
silc_asn1_free(asn1);
*ret_private_key = silc_privkey;
- return TRUE;
+ return key_len;
err:
silc_free(pkcs_name);
silc_free(silc_privkey);
if (asn1)
silc_asn1_free(asn1);
- return FALSE;
+ return 0;
}
/* Exports private key as SILC implementation style private key file */
silc_hash_final(sha1, keymat + 16);
/* Set the key to the cipher */
- silc_cipher_set_key(aes, keymat, 256);
+ silc_cipher_set_key(aes, keymat, 256, TRUE);
/* Encode the buffer to be encrypted. Add padding to it too, at least
block size of the cipher. */
SilcUInt32 src_len,
unsigned char *dst,
SilcUInt32 dst_size,
- SilcUInt32 *ret_dst_len)
+ SilcUInt32 *ret_dst_len,
+ SilcRng rng)
{
SilcSILCPublicKey silc_pubkey = public_key;
return silc_pubkey->pkcs->encrypt(silc_pubkey->public_key,
src, src_len,
- dst, dst_size, ret_dst_len);
+ dst, dst_size, ret_dst_len, rng);
}
/* Decrypts as specified in SILC protocol specification */