I have a private/public key pair generated and stored inside Secure Enclave.
It is 256-bit elliptic curve key. (The only key type that can be stored in Secure Enclave).
I use SecKeyCreateWithData
and SecKeyCopyExternalRepresentation
to import/export the public key between iOS devices, and it works.
However, the exported key doesn't seem to work with OpenSSL.
Because it always show 'unable to load Key'
on this command.
openssl ec -pubin -in public_key_file -text
What's the way to export the key ? So I can use it with OpenSSL.
To work with OpenSSL, you need
subject public key info (SPKI)
, eitherDER
orPEM
format.SPKI contains essential information, for example,
key.type
,key.parameters
,key.value
.SecKeyCopyExternalRepresentation
only returns raw key binary which is onlykey.value
part.You have to create SPKI from that
key.value
. The normal way to do this is to read https://tools.ietf.org/html/rfc5480, and encode ASN.1 structure to binary-encoded DER format.But here is a shortcut.
Secure Enclave only supports one key type, 256-bit EC key
secp256r1
(equivalent toprime256v1
in OpenSSL).The SPKI in DER format is a binary encoded data, for example,
It always consist of two parts
fixed schema header
3059301306072a8648ce3d020106082a8648ce3d030107034200
raw key value
04.......
You can create SPKI by combining these two parts.
spki = fixed_schema_header + SecKeyCopyExternalRepresentation(...)