1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.X509
{
/**
* <code>DisplayText</code> class, used in
* <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
*
* <p>It stores a string in a chosen encoding.
* <pre>
* DisplayText ::= CHOICE {
* ia5String IA5String (SIZE (1..200)),
* visibleString VisibleString (SIZE (1..200)),
* bmpString BMPString (SIZE (1..200)),
* utf8String UTF8String (SIZE (1..200)) }
* </pre></p>
* @see PolicyQualifierInfo
* @see PolicyInformation
*/
public class DisplayText
: Asn1Encodable, IAsn1Choice
{
/**
* Constant corresponding to ia5String encoding.
*
*/
public const int ContentTypeIA5String = 0;
/**
* Constant corresponding to bmpString encoding.
*
*/
public const int ContentTypeBmpString = 1;
/**
* Constant corresponding to utf8String encoding.
*
*/
public const int ContentTypeUtf8String = 2;
/**
* Constant corresponding to visibleString encoding.
*
*/
public const int ContentTypeVisibleString = 3;
/**
* Describe constant <code>DisplayTextMaximumSize</code> here.
*
*/
public const int DisplayTextMaximumSize = 200;
internal readonly int contentType;
internal readonly IAsn1String contents;
/**
* Creates a new <code>DisplayText</code> instance.
*
* @param type the desired encoding type for the text.
* @param text the text to store. Strings longer than 200
* characters are truncated.
*/
public DisplayText(
int type,
string text)
{
if (text.Length > DisplayTextMaximumSize)
{
// RFC3280 limits these strings to 200 chars
// truncate the string
text = text.Substring(0, DisplayTextMaximumSize);
}
contentType = type;
switch (type)
{
case ContentTypeIA5String:
contents = (IAsn1String)new DerIA5String (text);
break;
case ContentTypeUtf8String:
contents = (IAsn1String)new DerUtf8String(text);
break;
case ContentTypeVisibleString:
contents = (IAsn1String)new DerVisibleString(text);
break;
case ContentTypeBmpString:
contents = (IAsn1String)new DerBmpString(text);
break;
default:
contents = (IAsn1String)new DerUtf8String(text);
break;
}
}
// /**
// * return true if the passed in string can be represented without
// * loss as a PrintableString, false otherwise.
// */
// private bool CanBePrintable(
// string str)
// {
// for (int i = str.Length - 1; i >= 0; i--)
// {
// if (str[i] > 0x007f)
// {
// return false;
// }
// }
//
// return true;
// }
/**
* Creates a new <code>DisplayText</code> instance.
*
* @param text the text to encapsulate. Strings longer than 200
* characters are truncated.
*/
public DisplayText(
string text)
{
// by default use UTF8String
if (text.Length > DisplayTextMaximumSize)
{
text = text.Substring(0, DisplayTextMaximumSize);
}
contentType = ContentTypeUtf8String;
contents = new DerUtf8String(text);
}
/**
* Creates a new <code>DisplayText</code> instance.
* <p>Useful when reading back a <code>DisplayText</code> class
* from it's Asn1Encodable form.</p>
*
* @param contents an <code>Asn1Encodable</code> instance.
*/
public DisplayText(
IAsn1String contents)
{
this.contents = contents;
}
public static DisplayText GetInstance(object obj)
{
if (obj is IAsn1String asn1String)
return new DisplayText(asn1String);
if (obj is DisplayText displayText)
return displayText;
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public static DisplayText GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
Asn1Utilities.GetInstanceChoice(taggedObject, declaredExplicit, GetInstance);
public static DisplayText GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance);
public override Asn1Object ToAsn1Object()
{
return (Asn1Object) contents;
}
/**
* Returns the stored <code>string</code> object.
*
* @return the stored text as a <code>string</code>.
*/
public string GetString()
{
return contents.GetString();
}
}
}
|