Search code examples
c#bouncycastlecsrpkcs11interoppkcs#10

How to add custom OID and values in CSR using bouncy castle


I am trying to add custom OID in CSR like this

string subject = "CN=Name,O=Org,C=IN,ST=KA,OU=OrgUn,PostalCode=560103";
X509Name sub = new X509Name(subject);
var oidList = sub.GetOidList();
var valueList = sub.GetValueList();

DerObjectIdentifier Oid1 = new derObjectIdentifier("1.53.4.64");//here 1.53.4.64 is custom OID
oidList.Add(Oid1);
valueList.Add("Some Value");

DerObjectIdentifier Oid2 = new DerObjectIdentifier("1.53.4.65");
oidList.Add(derObjectIdentifier2);
valueList.Add("Some value");

X509Name subjectFinal = new X509Name(oidList, valueList);

Pkcs10CertificationRequestDelaySigned pkcs10 = new Pkcs10CertificationRequestDelaySigned(signatureAlgorihtm, subjectFinal, publicKeyParameters, null);

Everything compiles and works fine but when I decode and check CSR the value of Custom Oid name is changed from 1.53.4.65 to some other value 2.13.4.64 in ASN.1 Information.

Is this the correct way to add custom OID values

ASN.1 sequence

SET {
 16  11:         SEQUENCE {
 18   3:           OBJECT IDENTIFIER commonName (2 5 4 3)
 23   4:           UTF8String 'Name'
       :           }
       :         }
 29  12:       SET {
 31  10:         SEQUENCE {
 33   3:           OBJECT IDENTIFIER organizationName (2 5 4 10)
 38   3:           UTF8String 'Org'
       :           }
       :         }
 43  11:       SET {
 45   9:         SEQUENCE {
 47   3:           OBJECT IDENTIFIER countryName (2 5 4 6)
 52   2:           PrintableString 'IN'
       :           }
       :         }
 56  11:       SET {
 58   9:         SEQUENCE {
 60   3:           OBJECT IDENTIFIER stateOrProvinceName (2 5 4 8)
 65   2:           UTF8String 'KA'
       :           }
       :         }
 69  14:       SET {
 71  12:         SEQUENCE {
 73   3:           OBJECT IDENTIFIER organizationalUnitName (2 5 4 11)
 78   5:           UTF8String 'OrgUn'
       :           }
       :         }
 85  15:       SET {
 87  13:         SEQUENCE {
 89   3:           OBJECT IDENTIFIER postalCode (2 5 4 17)
 94   6:           UTF8String '560103'
       :           }
       :         }
102  21:       SET {
104  19:         SEQUENCE {
106   3:           OBJECT IDENTIFIER '2 13 4 64'
111  12:           UTF8String 'Some value 1'
       :           }
       :         }
125  21:       SET {
127  19:         SEQUENCE {
129   3:           OBJECT IDENTIFIER '2 13 4 65'
134  12:           UTF8String 'Some value 2'
       :           }
       :         }

Solution

  • code to add custom OID attributes is correct but we cannot use any random number as OID it must be registered for ex 1.3.6.1.4.1.XXXX.X.X or it must have some format.