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

tls: set selfsigned Elliptic Curve (EC) function #17

Merged
merged 1 commit into from
Nov 9, 2020

Conversation

cHuberCoffee
Copy link
Contributor

A new function to generate a self signed cert using elliptic curve cryptographic primitives.
The used elliptic curve is hardcoded with NID_secp521r1.
One can use this function in the barsip dtls_srtp module.

X509_free(cert);


return err = 0;
Copy link
Member

Choose a reason for hiding this comment

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

This return assignment looks a bit strange should it return err;?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes u are right. should be a normal return err;


if (!X509_set_pubkey(cert, key))
goto out;

if (!X509_sign(cert, key, EVP_sha1()))
if (!X509_sign(cert, key, EVP_sha256()))
Copy link
Contributor

Choose a reason for hiding this comment

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

is this change done on purpose ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not on purpose for this PR.
but on purpose in general. Since the SHA1 algo. is broken. This effects the cert. signatures with SHA1 as well. Using SHA2 alogs. is a small but effective change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree to the change, but I think it should be a separate PR.
also very important to test that it works.

@alfredh
Copy link
Contributor

alfredh commented Aug 28, 2020

have you done any interop testing with WebRTC browsers ?
(i.e. using DTLS-SRTP with EC certificate).

@cHuberCoffee
Copy link
Contributor Author

I'm working on a setup baresip-asterisk-sipML5 to test the dtls-srtp with the EC certificates.
It's a good idea in general to work through such a testsetup, to get a deeper understanding of the underlying structures.

I will give you more information of my results asap.

@cHuberCoffee
Copy link
Contributor Author

The setup is now baresip<->asterisk<->sipML5. The communication happens between these points. The key exchange via DTLSv1.2 is working with the cert. signed via ECDSA-SHA256 version. The media stream is established and audio data is transmitted.

@alfredh
Copy link
Contributor

alfredh commented Sep 10, 2020

did you try to call in both directions ? the DTLS server is normally the caller.

also please test with Chrome and Firefox.

@alfredh
Copy link
Contributor

alfredh commented Sep 29, 2020

I have tested this patch with Chrome and CtxSip. The calling direction is from baresip to CtxSip.

The DTLS handshake fails:

dtls_srtp: incoming DTLS connect from 10.0.1.10:51789 
tls: 140736434426816:error:1417A0C1:SSL routines:tls_post_process_client_hello:no shared cipher:ssl/statem/statem_srvr.c:2284:
dtls: accept error: 1
dtls_srtp: dtls_accept failed (Protocol error)

@alfredh
Copy link
Contributor

alfredh commented Sep 29, 2020

at Wire I implemented this function to generate ECDSA cert:

https://github.com/wireapp/wire-audio-video-signaling/blob/master/src/cert/cert.c#L37

perhaps this missing part will make it work:

	EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);

@cHuberCoffee cHuberCoffee marked this pull request as draft October 7, 2020 08:00
@cHuberCoffee
Copy link
Contributor Author

The test-setup is running with the original RSA method. I tested RSA and EC signed certificates against CtxSip with Firefox and Chrome. Both (RSA and EC) worked for both browsers. Call direction was barsip->CtxSip.

@cHuberCoffee cHuberCoffee marked this pull request as ready for review October 14, 2020 09:29
@alfredh
Copy link
Contributor

alfredh commented Oct 21, 2020

I tested the original code (no patches) between Safari and baresip-webrtc:

dtls_srtp: media=audio -- start DTLS client
dtls_srtp: component start: RTP [raddr=192.168.88.36:53019]
dtls_srtp: 'audio,RTP' dtls connect to 192.168.88.36:53019
    >> dtls cipher: ECDHE-ECDSA-CHACHA20-POLY1305
dtls_srtp: verified sha-256 fingerprint OK
dtls_srtp: ---> DTLS-SRTP complete (video/RTP) Profile=AES_CM_128_HMAC_SHA1_80
rtcsession: mediaenc event 'Secure' (video,RTP)

