提交 a8d278b9 编写于 作者: I igerasim

8181432: Better processing of unresolved permissions

Reviewed-by: mullan
上级 b1b3f032
...@@ -34,6 +34,7 @@ import java.util.Hashtable; ...@@ -34,6 +34,7 @@ import java.util.Hashtable;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.security.cert.*; import java.security.cert.*;
import sun.misc.IOUtils;
/** /**
* *
...@@ -546,6 +547,8 @@ public class CodeSource implements java.io.Serializable { ...@@ -546,6 +547,8 @@ public class CodeSource implements java.io.Serializable {
// could all be present in the stream at the same time // could all be present in the stream at the same time
cfs = new Hashtable<String, CertificateFactory>(3); cfs = new Hashtable<String, CertificateFactory>(3);
certList = new ArrayList<>(size > 20 ? 20 : size); certList = new ArrayList<>(size > 20 ? 20 : size);
} else if (size < 0) {
throw new IOException("size cannot be negative");
} }
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
...@@ -567,13 +570,7 @@ public class CodeSource implements java.io.Serializable { ...@@ -567,13 +570,7 @@ public class CodeSource implements java.io.Serializable {
cfs.put(certType, cf); cfs.put(certType, cf);
} }
// parse the certificate // parse the certificate
byte[] encoded = null; byte[] encoded = IOUtils.readNBytes(ois, ois.readInt());
try {
encoded = new byte[ois.readInt()];
} catch (OutOfMemoryError oome) {
throw new IOException("Certificate too big");
}
ois.readFully(encoded);
ByteArrayInputStream bais = new ByteArrayInputStream(encoded); ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
try { try {
certList.add(cf.generateCertificate(bais)); certList.add(cf.generateCertificate(bais));
......
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,12 +25,16 @@ ...@@ -25,12 +25,16 @@
package java.security; package java.security;
import sun.misc.IOUtils;
import java.io.IOException; import java.io.IOException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.security.cert.Certificate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.security.cert.*; import java.security.cert.*;
import java.util.List;
/** /**
* The UnresolvedPermission class is used to hold Permissions that * The UnresolvedPermission class is used to hold Permissions that
...@@ -549,6 +553,7 @@ implements java.io.Serializable ...@@ -549,6 +553,7 @@ implements java.io.Serializable
{ {
CertificateFactory cf; CertificateFactory cf;
Hashtable<String, CertificateFactory> cfs = null; Hashtable<String, CertificateFactory> cfs = null;
List<Certificate> certList = null;
ois.defaultReadObject(); ois.defaultReadObject();
...@@ -560,8 +565,10 @@ implements java.io.Serializable ...@@ -560,8 +565,10 @@ implements java.io.Serializable
if (size > 0) { if (size > 0) {
// we know of 3 different cert types: X.509, PGP, SDSI, which // we know of 3 different cert types: X.509, PGP, SDSI, which
// could all be present in the stream at the same time // could all be present in the stream at the same time
cfs = new Hashtable<String, CertificateFactory>(3); cfs = new Hashtable<>(3);
this.certs = new java.security.cert.Certificate[size]; certList = new ArrayList<>(size > 20 ? 20 : size);
} else if (size < 0) {
throw new IOException("size cannot be negative");
} }
for (int i=0; i<size; i++) { for (int i=0; i<size; i++) {
...@@ -583,20 +590,18 @@ implements java.io.Serializable ...@@ -583,20 +590,18 @@ implements java.io.Serializable
cfs.put(certType, cf); cfs.put(certType, cf);
} }
// parse the certificate // parse the certificate
byte[] encoded=null; byte[] encoded = IOUtils.readNBytes(ois, ois.readInt());
try {
encoded = new byte[ois.readInt()];
} catch (OutOfMemoryError oome) {
throw new IOException("Certificate too big");
}
ois.readFully(encoded);
ByteArrayInputStream bais = new ByteArrayInputStream(encoded); ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
try { try {
this.certs[i] = cf.generateCertificate(bais); certList.add(cf.generateCertificate(bais));
} catch (CertificateException ce) { } catch (CertificateException ce) {
throw new IOException(ce.getMessage()); throw new IOException(ce.getMessage());
} }
bais.close(); bais.close();
} }
if (certList != null) {
this.certs = certList.toArray(
new java.security.cert.Certificate[size]);
}
} }
} }
/* /*
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -34,6 +34,7 @@ import java.util.HashMap; ...@@ -34,6 +34,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.security.auth.x500.X500Principal; import javax.security.auth.x500.X500Principal;
import sun.misc.IOUtils;
import sun.security.util.ObjectIdentifier; import sun.security.util.ObjectIdentifier;
import sun.security.x509.InvalidityDateExtension; import sun.security.x509.InvalidityDateExtension;
...@@ -228,17 +229,17 @@ public class CertificateRevokedException extends CertificateException { ...@@ -228,17 +229,17 @@ public class CertificateRevokedException extends CertificateException {
int size = ois.readInt(); int size = ois.readInt();
if (size == 0) { if (size == 0) {
extensions = Collections.emptyMap(); extensions = Collections.emptyMap();
} else if (size < 0) {
throw new IOException("size cannot be negative");
} else { } else {
extensions = new HashMap<String, Extension>(size); extensions = new HashMap<>(size > 20 ? 20 : size);
} }
// Read in the extensions and put the mappings in the extensions map // Read in the extensions and put the mappings in the extensions map
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String oid = (String) ois.readObject(); String oid = (String) ois.readObject();
boolean critical = ois.readBoolean(); boolean critical = ois.readBoolean();
int length = ois.readInt(); byte[] extVal = IOUtils.readNBytes(ois, ois.readInt());
byte[] extVal = new byte[length];
ois.readFully(extVal);
Extension ext = sun.security.x509.Extension.newExtension Extension ext = sun.security.x509.Extension.newExtension
(new ObjectIdentifier(oid), critical, extVal); (new ObjectIdentifier(oid), critical, extVal);
extensions.put(oid, ext); extensions.put(oid, ext);
......
/* /*
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -37,9 +37,9 @@ import java.util.Arrays; ...@@ -37,9 +37,9 @@ import java.util.Arrays;
public class IOUtils { public class IOUtils {
/** /**
* Read up to <code>length</code> of bytes from <code>in</code> * Read up to {@code length} of bytes from {@code in}
* until EOF is detected. * until EOF is detected.
* @param in input stream, must not be null * @param is input stream, must not be null
* @param length number of bytes to read, -1 or Integer.MAX_VALUE means * @param length number of bytes to read, -1 or Integer.MAX_VALUE means
* read as much as possible * read as much as possible
* @param readAll if true, an EOFException will be thrown if not enough * @param readAll if true, an EOFException will be thrown if not enough
...@@ -77,4 +77,22 @@ public class IOUtils { ...@@ -77,4 +77,22 @@ public class IOUtils {
} }
return output; return output;
} }
/**
* Read {@code length} of bytes from {@code in}. An exception is
* thrown if there are not enough bytes in the stream.
*
* @param is input stream, must not be null
* @param length number of bytes to read, must not be negative
* @return bytes read
* @throws IOException if any IO error or a premature EOF is detected, or
* if {@code length} is negative since this length is usually also
* read from {@code is}.
*/
public static byte[] readNBytes(InputStream is, int length) throws IOException {
if (length < 0) {
throw new IOException("length cannot be negative: " + length);
}
return readFully(is, length, true);
}
} }
/* /*
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -111,7 +111,11 @@ class ObjectIdentifier implements Serializable ...@@ -111,7 +111,11 @@ class ObjectIdentifier implements Serializable
is.defaultReadObject(); is.defaultReadObject();
if (encoding == null) { // from an old version if (encoding == null) { // from an old version
init((int[])components, componentLen); int[] comp = (int[])components;
if (componentLen > comp.length) {
componentLen = comp.length;
}
init(comp, componentLen);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册