src/java.base/share/classes/sun/security/util/DerOutputStream.java
author redestad
Mon, 08 Oct 2018 18:16:03 +0200
changeset 52040 d8aebcc2d3ac
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8211860: Avoid reading security properties eagerly on Manifest class initialization Reviewed-by: mullan, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6109
007c43113d09 6670889: Keystore created under Hindi Locale causing ArrayIndexOutOfBoundsException
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3077
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3077
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3077
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3077
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3077
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.math.BigInteger;
6109
007c43113d09 6670889: Keystore created under Hindi Locale causing ArrayIndexOutOfBoundsException
weijun
parents: 5506
diff changeset
    37
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Output stream marshaling DER-encoded data.  This is eventually provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in the form of a byte array; there is no advance limit on the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <P>At this time, this class supports only a subset of the types of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * DER data encodings which are defined.  That subset is sufficient for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * generating most X.509 certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class DerOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
extends ByteArrayOutputStream implements DerEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Construct an DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param size how large a buffer to preallocate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public DerOutputStream(int size) { super(size); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Construct an DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public DerOutputStream() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Writes tagged, pre-marshaled data.  This calcuates and encodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the length, so that the output data is the standard triple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * { tag, length, data } used by all DER values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param tag the DER value tag for the data, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *          <em>DerValue.tag_Sequence</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param buf buffered data, which must be DER-encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void write(byte tag, byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        putLength(buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        write(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Writes tagged data using buffer-to-buffer copy.  As above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * this writes a standard DER record.  This is often used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * efficiently encapsulating values in sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param tag the DER value tag for the data, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *          <em>DerValue.tag_Sequence</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param out buffered data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void write(byte tag, DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        putLength(out.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        write(out.buf, 0, out.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Writes implicitly tagged data using buffer-to-buffer copy.  As above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * this writes a standard DER record.  This is often used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * efficiently encapsulating implicitly tagged values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param tag the DER value of the context-specific tag that replaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * original tag of the value in the output, such as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <pre>
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   106
     *          <em> {@code <field> [N] IMPLICIT <type>}</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * For example, <em>FooLength [1] IMPLICIT INTEGER</em>, with value=4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * would be encoded as "81 01 04"  whereas in explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * tagging it would be encoded as "A1 03 02 01 04".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Notice that the tag is A1 and not 81, this is because with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * explicit tagging the form is always constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param value original value being implicitly tagged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void writeImplicit(byte tag, DerOutputStream value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        write(value.buf, 1, value.count-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Marshals pre-encoded DER value onto the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void putDerValue(DerValue val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        val.encode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * PRIMITIVES -- these are "universal" ASN.1 simple types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *  BOOLEAN, INTEGER, BIT STRING, OCTET STRING, NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *  OBJECT IDENTIFIER, SEQUENCE(OF), SET(OF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *  PrintableString, T61String, IA5String, UTCTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Marshals a DER boolean on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void putBoolean(boolean val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        write(DerValue.tag_Boolean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        putLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            write(0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Marshals a DER enumerated on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param i the enumerated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void putEnumerated(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        write(DerValue.tag_Enumerated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        putIntegerContents(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Marshals a DER integer on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param i the integer in the form of a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void putInteger(BigInteger i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        write(DerValue.tag_Integer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[]    buf = i.toByteArray(); // least number  of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        putLength(buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        write(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Marshals a DER integer on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param i the integer in the form of an Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void putInteger(Integer i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        putInteger(i.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Marshals a DER integer on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param i the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void putInteger(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        write(DerValue.tag_Integer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        putIntegerContents(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void putIntegerContents(int i) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        byte[] bytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Obtain the four bytes of the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        bytes[3] = (byte) (i & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        bytes[2] = (byte)((i & 0xff00) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        bytes[1] = (byte)((i & 0xff0000) >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        bytes[0] = (byte)((i & 0xff000000) >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Reduce them to the least number of bytes needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // represent this int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
3077
4be3e0e4d42c 6855671: DerOutputStream encodes negative integer incorrectly
weijun
parents: 2
diff changeset
   202
        if (bytes[0] == (byte)0xff) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // Eliminate redundant 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int j = 0; j < 3; j++) {
3077
4be3e0e4d42c 6855671: DerOutputStream encodes negative integer incorrectly
weijun
parents: 2
diff changeset
   207
                if ((bytes[j] == (byte)0xff) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    ((bytes[j+1] & 0x80) == 0x80))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         } else if (bytes[0] == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
             // Eliminate redundant 0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (int j = 0; j < 3; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if ((bytes[j] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    ((bytes[j+1] & 0x80) == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        putLength(4 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        for (int k = start; k < 4; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            write(bytes[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Marshals a DER bit string on the output stream. The bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * string must be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param bits the bit string, MSB first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void putBitString(byte[] bits) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        write(DerValue.tag_BitString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        putLength(bits.length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        write(0);               // all of last octet is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        write(bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Marshals a DER bit string on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * The bit strings need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   248
     * @param ba the bit string, MSB first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void putUnalignedBitString(BitArray ba) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte[] bits = ba.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        write(DerValue.tag_BitString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        putLength(bits.length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        write(bits.length*8 - ba.length()); // excess bits in last octet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        write(bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Marshals a truncated DER bit string on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The bit strings need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   263
     * @param ba the bit string, MSB first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public void putTruncatedUnalignedBitString(BitArray ba) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        putUnalignedBitString(ba.truncate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * DER-encodes an ASN.1 OCTET STRING value on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param octets the octet string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void putOctetString(byte[] octets) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        write(DerValue.tag_OctetString, octets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Marshals a DER "null" value on the output stream.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * often used to indicate optional values which have been omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public void putNull() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        write(DerValue.tag_Null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        putLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Marshals an object identifier (OID) on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void putOID(ObjectIdentifier oid) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        oid.encode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Marshals a sequence on the output stream.  This supports both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * (one to N values) constructs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public void putSequence(DerValue[] seq) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for (i = 0; i < seq.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            seq[i].encode(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Marshals the contents of a set on the output stream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * ordering the elements.  Ok for BER encoding, but not for DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * For DER encoding, use orderedPutSet() or orderedPutSetOf().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void putSet(DerValue[] set) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        for (i = 0; i < set.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            set[i].encode(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        write(DerValue.tag_Set, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Marshals the contents of a set on the output stream.  Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * are semantically unordered, but DER requires that encodings of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * set elements be sorted into ascending lexicographical order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * before being output.  Hence sets with the same tags and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * elements have the same DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * This method supports the ASN.1 "SET OF" construct, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * "SET", which uses a different order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void putOrderedSetOf(byte tag, DerEncoder[] set) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        putOrderedSet(tag, set, lexOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Marshals the contents of a set on the output stream.  Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * are semantically unordered, but DER requires that encodings of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * set elements be sorted into ascending tag order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * before being output.  Hence sets with the same tags and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * elements have the same DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * This method supports the ASN.1 "SET" construct, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * "SET OF", which uses a different order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public void putOrderedSet(byte tag, DerEncoder[] set) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        putOrderedSet(tag, set, tagOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *  Lexicographical order comparison on byte arrays, for ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *  elements of a SET OF objects in DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private static ByteArrayLexOrder lexOrder = new ByteArrayLexOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *  Tag order comparison on byte arrays, for ordering elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *  SET objects in DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static ByteArrayTagOrder tagOrder = new ByteArrayTagOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Marshals a the contents of a set on the output stream with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * encodings of its sorted in increasing order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param order the order to use when sorting encodings of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private void putOrderedSet(byte tag, DerEncoder[] set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                               Comparator<byte[]> order) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        DerOutputStream[] streams = new DerOutputStream[set.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        for (int i = 0; i < set.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            streams[i] = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            set[i].derEncode(streams[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // order the element encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        byte[][] bufs = new byte[streams.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (int i = 0; i < streams.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            bufs[i] = streams[i].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Arrays.<byte[]>sort(bufs, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        for (int i = 0; i < streams.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            bytes.write(bufs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        write(tag, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Marshals a string as a DER encoded UTF8String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void putUTF8String(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        writeString(s, DerValue.tag_UTF8String, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Marshals a string as a DER encoded PrintableString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public void putPrintableString(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        writeString(s, DerValue.tag_PrintableString, "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Marshals a string as a DER encoded T61String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public void putT61String(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         * Works for characters that are defined in both ASCII and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * T61.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        writeString(s, DerValue.tag_T61String, "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Marshals a string as a DER encoded IA5String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void putIA5String(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        writeString(s, DerValue.tag_IA5String, "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Marshals a string as a DER encoded BMPString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public void putBMPString(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        writeString(s, DerValue.tag_BMPString, "UnicodeBigUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Marshals a string as a DER encoded GeneralString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void putGeneralString(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        writeString(s, DerValue.tag_GeneralString, "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Private helper routine for writing DER encoded string values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param s the string to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param stringTag one of the DER string tags that indicate which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * encoding should be used to write the string out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param enc the name of the encoder that should be used corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * to the above tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private void writeString(String s, byte stringTag, String enc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        byte[] data = s.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        write(stringTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        putLength(data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Marshals a DER UTC time/date value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   464
     * and with seconds (even if seconds=0) as per RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public void putUTCTime(Date d) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        putTime(d, DerValue.tag_UtcTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Marshals a DER Generalized Time/date value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   474
     * and with seconds (even if seconds=0) as per RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public void putGeneralizedTime(Date d) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        putTime(d, DerValue.tag_GeneralizedTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Private helper routine for marshalling a DER UTC/Generalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * time/date value. If the tag specified is not that for UTC Time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * then it defaults to Generalized Time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param d the date to be marshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param tag the tag for UTC Time or Generalized Time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private void putTime(Date d, byte tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * Format the date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        TimeZone tz = TimeZone.getTimeZone("GMT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        String pattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (tag == DerValue.tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            pattern = "yyMMddHHmmss'Z'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            tag = DerValue.tag_GeneralizedTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            pattern = "yyyyMMddHHmmss'Z'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
6109
007c43113d09 6670889: Keystore created under Hindi Locale causing ArrayIndexOutOfBoundsException
weijun
parents: 5506
diff changeset
   503
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        sdf.setTimeZone(tz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        byte[] time = (sdf.format(d)).getBytes("ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * Write the formatted date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        putLength(time.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        write(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Put the encoding of the length in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   519
     * @param len the length of the attribute.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @exception IOException on writing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public void putLength(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (len < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            write((byte)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        } else if (len < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            write((byte)0x081);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            write((byte)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } else if (len < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            write((byte)0x082);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            write((byte)(len >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            write((byte)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } else if (len < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            write((byte)0x083);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            write((byte)(len >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            write((byte)(len >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            write((byte)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            write((byte)0x084);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            write((byte)(len >> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            write((byte)(len >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            write((byte)(len >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            write((byte)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Put the tag of the attribute in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   553
     * @param tagClass the tag class type, one of UNIVERSAL, CONTEXT,
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   554
     *        APPLICATION or PRIVATE
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   555
     * @param form if true, the value is constructed, otherwise it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * primitive.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   557
     * @param val the tag value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public void putTag(byte tagClass, boolean form, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        byte tag = (byte)(tagClass | val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (form) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            tag |= (byte)0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *  Write the current contents of this <code>DerOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *  to an <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *  @exception IOException on output error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public void derEncode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        out.write(toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
}