# Week10 Update ## TL;DR 1. Implement the [libp2p peer auth](https://github.com/libp2p/specs/blob/master/tls/tls.md#peer-authentication) in the zig-libp2p, opened the [PR](https://github.com/zen-eth/zig-libp2p/pull/67). 2. Implement the [libp2p pubsub streams management](https://github.com/libp2p/specs/tree/e5af9174d2e07fc2be6ee720d215c1193bb21cc1/pubsub#stream-management) and [pubsub peers membership](https://github.com/libp2p/specs/blob/e5af9174d2e07fc2be6ee720d215c1193bb21cc1/pubsub/gossipsub/gossipsub-v1.0.md#peering-state) in the zig-libp2p, opened the [PR](https://github.com/zen-eth/zig-libp2p/pull/68). ## Peer Authentication In order to be able to use arbitrary key types, peers don’t use their host key to sign the X.509 certificate they send during the handshake. Instead, the host key is encoded into the libp2p Public Key Extension, which is carried in a self-signed certificate. The key used to generate and sign this certificate SHOULD NOT be related to the host's key. Endpoints MAY generate a new key and certificate for every connection attempt, or they MAY reuse the same key and certificate for multiple connections. Endpoints MUST choose a key that will allow the peer to verify the certificate (i.e. choose a signature algorithm that the peer supports), and SHOULD use a key type that (a) allows for efficient signature computation, and (b) reduces the combined size of the certificate and the signature. In particular, RSA SHOULD NOT be used unless no elliptic curve algorithms are supported. Endpoints MUST NOT send a certificate chain that contains more than one certificate. The certificate MUST have `NotBefore` and `NotAfter` fields set such that the certificate is valid at the time it is received by the peer. When receiving the certificate chain, an endpoint MUST check these conditions and abort the connection attempt if (a) the presented certificate is not yet valid, OR (b) if it is expired. Endpoints MUST abort the connection attempt if more than one certificate is received, or if the certificate’s self-signature is not valid. The certificate MUST contain the libp2p Public Key Extension. If this extension is missing, endpoints MUST abort the connection attempt. This extension MAY be marked critical. The certificate MAY contain other extensions. Implementations MUST ignore non-critical extensions with unknown OIDs. Endpoints MUST abort the connection attempt if the certificate contains critical extensions that the endpoint does not understand. Certificates MUST omit the deprecated `subjectUniqueId` and `issuerUniqueId` fields. Endpoints MAY abort the connection attempt if either is present. Note for clients: Since clients complete the TLS handshake immediately after sending the certificate (and the TLS `ClientFinished` message), the handshake will appear as having succeeded before the server had the chance to verify the certificate. In this state, the client can already send application data. If certificate verification fails on the server side, the server will close the connection without processing any data that the client sent. ### libp2p Public Key Extension In order to prove ownership of its host key, an endpoint sends two values: - the public host key - a signature performed using the private host key The public host key allows the peer to calculate the peer ID of the peer it is connecting to. Clients MUST verify that the peer ID derived from the certificate matches the peer ID they intended to connect to, and MUST abort the connection if there is a mismatch. The peer signs the concatenation of the string `libp2p-tls-handshake:` and the encoded public key that is used to generate the certificate carrying the libp2p Public Key Extension, using its private host key. The public key is encoded as a `SubjectPublicKeyInfo` structure as described in RFC 5280, Section 4.1: ```asn1 SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subject_public_key BIT STRING } AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL } ``` This signature provides cryptographic proof that the peer was in possession of the private host key at the time the certificate was signed. Peers MUST verify the signature, and abort the connection attempt if signature verification fails. The public host key and the signature are ANS.1-encoded into the SignedKey data structure, which is carried in the libp2p Public Key Extension. The libp2p Public Key Extension is a X.509 extension with the Object Identier `1.3.6.1.4.1.53594.1.1`, [allocated by IANA to the libp2p project at Protocol Labs](https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers). ```asn1 SignedKey ::= SEQUENCE { publicKey OCTET STRING, signature OCTET STRING } ``` The publicKey field of `SignedKey` contains the public host key of the endpoint, encoded using the following protobuf: ```protobuf syntax = "proto2"; enum KeyType { RSA = 0; Ed25519 = 1; Secp256k1 = 2; ECDSA = 3; } message PublicKey { required KeyType Type = 1; required bytes Data = 2; } ``` How the public key is encoded into the `Data` bytes depends on the Key Type. - `Ed25519`: Only the 32 bytes of the public key - `Secp256k1`: Only the compressed form of the public key. 33 bytes. - The rest of the keys are encoded as a [SubjectPublicKeyInfo structure](https://www.rfc-editor.org/rfc/rfc5280.html#section-4.1) in PKIX, ASN.1 DER form. ## Stream Management Data should be exchanged between peers using two separately negotiated streams, one inbound, one outbound. These streams are treated as unidirectional streams. The outbound stream is used only to write data. The inbound stream is used only to read data. ## Next Start to test gossipsub stream managenment and membership in zig-libp2p.