Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/guide/networking/peer-connection-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ config.cryptoOptions = crypto;

`certificates` takes the DTLS certificate a peer connection authenticates with, as an `RTCCertificatePEM`. Without one, WebRTC generates a certificate per connection. A certificate that does not parse makes `createPeerConnection()` and `setConfiguration()` throw.

A connection's certificate decides the DTLS fingerprint in its session description. To keep the same fingerprint across connections, so that a remote peer can recognize this one again, generate a certificate once, store it, and pass it to every connection:

```java
RTCCertificatePEM certificate = RTCCertificatePEM.generate(RTCCertificateKeyType.ECDSA, Duration.ofDays(365));

// Store certificate.getPrivateKey() and certificate.getCertificate(), e.g. in a key store.

config.certificates.add(certificate);
```

`generate` makes a self-signed certificate with a new key on the calling thread. `ECDSA` (P-256) takes milliseconds, `RSA` (2048 bits) takes far longer. Without a lifetime, a certificate is valid for 30 days; the lifetime can be at most a year. The private key is a secret: keep it where only your application can read it.

The expiration time passed to its constructor is informational: WebRTC takes it from the certificate. The certificates that `getConfiguration()` and `RTCDtlsTransport.getRemoteCertificates()` return carry that time in `getExpires()`, in milliseconds since the Unix epoch. If it cannot be read, `getExpires()` returns `RTCCertificatePEM.EXPIRES_UNKNOWN` (-1).

## Signaling
Expand Down
21 changes: 21 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_RTCCertificatePEM.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_RTCCertificatePEM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2026 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "JNI_RTCCertificatePEM.h"
#include "JavaBoundary.h"
#include "JavaUtils.h"
#include "rtc/RTCCertificatePEM.h"

#include "rtc_base/rtc_certificate_generator.h"
#include "rtc_base/ssl_identity.h"

#include <cstdint>
#include <optional>

JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_RTCCertificatePEM_generateCertificate
(JNIEnv * env, jclass, jint keyType, jlong lifetimeMillis)
{
return jni::CallFromJava(env, [&]() -> jobject {
// The order of RTCCertificateKeyType.
webrtc::KeyParams params = keyType == 1
? webrtc::KeyParams::RSA()
: webrtc::KeyParams::ECDSA(webrtc::EC_NIST_P256);

std::optional<uint64_t> expires;

if (lifetimeMillis > 0) {
expires = static_cast<uint64_t>(lifetimeMillis);
}

// On the calling thread, which the Java side leaves to the application.
webrtc::scoped_refptr<webrtc::RTCCertificate> certificate =
webrtc::RTCCertificateGenerator::GenerateCertificate(params, expires);

if (!certificate) {
ThrowJavaException(env, "java/lang/IllegalStateException", "Generating the certificate failed");
return nullptr;
}

return jni::RTCCertificatePEM::toJava(env, *certificate).release();
});
}
38 changes: 38 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCCertificateKeyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2026 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.onvoid.webrtc;

/**
* The kind of key a certificate made by {@link
* RTCCertificatePEM#generate(RTCCertificateKeyType)} has.
*
* @author Alex Andres
*/
public enum RTCCertificateKeyType {

/**
* An ECDSA key on the NIST P-256 curve. It is what WebRTC uses by default,
* and is made in a fraction of the time an RSA key takes.
*/
ECDSA,

/**
* An RSA key of 2048 bits, for peers that do not accept ECDSA.
*/
RSA

}
82 changes: 82 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCCertificatePEM.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

package dev.onvoid.webrtc;

import static java.util.Objects.requireNonNull;

import java.time.Duration;

import dev.onvoid.webrtc.internal.NativeLoader;

/**
* Represents a certificate used to authenticate WebRTC communications. This
* class contains PEM strings of an RTCCertificate's private key and
Expand All @@ -25,6 +31,21 @@
*/
public class RTCCertificatePEM {

static {
try {
NativeLoader.loadLibrary("webrtc-java");
}
catch (Exception e) {
throw new RuntimeException("Load library 'webrtc-java' failed", e);
}
}

/**
* The longest lifetime of a certificate made by {@link
* #generate(RTCCertificateKeyType, Duration)}, as WebRTC limits it.
*/
public static final Duration MAX_LIFETIME = Duration.ofDays(365);

/**
* The value of {@link #getExpires()} for a certificate whose expiration
* time cannot be read. No actual expiration time has this value: those
Expand Down Expand Up @@ -92,4 +113,65 @@ public String getCertificate() {
public long getExpires() {
return expires;
}

/**
* Generates a new self-signed certificate with a new private key, as
* {@code RTCPeerConnection.generateCertificate} of the WebRTC
* specification does, valid for 30 days.
*
* @param keyType The kind of key to make.
*
* @return The new certificate.
*
* @see #generate(RTCCertificateKeyType, Duration)
*/
public static RTCCertificatePEM generate(RTCCertificateKeyType keyType) {
requireNonNull(keyType, "keyType");

return generateCertificate(keyType.ordinal(), 0);
}

/**
* Generates a new self-signed certificate with a new private key, as
* {@code RTCPeerConnection.generateCertificate} of the WebRTC
* specification does.
* <p>
* A peer connection makes a certificate of its own unless {@link
* RTCConfiguration#certificates} has one. Its DTLS fingerprint, which the
* session description carries, therefore changes with every connection.
* An application that keeps a certificate, e.g. in a key store, and
* passes it to every connection keeps the fingerprint, so that a remote
* peer can recognize it again.
* <p>
* The key is made on the calling thread. An ECDSA key takes milliseconds,
* an RSA key can take far longer.
*
* @param keyType The kind of key to make.
* @param lifetime How long the certificate is valid from now, at most
* {@link #MAX_LIFETIME}.
*
* @return The new certificate, with its private key.
*
* @throws IllegalArgumentException if the lifetime is not positive or
* longer than {@link #MAX_LIFETIME}.
* @throws IllegalStateException if the certificate could not be made.
*/
public static RTCCertificatePEM generate(RTCCertificateKeyType keyType, Duration lifetime) {
requireNonNull(keyType, "keyType");
requireNonNull(lifetime, "lifetime");

if (lifetime.isNegative() || lifetime.isZero() || lifetime.compareTo(MAX_LIFETIME) > 0) {
throw new IllegalArgumentException("The lifetime must be positive and at most "
+ MAX_LIFETIME.toDays() + " days, got " + lifetime);
}

// At least a millisecond, which 0 would not be.
return generateCertificate(keyType.ordinal(), Math.max(1, lifetime.toMillis()));
}

/*
* Generates a certificate of the RTCCertificateKeyType with the ordinal,
* valid for the given milliseconds, or the default lifetime for 0.
*/
private static native RTCCertificatePEM generateCertificate(int keyType, long lifetimeMillis);
}
Loading
Loading