1 /*
2 * Copyright (c) 2013, 2018, 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
47 private final byte[] encoded;
48 private int hashValue = -1;
49
50 /**
51 * Constructs a PKCS12 attribute from its name and value.
52 * The name is an ASN.1 Object Identifier represented as a list of
53 * dot-separated integers.
54 * A string value is represented as the string itself.
55 * A binary value is represented as a string of colon-separated
56 * pairs of hexadecimal digits.
57 * Multi-valued attributes are represented as a comma-separated
58 * list of values, enclosed in square brackets. See
59 * {@link Arrays#toString(java.lang.Object[])}.
60 * <p>
61 * A string value will be DER-encoded as an ASN.1 UTF8String and a
62 * binary value will be DER-encoded as an ASN.1 Octet String.
63 *
64 * @param name the attribute's identifier
65 * @param value the attribute's value
66 *
67 * @exception NullPointerException if {@code name} or {@code value}
68 * is {@code null}
69 * @exception IllegalArgumentException if {@code name} or
70 * {@code value} is incorrectly formatted
71 */
72 public PKCS12Attribute(String name, String value) {
73 if (name == null || value == null) {
74 throw new NullPointerException();
75 }
76 // Validate name
77 ObjectIdentifier type;
78 try {
79 type = new ObjectIdentifier(name);
80 } catch (IOException e) {
81 throw new IllegalArgumentException("Incorrect format: name", e);
82 }
83 this.name = name;
84
85 // Validate value
86 int length = value.length();
87 String[] values;
88 if (length > 1 &&
89 value.charAt(0) == '[' && value.charAt(length - 1) == ']') {
100 }
101 }
102
103 /**
104 * Constructs a PKCS12 attribute from its ASN.1 DER encoding.
105 * The DER encoding is specified by the following ASN.1 definition:
106 * <pre>
107 *
108 * Attribute ::= SEQUENCE {
109 * type AttributeType,
110 * values SET OF AttributeValue
111 * }
112 * AttributeType ::= OBJECT IDENTIFIER
113 * AttributeValue ::= ANY defined by type
114 *
115 * </pre>
116 *
117 * @param encoded the attribute's ASN.1 DER encoding. It is cloned
118 * to prevent subsequent modificaion.
119 *
120 * @exception NullPointerException if {@code encoded} is
121 * {@code null}
122 * @exception IllegalArgumentException if {@code encoded} is
123 * incorrectly formatted
124 */
125 public PKCS12Attribute(byte[] encoded) {
126 if (encoded == null) {
127 throw new NullPointerException();
128 }
129 this.encoded = encoded.clone();
130
131 try {
132 parse(encoded);
133 } catch (IOException e) {
134 throw new IllegalArgumentException("Incorrect format: encoded", e);
135 }
136 }
137
138 /**
139 * Returns the attribute's ASN.1 Object Identifier represented as a
140 * list of dot-separated integers.
141 *
142 * @return the attribute's identifier
|
1 /*
2 * Copyright (c) 2013, 2019, 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
47 private final byte[] encoded;
48 private int hashValue = -1;
49
50 /**
51 * Constructs a PKCS12 attribute from its name and value.
52 * The name is an ASN.1 Object Identifier represented as a list of
53 * dot-separated integers.
54 * A string value is represented as the string itself.
55 * A binary value is represented as a string of colon-separated
56 * pairs of hexadecimal digits.
57 * Multi-valued attributes are represented as a comma-separated
58 * list of values, enclosed in square brackets. See
59 * {@link Arrays#toString(java.lang.Object[])}.
60 * <p>
61 * A string value will be DER-encoded as an ASN.1 UTF8String and a
62 * binary value will be DER-encoded as an ASN.1 Octet String.
63 *
64 * @param name the attribute's identifier
65 * @param value the attribute's value
66 *
67 * @throws NullPointerException if {@code name} or {@code value}
68 * is {@code null}
69 * @throws IllegalArgumentException if {@code name} or
70 * {@code value} is incorrectly formatted
71 */
72 public PKCS12Attribute(String name, String value) {
73 if (name == null || value == null) {
74 throw new NullPointerException();
75 }
76 // Validate name
77 ObjectIdentifier type;
78 try {
79 type = new ObjectIdentifier(name);
80 } catch (IOException e) {
81 throw new IllegalArgumentException("Incorrect format: name", e);
82 }
83 this.name = name;
84
85 // Validate value
86 int length = value.length();
87 String[] values;
88 if (length > 1 &&
89 value.charAt(0) == '[' && value.charAt(length - 1) == ']') {
100 }
101 }
102
103 /**
104 * Constructs a PKCS12 attribute from its ASN.1 DER encoding.
105 * The DER encoding is specified by the following ASN.1 definition:
106 * <pre>
107 *
108 * Attribute ::= SEQUENCE {
109 * type AttributeType,
110 * values SET OF AttributeValue
111 * }
112 * AttributeType ::= OBJECT IDENTIFIER
113 * AttributeValue ::= ANY defined by type
114 *
115 * </pre>
116 *
117 * @param encoded the attribute's ASN.1 DER encoding. It is cloned
118 * to prevent subsequent modificaion.
119 *
120 * @throws NullPointerException if {@code encoded} is
121 * {@code null}
122 * @throws IllegalArgumentException if {@code encoded} is
123 * incorrectly formatted
124 */
125 public PKCS12Attribute(byte[] encoded) {
126 if (encoded == null) {
127 throw new NullPointerException();
128 }
129 this.encoded = encoded.clone();
130
131 try {
132 parse(encoded);
133 } catch (IOException e) {
134 throw new IllegalArgumentException("Incorrect format: encoded", e);
135 }
136 }
137
138 /**
139 * Returns the attribute's ASN.1 Object Identifier represented as a
140 * list of dot-separated integers.
141 *
142 * @return the attribute's identifier
|