18118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro/*
28118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * Copyright (C) 2016 The Android Open Source Project
38118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro *
48118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * Licensed under the Apache License, Version 2.0 (the "License");
58118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * you may not use this file except in compliance with the License.
68118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * You may obtain a copy of the License at
78118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro *
88118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro *      http://www.apache.org/licenses/LICENSE-2.0
98118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro *
108118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * Unless required by applicable law or agreed to in writing, software
118118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * distributed under the License is distributed on an "AS IS" BASIS,
128118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * See the License for the specific language governing permissions and
148118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro * limitations under the License
158118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro */
168118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
178118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giropackage libcore.sun.security.x509;
188118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
198118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giroimport junit.framework.TestCase;
208118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
218118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giroimport java.util.function.Function;
228118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
238118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giroimport sun.security.util.ObjectIdentifier;
248118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giroimport sun.security.x509.AlgorithmId;
258118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
268118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
278118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giropublic class AlgorithmIdTest extends TestCase {
288118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
298118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    public void test_get_String() throws Exception {
3038cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("1.3.14.3.2.26", AlgorithmId.get("SHA-1").getOID().toString());
3138cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("1.3.14.3.2.26", AlgorithmId.get("SHA1").getOID().toString());
328118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals("2.16.840.1.101.3.4.2.4", AlgorithmId.get("SHA-224").getOID().toString());
3338cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro
3438cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        // Would throw NoSuchAlgorithmException in N
358118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals("2.16.840.1.101.3.4.2.4", AlgorithmId.get("SHA224").getOID().toString());
3638cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro
3738cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("2.16.840.1.101.3.4.2.1", AlgorithmId.get("SHA-256").getOID().toString());
3838cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro
3938cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        // Would throw NoSuchAlgorithmException in N
4038cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("2.16.840.1.101.3.4.2.1", AlgorithmId.get("SHA256").getOID().toString());
4138cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro
428118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals(
438118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro                "2.16.840.1.101.3.4.3.1", AlgorithmId.get("SHA224WithDSA").getOID().toString());
448118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals(
458118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro                "2.16.840.1.101.3.4.3.2", AlgorithmId.get("SHA256WithDSA").getOID().toString());
468118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        // Case is irrelevant.
478118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals(
488118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro                "2.16.840.1.101.3.4.3.1", AlgorithmId.get("sHA224withDSA").getOID().toString());
498118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals(
508118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro                "2.16.840.1.101.3.4.3.2", AlgorithmId.get("sHA256withDSA").getOID().toString());
510e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro
520e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // Used to be 2.16.840.1.101.3.4.42 until N because BouncyCastle accepts this alias. It
530e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // started with a typo they once had and for compatibility they still support it. Since we
540e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // scan the aliases, we were picking it as the canonical OID for AES. See:
550e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // http://www.docjar.org/html/api/org/bouncycastle/jce/provider/symmetric/AESMappings.java.html
560e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        assertEquals("2.16.840.1.101.3.4.1", AlgorithmId.get("AES").getOID().toString());
570e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        assertEquals("1.3.132.1.12", AlgorithmId.get("ECDH").getOID().toString());
588118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    }
598118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
608118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    public void test_getName() throws Exception {
6138cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        // Was "SHA" in N
6238cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("SHA-1", getOidName("1.3.14.3.2.26"));
6338cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("SHA-224", getOidName("2.16.840.1.101.3.4.2.4"));
6438cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        // Was "SHA256" in N
6538cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        assertEquals("SHA-256", getOidName("2.16.840.1.101.3.4.2.1"));
6638cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro        // Were SHA224WITHDSA, etc in N
678118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals("SHA224withDSA", getOidName("2.16.840.1.101.3.4.3.1"));
688118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals("SHA256withDSA", getOidName("2.16.840.1.101.3.4.3.2"));
698118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        assertEquals("SHA224withRSA", getOidName("1.2.840.113549.1.1.14"));
7038cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro
710e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        assertEquals("AES", getOidName("2.16.840.1.101.3.4.1"));
720e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // AES is also the result of 2.16.840.1.101.3.4.42 because BouncyCastle accepts this alias.
730e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // It started with a typo they once had and for compatibility they still support it. Since
740e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // we scan the aliases, we were picking it. See:
750e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // http://www.docjar.org/html/api/org/bouncycastle/jce/provider/symmetric/AESMappings.java.html
760e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        assertEquals("AES", getOidName("2.16.840.1.101.3.4.42"));
770e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro
780e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        // ECDH not present before and in N
790e82007ff6bfabded43f388a3e9465466f6e9224Sergio Giro        assertEquals("ECDH", getOidName("1.3.132.1.12"));
808118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    }
818118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro
828118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    private String getOidName(String oid) throws Exception {
838118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro        return new AlgorithmId(new ObjectIdentifier(oid)).getName();
848118de3b2fc80daf5fc61c15ced9c84da4d02da7Sergio Giro    }
8538cdee0ef8b52ea8e1ea96103867d7abf57b48ebSergio Giro}