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

Add two methods to the PKCS7 API #2111

Merged
merged 1 commit into from
Jan 19, 2024

Conversation

facutuesca
Copy link
Contributor

@facutuesca facutuesca commented Dec 1, 2023

This PR adds two new methods:

  • Pkcs7Ref::type_ returns the type of the PKCS#7 object (signed, envelope, etc)

  • Pkcs7Ref::signed returns the PKCS7_SIGNED member of a signed PKCS#7 object (as a Pkcs7SignedRef)

It also adds a Pkcs7SignedRef::certificates method, to access the stack of certificates of a signed PKCS7 object.

The motivation behind adding these methods is that cryptography is migrating its PKCS7 backend implementation from Python to Rust.
Here is the Python code that would be replaced by Rust, which manually access the data structures that this PR exposes through the API.

One thing I'm not sure about (in certificates()) is my handling of the Stack as a reference, and the ownership status of the certificates inside of it, so any suggestions/fixes are welcome.

openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
@facutuesca facutuesca force-pushed the pkcs7-add-apis branch 2 times, most recently from bd957d7 to 106c563 Compare December 11, 2023 13:04
openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
@facutuesca facutuesca force-pushed the pkcs7-add-apis branch 2 times, most recently from ec165c0 to 4fc1713 Compare December 11, 2023 13:09
openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
@facutuesca
Copy link
Contributor Author

@sfackler @alex Anything else needed for this PR?

@@ -281,11 +296,37 @@ impl Pkcs7Ref {
Ok(stack)
}
}

// Return the type of a PKCS#7 structure as an Asn1Object
pub fn type_oid(&self) -> Option<&Asn1ObjectRef> {
Copy link
Owner

Choose a reason for hiding this comment

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

Should this just be called type?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't use that here, unfortunately, because type is a strict keyword.

Choose a reason for hiding this comment

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

It could be r#type instead to use the raw identifier syntax, but yeah -- I think bare type won't work.

Copy link
Owner

Choose a reason for hiding this comment

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

You can go with a raw ident, but type_ is a bit easier to work with.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed (changed to type_)

}

// Retrieve all the certificates from a PKCS#7 structure used for signed data
pub fn signed_data_certificates(&self) -> Result<Option<&StackRef<X509>>, ErrorStack> {
Copy link
Owner

Choose a reason for hiding this comment

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

I think I'd rather go "1 step" here - instead having a pub fn signed(&self) -> Option<&Pkcs7SignedRef> method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't this mean that users of this library would have to use unsafe code to extract the stack of certificates from the Pkcs7SignedRef? Since they would have to do the extra step that this function now does:

                .and_then(|x| x.cert.as_mut())
                .and_then(|x| StackRef::<X509>::from_const_ptr_opt(x));

which is unsafe.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume @sfackler was suggesting that Pkcs7SignedRef should have safe methods for accessing this data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Fixed, now the stack of certificates can be accessed in two steps:

let signed_certificates = pkcs7.signed().and_then(|x| x.certificates());

@facutuesca facutuesca force-pushed the pkcs7-add-apis branch 2 times, most recently from 1ef2608 to 5630689 Compare January 8, 2024 11:35
openssl/src/pkcs7.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@alex alex left a comment

Choose a reason for hiding this comment

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

Will hold off on merging for @sfackler to have a look.

@sfackler
Copy link
Owner

Sorry for the delay! LGTM

@sfackler sfackler merged commit 951d771 into sfackler:master Jan 19, 2024
53 checks passed
@woodruffw woodruffw deleted the pkcs7-add-apis branch January 19, 2024 18:47
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.

None yet

5 participants