1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
//! BER-encoded Restricted Character String types.
//!
//! This is an internal module. It’s public items are re-exported by the
//! parent.
use std::{char, cmp, fmt, hash, ops, str};
use std::borrow::Cow;
use std::marker::PhantomData;
use bytes::Bytes;
use crate::{decode, encode};
use crate::decode::DecodeError;
use crate::tag::Tag;
use super::octet::{OctetString, OctetStringIter, OctetStringOctets};
//------------ CharSet -------------------------------------------------------
/// The character set of a restricted character string type.
///
/// The trait only includes associated functions and can thus be implemented
/// for marker types. It main purpose is to take an iterator over `u8`s and
/// produce `char`s or errors. This happens in [`next_char`].
///
/// The trait is primarily used to define the character set of the
/// [`RestrictedString`] type.
///
/// [`next_char`]: #tymethod.next_char
/// [`RestrictedString`]: struct.RestrictedString.html
pub trait CharSet {
/// The natural tag of the related restricted character string type.
const TAG: Tag;
/// Returns the next character from a octet sequence.
fn next_char<I: Iterator<Item=u8>>(
iter: &mut I
) -> Result<Option<char>, CharSetError>;
/// Converts a `str` into a octet sequence.
///
/// The method takes a `str` and converts it into one of three things.
/// If the string can be encoded in this character set and its own octet
/// sequence is identical to the encoded sequence, it returns its octet
/// sequence as a `Ok(Cow::Borrowed(_))`. If the octet sequence differs,
/// it creates that and returns it as a `Ok(Cow::Owned(_))`. Finally, if
/// the string cannot be encoded in this character set, it returns an
/// error.
fn from_str(s: &str) -> Result<Cow<[u8]>, CharSetError>;
/// Checks whether a sequence of octets is a valid string.
///
/// The method returns an error if the sequence of the octets represented
/// by `iter` is not in fact a valid string for this character set.
fn check<I: Iterator<Item=u8>>(iter: &mut I) -> Result<(), CharSetError> {
while Self::next_char(iter)?.is_some() { }
Ok(())
}
}
//------------ RestrictedString ----------------------------------------------
/// A generic restricted character string.
///
/// Restricted character strings essentially are a sequence of characters from
/// a specific character set mapped into a sequence of octets. In BER, these
/// are in fact encoded just like an [`OctetString`] with a different tag.
/// Consequently, this type is a wrapper around [`OctetString`] that makes
/// sure that the sequence of octets is correctly encoded for the given
/// character set.
///
/// As usual, you can parse a restricted character string from encoded data
/// by way of the [`take_from`] and [`from_content`] methods.
/// Alternatively, you can create a new value from a `String` or `str` via
/// the [`from_string`] and [`from_str`] associated functions.
///
/// Conversely, a restricted character string can be converted into a string
/// by way of the [`to_string`] and [`to_str`] methods.
///
/// In addition, the restricted character string mirrors the standard
/// library’s string types by dereffing to [`OctetString`] and using the
/// octet string’s iterators. In addition, the [`chars`] method provides an
/// iterator over the characters encoded in the string.
///
/// [`CharSet`]: trait.CharSet.html
/// [`OctetString`]: struct.OctetString.html
/// [`take_from`]: #method.take_from
/// [`from_content`]: #method.take_content
/// [`from_string`]: #method.from_string
/// [`from_str`]: #method.from_str
/// [`to_string`]: #method.to_string
/// [`to_str`]: #method.to_str
/// [`chars`]: #method.chars
#[derive(Clone, Debug)]
pub struct RestrictedString<L: CharSet> {
/// The underlying octet string.
octets: OctetString,
/// Marker for our character set.
marker: PhantomData<L>
}
impl<L: CharSet> RestrictedString<L> {
/// Creates a new character string without any checks.
unsafe fn new_unchecked(octets: OctetString) -> Self {
RestrictedString {
octets,
marker: PhantomData
}
}
/// Creates a new character string from an octet string.
///
/// If the octet string contains octet sequences that are not valid for
/// the character set, an appropriate error will be returned.
pub fn new(os: OctetString) -> Result<Self, CharSetError> {
L::check(&mut os.octets())?;
Ok(unsafe { Self::new_unchecked(os) })
}
/// Creates a new character string from a `String`.
///
/// If the string’s internal representation is identical to the encoding
/// of restricted character string, the string will be reused and no
/// allocation occurs. Otherwise, a new bytes value is created.
///
/// If the string cannot be encoded in the character set, an error is
/// returned.
pub fn from_string(s: String) -> Result<Self, CharSetError> {
let octets = match L::from_str(s.as_ref())? {
Cow::Borrowed(_) => s.into_bytes().into(),
Cow::Owned(owned) => owned.into(),
};
Ok(unsafe { Self::new_unchecked(OctetString::new(octets)) })
}
/// Returns an iterator over the character in the character string.
pub fn chars(&self) -> RestrictedStringChars<L> {
RestrictedStringChars::new(self.octets.octets())
}
/// Converts the string into its underlying bytes.
///
/// Note that the bytes value will contain the raw octets of the string
/// which are not necessarily a valid Rust string.
pub fn into_bytes(self) -> Bytes {
self.octets.into_bytes()
}
}
/// # Decoding and Encoding
///
impl<L: CharSet> RestrictedString<L> {
/// Takes a single character set value from constructed value content.
///
/// If there is no next value, if the next value does not have the natural
/// tag appropriate for this character set implementation, or if it does
/// not contain a correctly encoded character string, a malformed error is
/// returned.
pub fn take_from<S: decode::Source>(
cons: &mut decode::Constructed<S>
) -> Result<Self, DecodeError<S::Error>> {
cons.take_value_if(L::TAG, Self::from_content)
}
/// Takes a character set from content.
pub fn from_content<S: decode::Source>(
content: &mut decode::Content<S>
) -> Result<Self, DecodeError<S::Error>> {
let os = OctetString::from_content(content)?;
Self::new(os).map_err(|_| content.content_err("invalid character"))
}
/// Returns a value encoder for the character string with the natural tag.
pub fn encode(self) -> impl encode::Values {
self.encode_as(L::TAG)
}
/// Returns a value encoder for the character string with the given tag.
pub fn encode_as(self, tag: Tag) -> impl encode::Values {
self.octets.encode_as(tag)
}
/// Returns a value encoder for the character string with the natural tag.
pub fn encode_ref(&self) -> impl encode::Values + '_ {
self.encode_ref_as(L::TAG)
}
/// Returns a value encoder for the character string with the given tag.
pub fn encode_ref_as(&self, tag: Tag) -> impl encode::Values + '_ {
self.octets.encode_ref_as(tag)
}
}
//--- FromStr
impl<L: CharSet> str::FromStr for RestrictedString<L> {
type Err = CharSetError;
fn from_str(s: &str) -> Result<Self, CharSetError> {
Ok(unsafe { Self::new_unchecked(OctetString::new(
L::from_str(s)?.into_owned().into()
))})
}
}
//--- Deref and AsRef
impl<L: CharSet> ops::Deref for RestrictedString<L> {
type Target = OctetString;
fn deref(&self) -> &OctetString {
self.as_ref()
}
}
impl<L: CharSet> AsRef<OctetString> for RestrictedString<L> {
fn as_ref(&self) -> &OctetString {
&self.octets
}
}
//--- PartialEq and Eq, PartialOrd and Ord
//
// We only supply PartialEq<Self> because two identical octet sequences in
// different character sets can mean different things.
//
// XXX Once specialization lands we can bring back the generic comparision
// and implement it over `Self::chars`.
impl<L: CharSet> PartialEq for RestrictedString<L> {
fn eq(&self, other: &Self) -> bool {
self.octets.eq(&other.octets)
}
}
impl<L: CharSet> Eq for RestrictedString<L> { }
impl<L: CharSet> PartialOrd for RestrictedString<L> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<L: CharSet> Ord for RestrictedString<L> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.octets.cmp(&other.octets)
}
}
//--- Hash
impl<L: CharSet> hash::Hash for RestrictedString<L> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.octets.hash(state)
}
}
//--- IntoIterator
impl<'a, L: CharSet> IntoIterator for &'a RestrictedString<L> {
type Item = &'a [u8];
type IntoIter = OctetStringIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
//--- Display
impl<L: CharSet> fmt::Display for RestrictedString<L> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.chars().try_for_each(|ch| fmt::Display::fmt(&ch, fmt))
}
}
//------------ RestrictedStringChars ------------------------------------------
/// An iterator over the characters in a restricted character string.
///
/// You can obtain a value of this type via a restricted string’s [`chars`]
/// method.
///
/// [`chars`]: struct.RestrictedStringChars.html#method.chars
#[derive(Clone, Debug)]
pub struct RestrictedStringChars<'a, L: CharSet> {
/// The underlying octet iterator.
octets: OctetStringOctets<'a>,
/// Our character set.
marker: PhantomData<L>,
}
impl<'a, L: CharSet> RestrictedStringChars<'a, L> {
/// Creates a new character iterator from an octet iterator.
fn new(octets: OctetStringOctets<'a>) -> Self {
RestrictedStringChars {
octets,
marker: PhantomData
}
}
}
impl<'a, L: CharSet> Iterator for RestrictedStringChars<'a, L> {
type Item = char;
fn next(&mut self) -> Option<char> {
L::next_char(&mut self.octets).unwrap()
}
}
//============ Concrete Restricted String Types ==============================
//------------ Utf8String ----------------------------------------------------
/// A restricted character string containing UTF-8 encoded characters.
///
/// This character string allows all Unicode code points. It represents them
/// as a sequence of octets according to the UTF-8 encoding defined in
/// [RFC 3629].
///
/// See [`RestrictedString`] for more details on restricged character strings
/// in general.
///
/// [`RestrictedString`]: struct.RestrictedString.html
/// [RFC 3629]: https://tools.ietf.org/html/rfc3629
pub type Utf8String = RestrictedString<Utf8CharSet>;
/// The character set for the UTF8String ASN.1 type.
#[derive(Clone, Copy, Debug)]
pub struct Utf8CharSet;
impl CharSet for Utf8CharSet {
const TAG: Tag = Tag::UTF8_STRING;
fn next_char<I: Iterator<Item=u8>>(
iter: &mut I
) -> Result<Option<char>, CharSetError> {
let first = match iter.next() {
Some(ch) => ch,
None => return Ok(None)
};
if first < 0x80 {
return Ok(Some(char::from(first)))
}
let second = match iter.next() {
Some(ch) => ch,
None => return Err(CharSetError),
};
if first < 0xC0 || second < 0x80 {
return Err(CharSetError)
}
if first < 0xE0 {
return Ok(Some(unsafe {
char::from_u32_unchecked(
(u32::from(first & 0x1F)) << 6 |
u32::from(second & 0x3F)
)
}))
}
let third = match iter.next() {
Some(ch) => ch,
None => return Err(CharSetError)
};
if third < 0x80 {
return Err(CharSetError)
}
if first < 0xF0 {
return Ok(Some(unsafe {
char::from_u32_unchecked(
(u32::from(first & 0x0F)) << 12 |
(u32::from(second & 0x3F)) << 6 |
u32::from(third & 0x3F)
)
}))
}
let fourth = match iter.next() {
Some(ch) => ch,
None => return Err(CharSetError)
};
if first > 0xF7 || fourth < 0x80 {
return Err(CharSetError)
}
Ok(Some(unsafe {
char::from_u32_unchecked(
(u32::from(first & 0x07)) << 18 |
(u32::from(second & 0x3F)) << 12 |
(u32::from(third & 0x3F)) << 6 |
u32::from(fourth & 0x3F)
)
}))
}
fn from_str(s: &str) -> Result<Cow<[u8]>, CharSetError> {
Ok(Cow::Borrowed(s.as_bytes()))
}
}
//------------ NumericString -------------------------------------------------
/// A restricted character string containing only digits and spaces.
///
/// This character string allows only decimal digits `0` to `9` and the
/// space character ` `. It encodes them with their ASCII value.
///
/// See [`RestrictedString`] for more details on restricged character strings
/// in general.
///
/// [`RestrictedString`]: struct.RestrictedString.html
pub type NumericString = RestrictedString<NumericCharSet>;
/// The character set for the NumericString ASN.1 type.
#[derive(Clone, Copy, Debug)]
pub struct NumericCharSet;
impl CharSet for NumericCharSet {
const TAG: Tag = Tag::NUMERIC_STRING;
fn next_char<I: Iterator<Item=u8>>(
iter: &mut I
) -> Result<Option<char>, CharSetError> {
match iter.next() {
Some(ch) if ch == b' ' || ch.is_ascii_digit() => {
Ok(Some(char::from(ch)))
}
Some(_) => Err(CharSetError),
None => Ok(None)
}
}
fn from_str(s: &str) -> Result<Cow<[u8]>, CharSetError> {
Ok(Cow::Borrowed(s.as_bytes()))
}
}
//------------ PrintableString -----------------------------------------------
/// A restricted character string allowing a subset of ASCII characters.
///
/// This character string allows the following characters from the ASCII
/// character set and encodes them with their ASCII value:
///
/// * the letters `A` to `Z` and `a` to `z`,
/// * the digits `0` to `9`,
/// * the space character ` `,
/// * the symbols `'`, `(`, `)`, `+`, `,`, `-`, `.`, `/`, `:`, `=`, and `?`.
///
/// See [`RestrictedString`] for more details on restricged character strings
/// in general.
///
/// [`RestrictedString`]: struct.RestrictedString.html
pub type PrintableString = RestrictedString<PrintableCharSet>;
/// The character set for the PrintableString ASN.1 type.
#[derive(Clone, Debug)]
pub struct PrintableCharSet;
impl CharSet for PrintableCharSet {
const TAG: Tag = Tag::PRINTABLE_STRING;
fn next_char<I: Iterator<Item=u8>>(
iter: &mut I
) -> Result<Option<char>, CharSetError> {
match iter.next() {
Some(x) if x.is_ascii_alphanumeric() || // A-Z a-z 0-9
x == b' ' || x == b'\'' || x == b'(' || x == b')' ||
x == b'+' || x == b',' || x == b'-' || x == b'.' ||
x == b'/' || x == b':' || x == b'=' || x == b'?' => {
Ok(Some(char::from(x)))
}
Some(_) => Err(CharSetError),
None => Ok(None)
}
}
fn from_str(s: &str) -> Result<Cow<[u8]>, CharSetError> {
Ok(Cow::Borrowed(s.as_bytes()))
}
}
//------------ Ia5String -----------------------------------------------------
/// A restricted character string containing ASCII characters.
///
/// This character string allows all ASCII characters (i.e., octets with
/// values `0x00` to `0x7F`) and encodes them with their ASCII value.
///
/// The type’s name is derived from the name used in ASN.1. It is derived
/// from the name IA5 or International Alphabet No. 5 which is the ITU name
/// for ASCII and is specified in ITU.T recommendation T.50.
///
/// See [`RestrictedString`] for more details on restricged character strings
/// in general.
///
/// [`RestrictedString`]: struct.RestrictedString.html
pub type Ia5String = RestrictedString<Ia5CharSet>;
/// The character set for the IA5String ASN.1 type.
#[derive(Clone, Copy, Debug)]
pub struct Ia5CharSet;
impl CharSet for Ia5CharSet {
const TAG: Tag = Tag::IA5_STRING;
fn next_char<I: Iterator<Item=u8>>(
iter: &mut I
) -> Result<Option<char>, CharSetError> {
match iter.next() {
Some(ch) if ch.is_ascii() => Ok(Some(char::from(ch))),
Some(_) => Err(CharSetError),
None => Ok(None)
}
}
fn from_str(s: &str) -> Result<Cow<[u8]>, CharSetError> {
Ok(Cow::Borrowed(s.as_bytes()))
}
}
//------------ CharSetError --------------------------------------------------
/// An illegal value was encountered during character set conversion.
#[derive(Debug)]
pub struct CharSetError;
//------------ Testing -------------------------------------------------------
#[cfg(test)]
mod test {
use super::*;
use bytes::Bytes;
use crate::decode::IntoSource;
use crate::mode::Mode;
use crate::encode::Values;
#[test]
fn should_encode_decode_printable_string() {
let os = OctetString::new(Bytes::from_static(b"This is okay"));
let ps = PrintableString::new(os).unwrap();
let mut v = Vec::new();
ps.encode_ref().write_encoded(Mode::Der, &mut v).unwrap();
let decoded = Mode::Der.decode(
v.as_slice().into_source(),
PrintableString::take_from
).unwrap();
assert_eq!(ps, decoded);
}
#[test]
fn should_restrict_printable_string() {
let os = OctetString::new(Bytes::from_static(b"This is wrong!"));
assert!(PrintableString::new(os).is_err());
}
}