2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers;
25 import java.io.FileNotFoundException;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.security.KeyFactory;
31 import java.security.NoSuchAlgorithmException;
32 import java.security.PrivateKey;
33 import java.security.PublicKey;
34 import java.security.spec.EncodedKeySpec;
35 import java.security.spec.InvalidKeySpecException;
36 import java.security.spec.PKCS8EncodedKeySpec;
37 import java.security.spec.X509EncodedKeySpec;
38 import org.bouncycastle.util.io.pem.PemObject;
39 import org.bouncycastle.util.io.pem.PemReader;
41 public class PemUtils {
43 private static byte[] parsePEMFile(File pemFile) throws IOException {
44 if (!pemFile.isFile() || !pemFile.exists()) {
45 throw new FileNotFoundException(String.format("The file '%s' doesn't exist.", pemFile.getAbsolutePath()));
47 return parsePEMFile(new FileReader(pemFile));
49 private static byte[] parsePEMFile(Reader inputReader) throws IOException {
50 PemReader reader = new PemReader(inputReader);
51 PemObject pemObject = reader.readPemObject();
52 byte[] content = pemObject.getContent();
56 private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) {
57 PublicKey publicKey = null;
59 KeyFactory kf = KeyFactory.getInstance(algorithm);
60 EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
61 publicKey = kf.generatePublic(keySpec);
62 } catch (NoSuchAlgorithmException e) {
63 System.out.println("Could not reconstruct the public key, the given algorithm could not be found.");
64 } catch (InvalidKeySpecException e) {
65 System.out.println("Could not reconstruct the public key");
71 private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) {
72 PrivateKey privateKey = null;
74 KeyFactory kf = KeyFactory.getInstance(algorithm);
75 EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
76 privateKey = kf.generatePrivate(keySpec);
77 } catch (NoSuchAlgorithmException e) {
78 System.out.println("Could not reconstruct the private key, the given algorithm could not be found.");
79 } catch (InvalidKeySpecException e) {
80 System.out.println("Could not reconstruct the private key");
86 public static PublicKey readPublicKeyFromFile(String filepath, String algorithm) throws IOException {
87 byte[] bytes = PemUtils.parsePEMFile(new File(filepath));
88 return PemUtils.getPublicKey(bytes, algorithm);
91 public static PublicKey readPublicKey(String filecontent, String algorithm) throws IOException {
92 byte[] bytes = PemUtils.parsePEMFile(new StringReader(filecontent));
93 return PemUtils.getPublicKey(bytes, algorithm);
96 public static PrivateKey readPrivateKeyFromFile(String filepath, String algorithm) throws IOException {
97 byte[] bytes = PemUtils.parsePEMFile(new File(filepath));
98 return PemUtils.getPrivateKey(bytes, algorithm);
101 public static PrivateKey readPrivateKey(String filecontent, String algorithm) throws IOException {
102 byte[] bytes = PemUtils.parsePEMFile(new StringReader(filecontent));
103 return PemUtils.getPrivateKey(bytes, algorithm);