/
PemReader.java
169 lines (152 loc) · 5.42 KB
/
PemReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2013 Google Inc.
*
* 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 com.google.api.client.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* {@link Beta} <br>
* PEM file reader.
*
* <p>Supports reading any PEM stream that contains Base64 encoded content stored inside {@code
* "-----BEGIN ...-----"} and {@code "-----END ...-----"} tags. Each call to {@link
* #readNextSection()} parses the next section in the PEM file. If you need a section of a certain
* title use {@link #readNextSection(String)}, for example {@code readNextSection("PRIVATE KEY")}.
* To ensure that the stream is closed properly, call {@link #close()} in a finally block.
*
* <p>As a convenience, use {@link #readFirstSectionAndClose(Reader)} or {@link
* #readFirstSectionAndClose(Reader, String)} for the common case of only a single section in a PEM
* file (or only a single section of a given title).
*
* <p>Limitations:
*
* <p><ul>
* <li>Assumes the PEM file section content is not encrypted and cannot handle the case of any
* headers inside the BEGIN and END tag.
* <li>It also ignores any attributes associated with any PEM file section.
* </ul>
*
* @since 1.14
* @author Yaniv Inbar
*/
@Beta
public final class PemReader {
private static final Pattern BEGIN_PATTERN = Pattern.compile("-----BEGIN ([A-Z ]+)-----");
private static final Pattern END_PATTERN = Pattern.compile("-----END ([A-Z ]+)-----");
/** Reader. */
private BufferedReader reader;
/** @param reader reader */
public PemReader(Reader reader) {
this.reader = new BufferedReader(reader);
}
/** Reads the next section in the PEM file or {@code null} for end of file. */
public Section readNextSection() throws IOException {
return readNextSection(null);
}
/**
* Reads the next section in the PEM file, optionally based on a title to look for.
*
* @param titleToLookFor title to look for or {@code null} for any title
* @return next section or {@code null} for end of file
*/
public Section readNextSection(String titleToLookFor) throws IOException {
String title = null;
StringBuilder keyBuilder = null;
while (true) {
String line = reader.readLine();
if (line == null) {
Preconditions.checkArgument(title == null, "missing end tag (%s)", title);
return null;
}
if (keyBuilder == null) {
Matcher m = BEGIN_PATTERN.matcher(line);
if (m.matches()) {
String curTitle = m.group(1);
if (titleToLookFor == null || curTitle.equals(titleToLookFor)) {
keyBuilder = new StringBuilder();
title = curTitle;
}
}
} else {
Matcher m = END_PATTERN.matcher(line);
if (m.matches()) {
String endTitle = m.group(1);
Preconditions.checkArgument(
endTitle.equals(title), "end tag (%s) doesn't match begin tag (%s)", endTitle, title);
return new Section(title, Base64.decodeBase64(keyBuilder.toString()));
}
keyBuilder.append(line);
}
}
}
/**
* Reads the first section in the PEM file, and then closes the reader.
*
* @param reader reader
* @return first section found or {@code null} for none found
*/
public static Section readFirstSectionAndClose(Reader reader) throws IOException {
return readFirstSectionAndClose(reader, null);
}
/**
* Reads the first section in the PEM file, optionally based on a title to look for, and then
* closes the reader.
*
* @param titleToLookFor title to look for or {@code null} for any title
* @param reader reader
* @return first section found or {@code null} for none found
*/
public static Section readFirstSectionAndClose(Reader reader, String titleToLookFor)
throws IOException {
PemReader pemReader = new PemReader(reader);
try {
return pemReader.readNextSection(titleToLookFor);
} finally {
pemReader.close();
}
}
/**
* Closes the reader.
*
* <p>To ensure that the stream is closed properly, call {@link #close()} in a finally block.
*/
public void close() throws IOException {
reader.close();
}
/** Section in the PEM file. */
public static final class Section {
/** Title. */
private final String title;
/** Base64-decoded bytes. */
private final byte[] base64decodedBytes;
/**
* @param title title
* @param base64decodedBytes base64-decoded bytes
*/
Section(String title, byte[] base64decodedBytes) {
this.title = Preconditions.checkNotNull(title);
this.base64decodedBytes = Preconditions.checkNotNull(base64decodedBytes);
}
/** Returns the title. */
public String getTitle() {
return title;
}
/** Returns the base64-decoded bytes (modifiable array). */
public byte[] getBase64DecodedBytes() {
return base64decodedBytes;
}
}
}