intel_dcap_api/client/
registration.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 Matter Labs
3
4//! Registration
5
6use super::ApiClient; // Import from parent module
7use crate::{
8    error::{check_status, IntelApiError},
9    responses::AddPackageResponse,
10};
11use reqwest::{header, StatusCode};
12use std::num::ParseIntError;
13
14impl ApiClient {
15    /// POST /sgx/registration/v1/platform
16    /// Registers a multi-package SGX platform with the Intel Trusted Services API.
17    ///
18    /// # Arguments
19    ///
20    /// * `platform_manifest` - Binary data representing the platform manifest.
21    ///
22    /// # Returns
23    ///
24    /// Request body is binary Platform Manifest
25    /// Returns the hex-encoded PPID as a `String` upon success.
26    ///
27    /// # Errors
28    ///
29    /// Returns an `IntelApiError` if the request fails or if the response status
30    /// is not HTTP `201 CREATED`.
31    pub async fn register_platform(
32        &self,
33        platform_manifest: Vec<u8>,
34    ) -> Result<String, IntelApiError> {
35        // Registration paths are fixed, use the helper with "registration" service
36        let path = self.build_api_path("sgx", "registration", "platform")?;
37        let url = self.base_url.join(&path)?;
38
39        let request_builder = self
40            .client
41            .post(url)
42            .header(header::CONTENT_TYPE, "application/octet-stream")
43            .body(platform_manifest);
44
45        let response = self.execute_with_retry(request_builder).await?;
46
47        let response = check_status(response, &[StatusCode::CREATED]).await?;
48
49        // Response body is hex-encoded PPID
50        let ppid_hex = response.text().await?;
51        Ok(ppid_hex)
52    }
53
54    /// POST /sgx/registration/v1/package
55    /// Adds new package(s) to an already registered SGX platform instance.
56    ///
57    /// # Arguments
58    ///
59    /// * `add_package_request` - Binary data for the "Add Package" request body.
60    /// * `subscription_key` - The subscription key required by the Intel API.
61    ///
62    /// # Returns
63    ///
64    /// A [`AddPackageResponse`] containing the Platform Membership Certificates and
65    /// the count of them extracted from the response header.
66    ///
67    /// # Errors
68    ///
69    /// Returns an `IntelApiError` if the request fails, if the subscription key is invalid,
70    /// or if the response status is not HTTP `200 OK`.
71    pub async fn add_package(
72        &self,
73        add_package_request: Vec<u8>,
74        subscription_key: &str,
75    ) -> Result<AddPackageResponse, IntelApiError> {
76        if subscription_key.is_empty() {
77            return Err(IntelApiError::InvalidSubscriptionKey);
78        }
79
80        // Registration paths are fixed
81        let path = self.build_api_path("sgx", "registration", "package")?;
82        let url = self.base_url.join(&path)?;
83
84        let request_builder = self
85            .client
86            .post(url)
87            .header("Ocp-Apim-Subscription-Key", subscription_key)
88            .header(header::CONTENT_TYPE, "application/octet-stream")
89            .body(add_package_request);
90
91        let response = self.execute_with_retry(request_builder).await?;
92
93        let response = check_status(response, &[StatusCode::OK]).await?;
94
95        // Use the generic header helper, assuming header name is stable across reg versions
96        let cert_count_str = self.get_required_header(&response, "Certificate-Count", None)?;
97        let pck_cert_count: usize = cert_count_str.parse().map_err(|e: ParseIntError| {
98            IntelApiError::HeaderValueParse("Certificate-Count", e.to_string())
99        })?;
100
101        // Response body is a binary array of certificates
102        let pck_certs = response.bytes().await?.to_vec();
103        Ok(AddPackageResponse {
104            pck_certs,
105            pck_cert_count,
106        })
107    }
108}