Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

der: Provide from_der that doesn't error on trailing data #1278

Open
NWPlayer123 opened this issue Dec 13, 2023 · 0 comments
Open

der: Provide from_der that doesn't error on trailing data #1278

NWPlayer123 opened this issue Dec 13, 2023 · 0 comments

Comments

@NWPlayer123
Copy link

Hello, I'm currently trying to implement parsing of a certificate blob that Panda3D uses, and it's proving rather difficult with RustCrypto's implementation.

Basically, the format doesn't store the length so I have no way of determining how long each certificate in the blob is without actually parsing it, and the default way that from_der works is it calls SliceReader::finish which returns an error about TrailingData instead of the finished certificate and all leftover data, which means I can't make a nice loop to parse through like I could with d2i_X509 (which is what the original implementation uses).

For now, I've implemented it by creating my own Certificate that just keeps the remaining length as part of the struct:

use x509_cert::certificate::{CertificateInner, Rfc5280};
use der::{Decode, Reader, Result, SliceReader, Length};

#[derive(Debug)]
pub struct Certificate {
    pub certificate: CertificateInner<Rfc5280>,
    pub remaining_len: Length,
}

impl<'a> Decode<'a> for Certificate {
    fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Self> {
        let inner = CertificateInner::<Rfc5280>::decode(reader)?;
        Ok(Certificate { certificate: inner, remaining_len: Length::new(0) })
    }

    fn from_der(bytes: &'a [u8]) -> Result<Self> {
        let mut reader = SliceReader::new(bytes)?;
        let mut result = Self::decode(&mut reader)?;
        result.remaining_len = reader.remaining_len();
        Ok(result)
    }
}
@tarcieri tarcieri changed the title der/x509-cert: Provide from_der that doesn't error on trailing data der: Provide from_der that doesn't error on trailing data Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant