Search code examples
javacxfws-security

Apache cxf attachment security


I try to get my apache cxf client to sign and encrypt attachments. As i have my solution now it does sign and encrypt message body, but it ignores attachments.

I have following code:

    Map<String, Object> props = new HashMap<>();
    props.put("action", "Signature Encrypt");
    props.put("signaturePropFile", "client.properties");
    props.put("passwordCallbackClass", "******.KeystorePasswordCallback");
    props.put("user", "node1");
    props.put("signatureKeyIdentifier", "DirectReference");
    props.put("signatureParts",
            "{Element}{http://www.w3.org/2003/05/soap-envelope}Body;" +
                    "{}cid:Attachments;");
    props.put("encryptionParts",
            "{Content}{http://www.w3.org/2003/05/soap-envelope}Body;" +
                    "{Element}cid:Attachments;" );
    props.put("encryptionPropFile", "client.properties");
    props.put("encryptionKeyIdentifier", "IssuerSerial");
    props.put("encryptionKeyTransportAlgorithm",
            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");

    WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor(props);

    client.getOutInterceptors().add(wss4jOut);

I'm following this example to make my code.

And {}cid:Attachments part is from this apache page.


Solution

  • The problem was that Apache CXF for some reason runs sign/enrypt interceptor before interceptor that adds attachments to message.

    Simple workaround is to add your own WSS4J out/in interceptor ( the problem is in both ways - incoming/outgoing messages ) that adds attachments before encryption/decryption/signature(check) is done and. Basically you can open SAAJ interceptor that adds attachments and copy paste part of code from handleMessage method to your interceptor. For out incerceptor:

       @Override
        public void handleMessage(SoapMessage mc) throws Fault {
            super.handleMessage(mc);
            SOAPMessage soapMessage = mc.getContent(SOAPMessage.class);
            if (soapMessage != null) {
                if (soapMessage.countAttachments() > 0) {
                    if (mc.getAttachments() == null) {
                        mc.setAttachments(new ArrayList<Attachment>(soapMessage
                                .countAttachments()));
                    }
                    Iterator<AttachmentPart> it = CastUtils.cast(soapMessage.getAttachments());
                    while (it.hasNext()) {
                        AttachmentPart part = it.next();
                        String id = AttachmentUtil.cleanContentId(part.getContentId());
                        AttachmentImpl att = new AttachmentImpl(id);
                        try {
                            att.setDataHandler(part.getDataHandler());
                        } catch (SOAPException e) {
                            throw new Fault(e);
                        }
                        Iterator<MimeHeader> it2 = CastUtils.cast(part.getAllMimeHeaders());
                        while (it2.hasNext()) {
                            MimeHeader header = it2.next();
                            att.setHeader(header.getName(), header.getValue());
                        }
                        mc.getAttachments().add(att);
                        it.remove();
                    }
                }
            }
        }
    

    For in interceptor:

     @Override
        public void handleMessage(SoapMessage msg) throws Fault {
            super.handleMessage(msg);
            SOAPMessage soapMessage = msg.getContent(SOAPMessage.class);
            soapMessage.removeAllAttachments();
            Collection<Attachment> atts = msg.getAttachments();
            if (atts != null) {
                for (Attachment a : atts) {
                    if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                        try {
                            ((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(msg);
                        } catch (IOException e) {
                            throw new Fault(e);
                        }
                    }
                    AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
                    Iterator<String> i = a.getHeaderNames();
                    while (i != null && i.hasNext()) {
                        String h = i.next();
                        String val = a.getHeader(h);
                        ap.addMimeHeader(h, val);
                    }
                    if (StringUtils.isEmpty(ap.getContentId())) {
                        ap.setContentId(a.getId());
                    }
                    soapMessage.addAttachmentPart(ap);
                }
            }
            msg.setAttachments(Collections.<Attachment>emptyList());
            msg.setContent(SOAPMessage.class, soapMessage);
        }