Skip to content
Snippets Groups Projects
data_formats.rs 39 KiB
Newer Older
Jean-Michel Picod's avatar
Jean-Michel Picod committed
// Copyright 2019 Google LLC
//
// 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 super::status_code::Ctap2StatusCode;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::convert::TryFrom;
use crypto::{ecdh, ecdsa};

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct PublicKeyCredentialRpEntity {
    pub rp_id: String,
    pub rp_name: Option<String>,
    pub rp_icon: Option<String>,
}

impl TryFrom<&cbor::Value> for PublicKeyCredentialRpEntity {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let rp_map = read_map(cbor_value)?;
        let rp_id = read_text_string(ok_or_missing(rp_map.get(&cbor_text!("id")))?)?;
        let rp_name = rp_map
            .get(&cbor_text!("name"))
            .map(read_text_string)
            .transpose()?;
        let rp_icon = rp_map
            .get(&cbor_text!("icon"))
            .map(read_text_string)
            .transpose()?;
        Ok(Self {
            rp_id,
            rp_name,
            rp_icon,
        })
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct PublicKeyCredentialUserEntity {
    pub user_id: Vec<u8>,
    pub user_name: Option<String>,
    pub user_display_name: Option<String>,
    pub user_icon: Option<String>,
}

impl TryFrom<&cbor::Value> for PublicKeyCredentialUserEntity {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let user_map = read_map(cbor_value)?;
        let user_id = read_byte_string(ok_or_missing(user_map.get(&cbor_text!("id")))?)?;
        let user_name = user_map
            .get(&cbor_text!("name"))
            .map(read_text_string)
            .transpose()?;
        let user_display_name = user_map
            .get(&cbor_text!("displayName"))
            .map(read_text_string)
            .transpose()?;
        let user_icon = user_map
            .get(&cbor_text!("icon"))
            .map(read_text_string)
            .transpose()?;
        Ok(Self {
            user_id,
            user_name,
            user_display_name,
            user_icon,
        })
    }
}

impl From<PublicKeyCredentialUserEntity> for cbor::Value {
    fn from(entity: PublicKeyCredentialUserEntity) -> Self {
        cbor_map_options! {
            "id" => entity.user_id,
            "name" => entity.user_name,
            "displayName" => entity.user_display_name,
            "icon" => entity.user_icon,
        }
    }
}

#[derive(Clone, PartialEq)]
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))]
pub enum PublicKeyCredentialType {
    PublicKey,
}

impl From<PublicKeyCredentialType> for cbor::Value {
    fn from(cred_type: PublicKeyCredentialType) -> Self {
        match cred_type {
            PublicKeyCredentialType::PublicKey => "public-key",
        }
        .into()
    }
}

impl TryFrom<&cbor::Value> for PublicKeyCredentialType {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let cred_type_string = read_text_string(cbor_value)?;
        match &cred_type_string[..] {
            "public-key" => Ok(PublicKeyCredentialType::PublicKey),
            _ => Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM),
        }
    }
}

#[derive(PartialEq)]
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))]
pub struct PublicKeyCredentialParameter {
    pub cred_type: PublicKeyCredentialType,
    pub alg: SignatureAlgorithm,
}

impl TryFrom<&cbor::Value> for PublicKeyCredentialParameter {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let cred_param_map = read_map(cbor_value)?;
        let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(
            cred_param_map.get(&cbor_text!("type")),
        )?)?;
        let alg =
            SignatureAlgorithm::try_from(ok_or_missing(cred_param_map.get(&cbor_text!("alg")))?)?;
        Ok(Self { cred_type, alg })
    }
}

impl From<PublicKeyCredentialParameter> for cbor::Value {
    fn from(cred_param: PublicKeyCredentialParameter) -> Self {
        cbor_map_options! {
            "type" => cred_param.cred_type,
            "alg" => cred_param.alg as i64,
        }
    }
}

Jean-Michel Picod's avatar
Jean-Michel Picod committed
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub enum AuthenticatorTransport {
    Usb,
    Nfc,
    Ble,
    Internal,
}

