/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Output stream marshaling DER-encoded data. This is eventually provided
* in the form of a byte array; there is no advance limit on the size of
* that byte array.
*
* <P>At this time, this class supports only a subset of the types of
* DER data encodings which are defined. That subset is sufficient for
* generating most X.509 certificates.
*
*
* @author David Brownell
* @author Amit Kapoor
* @author Hemma Prafullchandra
*/
public class DerOutputStream
extends ByteArrayOutputStream implements DerEncoder {
/**
* Construct an DER output stream.
*
* @param size how large a buffer to preallocate.
*/
/**
* Construct an DER output stream.
*/
public DerOutputStream() { }
/**
* Writes tagged, pre-marshaled data. This calcuates and encodes
* the length, so that the output data is the standard triple of
* { tag, length, data } used by all DER values.
*
* @param tag the DER value tag for the data, such as
* <em>DerValue.tag_Sequence</em>
* @param buf buffered data, which must be DER-encoded
*/
}
/**
* Writes tagged data using buffer-to-buffer copy. As above,
* this writes a standard DER record. This is often used when
* efficiently encapsulating values in sequences.
*
* @param tag the DER value tag for the data, such as
* <em>DerValue.tag_Sequence</em>
* @param out buffered data
*/
}
/**
* Writes implicitly tagged data using buffer-to-buffer copy. As above,
* this writes a standard DER record. This is often used when
* efficiently encapsulating implicitly tagged values.
*
* @param tag the DER value of the context-specific tag that replaces
* original tag of the value in the output, such as in
* <pre>
* <em> <field> [N] IMPLICIT <type></em>
* </pre>
* For example, <em>FooLength [1] IMPLICIT INTEGER</em>, with value=4;
* would be encoded as "81 01 04" whereas in explicit
* tagging it would be encoded as "A1 03 02 01 04".
* Notice that the tag is A1 and not 81, this is because with
* explicit tagging the form is always constructed.
* @param value original value being implicitly tagged
*/
throws IOException {
}
/**
* Marshals pre-encoded DER value onto the output stream.
*/
}
/*
* PRIMITIVES -- these are "universal" ASN.1 simple types.
*
* BOOLEAN, INTEGER, BIT STRING, OCTET STRING, NULL
* OBJECT IDENTIFIER, SEQUENCE(OF), SET(OF)
* PrintableString, T61String, IA5String, UTCTime
*/
/**
* Marshals a DER boolean on the output stream.
*/
putLength(1);
if (val) {
write(0xff);
} else {
write(0);
}
}
/**
* Marshals a DER enumerated on the output stream.
* @param i the enumerated value.
*/
}
/**
* Marshals a DER integer on the output stream.
*
* @param i the integer in the form of a BigInteger.
*/
}
/**
* Marshals a DER integer on the output stream.
* @param i the integer in the form of an Integer.
*/
putInteger(i.intValue());
}
/**
* Marshals a DER integer on the output stream.
* @param i the integer.
*/
}
byte[] bytes = new byte[4];
int start = 0;
// Obtain the four bytes of the int
// Reduce them to the least number of bytes needed to
// represent this int
// Eliminate redundant 0xff
for (int j = 0; j < 3; j++) {
if ((bytes[j] == (byte)0xff) &&
start++;
else
break;
}
// Eliminate redundant 0x00
for (int j = 0; j < 3; j++) {
if ((bytes[j] == 0x00) &&
start++;
else
break;
}
}
for (int k = start; k < 4; k++)
}
/**
* Marshals a DER bit string on the output stream. The bit
* string must be byte-aligned.
*
* @param bits the bit string, MSB first
*/
}
/**
* Marshals a DER bit string on the output stream.
* The bit strings need not be byte-aligned.
*
* @param bits the bit string, MSB first
*/
}
/**
* Marshals a truncated DER bit string on the output stream.
* The bit strings need not be byte-aligned.
*
* @param bits the bit string, MSB first
*/
}
/**
* DER-encodes an ASN.1 OCTET STRING value on the output stream.
*
* @param octets the octet string
*/
}
/**
* Marshals a DER "null" value on the output stream. These are
* often used to indicate optional values which have been omitted.
*/
putLength(0);
}
/**
* Marshals an object identifier (OID) on the output stream.
* Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.
*/
}
/**
* Marshals a sequence on the output stream. This supports both
* the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF"
* (one to N values) constructs.
*/
int i;
}
/**
* Marshals the contents of a set on the output stream without
* ordering the elements. Ok for BER encoding, but not for DER
* encoding.
*
* For DER encoding, use orderedPutSet() or orderedPutSetOf().
*/
int i;
}
/**
* Marshals the contents of a set on the output stream. Sets
* are semantically unordered, but DER requires that encodings of
* set elements be sorted into ascending lexicographical order
* before being output. Hence sets with the same tags and
* elements have the same DER encoding.
*
* This method supports the ASN.1 "SET OF" construct, but not
* "SET", which uses a different order.
*/
}
/**
* Marshals the contents of a set on the output stream. Sets
* are semantically unordered, but DER requires that encodings of
* set elements be sorted into ascending tag order
* before being output. Hence sets with the same tags and
* elements have the same DER encoding.
*
* This method supports the ASN.1 "SET" construct, but not
* "SET OF", which uses a different order.
*/
}
/**
* Lexicographical order comparison on byte arrays, for ordering
* elements of a SET OF objects in DER encoding.
*/
/**
* Tag order comparison on byte arrays, for ordering elements of
* SET objects in DER encoding.
*/
/**
* Marshals a the contents of a set on the output stream with the
* encodings of its sorted in increasing order.
*
* @param order the order to use when sorting encodings of components.
*/
streams[i] = new DerOutputStream();
}
// order the element encodings
}
}
}
/**
* Marshals a string as a DER encoded UTF8String.
*/
}
/**
* Marshals a string as a DER encoded PrintableString.
*/
}
/**
* Marshals a string as a DER encoded T61String.
*/
/*
* Works for characters that are defined in both ASCII and
* T61.
*/
}
/**
* Marshals a string as a DER encoded IA5String.
*/
}
/**
* Marshals a string as a DER encoded BMPString.
*/
}
/**
* Marshals a string as a DER encoded GeneralString.
*/
}
/**
* Private helper routine for writing DER encoded string values.
* @param s the string to write
* @param stringTag one of the DER string tags that indicate which
* encoding should be used to write the string out.
* @param enc the name of the encoder that should be used corresponding
* to the above tag.
*/
throws IOException {
}
/**
*
* <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
* and with seconds (even if seconds=0) as per RFC 3280.
*/
}
/**
*
* <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
* and with seconds (even if seconds=0) as per RFC 3280.
*/
}
/**
* Private helper routine for marshalling a DER UTC/Generalized
* then it defaults to Generalized Time.
* @param d the date to be marshalled
* @param tag the tag for UTC Time or Generalized Time
*/
/*
* Format the date.
*/
pattern = "yyMMddHHmmss'Z'";
} else {
pattern = "yyyyMMddHHmmss'Z'";
}
/*
* Write the formatted date.
*/
}
/**
* Put the encoding of the length in the stream.
*
* @params len the length of the attribute.
* @exception IOException on writing errors.
*/
if (len < 128) {
write((byte)0x081);
write((byte)0x082);
write((byte)0x083);
} else {
write((byte)0x084);
}
}
/**
* Put the tag of the attribute in the stream.
*
* @params class the tag class type, one of UNIVERSAL, CONTEXT,
* APPLICATION or PRIVATE
* @params form if true, the value is constructed, otherwise it is
* primitive.
* @params val the tag value
*/
if (form) {
tag |= (byte)0x20;
}
}
/**
* Write the current contents of this <code>DerOutputStream</code>
* to an <code>OutputStream</code>.
*
* @exception IOException on output error.
*/
}
}