private SafeRsaHandle GenerateKey()
{
SafeRsaHandle key = Interop.Crypto.RsaCreate();
bool generated = false;
Interop.Crypto.CheckValidOpenSslHandle(key);
try
{
using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(s_defaultExponent))
{
// The documentation for RSA_generate_key_ex does not say that it returns only
// 0 or 1, so the call marshals it back as a full Int32 and checks for a value
// of 1 explicitly.
int response = Interop.Crypto.RsaGenerateKeyEx(
key,
KeySize,
exponent);
CheckBoolReturn(response);
generated = true;
}
}
finally
{
if (!generated)
{
key.Dispose();
}
}
return key;
}