BouncyCastle in C# - Adding a subject with embedded commas to a Certificate Signing Request - bouncycastle

I am using BouncyCastle 1.8.9 and C# .NET 5.0 to generate a Certificate Signing Request where one of the Organizational Unit values that must go in the subject contains an embedded comma:
OU=BANCO BILBAO VIZCAYA ARGENTARIA, S.A
If this is written verbatim in the dirName, BouncyCastle will correctly throw a "badly formated directory string" ArgumentException. Following the advice from this link, this OU value must be written delimited by quotes, which I have attempted in two different ways:
string subjectTest1 = "OU=\"BANCO BILBAO VIZCAYA ARGENTARIA, S.A\""; // OPTION 1 - Using a string escape sequence
string subjectTest2 = #"OU=""BANCO BILBAO VIZCAYA ARGENTARIA, S.A"""; // OPTION 2 - Using a verbatim string literal
However, when checking the resulting subject name, an additional backslash is being added before the comma:
{OU=BANCO BILBAO VIZCAYA ARGENTARIA\, S.A}
¿Has someone encountered this problem before? If so, ¿how were you able to solve it?

Related

encode variables with special characters for azure

Very Similar problem to AADSTS50012: Invalid client secret is provided when moving from a Test App to Production
The top answer says to Encode your secret e.g. replace + by %2B and = by %3D, etc how would I replace the special character Tilde ~
As Suggested by juunas, and as per the document yes, you can replace the special character.
URL encoding converts characters into a format that can be transmitted over the Internet.
Here is the link for complete information regarding Encoding Techniques.

JSONFormat.print() method encoding special characters and also adding extra slash

