use snmp_usm::{self, LocalizedKey, Md5AuthKey, SecurityError, SecurityParams, Sha1AuthKey}; const ENGINE_ID: [u8; 17] = [ 0x80, 0x00, 0x1f, 0x88, 0x80, 0xfa, 0xa8, 0x11, 0x60, 0x0f, 0xa2, 0xc5, 0x5e, 0x00, 0x00, 0x00, 0x00, ]; #[test] fn it_authenticates_out_msg_using_md5() { let mut out_msg = vec![ 0x30, 0x81, 0x89, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x53, 0xA7, 0xD4, 0x88, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x05, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x04, 0x02, 0x03, 0x01, 0x0B, 0xF6, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA0, 0x1C, 0x02, 0x04, 0x70, 0xE2, 0x6A, 0x4D, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x0E, 0x30, 0x0C, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); key.auth_out_msg(&mut out_msg).unwrap(); let expected = vec![ 0x30, 0x81, 0x89, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x53, 0xa7, 0xd4, 0x88, 0x02, 0x03, 0x00, 0xff, 0xe3, 0x04, 0x01, 0x05, 0x02, 0x01, 0x03, 0x04, 0x3c, 0x30, 0x3a, 0x04, 0x11, 0x80, 0x00, 0x1f, 0x88, 0x80, 0xfa, 0xa8, 0x11, 0x60, 0x0f, 0xa2, 0xc5, 0x5e, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x04, 0x02, 0x03, 0x01, 0x0b, 0xf6, 0x04, 0x0d, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6e, 0x65, 0x04, 0x0c, 0xb4, 0x00, 0x8c, 0x53, 0x8d, 0xda, 0xff, 0x2e, 0x51, 0xcd, 0x1a, 0x7a, 0x04, 0x00, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1f, 0x88, 0x80, 0xfa, 0xa8, 0x11, 0x60, 0x0f, 0xa2, 0xc5, 0x5e, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xa0, 0x1c, 0x02, 0x04, 0x70, 0xe2, 0x6a, 0x4d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, ]; assert_eq!(out_msg, expected); } #[test] fn it_authenticates_out_msg_using_sha1() { let mut out_msg = vec![ 0x30, 0x7C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x05, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x1E, 0x9C, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x30, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA0, 0x19, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x0E, 0x30, 0x0C, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); key.auth_out_msg(&mut out_msg).unwrap(); let expected = vec![ 0x30, 0x7C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x05, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x1E, 0x9C, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x1B, 0x70, 0x51, 0x7F, 0xBD, 0x90, 0x66, 0xA2, 0xC4, 0x49, 0x4B, 0x61, 0x04, 0x00, 0x30, 0x30, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA0, 0x19, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x0E, 0x30, 0x0C, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, ]; assert_eq!(out_msg, expected); } #[test] fn it_returns_malformed_msg_for_empty_out_msg() { let mut out_msg = []; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let result = key.auth_out_msg(&mut out_msg); assert_eq!(result, Err(SecurityError::MalformedMsg)); } #[test] fn it_returns_malformed_msg_for_malformed_out_msg() { // Authentication parameters of length 0. let mut out_msg = vec![ 0x30, 0x81, 0x89, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x53, 0xA7, 0xD4, 0x88, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x05, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x04, 0x02, 0x03, 0x01, 0x0B, 0xF6, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x00, 0x04, 0x00, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA0, 0x1C, 0x02, 0x04, 0x70, 0xE2, 0x6A, 0x4D, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x0E, 0x30, 0x0C, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let result = key.auth_out_msg(&mut out_msg); assert_eq!(result, Err(SecurityError::MalformedMsg)); } #[test] fn it_authenticates_in_msg_using_md5() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x0C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x18, 0x02, 0x03, 0x02, 0xBD, 0x40, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x0C, 0xB8, 0x7E, 0x47, 0x96, 0xCE, 0x7F, 0x69, 0x5F, 0x88, 0x74, 0xD9, 0xBF, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let local_engine_id = b""; let engine_boots = 4; let engine_time = 68_598; let result = key.auth_in_msg(&mut in_msg, local_engine_id, engine_boots, engine_time); assert_eq!(result, Ok(())); } #[test] fn it_authenticates_in_msg_using_sha1() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x05, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x39, 0xC7, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x14, 0x49, 0xB6, 0x43, 0xBC, 0x64, 0xFB, 0x07, 0x47, 0x28, 0x12, 0x58, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let local_engine_id = b""; let engine_boots = 4; let engine_time = 68_598; let result = key.auth_in_msg(&mut in_msg, local_engine_id, engine_boots, engine_time); assert_eq!(result, Ok(())); } #[test] fn it_returns_not_in_time_window_error_for_invalid_engine_boots_in_authoritative() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x05, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x39, 0xC7, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x14, 0x49, 0xB6, 0x43, 0xBC, 0x64, 0xFB, 0x07, 0x47, 0x28, 0x12, 0x58, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let engine_boots = 28; // Security parameters engine boots = 27 let engine_time = 14_791; let result = key.auth_in_msg(&mut in_msg, &ENGINE_ID, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::NotInTimeWindow)); } #[test] fn it_returns_not_in_time_window_error_for_invalid_engine_time_in_authoritative() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x05, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x39, 0xC7, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x14, 0x49, 0xB6, 0x43, 0xBC, 0x64, 0xFB, 0x07, 0x47, 0x28, 0x12, 0x58, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let engine_boots = 27; let engine_time = 14_942; // Security parameters engine time = 14_791 let result = key.auth_in_msg(&mut in_msg, &ENGINE_ID, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::NotInTimeWindow)); } #[test] fn it_returns_not_in_time_window_error_for_invalid_engine_boots_in_non_authoritative() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x05, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x39, 0xC7, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x14, 0x49, 0xB6, 0x43, 0xBC, 0x64, 0xFB, 0x07, 0x47, 0x28, 0x12, 0x58, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let local_engine_id = b""; let engine_boots = 28; // Security parameters engine boots = 27 let engine_time = 14_791; let result = key.auth_in_msg(&mut in_msg, local_engine_id, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::NotInTimeWindow)); } #[test] fn it_returns_not_in_time_window_error_for_invalid_engine_time_in_non_authoritative() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x05, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x35, 0x30, 0x33, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x1B, 0x02, 0x02, 0x39, 0xC7, 0x04, 0x07, 0x73, 0x68, 0x61, 0x75, 0x73, 0x65, 0x72, 0x04, 0x0C, 0x14, 0x49, 0xB6, 0x43, 0xBC, 0x64, 0xFB, 0x07, 0x47, 0x28, 0x12, 0x58, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Sha1AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let local_engine_id = b""; let engine_boots = 27; let engine_time = 14_942; // Security parameters engine time = 14_791 let result = key.auth_in_msg(&mut in_msg, local_engine_id, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::NotInTimeWindow)); } #[test] fn it_returns_error_for_in_msg_with_malformed_security_params() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x0C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x18, 0x02, 0x03, 0x02, 0xBD, 0x40, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x0C, 0xB7, 0x7E, 0x47, 0x96, 0xCE, 0x7F, 0x69, 0x5F, 0x88, 0x74, 0xD9, 0xBF, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let engine_id = b""; let engine_boots = 4; let engine_time = 68_598; let result = key.auth_in_msg(&mut in_msg, engine_id, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::WrongAuthParams)); } #[test] fn it_returns_error_for_malformed_in_msg() { // Authentication parameters of length 0. let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x0C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x18, 0x02, 0x03, 0x02, 0xBD, 0x40, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x00, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let engine_id = b""; let engine_boots = 4; let engine_time = 68_598; let result = key.auth_in_msg(&mut in_msg, engine_id, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::MalformedMsg)); } #[test] fn it_returns_not_in_time_window_for_engine_boots_max() { let mut in_msg = vec![ 0x30, 0x82, 0x01, 0x0C, 0x02, 0x01, 0x03, 0x30, 0x0E, 0x02, 0x01, 0x01, 0x02, 0x03, 0x00, 0xFF, 0xE3, 0x04, 0x01, 0x01, 0x02, 0x01, 0x03, 0x04, 0x3C, 0x30, 0x3A, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x18, 0x02, 0x03, 0x02, 0xBD, 0x40, 0x04, 0x0D, 0x64, 0x61, 0x76, 0x69, 0x64, 0x64, 0x75, 0x66, 0x72, 0x65, 0x73, 0x6E, 0x65, 0x04, 0x0C, 0xB8, 0x7E, 0x47, 0x96, 0xCE, 0x7F, 0x69, 0x5F, 0x88, 0x74, 0xD9, 0xBF, 0x04, 0x00, 0x30, 0x81, 0xB8, 0x04, 0x11, 0x80, 0x00, 0x1F, 0x88, 0x80, 0xFA, 0xA8, 0x11, 0x60, 0x0F, 0xA2, 0xC5, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xA2, 0x81, 0xA0, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0x94, 0x30, 0x81, 0x91, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04, 0x81, 0x84, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x64, 0x61, 0x76, 0x69, 0x64, 0x73, 0x2D, 0x6D, 0x62, 0x70, 0x2E, 0x6C, 0x61, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x20, 0x44, 0x61, 0x72, 0x77, 0x69, 0x6E, 0x20, 0x4B, 0x65, 0x72, 0x6E, 0x65, 0x6C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x31, 0x39, 0x2E, 0x35, 0x2E, 0x30, 0x3A, 0x20, 0x54, 0x75, 0x65, 0x20, 0x4D, 0x61, 0x79, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x3A, 0x34, 0x31, 0x3A, 0x34, 0x34, 0x20, 0x50, 0x44, 0x54, 0x20, 0x32, 0x30, 0x32, 0x30, 0x3B, 0x20, 0x72, 0x6F, 0x6F, 0x74, 0x3A, 0x78, 0x6E, 0x75, 0x2D, 0x36, 0x31, 0x35, 0x33, 0x2E, 0x31, 0x32, 0x31, 0x2E, 0x32, 0x7E, 0x32, 0x2F, 0x52, 0x45, 0x4C, 0x45, 0x41, 0x53, 0x45, 0x5F, 0x58, 0x38, 0x36, 0x5F, 0x36, 0x34, 0x20, 0x78, 0x38, 0x36, 0x5F, 0x36, 0x34, ]; let key = Md5AuthKey::new(LocalizedKey::new(b"12345678", &ENGINE_ID)); let local_engine_id = b""; let engine_boots = SecurityParams::ENGINE_BOOTS_MAX; let engine_time = 68_598; let result = key.auth_in_msg(&mut in_msg, local_engine_id, engine_boots, engine_time); assert_eq!(result, Err(SecurityError::NotInTimeWindow)); }