001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: FSAssertionArtifact.java,v 1.3 2008/06/25 05:46:43 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.federation.message;
030
031import com.sun.identity.federation.common.IFSConstants;
032import com.sun.identity.federation.message.common.FSMsgException;
033import com.sun.identity.federation.common.FSUtils;
034
035import com.sun.identity.saml.protocol.AssertionArtifact;
036import com.sun.identity.saml.common.SAMLUtils;
037
038import com.sun.identity.shared.encode.Base64;
039
040/**
041 * This class represents the <code>AssertionArtifact</code> element in the
042 * <code>SAML</code> protocol schema. Current implementation supports
043 * TYPE 1 artifact only. Other type of artifact can be supported by
044 * extending this class.
045 *
046 * @supported.all.api
047 */
048public class FSAssertionArtifact extends AssertionArtifact {
049    
050    /**
051     * Default Artifact length
052     */
053    public final static int ARTIFACT_1_LENGTH = 42;
054    
055    /**
056     * Default Artifact Type Code 0 Constant
057     */
058    public final static byte ARTIFACT_1_TYPE_CODE_0 = 0;
059    
060    /**
061     * Default Artifact Type Code 1 Constant
062     */
063    public final static byte ARTIFACT_1_TYPE_CODE_1 = 3;
064    
065    /**
066     * Default Artifact Type Code Byte Array
067     */
068    public final static byte[] ARTIFACT_1_TYPE_CODE = {0, 3};
069    
070    /**
071     * Default Constructor.
072     */
073    protected FSAssertionArtifact() {
074    }
075    
076    /**
077     * Constructor to create <code>AssertionArtifact</code> object.
078     *
079     * @param theArtifact is the string that is generated by a provider.
080     * @throws SAMLException if there is an error decoding
081     *         the artifact string , the length of the artifact string
082     *         is incorrect , the <code>TYPE CODE</code> in the artifact
083     *         or other errors which prevent creation of
084     *         this object.
085     */
086    public FSAssertionArtifact(String theArtifact) throws FSMsgException {
087        // check if the input is empty
088        if ((theArtifact == null) || (theArtifact.length() == 0)) {
089            FSUtils.debug.message("FSAssertionArtifact: empty input.");
090            throw new FSMsgException("nullInput",null);
091        }
092        
093        // decode the artifact
094        byte raw[] = Base64.decode(theArtifact);
095        if(raw == null) {
096            if (FSUtils.debug.messageEnabled()) {
097                FSUtils.debug.message("FSAssertionArtifact: decode error");
098            }
099            throw new FSMsgException("wrongInput",null);
100        }
101        
102        // check if the length is 42bytes
103        if (raw.length != ARTIFACT_1_LENGTH) {
104            if (FSUtils.debug.messageEnabled()) {
105                FSUtils.debug.message("FSAssertionArtifact: the length is"
106                        + " not 42:" + raw.length);
107            }
108            throw new FSMsgException("wrongInput",null);
109        }
110        
111        // check if the typecode is correct
112        if ((raw[0] != ARTIFACT_1_TYPE_CODE_0) ||
113                (raw[1] != ARTIFACT_1_TYPE_CODE_1)) {
114            FSUtils.debug.message("FSAssertionArtifact: wrong typecode.");
115            throw new FSMsgException("wrongInput", null);
116        }
117        typeCode = ARTIFACT_1_TYPE_CODE;
118        
119        artifact = theArtifact;
120        
121        // get the sourceID and assertionHandle
122        byte sBytes[] = new byte[IFSConstants.ART_ID_LENGTH];
123        byte aBytes[] = new byte[IFSConstants.ART_ID_LENGTH];
124        System.arraycopy(raw, 2, sBytes, 0, IFSConstants.ART_ID_LENGTH);
125        System.arraycopy(raw, 22, aBytes, 0, IFSConstants.ART_ID_LENGTH);
126        
127        sourceID = SAMLUtils.byteArrayToString(sBytes);
128        assertionHandle = SAMLUtils.byteArrayToString(aBytes);
129    }
130    
131    /**
132     * Constructor to create <code>FSAssertionArtifact</code> object.
133     *
134     * @param idBytes the source identifier in the <code>Assertion</code>
135     * @param handleBytes the assertion identifier
136     * @throws SAMLException if wrong input or couldn't encode the artifact.
137     */
138    public FSAssertionArtifact(byte[] idBytes, byte[] handleBytes)
139    throws FSMsgException {
140        if ((idBytes == null) || (handleBytes == null)) {
141            FSUtils.debug.message("FSAssertionArtifact: null input.");
142            throw new FSMsgException("nullInput",null);
143        }
144        
145        if ((idBytes.length != IFSConstants.ART_ID_LENGTH) ||
146                (handleBytes.length != IFSConstants.ART_ID_LENGTH)) {
147            FSUtils.debug.message("FSAssertionArtifact: wrong input length.");
148            throw new FSMsgException("wrongInput",null);
149        }
150        sourceID = SAMLUtils.byteArrayToString(idBytes);
151        assertionHandle = SAMLUtils.byteArrayToString(handleBytes);
152        byte raw[] = new byte[42];
153        raw[0] = ARTIFACT_1_TYPE_CODE_0;
154        raw[1] = ARTIFACT_1_TYPE_CODE_1;
155        for (int i = 0; i < IFSConstants.ART_ID_LENGTH; i++) {
156            raw[2+i] = idBytes[i];
157            raw[22+i] = handleBytes[i];
158        }
159        try {
160            artifact = Base64.encode(raw).trim();
161        } catch (Exception e) {
162            if (FSUtils.debug.messageEnabled()) {
163                FSUtils.debug.message("FSAssertionArtifact: exception encode"
164                        + " input:", e);
165            }
166            throw new FSMsgException("errorCreateArtifact",null);
167        }
168        typeCode = ARTIFACT_1_TYPE_CODE;
169    }
170}