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 * Portions Copyrighted 2014 ForgeRock AS
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 * @deprecated since 12.0.0
048 */
049@Deprecated
050public class FSAssertionArtifact extends AssertionArtifact {
051    
052    /**
053     * Default Artifact length
054     */
055    public final static int ARTIFACT_1_LENGTH = 42;
056    
057    /**
058     * Default Artifact Type Code 0 Constant
059     */
060    public final static byte ARTIFACT_1_TYPE_CODE_0 = 0;
061    
062    /**
063     * Default Artifact Type Code 1 Constant
064     */
065    public final static byte ARTIFACT_1_TYPE_CODE_1 = 3;
066    
067    /**
068     * Default Artifact Type Code Byte Array
069     */
070    public final static byte[] ARTIFACT_1_TYPE_CODE = {0, 3};
071    
072    /**
073     * Default Constructor.
074     */
075    protected FSAssertionArtifact() {
076    }
077    
078    /**
079     * Constructor to create <code>AssertionArtifact</code> object.
080     *
081     * @param theArtifact is the string that is generated by a provider.
082     * @throws SAMLException if there is an error decoding
083     *         the artifact string , the length of the artifact string
084     *         is incorrect , the <code>TYPE CODE</code> in the artifact
085     *         or other errors which prevent creation of
086     *         this object.
087     */
088    public FSAssertionArtifact(String theArtifact) throws FSMsgException {
089        // check if the input is empty
090        if ((theArtifact == null) || (theArtifact.length() == 0)) {
091            FSUtils.debug.message("FSAssertionArtifact: empty input.");
092            throw new FSMsgException("nullInput",null);
093        }
094        
095        // decode the artifact
096        byte raw[] = Base64.decode(theArtifact);
097        if(raw == null) {
098            if (FSUtils.debug.messageEnabled()) {
099                FSUtils.debug.message("FSAssertionArtifact: decode error");
100            }
101            throw new FSMsgException("wrongInput",null);
102        }
103        
104        // check if the length is 42bytes
105        if (raw.length != ARTIFACT_1_LENGTH) {
106            if (FSUtils.debug.messageEnabled()) {
107                FSUtils.debug.message("FSAssertionArtifact: the length is"
108                        + " not 42:" + raw.length);
109            }
110            throw new FSMsgException("wrongInput",null);
111        }
112        
113        // check if the typecode is correct
114        if ((raw[0] != ARTIFACT_1_TYPE_CODE_0) ||
115                (raw[1] != ARTIFACT_1_TYPE_CODE_1)) {
116            FSUtils.debug.message("FSAssertionArtifact: wrong typecode.");
117            throw new FSMsgException("wrongInput", null);
118        }
119        typeCode = ARTIFACT_1_TYPE_CODE;
120        
121        artifact = theArtifact;
122        
123        // get the sourceID and assertionHandle
124        byte sBytes[] = new byte[IFSConstants.ART_ID_LENGTH];
125        byte aBytes[] = new byte[IFSConstants.ART_ID_LENGTH];
126        System.arraycopy(raw, 2, sBytes, 0, IFSConstants.ART_ID_LENGTH);
127        System.arraycopy(raw, 22, aBytes, 0, IFSConstants.ART_ID_LENGTH);
128        
129        sourceID = SAMLUtils.byteArrayToString(sBytes);
130        assertionHandle = SAMLUtils.byteArrayToString(aBytes);
131    }
132    
133    /**
134     * Constructor to create <code>FSAssertionArtifact</code> object.
135     *
136     * @param idBytes the source identifier in the <code>Assertion</code>
137     * @param handleBytes the assertion identifier
138     * @throws SAMLException if wrong input or couldn't encode the artifact.
139     */
140    public FSAssertionArtifact(byte[] idBytes, byte[] handleBytes)
141    throws FSMsgException {
142        if ((idBytes == null) || (handleBytes == null)) {
143            FSUtils.debug.message("FSAssertionArtifact: null input.");
144            throw new FSMsgException("nullInput",null);
145        }
146        
147        if ((idBytes.length != IFSConstants.ART_ID_LENGTH) ||
148                (handleBytes.length != IFSConstants.ART_ID_LENGTH)) {
149            FSUtils.debug.message("FSAssertionArtifact: wrong input length.");
150            throw new FSMsgException("wrongInput",null);
151        }
152        sourceID = SAMLUtils.byteArrayToString(idBytes);
153        assertionHandle = SAMLUtils.byteArrayToString(handleBytes);
154        byte raw[] = new byte[42];
155        raw[0] = ARTIFACT_1_TYPE_CODE_0;
156        raw[1] = ARTIFACT_1_TYPE_CODE_1;
157        for (int i = 0; i < IFSConstants.ART_ID_LENGTH; i++) {
158            raw[2+i] = idBytes[i];
159            raw[22+i] = handleBytes[i];
160        }
161        try {
162            artifact = Base64.encode(raw).trim();
163        } catch (Exception e) {
164            if (FSUtils.debug.messageEnabled()) {
165                FSUtils.debug.message("FSAssertionArtifact: exception encode"
166                        + " input:", e);
167            }
168            throw new FSMsgException("errorCreateArtifact",null);
169        }
170        typeCode = ARTIFACT_1_TYPE_CODE;
171    }
172}