// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.jwt;

import java.security.GeneralSecurityException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Optional;
import com.google.crypto.tink.Parameters;
import com.google.crypto.tink.Key;
import com.google.crypto.tink.util.SecretBigInteger;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.errorprone.annotations.RestrictedApi;
import com.google.crypto.tink.signature.RsaSsaPkcs1PrivateKey;

public final class JwtRsaSsaPkcs1PrivateKey extends JwtSignaturePrivateKey
{
    private final JwtRsaSsaPkcs1PublicKey publicKey;
    private final RsaSsaPkcs1PrivateKey rsaSsaPkcs1PrivateKey;
    
    private JwtRsaSsaPkcs1PrivateKey(final JwtRsaSsaPkcs1PublicKey publicKey, final RsaSsaPkcs1PrivateKey rsaSsaPkcs1PrivateKey) {
        this.publicKey = publicKey;
        this.rsaSsaPkcs1PrivateKey = rsaSsaPkcs1PrivateKey;
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    public static Builder builder() {
        return new Builder();
    }
    
    @Override
    public JwtRsaSsaPkcs1Parameters getParameters() {
        return this.publicKey.getParameters();
    }
    
    @Override
    public JwtRsaSsaPkcs1PublicKey getPublicKey() {
        return this.publicKey;
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    @AccessesPartialKey
    public SecretBigInteger getPrimeP() {
        return this.rsaSsaPkcs1PrivateKey.getPrimeP();
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    @AccessesPartialKey
    public SecretBigInteger getPrimeQ() {
        return this.rsaSsaPkcs1PrivateKey.getPrimeQ();
    }
    
    @AccessesPartialKey
    public SecretBigInteger getPrivateExponent() {
        return this.rsaSsaPkcs1PrivateKey.getPrivateExponent();
    }
    
    @AccessesPartialKey
    public SecretBigInteger getPrimeExponentP() {
        return this.rsaSsaPkcs1PrivateKey.getPrimeExponentP();
    }
    
    @AccessesPartialKey
    public SecretBigInteger getPrimeExponentQ() {
        return this.rsaSsaPkcs1PrivateKey.getPrimeExponentQ();
    }
    
    @AccessesPartialKey
    public SecretBigInteger getCrtCoefficient() {
        return this.rsaSsaPkcs1PrivateKey.getCrtCoefficient();
    }
    
    @Override
    public boolean equalsKey(final Key o) {
        if (!(o instanceof JwtRsaSsaPkcs1PrivateKey)) {
            return false;
        }
        final JwtRsaSsaPkcs1PrivateKey that = (JwtRsaSsaPkcs1PrivateKey)o;
        return that.publicKey.equalsKey(this.publicKey) && that.rsaSsaPkcs1PrivateKey.equalsKey(this.rsaSsaPkcs1PrivateKey);
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    RsaSsaPkcs1PrivateKey getRsaSsaPkcs1PrivateKey() {
        return this.rsaSsaPkcs1PrivateKey;
    }
    
    public static class Builder
    {
        private Optional<JwtRsaSsaPkcs1PublicKey> publicKey;
        private Optional<SecretBigInteger> d;
        private Optional<SecretBigInteger> p;
        private Optional<SecretBigInteger> q;
        private Optional<SecretBigInteger> dP;
        private Optional<SecretBigInteger> dQ;
        private Optional<SecretBigInteger> qInv;
        
        private Builder() {
            this.publicKey = Optional.empty();
            this.d = Optional.empty();
            this.p = Optional.empty();
            this.q = Optional.empty();
            this.dP = Optional.empty();
            this.dQ = Optional.empty();
            this.qInv = Optional.empty();
        }
        
        @CanIgnoreReturnValue
        public Builder setPublicKey(final JwtRsaSsaPkcs1PublicKey publicKey) {
            this.publicKey = Optional.of(publicKey);
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setPrimes(final SecretBigInteger p, final SecretBigInteger q) {
            this.p = Optional.of(p);
            this.q = Optional.of(q);
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setPrivateExponent(final SecretBigInteger d) {
            this.d = Optional.of(d);
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setPrimeExponents(final SecretBigInteger dP, final SecretBigInteger dQ) {
            this.dP = Optional.of(dP);
            this.dQ = Optional.of(dQ);
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setCrtCoefficient(final SecretBigInteger qInv) {
            this.qInv = Optional.of(qInv);
            return this;
        }
        
        @AccessesPartialKey
        public JwtRsaSsaPkcs1PrivateKey build() throws GeneralSecurityException {
            if (!this.publicKey.isPresent()) {
                throw new GeneralSecurityException("Cannot build without a RSA SSA PKCS1 public key");
            }
            if (!this.p.isPresent() || !this.q.isPresent()) {
                throw new GeneralSecurityException("Cannot build without prime factors");
            }
            if (!this.d.isPresent()) {
                throw new GeneralSecurityException("Cannot build without private exponent");
            }
            if (!this.dP.isPresent() || !this.dQ.isPresent()) {
                throw new GeneralSecurityException("Cannot build without prime exponents");
            }
            if (!this.qInv.isPresent()) {
                throw new GeneralSecurityException("Cannot build without CRT coefficient");
            }
            final RsaSsaPkcs1PrivateKey rsaSsaPkcs1PrivateKey = RsaSsaPkcs1PrivateKey.builder().setPublicKey(this.publicKey.get().getRsaSsaPkcs1PublicKey()).setPrimes(this.p.get(), this.q.get()).setPrivateExponent(this.d.get()).setPrimeExponents(this.dP.get(), this.dQ.get()).setCrtCoefficient(this.qInv.get()).build();
            return new JwtRsaSsaPkcs1PrivateKey(this.publicKey.get(), rsaSsaPkcs1PrivateKey, null);
        }
    }
}
