1 /* 2 * Copyright (C) 2021 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 17 package com.android.remoteprovisioner.unittest; 18 19 import static com.android.remoteprovisioner.unittest.Utils.generateEcdsaKeyPair; 20 import static com.android.remoteprovisioner.unittest.Utils.getP256PubKeyFromBytes; 21 import static com.android.remoteprovisioner.unittest.Utils.signPublicKey; 22 23 import static org.junit.Assert.assertArrayEquals; 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertTrue; 26 27 import android.platform.test.annotations.Presubmit; 28 29 import androidx.test.runner.AndroidJUnit4; 30 31 import com.android.remoteprovisioner.X509Utils; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.io.ByteArrayOutputStream; 37 import java.security.KeyPair; 38 import java.security.cert.X509Certificate; 39 40 @RunWith(AndroidJUnit4.class) 41 public class X509UtilsTest { 42 43 @Presubmit 44 @Test testFormatX509Certs()45 public void testFormatX509Certs() throws Exception { 46 KeyPair root = generateEcdsaKeyPair(); 47 KeyPair intermediate = generateEcdsaKeyPair(); 48 KeyPair leaf = generateEcdsaKeyPair(); 49 X509Certificate[] certs = new X509Certificate[3]; 50 certs[2] = signPublicKey(root, root.getPublic()); 51 certs[1] = signPublicKey(root, intermediate.getPublic()); 52 certs[0] = signPublicKey(intermediate, leaf.getPublic()); 53 ByteArrayOutputStream os = new ByteArrayOutputStream(); 54 for (int i = 0; i < certs.length; i++) { 55 os.write(certs[i].getEncoded()); 56 } 57 X509Certificate[] roundTrip = X509Utils.formatX509Certs(os.toByteArray()); 58 assertEquals(certs.length, roundTrip.length); 59 for (int i = 0; i < certs.length; i++) { 60 assertArrayEquals("Failed on index " + i, 61 certs[i].getEncoded(), roundTrip[i].getEncoded()); 62 } 63 } 64 65 @Presubmit 66 @Test testGetAndFormatRawPublicKey()67 public void testGetAndFormatRawPublicKey() throws Exception { 68 KeyPair testKey = generateEcdsaKeyPair(); 69 X509Certificate testCert = signPublicKey(testKey, testKey.getPublic()); 70 byte[] formattedKey = X509Utils.getAndFormatRawPublicKey(testCert); 71 byte[] xPoint = new byte[32]; 72 byte[] yPoint = new byte[32]; 73 System.arraycopy(formattedKey, 0 /* offset */, xPoint, 0 /* offset */, 32 /* length */); 74 System.arraycopy(formattedKey, 32 /* offset */, yPoint, 0 /* offset */, 32 /* length */); 75 assertTrue(testKey.getPublic().equals(getP256PubKeyFromBytes(xPoint, yPoint))); 76 } 77 } 78