29 #if !defined ONLY_LOCAL_PROXY ||defined INCLUDE_MIDDLE_MIX
80 else if(m_pEC != NULL)
82 EC_KEY* tmpEC = EC_KEY_dup(m_pEC);
83 tmpSig->m_pEC = tmpEC;
95 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
97 SINT32 ret=DSA_generate_parameters_ex(
m_pDSA,size,NULL,0,NULL,NULL,NULL);
104 m_pDSA=DSA_generate_parameters(size,NULL,0,NULL,NULL,NULL,NULL);
108 if(DSA_generate_key(
m_pDSA)!=1)
121 EVP_PKEY* pPKey=EVP_PKEY_new();
122 EVP_PKEY_set1_DSA(pPKey,
m_pDSA);
123 PKCS12* pPKCS12=PKCS12_create(NULL,NULL, pPKey,pCert->
m_pCert,NULL,0,0,0,0,0);
126 EVP_PKEY_free(pPKey);
141 const DOMNode* node=n;
147 if(
equals(node->getNodeName(),
"X509PKCS12"))
167 node=node->getNextSibling();
175 if(buff==NULL||
len<1)
185 #if OPENSSL_VERSION_NUMBER > 0x009070CfL
186 PKCS12* tmpPKCS12=d2i_PKCS12(NULL,(
const UINT8**)&buff,
len);
188 PKCS12* tmpPKCS12=d2i_PKCS12(NULL,(
UINT8**)&buff,
len);
192 if(PKCS12_parse(tmpPKCS12,passwd,&key,NULL,NULL)!=1)
194 PKCS12_free(tmpPKCS12);
197 PKCS12_free(tmpPKCS12);
199 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
200 keyType = EVP_PKEY_id(key);
204 if(EVP_PKEY_type(keyType) == EVP_PKEY_DSA)
208 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
209 tmpDSA =
DSA_clone(EVP_PKEY_get1_DSA(key));
214 #if OPENSSL_VERSION_NUMBER < 0x1000204fL
215 if(DSA_sign_setup(tmpDSA,NULL,&tmpDSA->kinv,&tmpDSA->r)!=1)
225 else if(EVP_PKEY_type(keyType) == EVP_PKEY_RSA)
229 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
230 tmpRSA =
RSA_clone(EVP_PKEY_get1_RSA(key));
242 else if(EVP_PKEY_type(keyType) == EVP_PKEY_EC)
246 EC_KEY* tmpECKey = NULL;
247 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
248 tmpECKey = EVP_PKEY_get1_EC_KEY(key);
250 tmpECKey = EC_KEY_dup(key->pkey.ec);
258 CAMsg::printMsg(LOG_ERR,
"Found EC-Key but OpenSSL was built without ECC support!\n");
281 DOMElement* rootKeyInfo=doc->getDocumentElement();
282 if(rootKeyInfo == NULL)
286 if(!
equals(rootKeyInfo->getNodeName(),
"KeyInfo"))
290 DOMNode* elemKeyValue;
297 DSA* tmpDSA=DSA_new();
298 DOMNode* child=elemKeyValue->getFirstChild();
302 BIGNUM* priv_key = NULL;
303 BIGNUM* pub_key = NULL;
307 char* name=XMLString::transcode(child->getNodeName());
308 DOMNode* text=child->getFirstChild();
311 char* tmpStr=XMLString::transcode(text->getNodeValue());
314 XMLString::release(&tmpStr);
315 if(strcmp(name,
"P")==0)
319 p=BN_bin2bn(tbuff,tlen,NULL);
321 else if(strcmp(name,
"Q")==0)
325 q=BN_bin2bn(tbuff,tlen,NULL);
327 else if(strcmp(name,
"G")==0)
331 g=BN_bin2bn(tbuff,tlen,NULL);
333 else if(strcmp(name,
"X")==0)
337 priv_key=BN_bin2bn(tbuff,tlen,NULL);
340 else if(strcmp(name,
"Y")==0)
344 pub_key=BN_bin2bn(tbuff,tlen,NULL);
347 XMLString::release(&name);
348 child=child->getNextSibling();
350 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
351 DSA_set0_pqg(tmpDSA, p, q, g);
352 DSA_set0_key(tmpDSA, pub_key, priv_key);
357 tmpDSA->priv_key = priv_key;
358 tmpDSA->pub_key = pub_key;
359 if(DSA_sign_setup(tmpDSA,NULL,&tmpDSA->kinv,&tmpDSA->r)!=1)
378 DSA_SIG* signature = NULL;
385 DSA_SIG_free(signature);
388 DSA_SIG_free(signature);
393 UINT8 dgst[SHA_DIGEST_LENGTH];
395 return signRSA(dgst, SHA_DIGEST_LENGTH, sig, siglen);
398 else if(m_pEC != NULL)
400 UINT8 dgst[SHA_DIGEST_LENGTH];
402 return signECDSA(dgst, SHA_DIGEST_LENGTH, sig, siglen);
413 *pdsaSig=DSA_do_sign(dgst,SHA_DIGEST_LENGTH,
m_pDSA);
433 const EC_GROUP* tmpGroup = EC_KEY_get0_group(m_pEC);
435 BIGNUM* order = BN_new();
436 EC_GROUP_get_order(tmpGroup, order, NULL);
437 SINT32 size = BN_num_bytes(order) * 2;
634 EVP_PKEY* pPKey=EVP_PKEY_new();
635 EVP_PKEY_set1_DSA(pPKey,
m_pDSA);
637 (*ppCert)->m_pCert=X509_new();
641 X509_set_version((*ppCert)->m_pCert,3);
642 ASN1_TIME* pTime=ASN1_TIME_new();
643 ASN1_TIME_set(pTime,time(NULL));
644 #if OPENSSL_VERSION_NUMBER < 0x10100000L
645 X509_set_notBefore((*ppCert)->m_pCert,pTime);
646 X509_set_notAfter((*ppCert)->m_pCert,pTime);
648 X509_set1_notBefore((*ppCert)->m_pCert,pTime);
649 X509_set1_notAfter((*ppCert)->m_pCert,pTime);
651 X509_set_pubkey((*ppCert)->m_pCert,pPKey);
654 if( (*ppCert)->setSubjectKeyIdentifier() !=
E_SUCCESS )
656 CAMsg::printMsg( LOG_ERR,
"Couldn't add the SKI to the certificate!\n");
660 X509_sign((*ppCert)->m_pCert,pPKey,EVP_sha1());
661 EVP_PKEY_free(pPKey);
670 int l=i2d_DSA_PUBKEY(
m_pDSA,&tmpBuff);
671 SHA1(tmpBuff,l,buff);
672 *
len=SHA_DIGEST_LENGTH;
673 OPENSSL_free(tmpBuff);
707 EVP_PKEY *key=X509_get_pubkey(pCert->
m_pCert);
710 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
711 keyType=EVP_PKEY_id(key);
715 if(EVP_PKEY_type(keyType) == EVP_PKEY_DSA)
718 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
719 tmpDSA=
DSA_clone(EVP_PKEY_get1_DSA(key));
728 if(EVP_PKEY_type(keyType) == EVP_PKEY_RSA)
731 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
732 tmpRSA=
RSA_clone(EVP_PKEY_get1_RSA(key));
741 if(EVP_PKEY_type(keyType) == EVP_PKEY_EC)
744 EC_KEY* tmpECKey = NULL;
745 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
746 tmpECKey = EVP_PKEY_get1_EC_KEY(key);
748 tmpECKey = EC_KEY_dup(key->pkey.ec);
755 CAMsg::printMsg(LOG_ERR,
"Found EC-Key but OpenSSL was built without ECC support!\n");
769 UINT8 decodeBuffer[4096];
780 if(!
equals(xmlKey->getTagName(),
"JapPublicKey")!=0)
782 char* tmpStr=XMLString::transcode(xmlKey->getTagName());
783 CAMsg::printMsg(LOG_DEBUG,
"CASignature::setVerifyKey(): no JapPublicKey! -- Tagname is %s\n", tmpStr);
784 XMLString::release(&tmpStr);
790 strcmp((
char*)decodeBuffer,
"1.0")!=0 )
793 "CASignature::setVerifyKey(): JapPublicKey has unknown version %s. "
794 "Version 1.0 expected!",decodeBuffer);
803 "CASignature::setVerifyKey(): DSAKeyValue not found!");
822 encodedLen =
len;
len = 4096;
828 BIGNUM *pub_key = BN_bin2bn(decodeBuffer,
len,NULL);
844 encodedLen =
len;
len = 4096;
850 BIGNUM *g=BN_bin2bn(decodeBuffer,
len,NULL);
866 encodedLen =
len;
len = 4096;
872 BIGNUM* p=BN_bin2bn(decodeBuffer,
len,NULL);
895 BIGNUM* q=BN_bin2bn(decodeBuffer,
len,NULL);
897 if( pub_key!=NULL && g!=NULL && p!=NULL && q!=NULL)
903 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
904 DSA_set0_pqg(tmpDSA, p, q, g);
905 DSA_set0_key(tmpDSA, pub_key, NULL);
910 tmpDSA->pub_key = pub_key;
922 if(
m_pDSA==NULL||dsaSig==NULL
923 #
if OPENSSL_VERSION_NUMBER < 0x1000204fL
924 ||dsaSig->r==NULL||dsaSig->s==NULL
931 if(DSA_do_verify(dgst,SHA_DIGEST_LENGTH,dsaSig,
m_pDSA)==1)
954 UINT8 dgst[SHA_DIGEST_LENGTH];
957 if(
m_pDSA==NULL||dsaSig==NULL)
960 if((rc=DSA_verify(0, dgst, SHA_DIGEST_LENGTH, dsaSig, sigLen,
m_pDSA))==1)
989 DOMNode* elemSignature=NULL;
991 if(elemSignature==NULL)
993 DOMNode* elemSigValue=NULL;
995 if(elemSigValue==NULL)
997 DOMNode* elemSigInfo=NULL;
999 if(elemSigInfo==NULL)
1001 DOMNode* elemReference=NULL;
1003 if(elemReference==NULL)
1005 DOMNode* elemDigestValue=NULL;
1007 if(elemDigestValue==NULL)
1016 if(dgstlen!=SHA_DIGEST_LENGTH)
1041 DSA_SIG* dsaSig=DSA_SIG_new();
1045 DSA_SIG_free(dsaSig);
1050 DSA_SIG_free(dsaSig);
1054 UINT8 sha1[SHA_DIGEST_LENGTH];
1055 SHA1(out, outlen, sha1);
1059 ret = RSA_verify(NID_sha1, sha1, SHA_DIGEST_LENGTH, tmpSig, tmpSiglen,
m_pRSA);
1064 ret = ECDSA_verify(NID_sha1, sha1, SHA_DIGEST_LENGTH, tmpSig, tmpSiglen, m_pEC);
1075 DOMNode* tmpNode=root->removeChild(elemSignature);
1078 root->appendChild(tmpNode);
1079 UINT8 dgst1[SHA_DIGEST_LENGTH];
1080 SHA1(out,outlen,dgst1);
1083 for(
int i=0;i<SHA_DIGEST_LENGTH;i++)
1085 if(dgst1[i]!=dgst[i])
1100 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
1101 DSA_SIG_get0(pdsaSig,(
const BIGNUM **) &r,(
const BIGNUM **) &s);
1106 rSize = BN_num_bytes(r);
1107 sSize = BN_num_bytes(s);
1108 BN_bn2bin(r,out+20-rSize);
1109 BN_bn2bin(s,out+40-sSize);
1117 ASSERT(pDsaSig!=NULL,
"DSA_SIG is null");
1118 ASSERT(inLen>20,
"Inbuffer is <=20 bytes");
1119 BIGNUM * r = BN_bin2bn(in, 20, NULL);
1120 BIGNUM * s = BN_bin2bn(in+20, inLen-20, NULL);
1121 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
1122 DSA_SIG_set0(pDsaSig,r,s);
1132 if(RSA_sign(NID_sha1, dgst, dgstLen, sig, sigLen,
m_pRSA) != 1)
1148 ECDSA_SIG* ecdsaSig = ECDSA_do_sign(dgst, dgstLen, m_pEC);
1149 if(ecdsaSig == NULL)
1153 memset(sig, 0, *sigLen);
1157 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
1158 ECDSA_SIG_get0(ecdsaSig,(
const BIGNUM **) &r,(
const BIGNUM **) &s);
1166 rSize = BN_num_bytes(r);
1167 sSize = BN_num_bytes(s);
1173 BN_bn2bin(r, sig + rPos);
1174 BN_bn2bin(s, sig + sPos);
1180 ECDSA_SIG_free(ecdsaSig);
1188 UINT8 sha1[SHA_DIGEST_LENGTH];
1189 SHA1(in, inLen, sha1);
1193 ret =
verifyDSA(sha1, SHA_DIGEST_LENGTH, sig, sigLen);
1197 ret =
verifyRSA(sha1, SHA_DIGEST_LENGTH, sig, sigLen);
1202 ret = verifyECDSA(sha1, SHA_DIGEST_LENGTH, sig, sigLen);
1218 return RSA_verify(NID_sha1, dgst, dgstLen, sig, sigLen,
m_pRSA);
1227 DSA_SIG* dsaSig = DSA_SIG_new();
1230 r = BN_bin2bn(sig, 20, r);
1231 s = BN_bin2bn(sig+20, 20, s);
1232 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
1233 DSA_SIG_set0(dsaSig,r,s);
1239 SINT32 ret = DSA_do_verify(dgst, dgstLen, dsaSig,
m_pDSA);
1240 DSA_SIG_free(dsaSig);
1249 ECDSA_SIG* ecdsaSig = ECDSA_SIG_new();
1253 r = BN_bin2bn(sig,
len, r);
1254 s = BN_bin2bn(sig+
len,
len, s);
1255 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
1256 ECDSA_SIG_set0(ecdsaSig,r,s);
1262 SINT32 ret = ECDSA_do_verify(dgst, dgstLen, ecdsaSig, m_pEC);
1263 ECDSA_SIG_free(ecdsaSig);
1289 bool CASignature::isECDSA()
const
void setRSAFlags(RSA *pRSA)
#define ECDSA_SHA1_REFERENCE
#define DSA_SHA1_REFERENCE
#define RSA_SHA1_REFERENCE
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.
SINT32 setDOMElementValue(DOMElement *pElem, SINT32 value)
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.
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.
SINT32 getDOMChildByName(const DOMNode *pNode, const char *const name, DOMElement *&child, bool deep)
SINT32 getDOMElementAttribute(const DOMNode *const elem, const char *attrName, UINT8 *value, UINT32 *len)
DSA * DSA_clone(DSA *dsa)
Clones an OpenSSL DSA structure.
RSA * RSA_clone(RSA *rsa)
Clones an OpenSSL RSA structure.
#define ASSERT(cond, msg)
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 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
SINT32 verify(const UINT8 *const in, UINT32 inlen, DSA_SIG *const dsaSig) const
SINT32 verifyDSA(const UINT8 *dgst, const UINT32 dgstLen, UINT8 *sig, UINT32 sigLen) const
SINT32 getVerifyKey(CACertificate **)
Get the public key as XML encoded X509 certificate.
SINT32 verifyRSA(const UINT8 *dgst, const UINT32 dgstLen, UINT8 *sig, UINT32 sigLen) const
SINT32 verifyDER(UINT8 *in, UINT32 inlen, const UINT8 *dsaSig, const UINT32 sigLen)
Verifies an ASN.1 DER encoded SHA1-DSA signature.
SINT32 encodeRS(UINT8 *out, UINT32 *outLen, const DSA_SIG *const pdsaSig) const
UINT8 * getSignatureMethod()
SINT32 generateSignKey(UINT32 size)
SINT32 signRSA(const UINT8 *dgst, const UINT32 dgstLen, UINT8 *sig, UINT32 *sigLen) const
SINT32 decodeRS(const UINT8 *const in, const UINT32 inLen, DSA_SIG *pDsaSig) const
Converts a DSA signature from the XML Signature format to the openSSL R/S BigNumber format.
SINT32 setSignKey(const UINT8 *buff, UINT32 len, UINT32 type, const char *passwd=NULL)
SINT32 parseSignKeyXML(const UINT8 *buff, UINT32 len)
SINT32 getSignatureSize() const
SINT32 setVerifyKey(CACertificate *pCert)
Set the key for signature testing to the one include in pCert.
SINT32 verifyXML(DOMNode *node, CACertStore *pTrustedCerts=NULL)
Verifies a XML Signature under node root.
SINT32 getVerifyKeyHash(UINT8 *buff, UINT32 *len)
Calculates a SHA hash of the public key, which is represented as SubjectPublicKeyInfo.
SINT32 sign(const UINT8 *const in, UINT32 inlen, UINT8 *sig, UINT32 *siglen) const
Perform Signature with either DSA, RSA or ECDSA.
SINT32 getSignKey(DOMElement *&node, XERCES_CPP_NAMESPACE::DOMDocument *doc)
Gets the secret sign key as XML encode PKCS#12 struct.
static SINT32 makeCanonical(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs in a 'cannonical form' into buff.