MD5 校验值:195005882709ac21163d7a1b97aeec73
JsonWebSignature.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package org.jose4j.jws; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.Key; import org.jose4j.jwa.AlgorithmConstraints; import org.jose4j.jwa.AlgorithmFactoryFactory; import org.jose4j.jwx.CompactSerializer; import org.jose4j.jwx.HeaderParameterNames; import org.jose4j.jwx.JsonWebStructure; import org.jose4j.keys.KeyPersuasion; import org.jose4j.lang.IntegrityException; import org.jose4j.lang.InvalidAlgorithmException; import org.jose4j.lang.JoseException; import org.jose4j.lang.StringUtil; public class JsonWebSignature extends JsonWebStructure { public static final short COMPACT_SERIALIZATION_PARTS = 3; private String encodedPayload; private byte[] payloadBytes; private String payloadCharEncoding = "UTF-8"; private Boolean validSignature; public JsonWebSignature() { if (Boolean.getBoolean("org.jose4j.jws.default-allow-none")) { return; } setAlgorithmConstraints(AlgorithmConstraints.DISALLOW_NONE); } private JsonWebSignatureAlgorithm getAlgorithm(boolean z) throws InvalidAlgorithmException { String algorithmHeaderValue = getAlgorithmHeaderValue(); if (algorithmHeaderValue == null) { throw new InvalidAlgorithmException("Signature algorithm header (alg) not set."); } if (z) { getAlgorithmConstraints().checkConstraint(algorithmHeaderValue); } return AlgorithmFactoryFactory.getInstance().getJwsAlgorithmFactory().getAlgorithm(algorithmHeaderValue); } private byte[] getSigningInputBytes() throws JoseException { if (!isRfc7797UnencodedPayload()) { return StringUtil.getBytesAscii(CompactSerializer.serialize(getEncodedHeader(), getEncodedPayload())); } try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.write(StringUtil.getBytesAscii(getEncodedHeader())); byteArrayOutputStream.write(46); byteArrayOutputStream.write(this.payloadBytes); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new JoseException("This should never happen from a ByteArrayOutputStream", e); } } private String getStringPayload() { return StringUtil.newString(this.payloadBytes, this.payloadCharEncoding); } @Override public JsonWebSignatureAlgorithm getAlgorithm() throws InvalidAlgorithmException { return getAlgorithm(true); } @Override public JsonWebSignatureAlgorithm getAlgorithmNoConstraintCheck() throws InvalidAlgorithmException { return getAlgorithm(false); } @Override public String getCompactSerialization() throws JoseException { String encodedPayload; sign(); if (isRfc7797UnencodedPayload()) { encodedPayload = getStringPayload(); if (encodedPayload.contains(".")) { throw new JoseException("per https://tools.ietf.org/html/rfc7797#section-5.2 when using the JWS Compact Serialization, unencoded non-detached payloads using period ('.') characters would cause parsing errors; such payloads MUST NOT be used with the JWS Compact Serialization."); } } else { encodedPayload = getEncodedPayload(); } return CompactSerializer.serialize(getEncodedHeader(), encodedPayload, getEncodedSignature()); } public String getDetachedContentCompactSerialization() throws JoseException { sign(); return CompactSerializer.serialize(getEncodedHeader(), "", getEncodedSignature()); } public String getEncodedPayload() { return this.encodedPayload != null ? this.encodedPayload : this.base64url.base64UrlEncode(this.payloadBytes); } public String getEncodedSignature() { return this.base64url.base64UrlEncode(getSignature()); } public KeyPersuasion getKeyPersuasion() throws InvalidAlgorithmException { return getAlgorithmNoConstraintCheck().getKeyPersuasion(); } public String getKeyType() throws InvalidAlgorithmException { return getAlgorithmNoConstraintCheck().getKeyType(); } @Override public String getPayload() throws JoseException { if (Boolean.getBoolean("org.jose4j.jws.getPayload-skip-verify") || verifySignature()) { return getStringPayload(); } throw new IntegrityException("JWS signature is invalid."); } public byte[] getPayloadBytes() throws JoseException { if (verifySignature()) { return this.payloadBytes; } throw new IntegrityException("JWS signature is invalid."); } public String getPayloadCharEncoding() { return this.payloadCharEncoding; } protected byte[] getSignature() { return getIntegrity(); } public String getUnverifiedPayload() { return getStringPayload(); } public byte[] getUnverifiedPayloadBytes() { return this.payloadBytes; } protected boolean isRfc7797UnencodedPayload() { Object objectHeaderValue = this.headers.getObjectHeaderValue(HeaderParameterNames.BASE64URL_ENCODE_PAYLOAD); return (objectHeaderValue == null || !(objectHeaderValue instanceof Boolean) || ((Boolean) objectHeaderValue).booleanValue()) ? false : true; } @Override protected boolean isSupportedCriticalHeader(String str) { return HeaderParameterNames.BASE64URL_ENCODE_PAYLOAD.equals(str); } @Override protected void onNewKey() { this.validSignature = null; } @Override protected void setCompactSerializationParts(String[] strArr) throws JoseException { if (strArr.length != 3) { throw new JoseException("A JWS Compact Serialization must have exactly 3 parts separated by period ('.') characters"); } setEncodedHeader(strArr[0]); if (isRfc7797UnencodedPayload()) { setPayload(strArr[1]); } else { setEncodedPayload(strArr[1]); } setSignature(this.base64url.base64UrlDecode(strArr[2])); } public void setEncodedPayload(String str) { this.encodedPayload = str; this.payloadBytes = this.base64url.base64UrlDecode(str); } @Override public void setPayload(String str) { this.payloadBytes = StringUtil.getBytesUnchecked(str, this.payloadCharEncoding); } public void setPayloadBytes(byte[] bArr) { this.payloadBytes = bArr; } public void setPayloadCharEncoding(String str) { this.payloadCharEncoding = str; } protected void setSignature(byte[] bArr) { setIntegrity(bArr); } public void sign() throws JoseException { JsonWebSignatureAlgorithm algorithm = getAlgorithm(); Key key = getKey(); if (isDoKeyValidation()) { algorithm.validateSigningKey(key); } setSignature(algorithm.sign(key, getSigningInputBytes(), getProviderCtx())); } public boolean verifySignature() throws JoseException { JsonWebSignatureAlgorithm algorithm = getAlgorithm(); Key key = getKey(); if (isDoKeyValidation()) { algorithm.validateVerificationKey(key); } if (this.validSignature == null) { checkCrit(); this.validSignature = Boolean.valueOf(algorithm.verifySignature(getSignature(), key, getSigningInputBytes(), getProviderCtx())); } return this.validSignature.booleanValue(); } }