feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,371 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs10;
import java.io.PrintStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.cert.CertificateException;
import java.security.*;
import java.util.Base64;
import sun.security.util.*;
import sun.security.x509.AlgorithmId;
import sun.security.x509.X509Key;
import sun.security.x509.X500Name;
import sun.security.util.SignatureUtil;
/**
* A PKCS #10 certificate request is created and sent to a Certificate
* Authority, which then creates an X.509 certificate and returns it to
* the entity that requested it. A certificate request basically consists
* of the subject's X.500 name, public key, and optionally some attributes,
* signed using the corresponding private key.
*
* The ASN.1 syntax for a Certification Request is:
* <pre>
* CertificationRequest ::= SEQUENCE {
* certificationRequestInfo CertificationRequestInfo,
* signatureAlgorithm SignatureAlgorithmIdentifier,
* signature Signature
* }
*
* SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
* Signature ::= BIT STRING
*
* CertificationRequestInfo ::= SEQUENCE {
* version Version,
* subject Name,
* subjectPublicKeyInfo SubjectPublicKeyInfo,
* attributes [0] IMPLICIT Attributes
* }
* Attributes ::= SET OF Attribute
* </pre>
*
* @author David Brownell
* @author Amit Kapoor
* @author Hemma Prafullchandra
*/
public class PKCS10 {
/**
* Constructs an unsigned PKCS #10 certificate request. Before this
* request may be used, it must be encoded and signed. Then it
* must be retrieved in some conventional format (e.g. string).
*
* @param publicKey the public key that should be placed
* into the certificate generated by the CA.
*/
public PKCS10(PublicKey publicKey) {
subjectPublicKeyInfo = publicKey;
attributeSet = new PKCS10Attributes();
}
/**
* Constructs an unsigned PKCS #10 certificate request. Before this
* request may be used, it must be encoded and signed. Then it
* must be retrieved in some conventional format (e.g. string).
*
* @param publicKey the public key that should be placed
* into the certificate generated by the CA.
* @param attributes additonal set of PKCS10 attributes requested
* for in the certificate.
*/
public PKCS10(PublicKey publicKey, PKCS10Attributes attributes) {
subjectPublicKeyInfo = publicKey;
attributeSet = attributes;
}
/**
* Parses an encoded, signed PKCS #10 certificate request, verifying
* the request's signature as it does so. This constructor would
* typically be used by a Certificate Authority, from which a new
* certificate would then be constructed.
*
* @param data the DER-encoded PKCS #10 request.
* @exception IOException for low level errors reading the data
* @exception SignatureException when the signature is invalid
* @exception NoSuchAlgorithmException when the signature
* algorithm is not supported in this environment
*/
public PKCS10(byte[] data)
throws IOException, SignatureException, NoSuchAlgorithmException {
DerInputStream in;
DerValue[] seq;
AlgorithmId id;
byte[] sigData;
Signature sig;
encoded = data;
//
// Outer sequence: request, signature algorithm, signature.
// Parse, and prepare to verify later.
//
in = new DerInputStream(data);
seq = in.getSequence(3);
if (seq.length != 3)
throw new IllegalArgumentException("not a PKCS #10 request");
data = seq[0].toByteArray(); // reusing this variable
id = AlgorithmId.parse(seq[1]);
sigData = seq[2].getBitString();
//
// Inner sequence: version, name, key, attributes
//
BigInteger serial;
DerValue val;
serial = seq[0].data.getBigInteger();
if (!serial.equals(BigInteger.ZERO))
throw new IllegalArgumentException("not PKCS #10 v1");
subject = new X500Name(seq[0].data);
subjectPublicKeyInfo = X509Key.parse(seq[0].data.getDerValue());
// Cope with a somewhat common illegal PKCS #10 format
if (seq[0].data.available() != 0)
attributeSet = new PKCS10Attributes(seq[0].data);
else
attributeSet = new PKCS10Attributes();
if (seq[0].data.available() != 0)
throw new IllegalArgumentException("illegal PKCS #10 data");
//
// OK, we parsed it all ... validate the signature using the
// key and signature algorithm we found.
//
try {
sigAlg = id.getName();
sig = Signature.getInstance(sigAlg);
SignatureUtil.initVerifyWithParam(sig, subjectPublicKeyInfo,
SignatureUtil.getParamSpec(sigAlg, id.getParameters()));
sig.update(data);
if (!sig.verify(sigData)) {
throw new SignatureException("Invalid PKCS #10 signature");
}
} catch (InvalidKeyException e) {
throw new SignatureException("Invalid key");
} catch (InvalidAlgorithmParameterException e) {
throw new SignatureException("Invalid signature parameters", e);
} catch (ProviderException e) {
throw new SignatureException("Error parsing signature parameters",
e.getCause());
}
}
/**
* Create the signed certificate request. This will later be
* retrieved in either string or binary format.
*
* @param subject identifies the signer (by X.500 name).
* @param signature private key and signing algorithm to use.
* @exception IOException on errors.
* @exception CertificateException on certificate handling errors.
* @exception SignatureException on signature handling errors.
*/
public void encodeAndSign(X500Name subject, Signature signature)
throws CertificateException, IOException, SignatureException {
DerOutputStream out, scratch;
byte[] certificateRequestInfo;
byte[] sig;
if (encoded != null)
throw new SignatureException("request is already signed");
this.subject = subject;
/*
* Encode cert request info, wrap in a sequence for signing
*/
scratch = new DerOutputStream();
scratch.putInteger(BigInteger.ZERO); // PKCS #10 v1.0
subject.encode(scratch); // X.500 name
scratch.write(subjectPublicKeyInfo.getEncoded()); // public key
attributeSet.encode(scratch);
out = new DerOutputStream();
out.write(DerValue.tag_Sequence, scratch); // wrap it!
certificateRequestInfo = out.toByteArray();
scratch = out;
/*
* Sign it ...
*/
signature.update(certificateRequestInfo, 0,
certificateRequestInfo.length);
sig = signature.sign();
sigAlg = signature.getAlgorithm();
/*
* Build guts of SIGNED macro
*/
AlgorithmId algId = null;
try {
AlgorithmParameters params = signature.getParameters();
algId = params == null
? AlgorithmId.get(signature.getAlgorithm())
: AlgorithmId.get(params);
} catch (NoSuchAlgorithmException nsae) {
throw new SignatureException(nsae);
}
algId.encode(scratch); // sig algorithm
scratch.putBitString(sig); // sig
/*
* Wrap those guts in a sequence
*/
out = new DerOutputStream();
out.write(DerValue.tag_Sequence, scratch);
encoded = out.toByteArray();
}
/**
* Returns the subject's name.
*/
public X500Name getSubjectName() { return subject; }
/**
* Returns the subject's public key.
*/
public PublicKey getSubjectPublicKeyInfo()
{ return subjectPublicKeyInfo; }
/**
* Returns the signature algorithm.
*/
public String getSigAlg() { return sigAlg; }
/**
* Returns the additional attributes requested.
*/
public PKCS10Attributes getAttributes()
{ return attributeSet; }
/**
* Returns the encoded and signed certificate request as a
* DER-encoded byte array.
*
* @return the certificate request, or null if encodeAndSign()
* has not yet been called.
*/
public byte[] getEncoded() {
if (encoded != null)
return encoded.clone();
else
return null;
}
/**
* Prints an E-Mailable version of the certificate request on the print
* stream passed. The format is a common base64 encoded one, supported
* by most Certificate Authorities because Netscape web servers have
* used this for some time. Some certificate authorities expect some
* more information, in particular contact information for the web
* server administrator.
*
* @param out the print stream where the certificate request
* will be printed.
* @exception IOException when an output operation failed
* @exception SignatureException when the certificate request was
* not yet signed.
*/
public void print(PrintStream out)
throws IOException, SignatureException {
if (encoded == null)
throw new SignatureException("Cert request was not signed");
byte[] CRLF = new byte[] {'\r', '\n'};
out.println("-----BEGIN NEW CERTIFICATE REQUEST-----");
out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(encoded));
out.println("-----END NEW CERTIFICATE REQUEST-----");
}
/**
* Provides a short description of this request.
*/
public String toString() {
return "[PKCS #10 certificate request:\n"
+ subjectPublicKeyInfo.toString()
+ " subject: <" + subject + ">" + "\n"
+ " attributes: " + attributeSet.toString()
+ "\n]";
}
/**
* Compares this object for equality with the specified
* object. If the <code>other</code> object is an
* <code>instanceof</code> <code>PKCS10</code>, then
* its encoded form is retrieved and compared with the
* encoded form of this certificate request.
*
* @param other the object to test for equality with this object.
* @return true iff the encoded forms of the two certificate
* requests match, false otherwise.
*/
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof PKCS10))
return false;
if (encoded == null) // not signed yet
return false;
byte[] otherEncoded = ((PKCS10)other).getEncoded();
if (otherEncoded == null)
return false;
return java.util.Arrays.equals(encoded, otherEncoded);
}
/**
* Returns a hashcode value for this certificate request from its
* encoded form.
*
* @return the hashcode value.
*/
public int hashCode() {
int retval = 0;
if (encoded != null)
for (int i = 1; i < encoded.length; i++)
retval += encoded[i] * i;
return(retval);
}
private X500Name subject;
private PublicKey subjectPublicKeyInfo;
private String sigAlg;
private PKCS10Attributes attributeSet;
private byte[] encoded; // signed
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs10;
import java.io.OutputStream;
import java.io.IOException;
import sun.security.pkcs.PKCS9Attribute;
import sun.security.util.*;
/**
* Represent a PKCS#10 Attribute.
*
* <p>Attributes are additonal information which can be inserted in a PKCS#10
* certificate request. For example a "Driving License Certificate" could have
* the driving license number as an attribute.
*
* <p>Attributes are represented as a sequence of the attribute identifier
* (Object Identifier) and a set of DER encoded attribute values.
*
* ASN.1 definition of Attribute:
* <pre>
* Attribute :: SEQUENCE {
* type AttributeType,
* values SET OF AttributeValue
* }
* AttributeType ::= OBJECT IDENTIFIER
* AttributeValue ::= ANY defined by type
* </pre>
*
* @author Amit Kapoor
* @author Hemma Prafullchandra
*/
public class PKCS10Attribute implements DerEncoder {
protected ObjectIdentifier attributeId = null;
protected Object attributeValue = null;
/**
* Constructs an attribute from a DER encoding.
* This constructor expects the value to be encoded as defined above,
* i.e. a SEQUENCE of OID and SET OF value(s), not a literal
* X.509 v3 extension. Only PKCS9 defined attributes are supported
* currently.
*
* @param derVal the der encoded attribute.
* @exception IOException on parsing errors.
*/
public PKCS10Attribute(DerValue derVal) throws IOException {
PKCS9Attribute attr = new PKCS9Attribute(derVal);
this.attributeId = attr.getOID();
this.attributeValue = attr.getValue();
}
/**
* Constructs an attribute from individual components of
* ObjectIdentifier and the value (any java object).
*
* @param attributeId the ObjectIdentifier of the attribute.
* @param attributeValue an instance of a class that implements
* the attribute identified by the ObjectIdentifier.
*/
public PKCS10Attribute(ObjectIdentifier attributeId,
Object attributeValue) {
this.attributeId = attributeId;
this.attributeValue = attributeValue;
}
/**
* Constructs an attribute from PKCS9 attribute.
*
* @param attr the PKCS9Attribute to create from.
*/
public PKCS10Attribute(PKCS9Attribute attr) {
this.attributeId = attr.getOID();
this.attributeValue = attr.getValue();
}
/**
* DER encode this object onto an output stream.
* Implements the <code>DerEncoder</code> interface.
*
* @param out
* the OutputStream on which to write the DER encoding.
*
* @exception IOException on encoding errors.
*/
public void derEncode(OutputStream out) throws IOException {
PKCS9Attribute attr = new PKCS9Attribute(attributeId, attributeValue);
attr.derEncode(out);
}
/**
* Returns the ObjectIdentifier of the attribute.
*/
public ObjectIdentifier getAttributeId() {
return (attributeId);
}
/**
* Returns the attribute value.
*/
public Object getAttributeValue() {
return (attributeValue);
}
/**
* Returns the attribute in user readable form.
*/
public String toString() {
return (attributeValue.toString());
}
}

