< prev index next >

## src/java.base/share/classes/javax/crypto/SealedObject.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
    404                 c.init(Cipher.DECRYPT_MODE, key, params);
    405             else
    406                 c.init(Cipher.DECRYPT_MODE, key);
    407         } catch (InvalidAlgorithmParameterException iape) {
    408             // this should never happen, because we use the exact same
    409             // parameters that were used in the sealing operation
    410             throw new RuntimeException(iape.getMessage());
    411         }
    412
    413         ObjectInput a = getExtObjectInputStream(c);
    414         try {
    415             Object obj = a.readObject();
    416             return obj;
    417         } finally {
    418             a.close();
    419         }
    420     }
    421
    422     /**
    423      * Restores the state of the SealedObject from a stream.

    424      * @param s the object input stream.
    425      * @exception NullPointerException if s is null.

    426      */
    427     @java.io.Serial
    428     private void readObject(java.io.ObjectInputStream s)
    429         throws java.io.IOException, ClassNotFoundException
    430     {
    431         s.defaultReadObject();
    432         if (encryptedContent != null)
    433             encryptedContent = encryptedContent.clone();
    434         if (encodedParams != null)
    435             encodedParams = encodedParams.clone();
    436     }
    437
    438     // This method is also called inside SealedObjectForKeyProtector.java.
    439     private ObjectInputStream getExtObjectInputStream(Cipher c)
    440             throws BadPaddingException, IllegalBlockSizeException, IOException {
    441
    442         byte[] content = c.doFinal(this.encryptedContent);
    443         ByteArrayInputStream b = new ByteArrayInputStream(content);
    444         return new extObjectInputStream(b);
    445     }
      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
    404                 c.init(Cipher.DECRYPT_MODE, key, params);
    405             else
    406                 c.init(Cipher.DECRYPT_MODE, key);
    407         } catch (InvalidAlgorithmParameterException iape) {
    408             // this should never happen, because we use the exact same
    409             // parameters that were used in the sealing operation
    410             throw new RuntimeException(iape.getMessage());
    411         }
    412
    413         ObjectInput a = getExtObjectInputStream(c);
    414         try {
    415             Object obj = a.readObject();
    416             return obj;
    417         } finally {
    418             a.close();
    419         }
    420     }
    421
    422     /**
    423      * Restores the state of the SealedObject from a stream.
    424      *
    425      * @param s the object input stream.
    426      * @throws IOException if an I/O error occurs
    427      * @throws ClassNotFoundException if a serialized class cannot be loaded
    428      * @throws NullPointerException if s is null.
    429      */
    430     @java.io.Serial
    431     private void readObject(java.io.ObjectInputStream s)
    432         throws java.io.IOException, ClassNotFoundException
    433     {
    434         s.defaultReadObject();
    435         if (encryptedContent != null)
    436             encryptedContent = encryptedContent.clone();
    437         if (encodedParams != null)
    438             encodedParams = encodedParams.clone();
    439     }
    440
    441     // This method is also called inside SealedObjectForKeyProtector.java.
    442     private ObjectInputStream getExtObjectInputStream(Cipher c)
    443             throws BadPaddingException, IllegalBlockSizeException, IOException {
    444
    445         byte[] content = c.doFinal(this.encryptedContent);
    446         ByteArrayInputStream b = new ByteArrayInputStream(content);
    447         return new extObjectInputStream(b);
    448     }

< prev index next >