001package com.nimbusds.jose.util;
002
003
004import java.text.ParseException;
005import java.util.LinkedList;
006import java.util.List;
007
008import com.nimbusds.jose.util.Base64;
009import net.minidev.json.JSONArray;
010
011
012/**
013 * X.509 certificate chain utilities.
014 *
015 * @author Vladimir Dzhuvinov
016 * @version 2013-05-29
017 */
018public class X509CertChainUtils {
019
020        /**
021         * Parses an X.509 certificate chain from the specified JSON array.
022         *
023         * @param jsonArray The JSON array to parse. Must not be {@code null}.
024         *
025         * @return The X.509 certificate chain.
026         *
027         * @throws ParseException If the X.509 certificate chain couldn't be
028         *                        parsed.
029         */
030        public static List<Base64> parseX509CertChain(final JSONArray jsonArray)
031                throws ParseException {
032
033                List<Base64> chain = new LinkedList<>();
034
035                for (int i=0; i < jsonArray.size(); i++) {
036
037                        Object item = jsonArray.get(i);
038
039                        if (item == null) {
040                                throw new ParseException("The X.509 certificate at position " + i + " must not be null", 0);
041                        }
042
043                        if  (! (item instanceof String)) {
044                                throw new ParseException("The X.509 certificate at position " + i + " must be encoded as a Base64 string", 0);
045                        }
046
047                        chain.add(new Base64((String)item));
048                }
049
050                return chain;
051        }
052
053        /**
054         * Prevents public instantiation.
055         */
056        private X509CertChainUtils() {}
057}