/// Generate self-signed certificate command
/// It creates 2 files in the current directory:
Generate self-signed certificate command
It creates 2 files in the current directory.
Generate self - signed certificate command It creates 2 files in the current directory .
fn generate_signed_certificate() -> (X509, PKey, X509) {
	// Create CA certificate
	let ca_pkey = pkey();
	let mut ca_gen = X509::builder().unwrap();
	ca_gen.set_version(2).unwrap();
    let mut ca_name = X509Name::builder().unwrap();
    ca_name.append_entry_by_nid(nid::COMMONNAME, "My eidonkey CA").unwrap();
    let ca_name = ca_name.build();
    ca_gen.set_subject_name(&ca_name).unwrap();
    ca_gen.set_issuer_name(&ca_name).unwrap();
	ca_gen.set_not_before(&Asn1Time::days_from_now(0).unwrap());
    ca_gen.set_not_after(&Asn1Time::days_from_now(365*10).unwrap());
	let mut serial = BigNum::new().unwrap();
    serial.rand(128, MSB_MAYBE_ZERO, false).unwrap();
    ca_gen.set_serial_number(&serial.to_asn1_integer().unwrap()).unwrap();
    ca_gen.set_pubkey(&ca_pkey).unwrap();
    let basic_constraints = BasicConstraints::new().critical().ca().build().unwrap();
    ca_gen.append_extension(basic_constraints).unwrap();
    let ca_key_usage = KeyUsage::new().key_cert_sign().crl_sign().build().unwrap();
    ca_gen.append_extension(ca_key_usage).unwrap();
    let ca_subject_key_identifier = SubjectKeyIdentifier::new()
        .build(&ca_gen.x509v3_context(None, None))
        .unwrap();
    ca_gen.append_extension(ca_subject_key_identifier).unwrap();
    let ca_authority_key_identifier = AuthorityKeyIdentifier::new()
        .keyid(true)
        .build(&ca_gen.x509v3_context(None, None))
        .unwrap();
    let cert_authority_key_identifier = AuthorityKeyIdentifier::new()
        .keyid(true)
        .build(&ca_gen.x509v3_context(None, None))
        .unwrap();    ca_gen.append_extension(ca_authority_key_identifier).unwrap();
    ca_gen.sign(&ca_pkey, MessageDigest::sha256()).unwrap();
    let ca = ca_gen.build();
	let ca_path = SERVER_CA_CERTIFICATE_FILE;
	let mut file = File::create(ca_path).unwrap();
	assert!(file.write_all(&ca.to_pem().unwrap()).is_ok());
	// local host generator
	let cert_pkey = pkey();
	let mut cert_gen = X509::builder().unwrap();
	cert_gen.set_version(2).unwrap();
    let mut cert_name = X509Name::builder().unwrap();
    cert_name.append_entry_by_nid(nid::COMMONNAME, "localhost").unwrap();
    let cert_name = cert_name.build();
    cert_gen.set_subject_name(&cert_name).unwrap();
    cert_gen.set_issuer_name(&ca_name).unwrap();
	cert_gen.set_not_before(&Asn1Time::days_from_now(0).unwrap());
    cert_gen.set_not_after(&Asn1Time::days_from_now(365*10).unwrap());
    serial.rand(128, MSB_MAYBE_ZERO, false).unwrap();
    cert_gen.set_serial_number(&serial.to_asn1_integer().unwrap()).unwrap();
    cert_gen.set_pubkey(&cert_pkey).unwrap();
    let cert_key_usage = KeyUsage::new().digital_signature().key_encipherment().data_encipherment().build().unwrap();
    cert_gen.append_extension(cert_key_usage).unwrap();
    let cert_subject_key_identifier = SubjectKeyIdentifier::new()
        .build(&cert_gen.x509v3_context(None, None))
        .unwrap();
    cert_gen.append_extension(cert_subject_key_identifier).unwrap();
    cert_gen.append_extension(cert_authority_key_identifier).unwrap();
    let ext_key_usage = ExtendedKeyUsage::new().server_auth().build().unwrap();
    cert_gen.append_extension(ext_key_usage).unwrap();
    cert_gen.sign(&ca_pkey, MessageDigest::sha256()).unwrap();
    let cert = cert_gen.build();
	let cert_path = SERVER_CERTIFICATE_FILE;
	let mut file = File::create(cert_path).unwrap();
	assert!(file.write_all(&cert.to_pem().unwrap()).is_ok());
	register_eidonkey();
	(cert, cert_pkey, ca)
}
fn generate_signed_certificate ( ) -> ( X509 , PKey , X509 ) { // Create CA certificate let ca_pkey = pkey ( ) ; let mut ca_gen = X509 :: builder ( ) . unwrap ( ) ; ca_gen . set_version ( 2 ) . unwrap ( ) ; let mut ca_name = X509Name :: builder ( ) . unwrap ( ) ; ca_name . append_entry_by_nid ( nid :: COMMONNAME , " " ) . unwrap ( ) ; let ca_name = ca_name . build ( ) ; ca_gen . set_subject_name ( & ca_name ) . unwrap ( ) ; ca_gen . set_issuer_name ( & ca_name ) . unwrap ( ) ; ca_gen . set_not_before ( & Asn1Time :: days_from_now ( 0 ) . unwrap ( ) ) ; ca_gen . set_not_after ( & Asn1Time :: days_from_now ( 365 * 10 ) . unwrap ( ) ) ; let mut serial = BigNum :: new ( ) . unwrap ( ) ; serial . rand ( 128 , MSB_MAYBE_ZERO , false ) . unwrap ( ) ; ca_gen . set_serial_number ( & serial . to_asn1_integer ( ) . unwrap ( ) ) . unwrap ( ) ; ca_gen . set_pubkey ( & ca_pkey ) . unwrap ( ) ; let basic_constraints = BasicConstraints :: new ( ) . critical ( ) . ca ( ) . build ( ) . unwrap ( ) ; ca_gen . append_extension ( basic_constraints ) . unwrap ( ) ; let ca_key_usage = KeyUsage :: new ( ) . key_cert_sign ( ) . crl_sign ( ) . build ( ) . unwrap ( ) ; ca_gen . append_extension ( ca_key_usage ) . unwrap ( ) ; let ca_subject_key_identifier = SubjectKeyIdentifier :: new ( ) . build ( & ca_gen . x509v3_context ( None , None ) ) . unwrap ( ) ; ca_gen . append_extension ( ca_subject_key_identifier ) . unwrap ( ) ; let ca_authority_key_identifier = AuthorityKeyIdentifier :: new ( ) . keyid ( true ) . build ( & ca_gen . x509v3_context ( None , None ) ) . unwrap ( ) ; let cert_authority_key_identifier = AuthorityKeyIdentifier :: new ( ) . keyid ( true ) . build ( & ca_gen . x509v3_context ( None , None ) ) . unwrap ( ) ; ca_gen . append_extension ( ca_authority_key_identifier ) . unwrap ( ) ; ca_gen . sign ( & ca_pkey , MessageDigest :: sha256 ( ) ) . unwrap ( ) ; let ca = ca_gen . build ( ) ; let ca_path = SERVER_CA_CERTIFICATE_FILE ; let mut file = File :: create ( ca_path ) . unwrap ( ) ; assert ! ( file write_all ( ca to_pem ( ) unwrap ( ) ) is_ok ( ) ) ; // local host generator let cert_pkey = pkey ( ) ; let mut cert_gen = X509 :: builder ( ) . unwrap ( ) ; cert_gen . set_version ( 2 ) . unwrap ( ) ; let mut cert_name = X509Name :: builder ( ) . unwrap ( ) ; cert_name . append_entry_by_nid ( nid :: COMMONNAME , " " ) . unwrap ( ) ; let cert_name = cert_name . build ( ) ; cert_gen . set_subject_name ( & cert_name ) . unwrap ( ) ; cert_gen . set_issuer_name ( & ca_name ) . unwrap ( ) ; cert_gen . set_not_before ( & Asn1Time :: days_from_now ( 0 ) . unwrap ( ) ) ; cert_gen . set_not_after ( & Asn1Time :: days_from_now ( 365 * 10 ) . unwrap ( ) ) ; serial . rand ( 128 , MSB_MAYBE_ZERO , false ) . unwrap ( ) ; cert_gen . set_serial_number ( & serial . to_asn1_integer ( ) . unwrap ( ) ) . unwrap ( ) ; cert_gen . set_pubkey ( & cert_pkey ) . unwrap ( ) ; let cert_key_usage = KeyUsage :: new ( ) . digital_signature ( ) . key_encipherment ( ) . data_encipherment ( ) . build ( ) . unwrap ( ) ; cert_gen . append_extension ( cert_key_usage ) . unwrap ( ) ; let cert_subject_key_identifier = SubjectKeyIdentifier :: new ( ) . build ( & cert_gen . x509v3_context ( None , None ) ) . unwrap ( ) ; cert_gen . append_extension ( cert_subject_key_identifier ) . unwrap ( ) ; cert_gen . append_extension ( cert_authority_key_identifier ) . unwrap ( ) ; let ext_key_usage = ExtendedKeyUsage :: new ( ) . server_auth ( ) . build ( ) . unwrap ( ) ; cert_gen . append_extension ( ext_key_usage ) . unwrap ( ) ; cert_gen . sign ( & ca_pkey , MessageDigest :: sha256 ( ) ) . unwrap ( ) ; let cert = cert_gen . build ( ) ; let cert_path = SERVER_CERTIFICATE_FILE ; let mut file = File :: create ( cert_path ) . unwrap ( ) ; assert ! ( file write_all ( cert to_pem ( ) unwrap ( ) ) is_ok ( ) ) ; register_eidonkey ( ) ; ( cert , cert_pkey , ca ) }
Generate self-signed certificate command
It creates 2 files in the current directory:
Generate self - signed certificate command It creates 2 files in the current directory :