Skip to content

Conversation

@npmccallum
Copy link
Contributor

@npmccallum npmccallum commented Mar 4, 2022

x509/src/attr.rs Outdated
/// This function follows the rules in [RFC 4514].
///
/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514
pub fn parse(s: &str) -> Result<Vec<u8>, der::Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused by the fn parse name along with parse_str and parse_der. These seem to be combination parser/serializer methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree it is a bit awkward. Basically, RFC 4514 defines a human readable encoding for certificate name types. That is, we want to convert from the name type into a string (this part is easy) and from the string back into the relevant name type. The latter operation is difficult because the result needs to be owned, but the name types borrow references.

The compromise is that we parse the string into an owned buffer. Then we create the named type referencing that owned buffer. Then, because of the lifetime issues, we serialize the named type to der.

In practice it looks like this to avoid the lifetime issues:

            let der = RdnSequence::parse("CN=foo").unwrap();
            let rdns = RdnSequence::from_der(&der).unwrap();

If you have alternative suggestions, I'm all ears.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's producing encoded DER, I'd suggest something like RdnSequence::encode_*, like RdnSequence::encode_from_string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed all the public functions to encode_from_string(). The internal functions were renamed to encode_str() and encode_hex().

Copy link
Member

@tarcieri tarcieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small nits, otherwise looks good

@npmccallum npmccallum force-pushed the string branch 2 times, most recently from ee4d3ec to 2add659 Compare March 10, 2022 19:15
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
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

Successfully merging this pull request may close these issues.

x501: support string encoding/decoding for Distinguished Names

2 participants