I need to convert a protobuf message to JSON string in java. For this I am using the below API as recommended by the docs (https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat.Printer.html)
String jsonString = JsonFormat.printer().includingDefaultValueFields().print(protobufMessage);
This is working fine for a simple string, however, when my string contains special characters like &, single quote etc. the gson.toJson() method inside JsonFormat is converting special characters to octal format. For example "A&BC" is converted to "A\u0026BC". Also, the resultant string has an extra backslash appended.
So finally "A&BC" is converted to the string "A\\u0026BC".
If it were "A\u0026BC" then I could have converted to a byte array and formed a string with it. But because of the additional backslash I am not able to do so.
Currently I am using protobuf version 3.7.1 and I tried to upgrade and check if any latest API is available, but it did not help. I searched online but did not find any references (a similar issue was reported for JSONFormat.printToString but this API is removed in a later version. https://github.com/carlomedas/protobuf-java-format/issues/16). Can someone please help here if you have come across this issue.
I think the problem might be that you're using that string to pass along, and it's getting parsed a 2nd time. If you use the printer, it will convert "A&BC" to "A\u0026BC". Then when Jackson parses that, it will append the 2nd backslash. To avoid this, you can use #JsonRawValue annotation to avoid being parsed with the 2nd backslash.

janusgraph - store a password encrypted using bcrypt

I am using janusgraph with cassandra as storage backend. I am using a node package called as bcrypt to encrypt a password before saving it. The data type for that property in janusgraph is String. This is the password hash string which got generated - $2a$10$JSR6FClewTOHGxwpt/F0AePRzGnKvV2L9gj4TL1dA9fQERLWrig7u
This is the error I am getting while trying to save it in the db:
"message": "startup failed:\nScript88.groovy: 1: illegal string body character after dollar sign;\n solution: either escape a literal dollar sign \"\\$5\" or bracket the value expression \"${5}\" # line 1, column 228.\n elf_reg_ind\",\"2\",\"self_reg_pw\",\"$2a$10$J\n ^\n\n1 error\n",
"Exception-Class": "org.codehaus.groovy.control.MultipleCompilationErrorsException"
Please let me know if you need any other info.
The query you are passing to the server gets compiled with Groovy, and Groovy is attempting to resolve the $ as an identifier. You have a literal $ in your hash, so you need to put a \ in front of each $ to escape it. For example:
{ "gremlin":
"g.V(1234).property('hash', '\$2a\$10\$JSR6FClewTOHGxwpt/F0AePRzGnKvV2L9gj4TL1dA9fQERLWrig7u')"
}
Not a groovy guru myself, but I realised this evaluation is only attempted when using double quotes so I managed to solve this issue by ensuring that I use single quotes.
I'm using PHP so the process is to first json_encode then interchange double quotes for single quotes taking care of any already escaped quotes (if any) in between.

Azure Table Storage RowKey restricted Character Patterns?

Are there restricted character patterns within Azure TableStorage RowKeys? I've not been able to find any documented via numerous searches. However, I'm getting behavior that implies such in some performance testing.
I've got some odd behavior with RowKeys consisting on random characters (the test driver does prevent the restricted characters (/ \ # ?) plus blocking single quotes from occurring in the RowKey). The result is I've got a RowKey that will insert fine into the table, but cannot be queried (the result is InvalidInput). For example:
RowKey: 9}5O0J=5Z,4,D,{!IKPE,~M]%54+9G0ZQ&G34!G+
Attempting to query by this RowKwy (equality) will result in an error (both within our app, using Azure Storage Explorer, and Cloud Storage Studio 2). I took a look at the request being sent via Fiddler:
GET /foo()?$filter=RowKey%20eq%20'9%7D5O0J=5Z,4,D,%7B!IKPE,~M%5D%54+9G0ZQ&G34!G+' HTTP/1.1
It appears the %54 in the RowKey is not escaped in the filter. Interestingly, I get similar behavior for batch requests to table storage with URIs in the batch XML that include this RowKey. I've also seen similar behavior for RowKeys with embedded double quotes, though I have not isolated that pattern yet.
Has anyone co me across this behavior? I can easily restrict additional characters from occurring in RowKeys, but would really like to know the 'rules'.
The following characters are not allowed in PartitionKey and RowKey fields:
The forward slash (/) character
The backslash (\) character
The number sign (#) character
The question mark (?) character
Further Reading: Azure Docs > Understanding the Table service data model
public static readonly Regex DisallowedCharsInTableKeys = new Regex(#"[\\\\#%+/?\u0000-\u001F\u007F-\u009F]");
Detection of Invalid Table Partition and Row Keys:
bool invalidKey = DisallowedCharsInTableKeys.IsMatch(tableKey);
Sanitizing the Invalid Partition or Row Key:
string sanitizedKey = DisallowedCharsInTableKeys.Replace(tableKey, disallowedCharReplacement);
At this stage you may also want to prefix the sanitized key (Partition Key or Row Key) with the hash of the original key to avoid false collisions of different invalid keys having the same sanitized value.
Do not use the string.GetHashCode() though since it may produce different hash code for the same string and shall not be used to identify uniqueness and shall not be persisted.
I use SHA256: https://msdn.microsoft.com/en-us/library/s02tk69a(v=vs.110).aspx
to create the byte array hash of the invalid key, convert the byte array to hex string and prefix the sanitized table key with that.
Also see related MSDN Documentation:
https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx
Related Section from the link:
Characters Disallowed in Key Fields
The following characters are not allowed in values for the PartitionKey and RowKey properties:
The forward slash (/) character
The backslash (\) character
The number sign (#) character
The question mark (?) character
Control characters from U+0000 to U+001F, including:
The horizontal tab (\t) character
The linefeed (\n) character
The carriage return (\r) character
Control characters from U+007F to U+009F
Note that in addition to the mentioned chars in the MSDN article, I also added the % char to the pattern since I saw in a few places where people mention it being problematic. I guess some of this also depends on the language and the tech you are using to access the table storage.
If you detect additional problematic chars in your case, then you can add those to the regex pattern, nothing else needs to change.
I just found out (the hard way) that the '+' sign is allowed, but not possible to query in PartitionKey.
I found that in addition to the characters listed in Igorek's answer, these also can cause problems (e.g. inserts will fail):
|
[]
{}
<>
$^&
Tested with the Azure Node.js SDK.
I transform the key using this function:
private static string EncodeKey(string key)
{
return HttpUtility.UrlEncode(key);
}
This needs to be done for the insert and for the retrieve of course.

How do I pass a String into a function in an NVelocity Template?

I'm using the NVelocity Templating engine to produce a fixed-length field output - you know the kind of thing:
Field Start Pos Field Length Notes
---------- --------- ------------ ---------
Supplier 1 7 Leading Zeros
GRN 8 9 -
...
e.g.
>0001234 123A<
The problem is I'm trying to call String.PadRight() with the overload to specify the leading zero, and NVelocity is having none of it..
This works:
$Document.SupplierCode.PadRight(7)
But this doesn't:
$Document.SupplierCode.PadRight(7,"0")
I've tried:
Single Quotes ('0')
Double Single-Quotes (''0'')
Double Quotes ("0")
Double Double-Quotes (""0"")
Escaping the quotes for all of the above (\"0\")
No Quotes!
All I've found to work from is the NVelocity Homepage, and the Velocity Templating Language Reference page, niether are pointing me at a solution.
Sorry I'm unable to supply or point you somewhere where you can test out your ideas for yourself, but any suggestions you may have will be most welcome!
Thanks for your help ;o)
I'm coping with the same problem at the moment, as far as I understand it is due to the fact that PadLeft and PadRight functions of String class receive the second parameter, the leading "0", as a char, not as a string.
NVelocity allows you to specify the parameter as a string using '0', but in this way internally it generate a cast exception (or something similar), because the parameter is expected as char.
I haven't found yet (I'm just using NVelocity since 1 hour!) a way to specify the parameter as char, at the moment I have just a dirty solution such as applying a Replace(" ", "0") after the PadLeft / PadRight, so the template becomes
$Document.SupplierCode.PadRight(7).Replace(' ', '0')
One solution that a colleague has come up with is to create another property in the Document object that returns the formatted String:
E.g.
Public ReadOnly Property SupplierCodeFormatted() As String
Get
Return Supplier.Code.PadLeft(7, "0")
End Get
End Property

Resources