impl From<AuthenticatorTransport> for cbor::Value {
    fn from(transport: AuthenticatorTransport) -> Self {
        match transport {
            AuthenticatorTransport::Usb => "usb",
            AuthenticatorTransport::Nfc => "nfc",
            AuthenticatorTransport::Ble => "ble",
            AuthenticatorTransport::Internal => "internal",
        }
        .into()
    }
}

impl TryFrom<&cbor::Value> for AuthenticatorTransport {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let transport_string = read_text_string(cbor_value)?;
        match &transport_string[..] {
            "usb" => Ok(AuthenticatorTransport::Usb),
            "nfc" => Ok(AuthenticatorTransport::Nfc),
            "ble" => Ok(AuthenticatorTransport::Ble),
            "internal" => Ok(AuthenticatorTransport::Internal),
            _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
        }
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct PublicKeyCredentialDescriptor {
    pub key_type: PublicKeyCredentialType,
    pub key_id: Vec<u8>,
    pub transports: Option<Vec<AuthenticatorTransport>>,
}

impl TryFrom<&cbor::Value> for PublicKeyCredentialDescriptor {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let cred_desc_map = read_map(cbor_value)?;
        let key_type = PublicKeyCredentialType::try_from(ok_or_missing(
            cred_desc_map.get(&cbor_text!("type")),
        )?)?;
        let key_id = read_byte_string(ok_or_missing(cred_desc_map.get(&cbor_text!("id")))?)?;
        let transports = match cred_desc_map.get(&cbor_text!("transports")) {
            Some(exclude_entry) => {
                let transport_vec = read_array(exclude_entry)?;
                let mut transports = vec![];
                for transport_value in transport_vec {
                    transports.push(AuthenticatorTransport::try_from(transport_value)?);
                }
                Some(transports)
            }
            None => None,
        };
        Ok(Self {
            key_type,
            key_id,
            transports,
        })
    }
}

impl From<PublicKeyCredentialDescriptor> for cbor::Value {
    fn from(desc: PublicKeyCredentialDescriptor) -> Self {
        cbor_map_options! {
            "type" => desc.key_type,
            "id" => desc.key_id,
            "transports" => desc.transports.map(|vec| cbor_array_vec!(vec)),
        }
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct Extensions(BTreeMap<String, cbor::Value>);

impl TryFrom<&cbor::Value> for Extensions {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let mut extensions = BTreeMap::new();
        for (extension_key, extension_value) in read_map(cbor_value)? {
            if let cbor::KeyType::TextString(extension_key_string) = extension_key {
                extensions.insert(extension_key_string.to_string(), extension_value.clone());
            } else {
                return Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE);
            }
        }
        Ok(Extensions(extensions))
    }
}

impl From<Extensions> for cbor::Value {
    fn from(extensions: Extensions) -> Self {
        cbor_map_btree!(extensions
            .0
            .into_iter()
            .map(|(key, value)| (cbor_text!(key), value))
            .collect())
    }
}

impl Extensions {
    #[cfg(test)]
    pub fn new(extension_map: BTreeMap<String, cbor::Value>) -> Self {
        Extensions(extension_map)
    }

    pub fn has_make_credential_hmac_secret(&self) -> Result<bool, Ctap2StatusCode> {
        self.0
            .get("hmac-secret")
            .map(read_bool)
            .unwrap_or(Ok(false))
    }