please note that in most cases, the DTLS Server is normally the same as the caller.

the negotiated cipher depends heavily on the calling direction.

try to add this in your code:

	info("    >> dtls cipher: %s\n", tls_cipher_name(comp->tls_conn));

and try to call in both directions, and then check if the cipher is different.

@alfredh
Copy link
Contributor

alfredh commented Oct 21, 2020

additional test with ctxSip, calling both directions:

chrome -> baresip:
.... dtls cipher: ECDHE-ECDSA-CHACHA20-POLY1305

baresip -> chrome:
.... dtls cipher: ECDHE-RSA-AES128-GCM-SHA256

@alfredh
Copy link
Contributor

alfredh commented Oct 21, 2020

with your patch and _ec function used in baresip dtls_srtp module, DTLS handshake fails:

dtls_srtp: incoming DTLS connect from 10.0.1.10:55045
tls: 140736434426816:error:1417A0C1:SSL routines:tls_post_process_client_hello:no shared cipher:ssl/statem/statem_srvr.c:2284:
dtls: accept error: 1
dtls_srtp: dtls_accept failed (Protocol error)
stream: update 'audio'
stream: audio: starting mediaenc 'dtls_srtp' (wait_secure=1)
sip:[email protected]: session closed: Connection reset by peer
sip:[email protected]: Call with sip:[email protected] terminated (duration: 9 secs)

--- System info: ---
 Machine:  x86_64/darwin
 Version:  1.0.0 (libre v1.1.0)
 Build:    64-bit little endian
 Kernel:   Darwin Johns-MacBook-Pro.local 16.7.0 Darwin Kernel Version 16.7.0: Wed Apr 24 20:50:53 PDT 2019; root:xnu-3789.73.49~1/RELEASE_X86_64 x86_64
 Uptime:   1 min 48 secs
 Started:  Wed Oct 21 17:38:21 2020
 Compiler: 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)
 OpenSSL:  OpenSSL 1.1.1h  22 Sep 2020

calling direction is from baresip to chrome/ctxsip.

int r, err = ENOMEM;

if (!tls || !cn)
return EINVAL;

rsa = RSA_new();
if (!rsa)
eckey = EC_KEY_new_by_curve_name(NID_secp521r1);
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps the curve name can be added as a function argument.
then the application can choose which one to use.

@alfredh
Copy link
Contributor

alfredh commented Oct 21, 2020

perhaps this curve will make it work: "prime256v1"

@cHuberCoffee cHuberCoffee marked this pull request as draft October 22, 2020 07:10
@alfredh
Copy link
Contributor

alfredh commented Nov 2, 2020

please have a look at this specification:

https://tools.ietf.org/html/draft-ietf-rtcweb-security-arch-20#section-6.5

   All Implementations MUST support DTLS 1.2 with the
   TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 cipher suite and the P-256
   curve [FIPS186]. 

if I change the curve name in your patch to use "prime256v1", it works fine.

suggest using this curve or make it configurable via argument string.

@cHuberCoffee cHuberCoffee force-pushed the tls_selfsigned_EC branch 2 times, most recently from 77d76b5 to f573e27 Compare November 4, 2020 14:25
@alfredh
Copy link
Contributor

alfredh commented Nov 5, 2020

this looks good.

a small ccheck warning must be fixed before merging:

./include/re_tls.h:39: has trailing space(s)

@sreimers
Copy link
Member

sreimers commented Nov 9, 2020

@cHuberCoffee please prevent force-push, its easier to review normal commits and the changes. for cleanup we can squash the pull request afterwards (if necessary).

@alfredh
Copy link
Contributor

alfredh commented Nov 9, 2020

looks good now.

ready to merge to master, I would say ..

@sreimers sreimers marked this pull request as ready for review November 9, 2020 12:32
@sreimers sreimers merged commit e40b2f4 into baresip:master Nov 9, 2020
@cHuberCoffee cHuberCoffee deleted the tls_selfsigned_EC branch November 11, 2020 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants