1- use crate :: { sgx_extensions:: sgx_extensions_to_bytes, utils:: gen_key} ;
1+ use crate :: {
2+ sgx_extensions:: { sgx_extensions_to_bytes, SgxExtensionsBuilder } ,
3+ utils:: gen_key,
4+ } ;
25use anyhow:: bail;
3- use dcap_types:: cert:: SgxExtensions ;
6+ use dcap_types:: cert:: {
7+ SgxExtensions , SGX_PCK_CERT_CN , SGX_PCK_PLATFORM_CA_CN , SGX_PCK_PROCESSOR_CA_CN ,
8+ } ;
49use openssl:: {
510 asn1:: { Asn1Integer , Asn1Object , Asn1OctetString , Asn1Time } ,
611 bn:: BigNum ,
@@ -19,6 +24,17 @@ pub struct RootCa {
1924 pub crl : X509Crl ,
2025}
2126
27+ impl RootCa {
28+ pub fn with_new_crl ( & self , revoked_certs : Vec < X509 > ) -> Result < RootCa , anyhow:: Error > {
29+ let crl = gen_crl ( & self . cert , & self . key , revoked_certs, None ) ?;
30+ Ok ( RootCa {
31+ cert : self . cert . clone ( ) ,
32+ key : self . key . clone ( ) ,
33+ crl,
34+ } )
35+ }
36+ }
37+
2238pub fn gen_sgx_intel_root_ca (
2339 root_pkey : & PKey < Private > ,
2440 validity : Validity ,
@@ -75,7 +91,7 @@ pub fn gen_root_ca(
7591 & root_key,
7692 root_cert_validity. unwrap_or_else ( Validity :: long_duration) ,
7793 ) ?;
78- let crl = gen_crl ( & root_cert, & root_key, & [ ] , crl_validity) ?;
94+ let crl = gen_crl ( & root_cert, & root_key, vec ! [ ] , crl_validity) ?;
7995 Ok ( RootCa {
8096 cert : root_cert,
8197 key : root_key,
@@ -86,7 +102,7 @@ pub fn gen_root_ca(
86102pub fn gen_crl (
87103 issuer_cert : & X509Ref ,
88104 issuer_pkey : & PKeyRef < Private > ,
89- revoked_certs : & [ X509 ] ,
105+ revoked_certs : Vec < X509 > ,
90106 crl_validity : Option < Validity > ,
91107) -> Result < X509Crl , anyhow:: Error > {
92108 let mut crl = X509Crl :: new ( issuer_cert, None ) ?;
@@ -95,7 +111,7 @@ pub fn gen_crl(
95111 crl. set_next_update ( & validity. not_after ( ) ) ?;
96112 crl. increment_crl_number ( ) ?;
97113 for cert in revoked_certs {
98- crl. revoke ( cert) ?;
114+ crl. revoke ( & cert) ?;
99115 }
100116 crl. sign ( issuer_pkey, MessageDigest :: sha256 ( ) ) ?;
101117 Ok ( crl)
@@ -104,7 +120,7 @@ pub fn gen_crl(
104120pub fn gen_crl_der (
105121 issuer_cert : & X509Ref ,
106122 issuer_pkey : & PKeyRef < Private > ,
107- revoked_certs : & [ X509 ] ,
123+ revoked_certs : Vec < X509 > ,
108124 crl_validity : Option < Validity > ,
109125) -> Result < Vec < u8 > , anyhow:: Error > {
110126 Ok ( gen_crl ( issuer_cert, issuer_pkey, revoked_certs, crl_validity) ?. to_der ( ) ?)
@@ -177,28 +193,33 @@ pub fn gen_tcb_certchain(
177193 } )
178194}
179195
180- #[ derive( Debug , Clone , Copy ) ]
196+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
181197pub enum PckCa {
182198 Processor ,
183199 Platform ,
184200}
185201
186202impl PckCa {
203+ /// Create a PckCa from the CN of the certificate
187204 pub fn from_cn ( cn : & str ) -> Result < Self , anyhow:: Error > {
188- match cn {
189- "Intel SGX PCK Processor CA" => Ok ( PckCa :: Processor ) ,
190- "Intel SGX PCK Platform CA" => Ok ( PckCa :: Platform ) ,
191- _ => bail ! ( "Invalid PCK CA CN: {}" , cn) ,
205+ if cn == SGX_PCK_PROCESSOR_CA_CN {
206+ Ok ( PckCa :: Processor )
207+ } else if cn == SGX_PCK_PLATFORM_CA_CN {
208+ Ok ( PckCa :: Platform )
209+ } else {
210+ bail ! ( "Invalid PCK CA CN: {}" , cn)
192211 }
193212 }
194213
214+ /// Get the CN of the PckCa
195215 pub fn cn ( & self ) -> & ' static str {
196216 match self {
197- PckCa :: Processor => "Intel SGX PCK Processor CA" ,
198- PckCa :: Platform => "Intel SGX PCK Platform CA" ,
217+ PckCa :: Processor => SGX_PCK_PROCESSOR_CA_CN ,
218+ PckCa :: Platform => SGX_PCK_PLATFORM_CA_CN ,
199219 }
200220 }
201221
222+ /// Get the type of the PckCa
202223 pub fn ca_type ( & self ) -> & ' static str {
203224 match self {
204225 PckCa :: Processor => "processor" ,
@@ -277,7 +298,7 @@ pub fn gen_pck_cert(
277298 Asn1Integer :: from_bn ( BigNum :: from_slice ( calc_skid ( pck_cert_pkey) . as_slice ( ) ) ?. as_ref ( ) ) ?
278299 . as_ref ( ) ,
279300 ) ?;
280- builder. set_subject_name ( build_x509_name ( "Intel SGX PCK Certificate" ) ?. as_ref ( ) ) ?;
301+ builder. set_subject_name ( build_x509_name ( SGX_PCK_CERT_CN ) ?. as_ref ( ) ) ?;
281302 builder. set_pubkey ( pck_cert_pkey) ?;
282303
283304 builder. set_not_before ( & validity. not_before ( ) ) ?;
@@ -326,6 +347,40 @@ pub struct PckCertchain {
326347 pub pck_cert_crl : X509Crl ,
327348}
328349
350+ impl PckCertchain {
351+ /// Generate a new PCK certificate and CRL
352+ /// If `revoked` is true, the `self.pck_cert`` will be revoked in the CRL
353+ pub fn gen_new_pck_cert ( & self , revoked : bool ) -> PckCertchain {
354+ let pck_cert_key = gen_key ( ) ;
355+ let pck_cert = gen_pck_cert (
356+ & self . pck_cert_ca ,
357+ & self . pck_cert_ca_key ,
358+ & pck_cert_key,
359+ & SgxExtensionsBuilder :: new ( ) . build ( ) ,
360+ Validity :: new_with_duration ( 1 , 60 * 60 * 24 * 365 ) ,
361+ )
362+ . unwrap ( ) ;
363+ let pck_cert_crl = gen_crl (
364+ & self . pck_cert_ca ,
365+ & self . pck_cert_ca_key ,
366+ if revoked {
367+ vec ! [ self . pck_cert. clone( ) ]
368+ } else {
369+ vec ! [ ]
370+ } ,
371+ None ,
372+ )
373+ . unwrap ( ) ;
374+ PckCertchain {
375+ pck_cert_ca : self . pck_cert_ca . clone ( ) ,
376+ pck_cert_ca_key : self . pck_cert_ca_key . clone ( ) ,
377+ pck_cert,
378+ pck_cert_key,
379+ pck_cert_crl,
380+ }
381+ }
382+ }
383+
329384/// Generate Intel SGX Root CA and PCK Processor/Platform CA and PCK certificates and private keys and CRLs for testing
330385pub fn gen_pck_certchain (
331386 root_ca : & RootCa ,
@@ -354,7 +409,7 @@ pub fn gen_pck_certchain(
354409 let pck_cert_crl = gen_crl (
355410 & pck_cert_ca,
356411 & pck_cert_ca_key,
357- & [ ] ,
412+ vec ! [ ] ,
358413 pck_cert_ca_crl_validity,
359414 ) ?;
360415 Ok ( PckCertchain {
0 commit comments