Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
Make tls certs binary compat with the go implementation (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Aug 2, 2021
1 parent 2b8f3ec commit f2a8602
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
19 changes: 5 additions & 14 deletions src/tls/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::LIBP2P_SIGNING_PREFIX_LENGTH;
use libp2p::identity::Keypair;

const LIBP2P_OID: &[u64] = &[1, 3, 6, 1, 4, 1, 53594, 1, 1]; // Based on libp2p TLS 1.3 specs
const LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH: usize = 65;
const LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH: usize = 91;
static LIBP2P_SIGNATURE_ALGORITHM: &rcgen::SignatureAlgorithm = &rcgen::PKCS_ECDSA_P256_SHA256;

/// Generates a self-signed TLS certificate that includes a libp2p-specific
Expand All @@ -38,7 +38,7 @@ pub(crate) fn make_cert(keypair: &Keypair) -> Result<rcgen::Certificate, super::
// 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();
let certif_pubkey = certif_keypair.public_key_der();
assert_eq!(
certif_pubkey.len(),
LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH,
Expand All @@ -47,28 +47,19 @@ pub(crate) fn make_cert(keypair: &Keypair) -> Result<rcgen::Certificate, super::
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);
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);
})
})
yasna::encode_der(&(serialized_pubkey, libp2p_ext_signature))
};

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

Expand Down
6 changes: 4 additions & 2 deletions src/tls/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn verify_presented_certs(presented_certs: &[Certificate]) -> Result<(), TLSErro
certificate
.check_self_issued()
.map_err(TLSError::WebPKIError)?;
verify_libp2p_signature(&extension, certificate.subject_public_key_info().key())
verify_libp2p_signature(&extension, certificate.subject_public_key_info().spki())
.map_err(TLSError::WebPKIError)
}

Expand All @@ -204,7 +204,9 @@ struct Libp2pExtension<'a> {

fn parse_libp2p_extension(extension: Input<'_>) -> Result<Libp2pExtension<'_>, Error> {
fn read_bit_string<'a>(input: &mut Reader<'a>, e: Error) -> Result<Input<'a>, Error> {
der::bit_string_with_no_unused_bits(input).map_err(|_| e)
// The specification states that this is a BIT STRING, but the Go implementation
// uses an OCTET STRING. OCTET STRING is superior in this context, so use it.
der::expect_tag_and_get_value(input, der::Tag::OctetString).map_err(|_| e)
}

let e = Error::ExtensionValueInvalid;
Expand Down

0 comments on commit f2a8602

Please sign in to comment.