001    /*
002     * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.11/src/java/org/apache/commons/ssl/PEMItem.java $
003     * $Revision: 121 $
004     * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 2007) $
005     *
006     * ====================================================================
007     * Licensed to the Apache Software Foundation (ASF) under one
008     * or more contributor license agreements.  See the NOTICE file
009     * distributed with this work for additional information
010     * regarding copyright ownership.  The ASF licenses this file
011     * to you under the Apache License, Version 2.0 (the
012     * "License"); you may not use this file except in compliance
013     * with the License.  You may obtain a copy of the License at
014     *
015     *   http://www.apache.org/licenses/LICENSE-2.0
016     *
017     * Unless required by applicable law or agreed to in writing,
018     * software distributed under the License is distributed on an
019     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020     * KIND, either express or implied.  See the License for the
021     * specific language governing permissions and limitations
022     * under the License.
023     * ====================================================================
024     *
025     * This software consists of voluntary contributions made by many
026     * individuals on behalf of the Apache Software Foundation.  For more
027     * information on the Apache Software Foundation, please see
028     * <http://www.apache.org/>.
029     *
030     */
031    
032    package org.apache.commons.ssl;
033    
034    import org.apache.commons.ssl.util.Hex;
035    
036    import java.util.Collections;
037    import java.util.Map;
038    import java.util.StringTokenizer;
039    import java.util.TreeMap;
040    
041    /**
042     * @author Credit Union Central of British Columbia
043     * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
044     * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
045     * @since 13-Aug-2006
046     */
047    public class PEMItem {
048        public final static String DEK_INFO = "dek-info";
049    
050        private final byte[] derBytes;
051        public final String pemType;
052        public final Map properties;
053    
054        public final String dekInfo;
055        public final byte[] iv;
056        public final String cipher;
057        public final boolean des2;
058        public final String mode;
059        public final int keySizeInBits;
060    
061        public PEMItem(byte[] derBytes, String type) {
062            this(derBytes, type, null);
063        }
064    
065        public PEMItem(byte[] derBytes, String type, Map properties) {
066            this.derBytes = derBytes;
067            this.pemType = type;
068            if (properties == null) {
069                properties = new TreeMap(); // empty map
070            }
071            this.properties = Collections.unmodifiableMap(properties);
072            String di = (String) properties.get(DEK_INFO);
073            String diCipher = "";
074            String diIV = "";
075            if (di != null) {
076                StringTokenizer st = new StringTokenizer(di, ",");
077                if (st.hasMoreTokens()) {
078                    diCipher = st.nextToken().trim().toLowerCase();
079                }
080                if (st.hasMoreTokens()) {
081                    diIV = st.nextToken().trim().toLowerCase();
082                }
083            }
084            this.dekInfo = diCipher;
085            this.iv = Hex.decode(diIV);
086            if (!"".equals(diCipher)) {
087                OpenSSL.CipherInfo cipherInfo = OpenSSL.lookup(diCipher);
088                this.cipher = cipherInfo.javaCipher;
089                this.mode = cipherInfo.blockMode;
090                this.keySizeInBits = cipherInfo.keySize;
091                this.des2 = cipherInfo.des2;
092            } else {
093                this.mode = "";
094                cipher = "UNKNOWN";
095                keySizeInBits = -1;
096                des2 = false;
097            }
098        }
099    
100        public byte[] getDerBytes() {
101            byte[] b = new byte[derBytes.length];
102            System.arraycopy(derBytes, 0, b, 0, derBytes.length);
103            return b;
104        }
105    
106    }