Skip to content

Commit a3a2504

Browse files
authored
Merge pull request WrenSecurity#178 from WrenSecurity/fix-saml-extension-serialization
Fix serialization of SAML extensions
2 parents 27e74ad + 1b7656a commit a3a2504

File tree

4 files changed

+213
-178
lines changed

4 files changed

+213
-178
lines changed

openam-federation/openam-federation-library/src/main/java/com/sun/identity/saml2/meta/SAML2MetaUtils.java

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* $Id: SAML2MetaUtils.java,v 1.9 2009/09/21 17:28:12 exu Exp $
2626
*
2727
* Portions Copyrighted 2010-2015 ForgeRock AS.
28+
* Portions Copyrighted 2024 Wren Security.
2829
*/
2930
package com.sun.identity.saml2.meta;
3031

@@ -81,14 +82,12 @@ public final class SAML2MetaUtils {
8182
"com.sun.identity.saml2.jaxb.xmlsig:" +
8283
"com.sun.identity.saml2.jaxb.assertion:" +
8384
"com.sun.identity.saml2.jaxb.metadata:" +
84-
"com.sun.identity.saml2.jaxb.metadataattr:" +
85+
"com.sun.identity.saml2.jaxb.metadataattr:" +
8586
"com.sun.identity.saml2.jaxb.entityconfig:" +
8687
"com.sun.identity.saml2.jaxb.schema";
8788
private static final String JAXB_PACKAGE_LIST_PROP =
8889
"com.sun.identity.liberty.ws.jaxb.packageList";
8990
private static JAXBContext jaxbContext = null;
90-
private static final String PROP_JAXB_FORMATTED_OUTPUT =
91-
"jaxb.formatted.output";
9291
private static final String PROP_NAMESPACE_PREFIX_MAPPER =
9392
"com.sun.xml.bind.namespacePrefixMapper";
9493

@@ -171,18 +170,31 @@ public static Object convertNodeToJAXB(Node node)
171170
return u.unmarshal(node);
172171
}
173172

