FileDocCategorySizeDatePackage
NetscapeCertRequest.javaAPI DocAndroid 1.5 API8468Wed May 06 22:41:06 BST 2009org.bouncycastle.jce.netscape

NetscapeCertRequest

public class NetscapeCertRequest extends org.bouncycastle.asn1.ASN1Encodable
Handles NetScape certificate request (KEYGEN), these are constructed as:

SignedPublicKeyAndChallenge ::= SEQUENCE {
publicKeyAndChallenge PublicKeyAndChallenge,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING
}
PublicKey's encoded-format has to be X.509.

Fields Summary
org.bouncycastle.asn1.x509.AlgorithmIdentifier
sigAlg
org.bouncycastle.asn1.x509.AlgorithmIdentifier
keyAlg
byte[]
sigBits
String
challenge
org.bouncycastle.asn1.DERBitString
content
PublicKey
pubkey
Constructors Summary
public NetscapeCertRequest(byte[] req)

        this(getReq(req));
    
public NetscapeCertRequest(org.bouncycastle.asn1.ASN1Sequence spkac)

        try
        {

            //
            // SignedPublicKeyAndChallenge ::= SEQUENCE {
            //    publicKeyAndChallenge    PublicKeyAndChallenge,
            //    signatureAlgorithm    AlgorithmIdentifier,
            //    signature        BIT STRING
            // }
            //
            if (spkac.size() != 3)
            {
                throw new IllegalArgumentException("invalid SPKAC (size):"
                        + spkac.size());
            }

            sigAlg = new AlgorithmIdentifier((ASN1Sequence)spkac
                    .getObjectAt(1));
            sigBits = ((DERBitString)spkac.getObjectAt(2)).getBytes();

            //
            // PublicKeyAndChallenge ::= SEQUENCE {
            //    spki            SubjectPublicKeyInfo,
            //    challenge        IA5STRING
            // }
            //
            ASN1Sequence pkac = (ASN1Sequence)spkac.getObjectAt(0);

            if (pkac.size() != 2)
            {
                throw new IllegalArgumentException("invalid PKAC (len): "
                        + pkac.size());
            }

            challenge = ((DERIA5String)pkac.getObjectAt(1)).getString();

            //this could be dangerous, as ASN.1 decoding/encoding
            //could potentially alter the bytes
            content = new DERBitString(pkac);

            SubjectPublicKeyInfo pubkeyinfo = new SubjectPublicKeyInfo(
                    (ASN1Sequence)pkac.getObjectAt(0));

            X509EncodedKeySpec xspec = new X509EncodedKeySpec(new DERBitString(
                    pubkeyinfo).getBytes());

            keyAlg = pubkeyinfo.getAlgorithmId();
            pubkey = KeyFactory.getInstance(keyAlg.getObjectId().getId(), "BC")
                    .generatePublic(xspec);

        }
        catch (Exception e)
        {
            throw new IllegalArgumentException(e.toString());
        }
    
public NetscapeCertRequest(String challenge, org.bouncycastle.asn1.x509.AlgorithmIdentifier signing_alg, PublicKey pub_key)


        this.challenge = challenge;
        sigAlg = signing_alg;
        pubkey = pub_key;

        ASN1EncodableVector content_der = new ASN1EncodableVector();
        content_der.add(getKeySpec());
        //content_der.add(new SubjectPublicKeyInfo(sigAlg, new RSAPublicKeyStructure(pubkey.getModulus(), pubkey.getPublicExponent()).getDERObject()));
        content_der.add(new DERIA5String(challenge));

        content = new DERBitString(new DERSequence(content_der));
    
Methods Summary
public java.lang.StringgetChallenge()

        return challenge;
    
public org.bouncycastle.asn1.x509.AlgorithmIdentifiergetKeyAlgorithm()

        return keyAlg;
    
private org.bouncycastle.asn1.DERObjectgetKeySpec()

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        DERObject obj = null;
        try
        {

            baos.write(pubkey.getEncoded());
            baos.close();

            ASN1InputStream derin = new ASN1InputStream(
                    new ByteArrayInputStream(baos.toByteArray()));

            obj = derin.readObject();
        }
        catch (IOException ioe)
        {
            throw new InvalidKeySpecException(ioe.getMessage());
        }
        return obj;
    
public java.security.PublicKeygetPublicKey()

        return pubkey;
    
private static org.bouncycastle.asn1.ASN1SequencegetReq(byte[] r)

        ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(r));

        return ASN1Sequence.getInstance(aIn.readObject());
    
public org.bouncycastle.asn1.x509.AlgorithmIdentifiergetSigningAlgorithm()

        return sigAlg;
    
public voidsetChallenge(java.lang.String value)

        challenge = value;
    
public voidsetKeyAlgorithm(org.bouncycastle.asn1.x509.AlgorithmIdentifier value)

        keyAlg = value;
    
public voidsetPublicKey(java.security.PublicKey value)

        pubkey = value;
    
public voidsetSigningAlgorithm(org.bouncycastle.asn1.x509.AlgorithmIdentifier value)

        sigAlg = value;
    
public voidsign(java.security.PrivateKey priv_key)

        sign(priv_key, null);
    
public voidsign(java.security.PrivateKey priv_key, java.security.SecureRandom rand)

        Signature sig = Signature.getInstance(sigAlg.getObjectId().getId(),
                "BC");

        if (rand != null)
        {
            sig.initSign(priv_key, rand);
        }
        else
        {
            sig.initSign(priv_key);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DEROutputStream deros = new DEROutputStream(baos);

        ASN1EncodableVector pkac = new ASN1EncodableVector();

        pkac.add(getKeySpec());
        pkac.add(new DERIA5String(challenge));

        try
        {
            deros.writeObject(new DERSequence(pkac));
            deros.close();
        }
        catch (IOException ioe)
        {
            throw new SignatureException(ioe.getMessage());
        }

        sig.update(baos.toByteArray());

        sigBits = sig.sign();
    
public org.bouncycastle.asn1.DERObjecttoASN1Object()

        ASN1EncodableVector spkac = new ASN1EncodableVector();
        ASN1EncodableVector pkac = new ASN1EncodableVector();

        try
        {
            pkac.add(getKeySpec());
        }
        catch (Exception e)
        {
            //ignore
        }

        pkac.add(new DERIA5String(challenge));

        spkac.add(new DERSequence(pkac));
        spkac.add(sigAlg);
        spkac.add(new DERBitString(sigBits));

        return new DERSequence(spkac);
    
public booleanverify(java.lang.String challenge)

        if (!challenge.equals(this.challenge))
        {
            return false;
        }

        //
        // Verify the signature .. shows the response was generated
        // by someone who knew the associated private key
        //
        Signature sig = Signature.getInstance(sigAlg.getObjectId().getId(),
                "BC");
        sig.initVerify(pubkey);
        sig.update(content.getBytes());

        return sig.verify(sigBits);