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
// do not use the `asn1_string` macro, since types are not the same
// X.680 section 37.6 and X.690 section 8.21.7

use crate::*;
use alloc::borrow::Cow;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::iter::FromIterator;

/// ASN.1 `UniversalString` type
///
/// Note: parsing a `UniversalString` allocates memory since the UCS-4 to UTF-8 conversion requires a memory allocation.
#[derive(Debug, PartialEq)]
pub struct UniversalString<'a> {
    pub(crate) data: Cow<'a, str>,
}

impl<'a> UniversalString<'a> {
    pub const fn new(s: &'a str) -> Self {
        UniversalString {
            data: Cow::Borrowed(s),
        }
    }

    pub fn string(&self) -> String {
        self.data.to_string()
    }
}

impl<'a> AsRef<str> for UniversalString<'a> {
    fn as_ref(&self) -> &str {
        &self.data
    }
}

impl<'a> From<&'a str> for UniversalString<'a> {
    fn from(s: &'a str) -> Self {
        Self::new(s)
    }
}

impl From<String> for UniversalString<'_> {
    fn from(s: String) -> Self {
        Self {
            data: alloc::borrow::Cow::Owned(s),
        }
    }
}

impl<'a> core::convert::TryFrom<Any<'a>> for UniversalString<'a> {
    type Error = Error;

    fn try_from(any: Any<'a>) -> Result<UniversalString<'a>> {
        any.tag().assert_eq(Self::TAG)?;

        if any.data.len() % 4 != 0 {
            return Err(Error::StringInvalidCharset);
        }

        // read slice as big-endian UCS-4 string
        let v = &any
            .data
            .chunks(4)
            .map(|s| match s {
                [a, b, c, d] => {
                    let u32_val = ((*a as u32) << 24)
                        | ((*b as u32) << 16)
                        | ((*c as u32) << 8)
                        | (*d as u32);
                    char::from_u32(u32_val)
                }
                _ => unreachable!(),
            })
            .collect::<Option<Vec<_>>>()
            .ok_or(Error::StringInvalidCharset)?;

        let s = String::from_iter(v);
        let data = Cow::Owned(s);

        Ok(UniversalString { data })
    }
}

impl<'a> CheckDerConstraints for UniversalString<'a> {
    fn check_constraints(any: &Any) -> Result<()> {
        any.header.assert_primitive()?;
        Ok(())
    }
}

impl<'a> Tagged for UniversalString<'a> {
    const TAG: Tag = Tag::UniversalString;
}

#[cfg(feature = "std")]
impl ToDer for UniversalString<'_> {
    fn to_der_len(&self) -> Result<usize> {
        // UCS-4: 4 bytes per character
        let sz = self.data.as_bytes().len() * 4;
        if sz < 127 {
            // 1 (class+tag) + 1 (length) + len
            Ok(2 + sz)
        } else {
            // 1 (class+tag) + n (length) + len
            let n = Length::Definite(sz).to_der_len()?;
            Ok(1 + n + sz)
        }
    }

    fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
        let header = Header::new(
            Class::Universal,
            false,
            Self::TAG,
            Length::Definite(self.data.as_bytes().len() * 4),
        );
        header.write_der_header(writer).map_err(Into::into)
    }

    fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
        self.data
            .chars()
            .try_for_each(|c| writer.write(&(c as u32).to_be_bytes()[..]).map(|_| ()))?;
        Ok(self.data.as_bytes().len() * 4)
    }
}