size_t CryptoKeyRSA::keySizeInBits() const { Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return 0; } return modulus.size() * 8; }
std::unique_ptr<CryptoKeyData> CryptoKeyRSA::exportData() const { switch (CCRSAGetKeyType(m_platformKey)) { case ccRSAKeyPublic: { Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return nullptr; } return CryptoKeyDataRSAComponents::createPublic(modulus, publicExponent); } case ccRSAKeyPrivate: { Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return nullptr; } Vector<uint8_t> privateExponent; CryptoKeyDataRSAComponents::PrimeInfo firstPrimeInfo; CryptoKeyDataRSAComponents::PrimeInfo secondPrimeInfo; Vector<CryptoKeyDataRSAComponents::PrimeInfo> otherPrimeInfos; // Always empty, CommonCrypto only supports two primes (cf. <rdar://problem/15444074>). status = getPrivateKeyComponents(m_platformKey, privateExponent, firstPrimeInfo, secondPrimeInfo); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return nullptr; } return CryptoKeyDataRSAComponents::createPrivateWithAdditionalData(modulus, publicExponent, privateExponent, firstPrimeInfo, secondPrimeInfo, otherPrimeInfos); } default: return nullptr; } }
std::unique_ptr<KeyAlgorithm> CryptoKeyRSA::buildAlgorithm() const { String name = CryptoAlgorithmRegistry::singleton().name(algorithmIdentifier()); Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); publicExponent.clear(); return std::make_unique<RsaKeyAlgorithm>(name, 0, WTFMove(publicExponent)); } size_t modulusLength = modulus.size() * 8; if (m_restrictedToSpecificHash) return std::make_unique<RsaHashedKeyAlgorithm>(name, modulusLength, WTFMove(publicExponent), CryptoAlgorithmRegistry::singleton().name(m_hash)); return std::make_unique<RsaKeyAlgorithm>(name, modulusLength, WTFMove(publicExponent)); }
void CryptoKeyRSA::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const { CryptoKey::buildAlgorithmDescription(builder); Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return; } builder.add("modulusLength", modulus.size() * 8); builder.add("publicExponent", publicExponent); if (m_restrictedToSpecificHash) { auto hashDescriptionBuilder = builder.createEmptyClone(); hashDescriptionBuilder->add("name", CryptoAlgorithmRegistry::singleton().nameForIdentifier(m_hash)); builder.add("hash", *hashDescriptionBuilder); } }
std::unique_ptr<CryptoKeyData> CryptoKeyRSA::exportData() const { ASSERT(extractable()); switch (CCRSAGetKeyType(m_platformKey)) { case ccRSAKeyPublic: { Vector<uint8_t> modulus; Vector<uint8_t> publicExponent; CCCryptorStatus status = getPublicKeyComponents(m_platformKey, modulus, publicExponent); if (status) { WTFLogAlways("Couldn't get RSA key components, status %d", status); return nullptr; } return CryptoKeyDataRSAComponents::createPublic(modulus, publicExponent); } case ccRSAKeyPrivate: // Not supported yet. default: return nullptr; } }