| 351 | |
| 352 | impl<Key> CertRequest<'_, Key> { |
| 353 | fn into_cert_params(self) -> Result<CertificateParams> { |
| 354 | let mut params = CertificateParams::new(self.alt_names.unwrap_or_default().to_vec())?; |
| 355 | let mut dn = DistinguishedName::new(); |
| 356 | if let Some(org_name) = self.org_name { |
| 357 | dn.push(DnType::OrganizationName, org_name); |
| 358 | } |
| 359 | dn.push(DnType::CommonName, self.subject); |
| 360 | params.distinguished_name = dn; |
| 361 | params.key_usages = vec![KeyUsagePurpose::DigitalSignature]; |
| 362 | if self.usage_server_auth { |
| 363 | params |
| 364 | .extended_key_usages |
| 365 | .push(ExtendedKeyUsagePurpose::ServerAuth); |
| 366 | } |
| 367 | if self.usage_client_auth { |
| 368 | params |
| 369 | .extended_key_usages |
| 370 | .push(ExtendedKeyUsagePurpose::ClientAuth); |
| 371 | } |
| 372 | if let Some(app_id) = self.app_id { |
| 373 | add_ext(&mut params, PHALA_RATLS_APP_ID, app_id); |
| 374 | } |
| 375 | if let Some(app_info) = self.app_info { |
| 376 | let app_info_bytes = |
| 377 | rmp_serde::to_vec(&app_info).context("Failed to serialize app info")?; |
| 378 | add_ext(&mut params, PHALA_RATLS_APP_INFO, app_info_bytes); |
| 379 | } |
| 380 | if let Some(usage) = self.special_usage { |
| 381 | add_ext(&mut params, PHALA_RATLS_CERT_USAGE, usage); |
| 382 | } |
| 383 | if let Some(ver_att) = self.attestation { |
| 384 | let attestation_bytes = ver_att.clone().into_stripped().to_bytes()?; |
| 385 | add_ext(&mut params, PHALA_RATLS_ATTESTATION, &attestation_bytes); |
| 386 | } |
| 387 | if let Some(ca_level) = self.ca_level { |
| 388 | params.is_ca = IsCa::Ca(BasicConstraints::Constrained(ca_level)); |
| 389 | params.key_usages.push(KeyUsagePurpose::KeyCertSign); |
| 390 | params.key_usages.push(KeyUsagePurpose::CrlSign); |
| 391 | } |
| 392 | if let Some(not_before) = self.not_before { |
| 393 | params.not_before = not_before.into(); |
| 394 | } |
| 395 | params.not_after = self |
| 396 | .not_after |
| 397 | .unwrap_or_else(|| { |
| 398 | let now = SystemTime::now(); |
| 399 | let day = Duration::from_secs(86400); |
| 400 | now + day * 365 * 10 |
| 401 | }) |
| 402 | .into(); |
| 403 | Ok(params) |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | fn add_ext(params: &mut CertificateParams, oid: &[u64], content: impl AsRef<[u8]>) { |