1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Alexander Y. Kleymenov 20*/ 21 22package java.security.cert; 23 24import java.io.ByteArrayInputStream; 25import java.math.BigInteger; 26import java.security.InvalidKeyException; 27import java.security.NoSuchAlgorithmException; 28import java.security.NoSuchProviderException; 29import java.security.Principal; 30import java.security.PublicKey; 31import java.security.SignatureException; 32import java.util.Date; 33import java.util.Set; 34 35import javax.security.auth.x500.X500Principal; 36 37import org.apache.harmony.luni.util.Base64; 38import org.apache.harmony.security.tests.support.cert.TestUtils; 39 40import junit.framework.Test; 41import junit.framework.TestCase; 42import junit.framework.TestSuite; 43 44/** 45 * X509CertificateTest 46 */ 47public class X509CertificateTest extends TestCase { 48 49 // Base64 encoded form of ASN.1 DER encoded X.509 Certificate 50 // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt) 51 // (generated by using of classes from 52 // org.apache.harmony.security.x509 package) 53 static String base64cert = 54 "MIIByzCCATagAwIBAgICAiswCwYJKoZIhvcNAQEFMB0xGzAZBgNVBAoT" 55 + "EkNlcnRpZmljYXRlIElzc3VlcjAeFw0wNjA0MjYwNjI4MjJaFw0zMzAz" 56 + "MDExNjQ0MDlaMB0xGzAZBgNVBAoTEkNlcnRpZmljYXRlIElzc3VlcjCB" 57 + "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAkLGLsPdSPDMyP1OUOKu+" 58 + "U3cvbNK5RGaQ3bXc5aDjvApx43BcaoXgt6YD/5yXz0OsIooj5yA37+bY" 59 + "JGcVrvFD5FMPdDd3vjNPQOep0MzG4CdbkaZde5SigPabOMQYS4oUyLBx" 60 + "W3LGG0mUODe5AGGqtqXU0GlKg4K2je6cCtookCUCAwEAAaMeMBwwGgYD" 61 + "VR0RAQH/BBAwDoEMcmZjQDgyMi5OYW1lMAsGCSqGSIb3DQEBBQOBgQBZ" 62 + "pVXj01dOpqnZErU+Qb50j8lJD1dIaz1eJTvJCSadj7ziV1VtnnapI07c" 63 + "XEa7ONzcHQTYTG10poHfOK/a0BaULF3GlctDESilwQYbW5BdfpAlZpbH" 64 + "AFLcUDh6Eq50kc+0A/anh/j3mgBNuvbIMo7hHNnZB6k/prswm2BszyLD" 65 + "yw=="; 66 67 // Base64 encoded form of ASN.1 DER encoded X.509 CRL 68 // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt) 69 // (generated by using of classes from 70 // org.apache.harmony.security.x509 package) 71 static String base64crl = 72 "MIHXMIGXAgEBMAkGByqGSM44BAMwFTETMBEGA1UEChMKQ1JMIElzc3Vl" 73 + "chcNMDYwNDI3MDYxMzQ1WhcNMDYwNDI3MDYxNTI1WjBBMD8CAgIrFw0w" 74 + "NjA0MjcwNjEzNDZaMCowCgYDVR0VBAMKAQEwHAYDVR0YBBUYEzIwMDYw" 75 + "NDI3MDYxMzQ1LjQ2OFqgDzANMAsGA1UdFAQEBAQEBDAJBgcqhkjOOAQD" 76 + "AzAAMC0CFQCk0t0DTyu82QpajbBlxX9uXvUDSgIUSBN4g+xTEeexs/0k" 77 + "9AkjBhjF0Es="; 78 79 // has stub implementation for abstract methods 80 private static class MyX509Certificate extends X509Certificate { 81 82 public void checkValidity() 83 throws CertificateExpiredException, 84 CertificateNotYetValidException {} 85 86 public void checkValidity(Date date) 87 throws CertificateExpiredException, 88 CertificateNotYetValidException {} 89 90 public int getVersion() { 91 return 3; 92 } 93 94 public BigInteger getSerialNumber() { 95 return null; 96 } 97 98 public Principal getIssuerDN() { 99 return null; 100 } 101 102 public Principal getSubjectDN() { 103 return null; 104 } 105 106 public Date getNotBefore() { 107 return null; 108 } 109 110 public Date getNotAfter() { 111 return null; 112 } 113 114 public byte[] getTBSCertificate() 115 throws CertificateEncodingException 116 { 117 return null; 118 } 119 120 public byte[] getSignature() { 121 return null; 122 } 123 124 public String getSigAlgName() { 125 return null; 126 } 127 128 public String getSigAlgOID() { 129 return null; 130 } 131 132 public byte[] getSigAlgParams() { 133 return null; 134 } 135 136 public boolean[] getIssuerUniqueID() { 137 return null; 138 } 139 140 public boolean[] getSubjectUniqueID() { 141 return null; 142 } 143 144 public boolean[] getKeyUsage() { 145 return null; 146 } 147 148 public int getBasicConstraints() { 149 return 0; 150 } 151 152 public void verify(PublicKey key) 153 throws CertificateException, NoSuchAlgorithmException, 154 InvalidKeyException, NoSuchProviderException, 155 SignatureException 156 { 157 } 158 159 public void verify(PublicKey key, 160 String sigProvider) 161 throws CertificateException, NoSuchAlgorithmException, 162 InvalidKeyException, NoSuchProviderException, 163 SignatureException 164 { 165 } 166 167 public String toString() { 168 return ""; 169 } 170 171 public PublicKey getPublicKey() { 172 return null; 173 } 174 175 public byte[] getEncoded() throws CertificateEncodingException 176 { 177 return null; 178 } 179 180 public Set getNonCriticalExtensionOIDs() { 181 return null; 182 } 183 184 public Set getCriticalExtensionOIDs() { 185 return null; 186 } 187 188 public byte[] getExtensionValue(String oid) { 189 return null; 190 } 191 192 public boolean hasUnsupportedCriticalExtension() { 193 return false; 194 } 195 } 196 197 /** 198 * @tests java.security.cert.X509Certificate#getType() 199 */ 200 public void testGetType() { 201 assertEquals("X.509", new MyX509Certificate().getType()); 202 } 203 204 /** 205 * @tests java.security.cert.X509Certificate#getIssuerX500Principal() 206 */ 207 public void testGetIssuerX500Principal() { 208 // return valid encoding 209 MyX509Certificate cert = new MyX509Certificate() { 210 public byte[] getEncoded() { 211 return TestUtils.getX509Certificate_v1(); 212 }; 213 }; 214 215 assertEquals(new X500Principal("CN=Z"), cert.getIssuerX500Principal()); 216 } 217 218 /** 219 * @tests java.security.cert.X509Certificate#getSubjectX500Principal() 220 */ 221 public void testGetSubjectX500Principal() { 222 // return valid encoding 223 MyX509Certificate cert = new MyX509Certificate() { 224 public byte[] getEncoded() { 225 return TestUtils.getX509Certificate_v1(); 226 }; 227 }; 228 229 assertEquals(new X500Principal("CN=Y"), cert.getSubjectX500Principal()); 230 } 231 232 /** 233 * @tests java.security.cert.X509Certificate#getExtendedKeyUsage() 234 */ 235 public void testGetExtendedKeyUsage() throws CertificateParsingException { 236 assertNull(new MyX509Certificate().getExtendedKeyUsage()); 237 } 238 239 /** 240 * @tests java.security.cert.X509Certificate#getSubjectAlternativeNames() 241 */ 242 public void testGetSubjectAlternativeNames() 243 throws CertificateParsingException { 244 245 assertNull(new MyX509Certificate().getSubjectAlternativeNames()); 246 } 247 248 /** 249 * @tests java.security.cert.X509Certificate#getIssuerAlternativeNames() 250 */ 251 public void testGetIssuerAlternativeNames() 252 throws CertificateParsingException { 253 254 assertNull(new MyX509Certificate().getIssuerAlternativeNames()); 255 } 256 257 /** 258 * @tests java.security.cert.X509Certificate#getExtensionValue() 259 */ 260 public void testGetExtensionValue() throws Exception { 261 // Regression for HARMONY-419 262 ByteArrayInputStream is = null; 263 CertificateFactory certFactory = CertificateFactory 264 .getInstance("X.509"); 265 is = new ByteArrayInputStream(Base64.decode(base64cert.getBytes("UTF-8"))); 266 X509Certificate cert = (X509Certificate) certFactory 267 .generateCertificate(is); 268 cert.getExtensionValue("1.1.1.1"); 269 270 is = new ByteArrayInputStream(Base64.decode(base64crl.getBytes("UTF-8"))); 271 X509CRL crl = (X509CRL) certFactory.generateCRL(is); 272 crl.getExtensionValue("1.1.1.1"); 273 } 274 275 276 public static Test suite() { 277 return new TestSuite(X509CertificateTest.class); 278 } 279 280} 281