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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
// Copyright 2021 Shin Yoshida // // "LGPL-3.0-or-later OR Apache-2.0 OR BSD-2-Clause" // // 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. // // // Redistribution and use in source and binary forms, with or without modification, are permitted // provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this list of // conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, this // list of conditions and the following disclaimer in the documentation and/or other // materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. use crate::{identifier, length, Buffer, Der, DerRef, Error, IdRef, Length}; use core::convert::TryFrom; use core::ops::Deref; use std::borrow::Borrow; /// `BerRef` is a wrapper of `[u8]` and represents a BER. /// /// This struct is 'Unsized', and user usually uses a reference to the instance. #[derive(Debug, PartialEq, Eq)] pub struct BerRef { bytes: [u8], } impl<'a> From<&'a DerRef> for &'a BerRef { #[inline] fn from(der: &'a DerRef) -> Self { unsafe { BerRef::from_bytes_unchecked(der.as_ref()) } } } impl<'a> TryFrom<&'a [u8]> for &'a BerRef { type Error = Error; /// Parses `bytes` starting with octets of 'ASN.1 BER' and returns a reference to `BerRef` . /// /// This function ignores extra octet(s) at the end of `bytes` if any. fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> { let id = <&IdRef>::try_from(bytes)?; let parsing = &bytes[id.as_ref().len()..]; match length::try_from(parsing) { Err(e) => Err(e), Ok((Length::Definite(len), parsing)) => { let total_len = bytes.len() - parsing.len() + len; if bytes.len() < total_len { Err(Error::UnTerminatedBytes) } else { unsafe { Ok(BerRef::from_bytes_unchecked(&bytes[..total_len])) } } } Ok((Length::Indefinite, mut parsing)) => { while { let element = Self::try_from(parsing)?; let len = element.as_ref().len(); parsing = &parsing[len..]; if element.id() != IdRef::eoc() { true } else if element.contents().is_empty() { false } else { return Err(Error::BadEoc); } } {} let total_len = bytes.len() - parsing.len(); unsafe { Ok(BerRef::from_bytes_unchecked(&bytes[..total_len])) } } } } } impl BerRef { /// Provides a reference from `bytes` without any sanitization. /// /// `bytes` must be BER octets and must not include any extra octet. /// /// If it is sure that `bytes` starts with BER octets, but if some extra octet(s) may added /// after that, use [`from_bytes_starts_with_unchecked`] instead. /// If it is not sure whether `bytes` starts with BER octets or not, use [`TryFrom`] /// implementation. /// /// # Safety /// /// The behavior is undefined if `bytes` is not formatted as a BER. /// /// [`from_bytes_starts_with_unchecked`]: #method.from_bytes_starts_with_unchecked /// [`TryFrom`]: #impl-TryFrom%3C%26%27a%20%5Bu8%5D%3E /// /// # Examples /// /// ``` /// use bsn1::{Ber, BerRef, IdRef}; /// /// let id = IdRef::octet_string(); /// let ber = Ber::new(id, &[]); /// /// let bytes: &[u8] = ber.as_ref(); /// let deserialized = unsafe { BerRef::from_bytes_unchecked(bytes) }; /// assert_eq!(ber.as_ref() as &BerRef, deserialized); /// ``` #[inline] pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { let ptr = bytes as *const [u8]; let ptr = ptr as *const Self; &*ptr } /// Provides a reference from `bytes` that starts with a BER octets. /// /// `bytes` may include some extra octet(s) at the end. /// /// If it is not sure whether `bytes` starts with BER octets or not, use [`TryFrom`] /// implementation. /// /// # Safety /// /// The behavior is undefined if `bytes` does not start with BER octets. /// /// [`TryFrom`]: #impl-TryFrom%3C%26%27a%20%5Bu8%5D%3E /// /// # Examples /// /// ``` /// use bsn1::{Ber, BerRef, IdRef}; /// /// let id = IdRef::octet_string(); /// let ber = Ber::new(id, &[]); /// let mut bytes = Vec::from(ber.as_ref() as &[u8]); /// bytes.extend(&[1, 2, 3]); /// /// let deserialized = unsafe { BerRef::from_bytes_starts_with_unchecked(bytes.as_ref()) }; /// assert_eq!(ber.as_ref() as &BerRef, deserialized); /// ``` #[inline] pub unsafe fn from_bytes_starts_with_unchecked(bytes: &[u8]) -> &Self { let id = identifier::shrink_to_fit_unchecked(bytes); let parsing = &bytes[id.len()..]; let total_len = match length::try_from(parsing).unwrap() { (Length::Definite(len), parsing) => bytes.len() - parsing.len() + len, (Length::Indefinite, parsing) => { let mut total_len = bytes.len() - parsing.len(); let mut parsing = parsing; while { let element = Self::from_bytes_starts_with_unchecked(parsing); total_len += element.as_ref().len(); parsing = &parsing[element.as_ref().len()..]; element.id() != IdRef::eoc() } {} total_len } }; let bytes = &bytes[..total_len]; Self::from_bytes_unchecked(bytes) } } impl AsRef<[u8]> for BerRef { #[inline] fn as_ref(&self) -> &[u8] { &self.bytes } } impl Borrow<[u8]> for BerRef { #[inline] fn borrow(&self) -> &[u8] { &self.bytes } } impl ToOwned for BerRef { type Owned = Ber; fn to_owned(&self) -> Self::Owned { let buffer = Buffer::from(self.as_ref()); Ber { buffer } } } impl BerRef { /// Provides a reference to `IdRef` of `self` . /// /// # Examples /// /// ``` /// use bsn1::{Ber, BerRef, IdRef}; /// /// let id = IdRef::octet_string(); /// let contents = &[1, 2, 3]; /// /// // 'Ber' implements 'Deref<Target=BerRef>.' /// let ber = Ber::new(id, contents); /// assert_eq!(id, ber.id()); /// ``` #[inline] pub fn id(&self) -> &IdRef { unsafe { let bytes = identifier::shrink_to_fit_unchecked(&self.bytes); IdRef::from_bytes_unchecked(bytes) } } /// Returns `Length` of `self` . /// /// # Warnings /// /// `Length` stands for 'the length octets of the contents' in BER. /// The total bytes is greater than the value. /// /// # Examples /// /// ``` /// use bsn1::{Ber, BerRef, IdRef, Length}; /// /// let id = IdRef::octet_string(); /// let contents = &[1, 2, 3]; /// /// // 'Ber' implements 'Deref<Target=BerRef>.' /// let ber = Ber::new(id, contents); /// assert_eq!(Length::Definite(contents.len()), ber.length()); /// ``` #[inline] pub fn length(&self) -> Length { let id_len = self.id().as_ref().len(); let bytes = &self.bytes[id_len..]; length::try_from(bytes).unwrap().0 } /// Provides a reference to the 'contents' octets of `self` . /// /// # Examples /// /// ``` /// use bsn1::{Ber, BerRef, IdRef}; /// /// let id = IdRef::octet_string(); /// let contents = &[1, 2, 3]; /// /// // 'Ber' implements 'Deref<Target=BerRef>.' /// let ber = Ber::new(id, contents); /// assert_eq!(contents, ber.contents()); /// ``` #[inline] pub fn contents(&self) -> &[u8] { let id_len = self.id().as_ref().len(); let bytes = &self.bytes[id_len..]; length::try_from(bytes).unwrap().1 } } /// `Ber` owns [`BerRef`] and represents a BER. /// /// [`BerRef`]: struct.BerRef.html #[derive(Debug, Clone, PartialEq, Eq)] pub struct Ber { buffer: Buffer, } impl From<&DerRef> for Ber { #[inline] fn from(der: &DerRef) -> Self { <&BerRef>::from(der).to_owned() } } impl From<Der> for Ber { #[inline] fn from(der: Der) -> Self { Self { buffer: crate::der::disassemble_der(der), } } } impl From<&BerRef> for Ber { #[inline] fn from(ber_ref: &BerRef) -> Self { ber_ref.to_owned() } } impl TryFrom<&[u8]> for Ber { type Error = Error; /// Parses `bytes` starting with octets of 'ASN.1 BER' and returns a new instance. /// /// This function ignores extra octet(s) at the end of `bytes` if any. fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { let ber_ref = <&BerRef>::try_from(bytes)?; Ok(ber_ref.to_owned()) } } impl Ber { /// Creates a new instance from `id` and `contents` with definite length. /// /// Note that BER allows both definite and indefinite length, however, the length of the return /// value is always definite. /// (Generally speaking, the performance of definite length is better than that of indefinite /// length. Indefinite length is seldom used these days.) /// /// # Examples /// /// ``` /// use bsn1::{Ber, IdRef}; /// /// let id = IdRef::octet_string(); /// let _ber = Ber::new(id, &[]); /// ``` pub fn new(id: &IdRef, contents: &[u8]) -> Self { let der = Der::new(id, contents); Self::from(der) } /// Creates a new instance from `id` and `contents` . /// /// # Examples /// /// ``` /// use bsn1::{Ber, IdRef}; /// /// let id = IdRef::sequence(); /// /// // Build instance using function 'from_id_iterator()'. /// let contents: &[Ber] = &[Ber::utf8_string("foo"), Ber::integer(29)]; /// let ber = Ber::from_id_iterator(id, contents.iter()); /// /// // Build instance using function 'new()'. /// let contents: Vec<u8> = contents.iter() /// .map(|i| Vec::from(i.as_ref() as &[u8])) /// .flatten().collect(); /// let expected = Ber::new(id, &contents); /// /// assert_eq!(expected, ber); /// ``` pub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Self where I: Iterator + Clone, I::Item: AsRef<[u8]>, { let der = Der::from_id_iterator(id, contents); Self::from(der) } /// Returns a new instance representing boolean. /// /// # Examples /// /// ``` /// use bsn1::{contents, Ber, IdRef}; /// /// let val = true; /// let ber = Ber::boolean(val); /// /// assert_eq!(IdRef::boolean(), ber.id()); /// assert_eq!(val, contents::to_bool_ber(ber.contents()).unwrap()); /// ``` pub fn boolean(val: bool) -> Self { Self::from(Der::boolean(val)) } /// Returns a new instance representing ingeger. /// /// # Examples /// /// ``` /// use bsn1::{contents, Ber, IdRef}; /// /// let val = 39; /// let ber = Ber::integer(val); /// /// assert_eq!(IdRef::integer(), ber.id()); /// assert_eq!(val, contents::to_integer(ber.contents()).unwrap()); /// ``` pub fn integer(val: i128) -> Self { Self::from(Der::integer(val)) } /// Returns a new instance representing utf8_string. /// /// # Examples /// /// ``` /// use bsn1::{Ber, IdRef}; /// /// let val = &"foo"; /// let ber = Ber::utf8_string(val); /// /// assert_eq!(IdRef::utf8_string(), ber.id()); /// assert_eq!(val.as_bytes(), ber.contents()); /// ``` pub fn utf8_string(val: &str) -> Self { Self::from(Der::utf8_string(val)) } /// Returns a new instance representing octet_string. /// /// # Examples /// /// ``` /// use bsn1::{Ber, IdRef}; /// /// let val = &[1, 2, 3]; /// let ber = Ber::octet_string(val); /// /// assert_eq!(IdRef::octet_string(), ber.id()); /// assert_eq!(val, ber.contents()); /// ``` pub fn octet_string(val: &[u8]) -> Self { Self::from(Der::octet_string(val)) } } impl AsRef<[u8]> for Ber { #[inline] fn as_ref(&self) -> &[u8] { self.buffer.as_ref() } } impl AsRef<BerRef> for Ber { #[inline] fn as_ref(&self) -> &BerRef { self.deref() } } impl Borrow<[u8]> for Ber { #[inline] fn borrow(&self) -> &[u8] { self.buffer.borrow() } } impl Borrow<BerRef> for Ber { #[inline] fn borrow(&self) -> &BerRef { self.deref() } } impl Deref for Ber { type Target = BerRef; #[inline] fn deref(&self) -> &Self::Target { unsafe { BerRef::from_bytes_unchecked(self.buffer.as_ref()) } } } impl Ber { /// Consumes `self` , returning `Vec` . /// /// # Examples /// /// ``` /// use bsn1::{Ber, IdRef}; /// /// let id = IdRef::octet_string(); /// let contents: &[u8] = &[0, 1, 2, 3, 4]; /// /// let ber = Ber::new(id, contents); /// let v = ber.clone().into_vec(); /// /// assert_eq!(ber.as_ref() as &[u8], v.as_ref() as &[u8]); /// ``` pub fn into_vec(self) -> Vec<u8> { self.buffer.into_vec() } } /// Builds a `Ber` instance representing Constructed BER effectively. /// /// # Formula /// /// `constructed_ber!(id: &IdRef [, (id_1, contents_1) [, (id_2, contents_2) [...]]]) => Ber` /// /// `id_n` and `contents_n` must be bounded on `AsRef<[u8]>` . /// /// # Examples /// /// Empty contents. /// /// ``` /// # #[macro_use] extern crate bsn1; /// use bsn1::{Ber, IdRef}; /// /// let id = IdRef::sequence(); /// let expected = Ber::new(id, &[]); /// let ber = constructed_ber!(id); /// /// assert_eq!(expected, ber); /// ``` /// /// Sequence of 2 BERs. /// /// ``` /// # #[macro_use] extern crate bsn1; /// use bsn1::{contents, Ber, BerRef, IdRef}; /// use std::convert::TryFrom; /// /// let id = IdRef::sequence(); /// let id1 = IdRef::octet_string(); /// let contents1: [u8; 3] = [1, 2, 3]; /// let id2 = IdRef::integer(); /// let contents2 = contents::from_integer(10); /// /// let ber = constructed_ber!(id, (id1.to_owned(), contents1), (id2, &contents2)); /// /// assert_eq!(id, ber.id()); /// /// let bytes = ber.contents(); /// let ber1 = <&BerRef>::try_from(bytes).unwrap(); /// assert_eq!(id1, ber1.id()); /// assert_eq!(contents1, ber1.contents()); /// /// let bytes = &bytes[ber1.as_ref().len()..]; /// let ber2 = <&BerRef>::try_from(bytes).unwrap(); /// assert_eq!(id2, ber2.id()); /// assert_eq!(contents2.as_ref(), ber2.contents()); /// ``` #[macro_export] macro_rules! constructed_ber { ($id:expr $(, ($id_n:expr, $contents_n:expr))*) => { Ber::from(constructed_der!($id $(, ($id_n, $contents_n))*)) }; ($id:expr $(, ($id_n:expr, $contents_n:expr))*,) => { Ber::from(constructed_der!($id $(, ($id_n, $contents_n))*,)) }; } #[cfg(test)] mod tests { use super::*; #[test] fn from_bytes_starts_with_unchecked_definite() { let id = IdRef::octet_string(); let byteses: &[&[u8]] = &[&[], &[0x00], &[0xff], &[0x00, 0x00], &[0xff, 0xff]]; let extras: &[&[u8]] = &[&[], &[0x00], &[0xff], &[0x00, 0x00], &[0xff, 0xff]]; for &bytes in byteses { let ber = Ber::new(id, bytes); for &extra in extras { let mut bytes = Vec::from(ber.as_ref() as &[u8]); bytes.extend(extra); let ber_ref = unsafe { BerRef::from_bytes_starts_with_unchecked(bytes.as_ref()) }; assert_eq!(ber_ref, ber.as_ref()); } } } #[test] fn from_bytes_starts_with_unchecked_infinite() { let eoc = { let id = IdRef::eoc(); let contents: &[u8] = &[]; Ber::new(id, contents) }; let bers: Vec<Ber> = (0..10) .map(|i| { let id = IdRef::octet_string(); let contents: &[u8] = &[i]; Ber::new(id, contents) }) .collect(); for i in 0..10 { let id = IdRef::sequence(); let mut bytes: Vec<u8> = Vec::from(id.as_ref() as &[u8]); bytes.extend(Length::Indefinite.to_bytes().as_ref()); for ber in bers[0..i].iter() { bytes.extend(ber.as_ref() as &[u8]); } bytes.extend(eoc.as_ref() as &[u8]); let ber = unsafe { BerRef::from_bytes_starts_with_unchecked(bytes.as_ref()) }; assert_eq!(bytes.as_ref() as &[u8], ber.as_ref() as &[u8]); } } #[test] fn try_from_deinite() { let id = IdRef::octet_string(); let byteses: &[&[u8]] = &[&[], &[0x00], &[0xff], &[0x00, 0x00], &[0xff, 0xff]]; for &bytes in byteses { let ber = Ber::new(id, bytes); let ber_ref = <&BerRef>::try_from(ber.as_ref() as &[u8]).unwrap(); assert_eq!(ber_ref, ber.as_ref() as &BerRef); } } #[test] fn try_from_indefinite() { let eoc = { let id = IdRef::eoc(); let contents: &[u8] = &[]; Ber::new(id, contents) }; let bers: Vec<Ber> = (0..10) .map(|i| { let id = IdRef::octet_string(); let contents: &[u8] = &[i]; Ber::new(id, contents) }) .collect(); for i in 0..10 { let id = IdRef::sequence(); let mut bytes: Vec<u8> = Vec::from(id.as_ref() as &[u8]); bytes.extend(Length::Indefinite.to_bytes().as_ref()); for ber in bers[0..i].iter() { bytes.extend(ber.as_ref() as &[u8]); } bytes.extend(eoc.as_ref() as &[u8]); let ber = <&BerRef>::try_from(bytes.as_ref() as &[u8]).unwrap(); assert_eq!(bytes.as_ref() as &[u8], ber.as_ref() as &[u8]); } } }