Skip to content

Commit 8595393

Browse files
authored
Complete converting Rust Python imports (#9546)
* Convert src/rust/src/x509/common.rs * Convert src/rust/src/x509/certificate.rs
1 parent 73cfc50 commit 8595393

File tree

3 files changed

+110
-120
lines changed

3 files changed

+110
-120
lines changed

src/rust/src/types.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,48 @@ pub static RELATIVE_DISTINGUISHED_NAME: LazyPyImport =
207207
LazyPyImport::new("cryptography.x509", &["RelativeDistinguishedName"]);
208208
pub static NAME_ATTRIBUTE: LazyPyImport =
209209
LazyPyImport::new("cryptography.x509", &["NameAttribute"]);
210+
pub static NAME_CONSTRAINTS: LazyPyImport =
211+
LazyPyImport::new("cryptography.x509", &["NameConstraints"]);
212+
pub static MS_CERTIFICATE_TEMPLATE: LazyPyImport =
213+
LazyPyImport::new("cryptography.x509", &["MSCertificateTemplate"]);
214+
pub static CRL_DISTRIBUTION_POINTS: LazyPyImport =
215+
LazyPyImport::new("cryptography.x509", &["CRLDistributionPoints"]);
216+
pub static BASIC_CONSTRAINTS: LazyPyImport =
217+
LazyPyImport::new("cryptography.x509", &["BasicConstraints"]);
218+
pub static INHIBIT_ANY_POLICY: LazyPyImport =
219+
LazyPyImport::new("cryptography.x509", &["InhibitAnyPolicy"]);
220+
pub static OCSP_NO_CHECK: LazyPyImport = LazyPyImport::new("cryptography.x509", &["OCSPNoCheck"]);
221+
pub static POLICY_CONSTRAINTS: LazyPyImport =
222+
LazyPyImport::new("cryptography.x509", &["PolicyConstraints"]);
223+
pub static CERTIFICATE_POLICIES: LazyPyImport =
224+
LazyPyImport::new("cryptography.x509", &["CertificatePolicies"]);
225+
pub static SUBJECT_INFORMATION_ACCESS: LazyPyImport =
226+
LazyPyImport::new("cryptography.x509", &["SubjectInformationAccess"]);
227+
pub static KEY_USAGE: LazyPyImport = LazyPyImport::new("cryptography.x509", &["KeyUsage"]);
228+
pub static EXTENDED_KEY_USAGE: LazyPyImport =
229+
LazyPyImport::new("cryptography.x509", &["ExtendedKeyUsage"]);
230+
pub static SUBJECT_KEY_IDENTIFIER: LazyPyImport =
231+
LazyPyImport::new("cryptography.x509", &["SubjectKeyIdentifier"]);
232+
pub static TLS_FEATURE: LazyPyImport = LazyPyImport::new("cryptography.x509", &["TLSFeature"]);
233+
pub static SUBJECT_ALTERNATIVE_NAME: LazyPyImport =
234+
LazyPyImport::new("cryptography.x509", &["SubjectAlternativeName"]);
235+
pub static POLICY_INFORMATION: LazyPyImport =
236+
LazyPyImport::new("cryptography.x509", &["PolicyInformation"]);
237+
pub static USER_NOTICE: LazyPyImport = LazyPyImport::new("cryptography.x509", &["UserNotice"]);
238+
pub static NOTICE_REFERENCE: LazyPyImport =
239+
LazyPyImport::new("cryptography.x509", &["NoticeReference"]);
240+
pub static REGISTERED_ID: LazyPyImport = LazyPyImport::new("cryptography.x509", &["RegisteredID"]);
241+
pub static DIRECTORY_NAME: LazyPyImport =
242+
LazyPyImport::new("cryptography.x509", &["DirectoryName"]);
243+
pub static UNIFORM_RESOURCE_IDENTIFIER: LazyPyImport =
244+
LazyPyImport::new("cryptography.x509", &["UniformResourceIdentifier"]);
245+
pub static DNS_NAME: LazyPyImport = LazyPyImport::new("cryptography.x509", &["DNSName"]);
246+
pub static RFC822_NAME: LazyPyImport = LazyPyImport::new("cryptography.x509", &["RFC822Name"]);
247+
pub static OTHER_NAME: LazyPyImport = LazyPyImport::new("cryptography.x509", &["OtherName"]);
248+
pub static CERTIFICATE_VERSION_V1: LazyPyImport =
249+
LazyPyImport::new("cryptography.x509", &["Version", "v1"]);
250+
pub static CERTIFICATE_VERSION_V3: LazyPyImport =
251+
LazyPyImport::new("cryptography.x509", &["Version", "v3"]);
210252

211253
pub static CRL_REASON_FLAGS: LazyPyImport =
212254
LazyPyImport::new("cryptography.x509.extensions", &["_CRLREASONFLAGS"]);

src/rust/src/x509/certificate.rs

Lines changed: 46 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,9 @@ impl Certificate {
297297
}
298298

299299
fn cert_version(py: pyo3::Python<'_>, version: u8) -> Result<&pyo3::PyAny, CryptographyError> {
300-
let x509_module = py.import(pyo3::intern!(py, "cryptography.x509"))?;
301300
match version {
302-
0 => Ok(x509_module
303-
.getattr(pyo3::intern!(py, "Version"))?
304-
.get_item(pyo3::intern!(py, "v1"))?),
305-
2 => Ok(x509_module
306-
.getattr(pyo3::intern!(py, "Version"))?
307-
.get_item(pyo3::intern!(py, "v3"))?),
301+
0 => Ok(types::CERTIFICATE_VERSION_V1.get(py)?),
302+
2 => Ok(types::CERTIFICATE_VERSION_V3.get(py)?),
308303
_ => Err(CryptographyError::from(
309304
exceptions::InvalidVersion::new_err((
310305
format!("{} is not a valid X509 version", version),
@@ -450,7 +445,6 @@ fn parse_user_notice(
450445
py: pyo3::Python<'_>,
451446
un: UserNotice<'_>,
452447
) -> Result<pyo3::PyObject, CryptographyError> {
453-
let x509_module = py.import(pyo3::intern!(py, "cryptography.x509"))?;
454448
let et = match un.explicit_text {
455449
Some(data) => parse_display_text(py, data)?,
456450
None => py.None(),
@@ -462,15 +456,14 @@ fn parse_user_notice(
462456
for num in data.notice_numbers.unwrap_read().clone() {
463457
numbers.append(big_byte_slice_to_py_int(py, num.as_bytes())?.to_object(py))?;
464458
}
465-
x509_module
466-
.call_method1(pyo3::intern!(py, "NoticeReference"), (org, numbers))?
459+
types::NOTICE_REFERENCE
460+
.get(py)?
461+
.call1((org, numbers))?
467462
.to_object(py)
468463
}
469464
None => py.None(),
470465
};
471-
Ok(x509_module
472-
.call_method1(pyo3::intern!(py, "UserNotice"), (nr, et))?
473-
.to_object(py))
466+
Ok(types::USER_NOTICE.get(py)?.call1((nr, et))?.to_object(py))
474467
}
475468

476469
fn parse_policy_qualifiers<'a>(
@@ -512,7 +505,6 @@ fn parse_cp(
512505
ext: &Extension<'_>,
513506
) -> Result<pyo3::PyObject, CryptographyError> {
514507
let cp = ext.value::<asn1::SequenceOf<'_, PolicyInformation<'_>>>()?;
515-
let x509_module = py.import(pyo3::intern!(py, "cryptography.x509"))?;
516508
let certificate_policies = pyo3::types::PyList::empty(py);
517509
for policyinfo in cp {
518510
let pi_oid = oid_to_py_oid(py, &policyinfo.policy_identifier)?.to_object(py);
@@ -522,8 +514,9 @@ fn parse_cp(
522514
}
523515
None => py.None(),
524516
};
525-
let pi = x509_module
526-
.call_method1(pyo3::intern!(py, "PolicyInformation"), (pi_oid, py_pqis))?
517+
let pi = types::POLICY_INFORMATION
518+
.get(py)?
519+
.call1((pi_oid, py_pqis))?
527520
.to_object(py);
528521
certificate_policies.append(pi)?;
529522
}
@@ -669,24 +662,19 @@ pub fn parse_cert_ext<'p>(
669662
py: pyo3::Python<'p>,
670663
ext: &Extension<'_>,
671664
) -> CryptographyResult<Option<&'p pyo3::PyAny>> {
672-
let x509_module = py.import(pyo3::intern!(py, "cryptography.x509"))?;
673665
match ext.extn_id {
674666
oid::SUBJECT_ALTERNATIVE_NAME_OID => {
675667
let gn_seq = ext.value::<SubjectAlternativeName<'_>>()?;
676668
let sans = x509::parse_general_names(py, &gn_seq)?;
677669
Ok(Some(
678-
x509_module
679-
.getattr(pyo3::intern!(py, "SubjectAlternativeName"))?
680-
.call1((sans,))?,
670+
types::SUBJECT_ALTERNATIVE_NAME.get(py)?.call1((sans,))?,
681671
))
682672
}
683673
oid::ISSUER_ALTERNATIVE_NAME_OID => {
684674
let gn_seq = ext.value::<IssuerAlternativeName<'_>>()?;
685675
let ians = x509::parse_general_names(py, &gn_seq)?;
686676
Ok(Some(
687-
x509_module
688-
.getattr(pyo3::intern!(py, "IssuerAlternativeName"))?
689-
.call1((ians,))?,
677+
types::ISSUER_ALTERNATIVE_NAME.get(py)?.call1((ians,))?,
690678
))
691679
}
692680
oid::TLS_FEATURE_OID => {
@@ -697,17 +685,13 @@ pub fn parse_cert_ext<'p>(
697685
let py_feature = tls_feature_type_to_enum.get_item(feature.to_object(py))?;
698686
features.append(py_feature)?;
699687
}
700-
Ok(Some(
701-
x509_module
702-
.getattr(pyo3::intern!(py, "TLSFeature"))?
703-
.call1((features,))?,
704-
))
688+
Ok(Some(types::TLS_FEATURE.get(py)?.call1((features,))?))
705689
}
706690
oid::SUBJECT_KEY_IDENTIFIER_OID => {
707691
let identifier = ext.value::<&[u8]>()?;
708692
Ok(Some(
709-
x509_module
710-
.getattr(pyo3::intern!(py, "SubjectKeyIdentifier"))?
693+
types::SUBJECT_KEY_IDENTIFIER
694+
.get(py)?
711695
.call1((identifier,))?,
712696
))
713697
}
@@ -717,101 +701,71 @@ pub fn parse_cert_ext<'p>(
717701
let oid_obj = oid_to_py_oid(py, &oid)?;
718702
ekus.append(oid_obj)?;
719703
}
720-
Ok(Some(
721-
x509_module
722-
.getattr(pyo3::intern!(py, "ExtendedKeyUsage"))?
723-
.call1((ekus,))?,
724-
))
704+
Ok(Some(types::EXTENDED_KEY_USAGE.get(py)?.call1((ekus,))?))
725705
}
726706
oid::KEY_USAGE_OID => {
727707
let kus = ext.value::<KeyUsage<'_>>()?;
728708

729-
Ok(Some(
730-
x509_module.getattr(pyo3::intern!(py, "KeyUsage"))?.call1((
731-
kus.digital_signature(),
732-
kus.content_comitment(),
733-
kus.key_encipherment(),
734-
kus.data_encipherment(),
735-
kus.key_agreement(),
736-
kus.key_cert_sign(),
737-
kus.crl_sign(),
738-
kus.encipher_only(),
739-
kus.decipher_only(),
740-
))?,
741-
))
709+
Ok(Some(types::KEY_USAGE.get(py)?.call1((
710+
kus.digital_signature(),
711+
kus.content_comitment(),
712+
kus.key_encipherment(),
713+
kus.data_encipherment(),
714+
kus.key_agreement(),
715+
kus.key_cert_sign(),
716+
kus.crl_sign(),
717+
kus.encipher_only(),
718+
kus.decipher_only(),
719+
))?))
742720
}
743721
oid::AUTHORITY_INFORMATION_ACCESS_OID => {
744722
let ads = parse_access_descriptions(py, ext)?;
745723
Ok(Some(
746-
x509_module
747-
.getattr(pyo3::intern!(py, "AuthorityInformationAccess"))?
748-
.call1((ads,))?,
724+
types::AUTHORITY_INFORMATION_ACCESS.get(py)?.call1((ads,))?,
749725
))
750726
}
751727
oid::SUBJECT_INFORMATION_ACCESS_OID => {
752728
let ads = parse_access_descriptions(py, ext)?;
753729
Ok(Some(
754-
x509_module
755-
.getattr(pyo3::intern!(py, "SubjectInformationAccess"))?
756-
.call1((ads,))?,
730+
types::SUBJECT_INFORMATION_ACCESS.get(py)?.call1((ads,))?,
757731
))
758732
}
759733
oid::CERTIFICATE_POLICIES_OID => {
760734
let cp = parse_cp(py, ext)?;
761-
Ok(Some(x509_module.call_method1(
762-
pyo3::intern!(py, "CertificatePolicies"),
763-
(cp,),
764-
)?))
735+
Ok(Some(types::CERTIFICATE_POLICIES.get(py)?.call1((cp,))?))
765736
}
766737
oid::POLICY_CONSTRAINTS_OID => {
767738
let pc = ext.value::<PolicyConstraints>()?;
768-
Ok(Some(
769-
x509_module
770-
.getattr(pyo3::intern!(py, "PolicyConstraints"))?
771-
.call1((pc.require_explicit_policy, pc.inhibit_policy_mapping))?,
772-
))
739+
Ok(Some(types::POLICY_CONSTRAINTS.get(py)?.call1((
740+
pc.require_explicit_policy,
741+
pc.inhibit_policy_mapping,
742+
))?))
773743
}
774744
oid::OCSP_NO_CHECK_OID => {
775745
ext.value::<()>()?;
776-
Ok(Some(
777-
x509_module
778-
.getattr(pyo3::intern!(py, "OCSPNoCheck"))?
779-
.call0()?,
780-
))
746+
Ok(Some(types::OCSP_NO_CHECK.get(py)?.call0()?))
781747
}
782748
oid::INHIBIT_ANY_POLICY_OID => {
783749
let bignum = ext.value::<asn1::BigUint<'_>>()?;
784750
let pynum = big_byte_slice_to_py_int(py, bignum.as_bytes())?;
785-
Ok(Some(
786-
x509_module
787-
.getattr(pyo3::intern!(py, "InhibitAnyPolicy"))?
788-
.call1((pynum,))?,
789-
))
751+
Ok(Some(types::INHIBIT_ANY_POLICY.get(py)?.call1((pynum,))?))
790752
}
791753
oid::BASIC_CONSTRAINTS_OID => {
792754
let bc = ext.value::<BasicConstraints>()?;
793755
Ok(Some(
794-
x509_module
795-
.getattr(pyo3::intern!(py, "BasicConstraints"))?
756+
types::BASIC_CONSTRAINTS
757+
.get(py)?
796758
.call1((bc.ca, bc.path_length))?,
797759
))
798760
}
799761
oid::AUTHORITY_KEY_IDENTIFIER_OID => Ok(Some(parse_authority_key_identifier(py, ext)?)),
800762
oid::CRL_DISTRIBUTION_POINTS_OID => {
801763
let dp = parse_distribution_points(py, ext)?;
802-
Ok(Some(
803-
x509_module
804-
.getattr(pyo3::intern!(py, "CRLDistributionPoints"))?
805-
.call1((dp,))?,
806-
))
764+
Ok(Some(types::CRL_DISTRIBUTION_POINTS.get(py)?.call1((dp,))?))
807765
}
808766
oid::FRESHEST_CRL_OID => {
809767
let dp = parse_distribution_points(py, ext)?;
810-
Ok(Some(
811-
x509_module
812-
.getattr(pyo3::intern!(py, "FreshestCRL"))?
813-
.call1((dp,))?,
814-
))
768+
Ok(Some(types::FRESHEST_CRL.get(py)?.call1((dp,))?))
815769
}
816770
oid::NAME_CONSTRAINTS_OID => {
817771
let nc = ext.value::<NameConstraints<'_>>()?;
@@ -824,19 +778,19 @@ pub fn parse_cert_ext<'p>(
824778
None => py.None(),
825779
};
826780
Ok(Some(
827-
x509_module
828-
.getattr(pyo3::intern!(py, "NameConstraints"))?
781+
types::NAME_CONSTRAINTS
782+
.get(py)?
829783
.call1((permitted_subtrees, excluded_subtrees))?,
830784
))
831785
}
832786
oid::MS_CERTIFICATE_TEMPLATE => {
833787
let ms_cert_tpl = ext.value::<MSCertificateTemplate>()?;
834788
let py_oid = oid_to_py_oid(py, &ms_cert_tpl.template_id)?;
835-
Ok(Some(
836-
x509_module
837-
.getattr(pyo3::intern!(py, "MSCertificateTemplate"))?
838-
.call1((py_oid, ms_cert_tpl.major_version, ms_cert_tpl.minor_version))?,
839-
))
789+
Ok(Some(types::MS_CERTIFICATE_TEMPLATE.get(py)?.call1((
790+
py_oid,
791+
ms_cert_tpl.major_version,
792+
ms_cert_tpl.minor_version,
793+
))?))
840794
}
841795
_ => Ok(None),
842796
}

0 commit comments

Comments
 (0)