< prev index next >

## src/java.base/share/classes/java/security/SignedObject.java

Print this page

      1 /*
      2  * Copyright (c) 1997, 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
    101  * internally to any Java runtime as an unforgeable authorization
    102  * token -- one that can be passed around without the fear that the
    103  * token can be maliciously modified without being detected.
    104  * <li> It
    105  * can be used to sign and serialize data/object for storage outside
    106  * the Java runtime (e.g., storing critical access control data on
    107  * disk).
    108  * <li> Nested SignedObjects can be used to construct a logical
    109  * sequence of signatures, resembling a chain of authorization and
    110  * delegation.
    111  * </ul>
    112  *
    113  * @see Signature
    114  *
    115  * @author Li Gong
    116  * @since 1.2
    117  */
    118
    119 public final class SignedObject implements Serializable {
    120
    121     @java.io.Serial
    122     private static final long serialVersionUID = 720502720485447167L;
    123
    124     /*
    125      * The original content is "deep copied" in its serialized format
    126      * and stored in a byte array.  The signature field is also in the
    127      * form of byte array.
    128      */
    129
    130     private byte[] content;

    131     private byte[] signature;

    132     private String thealgorithm;
    133
    134     /**
    135      * Constructs a SignedObject from any Serializable object.
    136      * The given object is signed with the given signing key, using the
    137      * designated signature engine.
    138      *
    139      * @param object the object to be signed.
    140      * @param signingKey the private key for signing.
    141      * @param signingEngine the signature signing engine.
    142      *
    143      * @throws    IOException if an error occurs during serialization
    144      * @throws    InvalidKeyException if the key is invalid.
    145      * @throws    SignatureException if signing fails.
    146      */
    147     public SignedObject(Serializable object, PrivateKey signingKey,
    148                         Signature signingEngine)
    149         throws IOException, InvalidKeyException, SignatureException {
    150             // creating a stream pipe-line, from a to b
    151             ByteArrayOutputStream b = new ByteArrayOutputStream();
    233      * designated signature engine.
    234      *
    235      * @param signingKey the private key for signing.
    236      * @param signingEngine the signature signing engine.
    237      *
    238      * @throws    InvalidKeyException if the key is invalid.
    239      * @throws    SignatureException if signing fails.
    240      */
    241     private void sign(PrivateKey signingKey, Signature signingEngine)
    242         throws InvalidKeyException, SignatureException {
    243             // initialize the signing engine
    244             signingEngine.initSign(signingKey);
    245             signingEngine.update(this.content.clone());
    246             this.signature = signingEngine.sign().clone();
    247             this.thealgorithm = signingEngine.getAlgorithm();
    248     }
    249
    250     /**
    251      * readObject is called to restore the state of the SignedObject from
    252      * a stream.

    253      */
    254     @java.io.Serial
    255     private void readObject(java.io.ObjectInputStream s)
    256         throws java.io.IOException, ClassNotFoundException {
    257             java.io.ObjectInputStream.GetField fields = s.readFields();
    258             content = ((byte[])fields.get("content", null)).clone();
    259             signature = ((byte[])fields.get("signature", null)).clone();
    260             thealgorithm = (String)fields.get("thealgorithm", null);
    261     }
    262 }
      1 /*
      2  * Copyright (c) 1997, 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
    101  * internally to any Java runtime as an unforgeable authorization
    102  * token -- one that can be passed around without the fear that the
    103  * token can be maliciously modified without being detected.
    104  * <li> It
    105  * can be used to sign and serialize data/object for storage outside
    106  * the Java runtime (e.g., storing critical access control data on
    107  * disk).
    108  * <li> Nested SignedObjects can be used to construct a logical
    109  * sequence of signatures, resembling a chain of authorization and
    110  * delegation.
    111  * </ul>
    112  *
    113  * @see Signature
    114  *
    115  * @author Li Gong
    116  * @since 1.2
    117  */
    118
    119 public final class SignedObject implements Serializable {
    120
    121     @Serial
    122     private static final long serialVersionUID = 720502720485447167L;
    123
    124     /**
    125      * The original content is "deep copied" in its serialized format
    126      * and stored in a byte array.

    127      */

    128     private byte[] content;
    129
    130     /**
    131      * The signature field is stored as a byte array.
    132      */
    133     private byte[] signature;
    134
    135     /**
    136      * The algorithm used to sign the object.
    137      */
    138     private String thealgorithm;
    139
    140     /**
    141      * Constructs a SignedObject from any Serializable object.
    142      * The given object is signed with the given signing key, using the
    143      * designated signature engine.
    144      *
    145      * @param object the object to be signed.
    146      * @param signingKey the private key for signing.
    147      * @param signingEngine the signature signing engine.
    148      *
    149      * @throws    IOException if an error occurs during serialization
    150      * @throws    InvalidKeyException if the key is invalid.
    151      * @throws    SignatureException if signing fails.
    152      */
    153     public SignedObject(Serializable object, PrivateKey signingKey,
    154                         Signature signingEngine)
    155         throws IOException, InvalidKeyException, SignatureException {
    156             // creating a stream pipe-line, from a to b
    157             ByteArrayOutputStream b = new ByteArrayOutputStream();
    239      * designated signature engine.
    240      *
    241      * @param signingKey the private key for signing.
    242      * @param signingEngine the signature signing engine.
    243      *
    244      * @throws    InvalidKeyException if the key is invalid.
    245      * @throws    SignatureException if signing fails.
    246      */
    247     private void sign(PrivateKey signingKey, Signature signingEngine)
    248         throws InvalidKeyException, SignatureException {
    249             // initialize the signing engine
    250             signingEngine.initSign(signingKey);
    251             signingEngine.update(this.content.clone());
    252             this.signature = signingEngine.sign().clone();
    253             this.thealgorithm = signingEngine.getAlgorithm();
    254     }
    255
    256     /**
    257      * readObject is called to restore the state of the SignedObject from
    258      * a stream.
    259      *
    260      * @param  s the {@code ObjectInputStream} from which data is read
    261      * @throws IOException if an I/O error occurs
    262      * @throws ClassNotFoundException if a serialized class cannot be loaded
    263      */
    264     @Serial
    265     private void readObject(ObjectInputStream s)
    266         throws IOException, ClassNotFoundException {
    267             ObjectInputStream.GetField fields = s.readFields();
    268             content = ((byte[])fields.get("content", null)).clone();
    269             signature = ((byte[])fields.get("signature", null)).clone();
    270             thealgorithm = (String)fields.get("thealgorithm", null);
    271     }
    272 }

< prev index next >