| 1 |
use super::CryptoError; |
| 2 |
|
| 3 |
pub const TAG_INTEGER: u8 = 0x02; |
| 4 |
pub const TAG_BIT_STRING: u8 = 0x03; |
| 5 |
#[cfg(all(test, feature = "crypto_nss"))] |
| 6 |
pub const TAG_OCTET_STRING: u8 = 0x04; |
| 7 |
pub const TAG_NULL: u8 = 0x05; |
| 8 |
pub const TAG_OBJECT_ID: u8 = 0x06; |
| 9 |
pub const TAG_SEQUENCE: u8 = 0x30; |
| 10 |
|
| 11 |
// Object identifiers in DER tag-length-value form |
| 12 |
pub const OID_EC_PUBLIC_KEY_BYTES: &[u8] = &[ |
| 13 |
/* RFC 5480 (id-ecPublicKey) */ |
| 14 |
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, |
| 15 |
]; |
| 16 |
pub const OID_SECP256R1_BYTES: &[u8] = &[ |
| 17 |
/* RFC 5480 (secp256r1) */ |
| 18 |
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, |
| 19 |
]; |
| 20 |
pub const OID_ED25519_BYTES: &[u8] = &[/* RFC 8410 (id-ed25519) */ 0x2b, 0x65, 0x70]; |
| 21 |
pub const OID_RSA_ENCRYPTION_BYTES: &[u8] = &[ |
| 22 |
/* RFC 4055 (rsaEncryption) */ |
| 23 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, |
| 24 |
]; |
| 25 |
|
| 26 |
pub type Result<T> = std::result::Result<T, CryptoError>; |
| 27 |
|
| 28 |
const MAX_TAG_AND_LENGTH_BYTES: usize = 4; |
| 29 |
fn write_tag_and_length(out: &mut Vec<u8>, tag: u8, len: usize) -> Result<()> { |
| 30 |
if len > 0xFFFF { |
| 31 |
return Err(CryptoError::LibraryFailure); |
| 32 |
} |
| 33 |
out.push(tag); |
| 34 |
if len > 0xFF { |
| 35 |
out.push(0x82); |
| 36 |
out.push((len >> 8) as u8); |
| 37 |
out.push(len as u8); |
| 38 |
} else if len > 0x7F { |
| 39 |
out.push(0x81); |
| 40 |
out.push(len as u8); |
| 41 |
} else { |
| 42 |
out.push(len as u8); |
| 43 |
} |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
|
| 47 |
pub fn integer(val: &[u8]) -> Result<Vec<u8>> { |
| 48 |
if val.is_empty() { |
| 49 |
return Err(CryptoError::MalformedInput); |
| 50 |
} |
| 51 |
// trim leading zeros, leaving a single zero if the input is the zero vector. |
| 52 |
let mut val = val; |
| 53 |
while val.len() > 1 && val[0] == 0 { |
| 54 |
val = &val[1..]; |
| 55 |
} |
| 56 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + 1 + val.len()); |
| 57 |
if val[0] & 0x80 != 0 { |
| 58 |
// needs zero prefix |
| 59 |
write_tag_and_length(&mut out, TAG_INTEGER, 1 + val.len())?; |
| 60 |
out.push(0x00); |
| 61 |
out.extend_from_slice(val); |
| 62 |
} else { |
| 63 |
write_tag_and_length(&mut out, TAG_INTEGER, val.len())?; |
| 64 |
out.extend_from_slice(val); |
| 65 |
} |
| 66 |
Ok(out) |
| 67 |
} |
| 68 |
|
| 69 |
pub fn bit_string(val: &[u8]) -> Result<Vec<u8>> { |
| 70 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + 1 + val.len()); |
| 71 |
write_tag_and_length(&mut out, TAG_BIT_STRING, 1 + val.len())?; |
| 72 |
out.push(0x00); // trailing bits aren't supported |
| 73 |
out.extend_from_slice(val); |
| 74 |
Ok(out) |
| 75 |
} |
| 76 |
|
| 77 |
pub fn null() -> Result<Vec<u8>> { |
| 78 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES); |
| 79 |
write_tag_and_length(&mut out, TAG_NULL, 0)?; |
| 80 |
Ok(out) |
| 81 |
} |
| 82 |
|
| 83 |
pub fn object_id(val: &[u8]) -> Result<Vec<u8>> { |
| 84 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + val.len()); |
| 85 |
write_tag_and_length(&mut out, TAG_OBJECT_ID, val.len())?; |
| 86 |
out.extend_from_slice(val); |
| 87 |
Ok(out) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn sequence(items: &[&[u8]]) -> Result<Vec<u8>> { |
| 91 |
let len = items.iter().map(|i| i.len()).sum(); |
| 92 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + len); |
| 93 |
write_tag_and_length(&mut out, TAG_SEQUENCE, len)?; |
| 94 |
for item in items { |
| 95 |
out.extend_from_slice(item); |
| 96 |
} |
| 97 |
Ok(out) |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(all(test, feature = "crypto_nss"))] |
| 101 |
pub fn octet_string(val: &[u8]) -> Result<Vec<u8>> { |
| 102 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + val.len()); |
| 103 |
write_tag_and_length(&mut out, TAG_OCTET_STRING, val.len())?; |
| 104 |
out.extend_from_slice(val); |
| 105 |
Ok(out) |
| 106 |
} |
| 107 |
|
| 108 |
#[cfg(all(test, feature = "crypto_nss"))] |
| 109 |
pub fn context_specific_explicit_tag(tag: u8, content: &[u8]) -> Result<Vec<u8>> { |
| 110 |
let mut out = Vec::with_capacity(MAX_TAG_AND_LENGTH_BYTES + content.len()); |
| 111 |
write_tag_and_length(&mut out, 0xa0 + tag, content.len())?; |
| 112 |
out.extend_from_slice(content); |
| 113 |
Ok(out) |
| 114 |
} |
| 115 |
|
| 116 |
// Given "tag || len || value || rest" where tag and len are of length one, len is in [0, 127], |
| 117 |
// and value is of length len, returns (value, rest) |
| 118 |
#[cfg(all(test, feature = "crypto_nss"))] |
| 119 |
fn expect_tag_with_short_len(tag: u8, z: &[u8]) -> Result<(&[u8], &[u8])> { |
| 120 |
if z.is_empty() { |
| 121 |
return Err(CryptoError::MalformedInput); |
| 122 |
} |
| 123 |
let (h, z) = z.split_at(1); |
| 124 |
if h[0] != tag || z.is_empty() { |
| 125 |
return Err(CryptoError::MalformedInput); |
| 126 |
} |
| 127 |
let (h, z) = z.split_at(1); |
| 128 |
if h[0] >= 0x80 || h[0] as usize > z.len() { |
| 129 |
return Err(CryptoError::MalformedInput); |
| 130 |
} |
| 131 |
Ok(z.split_at(h[0] as usize)) |
| 132 |
} |
| 133 |
|
| 134 |
// Given a DER encoded RFC 3279 Ecdsa-Sig-Value, |
| 135 |
// Ecdsa-Sig-Value ::= SEQUENCE { |
| 136 |
// r INTEGER, |
| 137 |
// s INTEGER }, |
| 138 |
// with r and s < 2^256, returns a 64 byte array containing |
| 139 |
// r and s encoded as 32 byte zero-padded big endian unsigned |
| 140 |
// integers |
| 141 |
#[cfg(all(test, feature = "crypto_nss"))] |
| 142 |
pub fn read_p256_sig(z: &[u8]) -> Result<Vec<u8>> { |
| 143 |
// Strip the tag and length. |
| 144 |
let (z, rest) = expect_tag_with_short_len(TAG_SEQUENCE, z)?; |
| 145 |
|
| 146 |
// The input should not have any trailing data. |
| 147 |
if !rest.is_empty() { |
| 148 |
return Err(CryptoError::MalformedInput); |
| 149 |
} |
| 150 |
|
| 151 |
let read_u256 = |z| -> Result<(&[u8], &[u8])> { |
| 152 |
let (r, z) = expect_tag_with_short_len(TAG_INTEGER, z)?; |
| 153 |
// We're expecting r < 2^256, so no more than 33 bytes as a signed integer. |
| 154 |
if r.is_empty() || r.len() > 33 { |
| 155 |
return Err(CryptoError::MalformedInput); |
| 156 |
} |
| 157 |
// If it is 33 bytes the leading byte must be zero. |
| 158 |
if r.len() == 33 && r[0] != 0 { |
| 159 |
return Err(CryptoError::MalformedInput); |
| 160 |
} |
| 161 |
// Ensure r is no more than 32 bytes. |
| 162 |
if r.len() == 33 { |
| 163 |
Ok((&r[1..], z)) |
| 164 |
} else { |
| 165 |
Ok((r, z)) |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
let (r, z) = read_u256(z)?; |
| 170 |
let (s, z) = read_u256(z)?; |
| 171 |
|
| 172 |
// We should have consumed the entire buffer |
| 173 |
if !z.is_empty() { |
| 174 |
return Err(CryptoError::MalformedInput); |
| 175 |
} |
| 176 |
|
| 177 |
// Left pad each integer with zeros to length 32 and concatenate the results |
| 178 |
let mut out = vec![0u8; 64]; |
| 179 |
{ |
| 180 |
let (r_out, s_out) = out.split_at_mut(32); |
| 181 |
r_out[32 - r.len()..].copy_from_slice(r); |
| 182 |
s_out[32 - s.len()..].copy_from_slice(s); |
| 183 |
} |
| 184 |
Ok(out) |
| 185 |
} |