private static byte[] generateSignatureBlock( SignerConfig signerConfig, byte[] signatureFileBytes) throws InvalidKeyException, CertificateEncodingException, SignatureException { JcaCertStore certs = new JcaCertStore(signerConfig.certificates); X509Certificate signerCert = signerConfig.certificates.get(0); String jcaSignatureAlgorithm = getJcaSignatureAlgorithm( signerCert.getPublicKey(), signerConfig.signatureDigestAlgorithm); try { ContentSigner signer = new JcaContentSignerBuilder(jcaSignatureAlgorithm) .build(signerConfig.privateKey); CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); gen.addSignerInfoGenerator( new SignerInfoGeneratorBuilder( new JcaDigestCalculatorProviderBuilder().build(), SignerInfoSignatureAlgorithmFinder.INSTANCE) .setDirectSignature(true) .build(signer, new JcaX509CertificateHolder(signerCert))); gen.addCertificates(certs); CMSSignedData sigData = gen.generate(new CMSProcessableByteArray(signatureFileBytes), false); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) { DEROutputStream dos = new DEROutputStream(out); dos.writeObject(asn1.readObject()); } return out.toByteArray(); } catch (OperatorCreationException | CMSException | IOException e) { throw new SignatureException("Failed to generate signature", e); } }
public byte[] getEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject()); try { dOut.writeObject(info); dOut.close(); } catch (IOException e) { throw new RuntimeException("Error encoding RSA public key"); } return bOut.toByteArray(); }
public byte[] getEncoded() throws CRLException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(c); return bOut.toByteArray(); } catch (IOException e) { throw new CRLException(e.toString()); } }
public byte[] getTBSCertList() throws CRLException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(c.getTBSCertList()); return bOut.toByteArray(); } catch (IOException e) { throw new CRLException(e.toString()); } }
public byte[] getSigAlgParams() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); if ( c.getSignatureAlgorithm().getParameters() != null ) { try { DEROutputStream dOut = new DEROutputStream(bOut); dOut.writeObject(c.getSignatureAlgorithm().getParameters()); } catch (Exception e) { throw new RuntimeException("exception getting sig parameters " + e); } return bOut.toByteArray(); } return null; }
/** * return a DER encoded byte array representing this object */ public byte[] getEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(this); } catch (IOException e) { throw new RuntimeException(e.toString()); } return bOut.toByteArray(); }
/** * Add an extension with the given oid and the passed in value to be included * in the OCTET STRING associated with the extension. * * @param oid OID for the extension. * @param critical true if critical, false otherwise. * @param value the ASN.1 object to be included in the extension. */ public void addExtension( DERObjectIdentifier oid, boolean critical, DEREncodable value) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(value); } catch (IOException e) { throw new IllegalArgumentException("error encoding value: " + e); } this.addExtension(oid, critical, bOut.toByteArray()); }
private static byte[] generateSignatureBlock(SignerConfig signerConfig, byte[] signatureFileBytes) throws InvalidKeyException, CertificateEncodingException, SignatureException { JcaCertStore certs = new JcaCertStore(signerConfig.certificates); X509Certificate signerCert = signerConfig.certificates.get(0); String jcaSignatureAlgorithm = getJcaSignatureAlgorithm(signerCert.getPublicKey(), signerConfig.signatureDigestAlgorithm); try { ContentSigner signer = new JcaContentSignerBuilder(jcaSignatureAlgorithm).build(signerConfig.privateKey); CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); gen.addSignerInfoGenerator(new SignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build(), SignerInfoSignatureAlgorithmFinder.INSTANCE).setDirectSignature(true).build(signer, new JcaX509CertificateHolder(signerCert))); gen.addCertificates(certs); CMSSignedData sigData = gen.generate(new CMSProcessableByteArray(signatureFileBytes), false); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) { DEROutputStream dos = new DEROutputStream(out); dos.writeObject(asn1.readObject()); } return out.toByteArray(); } catch (OperatorCreationException | CMSException | IOException e) { throw new SignatureException("Failed to generate signature", e); } }
/** * add a given extension field for the standard extensions tag (tag 0) */ public void addExtension( DERObjectIdentifier OID, boolean critical, DEREncodable value) { if (extensions == null) { extensions = new Hashtable(); extOrdering = new Vector(); } ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(value); } catch (IOException e) { throw new IllegalArgumentException("error encoding value: " + e); } this.addExtension(OID, critical, bOut.toByteArray()); }
private byte[] derEncode( BigInteger r, BigInteger s) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); ASN1EncodableVector v = new ASN1EncodableVector(); v.add(new DERInteger(r)); v.add(new DERInteger(s)); dOut.writeObject(new DERSequence(v)); return bOut.toByteArray(); }
@Test public void testCommandMessageRoundTrip() throws Exception { CommandMessage msg = new CommandMessage(CommandMessage.Type.ACTIVATE_BOARD, new ASN1Integer(1)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DEROutputStream derOut = new DEROutputStream(bos); derOut.writeObject(msg.toASN1Primitive()); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ASN1InputStream din = new ASN1InputStream(bin); CommandMessage res = CommandMessage.getInstance(din.readObject()); TestCase.assertEquals(msg.getPayload(), res.getPayload()); TestCase.assertEquals(msg.getType(), res.getType()); }
@Test public void testPermuteAndMoveRoundTrip_1() throws Exception { PermuteAndMoveMessage msg = new PermuteAndMoveMessage(1, "Cat", 0, "Doc", "Fish", "Rabbit"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DEROutputStream derOut = new DEROutputStream(bos); derOut.writeObject(msg.toASN1Primitive()); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ASN1InputStream din = new ASN1InputStream(bin); PermuteAndMoveMessage res = PermuteAndMoveMessage.getInstance(din.readObject()); TestCase.assertEquals(msg.getBoardName(), res.getBoardName()); TestCase.assertEquals(msg.getDestinationNode(), res.getDestinationNode()); TestCase.assertEquals(msg.getKeyID(), res.getKeyID()); TestCase.assertEquals(msg.getTransformName(), res.getTransformName()); }
@Test public void testPermuteAndMoveRoundTrip_2() throws Exception { PermuteAndMoveMessage msg = new PermuteAndMoveMessage(1, "Cat", 0, "Doc", null, "Rabbit"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DEROutputStream derOut = new DEROutputStream(bos); derOut.writeObject(msg.toASN1Primitive()); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ASN1InputStream din = new ASN1InputStream(bin); PermuteAndMoveMessage res = PermuteAndMoveMessage.getInstance(din.readObject()); TestCase.assertEquals(msg.getBoardName(), res.getBoardName()); TestCase.assertEquals(msg.getDestinationNode(), res.getDestinationNode()); TestCase.assertEquals(msg.getKeyID(), res.getKeyID()); TestCase.assertEquals(msg.getTransformName(), res.getTransformName()); }
@Test public void testBoardErrorStatusMessage_1() throws Exception { BoardErrorStatusMessage msg = new BoardErrorStatusMessage("foo", BoardErrorStatusMessage.Status.NOT_SHUFFLE_LOCKED); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DEROutputStream derOut = new DEROutputStream(bos); derOut.writeObject(msg.toASN1Primitive()); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ASN1InputStream din = new ASN1InputStream(bin); BoardErrorStatusMessage res = BoardErrorStatusMessage.getInstance(ASN1TaggedObject.getInstance(din.readObject()).getObject()); TestCase.assertEquals(msg.getBoardName(), res.getBoardName()); TestCase.assertEquals(msg.getStatus(), res.getStatus()); }
@Test public void testBigIntegerMessage_1() throws Exception { BigIntegerMessage msg = new BigIntegerMessage(BigInteger.valueOf(Long.MAX_VALUE)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DEROutputStream derOut = new DEROutputStream(bos); derOut.writeObject(msg.toASN1Primitive()); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ASN1InputStream din = new ASN1InputStream(bin); BigIntegerMessage res = BigIntegerMessage.getInstance(din.readObject()); TestCase.assertEquals(msg.getValue(), res.getValue()); }
/** * @param s * @throws Exception */ protected void respondExiting(Socket s) throws Exception { OutputStream sOut = s.getOutputStream(); DEROutputStream aOut = new DEROutputStream(sOut); // TODO: NodeInfo actually is the first object in the protocol aOut.writeObject(new MessageReply(MessageReply.Type.EXITING)); aOut.flush(); aOut.close(); s.close(); }
private AlgorithmParameters getParameters() throws NoSuchAlgorithmException { AlgorithmParameters ap = AlgorithmParameters.getInstance(this.getAlgName()); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(infoObj.getEncryptionAlgorithm().getParameters()); dOut.close(); ap.init(bOut.toByteArray()); } catch (IOException e) { throw new NoSuchAlgorithmException("unable to parse parameters"); } return ap; }
/** * Parse the given rfc822 addr-spec into DER encoded byte array * representation. * * @param the * rfc822 addr-spec in well known String format * * @return the rfc822 addr-spec as byte array * * @exception IOException * if the String could not be parsed */ private static byte[] parseRfc822(String data) throws IOException { int tmpInt = data.indexOf('@'); if (tmpInt < 0 || tmpInt >= data.length() - 1) { throw new IOException("wrong format of rfc822Name:" + data); } // TODO more test for illegal charateers ASN1Object derData = new DERIA5String(data); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DEROutputStream derOutStream = new DEROutputStream(outStream); derOutStream.writeObject(derData); derOutStream.close(); return outStream.toByteArray(); }
/** * Creates an instance of <code>PolicyQualifierInfo</code> from the * encoded bytes. The encoded byte array is copied on construction.<br /> * <br /> * Uses {@link org.bouncycastle.asn1.ASN1InputStream ASN1InputStream}, * {@link org.bouncycastle.asn1.ASN1Sequence ASN1Sequence}, * {@link org.bouncycastle.asn1.ASN1ObjectIdentifier ASN1ObjectIdentifier} and * {@link org.bouncycastle.asn1.DEROutputStream DEROutputStream} * * @param encoded * a byte array containing the qualifier in DER encoding * * @exception IOException * thrown if the byte array does not represent a valid and * parsable policy qualifier */ public PolicyQualifierInfo(byte[] encoded) throws IOException { this.encoded = (byte[])encoded.clone(); try { ByteArrayInputStream inStream = new ByteArrayInputStream( this.encoded); ASN1InputStream derInStream = new ASN1InputStream(inStream); ASN1Sequence obj = (ASN1Sequence)derInStream.readObject(); id = ((ASN1ObjectIdentifier)obj.getObjectAt(0)).getId(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DEROutputStream derOutStream = new DEROutputStream(outStream); derOutStream.writeObject(obj.getObjectAt(1)); derOutStream.close(); qualifier = outStream.toByteArray(); } catch (Exception ex) { throw new IOException("parsing exception : " + ex.toString()); } }
protected byte[] engineGetEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { dOut.writeObject(params); } catch (IOException e) { throw new RuntimeException("Oooops! " + e.toString()); } return bOut.toByteArray(); }
/** * in the abscence of a standard way of doing it this will do for * now... */ protected byte[] engineGetEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); try { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(new DEROctetString(currentSpec.getDerivationV())); v.add(new DEROctetString(currentSpec.getEncodingV())); v.add(new DERInteger(currentSpec.getMacKeySize())); dOut.writeObject(new DERSequence(v)); dOut.close(); } catch (IOException e) { throw new RuntimeException("Error encoding IESParameters"); } return bOut.toByteArray(); }
private static byte[] generateSig(ContentSigner signer, ASN1Encodable tbsObj) throws IOException { OutputStream sOut = signer.getOutputStream(); DEROutputStream dOut = new DEROutputStream(sOut); dOut.writeObject(tbsObj); sOut.close(); return signer.getSignature(); }