jdk/test/java/security/SignedObject/Correctness.java
author weijun
Wed, 09 Feb 2011 11:50:29 +0800
changeset 9826 0f553990ca93
permissions -rw-r--r--
6618658: Deserialization allows creation of mutable SignedObject Reviewed-by: hawtin, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9826
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     1
/*
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     4
 *
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     8
 *
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    13
 * accompanied this code).
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    14
 *
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    18
 *
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    21
 * questions.
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    22
 */
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    23
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    24
/*
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    25
 * @test
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    26
 * @bug 6618658
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    27
 * @summary Deserialization allows creation of mutable SignedObject
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    28
 */
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    29
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    32
import java.io.ObjectInputStream;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    34
import java.security.KeyPair;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    35
import java.security.KeyPairGenerator;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    36
import java.security.Signature;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    37
import java.security.SignedObject;
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    38
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    39
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    40
public class Correctness {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    41
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    43
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    44
        String SIGALG = "SHA1withRSA";
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    45
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    46
        KeyPair kp = kpg.generateKeyPair();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    47
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    48
        SignedObject so1 = new SignedObject("Hello", kp.getPrivate(),
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    49
                Signature.getInstance(SIGALG));
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    50
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    51
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    52
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    53
        out.writeObject(so1);
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    54
        out.close();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    55
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    56
        byte[] data = byteOut.toByteArray();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    57
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    58
        SignedObject so2 = (SignedObject)new ObjectInputStream(
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    59
                new ByteArrayInputStream(data)).readObject();
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    60
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    61
        if (!so2.getObject().equals("Hello")) {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    62
            throw new Exception("Content changed");
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    63
        }
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    64
        if (!so2.getAlgorithm().equals(SIGALG)) {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    65
            throw new Exception("Signature algorithm unknown");
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    66
        }
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    67
        if (!so2.verify(kp.getPublic(), Signature.getInstance(SIGALG))) {
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    68
            throw new Exception("Not verified");
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    69
        }
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    70
    }
0f553990ca93 6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
diff changeset
    71
}