16
16
17
17
package io .grpc .xds .internal .security .trust ;
18
18
19
- import io .netty .buffer .ByteBuf ;
20
- import io .netty .buffer .Unpooled ;
21
- import io .netty .handler .codec .base64 .Base64 ;
22
- import io .netty .util .CharsetUtil ;
23
19
import java .io .BufferedInputStream ;
24
- import java .io .ByteArrayOutputStream ;
25
20
import java .io .File ;
26
21
import java .io .FileInputStream ;
27
22
import java .io .IOException ;
28
23
import java .io .InputStream ;
29
- import java .io .OutputStream ;
30
- import java .security .KeyException ;
31
- import java .security .KeyFactory ;
32
24
import java .security .PrivateKey ;
33
- import java .security .cert .Certificate ;
34
25
import java .security .cert .CertificateException ;
35
- import java .security .cert .CertificateFactory ;
36
26
import java .security .cert .X509Certificate ;
37
- import java .security .spec .PKCS8EncodedKeySpec ;
38
- import java .util .Collection ;
39
- import java .util .logging .Level ;
40
- import java .util .logging .Logger ;
41
- import java .util .regex .Matcher ;
42
- import java .util .regex .Pattern ;
43
27
44
28
/**
45
29
* Contains certificate utility method(s).
46
30
*/
47
31
public final class CertificateUtils {
48
- private static final Logger logger = Logger .getLogger (CertificateUtils .class .getName ());
49
-
50
- private static CertificateFactory factory ;
51
- private static final Pattern KEY_PATTERN = Pattern .compile (
52
- "-+BEGIN\\ s+.*PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" // Header
53
- + "([a-z0-9+/=\\ r\\ n]+)" // Base64 text
54
- + "-+END\\ s+.*PRIVATE\\ s+KEY[^-]*-+" , // Footer
55
- Pattern .CASE_INSENSITIVE );
56
-
57
- private static synchronized void initInstance () throws CertificateException {
58
- if (factory == null ) {
59
- factory = CertificateFactory .getInstance ("X.509" );
60
- }
61
- }
62
-
63
32
/**
64
33
* Generates X509Certificate array from a file on disk.
65
34
*
@@ -73,72 +42,15 @@ static X509Certificate[] toX509Certificates(File file) throws CertificateExcepti
73
42
}
74
43
75
44
/** Generates X509Certificate array from the {@link InputStream}. */
76
- public static synchronized X509Certificate [] toX509Certificates (InputStream inputStream )
45
+ public static X509Certificate [] toX509Certificates (InputStream inputStream )
77
46
throws CertificateException , IOException {
78
- initInstance ();
79
- Collection <? extends Certificate > certs = factory .generateCertificates (inputStream );
80
- return certs .toArray (new X509Certificate [0 ]);
81
-
82
- }
83
-
84
- /** See {@link CertificateFactory#generateCertificate(InputStream)}. */
85
- public static synchronized X509Certificate toX509Certificate (InputStream inputStream )
86
- throws CertificateException , IOException {
87
- initInstance ();
88
- Certificate cert = factory .generateCertificate (inputStream );
89
- return (X509Certificate ) cert ;
47
+ return io .grpc .util .CertificateUtils .getX509Certificates (inputStream );
90
48
}
91
49
92
50
/** Generates a {@link PrivateKey} from the {@link InputStream}. */
93
51
public static PrivateKey getPrivateKey (InputStream inputStream )
94
52
throws Exception {
95
- ByteBuf encodedKeyBuf = readPrivateKey (inputStream );
96
- byte [] encodedKey = new byte [encodedKeyBuf .readableBytes ()];
97
- encodedKeyBuf .readBytes (encodedKey ).release ();
98
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (encodedKey );
99
- return KeyFactory .getInstance ("RSA" ).generatePrivate (spec );
100
- }
101
-
102
- private static ByteBuf readPrivateKey (InputStream in ) throws KeyException {
103
- String content ;
104
- try {
105
- content = readContent (in );
106
- } catch (IOException e ) {
107
- throw new KeyException ("failed to read key input stream" , e );
108
- }
109
- Matcher m = KEY_PATTERN .matcher (content );
110
- if (!m .find ()) {
111
- throw new KeyException ("could not find a PKCS #8 private key in input stream" );
112
- }
113
- ByteBuf base64 = Unpooled .copiedBuffer (m .group (1 ), CharsetUtil .US_ASCII );
114
- ByteBuf der = Base64 .decode (base64 );
115
- base64 .release ();
116
- return der ;
117
- }
118
-
119
- private static String readContent (InputStream in ) throws IOException {
120
- ByteArrayOutputStream out = new ByteArrayOutputStream ();
121
- try {
122
- byte [] buf = new byte [8192 ];
123
- for (; ; ) {
124
- int ret = in .read (buf );
125
- if (ret < 0 ) {
126
- break ;
127
- }
128
- out .write (buf , 0 , ret );
129
- }
130
- return out .toString (CharsetUtil .US_ASCII .name ());
131
- } finally {
132
- safeClose (out );
133
- }
134
- }
135
-
136
- private static void safeClose (OutputStream out ) {
137
- try {
138
- out .close ();
139
- } catch (IOException e ) {
140
- logger .log (Level .WARNING , "Failed to close a stream." , e );
141
- }
53
+ return io .grpc .util .CertificateUtils .getPrivateKey (inputStream );
142
54
}
143
55
144
56
private CertificateUtils () {}
0 commit comments