src/java.base/share/classes/java/security/SignedObject.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2019, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <p> SignedObject is a class for the purpose of creating authentic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * runtime objects whose integrity cannot be compromised without being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> More specifically, a SignedObject contains another Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * object, the (to-be-)signed object and its signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> The signed object is a "deep copy" (in serialized form) of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * original object.  Once the copy is made, further manipulation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the original object has no side effect on the copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> The underlying signing algorithm is designated by the Signature
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    43
 * object passed to the constructor and the {@code verify} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A typical usage for signing is the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
    46
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Signature signingEngine = Signature.getInstance(algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *                                                 provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * SignedObject so = new SignedObject(myobject, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *                                    signingEngine);
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    51
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> A typical usage for verification is the following (having
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    54
 * received SignedObject {@code so}):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
    56
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Signature verificationEngine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     Signature.getInstance(algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * if (so.verify(publickey, verificationEngine))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *         Object myobj = so.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     } catch (java.lang.ClassNotFoundException e) {};
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    63
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> Several points are worth noting.  First, there is no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * initialize the signing or verification engine, as it will be
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    67
 * re-initialized inside the constructor and the {@code verify}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * method. Secondly, for verification to succeed, the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * public key must be the public key corresponding to the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * used to generate the SignedObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> More importantly, for flexibility reasons, the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    73
 * constructor and {@code verify} method allow for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * customized signature engines, which can implement signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * algorithms that are not installed formally as part of a crypto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * provider.  However, it is crucial that the programmer writing the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    77
 * verifier code be aware what {@code Signature} engine is being
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    78
 * used, as its own implementation of the {@code verify} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * is invoked to verify a signature.  In other words, a malicious
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    80
 * {@code Signature} may choose to always return true on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * verification in an attempt to bypass a security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> The signature algorithm can be, among others, the NIST standard
46053
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45434
diff changeset
    84
 * DSA, using DSA and SHA-256.  The algorithm is specified using the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * same convention as that for signatures. The DSA algorithm using the
46053
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45434
diff changeset
    86
 * SHA-256 message digest algorithm can be specified, for example, as
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45434
diff changeset
    87
 * "SHA256withDSA".  In the case of
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45434
diff changeset
    88
 * RSA the signing algorithm could be specified as, for example,
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45434
diff changeset
    89
 * "SHA256withRSA".  The algorithm name must be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * specified, as there is no default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p> The name of the Cryptography Package Provider is designated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * also by the Signature parameter to the constructor and the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
    94
 * {@code verify} method.  If the provider is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * specified, the default provider is used.  Each installation can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * be configured to use a particular provider as default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p> Potential applications of SignedObject include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <li> It can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * internally to any Java runtime as an unforgeable authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * token -- one that can be passed around without the fear that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * token can be maliciously modified without being detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <li> It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * can be used to sign and serialize data/object for storage outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * the Java runtime (e.g., storing critical access control data on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * disk).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <li> Nested SignedObjects can be used to construct a logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * sequence of signatures, resembling a chain of authorization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see Signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @author Li Gong
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 25859
diff changeset
   116
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public final class SignedObject implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   121
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final long serialVersionUID = 720502720485447167L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The original content is "deep copied" in its serialized format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * and stored in a byte array.  The signature field is also in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * form of byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private byte[] content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private byte[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private String thealgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Constructs a SignedObject from any Serializable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The given object is signed with the given signing key, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * designated signature engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param object the object to be signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param signingKey the private key for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param signingEngine the signature signing engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   143
     * @throws    IOException if an error occurs during serialization
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   144
     * @throws    InvalidKeyException if the key is invalid.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   145
     * @throws    SignatureException if signing fails.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public SignedObject(Serializable object, PrivateKey signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        Signature signingEngine)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        throws IOException, InvalidKeyException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // creating a stream pipe-line, from a to b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            ByteArrayOutputStream b = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            ObjectOutput a = new ObjectOutputStream(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // write and flush the object content to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            a.writeObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            a.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            a.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            this.content = b.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            b.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // now sign the encapsulated object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            this.sign(signingKey, signingEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Retrieves the encapsulated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The encapsulated object is de-serialized before it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return the encapsulated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   171
     * @throws    IOException if an error occurs during de-serialization
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   172
     * @throws    ClassNotFoundException if an error occurs during
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * de-serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public Object getObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // creating a stream pipe-line, from b to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ByteArrayInputStream b = new ByteArrayInputStream(this.content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ObjectInput a = new ObjectInputStream(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Object obj = a.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        b.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        a.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Retrieves the signature on the signed object, in the form of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the signature. Returns a new array each time this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public byte[] getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return this.signature.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Retrieves the name of the signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return the signature algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return this.thealgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Verifies that the signature in this SignedObject is the valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * signature for the object stored inside, with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * verification key, using the designated verification engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param verificationKey the public key for verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param verificationEngine the signature verification engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   215
     * @throws    SignatureException if signature verification failed (an
25524
e623ca817d50 4867890: Clarify the return value/exception for java.security.SignedObject.verify
mullan
parents: 21334
diff changeset
   216
     *     exception prevented the signature verification engine from completing
e623ca817d50 4867890: Clarify the return value/exception for java.security.SignedObject.verify
mullan
parents: 21334
diff changeset
   217
     *     normally).
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   218
     * @throws    InvalidKeyException if the verification key is invalid.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
   220
     * @return {@code true} if the signature
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9826
diff changeset
   221
     * is valid, {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean verify(PublicKey verificationKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                          Signature verificationEngine)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         throws InvalidKeyException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
             verificationEngine.initVerify(verificationKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
             verificationEngine.update(this.content.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
             return verificationEngine.verify(this.signature.clone());
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
     * Signs the encapsulated object with the given signing key, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * designated signature engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param signingKey the private key for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param signingEngine the signature signing engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   238
     * @throws    InvalidKeyException if the key is invalid.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   239
     * @throws    SignatureException if signing fails.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private void sign(PrivateKey signingKey, Signature signingEngine)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        throws InvalidKeyException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // initialize the signing engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            signingEngine.initSign(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            signingEngine.update(this.content.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.signature = signingEngine.sign().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            this.thealgorithm = signingEngine.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * readObject is called to restore the state of the SignedObject from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   254
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private void readObject(java.io.ObjectInputStream s)
9826
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents: 5506
diff changeset
   256
        throws java.io.IOException, ClassNotFoundException {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents: 5506
diff changeset
   257
            java.io.ObjectInputStream.GetField fields = s.readFields();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents: 5506
diff changeset
   258
            content = ((byte[])fields.get("content", null)).clone();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents: 5506
diff changeset
   259
            signature = ((byte[])fields.get("signature", null)).clone();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents: 5506
diff changeset
   260
            thealgorithm = (String)fields.get("thealgorithm", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}