Skip to content

der: Use #[asn1(type = "OCTET STRING")] with Vec<u8> #1539

@dzil123

Description

@dzil123

I'm trying to use the der and der_derive crates to implement encoding and encoding a struct containing a Vec<u8> as an OCTET STRING. I need my struct to be owned, not borrowed from the input DER bytes.

Using OctetString in the struct definition is inconvenient because I must use the fallible new() method when manually creating an instance of the struct. I would rather the struct use Vec<u8> directly, and keep the der-specific types and encoding error in the encode and decode steps.

I thought #[asn1(type = "OCTET STRING")] would implement this, as the documentation states:

performs an intermediate conversion to der::asn1::OctetString

This does not work. Contrary to the documentation, the macro performs an intermediate conversion to OctetStringRef, which does not have a TryFrom and Into implemented for Vec<u8>.

use der::Sequence;

#[derive(Sequence)]
struct Foo {
    #[asn1(type = "OCTET STRING")]
    bar: Vec<u8>,
}
error[E0277]: the trait bound `Vec<u8>: TryFrom<OctetStringRef<'_>>` is not satisfied
 --> src/lib.rs:3:10
  |
3 | #[derive(Sequence)]
  |          ^^^^^^^^ the trait `From<OctetStringRef<'_>>` is not implemented for `Vec<u8>`, which is required by `OctetStringRef<'_>: TryInto<_>`
  |
  = help: the following other types implement trait `From<T>`:
            <Vec<u8> as From<&str>>
            <Vec<u8> as From<CString>>
            <Vec<u8> as From<String>>
  = note: required for `OctetStringRef<'_>` to implement `Into<Vec<u8>>`
  = note: required for `Vec<u8>` to implement `TryFrom<OctetStringRef<'_>>`
  = note: required for `OctetStringRef<'_>` to implement `TryInto<Vec<u8>>`
  = note: this error originates in the derive macro `Sequence` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/lib.rs:3:10
   |
3  | #[derive(Sequence)]
   |          ^^^^^^^^
   |          |
   |          expected `&[u8]`, found `Vec<u8>`
   |          arguments to this function are incorrect
   |
   = note: expected reference `&[u8]`
                 found struct `Vec<u8>`
note: associated function defined here
  --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/der-0.7.9/src/asn1/octet_string.rs:21:12
   |
21 |     pub fn new(slice: &'a [u8]) -> Result<Self> {
   |            ^^^
   = note: this error originates in the derive macro `Sequence` (in Nightly builds, run with -Z macro-backtrace for more info)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions