yasna/models/der.rs
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
9use alloc::vec::Vec;
10use super::super::{PCBit, Tag};
11use 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)]
20pub struct TaggedDerValue {
21 tag: Tag,
22 pcbit: PCBit,
23 value: Vec<u8>,
24}
25
26impl TaggedDerValue {
27 /// Constructs a new `TaggedDerValue` as an octet string
28 pub fn from_octetstring(bytes: Vec<u8>) -> Self {
29 TaggedDerValue {
30 tag: TAG_OCTETSTRING,
31 pcbit: PCBit::Primitive,
32 value: bytes,
33 }
34 }
35
36 /// Constructs a new `TaggedDerValue` from its tag and content
37 pub fn from_tag_and_bytes(tag: Tag, bytes: Vec<u8>) -> Self {
38 let pcbit = match tag {
39 TAG_SEQUENCE | TAG_SET => PCBit::Constructed,
40 _ => PCBit::Primitive,
41 };
42 TaggedDerValue {
43 tag,
44 pcbit,
45 value: bytes,
46 }
47 }
48
49 /// Constructs a new `TaggedDerValue` from its tag,
50 /// primitive/constructed bit, and content
51 pub fn from_tag_pc_and_bytes(tag: Tag, pcbit: PCBit, bytes: Vec<u8>) -> Self {
52 TaggedDerValue {
53 tag,
54 pcbit,
55 value: bytes,
56 }
57 }
58
59 /// Returns the tag
60 pub fn tag(&self) -> Tag {
61 self.tag
62 }
63
64 /// Returns the primitive/constructed bit
65 pub fn pcbit(&self) -> PCBit {
66 self.pcbit
67 }
68
69 /// Returns the value
70 pub fn value(&self) -> &[u8] {
71 &self.value
72 }
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 pub fn as_bytes(&self) -> Option<&[u8]> {
84 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 if let Some(&0) = self.value.first() {
89 Some(&self.value[1..])
90 } else {
91 None
92 }
93 },
94 (TAG_OCTETSTRING, PCBit::Primitive) => Some(&self.value),
95 _ => None
96 }
97 }
98
99 /// If the value is something string-like, returns it as string.
100 pub fn as_str(&self) -> Option<&str> {
101 use alloc::str::from_utf8;
102
103 match (self.tag, self.pcbit) {
104 (TAG_IA5STRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
105 (TAG_PRINTABLESTRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
106 (TAG_UTF8STRING, PCBit::Primitive) => from_utf8(&self.value).ok(),
107 _ => None
108 }
109 }
110}