javax.xml.crypto.dsig.keyinfo.KeyValue Java Examples

The following examples show how to use javax.xml.crypto.dsig.keyinfo.KeyValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DigitalSignatures.java    From org.hl7.fhir.core with Apache License 2.0 8 votes vote down vote up
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, FHIRException {
  // http://docs.oracle.com/javase/7/docs/technotes/guides/security/xmldsig/XMLDigitalSignature.html
  //
  byte[] inputXml = "<Envelope xmlns=\"urn:envelope\">\r\n</Envelope>\r\n".getBytes();
  // load the document that's going to be signed
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
  dbf.setNamespaceAware(true);
  DocumentBuilder builder = dbf.newDocumentBuilder();  
  Document doc = builder.parse(new ByteArrayInputStream(inputXml)); 
  
  // create a key pair
  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(512);
  KeyPair kp = kpg.generateKeyPair(); 
  
  // sign the document
  DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement()); 
  XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); 
 
  Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null);
  SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));
  
  KeyInfoFactory kif = fac.getKeyInfoFactory(); 
  KeyValue kv = kif.newKeyValue(kp.getPublic());
  KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
  XMLSignature signature = fac.newXMLSignature(si, ki); 
  signature.sign(dsc);
  
  OutputStream os = System.out;
  new XmlGenerator().generate(doc.getDocumentElement(), os);
}
 
Example #2
Source File: DigitalSignatures.java    From org.hl7.fhir.core with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, FHIRException, org.hl7.fhir.exceptions.FHIRException {
  // http://docs.oracle.com/javase/7/docs/technotes/guides/security/xmldsig/XMLDigitalSignature.html
  //
  byte[] inputXml = "<Envelope xmlns=\"urn:envelope\">\r\n</Envelope>\r\n".getBytes();
  // load the document that's going to be signed
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
  dbf.setNamespaceAware(true);
  DocumentBuilder builder = dbf.newDocumentBuilder();  
  Document doc = builder.parse(new ByteArrayInputStream(inputXml)); 
  
  // create a key pair
  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(512);
  KeyPair kp = kpg.generateKeyPair(); 
  
  // sign the document
  DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement()); 
  XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); 
 
  Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null);
  SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));
  
  KeyInfoFactory kif = fac.getKeyInfoFactory(); 
  KeyValue kv = kif.newKeyValue(kp.getPublic());
  KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
  XMLSignature signature = fac.newXMLSignature(si, ki); 
  signature.sign(dsc);
  
  OutputStream os = System.out;
  new XmlGenerator().generate(doc.getDocumentElement(), os);
}
 
Example #3
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #4
Source File: DOMKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #5
Source File: DOMKeyValue.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #6
Source File: DOMKeyValue.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #7
Source File: DOMKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #8
Source File: DOMKeyValue.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #9
Source File: DOMKeyValue.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #10
Source File: KeyValueKeySelectorTest.java    From development with Apache License 2.0 6 votes vote down vote up
@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"));
    }
}
 
Example #11
Source File: DOMKeyValue.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #12
Source File: SignatureVerifier.java    From IDES-Data-Preparation-Java with Creative Commons Zero v1.0 Universal 6 votes vote down vote up
public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, 
		AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
	if (keyInfo == null)
		throw new KeySelectorException("Null KeyInfo");
	List<?> list = keyInfo.getContent();
	PublicKey pk = null;

	for (int i = 0; i < list.size(); i++) {
		XMLStructure xmlStructure = (XMLStructure) list.get(i);
		if (xmlStructure instanceof KeyValue) {
			try {
				pk = ((KeyValue)xmlStructure).getPublicKey();
			} catch(KeyException ke) {
				throw new KeySelectorException(ke.getMessage());
			}
			break;
		} else if (xmlStructure instanceof X509Data) {
			X509Data x509data = (X509Data)xmlStructure;
			List<?> x509datalist = x509data.getContent();
			for (int j = 0; j < x509datalist.size(); j++) {
				if (x509datalist.get(j) instanceof X509Certificate) {
					X509Certificate cert = (X509Certificate)x509datalist.get(j);
					pk = cert.getPublicKey();
					break;
				}
			}
		}
	}
	if (pk != null) {
		final PublicKey retpk = pk;
		logger.debug("PublicKey from XML=" + pk);
		return new KeySelectorResult() {public Key getKey(){return retpk;}};
	}
	throw new KeySelectorException("Missing KeyValue");
}
 