    pub fn get_assertion_hmac_secret(
        &self,
    ) -> Option<Result<GetAssertionHmacSecretInput, Ctap2StatusCode>> {
        self.0
            .get("hmac-secret")
            .map(GetAssertionHmacSecretInput::try_from)
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct GetAssertionHmacSecretInput {
    pub key_agreement: CoseKey,
    pub salt_enc: Vec<u8>,
    pub salt_auth: Vec<u8>,
}

impl TryFrom<&cbor::Value> for GetAssertionHmacSecretInput {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let input_map = read_map(cbor_value)?;
        let cose_key = read_map(ok_or_missing(input_map.get(&cbor_unsigned!(1)))?)?;
        let salt_enc = read_byte_string(ok_or_missing(input_map.get(&cbor_unsigned!(2)))?)?;
        let salt_auth = read_byte_string(ok_or_missing(input_map.get(&cbor_unsigned!(3)))?)?;
        Ok(Self {
            key_agreement: CoseKey(cose_key.clone()),
            salt_enc,
            salt_auth,
        })
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct GetAssertionHmacSecretOutput(Vec<u8>);

impl From<GetAssertionHmacSecretOutput> for cbor::Value {
    fn from(message: GetAssertionHmacSecretOutput) -> cbor::Value {
        cbor_bytes!(message.0)
    }
}

impl TryFrom<&cbor::Value> for GetAssertionHmacSecretOutput {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        Ok(GetAssertionHmacSecretOutput(read_byte_string(cbor_value)?))
    }
}

Jean-Michel Picod's avatar
Jean-Michel Picod committed
// Even though options are optional, we can use the default if not present.
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct MakeCredentialOptions {
    pub rk: bool,
    pub uv: bool,
}

impl TryFrom<&cbor::Value> for MakeCredentialOptions {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let options_map = read_map(cbor_value)?;
        let rk = match options_map.get(&cbor_text!("rk")) {
            Some(options_entry) => read_bool(options_entry)?,
            None => false,
        };
        if let Some(options_entry) = options_map.get(&cbor_text!("up")) {
            if !read_bool(options_entry)? {
                return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION);
            }
        }
        let uv = match options_map.get(&cbor_text!("uv")) {
            Some(options_entry) => read_bool(options_entry)?,
            None => false,
        };
        Ok(Self { rk, uv })
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct GetAssertionOptions {
    pub up: bool,
    pub uv: bool,
}

impl TryFrom<&cbor::Value> for GetAssertionOptions {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let options_map = read_map(cbor_value)?;
        if let Some(options_entry) = options_map.get(&cbor_text!("rk")) {
            // This is only for returning the correct status code.
            read_bool(options_entry)?;
            return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION);
        }
        let up = match options_map.get(&cbor_text!("up")) {
            Some(options_entry) => read_bool(options_entry)?,
            None => true,
        };
        let uv = match options_map.get(&cbor_text!("uv")) {
            Some(options_entry) => read_bool(options_entry)?,
            None => false,
        };
        Ok(Self { up, uv })
    }
}

#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))]
pub struct PackedAttestationStatement {
    pub alg: i64,
    pub sig: Vec<u8>,
    pub x5c: Option<Vec<Vec<u8>>>,
    pub ecdaa_key_id: Option<Vec<u8>>,
}

impl From<PackedAttestationStatement> for cbor::Value {
    fn from(att_stmt: PackedAttestationStatement) -> Self {
        cbor_map_options! {
            "alg" => att_stmt.alg,
            "sig" => att_stmt.sig,
            "x5c" => att_stmt.x5c.map(|x| cbor_array_vec!(x)),
            "ecdaaKeyId" => att_stmt.ecdaa_key_id,
        }
    }
}

#[derive(PartialEq)]
Jean-Michel Picod's avatar
Jean-Michel Picod committed
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))]
pub enum SignatureAlgorithm {
    ES256 = ecdsa::PubKey::ES256_ALGORITHM as isize,
}

impl TryFrom<&cbor::Value> for SignatureAlgorithm {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        match read_integer(cbor_value)? {
            ecdsa::PubKey::ES256_ALGORITHM => Ok(SignatureAlgorithm::ES256),
            _ => Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM),
        }
    }
}

Jean-Michel Picod's avatar
Jean-Michel Picod committed
#[derive(Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))]
pub struct PublicKeyCredentialSource {
    // TODO function to convert to / from Vec<u8>
    pub key_type: PublicKeyCredentialType,
    pub credential_id: Vec<u8>,
    pub private_key: ecdsa::SecKey, // TODO(kaczmarczyck) open for other algorithms
    pub rp_id: String,
    pub user_handle: Vec<u8>, // not optional, but nullable
    pub other_ui: Option<String>,
    pub cred_random: Option<Vec<u8>>,
Jean-Michel Picod's avatar
Jean-Michel Picod committed
}

impl From<PublicKeyCredentialSource> for cbor::Value {
    fn from(credential: PublicKeyCredentialSource) -> cbor::Value {
        let mut private_key = [0u8; 32];
        credential.private_key.to_bytes(&mut private_key);
        let other_ui = match credential.other_ui {
            None => cbor_null!(),
            Some(other_ui) => cbor_text!(other_ui),
        };
        let cred_random = match credential.cred_random {
            None => cbor_null!(),
            Some(cred_random) => cbor_bytes!(cred_random),
        };
Jean-Michel Picod's avatar
Jean-Michel Picod committed
        cbor_array! {
            credential.credential_id,
            private_key,
            credential.rp_id,
            credential.user_handle,
            other_ui,
            cred_random,
Jean-Michel Picod's avatar
Jean-Michel Picod committed
        }
    }
}

impl TryFrom<cbor::Value> for PublicKeyCredentialSource {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: cbor::Value) -> Result<PublicKeyCredentialSource, Ctap2StatusCode> {
        use cbor::{SimpleValue, Value};

        let fields = read_array(&cbor_value)?;
        if fields.len() != 6 {
Jean-Michel Picod's avatar
Jean-Michel Picod committed
            return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR);
        }
        let credential_id = read_byte_string(&fields[0])?;
        let private_key = read_byte_string(&fields[1])?;
        if private_key.len() != 32 {
            return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR);
        }
        let private_key = ecdsa::SecKey::from_bytes(array_ref!(private_key, 0, 32))
            .ok_or(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR)?;
        let rp_id = read_text_string(&fields[2])?;
        let user_handle = read_byte_string(&fields[3])?;
        let other_ui = match &fields[4] {
            Value::Simple(SimpleValue::NullValue) => None,
            cbor_value => Some(read_text_string(cbor_value)?),
        };
        let cred_random = match &fields[5] {
            Value::Simple(SimpleValue::NullValue) => None,
            cbor_value => Some(read_byte_string(cbor_value)?),
        };
Jean-Michel Picod's avatar
Jean-Michel Picod committed
        Ok(PublicKeyCredentialSource {
            key_type: PublicKeyCredentialType::PublicKey,
            credential_id,
            private_key,
            rp_id,
            user_handle,
            other_ui,
            cred_random,
Jean-Michel Picod's avatar
Jean-Michel Picod committed
        })
    }
}

// TODO(kaczmarczyck) we could decide to split this data type up
// It depends on the algorithm though, I think.
// So before creating a mess, this is my workaround.
#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub struct CoseKey(pub BTreeMap<cbor::KeyType, cbor::Value>);

// This is the algorithm specifier that is supposed to be used in a COSE key
// map. The CTAP specification says -25 which represents ECDH-ES + HKDF-256
// here: https://www.iana.org/assignments/cose/cose.xhtml#algorithms
// In fact, this is just used for compatibility with older specification versions.
const ECDH_ALGORITHM: i64 = -25;
// This is the identifier used by OpenSSH. To be compatible, we accept both.
const ES256_ALGORITHM: i64 = -7;
Jean-Michel Picod's avatar
Jean-Michel Picod committed
const EC2_KEY_TYPE: i64 = 2;
const P_256_CURVE: i64 = 1;

impl From<ecdh::PubKey> for CoseKey {
    fn from(pk: ecdh::PubKey) -> Self {
        let mut x_bytes = [0; ecdh::NBYTES];
        let mut y_bytes = [0; ecdh::NBYTES];
        pk.to_coordinates(&mut x_bytes, &mut y_bytes);
        let x_byte_cbor: cbor::Value = cbor_bytes_lit!(&x_bytes);
        let y_byte_cbor: cbor::Value = cbor_bytes_lit!(&y_bytes);
        // TODO(kaczmarczyck) do not write optional parameters, spec is unclear
        let cose_cbor_value = cbor_map_options! {
            1 => EC2_KEY_TYPE,
            3 => ECDH_ALGORITHM,
            -1 => P_256_CURVE,
            -2 => x_byte_cbor,
            -3 => y_byte_cbor,
        };
        if let cbor::Value::Map(cose_map) = cose_cbor_value {
            CoseKey(cose_map)
        } else {
            unreachable!();
        }
    }
}

