1 package com.ca.commons.cbutil; 2 3 12 13 14 public class CBSecurity 15 { 16 17 21 24 25 public static final byte[] PEM_BEGIN = (new String ("-----BEGIN")).getBytes(); 26 27 30 public static final byte[] PEM_END = (new String ("-----END")).getBytes(); 31 32 35 36 public static final byte[] PEM_CERT_HEADER = new String ("-----BEGIN CERTIFICATE-----").getBytes(); 37 38 41 42 public static final byte[] PEM_CERT_FOOTER = new String ("-----END CERTIFICATE-----").getBytes(); 43 44 45 48 49 public static final byte[] PEM_ENC_KEY_HEADER = (new String ("-----BEGIN ENCRYPTED PRIVATE KEY-----")).getBytes(); 50 51 54 55 public static final byte[] PEM_KEY_HEADER = (new String ("-----BEGIN PRIVATE KEY-----")).getBytes(); 56 57 60 61 public static final byte[] PEM_ENC_KEY_FOOTER = (new String ("-----END ENCRYPTED PRIVATE KEY-----")).getBytes(); 62 63 66 67 public static final byte[] PEM_KEY_FOOTER = (new String ("-----END PRIVATE KEY-----")).getBytes(); 68 69 72 73 public static final byte[] PEM_RSA_KEY_HEADER = new String ("-----BEGIN RSA PRIVATE KEY-----").getBytes(); 74 75 78 79 public static final byte[] PEM_RSA_KEY_FOOTER = new String ("-----END RSA PRIVATE KEY-----").getBytes(); 80 81 88 89 public static int indexOf(byte[] mainArray, byte searchByte) 90 { 91 return indexOf(mainArray, searchByte, 0); 92 } 93 94 101 102 public static int indexOf(byte[] mainArray, byte searchByte, int fromIndex) 103 { 104 int len = mainArray.length; 105 106 108 if (fromIndex < 0) 109 { 110 fromIndex = 0; 111 } 112 else if (fromIndex >= len) 113 { 114 return -1; 115 } 116 117 119 for (int i = fromIndex; i < len; i++) 120 if (mainArray[i] == searchByte) return i; 121 122 return -1; } 124 125 138 139 public static int indexOf(byte[] mainArray, byte[] searchSequence) 140 { 141 return indexOf(mainArray, searchSequence, 0); 142 } 143 144 158 159 public static int indexOf(byte[] mainArray, byte[] searchSequence, int fromIndex) 160 { 161 byte v1[] = mainArray; 162 byte v2[] = searchSequence; 163 164 int max = mainArray.length; 165 166 if (fromIndex >= max) 168 { 169 if (mainArray.length == 0 && fromIndex == 0 && searchSequence.length == 0) 170 { 171 172 return 0; 173 } 174 return -1; } 176 177 178 if (fromIndex < 0) 180 { 181 fromIndex = 0; 182 } 183 184 if (searchSequence.length == 0) 186 { 187 return fromIndex; 188 } 189 190 byte first = v2[0]; 191 int i = fromIndex; 192 193 startSearchForFirstChar: 194 195 while (true) 196 { 197 198 199 while (i < max && v1[i] != first) 200 { 201 i++; 202 } 203 204 if (i >= max) { 206 return -1; 207 } 208 209 210 int j = i + 1; 211 int end = j + searchSequence.length - 1; 212 int k = 1; 213 while (j < end) 214 { 215 if (v1[j++] != v2[k++]) 216 { 217 i++; 218 219 continue startSearchForFirstChar; 220 } 221 } 222 return i; } 224 } 225 226 231 232 public static boolean isPEM(byte[] test) 233 { 234 235 if (indexOf(test, PEM_BEGIN) == -1) 236 return false; 238 if (indexOf(test, PEM_END) == -1) 239 return false; 241 return true; } 243 244 245 254 255 public static byte[] convertFromPEM(byte[] pem) 256 { 257 return convertFromPEM(pem, PEM_BEGIN, PEM_END); 258 } 259 260 277 278 public static byte[] convertFromPEM(byte[] pem, byte[] header) 279 { 280 return convertFromPEM(pem, header, PEM_END); 281 } 282 283 292 293 public static byte[] convertFromPEMCertificate(byte[] pem) 294 { 295 return convertFromPEM(pem, PEM_CERT_HEADER, PEM_END); 296 } 297 298 299 315 316 317 public static byte[] convertFromPEM(byte[] pem, byte[] header, byte[] footer) 318 { 319 int start, end; 320 321 start = indexOf(pem, header); 322 323 end = indexOf(pem, footer); 324 325 if (start == -1 || end == -1) return null; 327 start = indexOf(pem, (byte) '\n', start) + 1; 328 329 331 int next; 332 while ((next = indexOf(pem, (byte) '\n', start)) < start + 64) 333 { 334 if (next == -1) break; 337 start = next + 1; } 339 340 if (start == -1) return null; 342 343 int len = end - start; 344 345 byte[] data = new byte[len]; 346 347 System.arraycopy(pem, start, data, 0, len); 349 return CBBase64.decode(data); } 351 352 359 360 public static byte[] convertToPEMCertificate(byte[] der) 361 { 362 return convertToPEM(der, PEM_CERT_HEADER, PEM_CERT_FOOTER); 363 } 364 365 372 373 public static byte[] convertToPEMRSAPrivateKey(byte[] der) 374 { 375 376 return convertToPEM(der, PEM_RSA_KEY_HEADER, PEM_RSA_KEY_FOOTER); 377 } 378 379 387 388 public static byte[] convertToPEMEncryptedPrivateKey(byte[] der) 389 { 390 return convertToPEM(der, PEM_ENC_KEY_HEADER, PEM_ENC_KEY_FOOTER); 391 } 392 393 public static byte[] convertToPEMPrivateKey(byte[] der) 394 { 395 return convertToPEM(der, PEM_KEY_HEADER, PEM_KEY_FOOTER); 396 } 397 398 protected static byte[] convertToPEM(byte[] der, byte[] header, byte[] footer) 399 { 400 try 401 { 402 byte[] base64Data = CBBase64.encodeFormatted(der, 0, 64); 404 int len = header.length + 1 + base64Data.length + footer.length + 1; 406 byte[] pem = new byte[len]; 407 408 int pos = 0; 409 System.arraycopy(header, 0, pem, 0, header.length); 410 pos += header.length; 411 412 pem[pos++] = (byte) '\n'; 413 414 System.arraycopy(base64Data, 0, pem, pos, base64Data.length); 415 pos += base64Data.length; 416 417 System.arraycopy(footer, 0, pem, pos, footer.length); 418 pos += footer.length; 419 420 pem[pos] = (byte) '\n'; 421 422 base64Data = null; 423 der = null; 424 425 return pem; 426 } 427 catch (Exception e) 428 { 429 System.err.println("error decoding pem file: " + e); 430 return null; 431 } 432 } 433 } 434 | Popular Tags |