Update
Misleading question:
From the discussion below as well as this question formulated thereafter ...
... it became clear that the assumption leading to this question ...
'iOS: Creating PublicKey in PKCS-Format instead of ASN.1-Format'
... was fundamentally wrong.
Fixing the assumptions
Hence the assumptions should have been:
- Swift (iOS) produces PKCS#1-formatted keys
- Java 17 produces keys which look alike but are slightly different using SubjectPublicKeyInfo (or SPKI)
For the sake of documentation, the question remains untouched (below)
Objective
Creating a PublicKey in PKCS-format:
- Generated on an iOS-device (Swift)
General approach
iOS generates PublicKeys which are represented in ASN.1-Format
Sample: Generating a KeyPair (ASN.1-Format)
SampleCode to extract the PublicKey in ASN.1-Format from the KeyPair (Swift):
// Keypair attributes
let tag = "my_personal_keystore_keypair_alias".data(using: .utf8)!
let attributes: [String: Any] =
[kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits as String: "4096",
kSecPrivateKeyAttrs as String:
[kSecAttrIsPermanent as String: true,
kSecAttrApplicationTag as String: tag]
]
var error: Unmanaged<CFError>?
// Generate PrivateKey / Keypair
guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
throw error!.takeRetainedValue() as Error
}
// Extract PublicKey
guard let publicKey = SecKeyCopyPublicKey(privateKey),
let publicKeyOptional = SecKeyCopyExternalRepresentation(publicKey, nil) else {
return "PublicKey could not be accessed."
}
let publicKeyData = publicKeyOptional as Data
let base64PublicKey = publicKeyData.base64EncodedString()
print("PublicKey, base64: ", base64PublicKey)
Problem
I am missing an option to retrieve the PublicKey in PKCS-Format.
Apple Developer Documentation
There seems to be some kind of a SecItemExport, but it lacks explanation to me.
Stackoverflow
The question is not new in its intent, still the best answer is linked to a dead resource and hence of no use.
Additional condition
External libraries for key generation are not allowed.
- The intent is that it is not possible to trust sources to handle PrivateKeys properly. Therefore only sources from Apple are trusted.
- Nevertheless, using a library to convert the PublicKey (only) from ASN.1 into PKCS-format would be feasible.
Anyways
Thanks for reading & sharing - any help is appreciated :)
Appendix for experimentation
Adding public keys which are produced by the given code. I ser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The fastest way to check the keys is to use a website like this ( https://www.javainuse.com/rsagenerator ). Use the "RSA Encryption"-SubSection, paste one of the Keys including some sample payload to be encrypted. We expect the keys to be formatted as PKCS#1. Therefore for 'Cipher Type' RSA/ECB/PKCS1Padding or RSA/None/PKCS1Padding is chosen. Both lead to ' unknown key type passed to RSA' although the "RSA Generate Keys"-Subsection above produces 'alike'-RSA keys. Hence the iOS-Key has a different Format, which is ASN.1 Still, PKCS#1 is searched for .. :)