001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
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.oauth2.sdk.jose.jwk;
019
020
021import java.security.Key;
022import java.security.KeyPair;
023import java.util.Collections;
024import java.util.LinkedList;
025import java.util.List;
026
027import com.nimbusds.jose.JOSEException;
028import com.nimbusds.jose.jwk.*;
029
030
031/**
032 * Key converter.
033 */
034@Deprecated
035public class KeyConverter {
036        
037
038        /**
039         * Converts the specified list of JSON Web Keys (JWK) their standard
040         * Java class representation. Asymmetric {@link RSAKey RSA} and
041         * {@link ECKey EC key} pairs are converted to
042         * {@link java.security.PublicKey} and {@link java.security.PrivateKey}
043         * (if specified) objects. {@link OctetSequenceKey secret JWKs} are
044         * converted to {@link javax.crypto.SecretKey} objects. Key conversion
045         * exceptions are silently ignored.
046         *
047         * @param jwkList The JWK list. May be {@code null}.
048         *
049         * @return The converted keys, empty set if none or {@code null}.
050         */
051        public static List<Key> toJavaKeys(final List<JWK> jwkList) {
052
053                if (jwkList == null) {
054                        return Collections.emptyList();
055                }
056
057                List<Key> out = new LinkedList<>();
058                for (JWK jwk: jwkList) {
059                        try {
060                                if (jwk instanceof AssymetricJWK) {
061                                        KeyPair keyPair = ((AssymetricJWK)jwk).toKeyPair();
062                                        out.add(keyPair.getPublic()); // add public
063                                        if (keyPair.getPrivate() != null) {
064                                                out.add(keyPair.getPrivate()); // add private if present
065                                        }
066                                } else if (jwk instanceof SecretJWK) {
067                                        out.add(((SecretJWK)jwk).toSecretKey());
068                                }
069                        } catch (JOSEException e) {
070                                // ignore and continue
071                        }
072                }
073                return out;
074        }
075}