1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import java.io.ByteArrayInputStream;
20import java.math.BigInteger;
21import junit.framework.TestCase;
22
23public class OpenSSLKeyTest extends TestCase {
24    static final String RSA_PUBLIC_KEY =
25        "-----BEGIN PUBLIC KEY-----\n" +
26        "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOHsK2E2FLYfEMWEVH/rJMTqDZLLLysh\n" +
27        "AH5odcfhYdF9xvFFU9rqJT7zXUDH4SjdhZGUUAO5IOC1e8ZIyRsbiY0CAwEAAQ==\n" +
28        "-----END PUBLIC KEY-----";
29
30    static final String RSA_PRIVATE_KEY =
31        "-----BEGIN RSA PRIVATE KEY-----\n" +
32        "MIIBOgIBAAJBAOHsK2E2FLYfEMWEVH/rJMTqDZLLLyshAH5odcfhYdF9xvFFU9rq\n" +
33        "JT7zXUDH4SjdhZGUUAO5IOC1e8ZIyRsbiY0CAwEAAQJBALcu+oGJC0QcbknpIWbT\n" +
34        "L+4mZTkYXLeYu8DDTHT0j47+6eEyYBOoRGcZDdlMWquvFIrV48RSot0GPh1MBE1p\n" +
35        "lKECIQD4krM4UshCwUHH9ZVkoxcPsxzPTTW7ukky4RZVN6mgWQIhAOisOAXVVjon\n" +
36        "fbGNQ6CezH7oOttEeZmiWCu48AVCyixVAiAaDZ41OA//Vywi3i2jV6iyH47Ud347\n" +
37        "R+ImMAtcMTJZOQIgF0+Z1UvIdc8bErzad68xQc22h91WaYQQXWEL+xrz8nkCIDcA\n" +
38        "MpCP/H5qTCj/l5rxQg+/NUGCg2pHHNLL+cy5N5RM\n" +
39        "-----END RSA PRIVATE KEY-----";
40
41    static final BigInteger RSA_MODULUS = new BigInteger(
42        "e1ec2b613614b61f10c584547feb24c4ea0d92cb2f2b21007e6875c7e161d17d" +
43        "c6f14553daea253ef35d40c7e128dd8591945003b920e0b57bc648c91b1b898d", 16);
44
45    static final BigInteger RSA_PUBLIC_EXPONENT = new BigInteger("10001", 16);
46    static final BigInteger RSA_PRIVATE_EXPONENT = new BigInteger(
47        "b72efa81890b441c6e49e92166d32fee266539185cb798bbc0c34c74f48f8efe" +
48        "e9e1326013a84467190dd94c5aabaf148ad5e3c452a2dd063e1d4c044d6994a1", 16);
49
50    public void test_fromPublicKeyPemInputStream() throws Exception {
51        ByteArrayInputStream is = new ByteArrayInputStream(RSA_PUBLIC_KEY.getBytes());
52        OpenSSLKey key = OpenSSLKey.fromPublicKeyPemInputStream(is);
53        OpenSSLRSAPublicKey publicKey = (OpenSSLRSAPublicKey)key.getPublicKey();
54        assertEquals(RSA_MODULUS, publicKey.getModulus());
55        assertEquals(RSA_PUBLIC_EXPONENT, publicKey.getPublicExponent());
56    }
57
58    public void test_fromPrivateKeyPemInputStream() throws Exception {
59        ByteArrayInputStream is = new ByteArrayInputStream(RSA_PRIVATE_KEY.getBytes());
60        OpenSSLKey key = OpenSSLKey.fromPrivateKeyPemInputStream(is);
61        OpenSSLRSAPrivateKey privateKey = (OpenSSLRSAPrivateKey)key.getPrivateKey();
62        assertEquals(RSA_MODULUS, privateKey.getModulus());
63        assertEquals(RSA_PRIVATE_EXPONENT, privateKey.getPrivateExponent());
64    }
65}
66
67