001/* 002 * Copyright 2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.revetsec.oauth.server; 018 019import com.google.errorprone.annotations.CheckReturnValue; 020import com.revetsec.jose.JwsAlgorithm; 021import com.revetsec.jose.JwsSigner; 022import org.jspecify.annotations.NonNull; 023import java.security.PrivateKey; 024import java.security.PublicKey; 025import javax.annotation.concurrent.Immutable; 026 027/** 028 * One application-owned RS256 signing key. Construction checks the public RSA structure without signing or 029 * encoding private material. Pair agreement is checked during bounded signing or explicit engine warm-up. 030 * The application must keep provider-backed private handles usable during in-flight operations and keep 031 * identifiers permanently bound to their public material across nodes and restarts. 032 * @author <a href="https://www.revetkn.com">Mark Allen</a> 033 * @since 1.0.0 034 */ 035@Immutable 036public final class OAuthIssuerSigningKey { 037 private final @NonNull String keyId; 038 private final @NonNull JwsSigner signer; 039 private OAuthIssuerSigningKey(@NonNull String keyId, @NonNull JwsSigner signer) { this.keyId=keyId; this.signer=signer; } 040 /** 041 * Checks one RS256 key pair structurally, without testing or encoding the private key. 042 * @param keyId nonempty identifier, at most 256 characters 043 * @param privateKey application-owned RSA private key or provider handle 044 * @param publicKey inspectable RSA public key, 2048–16384 bits under the existing exponent and ROCA policy 045 * @return an immutable redacted holder 046 * @throws NullPointerException if a required argument is null 047 * @throws IllegalArgumentException if supplied structure is invalid 048 * @since 1.0.0 049 */ 050 @CheckReturnValue 051 public static @NonNull OAuthIssuerSigningKey fromRsaKeyPair(@NonNull String keyId, @NonNull PrivateKey privateKey, 052 @NonNull PublicKey publicKey) { 053 return new OAuthIssuerSigningKey(OAuthServerConfiguration.text(keyId,256), 054 JwsSigner.fromRsaKeyPair(privateKey,publicKey,JwsAlgorithm.RS256)); 055 } 056 /** Returns the exact key identifier. 057 * @return the exact public identifier 058 * @since 1.0.0 059 */ 060 public @NonNull String getKeyId() { return this.keyId; } 061 /** Returns the checked public verification projection. 062 * @return the immutable checked public projection, without private material 063 * @since 1.0.0 064 */ 065 public @NonNull PublicKey getPublicKey() { return this.signer.getPublicKey(); } 066 @NonNull JwsSigner signer() { return this.signer; } 067 /** @return a description without identifiers or key material 068 * @since 1.0.0 069 */ 070 @Override public @NonNull String toString() { return "OAuthIssuerSigningKey{<redacted>}"; } 071}