Expose PKey::raw_{private,public}_key#364
Conversation
|
This is a useful functionality, but the API is very C-like. Getting the length with buffer.resize(pk.raw_public_key(&mut [])?, 0);
let len = pk.raw_public_key(&mut buffer)?;
let key = &buffer[..len];Since we don't already have a great consistency, I suggest improving the approach further. If you add fn raw_public_key<'buf>(&self, buf: &'buf mut [u8]) -> Result<&'buf [u8], ErrorStack> {
…
Ok(&buf[..size])
}then the API becomes nicer to use: let key = pk.raw_public_key(&mut buffer)?; // assuming we know the sizeand: buffer.resize(pk.raw_public_key_len()?, 0);
let key = pk.raw_public_key(&mut buffer)?;I've explored different variations like |
|
Done! I didn't include an explicit check for a non-empty buffer because BoringSSL should already do that—an empty buffer is just a special case of a too-short buffer. |
|
Now I'm wondering, should the input be of type |
|
Keys are small. |
Had occasion to want these, hopefully relatively uncontroversial. I did see that there are two different ways of adapting the "measure once, output once" APIs: SslSession methods follow this approach, using an
&mut [u8]argument and special-casing the empty case (though apparently that's done within BoringSSL itself);EcPointRef::to_bytes, by contrast, just calls the underlying BoringSSL API twice so it can return aVec<u8>. Since the caller must already know something about the kind of key to expect success, I figured it was worth providing the more flexible slice-based API.