Example #13
Source File: XMLDSigVerifier.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private PublicKey recoverPublicKeyFromXML(List xmlElements) throws KeyStoreException {
    boolean found = false;
    PublicKey keyVal = null;
    for (int i = 0; i < xmlElements.size(); i++)
    {
        XMLStructure xmlStructure = (XMLStructure) xmlElements.get(i);
        if (xmlStructure instanceof KeyValue)
        {
            //should only be one KeyValue
            if(found) throw new KeyStoreException("Duplicate Key found");
            found = true;
            KeyValue kv = (KeyValue) xmlStructure;
            try
            {
                keyVal = kv.getPublicKey();
            }
            catch (KeyException e)
            {
                e.printStackTrace();
            }
        }
    }
    return keyVal;
}
 
Example #14
Source File: DOMKeyValue.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #15
Source File: DOMKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #16
Source File: DOMKeyValue.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #17
Source File: DOMKeyValue.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #18
Source File: DOMKeyValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof KeyValue)) {
        return false;
    }
    try {
        KeyValue kv = (KeyValue)obj;
        if (publicKey == null ) {
            if (kv.getPublicKey() != null) {
                return false;
            }
        } else if (!publicKey.equals(kv.getPublicKey())) {
            return false;
        }
    } catch (KeyException ke) {
        // no practical way to determine if the keys are equal
        return false;
    }

    return true;
}
 
Example #19
Source File: DOMKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #20
Source File: DOMKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #21
Source File: SignatureVerifier.java    From IDES-Data-Preparation-Java with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
protected void setSigPublicKeyFromXml(String xml, DocumentBuilder docBuilderNSTrue) throws Exception {
	xml = sigStartElemToWrapXml + xml + sigEndElemToWrapXml;
       Document doc = docBuilderNSTrue.parse(new InputSource(new StringReader(xml)));
       DOMStructure ds = new DOMStructure(doc.getDocumentElement().getFirstChild());
       KeyInfo keyInfo = KeyInfoFactory.getInstance().unmarshalKeyInfo(ds);
	List<?> list = keyInfo.getContent();
	for (int i = 0; i < list.size(); i++) {
		XMLStructure xmlStructure = (XMLStructure) list.get(i);
		if (xmlStructure instanceof KeyValue) {
			try {
				sigPublicKey = ((KeyValue)xmlStructure).getPublicKey();
			} catch(KeyException ke) {
				throw new KeySelectorException(ke.getMessage());
			}
			break;
		} else if (xmlStructure instanceof X509Data) {
			X509Data x509data = (X509Data)xmlStructure;
			List<?> x509datalist = x509data.getContent();
			for (int j = 0; j < x509datalist.size(); j++) {
				if (x509datalist.get(j) instanceof X509Certificate) {
					X509Certificate cert = (X509Certificate)x509datalist.get(j);
					sigPublicKey = cert.getPublicKey();
					break;
				}
			}
		}
	}
}
 
Example #22
Source File: KeyValueKeySelector.java    From development with Apache License 2.0 5 votes vote down vote up
@Override
public KeySelectorResult select(KeyInfo keyInfo,
        KeySelector.Purpose purpose, AlgorithmMethod algorithmMethod,
        XMLCryptoContext context) throws KeySelectorException {

    if (keyInfo == null) {
        throw new KeySelectorException("Null KeyInfo object!");
    }

    @SuppressWarnings("unchecked")
    List<XMLStructure> list = keyInfo.getContent();
    for (XMLStructure xmlStructure : list) {
        if (xmlStructure instanceof KeyValue) {
            PublicKey publicKey = null;
            try {
                publicKey = ((KeyValue) xmlStructure).getPublicKey();
            } catch (KeyException ke) {
                throw new KeySelectorException(ke);
            }
            if (algorithmCompatibleWithMethod(
                    algorithmMethod.getAlgorithm(),
                    publicKey.getAlgorithm())) {
                return new SimpleKeySelectorResult(publicKey);
            }
        }
    }

    throw new KeySelectorException("No RSA/DSA KeyValue element found");
}
 
