pub(crate) unsafe fn ykpiv_set_mgmkey2(
    state: *mut YubiKey,
    new_key: *const u8,
    touch: u8,
) -> ErrorKind {
    let mut data = [0u8; 261];
    let mut recv_len = data.len() as u32;
    let mut sw: i32 = 0;
    let mut res = ErrorKind::Ok;
    let mut apdu = APDU::default();

    if _ykpiv_begin_transaction(state) != ErrorKind::Ok {
        return ErrorKind::PcscError;
    }

    if _ykpiv_ensure_application_selected(state) == ErrorKind::Ok {
        if yk_des_is_weak_key(new_key, (8i32 * 3i32) as (usize)) {
            if (*state).verbose != 0 {
                // TODO(tarcieri): format string
                eprint!("Won\'t set new key \'");
                dump_hex(new_key, DES_LEN_3DES as u32);
                eprintln!("\' since it\'s weak (with odd parity).");
            }
            res = ErrorKind::KeyError;
            apdu.ins = YKPIV_INS_SET_MGMKEY;
            apdu.p1 = 0xff;

            apdu.p2 = match touch {
                0 => 0xff,
                1 => 0xfe,
                _ => {
                    _ykpiv_end_transaction(state);
                    return ErrorKind::GenericError;
                }
            };

            apdu.lc = DES_LEN_3DES as u8 + 3;
            apdu.data[0] = YKPIV_ALGO_3DES;
            apdu.data[1] = YKPIV_KEY_CARDMGM;
            apdu.data[2] = DES_LEN_3DES as u8;
            memcpy(
                apdu.data.as_mut_ptr().offset(3) as *mut c_void,
                new_key as *const c_void,
                DES_LEN_3DES,
            );
        } else {
            res = _send_data(state, &mut apdu, data.as_mut_ptr(), &mut recv_len, &mut sw);

            if res != ErrorKind::Ok && sw != SW_SUCCESS {
                res = ErrorKind::GenericError;
            }
        }
    }

    apdu.zeroize();
    _ykpiv_end_transaction(state);
    res
}
/// Set the management key (MGM)
Set the management key (MGM)
Set the management key ( MGM )
pub(crate) unsafe fn ykpiv_set_mgmkey2(
    state: *mut YubiKey,
    new_key: *const u8,
    touch: u8,
) -> ErrorKind {
    let mut data = [0u8; 261];
    let mut recv_len = data.len() as u32;
    let mut sw: i32 = 0;
    let mut res = ErrorKind::Ok;
    let mut apdu = APDU::default();
    if _ykpiv_begin_transaction(state) != ErrorKind::Ok {
        return ErrorKind::PcscError;
    }
    if _ykpiv_ensure_application_selected(state) == ErrorKind::Ok {
        if yk_des_is_weak_key(new_key, (8i32 * 3i32) as (usize)) {
            if (*state).verbose != 0 {
                // TODO(tarcieri): format string
                eprint!("Won\'t set new key \'");
                dump_hex(new_key, DES_LEN_3DES as u32);
                eprintln!("\' since it\'s weak (with odd parity).");
            }
            res = ErrorKind::KeyError;
            apdu.ins = YKPIV_INS_SET_MGMKEY;
            apdu.p1 = 0xff;
            apdu.p2 = match touch {
                0 => 0xff,
                1 => 0xfe,
                _ => {
                    _ykpiv_end_transaction(state);
                    return ErrorKind::GenericError;
                }
            };
            apdu.lc = DES_LEN_3DES as u8 + 3;
            apdu.data[0] = YKPIV_ALGO_3DES;
            apdu.data[1] = YKPIV_KEY_CARDMGM;
            apdu.data[2] = DES_LEN_3DES as u8;
            memcpy(
                apdu.data.as_mut_ptr().offset(3) as *mut c_void,
                new_key as *const c_void,
                DES_LEN_3DES,
            );
        } else {
            res = _send_data(state, &mut apdu, data.as_mut_ptr(), &mut recv_len, &mut sw);
            if res != ErrorKind::Ok && sw != SW_SUCCESS {
                res = ErrorKind::GenericError;
            }
        }
    }
    apdu.zeroize();
    _ykpiv_end_transaction(state);
    res
}
pub ( crate ) unsafe fn ykpiv_set_mgmkey2 ( state : * mut YubiKey , new_key : * const u8 , touch : u8 , ) -> ErrorKind { let mut data = [ 0u8 ; 261 ] ; let mut recv_len = data . len ( ) as u32 ; let mut sw : i32 = 0 ; let mut res = ErrorKind :: Ok ; let mut apdu = APDU :: default ( ) ; if _ykpiv_begin_transaction ( state ) != ErrorKind :: Ok { return ErrorKind :: PcscError ; } if _ykpiv_ensure_application_selected ( state ) == ErrorKind :: Ok { if yk_des_is_weak_key ( new_key , ( 8i32 * 3i32 ) as ( usize ) ) { if ( * state ) . verbose != 0 { // TODO(tarcieri): format string eprint ! ( " \' \' " ) ; dump_hex ( new_key , DES_LEN_3DES as u32 ) ; eprintln ! ( " \' \' " ) ; } res = ErrorKind :: KeyError ; apdu . ins = YKPIV_INS_SET_MGMKEY ; apdu . p1 = 0xff ; apdu . p2 = match touch { 0 => 0xff , 1 => 0xfe , _ => { _ykpiv_end_transaction ( state ) ; return ErrorKind :: GenericError ; } } ; apdu . lc = DES_LEN_3DES as u8 + 3 ; apdu . data [ 0 ] = YKPIV_ALGO_3DES ; apdu . data [ 1 ] = YKPIV_KEY_CARDMGM ; apdu . data [ 2 ] = DES_LEN_3DES as u8 ; memcpy ( apdu . data . as_mut_ptr ( ) . offset ( 3 ) as * mut c_void , new_key as * const c_void , DES_LEN_3DES , ) ; } else { res = _send_data ( state , & mut apdu , data . as_mut_ptr ( ) , & mut recv_len , & mut sw ) ; if res != ErrorKind :: Ok && sw != SW_SUCCESS { res = ErrorKind :: GenericError ; } } } apdu . zeroize ( ) ; _ykpiv_end_transaction ( state ) ; res }
Set the management key (MGM)
Set the management key ( MGM )
state
*mut YubiKey
new_key
*const u8
touch
u8