@Override public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException { for (Object o : keyInfo.getContent()) { if (o instanceof X509Data) { for (Object o2 : ((X509Data) o).getContent()) { if (o2 instanceof X509Certificate) { final X509Certificate cert = (X509Certificate) o2; return new KeySelectorResult() { public Key getKey() { return cert.getPublicKey(); } }; } } } } return null; }
@Test() public void select_wrong_structType() throws Exception { // given KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); KeyName struct = mock(KeyName.class); list.add(struct); doReturn(list).when(keyinfo).getContent(); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getMessage().contains("No X509Data element found.")); } }
@Test() public void select_x509Data_empty() throws Exception { // given KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); X509Data x509Data = mock(X509Data.class); list.add(x509Data); doReturn(list).when(keyinfo).getContent(); doReturn(new ArrayList<Object>()).when(x509Data).getContent(); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getMessage().contains("No X509Data element found.")); } }
@Test() public void select_x509Data_noCertificate() throws Exception { // given KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); X509Data x509Data = mock(X509Data.class); list.add(x509Data); doReturn(list).when(keyinfo).getContent(); ArrayList<Object> x509DataContent = new ArrayList<Object>(); x509DataContent.add(new String()); doReturn(x509DataContent).when(x509Data).getContent(); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getMessage().contains("No X509Data element found.")); } }
@Test() public void select_publicKey_exception() throws Exception { // given selector = spy(new X509KeySelector(keystore)); KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); X509Data x509Data = mock(X509Data.class); list.add(x509Data); doReturn(list).when(keyinfo).getContent(); ArrayList<Object> x509DataContent = new ArrayList<Object>(); x509DataContent.add(mock(X509Certificate.class)); doReturn(x509DataContent).when(x509Data).getContent(); doThrow(new KeyStoreException("key exception")).when(selector) .getPublicKeyFromKeystore(any(X509Certificate.class), any(SignatureMethod.class)); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getCause().getMessage().contains("key exception")); } }
@Test() public void select_wrong_structType() throws Exception { // given KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); KeyName struct = mock(KeyName.class); list.add(struct); doReturn(list).when(keyinfo).getContent(); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getMessage().contains( "No RSA/DSA KeyValue element found")); } }
@Test() public void select_publicKey_exception() throws Exception { // given KeyInfo keyinfo = mock(KeyInfo.class); ArrayList<XMLStructure> list = new ArrayList<XMLStructure>(); KeyValue struct = mock(KeyValue.class); list.add(struct); doReturn(list).when(keyinfo).getContent(); doThrow(new KeyException("test")).when(struct).getPublicKey(); // when try { selector.select(keyinfo, null, null, null); fail(); } catch (KeySelectorException e) { assertTrue(e.getCause().getMessage().contains("test")); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof KeyInfo)) { return false; } KeyInfo oki = (KeyInfo)o; boolean idsEqual = (id == null ? oki.getId() == null : id.equals(oki.getId())); return (keyInfoTypes.equals(oki.getContent()) && idsEqual); }
/** * Creates a <code>DOMXMLSignature</code> from the specified components. * * @param si the <code>SignedInfo</code> * @param ki the <code>KeyInfo</code>, or <code>null</code> if not specified * @param objs a list of <code>XMLObject</code>s or <code>null</code> * if not specified. The list is copied to protect against subsequent * modification. * @param id an optional id (specify <code>null</code> to omit) * @param signatureValueId an optional id (specify <code>null</code> to * omit) * @throws NullPointerException if <code>si</code> is <code>null</code> */ public DOMXMLSignature(SignedInfo si, KeyInfo ki, List<? extends XMLObject> objs, String id, String signatureValueId) { if (si == null) { throw new NullPointerException("signedInfo cannot be null"); } this.si = si; this.id = id; this.sv = new DOMSignatureValue(signatureValueId); if (objs == null) { this.objects = Collections.emptyList(); } else { this.objects = Collections.unmodifiableList(new ArrayList<XMLObject>(objs)); for (int i = 0, size = this.objects.size(); i < size; i++) { if (!(this.objects.get(i) instanceof XMLObject)) { throw new ClassCastException ("objs["+i+"] is not an XMLObject"); } } } this.ki = ki; }
/** * Creates a <code>DOMXMLSignature</code> from the specified components. * * @param si the <code>SignedInfo</code> * @param ki the <code>KeyInfo</code>, or <code>null</code> if not specified * @param objs a list of <code>XMLObject</code>s or <code>null</code> * if not specified. The list is copied to protect against subsequent * modification. * @param id an optional id (specify <code>null</code> to omit) * @param signatureValueId an optional id (specify <code>null</code> to * omit) * @throws NullPointerException if <code>si</code> is <code>null</code> */ public DOMXMLSignature(SignedInfo si, KeyInfo ki, List<? extends XMLObject> objs, String id, String signatureValueId) { if (si == null) { throw new NullPointerException("signedInfo cannot be null"); } this.si = si; this.id = id; this.sv = new DOMSignatureValue(signatureValueId); List<XMLObject> tempList = Collections.checkedList(new ArrayList<XMLObject>(), XMLObject.class); if (objs != null) { tempList.addAll(objs); } this.objects = Collections.unmodifiableList(tempList); this.ki = ki; }
private static void marshalInternal(XmlWriter xwriter, KeyInfo ki, String dsPrefix, XMLCryptoContext context, boolean declareNamespace) throws MarshalException { xwriter.writeStartElement(dsPrefix, "KeyInfo", XMLSignature.XMLNS); if (declareNamespace) { xwriter.writeNamespace(dsPrefix, XMLSignature.XMLNS); } xwriter.writeIdAttribute("", "", "Id", ki.getId()); // create and append KeyInfoType elements List<XMLStructure> keyInfoTypes = getContent(ki); for (XMLStructure kiType : keyInfoTypes) { xwriter.marshalStructure(kiType, dsPrefix, context); } xwriter.writeEndElement(); // "KeyInfo" }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof KeyInfo)) { return false; } KeyInfo oki = (KeyInfo)o; boolean idsEqual = id == null ? oki.getId() == null : id.equals(oki.getId()); return keyInfoTypes.equals(oki.getContent()) && idsEqual; }
@org.junit.Test @SuppressWarnings("unchecked") public void testgetContent() { KeyInfo[] infos = new KeyInfo[2]; infos[0] = fac.newKeyInfo (Collections.singletonList(fac.newKeyName("foo")), "skeleton"); infos[1] = fac.newKeyInfo (Collections.singletonList(fac.newKeyName("foo"))); for (int j = 0; j < infos.length; j++) { KeyInfo ki = infos[j]; List<XMLStructure> li = ki.getContent(); assertNotNull(ki.getContent()); Object[] content = li.toArray(); for (int i = 0; i < content.length; i++) { if (!(content[i] instanceof XMLStructure)) { fail("KeyInfo element has the wrong type"); } } } }
@Override public KeySelectorResult select(final KeyInfo keyInfo, final KeySelector.Purpose purpose, final AlgorithmMethod method, final XMLCryptoContext context) throws KeySelectorException { for (final Object object : keyInfo.getContent()) { final XMLStructure info = (XMLStructure) object; if (info instanceof X509Data) { final X509Data x509Data = (X509Data) info; for (final Object certificado : x509Data.getContent()) { if (certificado instanceof X509Certificate) { final X509Certificate x509Certificate = (X509Certificate) certificado; if (this.algEquals(method.getAlgorithm(), x509Certificate.getPublicKey().getAlgorithm())) { return new KeySelectorResult() { @Override public Key getKey() { return x509Certificate.getPublicKey(); } }; } } } } } throw new KeySelectorException("N\u00e3o foi localizada a chave do certificado."); }
static KeyAccessor getKeyAccessor() { KeyAccessor accessor = new KeyAccessor() { @Override public KeySelector getKeySelector(Message message) throws Exception { return KeySelector.singletonKeySelector(getKeyFromKeystore()); } @Override public KeyInfo getKeyInfo(Message mess, Node messageBody, KeyInfoFactory keyInfoFactory) throws Exception { return null; } }; return accessor; }
/** * Creates a <code>DOMXMLSignature</code> from the specified components. * * @param si the <code>SignedInfo</code> * @param ki the <code>KeyInfo</code>, or <code>null</code> if not specified * @param objs a list of <code>XMLObject</code>s or <code>null</code> * if not specified. The list is copied to protect against subsequent * modification. * @param id an optional id (specify <code>null</code> to omit) * @param signatureValueId an optional id (specify <code>null</code> to * omit) * @throws NullPointerException if <code>si</code> is <code>null</code> */ public DOMXMLSignature(SignedInfo si, KeyInfo ki, List objs, String id, String signatureValueId) { if (si == null) { throw new NullPointerException("signedInfo cannot be null"); } this.si = si; this.id = id; this.sv = new DOMSignatureValue(signatureValueId); if (objs == null) { this.objects = Collections.EMPTY_LIST; } else { List objsCopy = new ArrayList(objs); for (int i = 0, size = objsCopy.size(); i < size; i++) { if (!(objsCopy.get(i) instanceof XMLObject)) { throw new ClassCastException ("objs["+i+"] is not an XMLObject"); } } this.objects = Collections.unmodifiableList(objsCopy); } this.ki = ki; }