2020-02-21 09:45:04 +00:00
|
|
|
use core::convert::TryFrom;
|
2019-09-06 10:55:00 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2020-02-02 14:23:47 +00:00
|
|
|
use derive_more::{Deref, DerefMut};
|
|
|
|
|
2020-02-21 09:45:04 +00:00
|
|
|
use crate::tags::ExtXKey;
|
|
|
|
use crate::types::{EncryptionMethod, ProtocolVersion};
|
2019-09-17 12:45:10 +00:00
|
|
|
use crate::utils::tag;
|
2019-10-04 09:02:21 +00:00
|
|
|
use crate::{Error, RequiredVersion};
|
2019-09-10 09:05:20 +00:00
|
|
|
|
2019-09-22 16:00:38 +00:00
|
|
|
/// # [4.3.4.5. EXT-X-SESSION-KEY]
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2020-02-14 12:05:18 +00:00
|
|
|
/// The [`ExtXSessionKey`] tag allows encryption keys from [`MediaPlaylist`]s
|
|
|
|
/// to be specified in a [`MasterPlaylist`]. This allows the client to
|
|
|
|
/// preload these keys without having to read the [`MediaPlaylist`]s
|
2019-09-22 16:00:38 +00:00
|
|
|
/// first.
|
2019-09-06 10:55:00 +00:00
|
|
|
///
|
2020-02-14 12:05:18 +00:00
|
|
|
/// [`MediaPlaylist`]: crate::MediaPlaylist
|
|
|
|
/// [`MasterPlaylist`]: crate::MasterPlaylist
|
2019-09-06 10:55:00 +00:00
|
|
|
/// [4.3.4.5. EXT-X-SESSION-KEY]: https://tools.ietf.org/html/rfc8216#section-4.3.4.5
|
2020-02-02 14:23:47 +00:00
|
|
|
#[derive(Deref, DerefMut, Debug, Clone, PartialEq, Eq, Hash)]
|
2020-02-21 09:45:04 +00:00
|
|
|
pub struct ExtXSessionKey(ExtXKey);
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
impl ExtXSessionKey {
|
|
|
|
pub(crate) const PREFIX: &'static str = "#EXT-X-SESSION-KEY:";
|
|
|
|
|
2019-10-03 14:23:27 +00:00
|
|
|
/// Makes a new [`ExtXSessionKey`] tag.
|
2019-09-22 16:00:38 +00:00
|
|
|
///
|
2019-09-15 14:45:43 +00:00
|
|
|
/// # Panic
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2019-10-03 14:23:27 +00:00
|
|
|
/// An [`ExtXSessionKey`] should only be used,
|
|
|
|
/// if the segments of the stream are encrypted.
|
|
|
|
/// Therefore this function will panic,
|
|
|
|
/// if the `method` is [`EncryptionMethod::None`].
|
2019-09-22 16:00:38 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2019-09-22 16:00:38 +00:00
|
|
|
/// ```
|
2020-02-21 09:45:04 +00:00
|
|
|
/// # use hls_m3u8::tags::{ExtXSessionKey, ExtXKey};
|
2019-09-22 16:00:38 +00:00
|
|
|
/// use hls_m3u8::types::EncryptionMethod;
|
|
|
|
///
|
2020-02-21 09:45:04 +00:00
|
|
|
/// ExtXSessionKey::new(ExtXKey::new(
|
|
|
|
/// EncryptionMethod::Aes128,
|
|
|
|
/// "https://www.example.com/",
|
|
|
|
/// ));
|
2019-09-22 16:00:38 +00:00
|
|
|
/// ```
|
2020-02-24 15:30:43 +00:00
|
|
|
#[must_use]
|
2020-02-21 09:45:04 +00:00
|
|
|
pub fn new(inner: ExtXKey) -> Self {
|
|
|
|
if inner.method() == EncryptionMethod::None {
|
|
|
|
panic!("the encryption method should never be `None`");
|
2019-09-15 14:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 09:45:04 +00:00
|
|
|
Self(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<ExtXKey> for ExtXSessionKey {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: ExtXKey) -> Result<Self, Self::Error> {
|
|
|
|
if value.method() == EncryptionMethod::None {
|
|
|
|
return Err(Error::custom(
|
|
|
|
"the encryption method should never be `None`",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(value))
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
2019-09-22 08:57:28 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2020-02-10 12:21:48 +00:00
|
|
|
/// This tag requires the same [`ProtocolVersion`] that is returned by
|
|
|
|
/// `DecryptionKey::required_version`.
|
2019-09-22 08:57:28 +00:00
|
|
|
impl RequiredVersion for ExtXSessionKey {
|
2019-10-03 15:01:15 +00:00
|
|
|
fn required_version(&self) -> ProtocolVersion { self.0.required_version() }
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ExtXSessionKey {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-02-21 09:45:04 +00:00
|
|
|
// TODO: this is not the most elegant solution
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}{}",
|
|
|
|
Self::PREFIX,
|
|
|
|
self.0.to_string().replacen(ExtXKey::PREFIX, "", 1)
|
|
|
|
)
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ExtXSessionKey {
|
2019-09-15 14:45:43 +00:00
|
|
|
type Err = Error;
|
2019-09-08 09:30:52 +00:00
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
2020-02-21 09:45:04 +00:00
|
|
|
Ok(Self(ExtXKey::parse_from_str(tag(input, Self::PREFIX)?)?))
|
2019-09-17 12:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-15 14:45:43 +00:00
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2019-09-22 16:00:38 +00:00
|
|
|
use crate::types::{EncryptionMethod, KeyFormat};
|
2019-10-08 13:42:33 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-09-15 14:45:43 +00:00
|
|
|
fn test_display() {
|
2020-02-21 09:45:04 +00:00
|
|
|
let mut key = ExtXSessionKey::new(ExtXKey::new(
|
2019-09-15 14:45:43 +00:00
|
|
|
EncryptionMethod::Aes128,
|
2019-09-21 10:11:36 +00:00
|
|
|
"https://www.example.com/hls-key/key.bin",
|
2020-02-21 09:45:04 +00:00
|
|
|
));
|
|
|
|
|
2019-09-22 18:33:40 +00:00
|
|
|
key.set_iv(Some([
|
2019-09-15 14:45:43 +00:00
|
|
|
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
|
2019-09-22 18:33:40 +00:00
|
|
|
]));
|
2019-09-15 14:45:43 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
key.to_string(),
|
2020-02-21 09:45:04 +00:00
|
|
|
concat!(
|
|
|
|
"#EXT-X-SESSION-KEY:",
|
|
|
|
"METHOD=AES-128,",
|
|
|
|
"URI=\"https://www.example.com/hls-key/key.bin\",",
|
|
|
|
"IV=0x10ef8f758ca555115584bb5b3c687f52"
|
|
|
|
)
|
|
|
|
.to_string()
|
2019-09-15 14:45:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parser() {
|
|
|
|
assert_eq!(
|
2020-02-21 09:45:04 +00:00
|
|
|
concat!(
|
|
|
|
"#EXT-X-SESSION-KEY:",
|
|
|
|
"METHOD=AES-128,",
|
|
|
|
"URI=\"https://priv.example.com/key.php?r=52\""
|
|
|
|
)
|
|
|
|
.parse::<ExtXSessionKey>()
|
|
|
|
.unwrap(),
|
|
|
|
ExtXSessionKey::new(ExtXKey::new(
|
2019-09-15 14:45:43 +00:00
|
|
|
EncryptionMethod::Aes128,
|
2019-09-21 10:11:36 +00:00
|
|
|
"https://priv.example.com/key.php?r=52"
|
2020-02-21 09:45:04 +00:00
|
|
|
))
|
2019-09-15 14:45:43 +00:00
|
|
|
);
|
|
|
|
|
2020-02-21 09:45:04 +00:00
|
|
|
let mut key = ExtXSessionKey::new(ExtXKey::new(
|
2019-09-15 14:45:43 +00:00
|
|
|
EncryptionMethod::Aes128,
|
2019-09-21 10:11:36 +00:00
|
|
|
"https://www.example.com/hls-key/key.bin",
|
2020-02-21 09:45:04 +00:00
|
|
|
));
|
2019-09-22 18:33:40 +00:00
|
|
|
key.set_iv(Some([
|
2019-09-15 14:45:43 +00:00
|
|
|
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
|
2019-09-22 18:33:40 +00:00
|
|
|
]));
|
2019-09-15 14:45:43 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-21 09:45:04 +00:00
|
|
|
concat!(
|
|
|
|
"#EXT-X-SESSION-KEY:",
|
|
|
|
"METHOD=AES-128,",
|
|
|
|
"URI=\"https://www.example.com/hls-key/key.bin\",",
|
|
|
|
"IV=0X10ef8f758ca555115584bb5b3c687f52"
|
|
|
|
)
|
|
|
|
.parse::<ExtXSessionKey>()
|
|
|
|
.unwrap(),
|
2019-09-15 14:45:43 +00:00
|
|
|
key
|
|
|
|
);
|
|
|
|
|
2019-09-22 16:00:38 +00:00
|
|
|
key.set_key_format(Some(KeyFormat::Identity));
|
2019-09-15 14:45:43 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-02-21 09:45:04 +00:00
|
|
|
concat!(
|
|
|
|
"#EXT-X-SESSION-KEY:",
|
|
|
|
"METHOD=AES-128,",
|
|
|
|
"URI=\"https://www.example.com/hls-key/key.bin\",",
|
|
|
|
"IV=0x10ef8f758ca555115584bb5b3c687f52,",
|
|
|
|
"KEYFORMAT=\"identity\"",
|
|
|
|
)
|
|
|
|
.parse::<ExtXSessionKey>()
|
|
|
|
.unwrap(),
|
2019-09-15 14:45:43 +00:00
|
|
|
key
|
|
|
|
)
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
2019-09-22 08:57:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_required_version() {
|
|
|
|
assert_eq!(
|
2020-02-21 09:45:04 +00:00
|
|
|
ExtXSessionKey::new(ExtXKey::new(
|
|
|
|
EncryptionMethod::Aes128,
|
|
|
|
"https://www.example.com/"
|
|
|
|
))
|
|
|
|
.required_version(),
|
2019-09-22 08:57:28 +00:00
|
|
|
ProtocolVersion::V1
|
|
|
|
);
|
|
|
|
}
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
// ExtXSessionKey::new should panic, if the provided
|
|
|
|
// EncryptionMethod is None!
|
2020-02-02 12:38:11 +00:00
|
|
|
#[test]
|
2020-02-21 09:45:04 +00:00
|
|
|
#[should_panic = "the encryption method should never be `None`"]
|
2020-02-24 15:45:10 +00:00
|
|
|
fn test_new_panic() { let _ = ExtXSessionKey::new(ExtXKey::new(EncryptionMethod::None, "")); }
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deref() {
|
2020-02-21 09:45:04 +00:00
|
|
|
let key = ExtXSessionKey::new(ExtXKey::new(
|
|
|
|
EncryptionMethod::Aes128,
|
|
|
|
"https://www.example.com/",
|
|
|
|
));
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
assert_eq!(key.method(), EncryptionMethod::Aes128);
|
2020-02-02 12:38:11 +00:00
|
|
|
assert_eq!(key.uri(), Some(&"https://www.example.com/".into()));
|
2019-09-22 18:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deref_mut() {
|
2020-02-21 09:45:04 +00:00
|
|
|
let mut key = ExtXSessionKey::new(ExtXKey::new(
|
|
|
|
EncryptionMethod::Aes128,
|
|
|
|
"https://www.example.com/",
|
|
|
|
));
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
key.set_method(EncryptionMethod::None);
|
|
|
|
assert_eq!(key.method(), EncryptionMethod::None);
|
|
|
|
key.set_uri(Some("https://www.github.com/"));
|
2020-02-02 12:38:11 +00:00
|
|
|
assert_eq!(key.uri(), Some(&"https://www.github.com/".into()));
|
2019-09-22 18:33:40 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|