001package com.nimbusds.oauth2.sdk.jose.jwk;
002
003
004import java.security.Key;
005import java.security.KeyPair;
006import java.util.Collections;
007import java.util.LinkedList;
008import java.util.List;
009
010import com.nimbusds.jose.JOSEException;
011import com.nimbusds.jose.jwk.*;
012
013
014/**
015 * Key converter.
016 */
017@Deprecated
018public class KeyConverter {
019        
020
021        /**
022         * Converts the specified list of JSON Web Keys (JWK) their standard
023         * Java class representation. Asymmetric {@link RSAKey RSA} and
024         * {@link ECKey EC key} pairs are converted to
025         * {@link java.security.PublicKey} and {@link java.security.PrivateKey}
026         * (if specified) objects. {@link OctetSequenceKey secret JWKs} are
027         * converted to {@link javax.crypto.SecretKey} objects. Key conversion
028         * exceptions are silently ignored.
029         *
030         * @param jwkList The JWK list. May be {@code null}.
031         *
032         * @return The converted keys, empty set if none or {@code null}.
033         */
034        public static List<Key> toJavaKeys(final List<JWK> jwkList) {
035
036                if (jwkList == null) {
037                        return Collections.emptyList();
038                }
039
040                List<Key> out = new LinkedList<>();
041                for (JWK jwk: jwkList) {
042                        try {
043                                if (jwk instanceof AssymetricJWK) {
044                                        KeyPair keyPair = ((AssymetricJWK)jwk).toKeyPair();
045                                        out.add(keyPair.getPublic()); // add public
046                                        if (keyPair.getPrivate() != null) {
047                                                out.add(keyPair.getPrivate()); // add private if present
048                                        }
049                                } else if (jwk instanceof SecretJWK) {
050                                        out.add(((SecretJWK)jwk).toSecretKey());
051                                }
052                        } catch (JOSEException e) {
053                                // ignore and continue
054                        }
055                }
056                return out;
057        }
058}