com.sun.xml.wss.impl.c14n.MimeHeaderCanonicalizer Maven / Gradle / Ivy
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.wss.impl.c14n;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import java.util.Iterator;
import java.util.StringTokenizer;
import jakarta.xml.soap.MimeHeader;
import com.sun.xml.wss.swa.MimeConstants;
import com.sun.xml.wss.XWSSecurityException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import jakarta.mail.internet.MimeUtility;
public class MimeHeaderCanonicalizer extends Canonicalizer {
public MimeHeaderCanonicalizer() {}
@Override
public byte[] canonicalize(byte[] input) {
return null;
}
@Override
public InputStream canonicalize(InputStream input, OutputStream outputStream)
throws javax.xml.crypto.dsig.TransformException {
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")
public byte[] _canonicalize(Iterator mimeHeaders /* internal class, therefore ok */)
throws XWSSecurityException {
String _mh = "";
List mimeHeaderList = new ArrayList();
while(mimeHeaders.hasNext()){
mimeHeaderList.add(mimeHeaders.next());
}
// rf. steps 2-10 Section 4.3.1 SwA Draft 13
// SAAJ RI returns trimmed (at start) Content-Description values
// Unstructured MIME Headers can be RFC2047 encoded, step 6
// Unfold, step 5; Uncomment, step 8; Steps 9-16 not applicable
String cDescription = getMatchingHeader(mimeHeaderList, MimeConstants.CONTENT_DESCRIPTION);
if (cDescription != null) {
_mh += MimeConstants.CONTENT_DESCRIPTION + _CL;
_mh += uncomment(rfc2047decode(unfold(cDescription)));
_mh += _CRLF;
}
// Unfold, step 5; unfold WSP, step 7;
String cDisposition = getMatchingHeader(mimeHeaderList, MimeConstants.CONTENT_DISPOSITION);
if (cDisposition != null) {
_mh += MimeConstants.CONTENT_DISPOSITION + _CL;
_mh += canonicalizeHeaderLine(uncomment(unfold(cDisposition)), true);
_mh += _CRLF;
}
// Unfold, step 5; unfold WSP, step 7;
String cId = getMatchingHeader(mimeHeaderList, MimeConstants.CONTENT_ID);
if (cId != null) {
_mh += MimeConstants.CONTENT_ID + _CL;
_mh += unfoldWS(uncomment(unfold(cId))).trim();
_mh += _CRLF;
}
// Unfold, step 5; unfold WSP, step 7;
String cLocation = getMatchingHeader(mimeHeaderList, MimeConstants.CONTENT_LOCATION);
if (cLocation != null) {
_mh += MimeConstants.CONTENT_LOCATION + _CL;
_mh += unfoldWS(uncomment(unfold(cLocation))).trim();
_mh += _CRLF;
}
// Unfold, step 5; unfold WSP, step 7;
String cType = getMatchingHeader(mimeHeaderList, MimeConstants.CONTENT_TYPE);
cType = (cType == null) ? "text/plain; charset=us-ascii" : cType;
_mh += MimeConstants.CONTENT_TYPE + _CL;
_mh += canonicalizeHeaderLine(uncomment(unfold(cType)), true);
_mh += _CRLF;
_mh += _CRLF; // step 17
byte[] b = null;
try {
b = _mh.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
// log
throw new XWSSecurityException(e);
}
return b;
}
private String getMatchingHeader(List mimeHeaders, String key) throws XWSSecurityException {
String header_line = null;
try {
for (int i=0; i v = new Vector<>();
StringTokenizer nzr = new StringTokenizer(input, _SC);
while (nzr.hasMoreTokens()) {
v.add(nzr.nextToken());
}
return v;
}
private String _rfc2184decode(String input) throws Exception {
StringTokenizer nzr = new StringTokenizer(input, _SQ);
if (nzr.countTokens() != 3) {
// log
throw new XWSSecurityException("Malformed RFC2184 encoded parameter");
}
String charset = nzr.nextToken();
String language = nzr.nextToken();
//String encoded = nzr.nextToken();
for (int i=0; i