Created
December 29, 2023 14:04
-
-
Save DJm00n/9c99bd454bde8c8a15a16687cde19e50 to your computer and use it in GitHub Desktop.
Search UAPKI public key on CMP server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace CA_Client.Helpers | |
{ | |
internal static class SearchByCmp | |
{ | |
private static int GetHexVal(char hex) | |
{ | |
int val = (int)hex; | |
//For uppercase A-F letters: | |
//return val - (val < 58 ? 48 : 55); | |
//For lowercase a-f letters: | |
//return val - (val < 58 ? 48 : 87); | |
//Or the two combined, but a bit slower: | |
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87)); | |
} | |
private static byte[] HexToBytes(string hex) | |
{ | |
if (hex.Length % 2 == 1) | |
throw new ArgumentException("The hexadecimal key must have an even number of digits"); | |
byte[] arr = new byte[hex.Length >> 1]; | |
for (int i = 0; i < hex.Length >> 1; ++i) | |
{ | |
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1]))); | |
} | |
return arr; | |
} | |
public static byte[] GetCertBundle(string url, string keyId1, string keyId2) | |
{ | |
string pre = "30818706092a864886f70d010701a07a04780d0000000000000002000000"; | |
string post = "0000000000000000000000000000000000000000000000000000000000000000010000000100000000000000"; | |
if (keyId1.Length == 40) | |
keyId1 += "000000000000000000000000"; | |
if (keyId2.Length == 40) | |
keyId2 += "000000000000000000000000"; | |
if ((keyId1.Length != 64) || (keyId2.Length != 64)) | |
throw new ArgumentException("CMP supports only DSTU keys"); | |
var req = HexToBytes(pre + keyId1 + keyId2 + post); | |
var byteContent = new ByteArrayContent(req); | |
var client = new HttpClient(); | |
var result = client.PostAsync(url, byteContent).Result.Content.ReadAsByteArrayAsync().Result; | |
if (result.Length < 280) | |
throw new Exception("Certificates not found"); | |
if (result.Length > 65536) | |
throw new Exception("Response too long"); | |
int offset = 31; | |
var bundle = new byte[result.Length - offset]; | |
Array.Copy(result, offset, bundle, 0, result.Length - offset); | |
return bundle; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment