|
| 1 | +// Copyright (c) 2015, Ben Boeckel |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without modification, |
| 5 | +// are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// * Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// * Neither the name of this project nor the names of its contributors |
| 13 | +// may be used to endorse or promote products derived from this software |
| 14 | +// without specific prior written permission. |
| 15 | +// |
| 16 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 23 | +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +use keytype::*; |
| 28 | + |
| 29 | +use std::borrow::Cow; |
| 30 | + |
| 31 | +/// An RxRPC client key. |
| 32 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 33 | +pub struct RxRPC; |
| 34 | + |
| 35 | +impl KeyType for RxRPC { |
| 36 | + /// RxRPC client key descriptions are free-form. |
| 37 | + type Description = str; |
| 38 | + type Payload = Payload; |
| 39 | + |
| 40 | + fn name() -> &'static str { |
| 41 | + "rxrpc" |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 46 | +pub struct Payload { |
| 47 | + expiry: u32, |
| 48 | + version: u8, |
| 49 | + session_key: [u8; 8], |
| 50 | + ticket: Vec<u8>, |
| 51 | +} |
| 52 | + |
| 53 | +impl KeyPayload for Payload { |
| 54 | + fn payload(&self) -> Cow<[u8]> { |
| 55 | + let mut payload = Vec::new(); |
| 56 | + |
| 57 | + // struct rxrpc_key_sec2_v1 { |
| 58 | + // uint16_t security_index; /* 2 */ |
| 59 | + // uint16_t ticket_length; /* length of ticket[] */ |
| 60 | + // uint32_t expiry; /* time at which expires */ |
| 61 | + // uint8_t kvno; /* key version number */ |
| 62 | + // uint8_t __pad[3]; |
| 63 | + // uint8_t session_key[8]; /* DES session key */ |
| 64 | + // uint8_t ticket[0]; /* the encrypted ticket */ |
| 65 | + // }; |
| 66 | + |
| 67 | + payload.extend((2 as u16).to_ne_bytes().iter()); |
| 68 | + payload.extend((self.ticket.len() as u16).to_ne_bytes().iter()); |
| 69 | + payload.extend(self.expiry.to_ne_bytes().iter()); |
| 70 | + payload.extend(self.version.to_ne_bytes().iter()); |
| 71 | + payload.extend([0u8; 3].iter()); |
| 72 | + payload.extend(self.session_key.iter()); |
| 73 | + payload.extend(self.ticket.iter()); |
| 74 | + |
| 75 | + Cow::Owned(payload) |
| 76 | + } |
| 77 | +} |
0 commit comments