use rcgen::{BasicConstraints, Certificate, CertificateParams};
use rcgen::{
	CertificateRevocationList, CrlDistributionPoint, CrlIssuingDistributionPoint, CrlScope,
};
use rcgen::{CertificateRevocationListParams, DnType, IsCa, KeyIdMethod};
use rcgen::{
	KeyUsagePurpose, RevocationReason, RevokedCertParams, SerialNumber, PKCS_ECDSA_P256_SHA256,
};
use time::{Duration, OffsetDateTime};

// Generated by adding `println!("{}", cert.serialize_private_key_pem());`
// to the test_webpki_25519 test and panicing explicitly.
// This is a "v2" key containing the public key as well as the
// private one.
#[allow(unused)]
pub const ED25519_TEST_KEY_PAIR_PEM_V2: &str = r#"
-----BEGIN PRIVATE KEY-----
MFMCAQEwBQYDK2VwBCIEIC2pHJYjFHhK8V7mj6BnHWUVMS4CRolUlDdRXKCtguDu
oSMDIQDrvH/x8Nx9untsuc6ET+ce3w7PSuLY8BLWcHdXDGvkQA==
-----END PRIVATE KEY-----
"#;
// Generated with `openssl genpkey -algorithm ED25519`
// A "v1" key as it doesn't contain the public key (which can be
// derived from the private one)
#[allow(unused)]
pub const ED25519_TEST_KEY_PAIR_PEM_V1: &str = r#"
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIDSat0MacDt2fokpnzuBaXvAQR6RJGS9rgIYOU2mZKld
-----END PRIVATE KEY-----
"#;

/*
Generated by: openssl genpkey -algorithm RSA \
 -pkeyopt rsa_keygen_bits:2048 \
 -pkeyopt rsa_keygen_pubexp:65537 | \
 openssl pkcs8 -topk8 -nocrypt -outform pem
*/
#[cfg(feature = "pem")]
pub const RSA_TEST_KEY_PAIR_PEM: &str = r#"
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDYjmgyV3/LSizJ
XrYrATZrrPr2Edo8yiOgBLFmi4sgeGdQ5n6nhjTGfBEIP2Ia6z+hbiGOMncabEBc
zkdME+JFYVCSkS7r4ivMOzp2egxLgcPKcerBoXI8DUbHhIR9z89lHiP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7Yb0jk+b6P3RravOCYmxwuPwfm7rVyV+kLczFxZUfauVJcrrI1Iy+BAoGBAJjG
MEDGeSxaLOS5LYgyNg3ePPzkhaEruRDpHUBNmW+npZPfgSVhObXUb2IfQXwvu0Qt
3yuPcgcQKDFFIH/8UOwGWWKE4cZyk1KGeY9K/5D6Yr3JfX5tj08vSX3Y0SMtvhZ4
u0izoZ8abiOIrtdwXlau76/D2ICLbON5Kykz/NE1AoGAId2+pO9p8jBt9l+5jZo7
Rw/mb5icMaG2hqAzs37gUPbpSwQFOmGhQmNM+WvYEvUUuiTxI3AOeEK8Mj+BVB4+
uE3X/fWK/JR9iOzH9OM31Nua8/EJzr7BmUpXeRr4dAtVimeQ+5HY6IgRsFGPKKwv
YPTHy8SWRA2sMII3ArhHJ8A=
-----END PRIVATE KEY-----
"#;

pub fn default_params() -> CertificateParams {
	let mut params =
		CertificateParams::new(vec!["crabs.crabs".to_string(), "localhost".to_string()]);
	params
		.distinguished_name
		.push(DnType::OrganizationName, "Crab widgits SE");
	params
		.distinguished_name
		.push(DnType::CommonName, "Master CA");
	params
}

#[allow(unused)] // Used by openssl + x509-parser features.
pub fn test_crl() -> (CertificateRevocationList, Certificate) {
	let mut issuer = default_params();
	issuer.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
	issuer.key_usages = vec![
		KeyUsagePurpose::KeyCertSign,
		KeyUsagePurpose::DigitalSignature,
		KeyUsagePurpose::CrlSign,
	];
	let issuer = Certificate::from_params(issuer).unwrap();

	let now = OffsetDateTime::now_utc();
	let next_week = now + Duration::weeks(1);
	let revoked_cert = RevokedCertParams {
		serial_number: SerialNumber::from_slice(&[0x00, 0xC0, 0xFF, 0xEE]),
		revocation_time: now,
		reason_code: Some(RevocationReason::KeyCompromise),
		invalidity_date: None,
	};

	let crl = CertificateRevocationListParams {
		this_update: now,
		next_update: next_week,
		crl_number: SerialNumber::from(1234),
		issuing_distribution_point: Some(CrlIssuingDistributionPoint {
			distribution_point: CrlDistributionPoint {
				uris: vec!["http://example.com/crl".to_string()],
			},
			scope: Some(CrlScope::UserCertsOnly),
		}),
		revoked_certs: vec![revoked_cert],
		alg: &PKCS_ECDSA_P256_SHA256,
		key_identifier_method: KeyIdMethod::Sha256,
	};
	let crl = CertificateRevocationList::from_params(crl).unwrap();

	(crl, issuer)
}

#[allow(unused)] // Used by openssl + x509-parser features.
pub fn cert_with_crl_dps() -> Vec<u8> {
	let mut params = default_params();
	params.crl_distribution_points = vec![
		CrlDistributionPoint {
			uris: vec![
				"http://example.com/crl.der".to_string(),
				"http://crls.example.com/1234".to_string(),
			],
		},
		CrlDistributionPoint {
			uris: vec!["ldap://example.com/crl.der".to_string()],
		},
	];

	let cert = Certificate::from_params(params).unwrap();
	cert.serialize_der().unwrap()
}
