private static SubjectKeyIdentifier createSubjectKeyIdentifier(Key key) throws IOException { try (ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(key.getEncoded()))) { ASN1Sequence seq = (ASN1Sequence) is.readObject(); SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(seq); return new BcX509ExtensionUtils().createSubjectKeyIdentifier(info); } }
@Override public int doEndTag() throws JspException { try { final ASN1InputStream decoder = new ASN1InputStream(value); ASN1Primitive primitive = decoder.readObject(); while (primitive != null && !(primitive instanceof ASN1Null)) { pageContext.getOut().write(doPrimitive(primitive)); primitive = decoder.readObject(); } } catch (Exception ex) { throw new JspException(ex.getMessage()); } return super.doEndTag(); }
private BigInteger getCrlNumber(X509CRL crl) { byte[] crlNumberExtensionValue = crl.getExtensionValue(Extension.cRLNumber.getId()); if (null == crlNumberExtensionValue) { return null; } try { ASN1InputStream asn1IS1 = null, asn1IS2 = null; try { asn1IS1 = new ASN1InputStream(crlNumberExtensionValue); ASN1OctetString octetString = (ASN1OctetString)asn1IS1.readObject(); byte[] octets = octetString.getOctets(); asn1IS2 = new ASN1InputStream(octets); ASN1Integer integer = (ASN1Integer)asn1IS2.readObject(); return integer.getPositiveValue(); } finally { IOUtils.closeQuietly(asn1IS2); IOUtils.closeQuietly(asn1IS1); } } catch (IOException e) { throw new RuntimeException("I/O error: " + e.getMessage(), e); } }
/** * Get the "subject" from the TBSCertificate bytes that are passed in * @param enc A TBSCertificate in a byte array * @return a ASN1Primitive */ public static ASN1Primitive getSubject(byte[] enc) { try { ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(enc)); ASN1Sequence seq = (ASN1Sequence)in.readObject(); return (ASN1Primitive)seq.getObjectAt(seq.getObjectAt(0) instanceof ASN1TaggedObject ? 5 : 4); } catch (IOException e) { throw new ExceptionConverter(e); } }
try (ByteArrayInputStream inStream = new ByteArrayInputStream(signature)) { ASN1InputStream asnInputStream = new ASN1InputStream(inStream); ASN1Primitive asn1 = asnInputStream.readObject(); if (asn1 instanceof ASN1Sequence) { ASN1Sequence asn1Sequence = (ASN1Sequence) asn1; ASN1Encodable[] asn1Encodables = asn1Sequence.toArray(); for (ASN1Encodable asn1Encodable : asn1Encodables) { ASN1Primitive asn1Primitive = asn1Encodable.toASN1Primitive(); if (asn1Primitive instanceof ASN1Integer) { ASN1Integer asn1Integer = (ASN1Integer) asn1Primitive; BigInteger integer = asn1Integer.getValue(); if (count < 2) { sigs[count] = integer;
/** * Octet String encapsulation - see RFC 3280 section 4.1 */ private static byte[] decodeEncapsulation(byte[] payload) throws IOException { ASN1InputStream payloadStream = new ASN1InputStream(new ByteArrayInputStream(payload)); return ((ASN1OctetString) payloadStream.readObject()).getOctets(); }
static DERForRecipientParams calculateDERForRecipientParams(byte[] in) throws IOException, GeneralSecurityException { String s = "1.2.840.113549.3.2"; DERForRecipientParams parameters = new DERForRecipientParams(); AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance(s); AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters(); ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(algorithmparameters.getEncoded("ASN.1")); ASN1InputStream asn1inputstream = new ASN1InputStream(bytearrayinputstream); ASN1Primitive derobject = asn1inputstream.readObject(); KeyGenerator keygenerator = KeyGenerator.getInstance(s); keygenerator.init(128); SecretKey secretkey = keygenerator.generateKey(); Cipher cipher = Cipher.getInstance(s); cipher.init(1, secretkey, algorithmparameters); parameters.abyte0 = secretkey.getEncoded(); parameters.abyte1 = cipher.doFinal(in); parameters.algorithmIdentifier = new AlgorithmIdentifier(new ASN1ObjectIdentifier(s), derobject); return parameters; }
/** * 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 static ASN1Sequence getReq( byte[] r) throws IOException { ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(r)); return ASN1Sequence.getInstance(aIn.readObject()); }
public KerberosRelevantAuthData ( byte[] token, Map<Integer, KerberosKey> keys ) throws PACDecodingException { DLSequence authSequence; try { try ( ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token)) ) { authSequence = ASN1Util.as(DLSequence.class, stream); } } catch ( IOException e ) { throw new PACDecodingException("Malformed kerberos ticket", e); } this.authorizations = new ArrayList<>(); Enumeration<?> authElements = authSequence.getObjects(); while ( authElements.hasMoreElements() ) { DLSequence authElement = ASN1Util.as(DLSequence.class, authElements); ASN1Integer authType = ASN1Util.as(ASN1Integer.class, ASN1Util.as(DERTaggedObject.class, authElement, 0)); DEROctetString authData = ASN1Util.as(DEROctetString.class, ASN1Util.as(DERTaggedObject.class, authElement, 1)); this.authorizations.addAll(KerberosAuthData.parse(authType.getValue().intValue(), authData.getOctets(), keys)); } }
InputStream signatureIn = new ByteArrayInputStream(signature); DERObject obj = new ASN1InputStream(signatureIn).readObject(); ContentInfo contentInfo = ContentInfo.getInstance(obj); while (certificates.hasMoreElements()) { DERObject certObj = (DERObject) certificates.nextElement(); InputStream in = new ByteArrayInputStream(certObj.getDEREncoded()); certList.add(cf.generateCertificate(in));
ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(data)); DERApplicationSpecific application = (DERApplicationSpecific) stream.readObject(); ASN1Sequence sequence = (ASN1Sequence) application.getObject(BERTags.SEQUENCE); Enumeration enum = sequence.getObjects(); while (enum.hasMoreElements()) { ASN1Primitive object = (ASN1Primitive) secEnum.nextElement(); System.out.println(object); }
public KerberosToken ( byte[] token, KerberosKey[] keys ) throws PACDecodingException { if ( token.length <= 0 ) throw new PACDecodingException("Empty kerberos token"); try { ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token)); DERApplicationSpecific derToken = ASN1Util.as(DERApplicationSpecific.class, stream); if ( derToken == null || !derToken.isConstructed() ) throw new PACDecodingException("Malformed kerberos token"); stream.close(); stream = new ASN1InputStream(new ByteArrayInputStream(derToken.getContents())); ASN1ObjectIdentifier kerberosOid = ASN1Util.as(ASN1ObjectIdentifier.class, stream); if ( !kerberosOid.getId().equals(KerberosConstants.KERBEROS_OID) ) throw new PACDecodingException("Not a kerberos token"); int read = 0; int readLow = stream.read() & 0xff; int readHigh = stream.read() & 0xff; read = ( readHigh << 8 ) + readLow; if ( read != 0x01 ) throw new PACDecodingException("Malformed kerberos token"); DERApplicationSpecific krbToken = ASN1Util.as(DERApplicationSpecific.class, stream); if ( krbToken == null || !krbToken.isConstructed() ) throw new PACDecodingException("Malformed kerberos token"); stream.close(); this.apRequest = new KerberosApRequest(krbToken.getContents(), keys); } catch ( IOException e ) { throw new PACDecodingException("Malformed kerberos token", e); } }
try ( final ASN1InputStream decoder = new ASN1InputStream( item ) ) final ASN1Primitive object = decoder.readObject(); final ASN1Sequence otherNameSeq = (ASN1Sequence) object; final ASN1ObjectIdentifier typeId = (ASN1ObjectIdentifier) otherNameSeq.getObjectAt( 0 ); final ASN1TaggedObject taggedValue = (ASN1TaggedObject) otherNameSeq.getObjectAt( 1 ); switch ( typeId.getId() ) return otherName; Log.debug( "Ignoring subjectAltName 'otherName' type-id '{}' that's neither id-on-xmppAddr nor id-on-dnsSRV.", typeId.getId() ); return null;
private static ASN1Primitive getObject( String oid, byte[] ext) throws AnnotatedException { try { ASN1InputStream aIn = new ASN1InputStream(ext); ASN1OctetString octs = (ASN1OctetString)aIn.readObject(); aIn = new ASN1InputStream(octs.getOctets()); return aIn.readObject(); } catch (Exception e) { throw new AnnotatedException("exception processing extension " + oid, e); } }
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()}; } // .... }
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()); } } }
/** * 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; }
try ( final ASN1InputStream decoder = new ASN1InputStream( (byte[]) value ) ) final ASN1Primitive object = decoder.readObject(); final ASN1Sequence otherNameSeq = (ASN1Sequence) object; final ASN1ObjectIdentifier typeId = (ASN1ObjectIdentifier) otherNameSeq.getObjectAt( 0 ); final ASN1TaggedObject taggedValue = (ASN1TaggedObject) otherNameSeq.getObjectAt( 1 );
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException String algorithm = PKCSObjectIdentifiers.RC2_CBC.getId(); AlgorithmParameterGenerator apg; KeyGenerator keygen; try (ASN1InputStream input = new ASN1InputStream(parameters.getEncoded("ASN.1"))) object = input.readObject(); DERSet set = new DERSet(new RecipientInfo(recipientInfo)); AlgorithmIdentifier algorithmId = new AlgorithmIdentifier(new ASN1ObjectIdentifier(algorithm), object); EncryptedContentInfo encryptedInfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmId, new DEROctetString(bytes));