xref: /aosp_15_r20/external/grpc-grpc-java/SECURITY.md (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker# Security Policy
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard WorkerFor information on gRPC Security Policy and reporting potentional security
4*e07d83d3SAndroid Build Coastguard Workerissues, please see [gRPC CVE Process][].
5*e07d83d3SAndroid Build Coastguard Worker
6*e07d83d3SAndroid Build Coastguard Worker[gRPC CVE Process]: https://github.com/grpc/proposal/blob/master/P4-grpc-cve-process.md
7*e07d83d3SAndroid Build Coastguard Worker
8*e07d83d3SAndroid Build Coastguard Worker# Authentication
9*e07d83d3SAndroid Build Coastguard Worker
10*e07d83d3SAndroid Build Coastguard WorkergRPC supports a number of different mechanisms for asserting identity between an
11*e07d83d3SAndroid Build Coastguard Workerclient and server. This document provides code samples demonstrating how to
12*e07d83d3SAndroid Build Coastguard Workerprovide SSL/TLS encryption support and identity assertions in Java, as well as
13*e07d83d3SAndroid Build Coastguard Workerpassing OAuth2 tokens to services that support it.
14*e07d83d3SAndroid Build Coastguard Worker
15*e07d83d3SAndroid Build Coastguard Worker# Transport Security (TLS)
16*e07d83d3SAndroid Build Coastguard Worker
17*e07d83d3SAndroid Build Coastguard WorkerHTTP/2 over TLS mandates the use of [ALPN](https://tools.ietf.org/html/rfc7301)
18*e07d83d3SAndroid Build Coastguard Workerto negotiate the use of the h2 protocol and support for the GCM mode of AES.
19*e07d83d3SAndroid Build Coastguard Worker
20*e07d83d3SAndroid Build Coastguard WorkerThere are multiple options available, but on Android we recommend using the
21*e07d83d3SAndroid Build Coastguard Worker[Play Services Provider](#tls-on-android) and for non-Android systems we
22*e07d83d3SAndroid Build Coastguard Workerrecommend [netty-tcnative with
23*e07d83d3SAndroid Build Coastguard WorkerBoringSSL](#tls-with-netty-tcnative-on-boringssl).
24*e07d83d3SAndroid Build Coastguard Worker
25*e07d83d3SAndroid Build Coastguard Worker## TLS on Android
26*e07d83d3SAndroid Build Coastguard Worker
27*e07d83d3SAndroid Build Coastguard WorkerOn Android we recommend the use of the [Play Services Dynamic Security
28*e07d83d3SAndroid Build Coastguard WorkerProvider][] to ensure your application has an up-to-date OpenSSL library with
29*e07d83d3SAndroid Build Coastguard Workerthe necessary cipher-suites and a reliable ALPN implementation. This requires
30*e07d83d3SAndroid Build Coastguard Worker[updating the security provider at runtime][config-psdsp].
31*e07d83d3SAndroid Build Coastguard Worker
32*e07d83d3SAndroid Build Coastguard WorkerAlthough ALPN mostly works on newer Android releases (especially since 5.0),
33*e07d83d3SAndroid Build Coastguard Workerthere are bugs and discovered security vulnerabilities that are only fixed by
34*e07d83d3SAndroid Build Coastguard Workerupgrading the security provider. Thus, we recommend using the Play Service
35*e07d83d3SAndroid Build Coastguard WorkerDynamic Security Provider for all Android versions.
36*e07d83d3SAndroid Build Coastguard Worker
37*e07d83d3SAndroid Build Coastguard Worker*Note: The Dynamic Security Provider must be installed **before** creating a
38*e07d83d3SAndroid Build Coastguard WorkergRPC OkHttp channel. gRPC statically initializes the security protocol(s)
39*e07d83d3SAndroid Build Coastguard Workeravailable, which means that changes to the security provider after the first
40*e07d83d3SAndroid Build Coastguard Workerchannel is created will not be noticed by gRPC.*
41*e07d83d3SAndroid Build Coastguard Worker
42*e07d83d3SAndroid Build Coastguard Worker[Play Services Dynamic Security Provider]: https://www.appfoundry.be/blog/2014/11/18/Google-Play-Services-Dynamic-Security-Provider/
43*e07d83d3SAndroid Build Coastguard Worker[config-psdsp]: https://developer.android.com/training/articles/security-gms-provider.html
44*e07d83d3SAndroid Build Coastguard Worker
45*e07d83d3SAndroid Build Coastguard Worker### Bundling Conscrypt
46*e07d83d3SAndroid Build Coastguard Worker
47*e07d83d3SAndroid Build Coastguard WorkerIf depending on Play Services is not an option for your app, then you may bundle
48*e07d83d3SAndroid Build Coastguard Worker[Conscrypt](https://conscrypt.org) with your application. Binaries are available
49*e07d83d3SAndroid Build Coastguard Workeron [Maven Central][conscrypt-maven].
50*e07d83d3SAndroid Build Coastguard Worker
51*e07d83d3SAndroid Build Coastguard WorkerLike the Play Services Dynamic Security Provider, you must still "install"
52*e07d83d3SAndroid Build Coastguard WorkerConscrypt before use.
53*e07d83d3SAndroid Build Coastguard Worker
54*e07d83d3SAndroid Build Coastguard Worker```java
55*e07d83d3SAndroid Build Coastguard Workerimport org.conscrypt.Conscrypt;
56*e07d83d3SAndroid Build Coastguard Workerimport java.security.Security;
57*e07d83d3SAndroid Build Coastguard Worker...
58*e07d83d3SAndroid Build Coastguard Worker
59*e07d83d3SAndroid Build Coastguard WorkerSecurity.insertProviderAt(Conscrypt.newProvider(), 1);
60*e07d83d3SAndroid Build Coastguard Worker```
61*e07d83d3SAndroid Build Coastguard Worker
62*e07d83d3SAndroid Build Coastguard Worker[conscrypt-maven]: https://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.conscrypt%20a%3Aconscrypt-android
63*e07d83d3SAndroid Build Coastguard Worker
64*e07d83d3SAndroid Build Coastguard Worker## TLS on non-Android
65*e07d83d3SAndroid Build Coastguard Worker
66*e07d83d3SAndroid Build Coastguard WorkerOpenJDK versions prior to Java 8u252 do not support ALPN. Java 8 has 10% the
67*e07d83d3SAndroid Build Coastguard Workerperformance of OpenSSL.
68*e07d83d3SAndroid Build Coastguard Worker
69*e07d83d3SAndroid Build Coastguard WorkerWe recommend most users use grpc-netty-shaded, which includes netty-tcnative on
70*e07d83d3SAndroid Build Coastguard WorkerBoringSSL. It includes pre-built libraries for 64 bit Windows, OS X, and 64 bit
71*e07d83d3SAndroid Build Coastguard WorkerLinux. For 32 bit Windows, Conscrypt is an option. For all other platforms, Java
72*e07d83d3SAndroid Build Coastguard Worker9+ is required.
73*e07d83d3SAndroid Build Coastguard Worker
74*e07d83d3SAndroid Build Coastguard WorkerFor users of xDS management protocol, the grpc-netty-shaded transport is
75*e07d83d3SAndroid Build Coastguard Workerparticularly appropriate since it is already used internally for the xDS
76*e07d83d3SAndroid Build Coastguard Workerprotocol and is a runtime dependency of grpc-xds.
77*e07d83d3SAndroid Build Coastguard Worker
78*e07d83d3SAndroid Build Coastguard WorkerFor users of grpc-netty we recommend [netty-tcnative with
79*e07d83d3SAndroid Build Coastguard WorkerBoringSSL](#tls-with-netty-tcnative-on-boringssl), although using the built-in
80*e07d83d3SAndroid Build Coastguard WorkerJDK support in Java 9+, [Conscrypt](#tls-with-conscrypt), and [netty-tcnative
81*e07d83d3SAndroid Build Coastguard Workerwith OpenSSL](#tls-with-netty-tcnative-on-openssl) are other valid options.
82*e07d83d3SAndroid Build Coastguard Worker
83*e07d83d3SAndroid Build Coastguard Worker[Netty TCNative](https://github.com/netty/netty-tcnative) is a fork of
84*e07d83d3SAndroid Build Coastguard Worker[Apache Tomcat's tcnative](https://tomcat.apache.org/native-doc/) and is a JNI
85*e07d83d3SAndroid Build Coastguard Workerwrapper around OpenSSL/BoringSSL/LibreSSL.
86*e07d83d3SAndroid Build Coastguard Worker
87*e07d83d3SAndroid Build Coastguard WorkerWe recommend BoringSSL for its simplicitly and low occurrence of security
88*e07d83d3SAndroid Build Coastguard Workervulnerabilities relative to OpenSSL. BoringSSL is used by Conscrypt as well.
89*e07d83d3SAndroid Build Coastguard Worker
90*e07d83d3SAndroid Build Coastguard Worker### TLS with netty-tcnative on BoringSSL
91*e07d83d3SAndroid Build Coastguard Worker
92*e07d83d3SAndroid Build Coastguard WorkerNetty-tcnative with BoringSSL includes BoringSSL statically linked in the
93*e07d83d3SAndroid Build Coastguard Workerbinary. This means the system's pre-installed TLS libraries will not be used.
94*e07d83d3SAndroid Build Coastguard WorkerProduction systems that have centralized upgrade agility in the face of
95*e07d83d3SAndroid Build Coastguard Workersecurity vulnerabilities may want to use [netty-tcnative on
96*e07d83d3SAndroid Build Coastguard WorkerOpenSSL](#tls-with-netty-tcnative-on-openssl) instead.
97*e07d83d3SAndroid Build Coastguard Worker
98*e07d83d3SAndroid Build Coastguard WorkerUsers of grpc-netty-shaded will automatically use netty-tcnative with
99*e07d83d3SAndroid Build Coastguard WorkerBoringSSL.
100*e07d83d3SAndroid Build Coastguard Worker
101*e07d83d3SAndroid Build Coastguard Workergrpc-netty users will need to add the appropriate
102*e07d83d3SAndroid Build Coastguard Worker`netty-tcnative-boringssl-static` artifact to the application's classpath.
103*e07d83d3SAndroid Build Coastguard WorkerArtifacts are available for 64 bit Windows, OS X, and 64 bit Linux.
104*e07d83d3SAndroid Build Coastguard Worker
105*e07d83d3SAndroid Build Coastguard WorkerDepending on netty-tcnative-boringssl-static will include binaries for all
106*e07d83d3SAndroid Build Coastguard Workersupported platforms. For Maven:
107*e07d83d3SAndroid Build Coastguard Worker
108*e07d83d3SAndroid Build Coastguard Worker```xml
109*e07d83d3SAndroid Build Coastguard Worker  <dependencies>
110*e07d83d3SAndroid Build Coastguard Worker    <dependency>
111*e07d83d3SAndroid Build Coastguard Worker      <groupId>io.netty</groupId>
112*e07d83d3SAndroid Build Coastguard Worker      <artifactId>netty-tcnative-boringssl-static</artifactId>
113*e07d83d3SAndroid Build Coastguard Worker      <version>2.0.20.Final</version> <!-- See table for correct version -->
114*e07d83d3SAndroid Build Coastguard Worker      <scope>runtime</scope>
115*e07d83d3SAndroid Build Coastguard Worker    </dependency>
116*e07d83d3SAndroid Build Coastguard Worker  </dependencies>
117*e07d83d3SAndroid Build Coastguard Worker```
118*e07d83d3SAndroid Build Coastguard Worker
119*e07d83d3SAndroid Build Coastguard WorkerAnd for Gradle:
120*e07d83d3SAndroid Build Coastguard Worker
121*e07d83d3SAndroid Build Coastguard Worker```gradle
122*e07d83d3SAndroid Build Coastguard Workerdependencies {
123*e07d83d3SAndroid Build Coastguard Worker  // See table for correct version
124*e07d83d3SAndroid Build Coastguard Worker  runtime 'io.netty:netty-tcnative-boringssl-static:2.0.20.Final'
125*e07d83d3SAndroid Build Coastguard Worker}
126*e07d83d3SAndroid Build Coastguard Worker```
127*e07d83d3SAndroid Build Coastguard Worker
128*e07d83d3SAndroid Build Coastguard WorkerFor projects sensitive to binary size, specify the classifier for the precise
129*e07d83d3SAndroid Build Coastguard Workerplatform you need: `windows-x86_64`, `osx-x86_64`, `linux-x86_64`. You can also
130*e07d83d3SAndroid Build Coastguard Workeruse [os-maven-plugin](https://github.com/trustin/os-maven-plugin) or
131*e07d83d3SAndroid Build Coastguard Worker[osdetector-gradle-plugin](https://github.com/google/osdetector-gradle-plugin),
132*e07d83d3SAndroid Build Coastguard Workerto choose the classifier for the platform running the build.
133*e07d83d3SAndroid Build Coastguard Worker
134*e07d83d3SAndroid Build Coastguard Worker### TLS with netty-tcnative on OpenSSL
135*e07d83d3SAndroid Build Coastguard Worker
136*e07d83d3SAndroid Build Coastguard WorkerUsing OpenSSL can have more initial configuration issues, but can be useful if
137*e07d83d3SAndroid Build Coastguard Workeryour OS's OpenSSL version is recent and kept up-to-date with security fixes.
138*e07d83d3SAndroid Build Coastguard WorkerOpenSSL is not included with tcnative, but instead is dynamically linked using
139*e07d83d3SAndroid Build Coastguard Workeryour operating system's OpenSSL.
140*e07d83d3SAndroid Build Coastguard Worker
141*e07d83d3SAndroid Build Coastguard WorkerTo use OpenSSL you will use the `netty-tcnative` artifact. It requires:
142*e07d83d3SAndroid Build Coastguard Worker
143*e07d83d3SAndroid Build Coastguard Worker1. [OpenSSL](https://www.openssl.org/) version >= 1.0.2 for ALPN support.
144*e07d83d3SAndroid Build Coastguard Worker2. [Apache APR library (libapr-1)](https://apr.apache.org/) version >= 1.5.2.
145*e07d83d3SAndroid Build Coastguard Worker
146*e07d83d3SAndroid Build Coastguard WorkerYou must specify a classifier for the correct netty-tcnative binary:
147*e07d83d3SAndroid Build Coastguard Worker`windows-x86_64`, `osx-x86_64`, `linux-x86_64`, or `linux-x86_64-fedora`.
148*e07d83d3SAndroid Build Coastguard WorkerFedora derivatives use a different soname from other Linux distributations, so
149*e07d83d3SAndroid Build Coastguard Workeryou must select the "fedora" version on those distributions.
150*e07d83d3SAndroid Build Coastguard Worker
151*e07d83d3SAndroid Build Coastguard WorkerIn Maven, you can use the
152*e07d83d3SAndroid Build Coastguard Worker[os-maven-plugin](https://github.com/trustin/os-maven-plugin) to help simplify
153*e07d83d3SAndroid Build Coastguard Workerthe dependency.
154*e07d83d3SAndroid Build Coastguard Worker
155*e07d83d3SAndroid Build Coastguard Worker```xml
156*e07d83d3SAndroid Build Coastguard Worker<project>
157*e07d83d3SAndroid Build Coastguard Worker  <dependencies>
158*e07d83d3SAndroid Build Coastguard Worker    <dependency>
159*e07d83d3SAndroid Build Coastguard Worker      <groupId>io.netty</groupId>
160*e07d83d3SAndroid Build Coastguard Worker      <artifactId>netty-tcnative</artifactId>
161*e07d83d3SAndroid Build Coastguard Worker      <version>2.0.20.Final</version> <!-- see table for correct version -->
162*e07d83d3SAndroid Build Coastguard Worker      <classifier>${tcnative.classifier}</classifier>
163*e07d83d3SAndroid Build Coastguard Worker      <scope>runtime</scope>
164*e07d83d3SAndroid Build Coastguard Worker    </dependency>
165*e07d83d3SAndroid Build Coastguard Worker  </dependencies>
166*e07d83d3SAndroid Build Coastguard Worker
167*e07d83d3SAndroid Build Coastguard Worker  <build>
168*e07d83d3SAndroid Build Coastguard Worker    <extensions>
169*e07d83d3SAndroid Build Coastguard Worker      <!-- Use os-maven-plugin to initialize the "os.detected" properties -->
170*e07d83d3SAndroid Build Coastguard Worker      <extension>
171*e07d83d3SAndroid Build Coastguard Worker        <groupId>kr.motd.maven</groupId>
172*e07d83d3SAndroid Build Coastguard Worker        <artifactId>os-maven-plugin</artifactId>
173*e07d83d3SAndroid Build Coastguard Worker        <version>1.7.1</version>
174*e07d83d3SAndroid Build Coastguard Worker      </extension>
175*e07d83d3SAndroid Build Coastguard Worker    </extensions>
176*e07d83d3SAndroid Build Coastguard Worker    <plugins>
177*e07d83d3SAndroid Build Coastguard Worker      <!-- Use Ant to configure the appropriate "tcnative.classifier" property -->
178*e07d83d3SAndroid Build Coastguard Worker      <plugin>
179*e07d83d3SAndroid Build Coastguard Worker        <groupId>org.apache.maven.plugins</groupId>
180*e07d83d3SAndroid Build Coastguard Worker        <artifactId>maven-antrun-plugin</artifactId>
181*e07d83d3SAndroid Build Coastguard Worker        <executions>
182*e07d83d3SAndroid Build Coastguard Worker          <execution>
183*e07d83d3SAndroid Build Coastguard Worker            <phase>initialize</phase>
184*e07d83d3SAndroid Build Coastguard Worker            <configuration>
185*e07d83d3SAndroid Build Coastguard Worker              <exportAntProperties>true</exportAntProperties>
186*e07d83d3SAndroid Build Coastguard Worker              <target>
187*e07d83d3SAndroid Build Coastguard Worker                <condition property="tcnative.classifier"
188*e07d83d3SAndroid Build Coastguard Worker                           value="${os.detected.classifier}-fedora"
189*e07d83d3SAndroid Build Coastguard Worker                           else="${os.detected.classifier}">
190*e07d83d3SAndroid Build Coastguard Worker                  <isset property="os.detected.release.fedora"/>
191*e07d83d3SAndroid Build Coastguard Worker                </condition>
192*e07d83d3SAndroid Build Coastguard Worker              </target>
193*e07d83d3SAndroid Build Coastguard Worker            </configuration>
194*e07d83d3SAndroid Build Coastguard Worker            <goals>
195*e07d83d3SAndroid Build Coastguard Worker              <goal>run</goal>
196*e07d83d3SAndroid Build Coastguard Worker            </goals>
197*e07d83d3SAndroid Build Coastguard Worker          </execution>
198*e07d83d3SAndroid Build Coastguard Worker        </executions>
199*e07d83d3SAndroid Build Coastguard Worker      </plugin>
200*e07d83d3SAndroid Build Coastguard Worker    </plugins>
201*e07d83d3SAndroid Build Coastguard Worker  </build>
202*e07d83d3SAndroid Build Coastguard Worker</project>
203*e07d83d3SAndroid Build Coastguard Worker```
204*e07d83d3SAndroid Build Coastguard Worker
205*e07d83d3SAndroid Build Coastguard WorkerAnd in Gradle you can use the
206*e07d83d3SAndroid Build Coastguard Worker[osdetector-gradle-plugin](https://github.com/google/osdetector-gradle-plugin).
207*e07d83d3SAndroid Build Coastguard Worker
208*e07d83d3SAndroid Build Coastguard Worker```gradle
209*e07d83d3SAndroid Build Coastguard Workerbuildscript {
210*e07d83d3SAndroid Build Coastguard Worker  repositories {
211*e07d83d3SAndroid Build Coastguard Worker    mavenCentral()
212*e07d83d3SAndroid Build Coastguard Worker  }
213*e07d83d3SAndroid Build Coastguard Worker  dependencies {
214*e07d83d3SAndroid Build Coastguard Worker    classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
215*e07d83d3SAndroid Build Coastguard Worker  }
216*e07d83d3SAndroid Build Coastguard Worker}
217*e07d83d3SAndroid Build Coastguard Worker
218*e07d83d3SAndroid Build Coastguard Worker// Use the osdetector-gradle-plugin
219*e07d83d3SAndroid Build Coastguard Workerapply plugin: "com.google.osdetector"
220*e07d83d3SAndroid Build Coastguard Worker
221*e07d83d3SAndroid Build Coastguard Workerdef tcnative_classifier = osdetector.classifier;
222*e07d83d3SAndroid Build Coastguard Worker// Fedora variants use a different soname for OpenSSL than other linux distributions
223*e07d83d3SAndroid Build Coastguard Worker// (see http://netty.io/wiki/forked-tomcat-native.html).
224*e07d83d3SAndroid Build Coastguard Workerif (osdetector.os == "linux" && osdetector.release.isLike("fedora")) {
225*e07d83d3SAndroid Build Coastguard Worker  tcnative_classifier += "-fedora";
226*e07d83d3SAndroid Build Coastguard Worker}
227*e07d83d3SAndroid Build Coastguard Worker
228*e07d83d3SAndroid Build Coastguard Workerdependencies {
229*e07d83d3SAndroid Build Coastguard Worker    runtime 'io.netty:netty-tcnative:2.0.20.Final:' + tcnative_classifier
230*e07d83d3SAndroid Build Coastguard Worker}
231*e07d83d3SAndroid Build Coastguard Worker```
232*e07d83d3SAndroid Build Coastguard Worker
233*e07d83d3SAndroid Build Coastguard Worker### TLS with Conscrypt
234*e07d83d3SAndroid Build Coastguard Worker
235*e07d83d3SAndroid Build Coastguard Worker[Conscrypt](https://conscrypt.org) provides an implementation of the JSSE
236*e07d83d3SAndroid Build Coastguard Workersecurity APIs based on BoringSSL. Pre-built binaries are available for 32 and
237*e07d83d3SAndroid Build Coastguard Worker64 bit Windows, OS X, and 64 bit Linux.
238*e07d83d3SAndroid Build Coastguard Worker
239*e07d83d3SAndroid Build Coastguard WorkerDepend on `conscrypt-openjdk-uber` for binaries of all supported JRE platforms.
240*e07d83d3SAndroid Build Coastguard WorkerFor projects sensitive to binary size, depend on `conscrypt-openjdk` and
241*e07d83d3SAndroid Build Coastguard Workerspecify the appropriate classifier. `os-maven-plugin` and
242*e07d83d3SAndroid Build Coastguard Worker`osdetector-gradle-plugin` may also be used. See the documentation for
243*e07d83d3SAndroid Build Coastguard Worker[netty-tcnative-boringssl-static](#tls-with-netty-tcnative-on-boringssl) for
244*e07d83d3SAndroid Build Coastguard Workerexample usage of the plugins.
245*e07d83d3SAndroid Build Coastguard Worker
246*e07d83d3SAndroid Build Coastguard WorkerGenerally you will "install" Conscrypt before use, for gRPC to find.
247*e07d83d3SAndroid Build Coastguard Worker
248*e07d83d3SAndroid Build Coastguard Worker```java
249*e07d83d3SAndroid Build Coastguard Workerimport org.conscrypt.Conscrypt;
250*e07d83d3SAndroid Build Coastguard Workerimport java.security.Security;
251*e07d83d3SAndroid Build Coastguard Worker...
252*e07d83d3SAndroid Build Coastguard Worker
253*e07d83d3SAndroid Build Coastguard Worker// Somewhere in main()
254*e07d83d3SAndroid Build Coastguard WorkerSecurity.insertProviderAt(Conscrypt.newProvider(), 1);
255*e07d83d3SAndroid Build Coastguard Worker```
256*e07d83d3SAndroid Build Coastguard Worker
257*e07d83d3SAndroid Build Coastguard Worker## Enabling TLS on a server
258*e07d83d3SAndroid Build Coastguard Worker
259*e07d83d3SAndroid Build Coastguard WorkerTo use TLS on the server, a certificate chain and private key need to be
260*e07d83d3SAndroid Build Coastguard Workerspecified in PEM format. The standard TLS port is 443, but we use 8443 below to
261*e07d83d3SAndroid Build Coastguard Workeravoid needing extra permissions from the OS.
262*e07d83d3SAndroid Build Coastguard Worker
263*e07d83d3SAndroid Build Coastguard Worker```java
264*e07d83d3SAndroid Build Coastguard WorkerServerCredentials creds = TlsServerCredentials.create(certChainFile, privateKeyFile);
265*e07d83d3SAndroid Build Coastguard WorkerServer server = Grpc.newServerBuilderForPort(8443, creds)
266*e07d83d3SAndroid Build Coastguard Worker    .addService(serviceImplementation)
267*e07d83d3SAndroid Build Coastguard Worker    .build()
268*e07d83d3SAndroid Build Coastguard Worker    .start();
269*e07d83d3SAndroid Build Coastguard Worker```
270*e07d83d3SAndroid Build Coastguard Worker
271*e07d83d3SAndroid Build Coastguard WorkerIf the issuing certificate authority is not known to the client then a properly
272*e07d83d3SAndroid Build Coastguard Workerconfigured trust manager should be provided to TlsChannelCredentials and used to
273*e07d83d3SAndroid Build Coastguard Workerconstruct the channel.
274*e07d83d3SAndroid Build Coastguard Worker
275*e07d83d3SAndroid Build Coastguard Worker## Mutual TLS
276*e07d83d3SAndroid Build Coastguard Worker
277*e07d83d3SAndroid Build Coastguard Worker[Mutual authentication][] (or "client-side authentication") configuration is similar to the server by providing truststores, a client certificate and private key to the client channel.  The server must also be configured to request a certificate from clients, as well as truststores for which client certificates it should allow.
278*e07d83d3SAndroid Build Coastguard Worker
279*e07d83d3SAndroid Build Coastguard Worker```java
280*e07d83d3SAndroid Build Coastguard WorkerServerCredentials creds = TlsServerCredentials.newBuilder()
281*e07d83d3SAndroid Build Coastguard Worker    .keyManager(certChainFile, privateKeyFile)
282*e07d83d3SAndroid Build Coastguard Worker    .trustManager(clientCAsFile)
283*e07d83d3SAndroid Build Coastguard Worker    .clientAuth(TlsServerCredentials.ClientAuth.REQUIRE)
284*e07d83d3SAndroid Build Coastguard Worker    .build();
285*e07d83d3SAndroid Build Coastguard Worker```
286*e07d83d3SAndroid Build Coastguard Worker
287*e07d83d3SAndroid Build Coastguard WorkerNegotiated client certificates are available in the SSLSession, which is found
288*e07d83d3SAndroid Build Coastguard Workerin the `Grpc.TRANSPORT_ATTR_SSL_SESSION` attribute of the call. A server
289*e07d83d3SAndroid Build Coastguard Workerinterceptor can provide details in the current Context.
290*e07d83d3SAndroid Build Coastguard Worker
291*e07d83d3SAndroid Build Coastguard Worker```java
292*e07d83d3SAndroid Build Coastguard Worker// The application uses this in its handlers.
293*e07d83d3SAndroid Build Coastguard Workerpublic static final Context.Key<MySecurityInfo> SECURITY_INFO = Context.key("my.security.Info");
294*e07d83d3SAndroid Build Coastguard Worker
295*e07d83d3SAndroid Build Coastguard Worker@Override
296*e07d83d3SAndroid Build Coastguard Workerpublic <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
297*e07d83d3SAndroid Build Coastguard Worker    Metadata headers, ServerCallHandler<ReqT, RespT> next) {
298*e07d83d3SAndroid Build Coastguard Worker    SSLSession sslSession = call.getAttributes().get(Grpc.TRANSPORT_ATTR_SSL_SESSION);
299*e07d83d3SAndroid Build Coastguard Worker    if (sslSession == null) {
300*e07d83d3SAndroid Build Coastguard Worker        return next.startCall(call, headers);
301*e07d83d3SAndroid Build Coastguard Worker    }
302*e07d83d3SAndroid Build Coastguard Worker    // This interceptor can provide a centralized policy to process the client's
303*e07d83d3SAndroid Build Coastguard Worker    // certificate. Avoid exposing low-level details (like SSLSession) and
304*e07d83d3SAndroid Build Coastguard Worker    // instead provide a higher-level concept like "authenticated user."
305*e07d83d3SAndroid Build Coastguard Worker    MySecurityInfo info = process(sslSession);
306*e07d83d3SAndroid Build Coastguard Worker    return Contexts.interceptCall(
307*e07d83d3SAndroid Build Coastguard Worker        Context.current().withValue(SECURITY_INFO, info), call, headers, next);
308*e07d83d3SAndroid Build Coastguard Worker}
309*e07d83d3SAndroid Build Coastguard Worker```
310*e07d83d3SAndroid Build Coastguard Worker
311*e07d83d3SAndroid Build Coastguard Worker[Mutual authentication]: http://en.wikipedia.org/wiki/Transport_Layer_Security#Client-authenticated_TLS_handshake
312*e07d83d3SAndroid Build Coastguard Worker
313*e07d83d3SAndroid Build Coastguard Worker## Troubleshooting
314*e07d83d3SAndroid Build Coastguard Worker
315*e07d83d3SAndroid Build Coastguard WorkerIf you received an error message "ALPN is not configured properly" or "Jetty ALPN/NPN has not been properly configured", it most likely means that:
316*e07d83d3SAndroid Build Coastguard Worker - ALPN related dependencies are either not present in the classpath
317*e07d83d3SAndroid Build Coastguard Worker - or that there is a classpath conflict
318*e07d83d3SAndroid Build Coastguard Worker - or that a wrong version is used due to dependency management
319*e07d83d3SAndroid Build Coastguard Worker - or you are on an unsupported platform (e.g., 32-bit OS). See [Transport
320*e07d83d3SAndroid Build Coastguard Worker   Security](#transport-security-tls) for supported platforms.
321*e07d83d3SAndroid Build Coastguard Worker
322*e07d83d3SAndroid Build Coastguard Worker### Netty
323*e07d83d3SAndroid Build Coastguard WorkerIf you aren't using gRPC on Android devices, you are most likely using `grpc-netty` transport.
324*e07d83d3SAndroid Build Coastguard Worker
325*e07d83d3SAndroid Build Coastguard WorkerIf you are developing for Android and have a dependency on `grpc-netty`, you should remove it as `grpc-netty` is unsupported on Android. Use `grpc-okhttp` instead.
326*e07d83d3SAndroid Build Coastguard Worker
327*e07d83d3SAndroid Build Coastguard WorkerIf you are on a 32-bit operating system, using Java 11+ may be the easiest
328*e07d83d3SAndroid Build Coastguard Workersolution, as ALPN was added to Java in Java 9. If on 32-bit Windows, [Conscrypt
329*e07d83d3SAndroid Build Coastguard Workeris an option](#tls-with-conscrypt). Otherwise you need to [build your own 32-bit
330*e07d83d3SAndroid Build Coastguard Workerversion of
331*e07d83d3SAndroid Build Coastguard Worker`netty-tcnative`](https://netty.io/wiki/forked-tomcat-native.html#wiki-h2-6).
332*e07d83d3SAndroid Build Coastguard Worker
333*e07d83d3SAndroid Build Coastguard WorkerIf on Alpine Linux and you see "Error loading shared library libcrypt.so.1: No
334*e07d83d3SAndroid Build Coastguard Workersuch file or directory". Run `apk update && apk add libc6-compat` to install the
335*e07d83d3SAndroid Build Coastguard Workernecessary dependency.
336*e07d83d3SAndroid Build Coastguard Worker
337*e07d83d3SAndroid Build Coastguard WorkerIf on Alpine Linux, try to use `grpc-netty-shaded` instead of `grpc-netty` or
338*e07d83d3SAndroid Build Coastguard Worker(if you need `grpc-netty`) `netty-tcnative-boringssl-static` instead of
339*e07d83d3SAndroid Build Coastguard Worker`netty-tcnative`. If those are not an option, you may consider using
340*e07d83d3SAndroid Build Coastguard Worker[netty-tcnative-alpine](https://github.com/pires/netty-tcnative-alpine).
341*e07d83d3SAndroid Build Coastguard Worker
342*e07d83d3SAndroid Build Coastguard WorkerIf on Fedora 30 or later and you see "libcrypt.so.1: cannot open shared object
343*e07d83d3SAndroid Build Coastguard Workerfile: No such file or directory". Run `dnf -y install libxcrypt-compat` to
344*e07d83d3SAndroid Build Coastguard Workerinstall the necessary dependency.
345*e07d83d3SAndroid Build Coastguard Worker
346*e07d83d3SAndroid Build Coastguard WorkerMost dependency versioning problems can be solved by using
347*e07d83d3SAndroid Build Coastguard Worker`io.grpc:grpc-netty-shaded` instead of `io.grpc:grpc-netty`, although this also
348*e07d83d3SAndroid Build Coastguard Workerlimits your usage of the Netty-specific APIs. `io.grpc:grpc-netty-shaded`
349*e07d83d3SAndroid Build Coastguard Workerincludes the proper version of Netty and `netty-tcnative-boringssl-static` in a
350*e07d83d3SAndroid Build Coastguard Workerway that won't conflict with other Netty usages.
351*e07d83d3SAndroid Build Coastguard Worker
352*e07d83d3SAndroid Build Coastguard WorkerFind the dependency tree (e.g., `mvn dependency:tree`), and look for versions of:
353*e07d83d3SAndroid Build Coastguard Worker - `io.grpc:grpc-netty`
354*e07d83d3SAndroid Build Coastguard Worker - `io.netty:netty-handler` (really, make sure all of io.netty except for
355*e07d83d3SAndroid Build Coastguard Worker   netty-tcnative has the same version)
356*e07d83d3SAndroid Build Coastguard Worker - `io.netty:netty-tcnative-boringssl-static:jar`
357*e07d83d3SAndroid Build Coastguard Worker
358*e07d83d3SAndroid Build Coastguard WorkerIf `netty-tcnative-boringssl-static` is missing, then you either need to add it as a dependency, or use alternative methods of providing ALPN capability by reading the *Transport Security (TLS)* section carefully.
359*e07d83d3SAndroid Build Coastguard Worker
360*e07d83d3SAndroid Build Coastguard WorkerIf you have both `netty-handler` and `netty-tcnative-boringssl-static` dependencies, then check the versions carefully. These versions could've been overridden by dependency management from another BOM. You would receive the "ALPN is not configured properly" exception if you are using incompatible versions.
361*e07d83d3SAndroid Build Coastguard Worker
362*e07d83d3SAndroid Build Coastguard WorkerIf you have other `netty` dependencies, such as `netty-all`, that are pulled in from other libraries, then ultimately you should make sure only one `netty` dependency is used to avoid classpath conflict. The easiest way is to exclude transitive Netty dependencies from all the immediate dependencies, e.g., in Maven use `<exclusions>`, and then add an explict Netty dependency in your project along with the corresponding `tcnative` versions. See the versions table below.
363*e07d83d3SAndroid Build Coastguard Worker
364*e07d83d3SAndroid Build Coastguard WorkerIf you are running in a runtime environment that also uses Netty (e.g., Hadoop, Spark, Spring Boot 2) and you have no control over the Netty version at all, then you should use a shaded gRPC Netty dependency to avoid classpath conflicts with other Netty versions in runtime the classpath:
365*e07d83d3SAndroid Build Coastguard Worker - Remove `io.grpc:grpc-netty` dependency
366*e07d83d3SAndroid Build Coastguard Worker - Add `io.grpc:grpc-netty-shaded` dependency
367*e07d83d3SAndroid Build Coastguard Worker
368*e07d83d3SAndroid Build Coastguard WorkerBelow are known to work version combinations:
369*e07d83d3SAndroid Build Coastguard Worker
370*e07d83d3SAndroid Build Coastguard Workergrpc-netty version | netty-handler version | netty-tcnative-boringssl-static version
371*e07d83d3SAndroid Build Coastguard Worker------------------ |-----------------------| ---------------------------------------
372*e07d83d3SAndroid Build Coastguard Worker1.0.0-1.0.1        | 4.1.3.Final           | 1.1.33.Fork19
373*e07d83d3SAndroid Build Coastguard Worker1.0.2-1.0.3        | 4.1.6.Final           | 1.1.33.Fork23
374*e07d83d3SAndroid Build Coastguard Worker1.1.x-1.3.x        | 4.1.8.Final           | 1.1.33.Fork26
375*e07d83d3SAndroid Build Coastguard Worker1.4.x              | 4.1.11.Final          | 2.0.1.Final
376*e07d83d3SAndroid Build Coastguard Worker1.5.x              | 4.1.12.Final          | 2.0.5.Final
377*e07d83d3SAndroid Build Coastguard Worker1.6.x              | 4.1.14.Final          | 2.0.5.Final
378*e07d83d3SAndroid Build Coastguard Worker1.7.x-1.8.x        | 4.1.16.Final          | 2.0.6.Final
379*e07d83d3SAndroid Build Coastguard Worker1.9.x-1.10.x       | 4.1.17.Final          | 2.0.7.Final
380*e07d83d3SAndroid Build Coastguard Worker1.11.x-1.12.x      | 4.1.22.Final          | 2.0.7.Final
381*e07d83d3SAndroid Build Coastguard Worker1.13.x             | 4.1.25.Final          | 2.0.8.Final
382*e07d83d3SAndroid Build Coastguard Worker1.14.x-1.15.x      | 4.1.27.Final          | 2.0.12.Final
383*e07d83d3SAndroid Build Coastguard Worker1.16.x-1.17.x      | 4.1.30.Final          | 2.0.17.Final
384*e07d83d3SAndroid Build Coastguard Worker1.18.x-1.19.x      | 4.1.32.Final          | 2.0.20.Final
385*e07d83d3SAndroid Build Coastguard Worker1.20.x-1.21.x      | 4.1.34.Final          | 2.0.22.Final
386*e07d83d3SAndroid Build Coastguard Worker1.22.x             | 4.1.35.Final          | 2.0.25.Final
387*e07d83d3SAndroid Build Coastguard Worker1.23.x-1.24.x      | 4.1.38.Final          | 2.0.25.Final
388*e07d83d3SAndroid Build Coastguard Worker1.25.x-1.27.x      | 4.1.42.Final          | 2.0.26.Final
389*e07d83d3SAndroid Build Coastguard Worker1.28.x             | 4.1.45.Final          | 2.0.28.Final
390*e07d83d3SAndroid Build Coastguard Worker1.29.x-1.31.x      | 4.1.48.Final          | 2.0.30.Final
391*e07d83d3SAndroid Build Coastguard Worker1.32.x-1.34.x      | 4.1.51.Final          | 2.0.31.Final
392*e07d83d3SAndroid Build Coastguard Worker1.35.x-1.41.x      | 4.1.52.Final          | 2.0.34.Final
393*e07d83d3SAndroid Build Coastguard Worker1.42.x-1.43.x      | 4.1.63.Final          | 2.0.38.Final
394*e07d83d3SAndroid Build Coastguard Worker1.44.x-1.47.x      | 4.1.72.Final          | 2.0.46.Final
395*e07d83d3SAndroid Build Coastguard Worker1.48.x-1.49.x      | 4.1.77.Final          | 2.0.53.Final
396*e07d83d3SAndroid Build Coastguard Worker1.50.x-1.53.x      | 4.1.79.Final          | 2.0.54.Final
397*e07d83d3SAndroid Build Coastguard Worker1.54.x-1.55.x      | 4.1.87.Final          | 2.0.56.Final
398*e07d83d3SAndroid Build Coastguard Worker1.56.x-            | 4.1.87.Final          | 2.0.61.Final
399*e07d83d3SAndroid Build Coastguard Worker
400*e07d83d3SAndroid Build Coastguard Worker_(grpc-netty-shaded avoids issues with keeping these versions in sync.)_
401*e07d83d3SAndroid Build Coastguard Worker
402*e07d83d3SAndroid Build Coastguard Worker### OkHttp
403*e07d83d3SAndroid Build Coastguard WorkerIf you are using gRPC on Android devices, you are most likely using
404*e07d83d3SAndroid Build Coastguard Worker`grpc-okhttp` transport.
405*e07d83d3SAndroid Build Coastguard Worker
406*e07d83d3SAndroid Build Coastguard WorkerFind the dependency tree (e.g., `mvn dependency:tree`), and look for
407*e07d83d3SAndroid Build Coastguard Worker`io.grpc:grpc-okhttp`. If you don't have `grpc-okhttp`, you should add it as a
408*e07d83d3SAndroid Build Coastguard Workerdependency.
409*e07d83d3SAndroid Build Coastguard Worker
410*e07d83d3SAndroid Build Coastguard Worker# gRPC over plaintext
411*e07d83d3SAndroid Build Coastguard Worker
412*e07d83d3SAndroid Build Coastguard WorkerAn option is provided to use gRPC over plaintext without TLS. While this is convenient for testing environments, users must be aware of the security risks of doing so for real production systems.
413*e07d83d3SAndroid Build Coastguard Worker
414*e07d83d3SAndroid Build Coastguard Worker# Using OAuth2
415*e07d83d3SAndroid Build Coastguard Worker
416*e07d83d3SAndroid Build Coastguard WorkerThe following code snippet shows how you can call the Google Cloud PubSub API using gRPC with a service account. The credentials are loaded from a key stored in a well-known location or by detecting that the application is running in an environment that can provide one automatically, e.g. Google Compute Engine. While this example is specific to Google and it's services, similar patterns can be followed for other service providers.
417*e07d83d3SAndroid Build Coastguard Worker
418*e07d83d3SAndroid Build Coastguard Worker```java
419*e07d83d3SAndroid Build Coastguard Worker// Use the default credentials from the environment
420*e07d83d3SAndroid Build Coastguard WorkerChannelCredentials creds = GoogleDefaultChannelCredentials.create();
421*e07d83d3SAndroid Build Coastguard Worker// Create a channel to the service
422*e07d83d3SAndroid Build Coastguard WorkerManagedChannel channel = Grpc.newChannelBuilder("dns:///pubsub.googleapis.com", creds)
423*e07d83d3SAndroid Build Coastguard Worker    .build();
424*e07d83d3SAndroid Build Coastguard Worker// Create a stub and send an RPC
425*e07d83d3SAndroid Build Coastguard WorkerPublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
426*e07d83d3SAndroid Build Coastguard WorkerpublisherStub.publish(someMessage);
427*e07d83d3SAndroid Build Coastguard Worker```
428