Home | History | Annotate | Download | only in x509
      1 /*
      2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package sun.security.x509;
     27 
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.io.OutputStream;
     31 import java.util.Enumeration;
     32 
     33 import sun.security.util.*;
     34 
     35 /**
     36  * This class defines the AlgorithmId for the Certificate.
     37  *
     38  * @author Amit Kapoor
     39  * @author Hemma Prafullchandra
     40  */
     41 public class CertificateAlgorithmId implements CertAttrSet<String> {
     42     private AlgorithmId algId;
     43 
     44     /**
     45      * Identifier for this attribute, to be used with the
     46      * get, set, delete methods of Certificate, x509 type.
     47      */
     48     public static final String IDENT = "x509.info.algorithmID";
     49     /**
     50      * Sub attributes name for this CertAttrSet.
     51      */
     52     public static final String NAME = "algorithmID";
     53 
     54     /**
     55      * Identifier to be used with get, set, and delete methods. When
     56      * using this identifier the associated object being passed in or
     57      * returned is an instance of AlgorithmId.
     58      * @see sun.security.x509.AlgorithmId
     59      */
     60     public static final String ALGORITHM = "algorithm";
     61 
     62     /**
     63      * Default constructor for the certificate attribute.
     64      *
     65      * @param algId the Algorithm identifier
     66      */
     67     public CertificateAlgorithmId(AlgorithmId algId) {
     68         this.algId = algId;
     69     }
     70 
     71     /**
     72      * Create the object, decoding the values from the passed DER stream.
     73      *
     74      * @param in the DerInputStream to read the serial number from.
     75      * @exception IOException on decoding errors.
     76      */
     77     public CertificateAlgorithmId(DerInputStream in) throws IOException {
     78         DerValue val = in.getDerValue();
     79         algId = AlgorithmId.parse(val);
     80     }
     81 
     82     /**
     83      * Create the object, decoding the values from the passed stream.
     84      *
     85      * @param in the InputStream to read the serial number from.
     86      * @exception IOException on decoding errors.
     87      */
     88     public CertificateAlgorithmId(InputStream in) throws IOException {
     89         DerValue val = new DerValue(in);
     90         algId = AlgorithmId.parse(val);
     91     }
     92 
     93     /**
     94      * Return the algorithm identifier as user readable string.
     95      */
     96     public String toString() {
     97         if (algId == null) return "";
     98         return (algId.toString() +
     99                 ", OID = " + (algId.getOID()).toString() + "\n");
    100     }
    101 
    102     /**
    103      * Encode the algorithm identifier in DER form to the stream.
    104      *
    105      * @param out the DerOutputStream to marshal the contents to.
    106      * @exception IOException on errors.
    107      */
    108     public void encode(OutputStream out) throws IOException {
    109         DerOutputStream tmp = new DerOutputStream();
    110         algId.encode(tmp);
    111 
    112         out.write(tmp.toByteArray());
    113     }
    114 
    115     /**
    116      * Set the attribute value.
    117      */
    118     public void set(String name, Object obj) throws IOException {
    119         if (!(obj instanceof AlgorithmId)) {
    120             throw new IOException("Attribute must be of type AlgorithmId.");
    121         }
    122         if (name.equalsIgnoreCase(ALGORITHM)) {
    123             algId = (AlgorithmId)obj;
    124         } else {
    125             throw new IOException("Attribute name not recognized by " +
    126                               "CertAttrSet:CertificateAlgorithmId.");
    127         }
    128     }
    129 
    130     /**
    131      * Get the attribute value.
    132      */
    133     public AlgorithmId get(String name) throws IOException {
    134         if (name.equalsIgnoreCase(ALGORITHM)) {
    135             return (algId);
    136         } else {
    137             throw new IOException("Attribute name not recognized by " +
    138                                "CertAttrSet:CertificateAlgorithmId.");
    139         }
    140     }
    141 
    142     /**
    143      * Delete the attribute value.
    144      */
    145     public void delete(String name) throws IOException {
    146         if (name.equalsIgnoreCase(ALGORITHM)) {
    147             algId = null;
    148         } else {
    149             throw new IOException("Attribute name not recognized by " +
    150                                "CertAttrSet:CertificateAlgorithmId.");
    151         }
    152     }
    153 
    154     /**
    155      * Return an enumeration of names of attributes existing within this
    156      * attribute.
    157      */
    158     public Enumeration<String> getElements() {
    159         AttributeNameEnumeration elements = new AttributeNameEnumeration();
    160         elements.addElement(ALGORITHM);
    161         return (elements.elements());
    162     }
    163 
    164    /**
    165     * Return the name of this attribute.
    166     */
    167    public String getName() {
    168       return (NAME);
    169    }
    170 }
    171