0

I have a PythonNet C# bytes[] object and I need to convert it to Python's bytes.

Is there a way to do this using Python?

Here is what I'm doing to get the bytes. I need to go into the winstore's credential store and export out a client certificate.

import clr, os
import requests

from cryptography.hazmat.primitives.serialization.pkcs12 import (
    load_key_and_certificates,
)
from cryptography.hazmat.primitives.serialization import Encoding, NoEncryption
from cryptography.hazmat.backends import default_backend



clr.AddReference("System")
clr.AddReference("System.Security.Cryptography.X509Certificates")
clr.AddReference("System.Security.Cryptography")

from System.Security.Cryptography.X509Certificates import (
    X509Store,
    StoreName,
    StoreLocation,
    OpenFlags,
    X509CertificateCollection,
    X509FindType,
    X509Certificate2,
    X509ContentType,
)

store = X509Store(StoreName.My, StoreLocation.CurrentUser)
store.Open(OpenFlags.ReadOnly)

serial_number = (
    'SUPER SECRET SERIAL NUMBER'  
)

collection = store.Certificates.Find(
    X509FindType.FindBySerialNumber, serial_number, False
)
cert = collection.get_Item(0)
pkcs12 = cert.Export(X509ContentType.Pkcs12, "<Secret Password>")
# I have System.Bytes[] need python bytes to use with pyopenssl/cryptography

This code is using the pythonnet library to access the crypto API on windows. It then dumps a client certificate, found by a serial number, to bytes. When the clr library returns the value, it's a system.Bytes[]. This is incompatible with other libraries, so I need a way to convert this object to Python bytes.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
JabberJabber
  • 341
  • 2
  • 17
  • How does the object enter your Python program in the first place? Please show a [mre]. – mkrieger1 Jul 07 '21 at 14:16
  • @mkrieger1 I added a sample to view. – JabberJabber Jul 07 '21 at 14:24
  • So `pkcs12` is the `bytes[]` object? – mkrieger1 Jul 07 '21 at 14:30
  • Correct, pkcs12 is the bytes[] – JabberJabber Jul 07 '21 at 14:37
  • I get the following error: ```builtins.TypeError: a bytes-like object is required, not 'Byte[]'``` – JabberJabber Jul 07 '21 at 14:40
  • Bytes are bytes. They're the same in all languages. What you posted exports a certificate in a specific format. You need to find out what that format is and use a library that can read the same format. Certificate formats are pretty standard – Panagiotis Kanavos Jul 07 '21 at 14:41
  • ```bytes(pkcs12)``` that does it. – JabberJabber Jul 07 '21 at 14:42
  • You need to understand what you're doing fist. You aren't trying to convert bytes. You don't have bytes. You're trying to pass one library's types to another without conversion. This wouldn't work between Python libraries either. You couldn't pass a `dataframe` to pyopenssl either. Why are you trying to use .NET Code to read and export a certificate anyway? Is you **real** question how to use a local certificate on Windows perhaps? The answer is to tell your library where the store is and which certificate to use – Panagiotis Kanavos Jul 07 '21 at 14:46
  • @PanagiotisKanavos correct I am trying to export a store client certificate from the Windows Credential Store to pass into the `requests` module for PKI validation. The issue is that the cryptography library doesn't support this, and supposely pyopenssl does, but I cannot get it to generate proper PEM files. – JabberJabber Jul 08 '21 at 10:30

1 Answers1

2

You should be able to use the Python function bytes (as @JabberJabber mentioned). The following works for me (c_sharp_bytes actually being the result of a C# function) with Python 3.8:

from System import Byte, Array
python_bytes = b'Some bytes'

# Python -> C#
c_sharp_bytes = Array[Byte](python_bytes)  # <System.Byte[] object at 0x000001A33CC2A3D0>

# C# -> Python
python_bytes = bytes(c_sharp_bytes)  # b'Some bytes'
phispi
  • 604
  • 7
  • 15