|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.provider.service.authentication.logout; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Base64; |
| 24 | +import java.util.zip.Deflater; |
| 25 | +import java.util.zip.DeflaterOutputStream; |
| 26 | +import java.util.zip.Inflater; |
| 27 | +import java.util.zip.InflaterOutputStream; |
| 28 | + |
| 29 | +import org.springframework.security.saml2.Saml2Exception; |
| 30 | + |
| 31 | +/** |
| 32 | + * Utility methods for working with serialized SAML messages. |
| 33 | + * |
| 34 | + * For internal use only. |
| 35 | + * |
| 36 | + * @author Josh Cummings |
| 37 | + */ |
| 38 | +final class Saml2Utils { |
| 39 | + |
| 40 | + private Saml2Utils() { |
| 41 | + } |
| 42 | + |
| 43 | + static String samlEncode(byte[] b) { |
| 44 | + return Base64.getEncoder().encodeToString(b); |
| 45 | + } |
| 46 | + |
| 47 | + static byte[] samlDecode(String s) { |
| 48 | + return Base64.getMimeDecoder().decode(s); |
| 49 | + } |
| 50 | + |
| 51 | + static byte[] samlDeflate(String s) { |
| 52 | + try { |
| 53 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 54 | + DeflaterOutputStream deflater = new DeflaterOutputStream(b, new Deflater(Deflater.DEFLATED, true)); |
| 55 | + deflater.write(s.getBytes(StandardCharsets.UTF_8)); |
| 56 | + deflater.finish(); |
| 57 | + return b.toByteArray(); |
| 58 | + } |
| 59 | + catch (IOException ex) { |
| 60 | + throw new Saml2Exception("Unable to deflate string", ex); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static String samlInflate(byte[] b) { |
| 65 | + try { |
| 66 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 67 | + InflaterOutputStream iout = new InflaterOutputStream(out, new Inflater(true)); |
| 68 | + iout.write(b); |
| 69 | + iout.finish(); |
| 70 | + return new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 71 | + } |
| 72 | + catch (IOException ex) { |
| 73 | + throw new Saml2Exception("Unable to inflate string", ex); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static EncodingConfigurer withDecoded(String decoded) { |
| 78 | + return new EncodingConfigurer(decoded); |
| 79 | + } |
| 80 | + |
| 81 | + static DecodingConfigurer withEncoded(String encoded) { |
| 82 | + return new DecodingConfigurer(encoded); |
| 83 | + } |
| 84 | + |
| 85 | + static final class EncodingConfigurer { |
| 86 | + |
| 87 | + private final String decoded; |
| 88 | + |
| 89 | + private boolean deflate; |
| 90 | + |
| 91 | + private EncodingConfigurer(String decoded) { |
| 92 | + this.decoded = decoded; |
| 93 | + } |
| 94 | + |
| 95 | + EncodingConfigurer deflate(boolean deflate) { |
| 96 | + this.deflate = deflate; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + String encode() { |
| 101 | + byte[] bytes = (this.deflate) ? Saml2Utils.samlDeflate(this.decoded) |
| 102 | + : this.decoded.getBytes(StandardCharsets.UTF_8); |
| 103 | + return Saml2Utils.samlEncode(bytes); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + static final class DecodingConfigurer { |
| 109 | + |
| 110 | + private static final Base64Checker BASE_64_CHECKER = new Base64Checker(); |
| 111 | + |
| 112 | + private final String encoded; |
| 113 | + |
| 114 | + private boolean inflate; |
| 115 | + |
| 116 | + private boolean requireBase64; |
| 117 | + |
| 118 | + private DecodingConfigurer(String encoded) { |
| 119 | + this.encoded = encoded; |
| 120 | + } |
| 121 | + |
| 122 | + DecodingConfigurer inflate(boolean inflate) { |
| 123 | + this.inflate = inflate; |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + DecodingConfigurer requireBase64(boolean requireBase64) { |
| 128 | + this.requireBase64 = requireBase64; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + String decode() { |
| 133 | + if (this.requireBase64) { |
| 134 | + BASE_64_CHECKER.checkAcceptable(this.encoded); |
| 135 | + } |
| 136 | + byte[] bytes = Saml2Utils.samlDecode(this.encoded); |
| 137 | + return (this.inflate) ? Saml2Utils.samlInflate(bytes) : new String(bytes, StandardCharsets.UTF_8); |
| 138 | + } |
| 139 | + |
| 140 | + static class Base64Checker { |
| 141 | + |
| 142 | + private static final int[] values = genValueMapping(); |
| 143 | + |
| 144 | + Base64Checker() { |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + private static int[] genValueMapping() { |
| 149 | + byte[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 150 | + .getBytes(StandardCharsets.ISO_8859_1); |
| 151 | + |
| 152 | + int[] values = new int[256]; |
| 153 | + Arrays.fill(values, -1); |
| 154 | + for (int i = 0; i < alphabet.length; i++) { |
| 155 | + values[alphabet[i] & 0xff] = i; |
| 156 | + } |
| 157 | + return values; |
| 158 | + } |
| 159 | + |
| 160 | + boolean isAcceptable(String s) { |
| 161 | + int goodChars = 0; |
| 162 | + int lastGoodCharVal = -1; |
| 163 | + |
| 164 | + // count number of characters from Base64 alphabet |
| 165 | + for (int i = 0; i < s.length(); i++) { |
| 166 | + int val = values[0xff & s.charAt(i)]; |
| 167 | + if (val != -1) { |
| 168 | + lastGoodCharVal = val; |
| 169 | + goodChars++; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // in cases of an incomplete final chunk, ensure the unused bits are zero |
| 174 | + switch (goodChars % 4) { |
| 175 | + case 0: |
| 176 | + return true; |
| 177 | + case 2: |
| 178 | + return (lastGoodCharVal & 0b1111) == 0; |
| 179 | + case 3: |
| 180 | + return (lastGoodCharVal & 0b11) == 0; |
| 181 | + default: |
| 182 | + return false; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + void checkAcceptable(String ins) { |
| 187 | + if (!isAcceptable(ins)) { |
| 188 | + throw new IllegalArgumentException("Failed to decode SAMLResponse"); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments