This source file includes following definitions.
- GetAttributeValue
- AddTypeValuePair
- SetSingle
- ParseDistinguishedName
#include "net/cert/x509_cert_types.h"
#include <windows.h>
#include <wincrypt.h>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "crypto/capi_util.h"
#pragma comment(lib, "crypt32.lib")
namespace net {
namespace {
const char* kOIDs[] = {
szOID_COMMON_NAME,
szOID_LOCALITY_NAME,
szOID_STATE_OR_PROVINCE_NAME,
szOID_COUNTRY_NAME,
szOID_STREET_ADDRESS,
szOID_ORGANIZATION_NAME,
szOID_ORGANIZATIONAL_UNIT_NAME,
szOID_DOMAIN_COMPONENT
};
bool GetAttributeValue(PCERT_RDN_ATTR attribute,
std::string* value) {
DWORD chars_needed = CertRDNValueToStrW(attribute->dwValueType,
&attribute->Value, NULL, 0);
if (chars_needed == 0)
return false;
if (chars_needed == 1) {
value->clear();
return true;
}
std::wstring wide_name;
DWORD chars_written = CertRDNValueToStrW(
attribute->dwValueType, &attribute->Value,
WriteInto(&wide_name, chars_needed), chars_needed);
if (chars_written <= 1)
return false;
wide_name.resize(chars_written - 1);
*value = base::WideToUTF8(wide_name);
return true;
}
bool AddTypeValuePair(PCERT_RDN_ATTR attribute,
std::vector<std::string>* values[]) {
for (size_t oid = 0; oid < arraysize(kOIDs); ++oid) {
if (strcmp(attribute->pszObjId, kOIDs[oid]) == 0) {
std::string value;
if (!GetAttributeValue(attribute, &value))
return false;
values[oid]->push_back(value);
break;
}
}
return true;
}
void SetSingle(const std::vector<std::string>& values,
std::string* single_value) {
LOG_IF(WARNING, values.size() > 1) << "Didn't expect multiple values";
if (!values.empty())
*single_value = values[0];
}
}
bool CertPrincipal::ParseDistinguishedName(const void* ber_name_data,
size_t length) {
DCHECK(ber_name_data);
CRYPT_DECODE_PARA decode_para;
decode_para.cbSize = sizeof(decode_para);
decode_para.pfnAlloc = crypto::CryptAlloc;
decode_para.pfnFree = crypto::CryptFree;
CERT_NAME_INFO* name_info = NULL;
DWORD name_info_size = 0;
BOOL rv;
rv = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
X509_NAME,
reinterpret_cast<const BYTE*>(ber_name_data),
length,
CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG,
&decode_para,
&name_info, &name_info_size);
if (!rv)
return false;
scoped_ptr<CERT_NAME_INFO, base::FreeDeleter> scoped_name_info(name_info);
std::vector<std::string> common_names, locality_names, state_names,
country_names;
std::vector<std::string>* values[] = {
&common_names, &locality_names,
&state_names, &country_names,
&this->street_addresses,
&this->organization_names,
&this->organization_unit_names,
&this->domain_components
};
DCHECK(arraysize(kOIDs) == arraysize(values));
for (DWORD cur_rdn = 0; cur_rdn < name_info->cRDN; ++cur_rdn) {
PCERT_RDN rdn = &name_info->rgRDN[cur_rdn];
for (DWORD cur_ava = 0; cur_ava < rdn->cRDNAttr; ++cur_ava) {
PCERT_RDN_ATTR ava = &rdn->rgRDNAttr[cur_ava];
if (!AddTypeValuePair(ava, values))
return false;
}
}
SetSingle(common_names, &this->common_name);
SetSingle(locality_names, &this->locality_name);
SetSingle(state_names, &this->state_or_province_name);
SetSingle(country_names, &this->country_name);
return true;
}
}