impl TryFrom<CoseKey> for ecdh::PubKey {
    type Error = Ctap2StatusCode;

    fn try_from(cose_key: CoseKey) -> Result<Self, Ctap2StatusCode> {
        let key_type = read_integer(ok_or_missing(cose_key.0.get(&cbor_int!(1)))?)?;
        if key_type != EC2_KEY_TYPE {
            return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM);
        }
        let algorithm = read_integer(ok_or_missing(cose_key.0.get(&cbor_int!(3)))?)?;
        if algorithm != ECDH_ALGORITHM && algorithm != ES256_ALGORITHM {
Jean-Michel Picod's avatar
Jean-Michel Picod committed
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 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
            return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM);
        }
        let curve = read_integer(ok_or_missing(cose_key.0.get(&cbor_int!(-1)))?)?;
        if curve != P_256_CURVE {
            return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM);
        }
        let x_bytes = read_byte_string(ok_or_missing(cose_key.0.get(&cbor_int!(-2)))?)?;
        if x_bytes.len() != ecdh::NBYTES {
            return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER);
        }
        let y_bytes = read_byte_string(ok_or_missing(cose_key.0.get(&cbor_int!(-3)))?)?;
        if y_bytes.len() != ecdh::NBYTES {
            return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER);
        }
        let x_array_ref = array_ref![x_bytes.as_slice(), 0, ecdh::NBYTES];
        let y_array_ref = array_ref![y_bytes.as_slice(), 0, ecdh::NBYTES];
        ecdh::PubKey::from_coordinates(x_array_ref, y_array_ref)
            .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)
    }
}

#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))]
pub enum ClientPinSubCommand {
    GetPinRetries,
    GetKeyAgreement,
    SetPin,
    ChangePin,
    GetPinUvAuthTokenUsingPin,
    GetPinUvAuthTokenUsingUv,
    GetUvRetries,
}

impl From<ClientPinSubCommand> for cbor::Value {
    fn from(subcommand: ClientPinSubCommand) -> Self {
        match subcommand {
            ClientPinSubCommand::GetPinRetries => 0x01,
            ClientPinSubCommand::GetKeyAgreement => 0x02,
            ClientPinSubCommand::SetPin => 0x03,
            ClientPinSubCommand::ChangePin => 0x04,
            ClientPinSubCommand::GetPinUvAuthTokenUsingPin => 0x05,
            ClientPinSubCommand::GetPinUvAuthTokenUsingUv => 0x06,
            ClientPinSubCommand::GetUvRetries => 0x07,
        }
        .into()
    }
}

impl TryFrom<&cbor::Value> for ClientPinSubCommand {
    type Error = Ctap2StatusCode;

    fn try_from(cbor_value: &cbor::Value) -> Result<Self, Ctap2StatusCode> {
        let subcommand_int = read_unsigned(cbor_value)?;
        match subcommand_int {
            0x01 => Ok(ClientPinSubCommand::GetPinRetries),
            0x02 => Ok(ClientPinSubCommand::GetKeyAgreement),
            0x03 => Ok(ClientPinSubCommand::SetPin),
            0x04 => Ok(ClientPinSubCommand::ChangePin),
            0x05 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingPin),
            0x06 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingUv),
            0x07 => Ok(ClientPinSubCommand::GetUvRetries),
            // TODO(kaczmarczyck) what is the correct status code for this error?
            _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER),
        }
    }
}

