All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.wss.impl.c14n.MimeHeaderCanonicalizer Maven / Gradle / Ivy

There is a newer version: 4.0.4
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010-2017 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
 * or LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

/*
 * $Id: MimeHeaderCanonicalizer.java,v 1.2 2010-10-21 15:37:19 snajper Exp $
 */

package com.sun.xml.wss.impl.c14n;

import java.util.Vector;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.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 javax.mail.internet.MimeUtility;

public class MimeHeaderCanonicalizer extends Canonicalizer {
    
    public MimeHeaderCanonicalizer() {}
    
    public byte[] canonicalize(byte[] input) throws XWSSecurityException {
        return null;
    }
    
    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("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