Search in sources :

Example 1 with RSAKeyValueType

use of org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType in project keycloak by keycloak.

the class SAMLParserTest method testSaml20AssertionContents.

@Test
public void testSaml20AssertionContents() throws Exception {
    AssertionType a = assertParsed("saml20-assertion-example.xml", AssertionType.class);
    assertThat(a.getSubject().getConfirmation(), hasSize(1));
    assertThat(a.getSubject().getConfirmation().get(0).getSubjectConfirmationData(), notNullValue());
    assertThat(a.getSubject().getConfirmation().get(0).getSubjectConfirmationData().getAnyType(), instanceOf(KeyInfoType.class));
    KeyInfoType kit = (KeyInfoType) a.getSubject().getConfirmation().get(0).getSubjectConfirmationData().getAnyType();
    assertThat(kit.getContent(), hasItem(instanceOf(RSAKeyValueType.class)));
    RSAKeyValueType rsaKit = (RSAKeyValueType) kit.getContent().get(0);
    assertThat(rsaKit.getModulus(), notNullValue());
    assertThat(rsaKit.getExponent(), notNullValue());
    assertThat(a.getStatements(), containsInAnyOrder(instanceOf(AuthnStatementType.class), instanceOf(AttributeStatementType.class)));
    for (StatementAbstractType statement : a.getStatements()) {
        if (statement instanceof AuthnStatementType) {
            AuthnStatementType as = (AuthnStatementType) statement;
            assertThat(as.getSessionNotOnOrAfter(), notNullValue());
            assertThat(as.getSessionNotOnOrAfter(), is(XMLTimeUtil.parse("2009-06-17T18:55:10.738Z")));
            final AuthnContextType ac = as.getAuthnContext();
            assertThat(ac, notNullValue());
            assertThat(ac.getSequence(), notNullValue());
            assertThat(ac.getSequence().getClassRef().getValue(), is(JBossSAMLURIConstants.AC_UNSPECIFIED.getUri()));
            assertThat(ac.getSequence(), notNullValue());
            assertThat(ac.getSequence().getAuthnContextDecl(), nullValue());
        }
    }
}
Also used : AuthnStatementType(org.keycloak.dom.saml.v2.assertion.AuthnStatementType) EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) AssertionType(org.keycloak.dom.saml.v2.assertion.AssertionType) AuthnContextType(org.keycloak.dom.saml.v2.assertion.AuthnContextType) RSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType) StatementAbstractType(org.keycloak.dom.saml.v2.assertion.StatementAbstractType) KeyInfoType(org.keycloak.dom.xmlsec.w3.xmldsig.KeyInfoType) Test(org.junit.Test)

Example 2 with RSAKeyValueType

use of org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType in project keycloak by keycloak.

the class XMLSignatureUtil method getRSAKeyValue.

/**
 * Given a dsig:DSAKeyValue element, return {@link DSAKeyValueType}
 *
 * @param element
 *
 * @return
 *
 * @throws ProcessingException
 */
public static RSAKeyValueType getRSAKeyValue(Element element) throws ParsingException {
    RSAKeyValueType rsa = new RSAKeyValueType();
    NodeList nl = element.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element childElement = (Element) node;
            String tag = childElement.getLocalName();
            byte[] text = childElement.getTextContent().getBytes(GeneralConstants.SAML_CHARSET);
            if (WSTrustConstants.XMLDSig.MODULUS.equals(tag)) {
                rsa.setModulus(text);
            } else if (WSTrustConstants.XMLDSig.EXPONENT.equals(tag)) {
                rsa.setExponent(text);
            }
        }
    }
    return rsa;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) RSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType)

Example 3 with RSAKeyValueType

use of org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType in project keycloak by keycloak.

the class SAML11ParserUtil method parseRSAKeyValue.

public static RSAKeyValueType parseRSAKeyValue(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, WSTrustConstants.XMLDSig.RSA_KEYVALUE);
    XMLEvent xmlEvent = null;
    String tag = null;
    RSAKeyValueType rsaKeyValue = new RSAKeyValueType();
    while (xmlEventReader.hasNext()) {
        xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            tag = StaxParserUtil.getElementName((EndElement) xmlEvent);
            if (tag.equals(WSTrustConstants.XMLDSig.RSA_KEYVALUE)) {
                xmlEvent = StaxParserUtil.getNextEndElement(xmlEventReader);
                break;
            } else
                throw logger.parserUnknownEndElement(tag, xmlEvent.getLocation());
        }
        startElement = (StartElement) xmlEvent;
        tag = StaxParserUtil.getElementName(startElement);
        if (tag.equals(WSTrustConstants.XMLDSig.MODULUS)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            String text = StaxParserUtil.getElementText(xmlEventReader);
            rsaKeyValue.setModulus(text.getBytes(GeneralConstants.SAML_CHARSET));
        } else if (tag.equals(WSTrustConstants.XMLDSig.EXPONENT)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            String text = StaxParserUtil.getElementText(xmlEventReader);
            rsaKeyValue.setExponent(text.getBytes(GeneralConstants.SAML_CHARSET));
        } else
            throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    return rsaKeyValue;
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) XMLEvent(javax.xml.stream.events.XMLEvent) RSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType)

Example 4 with RSAKeyValueType

use of org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType in project keycloak by keycloak.

the class XMLSignatureUtil method createKeyValue.

