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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
// Copyright 2021-2022 Shin Yoshida
//
// "LGPL-3.0-or-later OR Apache-2.0"
//
// This is part of bsn1
//
// bsn1 is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// bsn1 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with bsn1. If not, see <http://www.gnu.org/licenses/>.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Contents, Error};
use num::PrimInt;
use std::borrow::ToOwned;
use std::borrow::{Borrow, BorrowMut};
use std::mem;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
/// `ContentsRef` is a wrapper of [u8] and represents 'ASN.1 contents'.
///
/// User can access to the inner slice via [`Deref`] and [`DerefMut`] implementations.
///
/// This struct is `Unsized`, and user will usually use a reference.
///
/// [`Deref`]: #impl-Deref-for-ContentsRef
/// [`DerefMut`]: #impl-DerefMut-for-ContentsRef
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ContentsRef {
bytes: [u8],
}
impl<'a> From<&'a [u8]> for &'a ContentsRef {
/// This function is same to [`ContentsRef::from_bytes`].
///
/// [`ContentsRef::from_bytes`]: #method.from_bytes
fn from(bytes: &'a [u8]) -> Self {
unsafe { mem::transmute(bytes) }
}
}
impl<'a> From<&'a mut [u8]> for &'a mut ContentsRef {
/// This function is same to [`ContentsRef::from_bytes_mut`].
///
/// [`ContentsRef::from_bytes_mut`]: #method.from_bytes_mut
fn from(bytes: &'a mut [u8]) -> Self {
unsafe { mem::transmute(bytes) }
}
}
impl From<bool> for &'static ContentsRef {
/// This function is same to [`ContentsRef::from_bool`].
///
/// [`ContentsRef::from_bool`]: #method.from_bool
fn from(val: bool) -> Self {
ContentsRef::from_bool(val)
}
}
impl ContentsRef {
/// Creates a reference to `ContentsRef` holding `bytes`.
///
/// This function is same to [`<&ContentsRef>::from`].
///
/// [`<&ContentsRef>::from`]: #impl-From%3C%26%27a%20%5Bu8%5D%3E-for-%26%27a%20ContentsRef
///
/// # Examples
///
/// ```
/// use bsn1::ContentsRef;
///
/// let bytes: &[u8] = &[1, 2, 3];
/// let contents = ContentsRef::from_bytes(bytes);
///
/// assert_eq!(contents as &[u8], bytes);
/// ```
pub fn from_bytes(bytes: &[u8]) -> &Self {
<&ContentsRef>::from(bytes)
}
/// Creates a mutable reference to `ContentsRef` holding `bytes`.
///
/// This function is same to [`<&mut ContentsRef>::from`].
///
/// [`<&mut ContentsRef>::from`]:
/// #impl-From%3C%26%27a%20mut%20%5Bu8%5D%3E-for-%26%27a%20mut%20ContentsRef
///
/// # Examples
///
/// ```
/// use bsn1::ContentsRef;
///
/// let bytes: &mut [u8] = &mut [1, 2, 3];
///
/// {
/// let contents = ContentsRef::from_bytes_mut(bytes);
/// assert_eq!(contents as &[u8], &[1, 2, 3]);
///
/// contents[0] = 10;
/// assert_eq!(contents as &[u8], &[10, 2, 3]);
/// }
///
/// // 'bytes' is updated as well.
/// assert_eq!(bytes, &[10, 2, 3]);
/// ```
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self {
<&mut ContentsRef>::from(bytes)
}
/// Creates a reference to `ContentsRef` representing `val`.
///
/// The rule of 'ASN.1 bool' is slightly different among 'Ber', 'Der', and 'CER', however,
/// this function is valid for all of them.
///
/// # Examples
///
/// ```
/// use bsn1::ContentsRef;
///
/// let true_contents = ContentsRef::from_bool(true);
///
/// assert_eq!(Ok(true), true_contents.to_bool_ber());
/// assert_eq!(Ok(true), true_contents.to_bool_der());
///
/// let false_contents = ContentsRef::from_bool(false);
///
/// assert_eq!(Ok(false), false_contents.to_bool_ber());
/// assert_eq!(Ok(false), false_contents.to_bool_der());
/// ```
pub fn from_bool(val: bool) -> &'static Self {
if val {
Self::from_bytes(&[0xff])
} else {
Self::from_bytes(&[0x00])
}
}
}
impl AsRef<[u8]> for ContentsRef {
fn as_ref(&self) -> &[u8] {
self
}
}
impl AsMut<[u8]> for ContentsRef {
fn as_mut(&mut self) -> &mut [u8] {
self
}
}
impl Borrow<[u8]> for ContentsRef {
fn borrow(&self) -> &[u8] {
self
}
}
impl BorrowMut<[u8]> for ContentsRef {
fn borrow_mut(&mut self) -> &mut [u8] {
self
}
}
impl Deref for ContentsRef {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.bytes
}
}
impl DerefMut for ContentsRef {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.bytes
}
}
impl PartialEq<Contents> for ContentsRef {
fn eq(&self, other: &Contents) -> bool {
self == other
}
}
impl ToOwned for ContentsRef {
type Owned = Contents;
fn to_owned(&self) -> Self::Owned {
Contents::from_bytes(self)
}
}
impl ContentsRef {
/// Parses `self` as the ASN.1 contents of integer.
///
/// Type `T` should be the builtin primitive integer types (e.g., u8, i32, isize, i128...)
///
/// # Examples
///
/// ```
/// use bsn1::{Contents, ContentsRef};
///
/// let contents = Contents::from_integer(17);
/// assert_eq!(Ok(17_i32), contents.to_integer::<i32>());
///
/// // Overflow to convert i32::MAX into i16.
/// let contents = Contents::from_integer(i32::MAX);
/// assert!(contents.to_integer::<i16>().is_err());
///
/// // Cannot convert a negatibe value into unsigned type.
/// let contents = Contents::from_integer(-5);
/// assert!(contents.to_integer::<u32>().is_err());
/// ```
pub fn to_integer<T>(&self) -> Result<T, Error>
where
T: PrimInt,
{
if self.is_empty() {
return Err(Error::UnTerminatedBytes);
}
if 1 < self.len() {
if (self[0] == 0) && (self[1] & 0x80 == 0x00) {
return Err(Error::RedundantBytes);
}
if (self[0] == 0xff) && (self[1] & 0x80 == 0x80) {
return Err(Error::RedundantBytes);
}
}
// If 'T' is Unsigned type and the first octet is 0x00,
// We can ignore the first byte 0x00.
let bytes = if self[0] == 0x00 { &self[1..] } else { self };
if mem::size_of::<T>() < bytes.len() {
return Err(Error::OverFlow);
}
let v = unsafe { self.to_integer_unchecked() };
if self[0] & 0x80 == 0x00 {
if v < T::zero() {
return Err(Error::OverFlow);
}
} else {
if T::zero() <= v {
return Err(Error::OverFlow);
}
}
Ok(v)
}
/// Parses `self` as a contents of ASN.1 integer without any check.
///
/// Type `T` should be the builtin primitive integer type (e.g., u8, i32, isize, u128, ...)
///
/// # Safety
///
/// The behavior is undefined in the following cases.
///
/// - `self` is not formatted as the contents of ASN.1 integer.
/// - The value is greater than the max value of `T`, or less than the min value of `T`.
pub unsafe fn to_integer_unchecked<T>(&self) -> T
where
T: PrimInt,
{
// If 'T' is Unsigned type and the first octet is 0x00,
// We can ignore the first byte 0x00.
let bytes = if self[0] == 0x00 { &self[1..] } else { self };
let filler = if self[0] & 0x80 == 0x00 { 0x00 } else { 0xff };
let mut be: MaybeUninit<T> = MaybeUninit::uninit();
be.as_mut_ptr().write_bytes(filler, 1);
let dst = be.as_mut_ptr() as *mut u8;
let dst = dst.add(mem::size_of::<T>() - bytes.len());
dst.copy_from_nonoverlapping(bytes.as_ptr(), bytes.len());
T::from_be(be.assume_init())
}
/// Parses `self` as a contents of BER bool.
///
/// # Warnings
///
/// The rule of BER bool is different from that of DER and CER.
///
/// # Examples
///
/// ```
/// use bsn1::ContentsRef;
///
/// let true_contents = ContentsRef::from_bool(true);
/// assert_eq!(Ok(true), true_contents.to_bool_ber());
///
/// let false_contents = ContentsRef::from_bool(false);
/// assert_eq!(Ok(false), false_contents.to_bool_ber());
///
/// // 'BER' regards any octet except for 0x00 as 'True',
/// // while 'DER' regards octets except for 0x00 and 0xff as error.
/// let bytes = &[0x03];
/// let ber_contents = ContentsRef::from_bytes(bytes);
/// assert!(ber_contents.to_bool_ber().is_ok());
/// assert!(ber_contents.to_bool_der().is_err());
/// ```
pub fn to_bool_ber(&self) -> Result<bool, Error> {
if self.is_empty() {
Err(Error::UnTerminatedBytes)
} else if 1 < self.len() {
Err(Error::InvalidContents)
} else if self[0] == 0x00 {
Ok(false)
} else {
Ok(true)
}
}
/// Parses `self` as a contents of DER bool.
///
/// # Warnings
///
/// The rule of BER bool is different from that of DER and CER.
///
/// # Examples
///
/// ```
/// use bsn1::ContentsRef;
///
/// let true_contents = ContentsRef::from_bool(true);
/// assert_eq!(Ok(true), true_contents.to_bool_der());
///
/// let false_contents = ContentsRef::from_bool(false);
/// assert_eq!(Ok(false), false_contents.to_bool_der());
///
/// // 'BER' regards any octet except for 0x00 as 'True',
/// // while 'DER' regards octets except for 0x00 and 0xff as error.
/// let bytes = &[0x03];
/// let ber_contents = ContentsRef::from_bytes(bytes);
/// assert!(ber_contents.to_bool_ber().is_ok());
/// assert!(ber_contents.to_bool_der().is_err());
/// ```
pub fn to_bool_der(&self) -> Result<bool, Error> {
if self.is_empty() {
Err(Error::UnTerminatedBytes)
} else if 1 < self.len() {
Err(Error::InvalidContents)
} else {
match self[0] {
0x00 => Ok(false),
0xff => Ok(true),
_ => Err(Error::InvalidContents),
}
}
}
}