67 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
68 RSA_set_flags(pRSA,RSA_FLAG_THREAD_SAFE | RSA_FLAG_NO_BLINDING );
70 pRSA->flags |= RSA_FLAG_THREAD_SAFE;
71 pRSA->flags |= RSA_FLAG_SIGN_VER;
72 #ifdef RSA_FLAG_NO_BLINDING
73 pRSA->flags |= RSA_FLAG_NO_BLINDING;
76 #if OPENSSL_VERSION_NUMBER > 0x0090707fL
90 if (RSA_private_decrypt(
RSA_SIZE, from, to,
m_pRSA, RSA_NO_PADDING) == -1)
124 RSA_public_encrypt(fromlen, from, to,
m_pRSA, RSA_PKCS1_OAEP_PADDING);
143 SINT32 ret = RSA_public_encrypt(fromlen, from, to,
m_pRSA, RSA_PKCS1_PADDING);
159 if (RSA_public_encrypt(
RSA_SIZE, from, to,
m_pRSA, RSA_NO_PADDING) == -1)
174 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
176 BIGNUM* e = BN_new();
177 BN_set_word(e, 65537);
185 m_pRSA = ::RSA_generate_key(size, 65537, NULL, NULL);
310 #ifndef ONLY_LOCAL_PROXY
335 if (
m_pRSA == NULL || buff == NULL)
338 DOMElement *elemRoot = NULL;
350 #ifdef EXPORT_ASYM_PRIVATE_KEY
393 if (
m_pRSA == NULL || buff == NULL)
396 DOMElement *elemRoot = NULL;
397 getPrivateKeyAsDOMElement(elemRoot, pDoc);
407 SINT32 CAASymCipher::getPrivateKeyAsDOMElement(
408 DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
415 const BIGNUM *n, *e, *p, *q, *dmp1, *dmq1, *iqmp, *d;
416 RSA_get0_key(
m_pRSA, &n, &e, &d);
417 RSA_get0_factors(
m_pRSA, &p, &q);
418 RSA_get0_crt_params(
m_pRSA, &dmp1, &dmq1, &iqmp);
421 addKeyPart(elemRoot, docOwner,
"Exponent", e);
426 addKeyPart(elemRoot, docOwner,
"InverseQ", iqmp);
463 DOMElement *root = doc->getDocumentElement();
468 return setPrivateKeyAsDOMNode(root);
537 SINT32 CAASymCipher::setPrivateKeyAsDOMNode(DOMNode *node)
539 DOMNode *root = node;
542 if (
equals(root->getNodeName(),
"RSAKeyPair"))
544 RSA *tmpRSA = RSA_new();
545 BIGNUM *n, *e, *p, *q, *dmp1, *dmq1, *iqmp, *d;
546 n = e = p = q = dmp1 = dmq1 = iqmp = d =
nullptr;
547 DOMNode *child = root->getFirstChild();
548 while (child != NULL)
550 if (
equals(child->getNodeName(),
"Modulus"))
554 else if (
equals(child->getNodeName(),
"Exponent"))
558 else if (
equals(child->getNodeName(),
"P"))
562 else if (
equals(child->getNodeName(),
"Q"))
566 else if (
equals(child->getNodeName(),
"DP"))
570 else if (
equals(child->getNodeName(),
"DQ"))
574 else if (
equals(child->getNodeName(),
"InverseQ"))
578 else if (
equals(child->getNodeName(),
"D"))
582 child = child->getNextSibling();
584 if (n != NULL && e != NULL && e != NULL &&
585 p != NULL && q != NULL && d != NULL &&
586 iqmp != NULL && dmp1 != NULL &&
592 RSA_set0_key(tmpRSA, n, e, d);
593 RSA_set0_factors(tmpRSA, p, q);
594 RSA_set0_crt_params(tmpRSA, dmp1, dmq1, iqmp);
603 root = root->getNextSibling();
632 DOMElement *root = doc->getDocumentElement();
654 *part = BN_bin2bn(decBuff, decLen, NULL);
660 DOMNode *root = node;
663 if (
equals(root->getNodeName(),
"RSAKeyValue"))
665 RSA *tmpRSA = RSA_new();
668 DOMNode *child = root->getFirstChild();
669 while (child != NULL)
671 if (
equals(child->getNodeName(),
"Modulus"))
675 else if (
equals(child->getNodeName(),
"Exponent"))
679 child = child->getNextSibling();
681 if (n != NULL && e != NULL)
686 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
687 RSA_set0_key(
m_pRSA,n,e, NULL );
698 root = root->getNextSibling();
703 #ifndef ONLY_LOCAL_PROXY
714 EVP_PKEY *pubkey = X509_get_pubkey(pCert->
m_pCert);
716 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
717 keyType = EVP_PKEY_id(pubkey);
719 keyType = pubkey->type;
722 if (pubkey == NULL || (keyType != EVP_PKEY_RSA && keyType != EVP_PKEY_RSA2))
725 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
726 r = EVP_PKEY_get1_RSA(pubkey);
728 r = pubkey->pkey.rsa;
731 if (RSA_size(r) != 128)
743 RSA *tmpRSA = RSA_new();
747 BIGNUM* bnE = BN_bin2bn(decBuff, decLen, NULL);
750 BIGNUM* bnN = BN_bin2bn(decBuff, decLen, NULL);
751 if (bnN != NULL && bnE != NULL)
756 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
757 RSA_set0_key(
m_pRSA,bnN,bnE, NULL );
769 #ifdef INTEL_IPP_CRYPTO
770 IppStatus __STDCALL myIppBitSupplier(Ipp32u *pData,
int nBits,
780 const UINT32 runs = 10000;
785 #ifdef INTEL_IPP_CRYPTO
787 ippsRSAGetSize(1024, 512, IppRSAprivate, &size);
788 IppsRSAState *pCtx = (IppsRSAState *)
new UINT8[size];
789 IppStatus ret = ippsRSAInit(1024, 512, IppRSAprivate, pCtx);
790 if (ret != ippStsNoErr)
792 printf(
"Error in RSA init!\n");
795 ippsBigNumGetSize(1, &size);
796 IppsBigNumState *pE = (IppsBigNumState *)(
new UINT8[size]);
797 ippsBigNumInit(1, pE);
798 UINT32 pEValue[] = {0x010001};
799 ret = ippsSet_BN(IppsBigNumPOS, 1, pEValue, pE);
800 if (ret != ippStsNoErr)
802 printf(
"Error in setBN(e)!\n");
805 ippsRSAGenerate(pE, 1024, 512, 1024, pCtx, myIppBitSupplier, NULL);
806 if (ret != ippStsNoErr)
808 printf(
"Error in RSA generate key!\n");
812 ippsBigNumGetSize(32, &size);
813 IppsBigNumState *pY = (IppsBigNumState *)(
new UINT8[size]);
814 ippsBigNumInit(32, pY);
816 IppsBigNumState *pX = (IppsBigNumState *)(
new UINT8[size]);
817 ippsBigNumInit(32, pX);
819 memcpy(pXValue, inBuff, 128);
820 pXValue[0] &= 0x7FFFFFFF;
821 ippsSet_BN(IppsBigNumPOS, 32, pXValue, pX);
831 for (
UINT32 i = 0; i < runs; i++)
833 #ifdef INTEL_IPP_CRYPTO
834 IppStatus ret = ippsRSADecrypt(pX, pY, pCtx);
835 if (ret != ippStsNoErr)
837 printf(
"Error in RSADEcrypt %i!\n", ret);
847 printf(
"CAASymCiper::testSpeed() takes %u ms for %u decrypts!\n", d, runs);
851 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_MIDDLE_MIX
854 XERCES_CPP_NAMESPACE::DOMDocument *docOwner,
855 const char *partName, BIGNUM *part)
858 elemRoot->appendChild(node);
861 BN_bn2bin(part, tmpBuff);
864 DOMText *tmpTextNode =
createDOMText(docOwner, (
const char *
const)tmpBuff);
865 node->appendChild(tmpTextNode);
877 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
878 RSA_get0_key(
m_pRSA,(
const BIGNUM**) &n,(
const BIGNUM**) &e, NULL );
884 addKeyPart(elemRoot, docOwner,
"Exponent", e);
void setRSAFlags(RSA *pRSA)
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
SINT32 getDOMElementValue(const DOMNode *const pElem, UINT8 *value, UINT32 *valuelen)
Returns the content of the text node(s) under elem as null-terminated C String.
bool equals(const XMLCh *const e1, const char *const e2)
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument(const UINT8 *const buff, UINT32 len)
Parses a buffer containing an XML document and returns this document.
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
DOMText * createDOMText(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const text)
Creates a new DOMText with the given value which belongs to the DOMDocument owernDoc.
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
static SINT32 testSpeed()
SINT32 getPublicKeyAsXML(UINT8 *buff, UINT32 *len)
Stores the public key in buff.
SINT32 addKeyPart(DOMElement *elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner, const char *partName, BIGNUM *part)
SINT32 encryptPKCS1(const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
Encrypts one block of plain text using PKCS1 padding.
SINT32 getPublicKeyAsDOMElement(DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
SINT32 setPublicKeyAsDOMNode(DOMNode *node)
SINT32 setPublicKeyAsXML(const UINT8 *buff, UINT32 len)
Sets the public key to the values stored in key.
SINT32 generateKeyPair(UINT32 size)
Generates a new random key-pair of size bits.
SINT32 decryptOAEP(const UINT8 *from, UINT8 *to, UINT32 *len)
Decrypts one OAEP encoded block which is stored in from.
SINT32 encryptOAEP(const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
Encrypts one block of plain text using OAEP padding.
SINT32 decrypt(const UINT8 *from, UINT8 *to)
Decrypts exactly one block which is stored in from.
SINT32 encrypt(const UINT8 *from, UINT8 *to)
Encrypts exactly one block which is stored in from.
SINT32 setPublicKey(const CACertificate *pCert)
Sets the public key which is used for encryption to the contained in the provided certificate.
SINT32 getKeyPart(BIGNUM **part, DOMNode *node)
static SINT32 encode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
static SINT32 decode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
static SINT32 dumpToMem(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs into buff.