The following function generates a PKCS#10 certification request.
It uses BouncyCastle library to handle ASN1 and Der encoding:
Imports Org.BouncyCastle.Asn1.X509
Imports Org.BouncyCastle.Asn1.X500
Imports Org.BouncyCastle.Asn1.Pkcs
Imports Org.BouncyCastle.Asn1
Imports Org.BouncyCastle.X509
Imports Org.BouncyCastle.Math
...
Function generatePKCS10CertificationRequest(ByVal distinguishedName As String, ByVal priKey As RSAPrivateKey, ByVal pubKey As RSAPublicKey, ByVal session As Session) As Byte()
Dim subject As X509Name = New X509Name(distinguishedName)
Dim pk As RsaPublicKeyStructure = New RsaPublicKeyStructure(New BigInteger(1, pubKey.Modulus), New BigInteger(1, pubKey.PublicExponent))
Dim spkInfo As SubjectPublicKeyInfo = New SubjectPublicKeyInfo(New AlgorithmIdentifier(X509ObjectIdentifiers.IdSha1, DerNull.Instance), pk.GetDerEncoded())
Dim reqInfo As CertificationRequestInfo = New CertificationRequestInfo(subject, spkInfo, Nothing)
Dim toSign As Byte() = reqInfo.GetDerEncoded()
session.SignInit(Mechanism.SHA1_RSA_PKCS, priKey)
Dim signature As Byte() = session.Sign(toSign)
Dim pkcs10 As CertificationRequest = New CertificationRequest(reqInfo, New AlgorithmIdentifier(Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.Sha1WithRsaEncryption, DerNull.Instance), New DerBitString(signature))
generatePKCS10CertificationRequest = pkcs10.GetDerEncoded()
End Function