src/java.base/share/classes/com/sun/crypto/provider/JceKeyStore.java
changeset 47416 a627f88bed3a
parent 47216 71c04702a3d5
child 49783 977c6dd636bd
equal deleted inserted replaced
47415:354a527f3246 47416:a627f88bed3a
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    25 
    25 
    26 package com.sun.crypto.provider;
    26 package com.sun.crypto.provider;
    27 
    27 
    28 import java.io.*;
    28 import java.io.*;
    29 import java.util.*;
    29 import java.util.*;
       
    30 import java.security.AccessController;
    30 import java.security.DigestInputStream;
    31 import java.security.DigestInputStream;
    31 import java.security.DigestOutputStream;
    32 import java.security.DigestOutputStream;
    32 import java.security.MessageDigest;
    33 import java.security.MessageDigest;
    33 import java.security.NoSuchAlgorithmException;
    34 import java.security.NoSuchAlgorithmException;
    34 import java.security.Key;
    35 import java.security.Key;
    35 import java.security.PrivateKey;
    36 import java.security.PrivateKey;
       
    37 import java.security.PrivilegedAction;
    36 import java.security.KeyStoreSpi;
    38 import java.security.KeyStoreSpi;
    37 import java.security.KeyStoreException;
    39 import java.security.KeyStoreException;
    38 import java.security.UnrecoverableKeyException;
    40 import java.security.UnrecoverableKeyException;
    39 import java.security.cert.Certificate;
    41 import java.security.cert.Certificate;
    40 import java.security.cert.CertificateFactory;
    42 import java.security.cert.CertificateFactory;
   833                         entry.date = new Date(dis.readLong());
   835                         entry.date = new Date(dis.readLong());
   834 
   836 
   835                         // read the sealed key
   837                         // read the sealed key
   836                         try {
   838                         try {
   837                             ois = new ObjectInputStream(dis);
   839                             ois = new ObjectInputStream(dis);
       
   840                             final ObjectInputStream ois2 = ois;
       
   841                             // Set a deserialization checker
       
   842                             AccessController.doPrivileged(
       
   843                                 (PrivilegedAction<Void>)() -> {
       
   844                                     ois2.setObjectInputFilter(
       
   845                                         new DeserializationChecker());
       
   846                                     return null;
       
   847                             });
   838                             entry.sealedKey = (SealedObject)ois.readObject();
   848                             entry.sealedKey = (SealedObject)ois.readObject();
   839                             // NOTE: don't close ois here since we are still
   849                             // NOTE: don't close ois here since we are still
   840                             // using dis!!!
   850                             // using dis!!!
   841                         } catch (ClassNotFoundException cnfe) {
   851                         } catch (ClassNotFoundException cnfe) {
   842                             throw new IOException(cnfe.getMessage());
   852                             throw new IOException(cnfe.getMessage());
       
   853                         } catch (InvalidClassException ice) {
       
   854                             throw new IOException("Invalid secret key format");
   843                         }
   855                         }
   844 
   856 
   845                         // Add the entry to the list
   857                         // Add the entry to the list
   846                         entries.put(alias, entry);
   858                         entries.put(alias, entry);
   847 
   859 
   914             dataStream = new DataInputStream(stream);
   926             dataStream = new DataInputStream(stream);
   915         }
   927         }
   916 
   928 
   917         return JCEKS_MAGIC == dataStream.readInt();
   929         return JCEKS_MAGIC == dataStream.readInt();
   918     }
   930     }
       
   931 
       
   932     /*
       
   933      * An ObjectInputFilter that checks the format of the secret key being
       
   934      * deserialized.
       
   935      */
       
   936     private static class DeserializationChecker implements ObjectInputFilter {
       
   937         private static final int MAX_NESTED_DEPTH = 2;
       
   938 
       
   939         @Override
       
   940         public ObjectInputFilter.Status
       
   941             checkInput(ObjectInputFilter.FilterInfo info) {
       
   942 
       
   943             // First run a custom filter
       
   944             long nestedDepth = info.depth();
       
   945             if ((nestedDepth == 1 &&
       
   946                 info.serialClass() != SealedObjectForKeyProtector.class) ||
       
   947                 nestedDepth > MAX_NESTED_DEPTH) {
       
   948                 return Status.REJECTED;
       
   949             }
       
   950 
       
   951             // Next run the default filter, if available
       
   952             ObjectInputFilter defaultFilter =
       
   953                 ObjectInputFilter.Config.getSerialFilter();
       
   954             if (defaultFilter != null) {
       
   955                 return defaultFilter.checkInput(info);
       
   956             }
       
   957 
       
   958             return Status.UNDECIDED;
       
   959         }
       
   960     }
   919 }
   961 }