public virtual void DecodeX509KeyUsageExtension(byte[] encoded, out X509KeyUsageFlags keyUsages) { AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER); KeyUsageFlagsAsn keyUsagesAsn = reader.GetNamedBitListValue <KeyUsageFlagsAsn>(); reader.ThrowIfNotEmpty(); // DER encodings of BIT_STRING values number the bits as // 01234567 89 (big endian), plus a number saying how many bits of the last byte were padding. // // So digitalSignature (0) doesn't mean 2^0 (0x01), it means the most significant bit // is set in this byte stream. // // BIT_STRING values are compact. So a value of cRLSign (6) | keyEncipherment (2), which // is 0b0010001 => 0b0010 0010 (1 bit padding) => 0x22 encoded is therefore // 0x02 (length remaining) 0x01 (1 bit padding) 0x22. // // We will read that, and return 0x22. 0x22 lines up // exactly with X509KeyUsageFlags.CrlSign (0x20) | X509KeyUsageFlags.KeyEncipherment (0x02) // // Once the decipherOnly (8) bit is added to the mix, the values become: // 0b001000101 => 0b0010 0010 1000 0000 (7 bits padding) // { 0x03 0x07 0x22 0x80 } // And we read new byte[] { 0x22 0x80 } // // The value of X509KeyUsageFlags.DecipherOnly is 0x8000. 0x8000 in a little endian // representation is { 0x00 0x80 }. This means that the DER storage mechanism has effectively // ended up being little-endian for BIT_STRING values. Untwist the bytes, and now the bits all // line up with the existing X509KeyUsageFlags. keyUsages = (X509KeyUsageFlags)ReverseBitOrder((byte)keyUsagesAsn) | (X509KeyUsageFlags)(ReverseBitOrder((byte)(((ushort)keyUsagesAsn >> 8))) << 8); }
public virtual byte[] EncodeX509KeyUsageExtension(X509KeyUsageFlags keyUsages) { // The numeric values of X509KeyUsageFlags mean that if we interpret it as a little-endian // ushort it will line up with the flags in the spec. We flip bit order of each byte to get // the KeyUsageFlagsAsn order expected by AsnWriter. KeyUsageFlagsAsn keyUsagesAsn = (KeyUsageFlagsAsn)ReverseBitOrder((byte)keyUsages) | (KeyUsageFlagsAsn)(ReverseBitOrder((byte)(((ushort)keyUsages >> 8))) << 8); // The expected output of this method isn't the SEQUENCE value, but just the payload bytes. AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); writer.WriteNamedBitList(keyUsagesAsn); return(writer.Encode()); }