View Javadoc
1   /*
2    * Copyright (c) 1997, 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
23   * questions.
24   */
25  package sun.security.x509;
26  
27  import java.io.IOException;
28  import java.math.BigInteger;
29  
30  import sun.misc.HexDumpEncoder;
31  import sun.security.util.*;
32  
33  /**
34   * This class defines the UniqueIdentity class used by certificates.
35   *
36   * @author Amit Kapoor
37   * @author Hemma Prafullchandra
38   */
39  public class UniqueIdentity {
40      // Private data members
41      private BitArray    id;
42  
43      /**
44       * The default constructor for this class.
45       *
46       * @param id the byte array containing the unique identifier.
47       */
48      public UniqueIdentity(BitArray id) {
49          this.id = id;
50      }
51  
52      /**
53       * The default constructor for this class.
54       *
55       * @param id the byte array containing the unique identifier.
56       */
57      public UniqueIdentity(byte[] id) {
58          this.id = new BitArray(id.length*8, id);
59      }
60  
61      /**
62       * Create the object, decoding the values from the passed DER stream.
63       *
64       * @param in the DerInputStream to read the UniqueIdentity from.
65       * @exception IOException on decoding errors.
66       */
67      public UniqueIdentity(DerInputStream in) throws IOException {
68          DerValue derVal = in.getDerValue();
69          id = derVal.getUnalignedBitString(true);
70      }
71  
72      /**
73       * Create the object, decoding the values from the passed DER stream.
74       *
75       * @param derVal the DerValue decoded from the stream.
76       * @param tag the tag the value is encoded under.
77       * @exception IOException on decoding errors.
78       */
79      public UniqueIdentity(DerValue derVal) throws IOException {
80          id = derVal.getUnalignedBitString(true);
81      }
82  
83      /**
84       * Return the UniqueIdentity as a printable string.
85       */
86      public String toString() {
87          return ("UniqueIdentity:" + id.toString() + "\n");
88      }
89  
90      /**
91       * Encode the UniqueIdentity in DER form to the stream.
92       *
93       * @param out the DerOutputStream to marshal the contents to.
94       * @param tag enocode it under the following tag.
95       * @exception IOException on errors.
96       */
97      public void encode(DerOutputStream out, byte tag) throws IOException {
98          byte[] bytes = id.toByteArray();
99          int excessBits = bytes.length*8 - id.length();
100 
101         out.write(tag);
102         out.putLength(bytes.length + 1);
103 
104         out.write(excessBits);
105         out.write(bytes);
106     }
107 
108     /**
109      * Return the unique id.
110      */
111     public boolean[] getId() {
112         if (id == null) return null;
113 
114         return id.toBooleanArray();
115     }
116 }