QCA::PrivateKey EncryptioNgSimliteDecryptor::getPrivateKey(const Key &key)
{
	QByteArray keyData = key.key().toByteArray().trimmed();
	if (!keyData.startsWith(BEGIN_RSA_PRIVATE_KEY) || !keyData.endsWith(END_RSA_PRIVATE_KEY))
	{
		Valid = false;
		return QCA::PrivateKey();
	}

	keyData = keyData.mid(BEGIN_RSA_PRIVATE_KEY_LENGTH, keyData.length() - BEGIN_RSA_PRIVATE_KEY_LENGTH - END_RSA_PRIVATE_KEY_LENGTH).replace('\r', "").trimmed();

	QCA::SecureArray certificate;

	QCA::Base64 decoder;
	decoder.setLineBreaksEnabled(true);
	certificate = decoder.decode(keyData);

	// just some fake security added
	keyData.fill(' ', keyData.size());
	keyData.clear();

	if (!decoder.ok())
	{
		Valid = false;
		return QCA::PrivateKey();
	}

	PKCS1Certificate::ConversionStatus status;
	PKCS1Certificate pkcs1;

	QCA::PrivateKey privateKey = pkcs1.privateKeyFromDER(certificate, status);
	if (PKCS1Certificate::OK != status)
	{
		Valid = false;
		return QCA::PrivateKey();
	}

	if (!privateKey.canDecrypt())
	{
		Valid = false;
		return QCA::PrivateKey();
	}

	Valid = true;
	return privateKey;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	QCA::Initializer qcaInit;
	QCoreApplication app(argc, argv);

	if(argc < 3)
	{
		printf("usage: mozcerts [certdata.txt] [outfile.pem]\n");
		return 0;
	}

	QFile infile(argv[1]);
	if(!infile.open(QFile::ReadOnly))
	{
		fprintf(stderr, "Error opening input file\n");
		return 1;
	}

	QFile outfile(argv[2]);
	if(!outfile.open(QFile::WriteOnly | QFile::Truncate))
	{
		fprintf(stderr, "Error opening output file\n");
		return 1;
	}

	int count = 0;
	QString name;
	QTextStream ts(&infile);
	while(!ts.atEnd())
	{
		QString line = ts.readLine();
		if(QRegExp("^#").indexIn(line) != -1)
			continue;
		if(QRegExp("^\\s*$").indexIn(line) != -1)
			continue;
		line = line.trimmed();

		if(QRegExp("CKA_LABEL").indexIn(line) != -1)
		{
			QStringList list = splitWithQuotes(line, ' ');
			if(list.count() != 3)
				continue;

			name = list[2];
			// make an output filename based on the name
			//outname = name.replace(QRegExp("\\/"), "_")
			//	.replace(QRegExp("\\s+"), "_")
			//	.replace(QRegExp("[()]"), "=")
			//	.replace(QRegExp(","), "_") + ".pem";
			continue;
		}
		else if(QRegExp("CKA_VALUE MULTILINE_OCTAL").indexIn(line) != -1)
		{
			QByteArray buf;
			while(!ts.atEnd())
			{
				line = ts.readLine();
				if(QRegExp("^END").indexIn(line) != -1)
					break;
				line = line.trimmed();
				QRegExp rx("\\\\([0-3][0-7][0-7])");
				int pos = 0;
				while((pos = rx.indexIn(line, pos)) != -1)
				{
					QString str = rx.capturedTexts()[1];
					uchar c = str.toInt(0, 8);
					buf.append(c);
					pos += rx.matchedLength();
				}
			}

			printf(">> [%s], %d bytes\n", qPrintable(name), buf.size());

			QTextStream ts(&outfile);
			ts << "-----BEGIN CERTIFICATE-----" << '\n';
			QCA::Base64 enc;
			enc.setLineBreaksEnabled(true);
			enc.setLineBreaksColumn(64);
			ts << enc.arrayToString(buf) << '\n';
			ts << "-----END CERTIFICATE-----" << '\n';

			++count;
		}
	}
	printf("Wrote %d certs to [%s]\n", count, argv[2]);

	return 0;
}