Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateAlgorithmId.java
41159 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* This class defines the AlgorithmId for the Certificate.36*37* @author Amit Kapoor38* @author Hemma Prafullchandra39*/40public class CertificateAlgorithmId implements CertAttrSet<String> {41private AlgorithmId algId;4243/**44* Identifier for this attribute, to be used with the45* get, set, delete methods of Certificate, x509 type.46*/47public static final String IDENT = "x509.info.algorithmID";48/**49* Sub attributes name for this CertAttrSet.50*/51public static final String NAME = "algorithmID";5253/**54* Identifier to be used with get, set, and delete methods. When55* using this identifier the associated object being passed in or56* returned is an instance of AlgorithmId.57* @see sun.security.x509.AlgorithmId58*/59public static final String ALGORITHM = "algorithm";6061/**62* Default constructor for the certificate attribute.63*64* @param algId the Algorithm identifier65*/66public CertificateAlgorithmId(AlgorithmId algId) {67this.algId = algId;68}6970/**71* Create the object, decoding the values from the passed DER stream.72*73* @param in the DerInputStream to read the serial number from.74* @exception IOException on decoding errors.75*/76public CertificateAlgorithmId(DerInputStream in) throws IOException {77DerValue val = in.getDerValue();78algId = AlgorithmId.parse(val);79}8081/**82* Create the object, decoding the values from the passed stream.83*84* @param in the InputStream to read the serial number from.85* @exception IOException on decoding errors.86*/87public CertificateAlgorithmId(InputStream in) throws IOException {88DerValue val = new DerValue(in);89algId = AlgorithmId.parse(val);90}9192/**93* Return the algorithm identifier as user readable string.94*/95public String toString() {96if (algId == null) return "";97return (algId.toString() +98", OID = " + (algId.getOID()).toString() + "\n");99}100101/**102* Encode the algorithm identifier in DER form to the stream.103*104* @param out the DerOutputStream to marshal the contents to.105* @exception IOException on errors.106*/107public void encode(OutputStream out) throws IOException {108DerOutputStream tmp = new DerOutputStream();109algId.encode(tmp);110111out.write(tmp.toByteArray());112}113114/**115* Set the attribute value.116*/117public void set(String name, Object obj) throws IOException {118if (!(obj instanceof AlgorithmId)) {119throw new IOException("Attribute must be of type AlgorithmId.");120}121if (name.equalsIgnoreCase(ALGORITHM)) {122algId = (AlgorithmId)obj;123} else {124throw new IOException("Attribute name not recognized by " +125"CertAttrSet:CertificateAlgorithmId.");126}127}128129/**130* Get the attribute value.131*/132public AlgorithmId get(String name) throws IOException {133if (name.equalsIgnoreCase(ALGORITHM)) {134return (algId);135} else {136throw new IOException("Attribute name not recognized by " +137"CertAttrSet:CertificateAlgorithmId.");138}139}140141/**142* Delete the attribute value.143*/144public void delete(String name) throws IOException {145if (name.equalsIgnoreCase(ALGORITHM)) {146algId = null;147} else {148throw new IOException("Attribute name not recognized by " +149"CertAttrSet:CertificateAlgorithmId.");150}151}152153/**154* Return an enumeration of names of attributes existing within this155* attribute.156*/157public Enumeration<String> getElements() {158AttributeNameEnumeration elements = new AttributeNameEnumeration();159elements.addElement(ALGORITHM);160return (elements.elements());161}162163/**164* Return the name of this attribute.165*/166public String getName() {167return (NAME);168}169}170171172