|
2 | 2 | // 2.0, and the BSD License. See the LICENSE file in the root of this repository |
3 | 3 | // for complete details. |
4 | 4 |
|
| 5 | +use asn1::IA5String as Asn1IA5String; |
5 | 6 | use asn1::PrintableString as Asn1PrintableString; |
6 | 7 | use asn1::SimpleAsn1Readable; |
7 | 8 | use asn1::UtcTime as Asn1UtcTime; |
@@ -36,6 +37,8 @@ pub enum Type { |
36 | 37 | PyStr(), |
37 | 38 | /// PrintableString (`str`) |
38 | 39 | PrintableString(), |
| 40 | + /// IA5String (`str`) |
| 41 | + IA5String(), |
39 | 42 | /// UtcTime (`datetime`) |
40 | 43 | UtcTime(), |
41 | 44 | /// GeneralizedTime (`datetime`) |
@@ -162,6 +165,39 @@ impl PrintableString { |
162 | 165 | } |
163 | 166 | } |
164 | 167 |
|
| 168 | +#[derive(pyo3::FromPyObject)] |
| 169 | +#[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.asn1")] |
| 170 | +pub struct IA5String { |
| 171 | + pub(crate) inner: pyo3::Py<pyo3::types::PyString>, |
| 172 | +} |
| 173 | + |
| 174 | +#[pyo3::pymethods] |
| 175 | +impl IA5String { |
| 176 | + #[new] |
| 177 | + #[pyo3(signature = (inner,))] |
| 178 | + fn new(py: pyo3::Python<'_>, inner: pyo3::Py<pyo3::types::PyString>) -> pyo3::PyResult<Self> { |
| 179 | + if Asn1IA5String::new(&inner.to_cow(py)?).is_none() { |
| 180 | + return Err(pyo3::exceptions::PyValueError::new_err(format!( |
| 181 | + "invalid IA5String: {inner}" |
| 182 | + ))); |
| 183 | + } |
| 184 | + |
| 185 | + Ok(IA5String { inner }) |
| 186 | + } |
| 187 | + |
| 188 | + pub fn as_str(&self, py: pyo3::Python<'_>) -> pyo3::PyResult<pyo3::Py<pyo3::types::PyString>> { |
| 189 | + Ok(self.inner.clone_ref(py)) |
| 190 | + } |
| 191 | + |
| 192 | + fn __eq__(&self, py: pyo3::Python<'_>, other: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<bool> { |
| 193 | + (**self.inner.bind(py)).eq(other.inner.bind(py)) |
| 194 | + } |
| 195 | + |
| 196 | + pub fn __repr__(&self, py: pyo3::Python<'_>) -> pyo3::PyResult<String> { |
| 197 | + Ok(format!("IA5String({})", self.inner.bind(py).repr()?)) |
| 198 | + } |
| 199 | +} |
| 200 | + |
165 | 201 | #[derive(pyo3::FromPyObject)] |
166 | 202 | #[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.asn1")] |
167 | 203 | pub struct UtcTime { |
@@ -310,6 +346,8 @@ pub fn non_root_python_to_rust<'p>( |
310 | 346 | Type::PyBytes().into_pyobject(py) |
311 | 347 | } else if class.is(PrintableString::type_object(py)) { |
312 | 348 | Type::PrintableString().into_pyobject(py) |
| 349 | + } else if class.is(IA5String::type_object(py)) { |
| 350 | + Type::IA5String().into_pyobject(py) |
313 | 351 | } else if class.is(UtcTime::type_object(py)) { |
314 | 352 | Type::UtcTime().into_pyobject(py) |
315 | 353 | } else if class.is(GeneralizedTime::type_object(py)) { |
@@ -370,6 +408,7 @@ pub(crate) fn type_to_tag(t: &Type, encoding: &Option<pyo3::Py<Encoding>>) -> as |
370 | 408 | Type::PyBytes() => <&[u8] as SimpleAsn1Readable>::TAG, |
371 | 409 | Type::PyStr() => asn1::Utf8String::TAG, |
372 | 410 | Type::PrintableString() => asn1::PrintableString::TAG, |
| 411 | + Type::IA5String() => asn1::IA5String::TAG, |
373 | 412 | Type::UtcTime() => asn1::UtcTime::TAG, |
374 | 413 | Type::GeneralizedTime() => asn1::GeneralizedTime::TAG, |
375 | 414 | Type::BitString() => asn1::BitString::TAG, |
|
0 commit comments