< prev index next >

## src/java.security.jgss/share/classes/javax/security/auth/kerberos/KeyImpl.java

Print this page

      1 /*
      2  * Copyright (c) 2000, 2017, 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
    159     }
    160
    161     public final byte[] getEncoded() {
    162         if (destroyed)
    163             throw new IllegalStateException("This key is no longer valid");
    164         return keyBytes.clone();
    165     }
    166
    167     public void destroy() throws DestroyFailedException {
    168         if (!destroyed) {
    169             destroyed = true;
    170             Arrays.fill(keyBytes, (byte) 0);
    171         }
    172     }
    173
    174     public boolean isDestroyed() {
    175         return destroyed;
    176     }
    177
    178     /**

    179      * @serialData this {@code KeyImpl} is serialized by
    180      * writing out the ASN1 Encoded bytes of the encryption key.
    181      * The ASN1 encoding is defined in RFC4120 and as  follows:
    182      * EncryptionKey   ::= SEQUENCE {
    183      *          keytype    [0] Int32 -- actually encryption type --,
    184      *          keyvalue   [1] OCTET STRING

    185      * }
    186      */
    187     private void writeObject(ObjectOutputStream ois)
    188                 throws IOException {
    189         if (destroyed) {
    190            throw new IOException("This key is no longer valid");
    191         }
    192
    193         try {
    194            ois.writeObject((new EncryptionKey(keyType, keyBytes)).asn1Encode());
    195         } catch (Asn1Exception ae) {
    196            throw new IOException(ae.getMessage());
    197         }
    198     }
    199

    200     private void readObject(ObjectInputStream ois)
    201                 throws IOException, ClassNotFoundException {
    202         try {
    203             EncryptionKey encKey = new EncryptionKey(new
    204                                      DerValue((byte[])ois.readObject()));
    205             keyType = encKey.getEType();
    206             keyBytes = encKey.getBytes();
    207         } catch (Asn1Exception ae) {
    208             throw new IOException(ae.getMessage());
    209         }
    210     }
    211
    212     public String toString() {
    213         HexDumpEncoder hd = new HexDumpEncoder();
    214         return "EncryptionKey: keyType=" + keyType
    215                           + " keyBytes (hex dump)="
    216                           + (keyBytes == null || keyBytes.length == 0 ?
    217                              " Empty Key" :
    218                              '\n' + hd.encodeBuffer(keyBytes)
    219                           + '\n');
      1 /*
      2  * Copyright (c) 2000, 2021, 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
    159     }
    160
    161     public final byte[] getEncoded() {
    162         if (destroyed)
    163             throw new IllegalStateException("This key is no longer valid");
    164         return keyBytes.clone();
    165     }
    166
    167     public void destroy() throws DestroyFailedException {
    168         if (!destroyed) {
    169             destroyed = true;
    170             Arrays.fill(keyBytes, (byte) 0);
    171         }
    172     }
    173
    174     public boolean isDestroyed() {
    175         return destroyed;
    176     }
    177
    178     /**
    179      * Writes the state of this object to the stream.
    180
    181      * @serialData this {@code KeyImpl} is serialized by
    182      * writing out the ASN1 Encoded bytes of the encryption key.
    183      * The ASN1 encoding is defined in RFC4120 and as  follows:
    184      * EncryptionKey   ::= SEQUENCE {
    185      *          keytype    [0] Int32 -- actually encryption type --,
    186      *          keyvalue   [1] OCTET STRING
    187      *
    188      * @param  oos the {@code ObjectOutputStream} to which data is written
    189      * @throws IOException if an I/O error occurs
    190      * }
    191      */
    192     private void writeObject(ObjectOutputStream oos)
    193                 throws IOException {
    194         if (destroyed) {
    195            throw new IOException("This key is no longer valid");
    196         }
    197
    198         try {
    199            oos.writeObject((new EncryptionKey(keyType, keyBytes)).asn1Encode());
    200         } catch (Asn1Exception ae) {
    201            throw new IOException(ae.getMessage());
    202         }
    203     }
    204
    205     /**
    206      * Restores the state of this object from the stream.
    207      *
    208      * @param  ois the {@code ObjectInputStream} from which data is read
    209      * @throws IOException if an I/O error occurs
    210      * @throws ClassNotFoundException if a serialized class cannot be loaded
    211      */
    212     private void readObject(ObjectInputStream ois)
    213                 throws IOException, ClassNotFoundException {
    214         try {
    215             EncryptionKey encKey = new EncryptionKey(new
    216                                      DerValue((byte[])ois.readObject()));
    217             keyType = encKey.getEType();
    218             keyBytes = encKey.getBytes();
    219         } catch (Asn1Exception ae) {
    220             throw new IOException(ae.getMessage());
    221         }
    222     }
    223
    224     public String toString() {
    225         HexDumpEncoder hd = new HexDumpEncoder();
    226         return "EncryptionKey: keyType=" + keyType
    227                           + " keyBytes (hex dump)="
    228                           + (keyBytes == null || keyBytes.length == 0 ?
    229                              " Empty Key" :
    230                              '\n' + hd.encodeBuffer(keyBytes)
    231                           + '\n');

< prev index next >