11using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24
35namespace SysadminsLV . Asn1Parser . Universal {
46 /// <summary>
57 /// Represents base class for ASN.1 string types.
68 /// </summary>
79 public abstract class Asn1String : UniversalTagBase {
10+ #region allowedStringTypes:
11+ const Int32 CERT_RDN_ANY_TYPE = 0 ;
12+ const Int32 CERT_RDN_ENCODED_BLOB = 1 ;
13+ const Int32 CERT_RDN_OCTET_STRING = 2 ;
14+ const Int32 CERT_RDN_NUMERIC_STRING = 3 ;
15+ const Int32 CERT_RDN_PRINTABLE_STRING = 4 ;
16+ const Int32 CERT_RDN_TELETEX_STRING = 5 ;
17+ const Int32 CERT_RDN_T61_STRING = 5 ;
18+ const Int32 CERT_RDN_VIDEOTEX_STRING = 6 ;
19+ const Int32 CERT_RDN_IA5_STRING = 7 ;
20+ const Int32 CERT_RDN_GRAPHIC_STRING = 8 ; // not used
21+ const Int32 CERT_RDN_VISIBLE_STRING = 9 ;
22+ const Int32 CERT_RDN_ISO646_STRING = 9 ;
23+ const Int32 CERT_RDN_GENERAL_STRING = 10 ; // not used
24+ const Int32 CERT_RDN_UNIVERSAL_STRING = 11 ;
25+ const Int32 CERT_RDN_INT4_STRING = 11 ;
26+ const Int32 CERT_RDN_BMP_STRING = 12 ;
27+ const Int32 CERT_RDN_UNICODE_STRING = 12 ;
28+ const Int32 CERT_RDN_UTF8_STRING = 13 ;
29+ #endregion
30+
831 /// <summary>
932 /// Initializes a new instance of <strong>Asn1String</strong> class.
1033 /// </summary>
@@ -22,5 +45,52 @@ protected Asn1String(Asn1Reader asn, Asn1Type? type) : base(asn, type) { }
2245 /// </summary>
2346 public String Value { get ; protected set ; }
2447
48+ /// <summary>
49+ /// Decodes any ASN.1-encoded binary string into ASN.1 string type instance.
50+ /// </summary>
51+ /// <param name="rawData">Encoded ASN.1 string.</param>
52+ /// <param name="allowedStringTypes">An optional collection of allowed string allowedStringTypes.</param>
53+ /// <exception cref="ArgumentNullException">
54+ /// <strong>rawData</strong> parameter is null.
55+ /// </exception>
56+ /// <exception cref="ArgumentException">
57+ /// <strong>rawData</strong> parameter is either too small or is not allowed by restriction.
58+ /// </exception>
59+ /// <returns>ASN.1 string type instance.</returns>
60+ /// <exception cref="Asn1InvalidTagException">
61+ /// Input data is not valid string type.
62+ /// </exception>
63+ public static Asn1String DecodeAnyString ( Byte [ ] rawData , IEnumerable < Asn1Type > allowedStringTypes = null ) {
64+ if ( rawData == null ) {
65+ throw new ArgumentNullException ( nameof ( rawData ) ) ;
66+ }
67+ if ( rawData . Length < 2 ) {
68+ throw new ArgumentException ( "Raw data must have at least tag (1 byte) and length components (1 byte) in TLV structure." ) ;
69+ }
70+
71+ IEnumerable < Asn1Type > asn1Types = allowedStringTypes ? . ToList ( ) ;
72+ if ( asn1Types != null && ! asn1Types . Contains ( ( Asn1Type ) rawData [ 0 ] ) ) {
73+ throw new ArgumentException ( "Input string is not permitted by restriction." ) ;
74+ }
75+ var tag = ( Asn1Type ) ( rawData [ 0 ] & ( Int32 ) Asn1Type . TAG_MASK ) ;
76+ switch ( tag ) {
77+ case Asn1Type . IA5String :
78+ return new Asn1IA5String ( rawData ) ;
79+ case Asn1Type . PrintableString :
80+ return new Asn1PrintableString ( rawData ) ;
81+ case Asn1Type . VisibleString :
82+ return new Asn1VisibleString ( rawData ) ;
83+ case Asn1Type . UTF8String :
84+ return new Asn1UTF8String ( rawData ) ;
85+ case Asn1Type . UniversalString :
86+ return new Asn1UniversalString ( rawData ) ;
87+ case Asn1Type . BMPString :
88+ return new Asn1BMPString ( rawData ) ;
89+ case Asn1Type . TeletexString :
90+ return new Asn1TeletexString ( rawData ) ;
91+ default :
92+ throw new Asn1InvalidTagException ( "Input data is not valid string." ) ;
93+ }
94+ }
2595 }
2696}
0 commit comments