⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsseapi.c

📁 一款开源的soap库
💻 C
📖 第 1 页 / 共 5 页
字号:
soap_wsse_verify_X509(struct soap *soap, X509 *cert){ struct soap_wsse_data *data = (struct soap_wsse_data*)soap_lookup_plugin(soap, soap_wsse_id);  X509_STORE_CTX *verify;  DBGFUN("soap_wsse_verify_X509");  if (!data)    return soap_set_receiver_error(soap, "soap_wsse_sign", "Plugin not registered", SOAP_PLUGIN_ERROR);  if (!data->store)  { if (!(data->store = X509_STORE_new()))      return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not create X509_STORE object");    DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Setting up a new X509 store\n"));    X509_STORE_set_verify_cb_func(data->store, soap->fsslverify);    if (soap->cafile || soap->capath)    { if (X509_STORE_load_locations(data->store, soap->cafile, soap->capath) != 1)        return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not load CA file or path");    }#if (OPENSSL_VERSION_NUMBER > 0x00907000L)    if (soap->crlfile)    { X509_LOOKUP *lookup;      if (!(lookup = X509_STORE_add_lookup(data->store, X509_LOOKUP_file())))        return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not create X509_LOOKUP object");      if (X509_load_crl_file(lookup, soap->crlfile, X509_FILETYPE_PEM) != 1)        return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not read the CRL file");      X509_STORE_set_flags(data->store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);    }#endif  }  if (!(verify = X509_STORE_CTX_new()))    return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not create X509_STORE_CTX object");#if (OPENSSL_VERSION_NUMBER > 0x00907000L)  if (X509_STORE_CTX_init(verify, data->store, cert, NULL) != 1)  { X509_STORE_CTX_free(verify);    return soap_receiver_fault(soap, "soap_wsse_verify_X509", "Could not initialize X509_STORE_CTX object");  }#else  X509_STORE_CTX_init(verify, data->store, cert, NULL);#endif  if (X509_verify_cert(verify) != 1)  { X509_STORE_CTX_free(verify);    return soap_sender_fault(soap, "soap_wsse_verify_X509", "Invalid certificate");  }  DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Certificate is valid\n"));  return SOAP_OK;}/******************************************************************************\ * * ds:Signature/SignedInfo *\******************************************************************************//**@fn ds__SignedInfoType* soap_wsse_add_SignedInfo(struct soap *soap)@brief Adds SignedInfo element.@param soap context@return ds__SignedInfoType object*/struct ds__SignedInfoType*soap_wsse_add_SignedInfo(struct soap *soap){ ds__SignatureType *signature = soap_wsse_add_Signature(soap);  if (!signature->SignedInfo)  { signature->SignedInfo = (ds__SignedInfoType*)soap_malloc(soap, sizeof(ds__SignedInfoType));    soap_default_ds__SignedInfoType(soap, signature->SignedInfo);  }  return signature->SignedInfo;}/**@fn int soap_wsse_add_SignedInfo_Reference(struct soap *soap, const char *URI, const char *transform, const char *inclusiveNamespaces, const char *HA)@brief Adds SignedInfo element with Reference URI, transform algorithm used, and digest value.@param soap context@param[in] URI reference@param[in] transform string should be c14n_URI for exc-c14n or NULL@param[in] inclusiveNamespaces used by the exc-c14n transform or NULL@param[in] HA is the SHA1 digest in binary form (length=SOAP_SMD_SHA1_SIZE)@return SOAP_OK or SOAP_EOM when references exceed SOAP_WSSE_MAX_REFThis function can be called to add more references to the wsse:SignedInfoelement. A maximum number of SOAP_WSSE_MAX_REF references can be added. Thedigest method is always SHA1. Note: XPath transforms cannot be specified in this release.*/intsoap_wsse_add_SignedInfo_Reference(struct soap *soap, const char *URI, const char *transform, const char *inclusiveNamespaces, const char *HA){ ds__SignedInfoType *signedInfo = soap_wsse_add_SignedInfo(soap);  ds__ReferenceType *reference;  DBGFUN2("soap_wsse_add_SignedInfo_Reference", "URI=%s", URI?URI:"", "transform=%s", transform?transform:"");  /* if this is the first reference, allocate SOAP_WSSE_MAX_REF references */  if (signedInfo->__sizeReference == 0)    signedInfo->Reference = (ds__ReferenceType**)soap_malloc(soap, SOAP_WSSE_MAX_REF*sizeof(ds__ReferenceType*));  else  { /* maximum number of references exceeded? */    if (signedInfo->__sizeReference >= SOAP_WSSE_MAX_REF)      return SOAP_EOM;  }  /* allocate fresh new reference */  reference = (ds__ReferenceType*)soap_malloc(soap, sizeof(ds__ReferenceType));  soap_default_ds__ReferenceType(soap, reference);  /* populate the URI */  reference->URI = soap_strdup(soap, URI);  /* if a transform algorithm was used, populate the Transforms element */  if (transform)  { reference->Transforms = (ds__TransformsType*)soap_malloc(soap, sizeof(ds__TransformsType));    soap_default_ds__TransformsType(soap, reference->Transforms);    /* only one transform */    reference->Transforms->__sizeTransform = 1;    reference->Transforms->Transform = (ds__TransformType*)soap_malloc(soap, sizeof(ds__TransformType));    soap_default_ds__TransformType(soap, reference->Transforms->Transform);    reference->Transforms->Transform->Algorithm = (char*)transform;    /* populate the c14n:InclusiveNamespaces element */    if (inclusiveNamespaces && *inclusiveNamespaces)    { reference->Transforms->Transform->c14n__InclusiveNamespaces = (_c14n__InclusiveNamespaces*)soap_malloc(soap, sizeof(_c14n__InclusiveNamespaces));      soap_default__c14n__InclusiveNamespaces(soap, reference->Transforms->Transform->c14n__InclusiveNamespaces);      reference->Transforms->Transform->c14n__InclusiveNamespaces->PrefixList = soap_strdup(soap, inclusiveNamespaces);    }  }  /* populate the DigestMethod element */  reference->DigestMethod = (ds__DigestMethodType*)soap_malloc(soap, sizeof(ds__DigestMethodType));  soap_default_ds__DigestMethodType(soap, reference->DigestMethod);  /* the DigestMethod algorithm is always SHA1 */  reference->DigestMethod->Algorithm = (char*)ds_sha1URI;  /* populate the DigestValue element */  reference->DigestValue = soap_s2base64(soap, (unsigned char*)HA, NULL, SOAP_SMD_SHA1_SIZE);  /* add the fresh new reference to the array */  signedInfo->Reference[signedInfo->__sizeReference] = reference;  signedInfo->__sizeReference++;  return SOAP_OK;}/**@fn int soap_wsse_add_SignedInfo_SignatureMethod(struct soap *soap, const char *method, int canonical)@brief Adds SignedInfo element with SignatureMethod.@param soap context@param[in] method is the URI of the signature algorithm (e.g. ds_rsa_sha1)@param[in] canonical flag indicating that SignedInfo is signed in exc-c14n form@return SOAP_OKNote: the c14n:InclusiveNamespaces/PrefixList is set to "SOAP-ENV wsse".*/intsoap_wsse_add_SignedInfo_SignatureMethod(struct soap *soap, const char *method, int canonical){ ds__SignedInfoType *signedInfo = soap_wsse_add_SignedInfo(soap);  DBGFUN2("soap_wsse_add_SignedInfo_SignatureMethod", "method=%s", method?method:"", "canonical=%d", canonical);  /* if signed in exc-c14n form, populate CanonicalizationMethod element */  if (canonical)  { signedInfo->CanonicalizationMethod = (ds__CanonicalizationMethodType*)soap_malloc(soap, sizeof(ds__CanonicalizationMethodType));    soap_default_ds__CanonicalizationMethodType(soap, signedInfo->CanonicalizationMethod);    signedInfo->CanonicalizationMethod->Algorithm = (char*)c14n_URI;    /* TODO: check c14n:InclusiveNamespaces/PrefixList requirements. It seems     * that the WS-Security spec is at odds with the EXC C14N spec on this     * issue?     *    signedInfo->CanonicalizationMethod->c14n__InclusiveNamespaces = (_c14n__InclusiveNamespaces*)soap_malloc(soap, sizeof(_c14n__InclusiveNamespaces));    soap_default__c14n__InclusiveNamespaces(soap, signedInfo->CanonicalizationMethod->c14n__InclusiveNamespaces);    signedInfo->CanonicalizationMethod->c14n__InclusiveNamespaces->PrefixList = "SOAP-ENV wsse";    */  }  /* populate SignatureMethod element */  signedInfo->SignatureMethod = (ds__SignatureMethodType*)soap_malloc(soap, sizeof(ds__SignatureMethodType));  soap_default_ds__SignatureMethodType(soap, signedInfo->SignatureMethod);  signedInfo->SignatureMethod->Algorithm = (char*)method;  return SOAP_OK;}/**@fn ds__SignedInfoType* soap_wsse_SignedInfo(struct soap *soap)@brief Returns SignedInfo element if present.@param soap context@return ds__SignedInfoType object or NULL*/struct ds__SignedInfoType*soap_wsse_SignedInfo(struct soap *soap){ ds__SignatureType *signature = soap_wsse_Signature(soap);  if (signature)    return signature->SignedInfo;  return NULL;}/**@fn int soap_wsse_get_SignedInfo_SignatureMethod(struct soap *soap, int *alg)@brief Get SignatureMethod algorithm@param soap context@param[out] alg is SOAP_SMD_HMAC_SHA1, SOAP_SMD_VRFY_DSA_SHA1, or SOAP_SMD_VRFY_RSA_SHA1@return SOAP_OK or SOAP_FAULT with wsse:UnsupportedAlgorithm or wsse:FailedCheck fault*/intsoap_wsse_get_SignedInfo_SignatureMethod(struct soap *soap, int *alg){ ds__SignedInfoType *signedInfo = soap_wsse_SignedInfo(soap);  DBGFUN("soap_wsse_get_SignedInfo_SignatureMethod");  *alg = SOAP_SMD_NONE;  /* if we have a SignedInfo element, get the algorithm */  if (signedInfo   && signedInfo->SignatureMethod   && signedInfo->SignatureMethod->Algorithm)  { const char *method = signedInfo->SignatureMethod->Algorithm;    if (!strcmp(method, ds_hmac_sha1URI))      *alg = SOAP_SMD_HMAC_SHA1;    else if (!strcmp(method, ds_dsa_sha1URI))      *alg = SOAP_SMD_VRFY_DSA_SHA1;    else if (!strcmp(method, ds_rsa_sha1URI))      *alg = SOAP_SMD_VRFY_RSA_SHA1;    else      return soap_wsse_fault(soap, wsse__UnsupportedAlgorithm, method);    return SOAP_OK;  }  return soap_wsse_fault(soap, wsse__FailedCheck, "Signature required");}/******************************************************************************\ * * ds:Signature/SignatureValue *\******************************************************************************//**@fn int soap_wsse_add_SignatureValue(struct soap *soap, int alg, const void *key, int keylen)@brief Adds SignedInfo/SignatureMethod element, signs the SignedInfo element, and adds the resulting SignatureValue element.@param soap context@param[in] alg is SOAP_SMD_HMAC_SHA1, SOAP_SMD_SIGN_DSA_SHA1, or SOAP_SMD_SIGN_RSA_SHA1@param[in] key to use to sign (HMAC or EVP_PKEY)@param[in] keylen length of HMAC key@return SOAP_OK, SOAP_EOM, or faultTo sign the SignedInfo element with this function, populate SignedInfo withReference elements first using soap_wsse_add_SignedInfo_Reference. TheSignedInfo element must not be modified after signing.The SOAP_XML_INDENT and SOAP_XML_CANONICAL flags are used to serialize theSignedInfo to compute the signature.*/intsoap_wsse_add_SignatureValue(struct soap *soap, int alg, const void *key, int keylen){ ds__SignatureType *signature = soap_wsse_add_Signature(soap);  const char *c14nexclude, *method = NULL;  char *sig;  int siglen;  DBGFUN1("soap_wsse_add_SignatureValue", "alg=%d", alg);  /* determine signature algorithm to use */  switch (alg)  { case SOAP_SMD_HMAC_SHA1:      method = ds_hmac_sha1URI;      break;    case SOAP_SMD_SIGN_DSA_SHA1:      method = ds_dsa_sha1URI;      break;    case SOAP_SMD_SIGN_RSA_SHA1:      method = ds_rsa_sha1URI;      break;    default:      return soap_wsse_fault(soap, wsse__UnsupportedAlgorithm, NULL);  }  /* populate SignedInfo/SignatureMethod based on SOAP_XML_CANONICAL flag */  soap_wsse_add_SignedInfo_SignatureMethod(soap, method, (soap->mode & SOAP_XML_CANONICAL));  /* use the gSOAP engine's look-aside buffer to temporarily hold the sig */  if (soap_store_lab(soap, NULL, soap_smd_size(alg, key)))    return SOAP_EOM;  sig = soap->labbuf;  /* we will serialize SignedInfo as it appears exactly in the SOAP Header */  /* set indent level for XML SignedInfo as it appears in the SOAP Header */  soap->level = 4;  /* with SOAP_XML_CANONICAL flag, exclude the "ds" prefix */  c14nexclude = soap->c14nexclude;  soap->c14nexclude = "ds";  /* use smdevp engine to sign SignedInfo */  if (soap_smd_begin(soap, alg, key, keylen)   || soap_out_ds__SignedInfoType(soap, "ds:SignedInfo", 0, signature->SignedInfo, NULL)   || soap_smd_end(soap, sig, &siglen))    return soap_wsse_fault(soap, wsse__InvalidSecurity, "Could not sign");  /* restore c14nexclude */  soap->c14nexclude = c14nexclude;  /* populate the SignatureValue element */  signature->SignatureValue = soap_s2base64(soap, (unsigned char*)sig, NULL, siglen);  return SOAP_OK;}/**@fn int soap_wsse_verify_SignatureValue(struct soap *soap, int alg, const void *key, int keylen)@brief Verifies the SignatureValue of a SignedInfo element.@param soap context@param[in] alg is SOAP_SMD_HMAC_SHA1, SOAP_SMD_VRFY_DSA_SHA1, or SOAP_SMD_VRFY_RSA_SHA1 determined by the SignedInfo/SignatureMethod@param[in] key to use to verify (HMAC or EVP_PKEY)@param[in] keylen length of HMAC key@return SOAP_OK, SOAP_EOM, or faultThis function searches for the SignedInfo element in the soap->dom DOM tree toverify the signature in the SignatureValue element. Using the DOM ensures wewill verify the signature of a SignedInfo as it was exactly received by theparser, by using the -DWITH_DOM compile flag and SOAP_XML_DOM runtime flag. Ifthere is no DOM, it verifies the signature of the deserialized SignedInfoelement in the SOAP Header. However, serializing deserialized data may changethe octet stream that was signed, unless we're using gSOAP as producers andconsumers (with the SOAP_XML_INDENT flag reset).*/intsoap_wsse_verify_SignatureValue(struct soap *soap, int alg, const void *key, int keylen){ ds__SignatureType *signature = soap_wsse_Signature(soap);  DBGFUN1("soap_wsse_verify_SignatureValue", "alg=%d", alg);  /* always need an HMAC secret key or DSA/RSA public key to verify */  if (!key)    return soap_wsse_fault(soap, wsse__SecurityTokenUnavailable, NULL);  /* verify the SignedInfo element with the SignatureValue element */  if (signature   && signature->SignedInfo   && signature->SignatureValue)  { char *sig;    const char *sigval;    int method, siglen, sigvallen;    /* check that we are using the intended signature algorithm */    if (soap_wsse_get_SignedInfo_SignatureMethod(soap, &method))      return soap->error;    if (alg != method)      return soap_wsse_fault(soap, wsse__FailedCheck, "Incorrect signature algorithm");    /* retrieve the signature */    sigval = soap_base642s(soap, signature->SignatureValue, NULL, 0, &sigvallen);    /* search the DOM for SignedInfo */    if (soap->dom)    { struct soap_dom_element *elt;      /* traverse the DOM whi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -