src/share/classes/com/sun/org/apache/xml/internal/security/keys/content/x509/XMLX509SKI.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at


  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 package com.sun.org.apache.xml.internal.security.keys.content.x509;
  22 
  23 import java.io.IOException;
  24 import java.io.ByteArrayInputStream;
  25 import java.io.InputStream;
  26 import java.security.cert.X509Certificate;
  27 import java.lang.reflect.Constructor;
  28 import java.lang.reflect.Method;
  29 
  30 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  31 import com.sun.org.apache.xml.internal.security.utils.Base64;
  32 import com.sun.org.apache.xml.internal.security.utils.Constants;
  33 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  34 import org.w3c.dom.Document;
  35 import org.w3c.dom.Element;
  36 
  37 /**
  38  * Handles SubjectKeyIdentifier (SKI) for X.509v3.
  39  *
  40  * @author $Author: mullan $
  41  * @see <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/security/cert/X509Extension.html">Interface X509Extension</A>
  42  */
  43 public class XMLX509SKI extends SignatureElementProxy
  44         implements XMLX509DataContent {
  45 
  46     /** {@link java.util.logging} logging facility */
  47     static java.util.logging.Logger log =
  48         java.util.logging.Logger.getLogger(XMLX509SKI.class.getName());
  49 
  50     /**
  51      * <CODE>SubjectKeyIdentifier (id-ce-subjectKeyIdentifier) (2.5.29.14)</CODE>:
  52      * This extension identifies the public key being certified. It enables
  53      * distinct keys used by the same subject to be differentiated
  54      * (e.g., as key updating occurs).
  55      * <BR />
  56      * A key identifer shall be unique with respect to all key identifiers
  57      * for the subject with which it is used. This extension is always non-critical.
  58      */
  59     public static final String SKI_OID = "2.5.29.14";
  60 
  61     /**
  62      * Constructor X509SKI
  63      *
  64      * @param doc
  65      * @param skiBytes
  66      */
  67     public XMLX509SKI(Document doc, byte[] skiBytes) {
  68         super(doc);
  69         this.addBase64Text(skiBytes);
  70     }
  71 
  72     /**
  73      * Constructor XMLX509SKI
  74      *
  75      * @param doc
  76      * @param x509certificate
  77      * @throws XMLSecurityException
  78      */
  79     public XMLX509SKI(Document doc, X509Certificate x509certificate)
  80            throws XMLSecurityException {
  81         super(doc);
  82         this.addBase64Text(XMLX509SKI.getSKIBytesFromCert(x509certificate));
  83     }
  84 
  85     /**
  86      * Constructor XMLX509SKI
  87      *
  88      * @param element
  89      * @param BaseURI
  90      * @throws XMLSecurityException
  91      */
  92     public XMLX509SKI(Element element, String BaseURI)
  93            throws XMLSecurityException {
  94         super(element, BaseURI);
  95     }
  96 
  97     /**
  98      * Method getSKIBytes
  99      *
 100      * @return the skibytes
 101      * @throws XMLSecurityException
 102      */
 103     public byte[] getSKIBytes() throws XMLSecurityException {
 104         return this.getBytesFromTextChild();
 105     }
 106 
 107     /**
 108      * Method getSKIBytesFromCert
 109      *
 110      * @param cert
 111      * @return ski bytes from the given certificate
 112      *
 113      * @throws XMLSecurityException
 114      * @see java.security.cert.X509Extension#getExtensionValue(java.lang.String)
 115      */
 116     public static byte[] getSKIBytesFromCert(X509Certificate cert)
 117         throws XMLSecurityException {
 118 
 119         if (cert.getVersion() < 3) {
 120             Object exArgs[] = { new Integer(cert.getVersion()) };
 121             throw new XMLSecurityException("certificate.noSki.lowVersion",
 122                                            exArgs);
 123         }
 124 
 125         /*
 126          * Gets the DER-encoded OCTET string for the extension value
 127          * (extnValue) identified by the passed-in oid String. The oid
 128          * string is represented by a set of positive whole numbers
 129          * separated by periods.
 130          */
 131         byte[] extensionValue = cert.getExtensionValue(XMLX509SKI.SKI_OID);
 132         if (extensionValue == null) {
 133             throw new XMLSecurityException("certificate.noSki.null");
 134         }
 135 
 136         /**
 137          * Strip away first four bytes from the extensionValue
 138          * The first two bytes are the tag and length of the extensionValue
 139          * OCTET STRING, and the next two bytes are the tag and length of
 140          * the skid OCTET STRING.
 141          */
 142         byte skidValue[] = new byte[extensionValue.length - 4];
 143 
 144         System.arraycopy(extensionValue, 4, skidValue, 0, skidValue.length);
 145 
 146         if (log.isLoggable(java.util.logging.Level.FINE)) {
 147             log.log(java.util.logging.Level.FINE, "Base64 of SKI is " + Base64.encode(skidValue));
 148         }
 149 
 150         return skidValue;
 151     }
 152 
 153     /** @inheritDoc */
 154     public boolean equals(Object obj) {
 155         if (obj == null) {
 156             return false;
 157         }
 158         if (!this.getClass().getName().equals(obj.getClass().getName())) {
 159             return false;
 160         }
 161 
 162         XMLX509SKI other = (XMLX509SKI) obj;
 163 
 164         try {
 165             return java.security.MessageDigest.isEqual(other.getSKIBytes(),
 166                                         this.getSKIBytes());
 167         } catch (XMLSecurityException ex) {
 168             return false;
 169         }
















 170     }
 171 
 172     /** @inheritDoc */
 173     public String getBaseLocalName() {
 174         return Constants._TAG_X509SKI;
 175     }
 176 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.keys.content.x509;
  24 



  25 import java.security.cert.X509Certificate;
  26 import java.util.Arrays;

  27 
  28 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  29 import com.sun.org.apache.xml.internal.security.utils.Base64;
  30 import com.sun.org.apache.xml.internal.security.utils.Constants;
  31 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.Element;
  34 
  35 /**
  36  * Handles SubjectKeyIdentifier (SKI) for X.509v3.
  37  *
  38  * @see <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/security/cert/X509Extension.html">
  39  * Interface X509Extension</A>
  40  */
  41 public class XMLX509SKI extends SignatureElementProxy implements XMLX509DataContent {

  42 
  43     /** {@link org.apache.commons.logging} logging facility */
  44     private static java.util.logging.Logger log = 
  45         java.util.logging.Logger.getLogger(XMLX509SKI.class.getName());
  46 
  47     /**
  48      * <CODE>SubjectKeyIdentifier (id-ce-subjectKeyIdentifier) (2.5.29.14)</CODE>:
  49      * This extension identifies the public key being certified. It enables
  50      * distinct keys used by the same subject to be differentiated
  51      * (e.g., as key updating occurs).
  52      * <BR />
  53      * A key identifier shall be unique with respect to all key identifiers
  54      * for the subject with which it is used. This extension is always non-critical.
  55      */
  56     public static final String SKI_OID = "2.5.29.14";
  57 
  58     /**
  59      * Constructor X509SKI
  60      *
  61      * @param doc
  62      * @param skiBytes
  63      */
  64     public XMLX509SKI(Document doc, byte[] skiBytes) {
  65         super(doc);
  66         this.addBase64Text(skiBytes);
  67     }
  68 
  69     /**
  70      * Constructor XMLX509SKI
  71      *
  72      * @param doc
  73      * @param x509certificate
  74      * @throws XMLSecurityException
  75      */
  76     public XMLX509SKI(Document doc, X509Certificate x509certificate)
  77         throws XMLSecurityException {
  78         super(doc);
  79         this.addBase64Text(XMLX509SKI.getSKIBytesFromCert(x509certificate));
  80     }
  81 
  82     /**
  83      * Constructor XMLX509SKI
  84      *
  85      * @param element
  86      * @param BaseURI
  87      * @throws XMLSecurityException
  88      */
  89     public XMLX509SKI(Element element, String BaseURI) throws XMLSecurityException {

  90         super(element, BaseURI);
  91     }
  92 
  93     /**
  94      * Method getSKIBytes
  95      *
  96      * @return the skibytes
  97      * @throws XMLSecurityException
  98      */
  99     public byte[] getSKIBytes() throws XMLSecurityException {
 100         return this.getBytesFromTextChild();
 101     }
 102 
 103     /**
 104      * Method getSKIBytesFromCert
 105      *
 106      * @param cert
 107      * @return ski bytes from the given certificate
 108      *
 109      * @throws XMLSecurityException
 110      * @see java.security.cert.X509Extension#getExtensionValue(java.lang.String)
 111      */
 112     public static byte[] getSKIBytesFromCert(X509Certificate cert)
 113         throws XMLSecurityException {
 114 
 115         if (cert.getVersion() < 3) {
 116             Object exArgs[] = { Integer.valueOf(cert.getVersion()) };
 117             throw new XMLSecurityException("certificate.noSki.lowVersion", exArgs);

 118         }
 119 
 120         /*
 121          * Gets the DER-encoded OCTET string for the extension value 
 122          * (extnValue) identified by the passed-in oid String. The oid 
 123          * string is represented by a set of positive whole numbers 
 124          * separated by periods.
 125          */
 126         byte[] extensionValue = cert.getExtensionValue(XMLX509SKI.SKI_OID);
 127         if (extensionValue == null) {
 128             throw new XMLSecurityException("certificate.noSki.null");
 129         }
 130 
 131         /**
 132          * Strip away first four bytes from the extensionValue 
 133          * The first two bytes are the tag and length of the extensionValue
 134          * OCTET STRING, and the next two bytes are the tag and length of
 135          * the ski OCTET STRING.
 136          */
 137         byte skidValue[] = new byte[extensionValue.length - 4];
 138 
 139         System.arraycopy(extensionValue, 4, skidValue, 0, skidValue.length);
 140 
 141         if (log.isLoggable(java.util.logging.Level.FINE)) {
 142             log.log(java.util.logging.Level.FINE, "Base64 of SKI is " + Base64.encode(skidValue));
 143         }
 144 
 145         return skidValue;
 146     }
 147 
 148     /** @inheritDoc */
 149     public boolean equals(Object obj) {
 150         if (!(obj instanceof XMLX509SKI)) {



 151             return false;
 152         }
 153 
 154         XMLX509SKI other = (XMLX509SKI) obj;
 155 
 156         try {
 157             return Arrays.equals(other.getSKIBytes(), this.getSKIBytes());

 158         } catch (XMLSecurityException ex) {
 159             return false;
 160         }
 161     }
 162 
 163     public int hashCode() {
 164         int result = 17;
 165         try {
 166             byte[] bytes = getSKIBytes();
 167             for (int i = 0; i < bytes.length; i++) {
 168                 result = 31 * result + bytes[i];
 169             }
 170         } catch (XMLSecurityException e) {
 171             if (log.isLoggable(java.util.logging.Level.FINE)) {
 172                 log.log(java.util.logging.Level.FINE, e.getMessage(), e);
 173             }
 174         }
 175         return result;
 176 
 177     }
 178 
 179     /** @inheritDoc */
 180     public String getBaseLocalName() {
 181         return Constants._TAG_X509SKI;
 182     }
 183 }