< prev index next >

## src/java.base/share/classes/java/security/CodeSource.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
    505         } else {
    506             sb.append(" <no signer certificates>");
    507         }
    508         sb.append(")");
    509         return sb.toString();
    510     }
    511
    512     /**
    513      * Writes this object out to a stream (i.e., serializes it).
    514      *
    515      * @serialData An initial {@code URL} is followed by an
    516      * {@code int} indicating the number of certificates to follow
    517      * (a value of "zero" denotes that there are no certificates associated
    518      * with this object).
    519      * Each certificate is written out starting with a {@code String}
    520      * denoting the certificate type, followed by an
    521      * {@code int} specifying the length of the certificate encoding,
    522      * followed by the certificate encoding itself which is written out as an
    523      * array of bytes. Finally, if any code signers are present then the array
    524      * of code signers is serialized and written out too.

    525      */
    526     @java.io.Serial
    527     private void writeObject(java.io.ObjectOutputStream oos)
    528         throws IOException
    529     {
    530         oos.defaultWriteObject(); // location
    531
    532         // Serialize the array of certs
    533         if (certs == null || certs.length == 0) {
    534             oos.writeInt(0);
    535         } else {
    536             // write out the total number of certs
    537             oos.writeInt(certs.length);
    538             // write out each cert, including its type
    539             for (int i = 0; i < certs.length; i++) {
    540                 java.security.cert.Certificate cert = certs[i];
    541                 try {
    542                     oos.writeUTF(cert.getType());
    543                     byte[] encoded = cert.getEncoded();
    544                     oos.writeInt(encoded.length);
    545                     oos.write(encoded);
    546                 } catch (CertificateEncodingException cee) {
    547                     throw new IOException(cee.getMessage());
    548                 }
    549             }
    550         }
    551
    552         // Serialize the array of code signers (if any)
    553         if (signers != null && signers.length > 0) {
    554             oos.writeObject(signers);
    555         }
    556     }
    557
    558     /**
    559      * Restores this object from a stream (i.e., deserializes it).

    560      */
    561     @java.io.Serial
    562     private void readObject(java.io.ObjectInputStream ois)
    563         throws IOException, ClassNotFoundException
    564     {
    565         CertificateFactory cf;
    566         Hashtable<String, CertificateFactory> cfs = null;
    567         List<java.security.cert.Certificate> certList = null;
    568
    569         ois.defaultReadObject(); // location
    570
    571         // process any new-style certs in the stream (if present)
    572         int size = ois.readInt();
    573         if (size > 0) {
    574             // we know of 3 different cert types: X.509, PGP, SDSI, which
    575             // could all be present in the stream at the same time
    576             cfs = new Hashtable<>(3);
    577             certList = new ArrayList<>(size > 20 ? 20 : size);
    578         } else if (size < 0) {
    579             throw new IOException("size cannot be negative");
      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
    505         } else {
    506             sb.append(" <no signer certificates>");
    507         }
    508         sb.append(")");
    509         return sb.toString();
    510     }
    511
    512     /**
    513      * Writes this object out to a stream (i.e., serializes it).
    514      *
    515      * @serialData An initial {@code URL} is followed by an
    516      * {@code int} indicating the number of certificates to follow
    517      * (a value of "zero" denotes that there are no certificates associated
    518      * with this object).
    519      * Each certificate is written out starting with a {@code String}
    520      * denoting the certificate type, followed by an
    521      * {@code int} specifying the length of the certificate encoding,
    522      * followed by the certificate encoding itself which is written out as an
    523      * array of bytes. Finally, if any code signers are present then the array
    524      * of code signers is serialized and written out too.
    525      *
    526      * @param  oos the {@code ObjectOutputStream} to which data is written
    527      * @throws IOException if an I/O error occurs
    528      */
    529     @java.io.Serial
    530     private void writeObject(java.io.ObjectOutputStream oos)
    531         throws IOException
    532     {
    533         oos.defaultWriteObject(); // location
    534
    535         // Serialize the array of certs
    536         if (certs == null || certs.length == 0) {
    537             oos.writeInt(0);
    538         } else {
    539             // write out the total number of certs
    540             oos.writeInt(certs.length);
    541             // write out each cert, including its type
    542             for (int i = 0; i < certs.length; i++) {
    543                 java.security.cert.Certificate cert = certs[i];
    544                 try {
    545                     oos.writeUTF(cert.getType());
    546                     byte[] encoded = cert.getEncoded();
    547                     oos.writeInt(encoded.length);
    548                     oos.write(encoded);
    549                 } catch (CertificateEncodingException cee) {
    550                     throw new IOException(cee.getMessage());
    551                 }
    552             }
    553         }
    554
    555         // Serialize the array of code signers (if any)
    556         if (signers != null && signers.length > 0) {
    557             oos.writeObject(signers);
    558         }
    559     }
    560
    561     /**
    562      * Restores this object from a stream (i.e., deserializes it).
    563      *
    564      * @param  ois the {@code ObjectInputStream} from which data is read
    565      * @throws IOException if an I/O error occurs
    566      * @throws ClassNotFoundException if a serialized class cannot be loaded
    567      */
    568     @java.io.Serial
    569     private void readObject(java.io.ObjectInputStream ois)
    570         throws IOException, ClassNotFoundException
    571     {
    572         CertificateFactory cf;
    573         Hashtable<String, CertificateFactory> cfs = null;
    574         List<java.security.cert.Certificate> certList = null;
    575
    576         ois.defaultReadObject(); // location
    577
    578         // process any new-style certs in the stream (if present)
    579         int size = ois.readInt();
    580         if (size > 0) {
    581             // we know of 3 different cert types: X.509, PGP, SDSI, which
    582             // could all be present in the stream at the same time
    583             cfs = new Hashtable<>(3);
    584             certList = new ArrayList<>(size > 20 ? 20 : size);
    585         } else if (size < 0) {
    586             throw new IOException("size cannot be negative");

< prev index next >