|
12 | 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
13 | 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
14 | 14 |
|
| 15 | +#[cfg(feature = "alloc")] |
| 16 | +use alloc::string::String; |
| 17 | +#[cfg(feature = "alloc")] |
| 18 | +use core::fmt; |
| 19 | + |
15 | 20 | use super::dns_name::{self, IdRole}; |
16 | 21 | use super::ip_address; |
17 | 22 | use crate::der::{self, FromDer}; |
@@ -295,3 +300,136 @@ impl<'a> FromDer<'a> for GeneralName<'a> { |
295 | 300 |
|
296 | 301 | const TYPE_ID: DerTypeId = DerTypeId::GeneralName; |
297 | 302 | } |
| 303 | + |
| 304 | +#[cfg(feature = "alloc")] |
| 305 | +impl fmt::Debug for GeneralName<'_> { |
| 306 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 307 | + match self { |
| 308 | + GeneralName::DnsName(name) => write!( |
| 309 | + f, |
| 310 | + "DnsName(\"{}\")", |
| 311 | + String::from_utf8_lossy(name.as_slice_less_safe()) |
| 312 | + ), |
| 313 | + GeneralName::DirectoryName => write!(f, "DirectoryName"), |
| 314 | + GeneralName::IpAddress(ip) => { |
| 315 | + write!(f, "IpAddress({:?})", IpAddrSlice(ip.as_slice_less_safe())) |
| 316 | + } |
| 317 | + GeneralName::UniformResourceIdentifier(uri) => write!( |
| 318 | + f, |
| 319 | + "UniformResourceIdentifier(\"{}\")", |
| 320 | + String::from_utf8_lossy(uri.as_slice_less_safe()) |
| 321 | + ), |
| 322 | + GeneralName::Unsupported(tag) => write!(f, "Unsupported({})", tag), |
| 323 | + } |
| 324 | + } |
| 325 | +} |
| 326 | + |
| 327 | +#[cfg(feature = "alloc")] |
| 328 | +struct IpAddrSlice<'a>(&'a [u8]); |
| 329 | + |
| 330 | +#[cfg(feature = "alloc")] |
| 331 | +impl fmt::Debug for IpAddrSlice<'_> { |
| 332 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 333 | + match self.0.len() { |
| 334 | + 4 => { |
| 335 | + let mut first = true; |
| 336 | + for byte in self.0 { |
| 337 | + match first { |
| 338 | + true => first = false, |
| 339 | + false => f.write_str(".")?, |
| 340 | + } |
| 341 | + |
| 342 | + write!(f, "{}", byte)?; |
| 343 | + } |
| 344 | + |
| 345 | + Ok(()) |
| 346 | + } |
| 347 | + 16 => { |
| 348 | + let mut first = true; |
| 349 | + for group in self.0.chunks_exact(2) { |
| 350 | + match first { |
| 351 | + true => first = false, |
| 352 | + false => f.write_str(":")?, |
| 353 | + } |
| 354 | + |
| 355 | + match group { |
| 356 | + [0, 0] => f.write_str("0")?, |
| 357 | + _ => write!(f, "{:02x}{:02x}", group[0], group[1])?, |
| 358 | + } |
| 359 | + } |
| 360 | + Ok(()) |
| 361 | + } |
| 362 | + _ => { |
| 363 | + f.write_str("[invalid: ")?; |
| 364 | + let mut first = true; |
| 365 | + for byte in self.0 { |
| 366 | + match first { |
| 367 | + true => first = false, |
| 368 | + false => f.write_str(", ")?, |
| 369 | + } |
| 370 | + write!(f, "{:02x}", byte)?; |
| 371 | + } |
| 372 | + f.write_str("]") |
| 373 | + } |
| 374 | + } |
| 375 | + } |
| 376 | +} |
| 377 | + |
| 378 | +#[cfg(all(test, feature = "alloc"))] |
| 379 | +mod tests { |
| 380 | + use super::*; |
| 381 | + |
| 382 | + #[test] |
| 383 | + fn debug_names() { |
| 384 | + assert_eq!( |
| 385 | + format!( |
| 386 | + "{:?}", |
| 387 | + GeneralName::DnsName(untrusted::Input::from(b"example.com")) |
| 388 | + ), |
| 389 | + "DnsName(\"example.com\")" |
| 390 | + ); |
| 391 | + |
| 392 | + assert_eq!(format!("{:?}", GeneralName::DirectoryName), "DirectoryName"); |
| 393 | + |
| 394 | + assert_eq!( |
| 395 | + format!( |
| 396 | + "{:?}", |
| 397 | + GeneralName::IpAddress(untrusted::Input::from(&[192, 0, 2, 1][..])) |
| 398 | + ), |
| 399 | + "IpAddress(192.0.2.1)" |
| 400 | + ); |
| 401 | + |
| 402 | + assert_eq!( |
| 403 | + format!( |
| 404 | + "{:?}", |
| 405 | + GeneralName::IpAddress(untrusted::Input::from( |
| 406 | + &[0x20, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0d, 0xb8][..] |
| 407 | + )) |
| 408 | + ), |
| 409 | + "IpAddress(2001:0:0:0:0:0:0:0db8)" |
| 410 | + ); |
| 411 | + |
| 412 | + assert_eq!( |
| 413 | + format!( |
| 414 | + "{:?}", |
| 415 | + GeneralName::IpAddress(untrusted::Input::from(&[1, 2, 3, 4, 5, 6][..])) |
| 416 | + ), |
| 417 | + "IpAddress([invalid: 01, 02, 03, 04, 05, 06])" |
| 418 | + ); |
| 419 | + |
| 420 | + assert_eq!( |
| 421 | + format!( |
| 422 | + "{:?}", |
| 423 | + GeneralName::UniformResourceIdentifier(untrusted::Input::from( |
| 424 | + b"https://example.com" |
| 425 | + )) |
| 426 | + ), |
| 427 | + "UniformResourceIdentifier(\"https://example.com\")" |
| 428 | + ); |
| 429 | + |
| 430 | + assert_eq!( |
| 431 | + format!("{:?}", GeneralName::Unsupported(0x42)), |
| 432 | + "Unsupported(66)" |
| 433 | + ); |
| 434 | + } |
| 435 | +} |
0 commit comments