ASN1InputStream ais = new ASN1InputStream( new FileInputStream(new File("d:/myfile.cdr"))); while (ais.available() > 0) { ASN1Primitive obj = ais.readObject(); System.out.println(ASN1Dump.dumpAsString(obj, true)); } ais.close();
private boolean __verifyNonRepSignature(final byte[] expectedDigestValue, final byte[] signatureValue, final X509Certificate certificate) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException { final PublicKey publicKey = certificate.getPublicKey(); final Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); final byte[] actualSignatureDigestInfoValue = cipher.doFinal(signatureValue); final ASN1InputStream asnInputStream = new ASN1InputStream(actualSignatureDigestInfoValue); final DigestInfo actualSignatureDigestInfo = new DigestInfo((ASN1Sequence) asnInputStream.readObject()); asnInputStream.close(); final byte[] actualDigestValue = actualSignatureDigestInfo.getDigest(); return Arrays.equals(expectedDigestValue, actualDigestValue); }
/** * Convert <code>der</code> encoded data to <code>ASN1Primitive</code>. * For more information, * (<a href="http://stackoverflow.com/questions/2409618/how-do-i-decode-a-der-encoded-string-in-java">StackOverflow: How do I decode a DER encoded string in Java?</a>) * @param data byte[] of <code>der</code> encoded data * @return <code>ASN1Primitive</code> representation of <code>der</code> encoded data * @throws IOException */ static final ASN1Primitive toDERObject(byte[] data) throws IOException { ByteArrayInputStream inStream = new ByteArrayInputStream(data); ASN1InputStream asnInputStream = new ASN1InputStream(inStream); ASN1Primitive p = asnInputStream.readObject(); asnInputStream.close(); return p; }
private ASN1Primitive readANS1FromStream(InputStream is) { ASN1InputStream asn1is = new ASN1InputStream(is); ASN1Primitive primitive = null; try { primitive = asn1is.readObject(); } catch (IOException error) { LOGGER.getLevel(); LOGGER.log(Level.ERROR, "Error reading stream.", error); throw new RuntimeException(error); } finally { try { asn1is.close(); } catch (IOException error) { throw new RuntimeException(error); } } return primitive; }
protected PKIMessage generatePkiMessage(InputStream is) throws IOException { ASN1InputStream asn1Stream = new ASN1InputStream(ScepUtil.requireNonNull("is", is)); try { return PKIMessage.getInstance(asn1Stream.readObject()); } finally { try { asn1Stream.close(); } catch (Exception ex) { LOG.error("could not close stream: {}", ex.getMessage()); } } }
public enum CertPathEncoding { PKCS7, PkiPath }
private ASN1Encodable getCertAsDEREncodable(X509Certificate cert) { try { byte[] certBytes = cert.getEncoded(); ByteArrayInputStream bais = new ByteArrayInputStream(certBytes); ASN1InputStream is = new ASN1InputStream(bais); ASN1Object derCert = is.readObject(); is.close(); return derCert; } catch (CertificateEncodingException e) { throw new VOMSError("Error encoding X509 certificate: " + e.getMessage(), e); } catch (IOException e) { throw new VOMSError("Error encoding X509 certificate: " + e.getMessage(), e); } }
private static ASN1Primitive getObject(String oid, byte[] ext) throws AnnotatedException { try { ASN1InputStream aIn = new ASN1InputStream(ext); ASN1OctetString octs = (ASN1OctetString) aIn.readObject(); aIn.close(); aIn = new ASN1InputStream(octs.getOctets()); ASN1Primitive p = aIn.readObject(); aIn.close(); return p; } catch (Exception e) { throw new AnnotatedException("exception processing extension " + oid, e); } }
@Override public boolean hasNext() { try { if (asn1InputStream == null) { return false; } boolean availableDataInCurrentEntry = asn1InputStream.available() > 0; if (!availableDataInCurrentEntry) { // advance to the next entry. parent = getNextElement(); if (parent == null) { asn1InputStream.close(); availableDataInCurrentEntry = false; } else { availableDataInCurrentEntry = true; } } return availableDataInCurrentEntry; } catch (IOException exception) { //Just wrap the IOException as CamelRuntimeException throw new RuntimeCamelException(exception); } }
@Override public boolean hasNext() { try { if (asn1InputStream == null) { return false; } boolean availableDataInCurrentEntry = asn1InputStream.available() > 0; if (!availableDataInCurrentEntry) { // advance to the next entry. parent = getNextElement(); if (parent == null) { asn1InputStream.close(); availableDataInCurrentEntry = false; } else { availableDataInCurrentEntry = true; } } return availableDataInCurrentEntry; } catch (IOException exception) { // Just wrap the IOException as CamelRuntimeException throw new RuntimeCamelException(exception); } }
private void establishKeys(ProxyCertificateOptions param) throws InvalidKeyException { PublicKey proxyPublicKey = param.getPublicKey(); proxyPrivateKey = null; if (proxyPublicKey == null) { KeyPair pair = ProxyGeneratorHelper.generateKeyPair(param.getKeyLength()); proxyPublicKey = pair.getPublic(); proxyPrivateKey = pair.getPrivate(); } try { ASN1InputStream asn1IS = new ASN1InputStream(proxyPublicKey.getEncoded()); proxyPublicKeyInfo = SubjectPublicKeyInfo.getInstance(asn1IS.readObject()); asn1IS.close(); } catch (IOException e) { throw new InvalidKeyException("Can not parse the public key" + "being included in the proxy certificate", e); } }
public static ECDSASignature decodeFromDER(byte[] bytes) { ASN1InputStream decoder = null; try { decoder = new ASN1InputStream(bytes); DLSequence seq = (DLSequence) decoder.readObject(); if (seq == null) throw new RuntimeException("Reached past end of ASN.1 stream."); ASN1Integer r, s; try { r = (ASN1Integer) seq.getObjectAt(0); s = (ASN1Integer) seq.getObjectAt(1); } catch (ClassCastException e) { throw new IllegalArgumentException(e); } // OpenSSL deviates from the DER spec by interpreting these values as unsigned, though they should not be // Thus, we always use the positive versions. See: http://r6.ca/blog/20111119T211504Z.html return new ECDSASignature(r.getPositiveValue(), s.getPositiveValue()); } catch (IOException e) { throw new RuntimeException(e); } finally { if (decoder != null) try { decoder.close(); } catch (IOException x) {} } }
/** * Extracts an AC from a VOMS response * * @param request * the request * @param response * the received response * @return a possibly <code>null</code> {@link AttributeCertificate} object */ protected AttributeCertificate getACFromResponse(VOMSACRequest request, VOMSResponse response) { byte[] acBytes = response.getAC(); if (acBytes == null) return null; ASN1InputStream asn1InputStream = new ASN1InputStream(acBytes); AttributeCertificate attributeCertificate = null; try { attributeCertificate = AttributeCertificate.getInstance(asn1InputStream .readObject()); asn1InputStream.close(); return attributeCertificate; } catch (Throwable e) { requestListener.notifyVOMSRequestFailure(request, null, new VOMSError( "Error unmarshalling VOMS AC. Cause: " + e.getMessage(), e)); return null; } }
import java.io.IOException; import java.math.BigInteger; import org.bouncycastle.asn1.ASN1EncodableVector; import org.bouncycastle.asn1.ASN1InputStream; import org.bouncycastle.asn1.ASN1Integer; import org.bouncycastle.asn1.DERSequence; import org.bouncycastle.asn1.DLSequence; public class RsaAsn1Example { // ... public static BigInteger [] parseASN1RsaPublicKey(byte [] encoded) throws IOException { ASN1InputStream asn1_is = new ASN1InputStream(encoded); DLSequence dlSeq = (DLSequence) asn1_is.readObject(); ASN1Integer asn1_n = (ASN1Integer) dlSeq.getObjectAt(0); ASN1Integer asn1_e = (ASN1Integer) dlSeq.getObjectAt(1); asn1_is.close(); return new BigInteger[]{ asn1_n.getPositiveValue(), asn1_e.getPositiveValue()}; } // .... }
/** * Helper method to convert DER-encoded signature values (e.g. used by Java) * to concatenated signature values * (as used by the JWS-standard) * * @param derEncodedSignatureValue * DER-encoded signature value * @return concatenated signature value (as used by JWS standard) * @throws IOException */ public static byte[] convertDEREncodedSignatureToJWSConcatenated(final byte[] derEncodedSignatureValue) throws IOException { final ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue); final ASN1Primitive asn1Primitive = asn1InputStream.readObject(); asn1InputStream.close(); final ASN1Sequence asn1Sequence = (ASN1Sequence.getInstance(asn1Primitive)); final ASN1Integer rASN1 = (ASN1Integer) asn1Sequence.getObjectAt(0); final ASN1Integer sASN1 = (ASN1Integer) asn1Sequence.getObjectAt(1); final X9IntegerConverter x9IntegerConverter = new X9IntegerConverter(); final byte[] r = x9IntegerConverter.integerToBytes(rASN1.getValue(), 32); final byte[] s = x9IntegerConverter.integerToBytes(sASN1.getValue(), 32); final byte[] concatenatedSignatureValue = new byte[64]; System.arraycopy(r, 0, concatenatedSignatureValue, 0, 32); System.arraycopy(s, 0, concatenatedSignatureValue, 32, 32); return concatenatedSignatureValue; }
/** * DER - From byte[] to Big Integer rs * UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06 DER [ITU-X690-2008] encoded * ECDSA signature [RFC5480] on the secp256k1 curve. I.e. a DER encoded * SEQUENCE { r INTEGER, s INTEGER } * * @param signature * @return * @throws IOException */ public static BigInteger[] decodeToBigIntegerArray(byte[] signature) throws IOException { ASN1InputStream decoder = new ASN1InputStream(signature); DLSequence seq = (DLSequence) decoder.readObject(); ASN1Integer r = (ASN1Integer) seq.getObjectAt(0); ASN1Integer s = (ASN1Integer) seq.getObjectAt(1); decoder.close(); BigInteger[] ret = new BigInteger[2]; ret[0] = r.getPositiveValue(); ret[1] = s.getPositiveValue(); return ret; }
/** * DER - From byte[] to Big Integer rs * UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06 DER [ITU-X690-2008] encoded * ECDSA signature [RFC5480] on the secp256k1 curve. I.e. a DER encoded * SEQUENCE { r INTEGER, s INTEGER } * * @param signature * @return * @throws IOException */ public static BigInteger[] decodeToBigIntegerArray(byte[] signature) throws IOException { ASN1InputStream decoder = new ASN1InputStream(signature); DLSequence seq = (DLSequence) decoder.readObject(); ASN1Integer r = (ASN1Integer) seq.getObjectAt(0); ASN1Integer s = (ASN1Integer) seq.getObjectAt(1); decoder.close(); BigInteger[] ret = new BigInteger[2]; ret[0] = r.getPositiveValue(); ret[1] = s.getPositiveValue(); return ret; }
/** * This method returns the {@code ASN1Sequence} encapsulated in {@code DEROctetString}. The {@code DEROctetString} is represented as {@code byte} array. * * @param bytes {@code byte} representation of {@code DEROctetString} * @return encapsulated {@code ASN1Sequence} * @throws DSSException in case of a decoding problem */ public static ASN1Sequence getAsn1SequenceFromDerOctetString(byte[] bytes) throws DSSException { ASN1InputStream input = null; try { input = new ASN1InputStream(bytes); final DEROctetString s = (DEROctetString) input.readObject(); final byte[] content = s.getOctets(); input.close(); input = new ASN1InputStream(content); final ASN1Sequence seq = (ASN1Sequence) input.readObject(); return seq; } catch (IOException e) { throw new DSSException("Error when converting byte array to ASN1Sequence!", e); } finally { DSSUtils.closeQuietly(input); } }
@Override public boolean verify(byte[] hash, byte[] signature, byte[] publicKey) { ASN1InputStream asn1 = new ASN1InputStream(signature); try { ECDSASigner signer = new ECDSASigner(); signer.init(false, new ECPublicKeyParameters(curve.getCurve().decodePoint(publicKey), domain)); DLSequence seq = (DLSequence) asn1.readObject(); BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); return signer.verifySignature(hash, r, s); } catch (Exception e) { return false; } finally { try { asn1.close(); } catch (IOException ignored) { } } }