OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
9 import android.util.Log; | 9 import android.util.Log; |
10 | 10 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 return null; | 108 return null; |
109 } | 109 } |
110 } catch (Exception e) { | 110 } catch (Exception e) { |
111 Log.w(TAG, "Could not read key from '" + file + "': " + e); | 111 Log.w(TAG, "Could not read key from '" + file + "': " + e); |
112 return null; | 112 return null; |
113 } finally { | 113 } finally { |
114 try { | 114 try { |
115 if (input != null) { | 115 if (input != null) { |
116 input.close(); | 116 input.close(); |
117 } | 117 } |
118 } catch (Exception e) { | 118 } catch (IOException e) { |
119 Log.e(TAG, "Could not close key input stream '" + file + "': " +
e); | 119 Log.e(TAG, "Could not close key input stream '" + file + "': " +
e); |
120 } | 120 } |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 private static boolean writeKeyToFile(Context context, String basename, Secr
etKey key) { | 124 private static boolean writeKeyToFile(Context context, String basename, Secr
etKey key) { |
125 File file = context.getFileStreamPath(basename); | 125 File file = context.getFileStreamPath(basename); |
126 byte[] keyBytes = key.getEncoded(); | 126 byte[] keyBytes = key.getEncoded(); |
| 127 FileOutputStream output = null; |
127 if (MAC_KEY_BYTE_COUNT != keyBytes.length) { | 128 if (MAC_KEY_BYTE_COUNT != keyBytes.length) { |
128 Log.e(TAG, "writeKeyToFile got key encoded bytes length " + keyBytes
.length + | 129 Log.e(TAG, "writeKeyToFile got key encoded bytes length " + keyBytes
.length + |
129 "; expected " + MAC_KEY_BYTE_COUNT); | 130 "; expected " + MAC_KEY_BYTE_COUNT); |
130 return false; | 131 return false; |
131 } | 132 } |
132 | 133 |
133 try { | 134 try { |
134 FileOutputStream output = new FileOutputStream(file); | 135 output = new FileOutputStream(file); |
135 output.write(keyBytes); | 136 output.write(keyBytes); |
136 output.close(); | |
137 return true; | 137 return true; |
138 } catch (Exception e) { | 138 } catch (Exception e) { |
139 Log.e(TAG, "Could not write key to '" + file + "': " + e); | 139 Log.e(TAG, "Could not write key to '" + file + "': " + e); |
140 return false; | 140 return false; |
| 141 } finally { |
| 142 try { |
| 143 if (output != null) { |
| 144 output.close(); |
| 145 } |
| 146 } catch (IOException e) { |
| 147 Log.e(TAG, "Could not close key output stream '" + file + "': "
+ e); |
| 148 } |
141 } | 149 } |
142 } | 150 } |
143 | 151 |
144 private static SecretKey getKey(Context context) { | 152 private static SecretKey getKey(Context context) { |
145 synchronized (sLock) { | 153 synchronized (sLock) { |
146 if (sKey == null) { | 154 if (sKey == null) { |
147 SecretKey key = readKeyFromFile(context, MAC_KEY_BASENAME, MAC_A
LGORITHM_NAME); | 155 SecretKey key = readKeyFromFile(context, MAC_KEY_BASENAME, MAC_A
LGORITHM_NAME); |
148 if (key != null) { | 156 if (key != null) { |
149 sKey = key; | 157 sKey = key; |
150 return sKey; | 158 return sKey; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 250 } |
243 Mac mac = Mac.getInstance(MAC_ALGORITHM_NAME); | 251 Mac mac = Mac.getInstance(MAC_ALGORITHM_NAME); |
244 mac.init(key); | 252 mac.init(key); |
245 return mac; | 253 return mac; |
246 } catch (GeneralSecurityException e) { | 254 } catch (GeneralSecurityException e) { |
247 Log.w(TAG, "Error in creating MAC instance", e); | 255 Log.w(TAG, "Error in creating MAC instance", e); |
248 return null; | 256 return null; |
249 } | 257 } |
250 } | 258 } |
251 } | 259 } |
OLD | NEW |