All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsonwebtoken.impl.security.AbstractAsymmetricJwkBuilder Maven / Gradle / Ivy

/*
 * Copyright (C) 2021 jsonwebtoken.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jsonwebtoken.impl.security;

import io.jsonwebtoken.impl.ParameterMap;
import io.jsonwebtoken.lang.Assert;
import io.jsonwebtoken.security.AsymmetricJwk;
import io.jsonwebtoken.security.AsymmetricJwkBuilder;
import io.jsonwebtoken.security.EcPrivateJwk;
import io.jsonwebtoken.security.EcPrivateJwkBuilder;
import io.jsonwebtoken.security.EcPublicJwk;
import io.jsonwebtoken.security.EcPublicJwkBuilder;
import io.jsonwebtoken.security.MalformedKeyException;
import io.jsonwebtoken.security.OctetPrivateJwk;
import io.jsonwebtoken.security.OctetPrivateJwkBuilder;
import io.jsonwebtoken.security.OctetPublicJwk;
import io.jsonwebtoken.security.OctetPublicJwkBuilder;
import io.jsonwebtoken.security.PrivateJwk;
import io.jsonwebtoken.security.PrivateJwkBuilder;
import io.jsonwebtoken.security.PublicJwk;
import io.jsonwebtoken.security.PublicJwkBuilder;
import io.jsonwebtoken.security.RsaPrivateJwk;
import io.jsonwebtoken.security.RsaPrivateJwkBuilder;
import io.jsonwebtoken.security.RsaPublicJwk;
import io.jsonwebtoken.security.RsaPublicJwkBuilder;

import java.net.URI;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.List;

abstract class AbstractAsymmetricJwkBuilder, T extends AsymmetricJwkBuilder>
        extends AbstractJwkBuilder implements AsymmetricJwkBuilder {

    protected Boolean applyX509KeyUse = null;
    private KeyUseStrategy keyUseStrategy = DefaultKeyUseStrategy.INSTANCE;

    private final X509BuilderSupport x509;

    public AbstractAsymmetricJwkBuilder(JwkContext ctx) {
        super(ctx);
        ParameterMap map = Assert.isInstanceOf(ParameterMap.class, this.DELEGATE);
        this.x509 = new X509BuilderSupport(map, MalformedKeyException.class);
    }

    AbstractAsymmetricJwkBuilder(AbstractAsymmetricJwkBuilder b, JwkContext ctx) {
        this(ctx);
        this.applyX509KeyUse = b.applyX509KeyUse;
        this.keyUseStrategy = b.keyUseStrategy;
    }

    @Override
    public T publicKeyUse(String use) {
        Assert.hasText(use, "publicKeyUse cannot be null or empty.");
        this.DELEGATE.setPublicKeyUse(use);
        return self();
    }

    /*
    public T setKeyUseStrategy(KeyUseStrategy strategy) {
        this.keyUseStrategy = Assert.notNull(strategy, "KeyUseStrategy cannot be null.");
        return tthis();
    }
     */

    @Override
    public T x509Chain(List chain) {
        Assert.notEmpty(chain, "X509Certificate chain cannot be null or empty.");
        this.x509.x509Chain(chain);
        return self();
    }

    @Override
    public T x509Url(URI uri) {
        Assert.notNull(uri, "X509Url cannot be null.");
        this.x509.x509Url(uri);
        return self();
    }

    /*
    @Override
    public T withX509KeyUse(boolean enable) {
        this.applyX509KeyUse = enable;
        return tthis();
    }
     */

    @Override
    public T x509Sha1Thumbprint(byte[] thumbprint) {
        this.x509.x509Sha1Thumbprint(thumbprint);
        return self();
    }

    @Override
    public T x509Sha256Thumbprint(byte[] thumbprint) {
        this.x509.x509Sha256Thumbprint(thumbprint);
        return self();
    }

    @Override
    public T x509Sha1Thumbprint(boolean enable) {
        this.x509.x509Sha1Thumbprint(enable);
        return self();
    }

    @Override
    public T x509Sha256Thumbprint(boolean enable) {
        this.x509.x509Sha256Thumbprint(enable);
        return self();
    }

    @Override
    public J build() {
        this.x509.apply();
        return super.build();
    }

    private abstract static class DefaultPublicJwkBuilder, M extends PrivateJwk, P extends PrivateJwkBuilder,
            T extends PublicJwkBuilder>
            extends AbstractAsymmetricJwkBuilder
            implements PublicJwkBuilder {

        DefaultPublicJwkBuilder(JwkContext ctx) {
            super(ctx);
        }

        @Override
        public P privateKey(L privateKey) {
            Assert.notNull(privateKey, "PrivateKey argument cannot be null.");
            final K publicKey = Assert.notNull(DELEGATE.getKey(), "PublicKey cannot be null.");
            return newPrivateBuilder(newContext(privateKey)).publicKey(publicKey);
        }

        protected abstract P newPrivateBuilder(JwkContext ctx);
    }

    private abstract static class DefaultPrivateJwkBuilder, M extends PrivateJwk,
            T extends PrivateJwkBuilder>
            extends AbstractAsymmetricJwkBuilder
            implements PrivateJwkBuilder {

        DefaultPrivateJwkBuilder(JwkContext ctx) {
            super(ctx);
        }

        DefaultPrivateJwkBuilder(DefaultPublicJwkBuilder b, JwkContext ctx) {
            super(b, ctx);
            this.DELEGATE.setPublicKey(b.DELEGATE.getKey());
        }

        @Override
        public T publicKey(L publicKey) {
            this.DELEGATE.setPublicKey(publicKey);
            return self();
        }
    }

    static class DefaultRsaPublicJwkBuilder
            extends DefaultPublicJwkBuilder
            implements RsaPublicJwkBuilder {

        DefaultRsaPublicJwkBuilder(JwkContext ctx) {
            super(ctx);
        }

        @Override
        protected RsaPrivateJwkBuilder newPrivateBuilder(JwkContext ctx) {
            return new DefaultRsaPrivateJwkBuilder(this, ctx);
        }
    }

    static class DefaultEcPublicJwkBuilder
            extends DefaultPublicJwkBuilder
            implements EcPublicJwkBuilder {
        DefaultEcPublicJwkBuilder(JwkContext src) {
            super(src);
        }

        @Override
        protected EcPrivateJwkBuilder newPrivateBuilder(JwkContext ctx) {
            return new DefaultEcPrivateJwkBuilder(this, ctx);
        }
    }

    static class DefaultOctetPublicJwkBuilder
            extends DefaultPublicJwkBuilder, OctetPrivateJwk,
            OctetPrivateJwkBuilder, OctetPublicJwkBuilder>
            implements OctetPublicJwkBuilder {
        DefaultOctetPublicJwkBuilder(JwkContext ctx) {
            super(ctx);
            EdwardsCurve.assertEdwards(ctx.getKey());
        }

        @Override
        protected OctetPrivateJwkBuilder newPrivateBuilder(JwkContext ctx) {
            return new DefaultOctetPrivateJwkBuilder<>(this, ctx);
        }
    }

    static class DefaultRsaPrivateJwkBuilder
            extends DefaultPrivateJwkBuilder
            implements RsaPrivateJwkBuilder {
        DefaultRsaPrivateJwkBuilder(JwkContext src) {
            super(src);
        }

        DefaultRsaPrivateJwkBuilder(DefaultRsaPublicJwkBuilder b, JwkContext ctx) {
            super(b, ctx);
        }
    }

    static class DefaultEcPrivateJwkBuilder
            extends DefaultPrivateJwkBuilder
            implements EcPrivateJwkBuilder {
        DefaultEcPrivateJwkBuilder(JwkContext src) {
            super(src);
        }

        DefaultEcPrivateJwkBuilder(DefaultEcPublicJwkBuilder b, JwkContext ctx) {
            super(b, ctx);
        }
    }

    static class DefaultOctetPrivateJwkBuilder
            extends DefaultPrivateJwkBuilder, OctetPrivateJwk,
            OctetPrivateJwkBuilder> implements OctetPrivateJwkBuilder {
        DefaultOctetPrivateJwkBuilder(JwkContext src) {
            super(src);
            EdwardsCurve.assertEdwards(src.getKey());
        }

        DefaultOctetPrivateJwkBuilder(DefaultOctetPublicJwkBuilder b, JwkContext ctx) {
            super(b, ctx);
            EdwardsCurve.assertEdwards(ctx.getKey());
            EdwardsCurve.assertEdwards(ctx.getPublicKey());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy