Quantcast
Channel: Rebex Q&A Forum - Recent questions and answers
Viewing all articles
Browse latest Browse all 3862

Answered: Problem with SignedData.Decode when verify is done with sign -outform pem using openssl

$
0
0

Hello,

There is only slight but important difference between OpenSSL's der and pem formats. The DER file only contains binary ASN.1 data. The PEM file contain the same data, but Base-64 encoded, with a header and a footer. Rebex SignedData and EnvelopedData are currently unable to parse this - we'll add support for it to one of the next releases.

In the meantime, you can work around the issue by converting a PEM string loaded from a PEM file to DER data:

public static byte[] DecodeBase64(string text, out string typeName, out string info)
{
    if (!text.StartsWith("-----BEGIN ", StringComparison.Ordinal))
        throw new FormatException("Invalid header.");

    text = text.Replace("\r", "").TrimEnd('\0').Trim();

    int p = text.IndexOf('\n', 11, 64);
    if (p < 8)
        throw new FormatException("Invalid header length.");

    string type = text.Substring(11, p - 11);
    type = type.TrimEnd();

    if (!type.EndsWith("-----", StringComparison.Ordinal))
        throw new FormatException("Invalid header length.");

    typeName = type.Substring(0, type.Length - 5);

    string end = "-----END " + typeName + "-----";
    if (!text.EndsWith(end, StringComparison.Ordinal))
        throw new FormatException("Invalid footer.");

    for (int i = 0; i < text.Length; i++)
    {
        int c = text[i];
        if (c == '\n' || c == '\t')
            continue;

        if (c < 32 || c > 127)
            throw new FormatException(StringUtil.Format("Invalid character {0} at position {1}.", c, i));
    }

    text = text.Substring(end.Length + 2, text.Length - 2 - 2 * end.Length);

    int e = text.IndexOf("\n\n", StringComparison.Ordinal);
    if (e > 0)
    {
        info = text.Substring(0, e).Trim();
        text = text.Substring(e + 2);
    }
    else
    {
        info = null;
    }

    return Convert.FromBase64String(text);
}

Viewing all articles
Browse latest Browse all 3862

Trending Articles