173+
/**
174+
* See {@link #convertJAXBToString(Object, boolean)}.
175+
*/
176+
public static String convertJAXBToString(Object jaxbObj) throws JAXBException {
177+
return convertJAXBToString(jaxbObj, true, false);
178+
}
179+
174180
/**
175181
* Converts a JAXB object to a <code>String</code> object.
176182
* @param jaxbObj a JAXB object
183+
* @param format flag indicating whether the output XML should be formatted.
184+
* @param fragment flag indicating whether the specified JAXB object is the fragment.
177185
* @return a <code>String</code> representing the JAXB object.
178186
* @exception JAXBException if an error occurs while converting JAXB object
179187
*/
180-
public static String convertJAXBToString(Object jaxbObj)
181-
throws JAXBException {
182-
188+
public static String convertJAXBToString(Object jaxbObj, boolean format, boolean fragment) throws JAXBException {
183189
StringWriter sw = new StringWriter();
184190
Marshaller marshaller = jaxbContext.createMarshaller();
185-
marshaller.setProperty(PROP_JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
191+
if (format) {
192+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
193+
}
194+
if (fragment) {
195+
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
196+
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
197+
}
186198
marshaller.setProperty(PROP_NAMESPACE_PREFIX_MAPPER, nsPrefixMapper);
187199
marshaller.marshal(jaxbObj, sw);
188200
return sw.toString();
@@ -194,12 +206,9 @@ public static String convertJAXBToString(Object jaxbObj)
194206
* @param os an <code>OutputStream</code> object
195207
* @exception JAXBException if an error occurs while converting JAXB object
196208
*/
197-
public static void convertJAXBToOutputStream(Object jaxbObj,
198-
OutputStream os)
199-
throws JAXBException {
200-
209+
public static void convertJAXBToOutputStream(Object jaxbObj, OutputStream os) throws JAXBException {
201210
Marshaller marshaller = jaxbContext.createMarshaller();
202-
marshaller.setProperty(PROP_JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
211+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
203212
marshaller.setProperty(PROP_NAMESPACE_PREFIX_MAPPER, nsPrefixMapper);
204213
marshaller.marshal(jaxbObj, os);
205214
}
@@ -573,36 +582,32 @@ public static IDPSSOConfigElement getIDPSSOConfig(
573582
return null;
574583
}
575584

576-
public static String exportStandardMeta(String realm, String entityID,
577-
boolean sign)
578-
throws SAML2MetaException {
579-
580-
try {
581-
SAML2MetaManager metaManager = new SAML2MetaManager();
582-
EntityDescriptorElement descriptor =
583-
metaManager.getEntityDescriptor(realm, entityID);
585+
public static String exportStandardMeta(String realm, String entityID, boolean sign) throws SAML2MetaException {
586+
try {
587+
SAML2MetaManager metaManager = new SAML2MetaManager();
588+
EntityDescriptorElement descriptor =
589+
metaManager.getEntityDescriptor(realm, entityID);
584590

585-
String xmlstr = null;
586-
if (descriptor == null) {
587-
return null;
588-
}
591+
String xmlstr = null;
592+
if (descriptor == null) {
593+
return null;
594+
}
589595

590-
if (sign) {
591-
Document doc = SAML2MetaSecurityUtils.sign(realm, descriptor);
592-
if (doc != null) {
596+
if (sign) {
597+
Document doc = SAML2MetaSecurityUtils.sign(realm, descriptor);
598+
if (doc != null) {
593599
xmlstr = XMLUtils.print(doc);
594-
}
600+
}
595601
}
596602
if (xmlstr == null) {
597-
xmlstr = convertJAXBToString(descriptor);
598-
xmlstr = SAML2MetaSecurityUtils.formatBase64BinaryElement(
599-
xmlstr);
603+
xmlstr = convertJAXBToString(descriptor);
604+
xmlstr = SAML2MetaSecurityUtils.formatBase64BinaryElement(xmlstr);
600605
}
601606
xmlstr = workaroundAbstractRoleDescriptor(xmlstr);
602607
return xmlstr;
603-
} catch (JAXBException e) {
608+
} catch (JAXBException e) {
604609
throw new SAML2MetaException(e.getMessage());
605-
}
610+
}
606611
}
607612

608613
/**
@@ -734,7 +739,7 @@ private static String importSAML2Entity(SAML2MetaManager metaManager, String rea
734739

735740
return result;
736741
}
737-
742+
738743
private static Object workaroundJAXBBug(Object obj) throws JAXBException {
739744

740745
String metadata = convertJAXBToString(obj);
@@ -787,28 +792,28 @@ private static void workaroundAbstractRoleDescriptor(Document doc) {
787792
}
788793

789794
private static String workaroundAbstractRoleDescriptor(String xmlstr) {
790-
int index =
791-
xmlstr.indexOf(":" +SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR);
792-
if (index == -1) {
793-
return xmlstr;
794-
}
795+
int index =
796+
xmlstr.indexOf(":" +SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR);
797+
if (index == -1) {
798+
return xmlstr;
799+
}
795800

796801
int index2 = xmlstr.lastIndexOf("<", index);
797-
if (index2 == -1) {
798-
return xmlstr;
799-
}
800-
801-
String prefix = xmlstr.substring(index2 + 1, index);
802-
String type = prefix + ":" +
803-
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR_TYPE;
804-
805-
xmlstr = xmlstr.replaceAll("<" + prefix + ":" +
806-
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR,
807-
"<" + SAML2MetaConstants.ROLE_DESCRIPTOR + " " +
808-
SAML2Constants.XSI_DECLARE_STR + " xsi:type=\"" + type + "\"");
809-
xmlstr = xmlstr.replaceAll("</" + prefix + ":" +
810-
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR,
811-
"</" + SAML2MetaConstants.ROLE_DESCRIPTOR);
812-
return xmlstr;
802+
if (index2 == -1) {
803+
return xmlstr;
804+
}
805+
806+
String prefix = xmlstr.substring(index2 + 1, index);
807+
String type = prefix + ":" +
808+
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR_TYPE;
809+
810+
xmlstr = xmlstr.replaceAll("<" + prefix + ":" +
811+
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR,
812+
"<" + SAML2MetaConstants.ROLE_DESCRIPTOR + " " +
813+
SAML2Constants.XSI_DECLARE_STR + " xsi:type=\"" + type + "\"");
814+
xmlstr = xmlstr.replaceAll("</" + prefix + ":" +
815+
SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR,
816+
"</" + SAML2MetaConstants.ROLE_DESCRIPTOR);
817+
return xmlstr;
813818
}
814819
}

openam-federation/openam-federation-library/src/main/java/com/sun/identity/saml2/protocol/Extensions.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
* $Id: Extensions.java,v 1.2 2008/06/25 05:47:56 qcheng Exp $
2626
*
2727
* Portions Copyrighted 2016 ForgeRock AS.
28+
* Portions Copyrighted 2024 Wren Security.
2829
*/
2930

30-
3131
package com.sun.identity.saml2.protocol;
3232

3333
import com.fasterxml.jackson.annotation.JsonTypeInfo;
3434
import com.sun.identity.saml2.common.SAML2Exception;
3535
import com.sun.identity.saml2.protocol.impl.ExtensionsImpl;
3636
import java.util.List;
3737

38-
/**
38+
/**
3939
* The interface <code>Extensions</code> defines methods for
4040
* adding protcol message extension elements.
4141
*
@@ -45,33 +45,33 @@
4545
@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS,
4646
defaultImpl = ExtensionsImpl.class)
4747
public interface Extensions {
48-
49-
/**
48+
49+
/**
5050
* Sets the <code>Extensions</code> object.
5151
*
5252
* @param value List of Document Elements <code>Extensions</code> objects
5353
* @throws SAML2Exception if the object is immutable.
5454
* @see #getAny
5555
*/
56-
public void setAny(List value) throws SAML2Exception;
57-
58-
/**
56+
public void setAny(List<Object> value) throws SAML2Exception;
57+
58+
/**
5959
* Returns the list of <code>Extensions</code> object.
6060
*
6161
* @return a List of Document Elements <code>Extensions</code> objects.
6262
* @see #setAny(List)
6363
*/
64-
public List getAny() ;
65-
66-
/**
64+
public List<Object> getAny() ;
65+
66+
/**
6767
* Returns a String representation of this object.
6868
*
6969
* @return a String representation of this object.
7070
* @throws SAML2Exception if cannot convert to String.
7171
*/
7272
public String toXMLString() throws SAML2Exception;
73-
74-
/**
73+
74+
/**
7575
* Returns a String representation of this object.
7676
*
7777
* @param includeNSPrefix determines whether or not the namespace
@@ -81,17 +81,17 @@ public interface Extensions {
8181
* @return the String representation of this Object.
8282
* @throws SAML2Exception if cannot convert to String.
8383
**/
84-
84+
8585
public String toXMLString(boolean includeNSPrefix,
8686
boolean declareNS) throws SAML2Exception;
87-
88-
/**
89-
* Makes this object immutable.
87+
88+
/**
89+
* Makes this object immutable.
9090
*
9191
*/
9292
public void makeImmutable() ;
93-
94-
/**
93+
94+
/**
9595
* Returns value true if object is mutable.
9696
*
9797
* @return true if object is mutable.

0 commit comments

Comments
 (0)