use crate::apdu::command::Command;
use crate::tags::{ShortTag, Tags};
use crate::{KeyType, OPENPGP_APPLICATION};
pub(crate) fn select_openpgp() -> Command {
Command::new(0x00, 0xA4, 0x04, 0x00, OPENPGP_APPLICATION.to_vec())
}
fn get_data<T: Into<ShortTag>>(tag: T) -> Command {
match tag.into() {
ShortTag::One(tag0) => Command::new(0x00, 0xCA, 0, tag0, vec![]),
ShortTag::Two(tag0, tag1) => Command::new(0x00, 0xCA, tag0, tag1, vec![]),
}
}
pub(crate) fn application_related_data() -> Command {
get_data(Tags::ApplicationRelatedData)
}
pub(crate) fn private_use_do(num: u8) -> Command {
match num {
1 => get_data(Tags::PrivateUse1),
2 => get_data(Tags::PrivateUse2),
3 => get_data(Tags::PrivateUse3),
4 => get_data(Tags::PrivateUse4),
_ => panic!("this should never happen"), }
}
pub(crate) fn url() -> Command {
get_data(Tags::Url)
}
pub(crate) fn login_data() -> Command {
get_data(Tags::LoginData)
}
pub(crate) fn cardholder_related_data() -> Command {
get_data(Tags::CardholderRelatedData)
}
pub(crate) fn security_support_template() -> Command {
get_data(Tags::SecuritySupportTemplate)
}
pub(crate) fn cardholder_certificate() -> Command {
get_data(Tags::CardholderCertificate)
}
pub(crate) fn get_next_cardholder_certificate() -> Command {
Command::new(0x00, 0xCC, 0x7f, 0x21, vec![])
}
pub(crate) fn algo_info() -> Command {
get_data(Tags::AlgorithmInformation)
}
pub(crate) fn attestation_certificate() -> Command {
get_data(Tags::AttestationCertificate)
}
pub(crate) fn firmware_version() -> Command {
Command::new(0x00, 0xF1, 0x00, 0x00, vec![])
}
pub(crate) fn set_identity(id: u8) -> Command {
Command::new(0x00, 0x85, 0x00, id, vec![])
}
pub(crate) fn get_response() -> Command {
Command::new(0x00, 0xC0, 0x00, 0x00, vec![])
}
pub(crate) fn select_data(num: u8, data: Vec<u8>) -> Command {
Command::new(0x00, 0xA5, num, 0x04, data)
}
pub(crate) fn verify_pw1_81(pin: Vec<u8>) -> Command {
Command::new(0x00, 0x20, 0x00, 0x81, pin)
}
pub(crate) fn verify_pw1_82(pin: Vec<u8>) -> Command {
Command::new(0x00, 0x20, 0x00, 0x82, pin)
}
pub(crate) fn verify_pw3(pin: Vec<u8>) -> Command {
Command::new(0x00, 0x20, 0x00, 0x83, pin)
}
pub(crate) fn put_data<T: Into<ShortTag>>(tag: T, data: Vec<u8>) -> Command {
match tag.into() {
ShortTag::One(tag0) => Command::new(0x00, 0xda, 0, tag0, data),
ShortTag::Two(tag0, tag1) => Command::new(0x00, 0xda, tag0, tag1, data),
}
}
pub(crate) fn put_private_use_do(num: u8, data: Vec<u8>) -> Command {
match num {
1 => put_data(Tags::PrivateUse1, data),
2 => put_data(Tags::PrivateUse2, data),
3 => put_data(Tags::PrivateUse3, data),
4 => put_data(Tags::PrivateUse4, data),
_ => panic!("this should never happen"), }
}
pub(crate) fn put_login_data(login_data: Vec<u8>) -> Command {
put_data(Tags::LoginData, login_data)
}
pub(crate) fn put_name(name: Vec<u8>) -> Command {
put_data(Tags::Name, name)
}
pub(crate) fn put_lang(lang: Vec<u8>) -> Command {
put_data(Tags::LanguagePref, lang)
}
pub(crate) fn put_sex(sex: u8) -> Command {
put_data(Tags::Sex, vec![sex])
}
pub(crate) fn put_url(url: Vec<u8>) -> Command {
put_data(Tags::Url, url)
}
pub(crate) fn put_pw_status(data: Vec<u8>) -> Command {
put_data(Tags::PWStatusBytes, data)
}
pub(crate) fn put_cardholder_certificate(data: Vec<u8>) -> Command {
put_data(Tags::CardholderCertificate, data)
}
pub(crate) fn reset_retry_counter_pw1(resetting_code: Option<&[u8]>, new_pin: &[u8]) -> Command {
if let Some(resetting_code) = resetting_code {
let mut data = vec![];
data.extend(resetting_code);
data.extend(new_pin);
Command::new(0x00, 0x2C, 0x00, 0x81, data)
} else {
Command::new(0x00, 0x2C, 0x02, 0x81, new_pin.to_vec())
}
}
pub(crate) fn change_pw1(data: Vec<u8>) -> Command {
Command::new(0x00, 0x24, 0x00, 0x81, data)
}
pub(crate) fn change_pw3(data: Vec<u8>) -> Command {
Command::new(0x00, 0x24, 0x00, 0x83, data)
}
pub(crate) fn signature(data: Vec<u8>) -> Command {
Command::new(0x00, 0x2A, 0x9e, 0x9a, data)
}
pub(crate) fn decryption(data: Vec<u8>) -> Command {
Command::new(0x00, 0x2A, 0x80, 0x86, data)
}
pub(crate) fn internal_authenticate(data: Vec<u8>) -> Command {
Command::new(0x00, 0x88, 0x00, 0x00, data)
}
pub(crate) fn gen_key(data: Vec<u8>) -> Command {
Command::new(0x00, 0x47, 0x80, 0x00, data)
}
pub(crate) fn get_pub_key(data: Vec<u8>) -> Command {
Command::new(0x00, 0x47, 0x81, 0x00, data)
}
pub(crate) fn key_import(data: Vec<u8>) -> Command {
Command::new(0x00, 0xDB, 0x3F, 0xFF, data)
}
pub(crate) fn generate_attestation(key: u8) -> Command {
Command::new(0x80, 0xFB, key, 0x00, vec![])
}
pub(crate) fn terminate_df() -> Command {
Command::new(0x00, 0xe6, 0x00, 0x00, vec![])
}
pub(crate) fn activate_file() -> Command {
Command::new(0x00, 0x44, 0x00, 0x00, vec![])
}
pub(crate) fn manage_security_environment(for_operation: KeyType, key_ref: KeyType) -> Command {
let p2 = match for_operation {
KeyType::Authentication => 0xA4,
KeyType::Decryption => 0xB8,
_ => unreachable!(), };
let data = match key_ref {
KeyType::Decryption => vec![0x83, 0x01, 0x02],
KeyType::Authentication => vec![0x83, 0x01, 0x03],
_ => unreachable!(),
};
Command::new(0, 0x22, 0x41, p2, data)
}