BytesToUtf8String() public static méthode

Converts byte array (not null terminated) to UTF-8 string
public static BytesToUtf8String ( byte value ) : string
value byte Byte array that should be converted
Résultat string
Exemple #1
0
        /// <summary>
        /// Parses query attribute
        /// </summary>
        /// <param name="attribute">Query attribute that should be parsed</param>
        private void ParseQueryAttribute(string attribute)
        {
            int separatorIndex = attribute.IndexOf(Pkcs11UriSpec.Pk11QueryAttributeNameAndValueSeparator);

            if (separatorIndex == -1)
            {
                throw new Pkcs11UriException("Attribute name and value are not separated");
            }

            string attributeName  = attribute.Substring(0, separatorIndex);
            string attributeValue = attribute.Substring(separatorIndex + 1);

            switch (attributeName)
            {
            case Pkcs11UriSpec.Pk11PinSource:

                if (_pinSource != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the query component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11QueryAttrValueChars, true);
                    _pinSource = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _pinSource = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11PinValue:

                if (_pinValue != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the query component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11QueryAttrValueChars, true);
                    _pinValue = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _pinValue = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11ModuleName:

                if (_moduleName != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the query component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11QueryAttrValueChars, true);
                    _moduleName = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _moduleName = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11ModulePath:

                if (_modulePath != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the query component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11QueryAttrValueChars, true);
                    _modulePath = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _modulePath = string.Empty;
                }

                break;

            default:

                if (string.IsNullOrEmpty(attributeName))
                {
                    throw new Pkcs11UriException("Attribute without name found in the query component");
                }

                byte[] vendorAttrName = DecodePk11String(null, attributeName, Pkcs11UriSpec.Pk11VendorAttrNameChars, false);
                attributeName = ConvertUtils.BytesToUtf8String(vendorAttrName);

                if (attributeValue != string.Empty)
                {
                    byte[] vendorAttrValue = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11QueryAttrValueChars, true);
                    attributeValue = ConvertUtils.BytesToUtf8String(vendorAttrValue);
                }

                if (_unknownQueryAttributes.ContainsKey(attributeName))
                {
                    _unknownQueryAttributes[attributeName].Add(attributeValue);
                }
                else
                {
                    _unknownQueryAttributes.Add(attributeName, new List <string>()
                    {
                        attributeValue
                    });
                }

                break;
            }
        }
