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