|
| 1 | +package com.auth0.jwt.algorithms; |
| 2 | +// Copyright (c) 2017 The Authors of 'JWTS for Java' |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +// this software and associated documentation files (the "Software"), to deal in |
| 6 | +// the Software without restriction, including without limitation the rights to |
| 7 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | +// subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +import org.bouncycastle.util.io.pem.PemObject; |
| 22 | +import org.bouncycastle.util.io.pem.PemReader; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileNotFoundException; |
| 26 | +import java.io.FileReader; |
| 27 | +import java.io.IOException; |
| 28 | +import java.security.KeyFactory; |
| 29 | +import java.security.NoSuchAlgorithmException; |
| 30 | +import java.security.PrivateKey; |
| 31 | +import java.security.PublicKey; |
| 32 | +import java.security.spec.EncodedKeySpec; |
| 33 | +import java.security.spec.InvalidKeySpecException; |
| 34 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 35 | +import java.security.spec.X509EncodedKeySpec; |
| 36 | + |
| 37 | +import org.bouncycastle.util.io.pem.PemObject; |
| 38 | +import org.bouncycastle.util.io.pem.PemReader; |
| 39 | + |
| 40 | +import java.io.File; |
| 41 | +import java.io.FileNotFoundException; |
| 42 | +import java.io.FileReader; |
| 43 | +import java.io.IOException; |
| 44 | +import java.security.KeyFactory; |
| 45 | +import java.security.NoSuchAlgorithmException; |
| 46 | +import java.security.PrivateKey; |
| 47 | +import java.security.PublicKey; |
| 48 | +import java.security.spec.EncodedKeySpec; |
| 49 | +import java.security.spec.InvalidKeySpecException; |
| 50 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 51 | +import java.security.spec.X509EncodedKeySpec; |
| 52 | + |
| 53 | +public class PemUtils { |
| 54 | + |
| 55 | + private static byte[] parsePEMFile(File pemFile) throws IOException { |
| 56 | + if (!pemFile.isFile() || !pemFile.exists()) { |
| 57 | + throw new FileNotFoundException(String.format("The file '%s' doesn't exist.", pemFile.getAbsolutePath())); |
| 58 | + } |
| 59 | + PemReader reader = new PemReader(new FileReader(pemFile)); |
| 60 | + PemObject pemObject = reader.readPemObject(); |
| 61 | + byte[] content = pemObject.getContent(); |
| 62 | + reader.close(); |
| 63 | + return content; |
| 64 | + } |
| 65 | + |
| 66 | + private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) { |
| 67 | + PublicKey publicKey = null; |
| 68 | + try { |
| 69 | + KeyFactory kf = KeyFactory.getInstance(algorithm); |
| 70 | + EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
| 71 | + publicKey = kf.generatePublic(keySpec); |
| 72 | + } catch (NoSuchAlgorithmException e) { |
| 73 | + System.out.println("Could not reconstruct the public key, the given algorithm could not be found."); |
| 74 | + } catch (InvalidKeySpecException e) { |
| 75 | + System.out.println("Could not reconstruct the public key"); |
| 76 | + } |
| 77 | + |
| 78 | + return publicKey; |
| 79 | + } |
| 80 | + |
| 81 | + private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) { |
| 82 | + PrivateKey privateKey = null; |
| 83 | + try { |
| 84 | + KeyFactory kf = KeyFactory.getInstance(algorithm); |
| 85 | + EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
| 86 | + privateKey = kf.generatePrivate(keySpec); |
| 87 | + } catch (NoSuchAlgorithmException e) { |
| 88 | + System.out.println("Could not reconstruct the private key, the given algorithm could not be found."); |
| 89 | + } catch (InvalidKeySpecException e) { |
| 90 | + System.out.println("Could not reconstruct the private key"); |
| 91 | + } |
| 92 | + |
| 93 | + return privateKey; |
| 94 | + } |
| 95 | + |
| 96 | + public static PublicKey readPublicKeyFromFile(String filepath, String algorithm) throws IOException { |
| 97 | + byte[] bytes = PemUtils.parsePEMFile(new File(filepath)); |
| 98 | + return PemUtils.getPublicKey(bytes, algorithm); |
| 99 | + } |
| 100 | + |
| 101 | + public static PrivateKey readPrivateKeyFromFile(String filepath, String algorithm) throws IOException { |
| 102 | + byte[] bytes = PemUtils.parsePEMFile(new File(filepath)); |
| 103 | + return PemUtils.getPrivateKey(bytes, algorithm); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments