Unfortunately, the patch would be massive. I've removed all unused functions, and rewritten a good chunk of what remained. And then it would take weeks for the code to be cleared to be let out of the company.
Since the hard part is creating the final CMS signature, here's the sequence of OpenSSL functions you'll need to call:
EVP_get_digestbyname()
d2i_X509()
CMS_ContentInfo_new()
ASN1_OBJECT_free()
CMS_SignedData_init()
M_ASN1_new_of()
cms_set1_SignerIdentifier()
cms_DigestAlgorithm_set() /* for mechanism */
cms_DigestAlgorithm_set() /* for message digest algorithm */
sk_X509_ALGOR_push()
CMS_SignerInfo_get0_algs()
X509_ALGOR_set0()
sk_CMS_SignerInfo_push()
CMS_signed_add1_attr_by_NID() /* for message digest */
CMS_signed_add1_attr_by_NID() /* for X509_gmtime_adj() */
CMS_signed_add1_attr_by_NID() /* for content type */
ASN1_item_i2d() /* for signed attributes */
/* finalize HSM digest */
ASN1_STRING_set0() /* add signature data */
ASN1_item_i2d() /* for CMS content info */
All of this is based on the OpenSSL code. Look in cms_smime.c CMS_sign(), cms_sd.c CMS_add1_signer() and CMS_SignerInfo_sign(). First get familiar with the HSM sample code and functions, and then write a signature function. Then you'll be able to plug it into the CST code without much difficulty.