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

SecCertificate::public_key_info_der #75

Merged
merged 12 commits into from
Feb 15, 2019
Merged

Conversation

SergejJurecko
Copy link
Contributor

No description provided.

#[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
/// Returns DER encoded subjectPublicKeyInfo of certificate if available. This can be used
/// for certificate pinning.
pub fn public_key_info_der(&self) -> Result<Option<Vec<u8>>> {
Copy link
Owner

Choose a reason for hiding this comment

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

My concern is that this is a very specific single-purpose high-level function. Could it be broken down into components that help others write their public_key_info_der and alike themselves?

// Imported from TrustKit
// https://github.com/datatheorem/TrustKit/blob/master/TrustKit/Pinning/TSKSPKIHashCache.m
unsafe {
let public_key = self.copy_public_key_from_certificate()?;
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a reason why this function is not public?

unsafe {
let public_key = self.copy_public_key_from_certificate()?;
let mut error: CFErrorRef = ptr::null_mut();
let public_key_attributes = SecKeyCopyAttributes(public_key);
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe there could be a high-level wrapper for SecKeyCopyAttributes, so that it doesn't require so much unsafe boilerplate for each property?

return &EC_DSA_SECP_384_R1_ASN1_HEADER;
}
}
&[]
Copy link
Owner

Choose a reason for hiding this comment

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

Better error handling would be to return None


extern "C" {
#[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
pub fn CFStringCompare(
Copy link
Owner

Choose a reason for hiding this comment

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

You don't need this. CFString implements Eq, so you can compare them with just ==

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'm using CFStringRef's due to comparing with constants kSecAttrKeyTypeRSA and kSecAttrKeyTypeECSECPrimeRandom. Can I turn those into CFString without allocating?

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 you can. The CFString is a refcounted type, so a conversion from Ref to non-Ref should only need to change reference count:

https://docs.rs/core-foundation/0.6.3/core_foundation/base/trait.TCFType.html#tymethod.wrap_under_get_rule

Copy link
Owner

Choose a reason for hiding this comment

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

Also the eq is implemented as self.as_CFType().eq(&other.as_CFType())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok switched to CFString now.

@SergejJurecko
Copy link
Contributor Author

I've expanded SecKey and cut down on the big unsafe block into mostly safe code.

@SergejJurecko
Copy link
Contributor Author

Another small refactor to get rid of verbose error handling. Do you have any other suggestions for improving it?

@kornelski
Copy link
Owner

This is much nicer now. Thank you!

@kornelski kornelski merged commit 1f757b5 into kornelski:master Feb 15, 2019
@SergejJurecko
Copy link
Contributor Author

Can this be released?

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.

2 participants