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) {} } }
/** * Change current SEQUENCE object to be encoded as {@link DLSequence}. * This is part of Direct Length form serialization. */ ASN1Primitive toDLObject() { ASN1Sequence dlSeq = new DLSequence(); dlSeq.seq = this.seq; return dlSeq; }
return null; dlSequence = (DLSequence) fromExtensionValue; for (int i = 0; i < dlSequence.size(); i++) { asn1Encodable = dlSequence.getObjectAt(i); if (asn1Encodable instanceof DLSequence) break; return null; dlSequence = (DLSequence) asn1Encodable; for (int i = 0; i < dlSequence.size(); i++) { asn1Encodable = dlSequence.getObjectAt(i); if (asn1Encodable instanceof DERTaggedObject) break;
/** * Add a SafeBag that is to be included as is. * * @param data the SafeBag to add. * @return this builder. * @throws IOException */ public PKCS12PfxPduBuilder addData(PKCS12SafeBag data) throws IOException { dataVector.add(new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(new DLSequence(data.toASN1Structure()).getEncoded()))); return this; }
void encode( ASN1OutputStream out) throws IOException { ASN1OutputStream dOut = out.getDLSubStream(); int length = getBodyLength(); out.write(BERTags.SEQUENCE | BERTags.CONSTRUCTED); out.writeLength(length); for (Enumeration e = this.getObjects(); e.hasMoreElements();) { Object obj = e.nextElement(); dOut.writeObject((ASN1Encodable)obj); } } }
int encodedLength() throws IOException { int length = getBodyLength(); return 1 + StreamUtil.calculateBodyLength(length) + length; }
private static BigInteger extractPrivateKeyFromASN1(byte[] asn1privkey) { // To understand this code, see the definition of the ASN.1 format for EC private keys in the OpenSSL source // code in ec_asn1.c: // // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) // try { ASN1InputStream decoder = new ASN1InputStream(asn1privkey); DLSequence seq = (DLSequence) decoder.readObject(); checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key"); checkArgument(((DERInteger) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE), "Input is of wrong version"); Object obj = seq.getObjectAt(1); byte[] bits = ((ASN1OctetString) obj).getOctets(); decoder.close(); return new BigInteger(1, bits); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, reading from memory stream. } }
/** * Add a SafeBag that is to be included as is. * * @param data the SafeBag to add. * @return this builder. * @throws IOException */ public PKCS12PfxPduBuilder addData(PKCS12SafeBag data) throws IOException { dataVector.add(new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(new DLSequence(data.toASN1Structure()).getEncoded()))); return this; }
/** * A note on the implementation: * <p> * As DL requires the constructed, definite-length model to * be used for structured types, this varies slightly from the * ASN.1 descriptions given. Rather than just outputting SEQUENCE, * we also have to specify CONSTRUCTED, and the objects length. */ void encode( ASN1OutputStream out) throws IOException { ASN1OutputStream dOut = out.getDLSubStream(); int length = getBodyLength(); out.write(BERTags.SEQUENCE | BERTags.CONSTRUCTED); out.writeLength(length); for (Enumeration e = this.getObjects(); e.hasMoreElements();) { Object obj = e.nextElement(); dOut.writeObject((ASN1Encodable)obj); } } }
int encodedLength() throws IOException { int length = getBodyLength(); return 1 + StreamUtil.calculateBodyLength(length) + length; }
public static ECDSASignature decodeFromDER(byte[] bytes) { try (ASN1InputStream 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); } // ignore }
decoder.close(); checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key"); checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE), "Input is of wrong version"); byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets(); BigInteger privkey = new BigInteger(1, privbits); ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3); checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number"); byte[] pubbits = ((DERBitString)pubkey.getObject()).getBytes();
ASN1Primitive toDLObject() { ASN1Sequence dlSeq = new DLSequence(); dlSeq.seq = this.seq; return dlSeq; }
/** * return the ASN.1 encoded representation of this object. */ public byte[] getEncoded(String encoding) throws IOException { if (ASN1Encoding.DL.equals(encoding)) { return new DLSequence(new ASN1Encodable[] { resp.getStatus(), timeStampToken.toCMSSignedData().toASN1Structure() }).getEncoded(encoding); } return resp.getEncoded(encoding); } }
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) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (decoder != null) try { decoder.close(); } catch (IOException x) {} } }
decoder.close(); checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key"); checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE), "Input is of wrong version"); byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets(); BigInteger privkey = new BigInteger(1, privbits); ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3); checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number"); byte[] pubbits = ((DERBitString)pubkey.getObject()).getBytes();
static ASN1Sequence createSequence(ASN1EncodableVector v) { return v.size() < 1 ? EMPTY_SEQUENCE : new DLSequence(v); }
public static ECDSASignature decodeFromDER(byte[] bytes) throws IllegalArgumentException { ASN1InputStream decoder = null; try { decoder = new ASN1InputStream(bytes); final ASN1Primitive seqObj = decoder.readObject(); if (seqObj == null) throw new IllegalArgumentException("Reached past end of ASN.1 stream."); if (!(seqObj instanceof DLSequence)) throw new IllegalArgumentException("Read unexpected class: " + seqObj.getClass().getName()); final DLSequence seq = (DLSequence) seqObj; 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 IllegalArgumentException(e); } finally { if (decoder != null) try { decoder.close(); } catch (IOException x) {} } }
decoder.close(); checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key"); checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE), "Input is of wrong version"); byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets(); BigInteger privkey = new BigInteger(1, privbits); ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3); checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number"); byte[] pubbits = ((DERBitString)pubkey.getObject()).getBytes();
static ASN1Sequence createSequence(ASN1EncodableVector v) { return v.size() < 1 ? EMPTY_SEQUENCE : new DLSequence(v); }