@@ -103,6 +103,66 @@ fn sub_ca_certificate() {
103
103
zlint:: check_certificate ( pem. as_bytes ( ) , ignored) ;
104
104
}
105
105
106
+ #[ test]
107
+ fn leaf_certificate ( ) {
108
+ let serial_number = SerialNumber :: from ( 42u32 ) ;
109
+ let validity = Validity :: from_now ( Duration :: new ( 5 , 0 ) ) . unwrap ( ) ;
110
+
111
+ let issuer = Name :: from_str ( "CN=World domination corporation,O=World domination Inc,C=US" )
112
+ . unwrap ( )
113
+ . to_der ( )
114
+ . unwrap ( ) ;
115
+ let issuer = Name :: from_der ( & issuer) . unwrap ( ) ;
116
+ let profile = Profile :: Leaf {
117
+ issuer,
118
+ enable_key_agreement : false ,
119
+ } ;
120
+
121
+ let subject = Name :: from_str ( "CN=service.domination.world" )
122
+ . unwrap ( )
123
+ . to_der ( )
124
+ . unwrap ( ) ;
125
+ let subject = Name :: from_der ( & subject) . unwrap ( ) ;
126
+ let pub_key =
127
+ SubjectPublicKeyInfoOwned :: try_from ( RSA_2048_DER_EXAMPLE ) . expect ( "get rsa pub key" ) ;
128
+
129
+ let mut signer = ecdsa_signer ( ) ;
130
+ let mut builder = CertificateBuilder :: new :: < ecdsa:: Signature < NistP256 > > (
131
+ profile,
132
+ Version :: V3 ,
133
+ serial_number,
134
+ validity,
135
+ subject,
136
+ pub_key,
137
+ & mut signer,
138
+ )
139
+ . expect ( "Create certificate" ) ;
140
+
141
+ let certificate = builder. build :: < ecdsa:: Signature < NistP256 > > ( ) . unwrap ( ) ;
142
+
143
+ let pem = certificate. to_pem ( LineEnding :: LF ) . expect ( "generate pem" ) ;
144
+ println ! ( "{}" , openssl:: check_certificate( pem. as_bytes( ) ) ) ;
145
+
146
+ // TODO(baloo): not too sure we should tackle those in this API.
147
+ let ignored = & [
148
+ "e_sub_cert_aia_missing" ,
149
+ "e_sub_cert_crl_distribution_points_missing" ,
150
+ "w_sub_cert_aia_does_not_contain_issuing_ca_url" ,
151
+ // Missing policies
152
+ "e_sub_cert_certificate_policies_missing" ,
153
+ "e_sub_cert_cert_policy_empty" ,
154
+ // Needs to be added by the end-user
155
+ "e_sub_cert_aia_does_not_contain_ocsp_url" ,
156
+ // SAN needs to include DNS name (if used)
157
+ "e_ext_san_missing" ,
158
+ "e_subject_common_name_not_exactly_from_san" ,
159
+ // Extended key usage needs to be added by end-user and is use-case dependent
160
+ "e_sub_cert_eku_missing" ,
161
+ ] ;
162
+
163
+ zlint:: check_certificate ( pem. as_bytes ( ) , ignored) ;
164
+ }
165
+
106
166
const RSA_2048_PRIV_DER_EXAMPLE : & [ u8 ] = include_bytes ! ( "examples/rsa2048-priv.der" ) ;
107
167
108
168
fn rsa_signer ( ) -> SigningKey < Sha256 > {
0 commit comments