pub(super) fn read_unsigned(cbor_value: &cbor::Value) -> Result<u64, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(*unsigned),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn read_integer(cbor_value: &cbor::Value) -> Result<i64, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => {
            if *unsigned <= core::i64::MAX as u64 {
                Ok(*unsigned as i64)
            } else {
                Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
            }
        }
        cbor::Value::KeyValue(cbor::KeyType::Negative(signed)) => Ok(*signed),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub fn read_byte_string(cbor_value: &cbor::Value) -> Result<Vec<u8>, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::KeyValue(cbor::KeyType::ByteString(byte_string)) => Ok(byte_string.to_vec()),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn read_text_string(cbor_value: &cbor::Value) -> Result<String, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::KeyValue(cbor::KeyType::TextString(text_string)) => {
            Ok(text_string.to_string())
        }
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn read_array(cbor_value: &cbor::Value) -> Result<&Vec<cbor::Value>, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::Array(array) => Ok(array),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn read_map(
    cbor_value: &cbor::Value,
) -> Result<&BTreeMap<cbor::KeyType, cbor::Value>, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::Map(map) => Ok(map),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn read_bool(cbor_value: &cbor::Value) -> Result<bool, Ctap2StatusCode> {
    match cbor_value {
        cbor::Value::Simple(cbor::SimpleValue::FalseValue) => Ok(false),
        cbor::Value::Simple(cbor::SimpleValue::TrueValue) => Ok(true),
        _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE),
    }
}

pub(super) fn ok_or_missing(
    value_option: Option<&cbor::Value>,
) -> Result<&cbor::Value, Ctap2StatusCode> {
    value_option.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)
}

#[cfg(test)]
mod test {
    use self::Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE;
    use super::*;
    use alloc::collections::BTreeMap;

