How to create an identity who can be used for signing code from custom certificate?
Your certificate is missing specific codesign requirements. These go beyond keyUsage = digitalSignature.

Conforming Certificate

You need to create a certificate that conforms to macOS's codesigning requirements. You can find the requirements in Certification Authority Certification Practice Statement Developer ID:


  4.12.2. Code Signing Certificates
  
  A Code Signing Certificate issued by the Developer ID Sub-CA shall conform to the X.509 Certificate format and shall contain, at a minimum, the following data elements:
  
  
  Serial Number
  Subject Distinguished Name
  Issuer Distinguished Name
  Algorithm used (RSA)
  Modulus (Size in bits)
  Validity period
  Certificate Policies extension listing the CP
  Certificate qualifiers listing this CPS (URL)
  User notice qualifier
  Extended Key Usage extension, critical, with a purpose containing Code Signing (1.3.6.1.5.5.7.3.3)
  Developer ID Code Signing (1.2.840.113635.100.6.1.13)
  Certificate Authority Information Access extension, non-critical, with a method of obtaining certificate authority information via Online Certificate Status Protocol method.
  


For more details, see the Security StackExchange question How to create an Apple Installer Package Signing Certificate.

Trusted Certificate Authority

With a conforming certificate, you will be able to codesign your binary. For the code signature to recognised, you will need to install and trust the Certificate Authority (CA) certificate on your Mac. This needs to be done in a way that Gatekeeper trusts.

You can learn more about this step by searching for install root certificate mac.