• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19758

02 Apr 2025 04:40AM UTC coverage: 88.103% (-0.5%) from 88.593%
#19758

push

github

web-flow
okhttp: Per-rpc call option authority verification (#11754)

30021 of 34075 relevant lines covered (88.1%)

1.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/../core/src/main/java/io/grpc/internal/CertificateUtils.java
1
/*
2
 * Copyright 2024 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.internal;
18

19
import java.io.ByteArrayInputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.security.GeneralSecurityException;
23
import java.security.KeyStore;
24
import java.security.cert.Certificate;
25
import java.security.cert.CertificateException;
26
import java.security.cert.CertificateFactory;
27
import java.security.cert.X509Certificate;
28
import java.util.Collection;
29
import javax.net.ssl.TrustManager;
30
import javax.net.ssl.TrustManagerFactory;
31
import javax.security.auth.x500.X500Principal;
32

33
/**
34
 * Contains certificate/key PEM file utility method(s) for internal usage.
35
 */
36
public final class CertificateUtils {
×
37
  /**
38
   * Creates X509TrustManagers using the provided CA certs.
39
   */
40
  public static TrustManager[] createTrustManager(byte[] rootCerts)
41
      throws GeneralSecurityException {
42
    InputStream rootCertsStream = new ByteArrayInputStream(rootCerts);
1✔
43
    try {
44
      return CertificateUtils.createTrustManager(rootCertsStream);
1✔
45
    } finally {
46
      GrpcUtil.closeQuietly(rootCertsStream);
1✔
47
    }
48
  }
49

50
  /**
51
   * Creates X509TrustManagers using the provided input stream of CA certs.
52
   */
53
  public static TrustManager[] createTrustManager(InputStream rootCerts)
54
          throws GeneralSecurityException {
55
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
1✔
56
    try {
57
      ks.load(null, null);
1✔
58
    } catch (IOException ex) {
×
59
      // Shouldn't really happen, as we're not loading any data.
60
      throw new GeneralSecurityException(ex);
×
61
    }
1✔
62
    X509Certificate[] certs = CertificateUtils.getX509Certificates(rootCerts);
1✔
63
    for (X509Certificate cert : certs) {
1✔
64
      X500Principal principal = cert.getSubjectX500Principal();
1✔
65
      ks.setCertificateEntry(principal.getName("RFC2253"), cert);
1✔
66
    }
67

68
    TrustManagerFactory trustManagerFactory =
69
            TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
1✔
70
    trustManagerFactory.init(ks);
1✔
71
    return trustManagerFactory.getTrustManagers();
1✔
72
  }
73

74
  private static X509Certificate[] getX509Certificates(InputStream inputStream)
75
          throws CertificateException {
76
    CertificateFactory factory = CertificateFactory.getInstance("X.509");
1✔
77
    Collection<? extends Certificate> certs = factory.generateCertificates(inputStream);
1✔
78
    return certs.toArray(new X509Certificate[0]);
1✔
79
  }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc