IPFS ping protocol

I extracted the 294 bytes (those which start with 30 82 01 22 30). To recap, the bytes are found inside the secio handshake. The handshake is protobuf serialized Propose with 5 fields, of which the field pubkey ID=2 is again protobuf serialized PublicKey with 2 fields, of which the field with again ID=2 contains the public key in DER format.


   Response -> {nonce,
    	    	pubkey -> PublicKey {KeyType, 

Once you save these bytes to for example publickey.der, then you can run openssl to check if you exctracted correctly the public key data.

You do it by invoking the following command to CLI:

openssl rsa -text -inform DER -in publickey.der -pubin

For example, I’m giving what I got (you’ll get of course something else):

Public-Key: (2048 bit)
Exponent: 65537 (0x10001)
writing RSA key
-----END PUBLIC KEY-----

From what I understood insofar, you have to answer in the same way, but in order to do that you have to generate first a new public/private key (and nonce, 16 random bytes).

To generate the pair we can use openssl.

We generate first the private key.
openssl genrsa -out client-pk.pem

From the private key we can generate the public key in DER format.
openssl rsa -in clien-pk.pem -pubout -outform DER > client-pubk.der

When you answer you get a response in a similar way than before.
Again first 4 bytes are the length of the message, followed by protobuf serialized data Response.

This time we have just 2 fields, both of type 2 (string, bytes[]).

The first field (ID=1) is the so called ephemeral public key, which is the public key of a key pair, that you generate each time you form a new connection. You do this so that even if someone breaks the key, it broke only the session, not all the communication before, but you do use the same key for establishing who you are (peerID).

This key is supposed to be in P-256 eliptical form, but I’m not sure what that is. I suspect it’s secp256k1 or prime256v1. I think NIST named P-256 the later.

For some reason, the field is 65 bytes long and the first byte is always 0x04, the rest being the actual key bytes.

If I generate new eliptical keys using openssl:

openssl ecparam -name secp256k1 -genkey -noout -out ec-key.pem
openssl ec -in ec-key.pem -pubout -outform der -out ec-pub.der

I get the public key file ec-pub.der with 88 bytes (91 bytes with prime256v1).

Each time I generate new keys, the first 24 bytes (27 bytes with prime256v1) are always the same and the last 64 bytes are different. The byte before the bytes of the key is always 0x04, so it might be that it’s just a badly stripped header byte or it indicates the type of key. No idea.

The second field (ID=2) in the protobuf data is the signature. It’s 256 bytes long. It’s a SHA-256 RSA digest of the corpus in secio lingo.

The corpus is composed of 3 strings concatenated together:

  • Propose protobuf serialized data of the server (i.e. what we’ve received before Response, without the first 4 bytes indicating length)
  • Propose protobuf serialized data of the client (i.e. what we’ve sent back, without the first 4 bytes indicating length)
  • Ephemeral public key (i.e. the 65 bytes we just received)

We can check if the signature coresponds to the corpus if we save the corpus as described to a file e.g. corpus.dat, the signature to e.g. sig.dat and the public key of the server (which we received in the previous message, i.e. Propose) to server-pubkey.der and then running the following:

openssl dgst -sha256 -verify server-pubkey.der -keyform der -signature sig.dat corpus.dat

It should return Verified OK.

You respond to this the same way, just that in the corpus you reverse the order of client/server and use your own ephemeral public key, i.e. you concatenate your data Response, their data Response, your ephemeral public key, which you have to generate. I think you generate it as I described before, i.e. ec-pub.der, and you just take the last 65 bytes. You should use prime256v1, otherwise it doesn’t work.

We save this contatenation to a file e.g. `corpus2.dat.

Now we sign this using our private key.

openssl dgst -sha256 -sign client-private-key.pem -out signature.dat corpus2.dat'

The signature is in the file signature.dat, which will be included now in our protobuf response.

We protobuf serialize the data, so that ID=1 contains the 65 bytes of the generated ephemeral public key and ID=2 contains 256 bytes of the contents of signature.dat. Both fields are of type 2.

Now we send this back, together with 4 bytes at the begining indicating length of message.

We get the response, again with 4 leading bytes with the lenght anda 48 bytes response, which is everytime different.

It is supposed to be the received nonce, but now encrypted (16 bytes) and the SHA256 HMAC of the encrypted nonce (32 bytes).

Unforunately I got kinda stuck here, how to proceed. How it’s even encrypted, how is the HMAC generated, i.e. what does it use for the shared secret.