pub(crate) fn make_cert(
    keypair: &identity::Keypair,
) -> Result<rcgen::Certificate, super::ConfigError> {
    // Keypair used to sign the certificate.
    let certif_keypair = rcgen::KeyPair::generate(&LIBP2P_SIGNATURE_ALGORITHM)?;

    // The libp2p-specific extension to the certificate contains a signature of the public key
    // of the certificate using the libp2p private key.
    let libp2p_ext_signature = {
        let certif_pubkey = certif_keypair.public_key_raw();
        assert_eq!(
            certif_pubkey.len(),
            LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH,
            "ed25519 public keys are {} bytes",
            LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH
        );

        let mut buf =
            [0u8; LIBP2P_SIGNING_PREFIX_LENGTH + LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH];
        buf[..LIBP2P_SIGNING_PREFIX_LENGTH].copy_from_slice(&super::LIBP2P_SIGNING_PREFIX[..]);
        buf[LIBP2P_SIGNING_PREFIX_LENGTH..].copy_from_slice(certif_pubkey);
        keypair.sign(&buf)?
    };

    // Generate the libp2p-specific extension.
    let libp2p_extension: rcgen::CustomExtension = {
        let extension_content = {
            let serialized_pubkey = keypair.public().into_protobuf_encoding();
            yasna::construct_der(|writer| {
                writer.write_sequence(|writer| {
                    writer
                        .next()
                        .write_bitvec_bytes(&serialized_pubkey, serialized_pubkey.len() * 8);
                    writer
                        .next()
                        .write_bitvec_bytes(&libp2p_ext_signature, libp2p_ext_signature.len() * 8);
                })
            })
        };

        let mut ext = rcgen::CustomExtension::from_oid_content(LIBP2P_OID, extension_content);
        ext.set_criticality(true);
        ext
    };

    let certificate = {
        let mut params = rcgen::CertificateParams::new(vec![]);
        params.distinguished_name = rcgen::DistinguishedName::new();
        params.custom_extensions.push(libp2p_extension);
        params.alg = &LIBP2P_SIGNATURE_ALGORITHM;
        params.key_pair = Some(certif_keypair);
        rcgen::Certificate::from_params(params)?
    };

    Ok(certificate)
}
/// Generates a self-signed TLS certificate that includes a libp2p-specific
/// certificate extension containing the public key of the given keypair.
Generates a self-signed TLS certificate that includes a libp2p-specific
certificate extension containing the public key of the given keypair.
Generates a self - signed TLS certificate that includes a libp2p - specific certificate extension containing the public key of the given keypair .
pub(crate) fn make_cert(
    keypair: &identity::Keypair,
) -> Result<rcgen::Certificate, super::ConfigError> {
    // Keypair used to sign the certificate.
    let certif_keypair = rcgen::KeyPair::generate(&LIBP2P_SIGNATURE_ALGORITHM)?;
    // The libp2p-specific extension to the certificate contains a signature of the public key
    // of the certificate using the libp2p private key.
    let libp2p_ext_signature = {
        let certif_pubkey = certif_keypair.public_key_raw();
        assert_eq!(
            certif_pubkey.len(),
            LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH,
            "ed25519 public keys are {} bytes",
            LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH
        );
        let mut buf =
            [0u8; LIBP2P_SIGNING_PREFIX_LENGTH + LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH];
        buf[..LIBP2P_SIGNING_PREFIX_LENGTH].copy_from_slice(&super::LIBP2P_SIGNING_PREFIX[..]);
        buf[LIBP2P_SIGNING_PREFIX_LENGTH..].copy_from_slice(certif_pubkey);
        keypair.sign(&buf)?
    };
    // Generate the libp2p-specific extension.
    let libp2p_extension: rcgen::CustomExtension = {
        let extension_content = {
            let serialized_pubkey = keypair.public().into_protobuf_encoding();
            yasna::construct_der(|writer| {
                writer.write_sequence(|writer| {
                    writer
                        .next()
                        .write_bitvec_bytes(&serialized_pubkey, serialized_pubkey.len() * 8);
                    writer
                        .next()
                        .write_bitvec_bytes(&libp2p_ext_signature, libp2p_ext_signature.len() * 8);
                })
            })
        };
        let mut ext = rcgen::CustomExtension::from_oid_content(LIBP2P_OID, extension_content);
        ext.set_criticality(true);
        ext
    };
    let certificate = {
        let mut params = rcgen::CertificateParams::new(vec![]);
        params.distinguished_name = rcgen::DistinguishedName::new();
        params.custom_extensions.push(libp2p_extension);
        params.alg = &LIBP2P_SIGNATURE_ALGORITHM;
        params.key_pair = Some(certif_keypair);
        rcgen::Certificate::from_params(params)?
    };
    Ok(certificate)
}
pub ( crate ) fn make_cert ( keypair : & identity :: Keypair , ) -> Result < rcgen :: Certificate , super :: ConfigError > { // Keypair used to sign the certificate. let certif_keypair = rcgen :: KeyPair :: generate ( & LIBP2P_SIGNATURE_ALGORITHM ) ? ; // The libp2p-specific extension to the certificate contains a signature of the public key // of the certificate using the libp2p private key. let libp2p_ext_signature = { let certif_pubkey = certif_keypair . public_key_raw ( ) ; assert_eq ! ( certif_pubkey len ( ) LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH " " LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH ) ; let mut buf = [ 0u8 ; LIBP2P_SIGNING_PREFIX_LENGTH + LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH ] ; buf [ .. LIBP2P_SIGNING_PREFIX_LENGTH ] . copy_from_slice ( & super :: LIBP2P_SIGNING_PREFIX [ .. ] ) ; buf [ LIBP2P_SIGNING_PREFIX_LENGTH .. ] . copy_from_slice ( certif_pubkey ) ; keypair . sign ( & buf ) ? } ; // Generate the libp2p-specific extension. let libp2p_extension : rcgen :: CustomExtension = { let extension_content = { let serialized_pubkey = keypair . public ( ) . into_protobuf_encoding ( ) ; yasna :: construct_der ( | writer | { writer . write_sequence ( | writer | { writer . next ( ) . write_bitvec_bytes ( & serialized_pubkey , serialized_pubkey . len ( ) * 8 ) ; writer . next ( ) . write_bitvec_bytes ( & libp2p_ext_signature , libp2p_ext_signature . len ( ) * 8 ) ; } ) } ) } ; let mut ext = rcgen :: CustomExtension :: from_oid_content ( LIBP2P_OID , extension_content ) ; ext . set_criticality ( true ) ; ext } ; let certificate = { let mut params = rcgen :: CertificateParams :: new ( vec ! [ ] ) ; params . distinguished_name = rcgen :: DistinguishedName :: new ( ) ; params . custom_extensions . push ( libp2p_extension ) ; params . alg = & LIBP2P_SIGNATURE_ALGORITHM ; params . key_pair = Some ( certif_keypair ) ; rcgen :: Certificate :: from_params ( params ) ? } ; Ok ( certificate ) }
Generates a self-signed TLS certificate that includes a libp2p-specific
certificate extension containing the public key of the given keypair.
Generates a self - signed TLS certificate that includes a libp2p - specific certificate extension containing the public key of the given keypair .
keypair
&identity::Keypair
keypair
&identity::Keypair