diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-06-29 08:35:02 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-06-29 08:35:02 +0400 |
commit | 70c79acb368609b2a5c1419730d08440bd402cac (patch) | |
tree | ba8efef9540284aec1a18092dbc00e93d557ee67 /core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java | |
parent | d5f802e2244a28b1e24b630bf892cd01be28b0e7 (diff) |
New struct URLAndHash from RFC 6066 (3546)
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java b/core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java new file mode 100644 index 00000000..bab3ee44 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/tls/URLAndHash.java @@ -0,0 +1,101 @@ +package org.bouncycastle.crypto.tls; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.bouncycastle.util.Strings; + +/** + * RFC 6066 5. + */ +public class URLAndHash +{ + protected String url; + protected byte[] sha1Hash; + + public URLAndHash(String url, byte[] sha1Hash) + { + if (url == null || url.length() < 1 || url.length() >= (1 << 16)) + { + throw new IllegalArgumentException("'url' must have length from 1 to (2^16 - 1)"); + } + if (sha1Hash != null && sha1Hash.length != 20) + { + throw new IllegalArgumentException("'sha1Hash' must have length == 20, if present"); + } + + this.url = url; + this.sha1Hash = sha1Hash; + } + + public String getURL() + { + return url; + } + + public byte[] getSHA1Hash() + { + return sha1Hash; + } + + /** + * Encode this {@link URLAndHash} to an {@link OutputStream}. + * + * @param output the {@link OutputStream} to encode to. + * @throws IOException + */ + public void encode(OutputStream output) + throws IOException + { + byte[] urlEncoding = Strings.toByteArray(this.url); + TlsUtils.writeOpaque16(urlEncoding, output); + + if (this.sha1Hash == null) + { + TlsUtils.writeUint8(0, output); + } + else + { + TlsUtils.writeUint8(1, output); + output.write(this.sha1Hash); + } + } + + /** + * Parse a {@link URLAndHash} from an {@link InputStream}. + * + * @param input the {@link InputStream} to parse from. + * @return a {@link URLAndHash} object. + * @throws IOException + */ + public static URLAndHash parse(TlsContext context, InputStream input) + throws IOException + { + byte[] urlEncoding = TlsUtils.readOpaque16(input); + if (urlEncoding.length < 1) + { + throw new TlsFatalAlert(AlertDescription.illegal_parameter); + } + String url = Strings.fromByteArray(urlEncoding); + + byte[] sha1Hash = null; + short padding = TlsUtils.readUint8(input); + switch (padding) + { + case 0: + if (TlsUtils.isTLSv12(context)) + { + throw new TlsFatalAlert(AlertDescription.illegal_parameter); + } + break; + case 1: + sha1Hash = TlsUtils.readFully(20, input); + break; + default: + throw new TlsFatalAlert(AlertDescription.illegal_parameter); + } + + return new URLAndHash(url, sha1Hash); + } +} |