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.test;
24 import static org.junit.Assert.fail;
25 import com.auth0.jwt.JWT;
26 import com.auth0.jwt.algorithms.Algorithm;
27 import com.auth0.jwt.exceptions.JWTVerificationException;
28 import com.auth0.jwt.interfaces.JWTVerifier;
29 import java.io.IOException;
30 import java.security.Security;
31 import java.security.interfaces.RSAPrivateKey;
32 import java.security.interfaces.RSAPublicKey;
33 import java.util.Date;
34 import org.bouncycastle.jce.provider.BouncyCastleProvider;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers.RSAKeyReader;
44 public class TestRSAAlgorithms {
46 private static final String ISSUER = "jwttest";
47 private static final String SUBJECT = "meandmymonkey";
50 public static void init() {
52 new BouncyCastleProvider()
57 * private and public key were generated in ubuntu 20.04 with
58 * $ ssh-keygen -t rsa -b 4096 -m PEM -P "" -f jwtRS512.key
59 * $ openssl rsa -in jwtRS512.key -pubout -outform PEM -out jwtRS512.key.pub
62 public void testRSA512() {
63 RSAPrivateKey privKey = null;
64 RSAPublicKey pubKey = null;
66 privKey = RSAKeyReader.getPrivateKey("file://src/test/resources/jwtRS512.key");
67 pubKey = RSAKeyReader.getPublicKey("file://src/test/resources/jwtRS512.key.pub");
68 } catch (IOException e) {
72 verifyAlg(Algorithm.RSA512(pubKey, privKey));
76 * private and public key were generated in ubuntu 20.04 with
77 * $ openssl genrsa 2048 -out rsa-2048bit-jwtRS256.key
78 * $ openssl rsa -in jwtRS256.key -pubout > jwtRS256.key.pub
81 public void testRSA256() {
82 RSAPrivateKey privKey = null;
83 RSAPublicKey pubKey = null;
85 privKey = RSAKeyReader.getPrivateKey("file://src/test/resources/jwtRS256.key");
86 pubKey = RSAKeyReader.getPublicKey("file://src/test/resources/jwtRS256.key.pub");
87 } catch (IOException e) {
91 verifyAlg(Algorithm.RSA512(pubKey, privKey));
94 private static void verifyAlg(Algorithm a) {
95 long now = new Date().getTime();
96 final String token = JWT.create().withIssuer(ISSUER).withExpiresAt(new Date(now+10000))
97 .withIssuedAt(new Date(now))
101 JWTVerifier verifier = JWT.require(a).withIssuer(ISSUER).build();
102 verifier.verify(token);
104 } catch (JWTVerificationException e) {
105 fail(e.getMessage());