home *** CD-ROM | disk | FTP | other *** search
- package sun.security.pkcs;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.security.Signature;
- import java.security.SignatureException;
- import sun.misc.BASE64Encoder;
- import sun.misc.CharacterEncoder;
- import sun.security.util.BigInt;
- import sun.security.util.DerInputStream;
- import sun.security.util.DerOutputStream;
- import sun.security.util.DerValue;
- import sun.security.x509.AlgorithmId;
- import sun.security.x509.X500Name;
- import sun.security.x509.X500Signer;
- import sun.security.x509.X509Key;
-
- public class PKCS10 {
- private X500Name subject;
- private X509Key subjectPublicKeyInfo;
- private DerValue attributeSet;
- private byte[] certificateRequest;
-
- public PKCS10(X509Key var1) {
- this.subjectPublicKeyInfo = var1;
- }
-
- public PKCS10(byte[] var1) throws IOException, SignatureException, NoSuchAlgorithmException {
- this.certificateRequest = var1;
- DerInputStream var2 = new DerInputStream(var1);
- DerValue[] var3 = var2.getSequence(3);
- if (var3.length != 3) {
- throw new IllegalArgumentException("not a PKCS #10 request");
- } else {
- var1 = var3[0].toByteArray();
- AlgorithmId var4 = AlgorithmId.parse(var3[1]);
- byte[] var5 = var3[2].getBitString();
- BigInt var7 = var3[0].data.getInteger();
- if (var7.toInt() != 0) {
- throw new IllegalArgumentException("not PKCS #10 v1");
- } else {
- this.subject = new X500Name(var3[0].data);
- this.subjectPublicKeyInfo = X509Key.parse(var3[0].data.getDerValue());
- if (var3[0].data.available() != 0) {
- this.attributeSet = var3[0].data.getDerValue();
- } else {
- this.attributeSet = null;
- }
-
- if (var3[0].data.available() != 0) {
- throw new IllegalArgumentException("illegal PKCS #10 data");
- } else {
- try {
- Signature var6 = Signature.getInstance(var4.getName());
- var6.initVerify(this.subjectPublicKeyInfo);
- var6.update(var1);
- if (!var6.verify(var5)) {
- throw new SignatureException("Invalid PKCS #10 signature");
- }
- } catch (InvalidKeyException var8) {
- throw new SignatureException("invalid key");
- }
- }
- }
- }
- }
-
- public void encodeAndSign(X500Signer var1) throws IOException, SignatureException {
- if (this.certificateRequest != null) {
- throw new SignatureException("request is already signed");
- } else {
- this.subject = var1.getSigner();
- DerOutputStream var3 = new DerOutputStream();
- DerOutputStream var2 = new DerOutputStream();
- var2.write((byte)-96, var3);
- this.attributeSet = new DerValue(((ByteArrayOutputStream)var2).toByteArray());
- var3 = new DerOutputStream();
- var3.putInteger(new BigInt(0));
- this.subject.emit(var3);
- this.subjectPublicKeyInfo.emit(var3);
- this.attributeSet.emit(var3);
- var2 = new DerOutputStream();
- var2.write((byte)48, var3);
- byte[] var4 = ((ByteArrayOutputStream)var2).toByteArray();
- var3 = var2;
- var1.update(var4, 0, var4.length);
- byte[] var5 = var1.sign();
- var1.getAlgorithmId().emit(var2);
- var2.putBitString(var5);
- var2 = new DerOutputStream();
- var2.write((byte)48, var3);
- this.certificateRequest = ((ByteArrayOutputStream)var2).toByteArray();
- }
- }
-
- public X500Name getSubjectName() {
- return this.subject;
- }
-
- public X509Key getSubjectPublicKeyInfo() {
- return this.subjectPublicKeyInfo;
- }
-
- public byte[] toByteArray() {
- return this.certificateRequest;
- }
-
- public void print(PrintStream var1) throws IOException, SignatureException {
- if (this.certificateRequest == null) {
- throw new SignatureException("Cert request was not signed");
- } else {
- BASE64Encoder var2 = new BASE64Encoder();
- var1.println("-----BEGIN NEW CERTIFICATE REQUEST-----");
- ((CharacterEncoder)var2).encodeBuffer(this.certificateRequest, var1);
- var1.println("-----END NEW CERTIFICATE REQUEST-----");
- }
- }
-
- public String toString() {
- return "[PKCS #10 certificate request, " + this.subjectPublicKeyInfo.getAlgorithmId().getName() + " public key, for <" + this.subject + ">]";
- }
- }
-