Example #23
Source File: XMLSignatureBuilder.java    From development with Apache License 2.0 5 votes vote down vote up
public Document sign(FileInputStream fileStream, KeyPair keyPair)
        throws ParserConfigurationException, SAXException, IOException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        KeyException, MarshalException, XMLSignatureException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(fileStream);

    DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),
            document.getDocumentElement());
    XMLSignatureFactory signFactory = XMLSignatureFactory
            .getInstance("DOM");
    Reference ref = signFactory.newReference("", signFactory
            .newDigestMethod(digestMethod, null), Collections
            .singletonList(signFactory.newTransform(Transform.ENVELOPED,
                    (TransformParameterSpec) null)), null, null);
    SignedInfo si = signFactory.newSignedInfo(signFactory
            .newCanonicalizationMethod(
                    CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                    (C14NMethodParameterSpec) null), signFactory
            .newSignatureMethod(signatureMethod, null), Collections
            .singletonList(ref));

    KeyInfoFactory kif = signFactory.getKeyInfoFactory();
    KeyValue kv = kif.newKeyValue(keyPair.getPublic());
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));

    XMLSignature signature = signFactory.newXMLSignature(si, ki);
    signature.sign(signContext);

    return document;
}
 
Example #24
Source File: DOMKeyValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #25
Source File: DOMKeyValue.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #26
Source File: XML.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * Sign the XML document using xmldsig.
 * @param document the document to sign; it will be modified by the method.
 * @param publicKey the public key from the key pair to sign the document.
 * @param privateKey the private key from the key pair to sign the document.
 * @return the signed document for chaining.
 */
public static Document sign(Document document, RSAPublicKey publicKey, RSAPrivateKey privateKey) {
    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory();

    try {
        Reference ref =fac.newReference(
                "",
                fac.newDigestMethod(DigestMethod.SHA1, null),
                Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
                null,
                null);
        SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
                                                                        (C14NMethodParameterSpec) null),
                                          fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
                                          Collections.singletonList(ref));
        DOMSignContext dsc = new DOMSignContext(privateKey, document.getDocumentElement());
        KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey);
        KeyInfo ki = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue));
        XMLSignature signature = fac.newXMLSignature(si, ki);
        signature.sign(dsc);
    } catch (Exception e) {
        Logger.warn("Error while signing an XML document.", e);
    }

    return document;
}
 
Example #27
Source File: DefaultSAML2Validator.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {

    if (keyInfo == null) {
        throw new KeySelectorException("Null KeyInfo object!");
    }
    SignatureMethod sm = (SignatureMethod) method;
    @SuppressWarnings("unchecked")
    List<XMLStructure> list = keyInfo.getContent();

    for (XMLStructure xmlStructure : list) {
        if (xmlStructure instanceof KeyValue) {
            PublicKey pk = null;
            try {
                pk = ((KeyValue) xmlStructure).getPublicKey();
            } catch (KeyException ke) {
                throw new KeySelectorException(ke);
            }
            // make sure algorithm is compatible with method
            if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
                return new SimpleKeySelectorResult(pk);
            }
        }
        if (xmlStructure instanceof X509Data) {
            X509Data xd = (X509Data) xmlStructure;
            @SuppressWarnings("unchecked")
            Iterator<Object> data = xd.getContent().iterator();
            for (; data.hasNext();) {
                Object o = data.next();
                if (o instanceof X509Certificate) {
                    X509Certificate cert = (X509Certificate) o;
                    return new SimpleKeySelectorResult(cert.getPublicKey());
                }
            }
        }
    }
    throw new KeySelectorException("No KeyValue element found!");
}
 
Example #28
Source File: DOMKeyValue.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #29
Source File: DOMKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}
 
Example #30
Source File: DOMKeyValue.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static KeyValue unmarshal(Element kvElem) throws MarshalException {
    Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
    if (kvtElem.getLocalName().equals("DSAKeyValue")) {
        return new DSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
        return new RSA(kvtElem);
    } else if (kvtElem.getLocalName().equals("ECKeyValue")) {
        return new EC(kvtElem);
    } else {
        return new Unknown(kvtElem);
    }
}