Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jce.netscape;
  
 
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.
 
 public class NetscapeCertRequest
     extends ASN1Object
 {
     AlgorithmIdentifier    sigAlg;
     AlgorithmIdentifier    keyAlg;
     byte        sigBits [];
     String challenge;
     PublicKey pubkey ;
     
     private static ASN1Sequence getReq(
         byte[]  r)
         throws IOException
     {
         ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(r));
 
         return ASN1Sequence.getInstance(aIn.readObject());
     }
 
     public NetscapeCertRequest(
         byte[]  req)
         throws IOException
     {
         this(getReq(req));
     }
 
     public NetscapeCertRequest (ASN1Sequence spkac)
     {
         try
         {
 
             //
             // SignedPublicKeyAndChallenge ::= SEQUENCE {
             //    publicKeyAndChallenge    PublicKeyAndChallenge,
             //    signatureAlgorithm    AlgorithmIdentifier,
             //    signature        BIT STRING
             // }
             //
             if (spkac.size() != 3)
             {
                 throw new IllegalArgumentException("invalid SPKAC (size):"
                         + spkac.size());
             }
 
              = new AlgorithmIdentifier((ASN1Sequence)spkac
                     .getObjectAt(1));
              = ((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());
            }
             = ((DERIA5String)pkac.getObjectAt(1)).getString();
            //this could be dangerous, as ASN.1 decoding/encoding
            //could potentially alter the bytes
             = new DERBitString(pkac);
            SubjectPublicKeyInfo pubkeyinfo = new SubjectPublicKeyInfo(
                    (ASN1Sequence)pkac.getObjectAt(0));
            X509EncodedKeySpec xspec = new X509EncodedKeySpec(new DERBitString(
                    pubkeyinfo).getBytes());
             = pubkeyinfo.getAlgorithmId();
             = KeyFactory.getInstance(.getObjectId().getId(), "BC")
                    .generatePublic(xspec);
        }
        catch (Exception e)
        {
            throw new IllegalArgumentException(e.toString());
        }
    }
    public NetscapeCertRequest(
        String challenge,
        AlgorithmIdentifier signing_alg,
        PublicKey pub_keythrows NoSuchAlgorithmException,
    {
        this. = challenge;
         = signing_alg;
         = 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));
        try
        {
             = new DERBitString(new DERSequence(content_der));
        }
        catch (IOException e)
        {
            throw new InvalidKeySpecException("exception encoding key: " + e.toString());
        }
    }
    public String getChallenge()
    {
        return ;
    }
    public void setChallenge(String value)
    {
         = value;
    }
    {
        return ;
    }
    public void setSigningAlgorithm(AlgorithmIdentifier value)
    {
         = value;
    }
    {
        return ;
    }
    public void setKeyAlgorithm(AlgorithmIdentifier value)
    {
         = value;
    }
    public PublicKey getPublicKey()
    {
        return ;
    }
    public void setPublicKey(PublicKey value)
    {
         = value;
    }
    public boolean verify(String challengethrows NoSuchAlgorithmException,
    {
        if (!challenge.equals(this.))
        {
            return false;
        }
        //
        // Verify the signature .. shows the response was generated
        // by someone who knew the associated private key
        //
        Signature sig = Signature.getInstance(.getObjectId().getId(),
                "BC");
        sig.initVerify();
        sig.update(.getBytes());
        return sig.verify();
    }
    public void sign(PrivateKey priv_keythrows NoSuchAlgorithmException,
            InvalidKeySpecException
    {
        sign(priv_keynull);
    }
    public void sign(PrivateKey priv_keySecureRandom rand)
            throws NoSuchAlgorithmExceptionInvalidKeyException,
            InvalidKeySpecException
    {
        Signature sig = Signature.getInstance(.getAlgorithm().getId(),
                "BC");
        if (rand != null)
        {
            sig.initSign(priv_keyrand);
        }
        else
        {
            sig.initSign(priv_key);
        }
        ASN1EncodableVector pkac = new ASN1EncodableVector();
        pkac.add(getKeySpec());
        pkac.add(new DERIA5String());
        try
        {
            sig.update(new DERSequence(pkac).getEncoded(.));
        }
        catch (IOException ioe)
        {
            throw new SignatureException(ioe.getMessage());
        }
         = sig.sign();
    }
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ASN1Primitive obj = null;
        try
        {
            baos.write(.getEncoded());
            baos.close();
            ASN1InputStream derin = new ASN1InputStream(
                    new ByteArrayInputStream(baos.toByteArray()));
            obj = derin.readObject();
        }
        catch (IOException ioe)
        {
            throw new InvalidKeySpecException(ioe.getMessage());
        }
        return obj;
    }
    {
        ASN1EncodableVector spkac = new ASN1EncodableVector();
        ASN1EncodableVector pkac = new ASN1EncodableVector();
        try
        {
            pkac.add(getKeySpec());
        }
        catch (Exception e)
        {
            //ignore
        }
        pkac.add(new DERIA5String());
        spkac.add(new DERSequence(pkac));
        spkac.add();
        spkac.add(new DERBitString());
        return new DERSequence(spkac);
    }
New to GrepCode? Check out our FAQ X