J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-1998 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.pkcs; |
| 27 | |
| 28 | import java.io.OutputStream; |
| 29 | import java.io.IOException; |
| 30 | |
| 31 | import sun.security.util.*; |
| 32 | |
| 33 | /** |
| 34 | * Represent a PKCS#10 Attribute. |
| 35 | * |
| 36 | * <p>Attributes are additonal information which can be inserted in a PKCS#10 |
| 37 | * certificate request. For example a "Driving License Certificate" could have |
| 38 | * the driving license number as an attribute. |
| 39 | * |
| 40 | * <p>Attributes are represented as a sequence of the attribute identifier |
| 41 | * (Object Identifier) and a set of DER encoded attribute values. |
| 42 | * |
| 43 | * ASN.1 definition of Attribute: |
| 44 | * <pre> |
| 45 | * Attribute :: SEQUENCE { |
| 46 | * type AttributeType, |
| 47 | * values SET OF AttributeValue |
| 48 | * } |
| 49 | * AttributeType ::= OBJECT IDENTIFIER |
| 50 | * AttributeValue ::= ANY defined by type |
| 51 | * </pre> |
| 52 | * |
| 53 | * @author Amit Kapoor |
| 54 | * @author Hemma Prafullchandra |
| 55 | */ |
| 56 | public class PKCS10Attribute implements DerEncoder { |
| 57 | |
| 58 | protected ObjectIdentifier attributeId = null; |
| 59 | protected Object attributeValue = null; |
| 60 | |
| 61 | /** |
| 62 | * Constructs an attribute from a DER encoding. |
| 63 | * This constructor expects the value to be encoded as defined above, |
| 64 | * i.e. a SEQUENCE of OID and SET OF value(s), not a literal |
| 65 | * X.509 v3 extension. Only PKCS9 defined attributes are supported |
| 66 | * currently. |
| 67 | * |
| 68 | * @param derVal the der encoded attribute. |
| 69 | * @exception IOException on parsing errors. |
| 70 | */ |
| 71 | public PKCS10Attribute(DerValue derVal) throws IOException { |
| 72 | PKCS9Attribute attr = new PKCS9Attribute(derVal); |
| 73 | this.attributeId = attr.getOID(); |
| 74 | this.attributeValue = attr.getValue(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Constructs an attribute from individual components of |
| 79 | * ObjectIdentifier and the value (any java object). |
| 80 | * |
| 81 | * @param attributeId the ObjectIdentifier of the attribute. |
| 82 | * @param attributeValue an instance of a class that implements |
| 83 | * the attribute identified by the ObjectIdentifier. |
| 84 | */ |
| 85 | public PKCS10Attribute(ObjectIdentifier attributeId, |
| 86 | Object attributeValue) { |
| 87 | this.attributeId = attributeId; |
| 88 | this.attributeValue = attributeValue; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Constructs an attribute from PKCS9 attribute. |
| 93 | * |
| 94 | * @param attr the PKCS9Attribute to create from. |
| 95 | */ |
| 96 | public PKCS10Attribute(PKCS9Attribute attr) { |
| 97 | this.attributeId = attr.getOID(); |
| 98 | this.attributeValue = attr.getValue(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * DER encode this object onto an output stream. |
| 103 | * Implements the <code>DerEncoder</code> interface. |
| 104 | * |
| 105 | * @param out |
| 106 | * the OutputStream on which to write the DER encoding. |
| 107 | * |
| 108 | * @exception IOException on encoding errors. |
| 109 | */ |
| 110 | public void derEncode(OutputStream out) throws IOException { |
| 111 | PKCS9Attribute attr = new PKCS9Attribute(attributeId, attributeValue); |
| 112 | attr.derEncode(out); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the ObjectIdentifier of the attribute. |
| 117 | */ |
| 118 | public ObjectIdentifier getAttributeId() { |
| 119 | return (attributeId); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the attribute value. |
| 124 | */ |
| 125 | public Object getAttributeValue() { |
| 126 | return (attributeValue); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the attribute in user readable form. |
| 131 | */ |
| 132 | public String toString() { |
| 133 | return (attributeValue.toString()); |
| 134 | } |
| 135 | } |