2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.cm.test;
25 import java.math.BigInteger;
26 import java.security.KeyPair;
27 import java.security.PrivateKey;
28 import java.security.PublicKey;
29 import java.security.cert.Certificate;
30 import java.security.cert.X509Certificate;
31 import java.security.interfaces.RSAPrivateKey;
32 import java.security.interfaces.RSAPublicKey;
33 import java.util.Collection;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import static org.junit.Assert.fail;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.cadi.cm.Factory;
43 public class JU_SignTest {
46 public static void tearDownAfterClass() throws Exception {
50 public void test() throws Exception {
51 AuthzEnv env = new AuthzEnv();
52 AuthzTrans trans = env.newTrans();
53 KeyPair kpair = Factory.generateKeyPair(trans);
54 PrivateKey privateKey = kpair.getPrivate();
55 String privateString = Factory.toString(trans, privateKey);
56 System.out.println("Private as base64 encoded as PKCS8 Spec");
57 System.out.println(privateString);
59 PublicKey publicKey = kpair.getPublic();
60 String publicString = Factory.toString(trans, publicKey);
61 System.out.println("public as base64 encoded as PKCS8 Spec");
62 System.out.println(publicString);
64 byte data[] = "Sign this please.".getBytes();
65 byte sig[] = Factory.sign(trans, data, privateKey);
66 System.out.println("Signature");
67 System.out.println(Factory.toSignatureString(sig));
69 Assert.assertTrue(Factory.verify(trans, data, sig, publicKey));
73 // public void test2() throws Exception {
74 // AuthzEnv env = new AuthzEnv();
75 // AuthzTrans trans = env.newTrans();
76 // File key = new File("/opt/app/aaf/common/com.att.aaf.key");
77 // PrivateKey privKey = Factory.toPrivateKey(trans, key);
78 // RSAPrivateKey rPrivKey = (RSAPrivateKey)privKey;
79 // BigInteger privMod, pubMod;
80 // System.out.println((privMod = rPrivKey.getModulus()).toString(16));
82 // byte data[] = "Sign this please.".getBytes();
83 // byte sig[] = Factory.sign(trans, data, privKey);
84 // System.out.println("Signature");
85 // System.out.println(Factory.toSignatureString(sig));
88 // File crt = new File("/opt/app/aaf/common/com.att.aaf.crt");
89 // Collection<? extends Certificate> x509s = Factory.toX509Certificate(trans, crt);
90 // X509Certificate cert = null;
91 // for(Certificate c : x509s) {
92 // cert = (X509Certificate)c;
95 // PublicKey pubKey = cert.getPublicKey();
96 // RSAPublicKey rPubKey = (RSAPublicKey)pubKey;
98 // System.out.println((pubMod = rPubKey.getModulus()).toString(16));
100 // Assert.assertTrue(Factory.verify(trans, data, sig, pubKey));
101 // Assert.assertEquals(privMod,pubMod);
105 @Test //TODO: Temporary fix AAF-111
106 public void netYetTested() {
107 fail("Tests not yet implemented");