/**
 * <p>
 * Creates a {@code KeyValueType} that wraps the specified public key. This method supports DSA and RSA keys.
 * </p>
 *
 * @param key the {@code PublicKey} that will be represented as a {@code KeyValueType}.
 *
 * @return the constructed {@code KeyValueType} or {@code null} if the specified key is neither a DSA nor a RSA
 *         key.
 */
public static KeyValueType createKeyValue(PublicKey key) {
    if (key instanceof RSAPublicKey) {
        RSAPublicKey pubKey = (RSAPublicKey) key;
        byte[] modulus = pubKey.getModulus().toByteArray();
        byte[] exponent = pubKey.getPublicExponent().toByteArray();
        RSAKeyValueType rsaKeyValue = new RSAKeyValueType();
        rsaKeyValue.setModulus(Base64.encodeBytes(modulus).getBytes(GeneralConstants.SAML_CHARSET));
        rsaKeyValue.setExponent(Base64.encodeBytes(exponent).getBytes(GeneralConstants.SAML_CHARSET));
        return rsaKeyValue;
    } else if (key instanceof DSAPublicKey) {
        DSAPublicKey pubKey = (DSAPublicKey) key;
        byte[] P = pubKey.getParams().getP().toByteArray();
        byte[] Q = pubKey.getParams().getQ().toByteArray();
        byte[] G = pubKey.getParams().getG().toByteArray();
        byte[] Y = pubKey.getY().toByteArray();
        DSAKeyValueType dsaKeyValue = new DSAKeyValueType();
        dsaKeyValue.setP(Base64.encodeBytes(P).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setQ(Base64.encodeBytes(Q).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setG(Base64.encodeBytes(G).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setY(Base64.encodeBytes(Y).getBytes(GeneralConstants.SAML_CHARSET));
        return dsaKeyValue;
    }
    throw logger.unsupportedType(key.toString());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType) DSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.DSAKeyValueType) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 5 with RSAKeyValueType

use of org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType in project keycloak by keycloak.

the class SignatureUtil method createKeyValue.

/**
 * <p>
 * Creates a {@code KeyValueType} that wraps the specified public key. This method supports DSA and RSA keys.
 * </p>
 *
 * @param key the {@code PublicKey} that will be represented as a {@code KeyValueType}.
 *
 * @return the constructed {@code KeyValueType} or {@code null} if the specified key is neither a DSA nor a RSA
 *         key.
 */
public static KeyValueType createKeyValue(PublicKey key) {
    if (key instanceof RSAPublicKey) {
        RSAPublicKey pubKey = (RSAPublicKey) key;
        byte[] modulus = pubKey.getModulus().toByteArray();
        byte[] exponent = pubKey.getPublicExponent().toByteArray();
        RSAKeyValueType rsaKeyValue = new RSAKeyValueType();
        rsaKeyValue.setModulus(Base64.encodeBytes(modulus).getBytes(GeneralConstants.SAML_CHARSET));
        rsaKeyValue.setExponent(Base64.encodeBytes(exponent).getBytes(GeneralConstants.SAML_CHARSET));
        return rsaKeyValue;
    } else if (key instanceof DSAPublicKey) {
        DSAPublicKey pubKey = (DSAPublicKey) key;
        byte[] P = pubKey.getParams().getP().toByteArray();
        byte[] Q = pubKey.getParams().getQ().toByteArray();
        byte[] G = pubKey.getParams().getG().toByteArray();
        byte[] Y = pubKey.getY().toByteArray();
        DSAKeyValueType dsaKeyValue = new DSAKeyValueType();
        dsaKeyValue.setP(Base64.encodeBytes(P).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setQ(Base64.encodeBytes(Q).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setG(Base64.encodeBytes(G).getBytes(GeneralConstants.SAML_CHARSET));
        dsaKeyValue.setY(Base64.encodeBytes(Y).getBytes(GeneralConstants.SAML_CHARSET));
        return dsaKeyValue;
    }
    throw logger.unsupportedType(key.toString());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType) DSAKeyValueType(org.keycloak.dom.xmlsec.w3.xmldsig.DSAKeyValueType) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

RSAKeyValueType (org.keycloak.dom.xmlsec.w3.xmldsig.RSAKeyValueType)6 DSAKeyValueType (org.keycloak.dom.xmlsec.w3.xmldsig.DSAKeyValueType)3 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 Element (org.w3c.dom.Element)2 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 Test (org.junit.Test)1 AssertionType (org.keycloak.dom.saml.v2.assertion.AssertionType)1 AuthnContextType (org.keycloak.dom.saml.v2.assertion.AuthnContextType)1 AuthnStatementType (org.keycloak.dom.saml.v2.assertion.AuthnStatementType)1 EncryptedAssertionType (org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType)1 StatementAbstractType (org.keycloak.dom.saml.v2.assertion.StatementAbstractType)1 KeyInfoType (org.keycloak.dom.xmlsec.w3.xmldsig.KeyInfoType)1 KeyValueType (org.keycloak.dom.xmlsec.w3.xmldsig.KeyValueType)1 X509CertificateType (org.keycloak.dom.xmlsec.w3.xmldsig.X509CertificateType)1 X509DataType (org.keycloak.dom.xmlsec.w3.xmldsig.X509DataType)1 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)1 Node (org.w3c.dom.Node)1