1 /* 2 * Copyright 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import org.webrtc.PeerConnection; 14 15 /** 16 * Easily storable/serializable version of a native C++ RTCCertificatePEM. 17 */ 18 public class RtcCertificatePem { 19 /** PEM string representation of the private key. */ 20 public final String privateKey; 21 /** PEM string representation of the certificate. */ 22 public final String certificate; 23 /** Default expiration time of 30 days. */ 24 private static final long DEFAULT_EXPIRY = 60 * 60 * 24 * 30; 25 26 /** Instantiate an RtcCertificatePem object from stored strings. */ 27 @CalledByNative RtcCertificatePem(String privateKey, String certificate)28 public RtcCertificatePem(String privateKey, String certificate) { 29 this.privateKey = privateKey; 30 this.certificate = certificate; 31 } 32 33 @CalledByNative getPrivateKey()34 String getPrivateKey() { 35 return privateKey; 36 } 37 38 @CalledByNative getCertificate()39 String getCertificate() { 40 return certificate; 41 } 42 43 /** 44 * Generate a new RtcCertificatePem with the default settings of KeyType = ECDSA and 45 * expires = 30 days. 46 */ generateCertificate()47 public static RtcCertificatePem generateCertificate() { 48 return nativeGenerateCertificate(PeerConnection.KeyType.ECDSA, DEFAULT_EXPIRY); 49 } 50 51 /** 52 * Generate a new RtcCertificatePem with a custom KeyType and the default setting of 53 * expires = 30 days. 54 */ generateCertificate(PeerConnection.KeyType keyType)55 public static RtcCertificatePem generateCertificate(PeerConnection.KeyType keyType) { 56 return nativeGenerateCertificate(keyType, DEFAULT_EXPIRY); 57 } 58 59 /** 60 * Generate a new RtcCertificatePem with a custom expires and the default setting of 61 * KeyType = ECDSA. 62 */ generateCertificate(long expires)63 public static RtcCertificatePem generateCertificate(long expires) { 64 return nativeGenerateCertificate(PeerConnection.KeyType.ECDSA, expires); 65 } 66 67 /** Generate a new RtcCertificatePem with a custom KeyType and a custom expires. */ generateCertificate( PeerConnection.KeyType keyType, long expires)68 public static RtcCertificatePem generateCertificate( 69 PeerConnection.KeyType keyType, long expires) { 70 return nativeGenerateCertificate(keyType, expires); 71 } 72 nativeGenerateCertificate( PeerConnection.KeyType keyType, long expires)73 private static native RtcCertificatePem nativeGenerateCertificate( 74 PeerConnection.KeyType keyType, long expires); 75 } 76