isahc/config/ssl.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
//! Configuration options related to SSL/TLS.
use super::SetOpt;
use curl::easy::{Easy2, SslOpt};
use std::{
iter::FromIterator,
ops::{BitOr, BitOrAssign},
path::PathBuf,
};
#[derive(Clone, Debug)]
enum PathOrBlob {
Path(PathBuf),
Blob(Vec<u8>),
}
/// A client certificate for SSL/TLS client validation.
///
/// Note that this isn't merely an X.509 certificate, but rather a certificate
/// and private key pair.
#[derive(Clone, Debug)]
pub struct ClientCertificate {
/// Name of the cert format.
format: &'static str,
/// The certificate data, either a path or a blob.
data: PathOrBlob,
/// Private key corresponding to the SSL/TLS certificate.
private_key: Option<PrivateKey>,
/// Password to decrypt the certificate file.
password: Option<String>,
}
impl ClientCertificate {
/// Use a PEM-encoded certificate stored in the given byte buffer.
///
/// The certificate object takes ownership of the byte buffer. If a borrowed
/// type is supplied, such as `&[u8]`, then the bytes will be copied.
///
/// The certificate is not parsed or validated here. If the certificate is
/// malformed or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn pem<B, P>(bytes: B, private_key: P) -> Self
where
B: Into<Vec<u8>>,
P: Into<Option<PrivateKey>>,
{
Self {
format: "PEM",
data: PathOrBlob::Blob(bytes.into()),
private_key: private_key.into(),
password: None,
}
}
/// Use a DER-encoded certificate stored in the given byte buffer.
///
/// The certificate object takes ownership of the byte buffer. If a borrowed
/// type is supplied, such as `&[u8]`, then the bytes will be copied.
///
/// The certificate is not parsed or validated here. If the certificate is
/// malformed or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn der<B, P>(bytes: B, private_key: P) -> Self
where
B: Into<Vec<u8>>,
P: Into<Option<PrivateKey>>,
{
Self {
format: "DER",
data: PathOrBlob::Blob(bytes.into()),
private_key: private_key.into(),
password: None,
}
}
/// Use a certificate and private key from a PKCS #12 archive stored in the
/// given byte buffer.
///
/// The certificate object takes ownership of the byte buffer. If a borrowed
/// type is supplied, such as `&[u8]`, then the bytes will be copied.
///
/// The certificate is not parsed or validated here. If the certificate is
/// malformed or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn pkcs12<B, P>(bytes: B, password: P) -> Self
where
B: Into<Vec<u8>>,
P: Into<Option<String>>,
{
Self {
format: "P12",
data: PathOrBlob::Blob(bytes.into()),
private_key: None,
password: password.into(),
}
}
/// Get a certificate from a PEM-encoded file.
///
/// The certificate file is not loaded or validated here. If the file does
/// not exist or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn pem_file(path: impl Into<PathBuf>, private_key: impl Into<Option<PrivateKey>>) -> Self {
Self {
format: "PEM",
data: PathOrBlob::Path(path.into()),
private_key: private_key.into(),
password: None,
}
}
/// Get a certificate from a DER-encoded file.
///
/// The certificate file is not loaded or validated here. If the file does
/// not exist or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn der_file(path: impl Into<PathBuf>, private_key: impl Into<Option<PrivateKey>>) -> Self {
Self {
format: "DER",
data: PathOrBlob::Path(path.into()),
private_key: private_key.into(),
password: None,
}
}
/// Get a certificate and private key from a PKCS #12-encoded file.
///
/// The certificate file is not loaded or validated here. If the file does
/// not exist or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn pkcs12_file(path: impl Into<PathBuf>, password: impl Into<Option<String>>) -> Self {
Self {
format: "P12",
data: PathOrBlob::Path(path.into()),
private_key: None,
password: password.into(),
}
}
/// Get a certificate and private key from a PKCS #12-encoded file.
///
/// Use [`pkcs12_file`][ClientCertificate::pkcs12_file] instead.
#[inline]
#[doc(hidden)]
#[deprecated(
since = "1.4.0",
note = "please use the more clearly-named `pkcs12_file` instead"
)]
pub fn p12_file(path: impl Into<PathBuf>, password: impl Into<Option<String>>) -> Self {
Self::pkcs12_file(path, password)
}
}
impl SetOpt for ClientCertificate {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
easy.ssl_cert_type(self.format)?;
match &self.data {
PathOrBlob::Path(path) => easy.ssl_cert(path.as_path()),
PathOrBlob::Blob(bytes) => easy.ssl_cert_blob(bytes.as_slice()),
}?;
if let Some(key) = self.private_key.as_ref() {
key.set_opt(easy)?;
}
if let Some(password) = self.password.as_ref() {
easy.key_password(password)?;
}
Ok(())
}
}
/// A private key file.
#[derive(Clone, Debug)]
pub struct PrivateKey {
/// Key format name.
format: &'static str,
/// The certificate data, either a path or a blob.
data: PathOrBlob,
/// Password to decrypt the key file.
password: Option<String>,
}
impl PrivateKey {
/// Use a PEM-encoded private key stored in the given byte buffer.
///
/// The private key object takes ownership of the byte buffer. If a borrowed
/// type is supplied, such as `&[u8]`, then the bytes will be copied.
///
/// The key is not parsed or validated here. If the key is malformed or the
/// format is not supported by the underlying SSL/TLS engine, an error will
/// be returned when attempting to send a request using the offending key.
pub fn pem<B, P>(bytes: B, password: P) -> Self
where
B: Into<Vec<u8>>,
P: Into<Option<String>>,
{
Self {
format: "PEM",
data: PathOrBlob::Blob(bytes.into()),
password: password.into(),
}
}
/// Use a DER-encoded private key stored in the given byte buffer.
///
/// The private key object takes ownership of the byte buffer. If a borrowed
/// type is supplied, such as `&[u8]`, then the bytes will be copied.
///
/// The key is not parsed or validated here. If the key is malformed or the
/// format is not supported by the underlying SSL/TLS engine, an error will
/// be returned when attempting to send a request using the offending key.
pub fn der<B, P>(bytes: B, password: P) -> Self
where
B: Into<Vec<u8>>,
P: Into<Option<String>>,
{
Self {
format: "DER",
data: PathOrBlob::Blob(bytes.into()),
password: password.into(),
}
}
/// Get a PEM-encoded private key file.
///
/// The key file is not loaded or validated here. If the file does not exist
/// or the format is not supported by the underlying SSL/TLS engine, an
/// error will be returned when attempting to send a request using the
/// offending key.
pub fn pem_file(path: impl Into<PathBuf>, password: impl Into<Option<String>>) -> Self {
Self {
format: "PEM",
data: PathOrBlob::Path(path.into()),
password: password.into(),
}
}
/// Get a DER-encoded private key file.
///
/// The key file is not loaded or validated here. If the file does not exist
/// or the format is not supported by the underlying SSL/TLS engine, an
/// error will be returned when attempting to send a request using the
/// offending key.
pub fn der_file(path: impl Into<PathBuf>, password: impl Into<Option<String>>) -> Self {
Self {
format: "DER",
data: PathOrBlob::Path(path.into()),
password: password.into(),
}
}
}
impl SetOpt for PrivateKey {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
easy.ssl_key_type(self.format)?;
match &self.data {
PathOrBlob::Path(path) => easy.ssl_key(path.as_path()),
PathOrBlob::Blob(bytes) => easy.ssl_key_blob(bytes.as_slice()),
}?;
if let Some(password) = self.password.as_ref() {
easy.key_password(password)?;
}
Ok(())
}
}
/// A public CA certificate bundle file.
#[derive(Clone, Debug)]
pub struct CaCertificate {
/// Path to the certificate bundle file. Currently only file paths are
/// supported.
path: PathBuf,
}
impl CaCertificate {
/// Get a CA certificate from a path to a certificate bundle file.
///
/// The certificate file is not loaded or validated here. If the file does
/// not exist or the format is not supported by the underlying SSL/TLS
/// engine, an error will be returned when attempting to send a request
/// using the offending certificate.
pub fn file(ca_bundle_path: impl Into<PathBuf>) -> Self {
Self {
path: ca_bundle_path.into(),
}
}
}
impl SetOpt for CaCertificate {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
easy.cainfo(&self.path)
}
}
#[derive(Clone, Debug)]
pub(crate) struct Ciphers(String);
impl FromIterator<String> for Ciphers {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
Ciphers(iter.into_iter().collect::<Vec<_>>().join(":"))
}
}
impl SetOpt for Ciphers {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
easy.ssl_cipher_list(&self.0)
}
}
/// A flag that can be used to alter the behavior of SSL/TLS connections.
///
/// Most options are for disabling security checks that introduce security
/// risks, but may be required as a last resort.
#[derive(Clone, Copy, Debug)]
pub struct SslOption(usize);
impl Default for SslOption {
fn default() -> Self {
Self::NONE
}
}
impl SslOption {
/// An empty set of options. This is the default.
pub const NONE: Self = SslOption(0);
/// Disables certificate validation.
///
/// # Warning
///
/// You should think very carefully before using this method. If invalid
/// certificates are trusted, *any* certificate for any site will be trusted
/// for use. This includes expired certificates. This introduces significant
/// vulnerabilities, and should only be used as a last resort.
pub const DANGER_ACCEPT_INVALID_CERTS: Self = SslOption(0b0001);
/// Disables hostname verification on certificates.
///
/// # Warning
///
/// You should think very carefully before you use this method. If hostname
/// verification is not used, any valid certificate for any site will be
/// trusted for use from any other. This introduces a significant
/// vulnerability to man-in-the-middle attacks.
pub const DANGER_ACCEPT_INVALID_HOSTS: Self = SslOption(0b0010);
/// Disables certificate revocation checks for backends where such behavior
/// is present.
///
/// This option is only supported for Schannel (the native Windows SSL
/// library).
pub const DANGER_ACCEPT_REVOKED_CERTS: Self = SslOption(0b0100);
const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
}
impl BitOr for SslOption {
type Output = Self;
fn bitor(mut self, other: Self) -> Self {
self |= other;
self
}
}
impl BitOrAssign for SslOption {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl SetOpt for SslOption {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
let mut opt = SslOpt::new();
opt.no_revoke(self.contains(Self::DANGER_ACCEPT_REVOKED_CERTS));
easy.ssl_options(&opt)?;
easy.ssl_verify_peer(!self.contains(Self::DANGER_ACCEPT_INVALID_CERTS))?;
easy.ssl_verify_host(!self.contains(Self::DANGER_ACCEPT_INVALID_HOSTS))
}
}
#[cfg(test)]
mod tests {
use super::SslOption;
#[test]
fn default_ssl_options() {
let options = SslOption::default();
assert!(!options.contains(SslOption::DANGER_ACCEPT_INVALID_CERTS));
assert!(!options.contains(SslOption::DANGER_ACCEPT_INVALID_HOSTS));
assert!(!options.contains(SslOption::DANGER_ACCEPT_REVOKED_CERTS));
}
#[test]
fn ssl_option_invalid_certs() {
let options = SslOption::DANGER_ACCEPT_INVALID_CERTS;
assert!(options.contains(SslOption::DANGER_ACCEPT_INVALID_CERTS));
assert!(!options.contains(SslOption::DANGER_ACCEPT_INVALID_HOSTS));
let options = SslOption::DANGER_ACCEPT_INVALID_HOSTS;
assert!(!options.contains(SslOption::DANGER_ACCEPT_INVALID_CERTS));
assert!(options.contains(SslOption::DANGER_ACCEPT_INVALID_HOSTS));
}
}