formats icon indicating copy to clipboard operation
formats copied to clipboard

pkcs8: provide `PrivateKeyInfoRef`/`PrivateKeyInfoOwned`

Open baloo opened this issue 1 year ago • 6 comments

see #1117

baloo avatar Aug 04 '23 04:08 baloo

Why are you exposing BytesRef/BytesOwned?

Why not OctetString/OctetStringRef ala spki (i.e. the ASN.1 type, though it's BIT STRING there), or if not that then &'a [u8] and Vec<u8>?

Really BytesRef and BytesOwned exist for one reason: because they pre-check that the Length is valid and thus permit operations which would otherwise be fallible. Otherwise they're just analogues for &'a [u8] and Vec<u8>.

tarcieri avatar Aug 04 '23 12:08 tarcieri

I was actually looking for a &'a [u8]/Box<[u8]> tuple. But the BytesRef/BytesOwned was looking like the closest to that.

I don't think I'm too comfortable with OctetString, went with just the &'a [u8]/Box<[u8]> and implement RefToOwned there.

baloo avatar Aug 04 '23 19:08 baloo

Why not OctetString/OctetStringRef

Why not merge OctetString and OctetStringRef into something like Cow<[u8]> ? With minor CPU overhead an enum would be easier to maintain.

Regular OctetString would just contain Cow<'static, [u8]> which is Vec<u8> Ref OctetString would contain &'a [u8]

https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-From%3CCow%3C'a,+%5BT%5D%3E%3E-for-Vec%3CT%3E

Also, conversions from/to owned would just swap an enum.

dishmaker avatar Jun 04 '24 13:06 dishmaker

@dishmaker this crate is designed to support heapless targets that don’t have alloc available, which OctetStringRef is capable of working on

tarcieri avatar Jun 04 '24 13:06 tarcieri

Ohhh so such Cow feature needs a kill switch 😢

struct BytesRef<'a> {

    pub length: Length,

    #[cfg(feature = "alloc")]
    pub inner: Cow<'a, [u8]>,

    #[cfg(not(feature = "alloc"))]
    pub inner: &'a [u8],
}

dishmaker avatar Jun 04 '24 13:06 dishmaker

But then you haven't actually gotten rid of the *Ref type, and since it's immutable anyway, there's no "write" case to worry about, so Cow doesn't buy you anything.

Anyway, if you'd like to keep discussing this, it would probably be better to open a separate issue.

tarcieri avatar Jun 04 '24 13:06 tarcieri

@baloo are you going to redo this? I think it would be great to get in before the next stable release series

tarcieri avatar Aug 18 '24 20:08 tarcieri

I guess I could, I honestly lost track.

baloo avatar Aug 18 '24 21:08 baloo

Aah ok, I'm down to take it then. Seems like it will be a bit of a mess but worth it.

tarcieri avatar Aug 18 '24 21:08 tarcieri

The rebase looks like it's a mess indeed.

I can give it a shot, you have a fair amount of things on your hand.

baloo avatar Aug 18 '24 21:08 baloo

Sure!

tarcieri avatar Aug 18 '24 21:08 tarcieri