    #[test]
    fn test_read_unsigned() {
        assert_eq!(read_unsigned(&cbor_int!(123)), Ok(123));
        assert_eq!(
            read_unsigned(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_unsigned_limits() {
        assert_eq!(
            read_unsigned(&cbor_unsigned!(std::u64::MAX)),
            Ok(std::u64::MAX)
        );
        assert_eq!(
            read_unsigned(&cbor_unsigned!((std::i64::MAX as u64) + 1)),
            Ok((std::i64::MAX as u64) + 1)
        );
        assert_eq!(
            read_unsigned(&cbor_int!(std::i64::MAX)),
            Ok(std::i64::MAX as u64)
        );
        assert_eq!(read_unsigned(&cbor_int!(123)), Ok(123));
        assert_eq!(read_unsigned(&cbor_int!(1)), Ok(1));
        assert_eq!(read_unsigned(&cbor_int!(0)), Ok(0));
        assert_eq!(
            read_unsigned(&cbor_int!(-1)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_int!(-123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_unsigned(&cbor_int!(std::i64::MIN)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_integer() {
        assert_eq!(read_integer(&cbor_int!(123)), Ok(123));
        assert_eq!(read_integer(&cbor_int!(-123)), Ok(-123));
        assert_eq!(
            read_integer(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_integer(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_integer(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_integer(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_integer(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_integer_limits() {
        assert_eq!(
            read_integer(&cbor_unsigned!(std::u64::MAX)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_integer(&cbor_unsigned!((std::i64::MAX as u64) + 1)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_integer(&cbor_int!(std::i64::MAX)), Ok(std::i64::MAX));
        assert_eq!(read_integer(&cbor_int!(123)), Ok(123));
        assert_eq!(read_integer(&cbor_int!(1)), Ok(1));
        assert_eq!(read_integer(&cbor_int!(0)), Ok(0));
        assert_eq!(read_integer(&cbor_int!(-1)), Ok(-1));
        assert_eq!(read_integer(&cbor_int!(-123)), Ok(-123));
        assert_eq!(read_integer(&cbor_int!(std::i64::MIN)), Ok(std::i64::MIN));
    }

    #[test]
    fn test_read_byte_string() {
        assert_eq!(
            read_byte_string(&cbor_int!(123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_byte_string(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_byte_string(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_byte_string(&cbor_bytes_lit!(b"")), Ok(Vec::new()));
        assert_eq!(
            read_byte_string(&cbor_bytes_lit!(b"bar")),
            Ok(b"bar".to_vec())
        );
        assert_eq!(
            read_byte_string(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_byte_string(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_text_string() {
        assert_eq!(
            read_text_string(&cbor_int!(123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_text_string(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_text_string(&cbor_text!("")), Ok(String::new()));
        assert_eq!(
            read_text_string(&cbor_text!("foo")),
            Ok(String::from("foo"))
        );
        assert_eq!(
            read_text_string(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_text_string(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_text_string(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_array() {
        assert_eq!(
            read_array(&cbor_int!(123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_array(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_array(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_array(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_array(&cbor_array![]), Ok(&Vec::new()));
        assert_eq!(
            read_array(&cbor_array![
                123,
                cbor_null!(),
                "foo",
                cbor_array![],
                cbor_map! {},
            ]),
            Ok(&vec![
                cbor_int!(123),
                cbor_null!(),
                cbor_text!("foo"),
                cbor_array![],
                cbor_map! {},
            ])
        );
        assert_eq!(
            read_array(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_read_map() {
        assert_eq!(
            read_map(&cbor_int!(123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_map(&cbor_bool!(true)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_map(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_map(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_map(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_map(&cbor_map! {}), Ok(&BTreeMap::new()));
        assert_eq!(
            read_map(&cbor_map! {
                1 => cbor_false!(),
                "foo" => b"bar",
                b"bin" => -42,
            }),
            Ok(&[
                (cbor_unsigned!(1), cbor_false!()),
                (cbor_text!("foo"), cbor_bytes_lit!(b"bar")),
                (cbor_bytes_lit!(b"bin"), cbor_int!(-42)),
            ]
            .iter()
            .cloned()
            .collect::<BTreeMap<_, _>>())
        );
    }

    #[test]
    fn test_read_bool() {
        assert_eq!(
            read_bool(&cbor_int!(123)),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(read_bool(&cbor_bool!(true)), Ok(true));
        assert_eq!(read_bool(&cbor_bool!(false)), Ok(false));
        assert_eq!(
            read_bool(&cbor_text!("foo")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_bool(&cbor_bytes_lit!(b"bar")),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_bool(&cbor_array![]),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
        assert_eq!(
            read_bool(&cbor_map! {}),
            Err(CTAP2_ERR_CBOR_UNEXPECTED_TYPE)
        );
    }

    #[test]
    fn test_from_public_key_credential_rp_entity() {
        let cbor_rp_entity = cbor_map! {
            "id" => "example.com",
            "name" => "Example",
            "icon" => "example.com/icon.png",
        };
        let rp_entity = PublicKeyCredentialRpEntity::try_from(&cbor_rp_entity);
        let expected_rp_entity = PublicKeyCredentialRpEntity {
            rp_id: "example.com".to_string(),
            rp_name: Some("Example".to_string()),
            rp_icon: Some("example.com/icon.png".to_string()),
        };
        assert_eq!(rp_entity, Ok(expected_rp_entity));
    }

    #[test]
    fn test_from_into_public_key_credential_user_entity() {
        let cbor_user_entity = cbor_map! {
            "id" => vec![0x1D, 0x1D, 0x1D, 0x1D],
            "name" => "foo",
            "displayName" => "bar",
            "icon" => "example.com/foo/icon.png",
        };
        let user_entity = PublicKeyCredentialUserEntity::try_from(&cbor_user_entity);
        let expected_user_entity = PublicKeyCredentialUserEntity {
            user_id: vec![0x1D, 0x1D, 0x1D, 0x1D],
            user_name: Some("foo".to_string()),
            user_display_name: Some("bar".to_string()),
            user_icon: Some("example.com/foo/icon.png".to_string()),
        };
        assert_eq!(user_entity, Ok(expected_user_entity));
        let created_cbor: cbor::Value = user_entity.unwrap().into();
        assert_eq!(created_cbor, cbor_user_entity);
    }

    #[test]
    fn test_from_into_public_key_credential_type() {
        let cbor_credential_type = cbor_text!("public-key");
        let credential_type = PublicKeyCredentialType::try_from(&cbor_credential_type);
        let expected_credential_type = PublicKeyCredentialType::PublicKey;
        assert_eq!(credential_type, Ok(expected_credential_type));
        let created_cbor: cbor::Value = credential_type.unwrap().into();
        assert_eq!(created_cbor, cbor_credential_type);
    }

    #[test]
    fn test_from_into_authenticator_transport() {
        let cbor_authenticator_transport = cbor_text!("usb");
        let authenticator_transport =
            AuthenticatorTransport::try_from(&cbor_authenticator_transport);
        let expected_authenticator_transport = AuthenticatorTransport::Usb;