Skip to content

Commit a5ba279

Browse files
committed
Merge branch 'master' into gh-pages
2 parents 09d0a09 + ef0efa5 commit a5ba279

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/net/DtlsSrtp/DtlsSrtpServer.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ public DtlsSrtpServer(Certificate certificateChain, AsymmetricKeyParameter priva
148148
(certificateChain, privateKey) = DtlsUtils.CreateSelfSignedTlsCert();
149149
}
150150

151-
this.cipherSuites = base.GetCipherSuites();
151+
this.cipherSuites = base.GetCipherSuites();
152+
153+
// Add some additional cipher suites to test ECDSA. Bouncy Castle's default list does not include enough options to overlap with some common webrtc libraries.
154+
//int[] newCipherSuites = new int[this.cipherSuites.Length + 1];
155+
//Array.Copy(this.cipherSuites, newCipherSuites, this.cipherSuites.Length);
156+
//newCipherSuites[this.cipherSuites.Length] = CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
157+
//this.cipherSuites = newCipherSuites;
152158

153159
this.mPrivateKey = privateKey;
154160
mCertificateChain = certificateChain;
@@ -226,7 +232,10 @@ public override int GetSelectedCipherSuite()
226232
{
227233
return this.mSelectedCipherSuite = cipherSuite;
228234
}
229-
}
235+
}
236+
237+
logger.LogWarning($"DTLS server no matching cipher suite. Our server cipher suites {string.Join(" ", cipherSuites)}, client offered suites {string.Join(" ", this.mOfferedCipherSuites)}.");
238+
230239
throw new TlsFatalAlert(AlertDescription.handshake_failure);
231240
}
232241

@@ -502,7 +511,8 @@ protected override int[] GetCipherSuites()
502511
for (int i = 0; i < this.cipherSuites.Length; i++)
503512
{
504513
cipherSuites[i] = this.cipherSuites[i];
505-
}
514+
}
515+
506516
return cipherSuites;
507517
}
508518

@@ -582,4 +592,4 @@ public override void NotifySecureRenegotiation(bool secureRenegotiation)
582592
}
583593
}
584594
}
585-
}
595+
}

src/net/DtlsSrtp/DtlsUtils.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
using Org.BouncyCastle.Asn1;
5151
using Org.BouncyCastle.Asn1.Pkcs;
5252
using Org.BouncyCastle.Asn1.X509;
53+
using Org.BouncyCastle.Asn1.X9;
5354
using Org.BouncyCastle.Bcpg;
5455
using Org.BouncyCastle.Crypto;
5556
using Org.BouncyCastle.Crypto.Generators;
@@ -484,9 +485,61 @@ public static (Org.BouncyCastle.X509.X509Certificate certificate, AsymmetricKeyP
484485
return (certificate, subjectKeyPair.Private);
485486
}
486487

488+
public static (Org.BouncyCastle.X509.X509Certificate certificate, AsymmetricKeyParameter privateKey) CreateSelfSignedEcdsaCert(string subjectName, string issuerName)
489+
{
490+
var randomGenerator = new CryptoApiRandomGenerator();
491+
var random = new SecureRandom(randomGenerator);
492+
493+
// Choose an elliptic curve, e.g., secp256r1 (P-256)
494+
var ecSpec = ECNamedCurveTable.GetByName("secp256r1");
495+
496+
// Convert X9ECParameters to ECDomainParameters
497+
var ecDomainParameters = new ECDomainParameters(ecSpec.Curve, ecSpec.G, ecSpec.N, ecSpec.H, ecSpec.GetSeed());
498+
499+
// Generate ECDSA key pair
500+
var keyPairGenerator = new ECKeyPairGenerator("EC");
501+
var keyGenerationParameters = new ECKeyGenerationParameters(ecDomainParameters, random);
502+
keyPairGenerator.Init(keyGenerationParameters);
503+
var subjectKeyPair = keyPairGenerator.GenerateKeyPair();
504+
505+
// Generate ECDSA signature factory
506+
ISignatureFactory signatureFactory = new Asn1SignatureFactory("SHA256WITHECDSA", subjectKeyPair.Private, random);
507+
508+
// The Certificate Generator
509+
var certificateGenerator = new X509V3CertificateGenerator();
510+
certificateGenerator.SetSerialNumber(BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.ValueOf(Int64.MaxValue), random));
511+
certificateGenerator.SetIssuerDN(new X509Name(issuerName));
512+
certificateGenerator.SetSubjectDN(new X509Name(subjectName));
513+
certificateGenerator.SetNotBefore(DateTime.UtcNow.Date);
514+
certificateGenerator.SetNotAfter(DateTime.UtcNow.Date.AddYears(70));
515+
certificateGenerator.SetPublicKey(subjectKeyPair.Public);
516+
517+
// Generate the self-signed certificate
518+
var certificate = certificateGenerator.Generate(signatureFactory);
519+
520+
return (certificate, subjectKeyPair.Private);
521+
}
522+
487523
public static (Org.BouncyCastle.Crypto.Tls.Certificate certificate, AsymmetricKeyParameter privateKey) CreateSelfSignedTlsCert()
488524
{
489525
return CreateSelfSignedTlsCert("CN=localhost", "CN=root", null);
526+
527+
// Testing with ECDSA certificate. Worked with aiortc WebRTC Python library.
528+
// ECDSA is recommended over RSA but is it as well supported as of 14 Oct 2024??
529+
// ECSA failed with:
530+
// - libwebrtc (albeit a 3 year old verison)
531+
// - webrtc-rs (Rust library)
532+
// - werift-webtc (nodejs)
533+
// ECDA Succeeded with:
534+
// - aiortc (Python library)
535+
// - pion (Go library)
536+
537+
//var (cert, key) = CreateSelfSignedEcdsaCert("CN=localhost", "CN=root");
538+
539+
//var chain = new Org.BouncyCastle.Asn1.X509.X509CertificateStructure[] { X509CertificateStructure.GetInstance(cert.GetEncoded()) };
540+
//var tlsCertificate = new Org.BouncyCastle.Crypto.Tls.Certificate(chain);
541+
542+
//return (tlsCertificate, key);
490543
}
491544

492545
public static (Org.BouncyCastle.Crypto.Tls.Certificate certificate, AsymmetricKeyParameter privateKey) CreateSelfSignedTlsCert(string subjectName, string issuerName, AsymmetricKeyParameter issuerPrivateKey)

0 commit comments

Comments
 (0)