Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tls-certificate): generate and parse libp2p tls certificate #1209

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

diegomrsantos
Copy link
Contributor

@diegomrsantos diegomrsantos commented Oct 3, 2024

This PR implements the certificate generation and parsing for https://github.com/libp2p/specs/blob/master/tls/tls.md.

const extValueSize = 256 # Buffer size for ASN.1 encoding
var
extValue: array[extValueSize, byte]
extPtr: ptr byte = addr extValue[extValueSize - 1] # Start at the end of the buffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I thought it was an error, I had to check mbedtls_asn1_write_octet_string to understand that this function works backward. Maybe you can add this as a comment somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


# Exception types for TLS certificate errors
type
TLSCertificateError* = object of Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is inside nim-libp2p, maybe inherit from LPError instead of exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

return extValueSeq

proc makeLibp2pExtension(
identityKeypair: KeyPair, certificateKeypair: mbedtls_pk_context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's my C background talking, but I would use a ptr mbedtls_pk_context here instead of mbedtls_pk_context. It could work in Nim (I'm not sure if it uses a copy, a reference or a pointer for this kind of call), but as mbedtls only use pointer for their context, I would change that for our own function aswell.

Copy link
Contributor Author

@diegomrsantos diegomrsantos Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of it? I don't think this is a pattern in Nim, is it?

Comment on lines +178 to +179
for i in 0 ..< P2P_SIGNING_PREFIX.len:
msg[i] = byte(P2P_SIGNING_PREFIX[i])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use copyMem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it's not possible to use it with a const

## - `ASN1EncodingError` if encoding fails.
# Initialize entropy and DRBG contexts
var
entropy = mbedtls_entropy_context()
Copy link
Contributor

@lchenut lchenut Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, entropy is a bit heavy to create. It might be interesting to find a way to only initialize it once and not every time you generate. I'm not 100% sure though, but this could be checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how would be the right way of initializing and freeing this only once in Nim?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried one approach.

Comment on lines +294 to +295
let notBefore = "19750101000000"
let notAfter = "40960101000000"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it from the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't mention this in the spec, I used the same values in the Rust implementation.

Comment on lines +411 to +413
for i in 0 ..< LIBP2P_EXT_OID_DER.len:
if ptrInc(oid.p, i.uint)[] != LIBP2P_EXT_OID_DER[i]:
return MBEDTLS_ERR_OID_NOT_FOUND # Extension not handled by this callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use nimCmpMem? Not sure if importing just for that is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it defined?

@diegomrsantos diegomrsantos changed the title generate and parse libp2p tls certificate feat(tls-certificate): generate and parse libp2p tls certificate Oct 10, 2024
@diegomrsantos diegomrsantos mentioned this pull request Oct 17, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Pipeline
Development

Successfully merging this pull request may close these issues.

2 participants