@Test public void testMalleableSignature() throws Exception { for (int i = 0; i < 1000; i++) { PrivateKey key = PrivateKey.createNew(true); byte[] signature = key.sign(MESSAGE.getBytes()); ASN1StreamParser asn1 = new ASN1StreamParser(signature); DERSequence seq = (DERSequence) asn1.readObject().toASN1Primitive(); BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue(); assertTrue(key.getPublic().verify(MESSAGE.getBytes(), signature)); assertTrue(String.format("Signature is not canonical for iteration %d key %s", i, key), isCanonical(s)); } }
private void parserTest() { for (int i = 0; i != streams.length; i++) { ASN1StreamParser aIn = new ASN1StreamParser(Base64.decode(streams[i])); try { Object obj; while ((obj = aIn.readObject()) != null) { } fail("bad stream parsed successfully!"); } catch (IOException e) { // ignore } } }
public void performTest() throws Exception { testPKCS12Store(); testGOSTStore(); // converter tests KeyStore kS = KeyStore.getInstance("PKCS12", "BC"); byte[] data = PKCS12Util.convertToDefiniteLength(pkcs12); kS.load(new ByteArrayInputStream(data), passwd); // check MAC ASN1Encodable obj = new ASN1StreamParser(data).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed DER conversion test."); } data = PKCS12Util.convertToDefiniteLength(pkcs12, passwd, "BC"); kS.load(new ByteArrayInputStream(data), passwd); //check MAC obj = new ASN1StreamParser(data).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed deep DER conversion test - outer."); } Pfx pfx = Pfx.getInstance(obj); obj = new ASN1StreamParser(ASN1OctetString.getInstance(pfx.getAuthSafe().getContent()).getOctets()).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed deep DER conversion test - inner."); } }
private static ASN1Encodable readAsn1Encodable(byte[] encoded) throws CertprofileException { ASN1StreamParser parser = new ASN1StreamParser(encoded); try { return parser.readObject(); } catch (IOException ex) { throw new CertprofileException("could not parse the constant extension value", ex); } }
public static Map<ASN1ObjectIdentifier, ExtensionValue> buildConstantExtesions( ExtensionsType extensionsType) throws CertprofileException { if (extensionsType == null) { return null; } Map<ASN1ObjectIdentifier, ExtensionValue> map = new HashMap<>(); for (ExtensionType m : extensionsType.getExtension()) { ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier(m.getType().getValue()); if (Extension.subjectAlternativeName.equals(oid) || Extension.subjectInfoAccess.equals(oid) || Extension.biometricInfo.equals(oid)) { continue; } if (m.getValue() == null || !(m.getValue().getAny() instanceof ConstantExtValue)) { continue; } ConstantExtValue extConf = (ConstantExtValue) m.getValue().getAny(); byte[] encodedValue = extConf.getValue(); ASN1StreamParser parser = new ASN1StreamParser(encodedValue); ASN1Encodable value; try { value = parser.readObject(); } catch (IOException ex) { throw new CertprofileException("could not parse the constant extension value", ex); } ExtensionValue extension = new ExtensionValue(m.isCritical(), value); map.put(oid, extension); } if (CollectionUtil.isEmpty(map)) { return null; } return Collections.unmodifiableMap(map); }
public static Map<ASN1ObjectIdentifier, QaExtensionValue> buildConstantExtesions( ExtensionsType extensionsType) throws CertprofileException { if (extensionsType == null) { return null; } Map<ASN1ObjectIdentifier, QaExtensionValue> map = new HashMap<>(); for (ExtensionType m : extensionsType.getExtension()) { if (m.getValue() == null || !(m.getValue().getAny() instanceof ConstantExtValue)) { continue; } ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier(m.getType().getValue()); if (Extension.subjectAlternativeName.equals(oid) || Extension.subjectInfoAccess.equals(oid) || Extension.biometricInfo.equals(oid)) { continue; } ConstantExtValue extConf = (ConstantExtValue) m.getValue().getAny(); byte[] encodedValue = extConf.getValue(); ASN1StreamParser parser = new ASN1StreamParser(encodedValue); try { parser.readObject(); } catch (IOException ex) { throw new CertprofileException("could not parse the constant extension value", ex); } QaExtensionValue extension = new QaExtensionValue(m.isCritical(), encodedValue); map.put(oid, extension); } if (CollectionUtil.isEmpty(map)) { return null; } return Collections.unmodifiableMap(map); }
public void testLongTag() throws IOException { ASN1StreamParser aIn = new ASN1StreamParser(longTagged); ASN1TaggedObjectParser tagged = (ASN1TaggedObjectParser)aIn.readObject(); assertEquals(31, tagged.getTagNo()); }
public void testDERReading() throws Exception { ASN1StreamParser aIn = new ASN1StreamParser(seqData); ASN1SequenceParser seq = (ASN1SequenceParser)aIn.readObject(); Object o; int count = 0; assertNotNull("null sequence returned", seq); while ((o = seq.readObject()) != null) { switch (count) { case 0: assertTrue(o instanceof DERInteger); break; case 1: assertTrue(o instanceof DERObjectIdentifier); break; } count++; } assertEquals("wrong number of objects in sequence", 2, count); }
private void testNestedReading( byte[] data) throws Exception { ASN1StreamParser aIn = new ASN1StreamParser(data); ASN1SequenceParser seq = (ASN1SequenceParser)aIn.readObject(); Object o; int count = 0; assertNotNull("null sequence returned", seq); while ((o = seq.readObject()) != null) { switch (count) { case 0: assertTrue(o instanceof DERInteger); break; case 1: assertTrue(o instanceof DERObjectIdentifier); break; case 2: assertTrue(o instanceof ASN1SequenceParser); ASN1SequenceParser s = (ASN1SequenceParser)o; // NB: Must exhaust the nested parser while (s.readObject() != null) { // Nothing } break; } count++; } assertEquals("wrong number of objects in sequence", 3, count); }
public void testBERReading() throws Exception { ASN1StreamParser aIn = new ASN1StreamParser(berSeqData); ASN1SequenceParser seq = (ASN1SequenceParser)aIn.readObject(); Object o; int count = 0; assertNotNull("null sequence returned", seq); while ((o = seq.readObject()) != null) { switch (count) { case 0: assertTrue(o instanceof DERInteger); break; case 1: assertTrue(o instanceof DERObjectIdentifier); break; } count++; } assertEquals("wrong number of objects in sequence", 2, count); }
private void testParseWithNull(byte[] data) throws IOException { ASN1StreamParser aIn = new ASN1StreamParser(data); ASN1SequenceParser seq = (ASN1SequenceParser)aIn.readObject(); Object o; int count = 0; assertNotNull("null sequence returned", seq); while ((o = seq.readObject()) != null) { switch (count) { case 0: assertTrue(o instanceof ASN1Null); break; case 1: assertTrue(o instanceof DERInteger); break; case 2: assertTrue(o instanceof DERObjectIdentifier); break; } count++; } assertEquals("wrong number of objects in sequence", 3, count); }
public void testReadingWriting() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); BEROctetStringGenerator octGen = new BEROctetStringGenerator(bOut); OutputStream out = octGen.getOctetOutputStream(); out.write(new byte[] { 1, 2, 3, 4 }); out.write(new byte[4]); out.close(); ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray()); ASN1OctetStringParser s = (ASN1OctetStringParser)aIn.readObject(); InputStream in = s.getOctetStream(); int count = 0; while (in.read() >= 0) { count++; } assertEquals(8, count); }
public void testReadingWritingZeroInLength() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); BEROctetStringGenerator octGen = new BEROctetStringGenerator(bOut); OutputStream out = octGen.getOctetOutputStream(); out.write(new byte[] { 1, 2, 3, 4 }); out.write(new byte[512]); // forces a zero to appear in length out.close(); ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray()); ASN1OctetStringParser s = (ASN1OctetStringParser)aIn.readObject(); InputStream in = s.getOctetStream(); int count = 0; while (in.read() >= 0) { count++; } assertEquals(516, count); }
public CVCertificate(byte[] in) throws IllegalArgumentException, IOException { ASN1StreamParser asn1Parser = new ASN1StreamParser(in); DERApplicationSpecific cvcert = (DERApplicationSpecific) asn1Parser.readObject(); if (cvcert.getApplicationTag()!=0x21) throw new IllegalArgumentException("Can't find a CV Certificate"); ASN1Sequence derCert= (ASN1Sequence)cvcert.getObject(BERTags.SEQUENCE); // Das CV Cerificate ist eine Sequence DERApplicationSpecific body = (DERApplicationSpecific) derCert.getObjectAt(0); //Das erste Objekt des Certificates ist der Cert-Body if (body.getApplicationTag()!=0x4E) throw new IllegalArgumentException("Can't find a Body in the CV Certificate"); certBody = new CVCertBody(body); DERApplicationSpecific signature = (DERApplicationSpecific) derCert.getObjectAt(1); //Das zweite Objekt des Certificates ist die Signatur if (signature.getApplicationTag()!=0x37) throw new IllegalArgumentException("Can't find a Signature in the CV Certificate"); certSignature = new CVCertSignature(signature.getContents()); }
private void parseEnveloped(byte[] data) throws IOException { ASN1StreamParser aIn = new ASN1StreamParser(data); ContentInfoParser cP = new ContentInfoParser((ASN1SequenceParser)aIn.readObject()); EnvelopedDataParser eP = new EnvelopedDataParser((ASN1SequenceParser)cP.getContent(BERTags.SEQUENCE)); eP.getRecipientInfos().toASN1Primitive(); // Must drain the parser! EncryptedContentInfoParser ecP = eP.getEncryptedContentInfo(); ASN1OctetStringParser content = (ASN1OctetStringParser)ecP.getEncryptedContent(BERTags.OCTET_STRING); Streams.drain(content.getOctetStream()); }
public void testNestedStructure() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); BERSequenceGenerator sGen = new BERSequenceGenerator(bOut); sGen.addObject(new DERObjectIdentifier(CMSObjectIdentifiers.compressedData.getId())); BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true); cGen.addObject(new DERInteger(0)); // // AlgorithmIdentifier // DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream()); algGen.addObject(new DERObjectIdentifier("1.2")); algGen.close(); // // Encapsulated ContentInfo // BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream()); eiGen.addObject(new DERObjectIdentifier("1.1")); BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, true); // // output containing zeroes // OutputStream out = octGen.getOctetOutputStream(); out.write(new byte[] { 1, 2, 3, 4 }); out.write(new byte[4]); out.write(new byte[20]); out.close(); eiGen.close(); cGen.close(); sGen.close(); // // reading back // ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray()); ContentInfoParser cp = new ContentInfoParser((ASN1SequenceParser)aIn.readObject()); CompressedDataParser comData = new CompressedDataParser((ASN1SequenceParser)cp.getContent(BERTags.SEQUENCE)); ContentInfoParser content = comData.getEncapContentInfo(); ASN1OctetStringParser bytes = (ASN1OctetStringParser)content.getContent(BERTags.OCTET_STRING); InputStream in = bytes.getOctetStream(); int count = 0; while (in.read() >= 0) { count++; } assertEquals(28, count); }
public void testReadingWritingNested() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); BERSequenceGenerator sGen = new BERSequenceGenerator(bOut); BEROctetStringGenerator octGen = new BEROctetStringGenerator(sGen.getRawOutputStream()); OutputStream out = octGen.getOctetOutputStream(); BERSequenceGenerator inSGen = new BERSequenceGenerator(out); BEROctetStringGenerator inOctGen = new BEROctetStringGenerator(inSGen.getRawOutputStream()); OutputStream inOut = inOctGen.getOctetOutputStream(); inOut.write(new byte[] { 1, 2, 3, 4 }); inOut.write(new byte[10]); inOut.close(); inSGen.close(); out.close(); sGen.close(); ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray()); ASN1SequenceParser sq = (ASN1SequenceParser)aIn.readObject(); ASN1OctetStringParser s = (ASN1OctetStringParser)sq.readObject(); ASN1StreamParser aIn2 = new ASN1StreamParser(s.getOctetStream()); ASN1SequenceParser sq2 = (ASN1SequenceParser)aIn2.readObject(); ASN1OctetStringParser inS = (ASN1OctetStringParser)sq2.readObject(); InputStream in = inS.getOctetStream(); int count = 0; while (in.read() >= 0) { count++; } assertEquals(14, count); }