001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.text.ParseException;
022import java.util.LinkedList;
023import java.util.List;
024
025import com.nimbusds.jose.util.Base64;
026import net.minidev.json.JSONArray;
027
028
029/**
030 * X.509 certificate chain utilities.
031 *
032 * @author Vladimir Dzhuvinov
033 * @version 2013-05-29
034 */
035public class X509CertChainUtils {
036
037        /**
038         * Parses an X.509 certificate chain from the specified JSON array.
039         *
040         * @param jsonArray The JSON array to parse. Must not be {@code null}.
041         *
042         * @return The X.509 certificate chain.
043         *
044         * @throws ParseException If the X.509 certificate chain couldn't be
045         *                        parsed.
046         */
047        public static List<Base64> parseX509CertChain(final JSONArray jsonArray)
048                throws ParseException {
049
050                List<Base64> chain = new LinkedList<>();
051
052                for (int i=0; i < jsonArray.size(); i++) {
053
054                        Object item = jsonArray.get(i);
055
056                        if (item == null) {
057                                throw new ParseException("The X.509 certificate at position " + i + " must not be null", 0);
058                        }
059
060                        if  (! (item instanceof String)) {
061                                throw new ParseException("The X.509 certificate at position " + i + " must be encoded as a Base64 string", 0);
062                        }
063
064                        chain.add(new Base64((String)item));
065                }
066
067                return chain;
068        }
069
070        /**
071         * Prevents public instantiation.
072         */
073        private X509CertChainUtils() {}
074}