1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_CERT_X509_CERT_TYPES_H_
6#define NET_CERT_X509_CERT_TYPES_H_
7
8#include <stddef.h>
9#include <string.h>
10
11#include <map>
12#include <set>
13#include <string>
14#include <vector>
15
16#include "base/logging.h"
17#include "base/strings/string_piece.h"
18#include "build/build_config.h"
19#include "net/base/hash_value.h"
20#include "net/base/net_export.h"
21#include "net/cert/cert_status_flags.h"
22
23namespace base {
24class Time;
25} // namespace base
26
27namespace net {
28
29// CertPrincipal represents the issuer or subject field of an X.509 certificate.
30struct NET_EXPORT CertPrincipal {
31 CertPrincipal();
32 explicit CertPrincipal(const std::string& name);
33 ~CertPrincipal();
34
35 // Configures handling of PrintableString values in the DistinguishedName. Do
36 // not use non-default handling without consulting //net owners. With
37 // kAsUTF8Hack, PrintableStrings are interpreted as UTF-8 strings.
38 enum class PrintableStringHandling { kDefault, kAsUTF8Hack };
39
40 // Parses a BER-format DistinguishedName.
41 // TODO(mattm): change this to take a der::Input.
42 bool ParseDistinguishedName(
43 const void* ber_name_data,
44 size_t length,
45 PrintableStringHandling printable_string_handling =
46 PrintableStringHandling::kDefault);
47
48 // Returns a name that can be used to represent the issuer. It tries in this
49 // order: CN, O and OU and returns the first non-empty one found.
50 std::string GetDisplayName() const;
51
52 // The different attributes for a principal, stored in UTF-8. They may be "".
53 // Note that some of them can have several values.
54
55 std::string common_name;
56 std::string locality_name;
57 std::string state_or_province_name;
58 std::string country_name;
59
60 std::vector<std::string> street_addresses;
61 std::vector<std::string> organization_names;
62 std::vector<std::string> organization_unit_names;
63 std::vector<std::string> domain_components;
64};
65
66// A list of ASN.1 date/time formats that ParseCertificateDate() supports,
67// encoded in the canonical forms specified in RFC 2459/3280/5280.
68enum CertDateFormat {
69 // UTCTime: Format is YYMMDDHHMMSSZ
70 CERT_DATE_FORMAT_UTC_TIME,
71
72 // GeneralizedTime: Format is YYYYMMDDHHMMSSZ
73 CERT_DATE_FORMAT_GENERALIZED_TIME,
74};
75
76// Attempts to parse |raw_date|, an ASN.1 date/time string encoded as
77// |format|, and writes the result into |*time|. If an invalid date is
78// specified, or if parsing fails, returns false, and |*time| will not be
79// updated.
80NET_EXPORT_PRIVATE bool ParseCertificateDate(const base::StringPiece& raw_date,
81 CertDateFormat format,
82 base::Time* time);
83} // namespace net
84
85#endif // NET_CERT_X509_CERT_TYPES_H_
86