Exemplo n.º 1
0
/* static struct RSAKey *rsa2_openssh_createkey(unsigned char **blob, int *len)
{
	const char **b = (const char **) blob;
	struct RSAKey *rsa = new RSAKey;

	rsa->comment = NULL;
	rsa->modulus = getmp(b, len);
	rsa->exponent = getmp(b, len);
	rsa->private_exponent = getmp(b, len);
	rsa->iqmp = getmp(b, len);
	rsa->p = getmp(b, len);
	rsa->q = getmp(b, len);

	if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
			!rsa->iqmp || !rsa->p || !rsa->q)
	{
		delete rsa;
		return NULL;
	}

	return rsa;
}

static int rsa2_openssh_fmtkey( struct RSAKey *rsa, unsigned char *blob, int len )
{
	int bloblen =
		ssh2_bignum_length(rsa->modulus) +
		ssh2_bignum_length(rsa->exponent) +
		ssh2_bignum_length(rsa->private_exponent) +
		ssh2_bignum_length(rsa->iqmp) +
		ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);

	if (bloblen > len)
		return bloblen;

	bloblen = 0;
#define ENC(x) \
	PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
		for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
	int i;
	ENC(rsa->modulus);
	ENC(rsa->exponent);
	ENC(rsa->private_exponent);
	ENC(rsa->iqmp);
	ENC(rsa->p);
	ENC(rsa->q);

	return bloblen;
}
*/
int rsa2_pubkey_bits( const CString &blob )
{
	RSAKey rsa;
	if( !rsa.LoadFromPublicBlob( blob ) )
		return 0;

	return bignum_bitcount(rsa.modulus);
}
Exemplo n.º 2
0
RSAKey generateRSAKey()
{
	RSA *rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);
	RSAKey key;
	if(rsa)
		key.setData(rsa);
	return key;
}
Exemplo n.º 3
0
		EVPPKey EVPPKey::createFromRSAKey(const RSAKey& key)
		{
			boost::shared_ptr<EVP_PKEY> _pkey(EVP_PKEY_new(), EVP_PKEY_free);

			EXCEPTION_ASSERT(_pkey, Exception::bad_function_call, "Unable to create a EVP_PKEY structure");
			EVP_PKEY_set1_RSA(_pkey.get(), key.d_rsa.get());

			return EVPPKey(_pkey, key.hasPrivateCompound());
		}
Exemplo n.º 4
0
        bool operator==(const RSAKey& lhs, const RSAKey& rhs)
        {
            if (lhs.hasPrivateCompound())
            {
                if (rhs.hasPrivateCompound())
                {
                    return lhs.d_private_key == rhs.d_private_key;
                }
            }
            else
            {
                if (!rhs.hasPrivateCompound())
                {
                    return lhs.d_public_key == rhs.d_public_key;
                }
            }

            return false;
        }
Exemplo n.º 5
0
QByteArray decryptRSA2(const QByteArray &buf, const RSAKey &key, bool *ok)
{
	if(ok)
		*ok = false;

	int size = RSA_size((RSA *)key.data());
	int flen = buf.size();
	QByteArray result(size);
	unsigned char *from = (unsigned char *)buf.data();
	unsigned char *to = (unsigned char *)result.data();
	int r = RSA_private_decrypt(flen, from, to, (RSA *)key.data(), RSA_PKCS1_OAEP_PADDING);
	if(r == -1)
		return QByteArray();
	result.resize(r);

	if(ok)
		*ok = true;
	return result;
}
Exemplo n.º 6
0
    //==============================================================================
    static String encryptXML (const XmlElement& xml, RSAKey privateKey)
    {
        MemoryOutputStream text;
        text << xml.createDocument (StringRef(), true);

        BigInteger val;
        val.loadFromMemoryBlock (text.getMemoryBlock());

        privateKey.applyToValue (val);

        return val.toString (16);
    }
Exemplo n.º 7
0
    //==============================================================================
    static XmlElement decryptXML (String hexData, RSAKey rsaPublicKey)
    {
        BigInteger val;
        val.parseString (hexData, 16);

        RSAKey key (rsaPublicKey);
        jassert (key.isValid());

        ScopedPointer<XmlElement> xml;

        if (! val.isZero())
        {
            key.applyToValue (val);

            const MemoryBlock mb (val.toMemoryBlock());

            if (CharPointer_UTF8::isValidString (static_cast<const char*> (mb.getData()), (int) mb.getSize()))
                xml = XmlDocument::parse (mb.toString());
        }

        return xml != nullptr ? *xml : XmlElement("key");
    }
String EncryptedString::decrypt (const String& encryptedString, const String& privateKey, bool inputIsHex)
{
    RSAKey rsaKey (privateKey);
    
    MemoryBlock encryptedMemoryBlock;
    
    if (inputIsHex)
    {
        encryptedMemoryBlock.loadFromHexString (encryptedString);
    }
    else
    {
        encryptedMemoryBlock.fromBase64Encoding (encryptedString);
    }

    BigInteger stringAsData;
    stringAsData.loadFromMemoryBlock (encryptedMemoryBlock);
    
    rsaKey.applyToValue (stringAsData);
    
    return stringAsData.toMemoryBlock().toString();
}
String EncryptedString::encrypt (const String& stringToEncrypt, const String& publicKey, bool resultAsHex)
{
    RSAKey rsaKey (publicKey);
    
    CharPointer_UTF8 stringPointer (stringToEncrypt.toUTF8());
    MemoryBlock stringMemoryBlock (stringPointer.getAddress(), stringPointer.sizeInBytes());

    BigInteger stringAsData;
    stringAsData.loadFromMemoryBlock (stringMemoryBlock);

    rsaKey.applyToValue (stringAsData);
    
    if (resultAsHex)
    {
        MemoryBlock encryptedMemoryBlock (stringAsData.toMemoryBlock());
        return String::toHexString ((char*) encryptedMemoryBlock.getData(), (int) encryptedMemoryBlock.getSize(), 0);
    }
    else
    {
        return stringAsData.toMemoryBlock().toBase64Encoding();
    }
}