View File

@@ -0,0 +1,219 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs10;
import java.io.IOException;
import java.io.OutputStream;
import java.security.cert.CertificateException;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import sun.security.util.*;
/**
* This class defines the PKCS10 attributes for the request.
* The ASN.1 syntax for this is:
* <pre>
* Attributes ::= SET OF Attribute
* </pre>
*
* @author Amit Kapoor
* @author Hemma Prafullchandra
* @see PKCS10
* @see PKCS10Attribute
*/
public class PKCS10Attributes implements DerEncoder {
private Hashtable<String, PKCS10Attribute> map =
new Hashtable<String, PKCS10Attribute>(3);
/**
* Default constructor for the PKCS10 attribute.
*/
public PKCS10Attributes() { }
/**
* Create the object from the array of PKCS10Attribute objects.
*
* @param attrs the array of PKCS10Attribute objects.
*/
public PKCS10Attributes(PKCS10Attribute[] attrs) {
for (int i = 0; i < attrs.length; i++) {
map.put(attrs[i].getAttributeId().toString(), attrs[i]);
}
}
/**
* Create the object, decoding the values from the passed DER stream.
* The DER stream contains the SET OF Attribute.
*
* @param in the DerInputStream to read the attributes from.
* @exception IOException on decoding errors.
*/
public PKCS10Attributes(DerInputStream in) throws IOException {
DerValue[] attrs = in.getSet(3, true);
if (attrs == null)
throw new IOException("Illegal encoding of attributes");
for (int i = 0; i < attrs.length; i++) {
PKCS10Attribute attr = new PKCS10Attribute(attrs[i]);
map.put(attr.getAttributeId().toString(), attr);
}
}
/**
* Encode the attributes in DER form to the stream.
*
* @param out the OutputStream to marshal the contents to.
* @exception IOException on encoding errors.
*/
public void encode(OutputStream out) throws IOException {
derEncode(out);
}
/**
* Encode the attributes in DER form to the stream.
* Implements the {@code DerEncoder} interface.
*
* @param out the OutputStream to marshal the contents to.
* @exception IOException on encoding errors.
*/
public void derEncode(OutputStream out) throws IOException {
// first copy the elements into an array
Collection<PKCS10Attribute> allAttrs = map.values();
PKCS10Attribute[] attribs =
allAttrs.toArray(new PKCS10Attribute[map.size()]);
DerOutputStream attrOut = new DerOutputStream();
attrOut.putOrderedSetOf(DerValue.createTag(DerValue.TAG_CONTEXT,
true, (byte)0),
attribs);
out.write(attrOut.toByteArray());
}
/**
* Set the attribute value.
*/
public void setAttribute(String name, Object obj) {
if (obj instanceof PKCS10Attribute) {
map.put(name, (PKCS10Attribute)obj);
}
}
/**
* Get the attribute value.
*/
public Object getAttribute(String name) {
return map.get(name);
}
/**
* Delete the attribute value.
*/
public void deleteAttribute(String name) {
map.remove(name);
}
/**
* Return an enumeration of names of attributes existing within this
* attribute.
*/
public Enumeration<PKCS10Attribute> getElements() {
return (map.elements());
}
/**
* Return a Collection of attributes existing within this
* PKCS10Attributes object.
*/
public Collection<PKCS10Attribute> getAttributes() {
return (Collections.unmodifiableCollection(map.values()));
}
/**
* Compares this PKCS10Attributes for equality with the specified
* object. If the {@code other} object is an
* {@code instanceof} {@code PKCS10Attributes}, then
* all the entries are compared with the entries from this.
*
* @param other the object to test for equality with this PKCS10Attributes.
* @return true if all the entries match that of the Other,
* false otherwise.
*/
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof PKCS10Attributes))
return false;
Collection<PKCS10Attribute> othersAttribs =
((PKCS10Attributes)other).getAttributes();
PKCS10Attribute[] attrs =
othersAttribs.toArray(new PKCS10Attribute[othersAttribs.size()]);
int len = attrs.length;
if (len != map.size())
return false;
PKCS10Attribute thisAttr, otherAttr;
String key = null;
for (int i=0; i < len; i++) {
otherAttr = attrs[i];
key = otherAttr.getAttributeId().toString();
if (key == null)
return false;
thisAttr = map.get(key);
if (thisAttr == null)
return false;
if (! thisAttr.equals(otherAttr))
return false;
}
return true;
}
/**
* Returns a hashcode value for this PKCS10Attributes.
*
* @return the hashcode value.
*/
public int hashCode() {
return map.hashCode();
}
/**
* Returns a string representation of this {@code PKCS10Attributes} object
* in the form of a set of entries, enclosed in braces and separated
* by the ASCII characters "{@code ,&nbsp;}" (comma and space).
* <p>Overrides the {@code toString} method of {@code Object}.
*
* @return a string representation of this PKCS10Attributes.
*/
public String toString() {
String s = map.size() + "\n" + map.toString();
return s;
}
}