@@ -68,34 +68,44 @@ static ByteBuf[] readCertificates(InputStream in) throws CertificateException { |
68 | 68 | List<ByteBuf> certs = new ArrayList<ByteBuf>(); |
69 | 69 | Matcher m = CERT_HEADER.matcher(content); |
70 | 70 | int start = 0; |
71 | | - for (;;) { |
72 | | - if (!m.find(start)) { |
73 | | - break; |
74 | | - } |
75 | | - |
76 | | - // Here and below it's necessary to save the position as it is reset |
77 | | - // after calling usePattern() on Android due to a bug. |
78 | | - // |
79 | | - // See https://issuetracker.google.com/issues/293206296 |
80 | | - start = m.end(); |
81 | | - m.usePattern(BODY); |
82 | | - if (!m.find(start)) { |
83 | | - break; |
| 71 | + try { |
| 72 | + for (;;) { |
| 73 | + if (!m.find(start)) { |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + // Here and below it's necessary to save the position as it is reset |
| 78 | + // after calling usePattern() on Android due to a bug. |
| 79 | + // |
| 80 | + // See https://issuetracker.google.com/issues/293206296 |
| 81 | + start = m.end(); |
| 82 | + m.usePattern(BODY); |
| 83 | + if (!m.find(start)) { |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + ByteBuf base64 = Unpooled.copiedBuffer(m.group(0), CharsetUtil.US_ASCII); |
| 88 | + try { |
| 89 | + start = m.end(); |
| 90 | + m.usePattern(CERT_FOOTER); |
| 91 | + if (!m.find(start)) { |
| 92 | + // Certificate is incomplete. |
| 93 | + break; |
| 94 | + } |
| 95 | + ByteBuf der = Base64.decode(base64); |
| 96 | + certs.add(der); |
| 97 | + } finally { |
| 98 | + base64.release(); |
| 99 | + } |
| 100 | + |
| 101 | + start = m.end(); |
| 102 | + m.usePattern(CERT_HEADER); |
84 | 103 | } |
85 | | - |
86 | | - ByteBuf base64 = Unpooled.copiedBuffer(m.group(0), CharsetUtil.US_ASCII); |
87 | | - start = m.end(); |
88 | | - m.usePattern(CERT_FOOTER); |
89 | | - if (!m.find(start)) { |
90 | | - // Certificate is incomplete. |
91 | | - break; |
| 104 | + } catch (Throwable e) { |
| 105 | + for (ByteBuf cert : certs) { |
| 106 | + cert.release(); |
92 | 107 | } |
93 | | - ByteBuf der = Base64.decode(base64); |
94 | | - base64.release(); |
95 | | - certs.add(der); |
96 | | - |
97 | | - start = m.end(); |
98 | | - m.usePattern(CERT_HEADER); |
| 108 | + throw e; |
99 | 109 | } |
100 | 110 |
|
101 | 111 | if (certs.isEmpty()) { |
@@ -132,15 +142,17 @@ static ByteBuf readPrivateKey(InputStream in) throws KeyException { |
132 | 142 | } |
133 | 143 |
|
134 | 144 | ByteBuf base64 = Unpooled.copiedBuffer(m.group(0), CharsetUtil.US_ASCII); |
135 | | - start = m.end(); |
136 | | - m.usePattern(KEY_FOOTER); |
137 | | - if (!m.find(start)) { |
138 | | - // Key is incomplete. |
139 | | - throw keyNotFoundException(); |
| 145 | + try { |
| 146 | + start = m.end(); |
| 147 | + m.usePattern(KEY_FOOTER); |
| 148 | + if (!m.find(start)) { |
| 149 | + // Key is incomplete. |
| 150 | + throw keyNotFoundException(); |
| 151 | + } |
| 152 | + return Base64.decode(base64); |
| 153 | + } finally { |
| 154 | + base64.release(); |
140 | 155 | } |
141 | | - ByteBuf der = Base64.decode(base64); |
142 | | - base64.release(); |
143 | | - return der; |
144 | 156 | } |
145 | 157 |
|
146 | 158 | private static KeyException keyNotFoundException() { |
|