Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/yasna-0.5.2/src/models/der.rs
Line
Count
Source
1
// Copyright 2017 Fortanix, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8
9
use alloc::vec::Vec;
10
use super::super::{PCBit, Tag};
11
use super::super::tags::*;
12
13
/// Container for a tag and arbitrary DER value.
14
///
15
/// When obtained by `BERReader::read_tagged_der` in DER mode,
16
/// the reader verifies that the payload is actually valid DER.
17
/// When constructed from bytes, the caller is responsible for
18
/// providing valid DER.
19
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
20
pub struct TaggedDerValue {
21
    tag: Tag,
22
    pcbit: PCBit,
23
    value: Vec<u8>,
24
}
25
26
impl TaggedDerValue {
27
    /// Constructs a new `TaggedDerValue` as an octet string
28
0
    pub fn from_octetstring(bytes: Vec<u8>) -> Self {
29
0
        TaggedDerValue {
30
0
            tag: TAG_OCTETSTRING,
31
0
            pcbit: PCBit::Primitive,
32
0
            value: bytes,
33
0
        }
34
0
    }
35
36
    /// Constructs a new `TaggedDerValue` from its tag and content
37
0
    pub fn from_tag_and_bytes(tag: Tag, bytes: Vec<u8>) -> Self {
38
0
        let pcbit = match tag {
39
0
            TAG_SEQUENCE | TAG_SET => PCBit::Constructed,
40
0
            _ => PCBit::Primitive,
41
        };
42
0
        TaggedDerValue {
43
0
            tag,
44
0
            pcbit,
45
0
            value: bytes,
46
0
        }
47
0
    }
48
49
    /// Constructs a new `TaggedDerValue` from its tag,
50
    /// primitive/constructed bit, and content
51
0
    pub fn from_tag_pc_and_bytes(tag: Tag, pcbit: PCBit, bytes: Vec<u8>) -> Self {
52
0
        TaggedDerValue {
53
0
            tag,
54
0
            pcbit,
55
0
            value: bytes,
56
0
        }
57
0
    }
58
59
    /// Returns the tag
60
0
    pub fn tag(&self) -> Tag {
61
0
        self.tag
62
0
    }
63
64
    /// Returns the primitive/constructed bit
65
0
    pub fn pcbit(&self) -> PCBit {
66
0
        self.pcbit
67
0
    }
68
69
    /// Returns the value
70
0
    pub fn value(&self) -> &[u8] {
71
0
        &self.value
72
0
    }
73
74
    /// If the value is something that contains raw bytes,
75
    /// returns its content.
76
    ///
77
    /// # Examples
78
    /// ```
79
    /// use yasna::models::TaggedDerValue;
80
    /// let value = TaggedDerValue::from_octetstring(vec![1, 2, 3, 4, 5, 6]);
81
    /// assert!(value.as_bytes() == Some(&[1, 2, 3, 4, 5, 6]));
82
    /// ```
83
0
    pub fn as_bytes(&self) -> Option<&[u8]> {
84
0
        match (self.tag, self.pcbit) {
85
            (TAG_BITSTRING, PCBit::Primitive) => {
86
                // First byte of bitstring value is number of unused bits.
87
                // We only accept bitstrings that are multiples of bytes.
88
0
                if let Some(&0) = self.value.first() {
89
0
                    Some(&self.value[1..])
90
                } else {
91
0
                    None
92
                }
93
            },
94
0
            (TAG_OCTETSTRING, PCBit::Primitive) => Some(&self.value),
95
0
            _ => None
96
        }
97
0
    }
98
99
    /// If the value is something string-like, returns it as string.
100
0
    pub fn as_str(&self) -> Option<&str> {
101
        use alloc::str::from_utf8;
102
103
0
        match (self.tag, self.pcbit) {
104
0
            (TAG_IA5STRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
105
0
            (TAG_PRINTABLESTRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
106
0
            (TAG_UTF8STRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
107
0
            _ => None
108
        }
109
0
    }
110
}