PrivateKey PrivateKey::fromPEM(const QString &s, const QSecureArray &passphrase, ConvertResult *result, const QString &provider)
{
	PrivateKey out;
	ConvertResult r;
	out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, s, passphrase, &r);

	// error converting without passphrase?  maybe a passphrase is needed
	// FIXME: we should only do this if we get ErrorPassphrase?
	if(r != ConvertGood && passphrase.isEmpty())
	{
		QSecureArray pass;
		if(ask_passphrase(QString(), 0, &pass))
			out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, s, pass, &r);
	}
	if(result)
		*result = r;
	return out;

	/*PrivateKey k;
	PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", provider));
	ConvertResult r = c->privateFromPEM(s, passphrase);
	if(result)
		*result = r;
	if(r == ConvertGood)
		k.change(c);
	return k;*/
}
PrivateKey PrivateKey::fromDER(const QSecureArray &a, const QSecureArray &passphrase, ConvertResult *result, const QString &provider)
{
	PrivateKey out;
	ConvertResult r;
	out = getKey<PrivateKey, Getter_PrivateKey<QSecureArray>, QSecureArray>(provider, a, passphrase, &r);

	// error converting without passphrase?  maybe a passphrase is needed
	// FIXME: we should only do this if we get ErrorPassphrase?
	if(r != ConvertGood && passphrase.isEmpty())
	{
		QSecureArray pass;
		if(ask_passphrase(QString(), 0, &pass))
			out = getKey<PrivateKey, Getter_PrivateKey<QSecureArray>, QSecureArray>(provider, a, pass, &r);
	}
	if(result)
		*result = r;
	return out;

	/*PrivateKey k;

	// single
	if(!provider.isEmpty())
	{
		Provider *p = providerForName(provider);
		if(!p)
			return k;
		PKeyContext *c = static_cast<PKeyContext *>(p->createContext("pkey"));
		if(!c)
			return k;
		ConvertResult r = c->privateFromDER(a, passphrase);
		if(result)
			*result = r;
		if(r == ConvertGood)
			k.change(c);
	}
	// all
	else
	{
		ProviderList pl = allProviders();
		for(int n = 0; n < pl.count(); ++n)
		{
			PKeyContext *c = static_cast<PKeyContext *>(pl[n]->createContext("pkey"));
			if(!c)
				continue;
			ConvertResult r = c->privateFromDER(a, passphrase);
			if(result)
				*result = r;
			if(r == ConvertGood)
				k.change(c);
			if(!k.isNull())
				break;
		}
	}
	return k;*/
}
示例#3
0
static PrivateKey get_privatekey_pem(const QString &pem, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
{
	PrivateKey out;
	ConvertResult r;
	out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, passphrase, &r);

	// error converting without passphrase?  maybe a passphrase is needed
	if(use_asker_fallback(r) && passphrase.isEmpty())
	{
		SecureArray pass;
		if(ask_passphrase(fileName, ptr, &pass))
			out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, pass, &r);
	}
	if(result)
		*result = r;
	return out;
}