1f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson/*
2f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Copyright (C) 2009 The Android Open Source Project
3f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
4f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * you may not use this file except in compliance with the License.
6f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * You may obtain a copy of the License at
7f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
8f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
9f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
10f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * See the License for the specific language governing permissions and
14f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * limitations under the License.
15f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson */
16f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
17f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpackage tests.security;
18f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
19f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.InvalidKeyException;
20f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.KeyPair;
21f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.NoSuchAlgorithmException;
22f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.PrivateKey;
23f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.PublicKey;
24f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.Signature;
25f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.SignatureException;
26f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport junit.framework.Assert;
27f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
28f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpublic class SignatureHelper extends TestHelper<KeyPair> {
29f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
30f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String algorithmName;
31f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String plainData = "some data do sign and verify";
32f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
33f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public SignatureHelper(String algorithmName) {
34f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.algorithmName = algorithmName;
35f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
36f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
37f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    @Override
38f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public void test(KeyPair keyPair) {
39f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        test(keyPair.getPrivate(), keyPair.getPublic());
40f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
41f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
42f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public void test(PrivateKey encryptKey, PublicKey decryptKey) {
43f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
44f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        Signature signature = null;
45f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
46f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signature = Signature.getInstance(algorithmName);
47f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (NoSuchAlgorithmException e) {
48f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
49f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
50f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
51f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
52f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signature.initSign(encryptKey);
53f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (InvalidKeyException e) {
54f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
55f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
56f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
57f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
58f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signature.update(plainData.getBytes());
59f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (SignatureException e) {
60f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
61f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
62f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
63f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        byte[] signed = null;
64f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
65f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signed = signature.sign();
66f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (SignatureException e) {
67f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
68f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
69f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
70f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
71f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signature.initVerify(decryptKey);
72f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (InvalidKeyException e) {
73f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
74f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
75f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
76f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
77f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            signature.update(plainData.getBytes());
78f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (SignatureException e) {
79f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
80f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
81f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
82f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
83f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.assertTrue("signature could not be verified", signature
84f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson                    .verify(signed));
85f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (SignatureException e) {
86f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
87f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
88f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
89f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson}
90