Debian Codegraph @

third_party/rust/nss-gk-api/src/der.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use crate::Error;
8
9 pub const TAG_INTEGER: u8 = 0x02;
10 pub const TAG_BIT_STRING: u8 = 0x03;
11 pub const TAG_OCTET_STRING: u8 = 0x04;
12 pub const TAG_NULL: u8 = 0x05;
13 pub const TAG_OBJECT_ID: u8 = 0x06;
14 pub const TAG_SEQUENCE: u8 = 0x30;
15
16 // Object identifiers in DER tag-length-value form
17 pub const OID_EC_PUBLIC_KEY_BYTES: &[u8] = &[
18 /* RFC 5480 (id-ecPublicKey) */
19 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01,
20 ];
21 pub const OID_SECP256R1_BYTES: &[u8] = &[
22 /* RFC 5480 (secp256r1) */
23 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
24 ];
25 pub const OID_ED25519_BYTES: &[u8] = &[/* RFC 8410 (id-ed25519) */ 0x2b, 0x65, 0x70];
26 pub const OID_RS256_BYTES: &[u8] = &[
27 /* RFC 4055 (sha256WithRSAEncryption) */
28 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
29 ];
30
31 pub const MAX_TAG_AND_LENGTH_BYTES: usize = 4;
32
33 pub fn write_tag_and_length(out: &mut Vec<u8>, tag: u8, len: usize) -> Result<(), crate::Error> {
34 if len > 0xFFFF {
35 return Err(crate::Error::InternalError);
36 }
37 out.push(tag);
38 if len > 0xFF {
39 out.push(0x82);
40 out.push((len >> 8) as u8);
41 out.push(len as u8);
42 } else if len > 0x7F {
43 out.push(0x81);
44 out.push(len as u8);
45 } else {
46 out.push(len as u8);
47 }
48 Ok(())
49 }
50
51 pub fn integer(val: &[u8]) -> Result<Vec<u8>, crate::Error> {
52 if val.is_empty() {
53 return Err(crate::Error::InvalidInput);
54 }
55 // trim leading zeros, leaving a single zero if the input is the zero vector.
56 let mut val = val;
57 while val.len() > 1 && val[0] == 0 {
58 val = &val[1..];
59 }
60 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + 1 + val.len());
61 if val[0] & 0x80 != 0 {
62 // needs zero prefix
63 write_tag_and_length(&mut out, TAG_INTEGER, 1 + val.len())?;
64 out.push(0x00);
65 out.extend_from_slice(val);
66 } else {
67 write_tag_and_length(&mut out, TAG_INTEGER, val.len())?;
68 out.extend_from_slice(val);
69 }
70 Ok(out)
71 }
72
73 pub fn bit_string(val: &[u8]) -> Result<Vec<u8>, crate::Error> {
74 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + 1 + val.len());
75 write_tag_and_length(&mut out, TAG_BIT_STRING, 1 + val.len())?;
76 out.push(0x00); // trailing bits aren't supported
77 out.extend_from_slice(val);
78 Ok(out)
79 }
80
81 pub fn null() -> Result<Vec<u8>, crate::Error> {
82 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES);
83 write_tag_and_length(&mut out, TAG_NULL, 0)?;
84 Ok(out)
85 }
86
87 pub fn object_id(val: &[u8]) -> Result<Vec<u8>, crate::Error> {
88 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + val.len());
89 write_tag_and_length(&mut out, TAG_OBJECT_ID, val.len())?;
90 out.extend_from_slice(val);
91 Ok(out)
92 }
93
94 pub fn sequence(items: &[&[u8]]) -> Result<Vec<u8>, crate::Error> {
95 let len = items.iter().map(|i| i.len()).sum();
96 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + len);
97 write_tag_and_length(&mut out, TAG_SEQUENCE, len)?;
98 for item in items {
99 out.extend_from_slice(item);
100 }
101 Ok(out)
102 }
103
104 pub fn octet_string(val: &[u8]) -> Result<Vec<u8>, crate::Error> {
105 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + val.len());
106 write_tag_and_length(&mut out, TAG_OCTET_STRING, val.len())?;
107 out.extend_from_slice(val);
108 Ok(out)
109 }
110
111 pub fn context_specific_explicit_tag(tag: u8, content: &[u8]) -> Result<Vec<u8>, crate::Error> {
112 let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + content.len());
113 write_tag_and_length(&mut out, 0xa0 + tag, content.len())?;
114 out.extend_from_slice(content);
115 Ok(out)
116 }
117
118 // Given "tag || len || value || rest" where tag and len are of length one, len is in [0, 127],
119 // and value is of length len, returns (value, rest)
120 pub fn expect_tag_with_short_len(tag: u8, z: &[u8]) -> Result<(&[u8], &[u8]), crate::Error> {
121 if z.is_empty() {
122 return Err(Error::InvalidInput);
123 }
124 let (h, z) = z.split_at(1);
125 if h[0] != tag || z.is_empty() {
126 return Err(Error::InvalidInput);
127 }
128 let (h, z) = z.split_at(1);
129 if h[0] >= 0x80 || h[0] as usize > z.len() {
130 return Err(Error::InvalidInput);
131 }
132 Ok(z.split_at(h[0] as usize))
133 }