01: package org.bouncycastle.bcpg.sig;
02:
03: import org.bouncycastle.bcpg.SignatureSubpacket;
04: import org.bouncycastle.bcpg.SignatureSubpacketTags;
05: import org.bouncycastle.util.Strings;
06:
07: import java.io.ByteArrayOutputStream;
08:
09: /**
10: * Class provided a NotationData object according to
11: * RFC2440, Chapter 5.2.3.15. Notation Data
12: */
13: public class NotationData extends SignatureSubpacket {
14: public static final int HEADER_FLAG_LENGTH = 4;
15: public static final int HEADER_NAME_LENGTH = 2;
16: public static final int HEADER_VALUE_LENGTH = 2;
17:
18: public NotationData(boolean critical, byte[] data) {
19: super (SignatureSubpacketTags.NOTATION_DATA, critical, data);
20: }
21:
22: public NotationData(boolean critical, boolean humanReadable,
23: String notationName, String notationValue) {
24: super (SignatureSubpacketTags.NOTATION_DATA, critical,
25: createData(humanReadable, notationName, notationValue));
26: }
27:
28: private static byte[] createData(boolean humanReadable,
29: String notationName, String notationValue) {
30: ByteArrayOutputStream out = new ByteArrayOutputStream();
31:
32: // (4 octets of flags, 2 octets of name length (M),
33: // 2 octets of value length (N),
34: // M octets of name data,
35: // N octets of value data)
36:
37: // flags
38: out.write(humanReadable ? 0x80 : 0x00);
39: out.write(0x0);
40: out.write(0x0);
41: out.write(0x0);
42:
43: byte[] nameData, valueData = null;
44: int nameLength, valueLength;
45:
46: nameData = Strings.toUTF8ByteArray(notationName);
47: nameLength = Math.min(nameData.length, 0xFF);
48:
49: valueData = Strings.toUTF8ByteArray(notationValue);
50: valueLength = Math.min(valueData.length, 0xFF);
51:
52: // name length
53: out.write((nameLength >>> 8) & 0xFF);
54: out.write((nameLength >>> 0) & 0xFF);
55:
56: // value length
57: out.write((valueLength >>> 8) & 0xFF);
58: out.write((valueLength >>> 0) & 0xFF);
59:
60: // name
61: out.write(nameData, 0, nameLength);
62:
63: // value
64: out.write(valueData, 0, valueLength);
65:
66: return out.toByteArray();
67: }
68:
69: public boolean isHumanReadable() {
70: return data[0] == (byte) 0x80;
71: }
72:
73: public String getNotationName() {
74: int nameLength = ((data[HEADER_FLAG_LENGTH] << 8) + (data[HEADER_FLAG_LENGTH + 1] << 0));
75:
76: byte bName[] = new byte[nameLength];
77: System.arraycopy(data, HEADER_FLAG_LENGTH + HEADER_NAME_LENGTH
78: + HEADER_VALUE_LENGTH, bName, 0, nameLength);
79:
80: return Strings.fromUTF8ByteArray(bName);
81: }
82:
83: public String getNotationValue() {
84: int nameLength = ((data[HEADER_FLAG_LENGTH] << 8) + (data[HEADER_FLAG_LENGTH + 1] << 0));
85: int valueLength = ((data[HEADER_FLAG_LENGTH
86: + HEADER_NAME_LENGTH] << 8) + (data[HEADER_FLAG_LENGTH
87: + HEADER_NAME_LENGTH + 1] << 0));
88:
89: byte bValue[] = new byte[valueLength];
90: System.arraycopy(data, HEADER_FLAG_LENGTH + HEADER_NAME_LENGTH
91: + HEADER_VALUE_LENGTH + nameLength, bValue, 0,
92: valueLength);
93:
94: return Strings.fromUTF8ByteArray(bValue);
95: }
96: }
|