Exemple #2
0
        /// <summary>
        /// Parses path attribute
        /// </summary>
        /// <param name="attribute">Path attribute that should be parsed</param>
        private void ParsePathAttribute(string attribute)
        {
            int separatorIndex = attribute.IndexOf(Pkcs11UriSpec.Pk11PathAttributeNameAndValueSeparator);

            if (separatorIndex == -1)
            {
                throw new Pkcs11UriException("Attribute name and value are not separated");
            }

            string attributeName  = attribute.Substring(0, separatorIndex);
            string attributeValue = attribute.Substring(separatorIndex + 1);

            switch (attributeName)
            {
            case Pkcs11UriSpec.Pk11Token:

                if (_token != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11TokenMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _token = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _token = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11Manuf:

                if (_manufacturer != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11ManufMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _manufacturer = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _manufacturer = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11Serial:

                if (_serial != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11SerialMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _serial = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _serial = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11Model:

                if (_model != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11ModelMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _model = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _model = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11LibManuf:

                if (_libraryManufacturer != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11LibManufMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _libraryManufacturer = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _libraryManufacturer = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11LibDesc:

                if (_libraryDescription != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11LibDescMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _libraryDescription = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _libraryDescription = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11LibVer:

                if (_libraryVersion != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue == string.Empty)
                {
                    throw new Pkcs11UriException("Value of " + attributeName + " attribute cannot be empty");
                }

                int major = 0;
                int minor = 0;

                string[] parts = attributeValue.Split(new char[] { '.' }, StringSplitOptions.None);
                if (parts.Length == 1)
                {
                    major = Convert.ToInt32(parts[0]);
                }
                else if (parts.Length == 2)
                {
                    if (string.IsNullOrEmpty(parts[0]))
                    {
                        throw new Pkcs11UriException("Attribute " + attributeName + " does not specify major version");
                    }

                    if (string.IsNullOrEmpty(parts[1]))
                    {
                        throw new Pkcs11UriException("Attribute " + attributeName + " does not specify minor version");
                    }

                    try
                    {
                        major = Convert.ToInt32(parts[0]);
                    }
                    catch (Exception ex)
                    {
                        throw new Pkcs11UriException("Attribute " + attributeName + " contains major version that cannot be converted to integer", ex);
                    }

                    try
                    {
                        minor = Convert.ToInt32(parts[1]);
                    }
                    catch (Exception ex)
                    {
                        throw new Pkcs11UriException("Attribute " + attributeName + " contains minor version that cannot be converted to integer", ex);
                    }
                }
                else
                {
                    throw new Pkcs11UriException("Invalid value of " + attributeName + " attribute");
                }

                if ((_checkLengths == true) && ((major > 0xff) || (minor > 0xff)))
                {
                    throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                }

                _libraryVersion = string.Format("{0}.{1}", major, minor);

                break;

            case Pkcs11UriSpec.Pk11Object:

                if (_object != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    _object = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _object = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11Type:

                if (_type != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue == string.Empty)
                {
                    throw new Pkcs11UriException("Value of " + attributeName + " attribute cannot be empty");
                }

                switch (attributeValue)
                {
                case Pkcs11UriSpec.Pk11TypePublic:
                    _type = CKO.CKO_PUBLIC_KEY;
                    break;

                case Pkcs11UriSpec.Pk11TypePrivate:
                    _type = CKO.CKO_PRIVATE_KEY;
                    break;

                case Pkcs11UriSpec.Pk11TypeCert:
                    _type = CKO.CKO_CERTIFICATE;
                    break;

                case Pkcs11UriSpec.Pk11TypeSecretKey:
                    _type = CKO.CKO_SECRET_KEY;
                    break;

                case Pkcs11UriSpec.Pk11TypeData:
                    _type = CKO.CKO_DATA;
                    break;

                default:
                    throw new Pkcs11UriException("Invalid value of " + attributeName + " attribute");
                }

                break;

            case Pkcs11UriSpec.Pk11Id:

                if (_id != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    _id = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                }
                else
                {
                    _id = new byte[0];
                }

                break;

            case Pkcs11UriSpec.Pk11SlotManuf:

                if (_slotManufacturer != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11SlotManufMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _slotManufacturer = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _slotManufacturer = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11SlotDesc:

                if (_slotDescription != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue != string.Empty)
                {
                    byte[] bytes = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    if ((_checkLengths == true) && (bytes.Length > Pkcs11UriSpec.Pk11SlotDescMaxLength))
                    {
                        throw new Pkcs11UriException("Value of " + attributeName + " attribute exceeds the maximum allowed length");
                    }
                    _slotDescription = ConvertUtils.BytesToUtf8String(bytes);
                }
                else
                {
                    _slotDescription = string.Empty;
                }

                break;

            case Pkcs11UriSpec.Pk11SlotId:

                if (_slotId != null)
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }

                if (attributeValue == string.Empty)
                {
                    throw new Pkcs11UriException("Value of " + attributeName + " attribute cannot be empty");
                }

                try
                {
                    _slotId = Convert.ToUInt64(attributeValue);
                }
                catch (Exception ex)
                {
                    throw new Pkcs11UriException("Invalid value of " + attributeName + " attribute", ex);
                }

                break;

            default:

                if (string.IsNullOrEmpty(attributeName))
                {
                    throw new Pkcs11UriException("Attribute without name found in the path component");
                }

                byte[] vendorAttrName = DecodePk11String(null, attributeName, Pkcs11UriSpec.Pk11VendorAttrNameChars, false);
                attributeName = ConvertUtils.BytesToUtf8String(vendorAttrName);

                if (attributeValue != string.Empty)
                {
                    byte[] vendorAttrValue = DecodePk11String(attributeName, attributeValue, Pkcs11UriSpec.Pk11PathAttrValueChars, true);
                    attributeValue = ConvertUtils.BytesToUtf8String(vendorAttrValue);
                }

                if (_unknownPathAttributes.ContainsKey(attributeName))
                {
                    throw new Pkcs11UriException("Duplicate attribute " + attributeName + " found in the path component");
                }
                else
                {
                    _unknownPathAttributes.Add(attributeName, attributeValue);
                }

                break;
            }
        }