fix oauth code
[ccsdk/features.git] / sdnr / wt / oauth-provider / oauth-core / src / main / java / org / onap / ccsdk / features / sdnr / wt / oauthprovider / providers / PemUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers;
23
24 import java.io.File;
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;
40
41 public class PemUtils {
42
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()));
46         }
47         return parsePEMFile(new FileReader(pemFile));
48     }
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();
53         reader.close();
54         return content;
55     }
56     private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) {
57         PublicKey publicKey = null;
58         try {
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");
66         }
67
68         return publicKey;
69     }
70
71     private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) {
72         PrivateKey privateKey = null;
73         try {
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");
81         }
82
83         return privateKey;
84     }
85
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);
89     }
90
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);
94     }
95
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);
99     }
100
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);
104     }
105
106 }