1// Copyright (C) 2014 Jeremy Lainé <jeremy.laine@m4x.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5#ifndef QASN1ELEMENT_P_H
6#define QASN1ELEMENT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include <QtCore/qdatetime.h>
21#include <QtCore/qmap.h>
22
23QT_BEGIN_NAMESPACE
24
25// General
26#define RSADSI_OID "1.2.840.113549."
27
28#define RSA_ENCRYPTION_OID QByteArrayLiteral(RSADSI_OID "1.1.1")
29#define DSA_ENCRYPTION_OID QByteArrayLiteral("1.2.840.10040.4.1")
30#define EC_ENCRYPTION_OID QByteArrayLiteral("1.2.840.10045.2.1")
31#define DH_ENCRYPTION_OID QByteArrayLiteral(RSADSI_OID "1.3.1")
32
33// These are mostly from the RFC for PKCS#5
34// PKCS#5: https://tools.ietf.org/html/rfc8018#appendix-B
35#define PKCS5_OID RSADSI_OID "1.5."
36// PKCS#12: https://tools.ietf.org/html/rfc7292#appendix-D)
37#define PKCS12_OID RSADSI_OID "1.12."
38
39// -PBES1
40#define PKCS5_MD2_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "1") // Not (yet) implemented
41#define PKCS5_MD2_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "4") // Not (yet) implemented
42#define PKCS5_MD5_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "3")
43#define PKCS5_MD5_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "6")
44#define PKCS5_SHA1_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "10")
45#define PKCS5_SHA1_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "11")
46#define PKCS12_SHA1_RC4_128_OID QByteArrayLiteral(PKCS12_OID "1.1") // Not (yet) implemented
47#define PKCS12_SHA1_RC4_40_OID QByteArrayLiteral(PKCS12_OID "1.2") // Not (yet) implemented
48#define PKCS12_SHA1_3KEY_3DES_CBC_OID QByteArrayLiteral(PKCS12_OID "1.3")
49#define PKCS12_SHA1_2KEY_3DES_CBC_OID QByteArrayLiteral(PKCS12_OID "1.4")
50#define PKCS12_SHA1_RC2_128_CBC_OID QByteArrayLiteral(PKCS12_OID "1.5")
51#define PKCS12_SHA1_RC2_40_CBC_OID QByteArrayLiteral(PKCS12_OID "1.6")
52
53// -PBKDF2
54#define PKCS5_PBKDF2_ENCRYPTION_OID QByteArrayLiteral(PKCS5_OID "12")
55
56// -PBES2
57#define PKCS5_PBES2_ENCRYPTION_OID QByteArrayLiteral(PKCS5_OID "13")
58
59// Digest
60#define DIGEST_ALGORITHM_OID RSADSI_OID "2."
61// -HMAC-SHA-1
62#define HMAC_WITH_SHA1 QByteArrayLiteral(DIGEST_ALGORITHM_OID "7")
63// -HMAC-SHA-2
64#define HMAC_WITH_SHA224 QByteArrayLiteral(DIGEST_ALGORITHM_OID "8")
65#define HMAC_WITH_SHA256 QByteArrayLiteral(DIGEST_ALGORITHM_OID "9")
66#define HMAC_WITH_SHA384 QByteArrayLiteral(DIGEST_ALGORITHM_OID "10")
67#define HMAC_WITH_SHA512 QByteArrayLiteral(DIGEST_ALGORITHM_OID "11")
68#define HMAC_WITH_SHA512_224 QByteArrayLiteral(DIGEST_ALGORITHM_OID "12")
69#define HMAC_WITH_SHA512_256 QByteArrayLiteral(DIGEST_ALGORITHM_OID "13")
70
71// Encryption algorithms
72#define ENCRYPTION_ALGORITHM_OID RSADSI_OID "3."
73#define DES_CBC_ENCRYPTION_OID QByteArrayLiteral("1.3.14.3.2.7")
74#define DES_EDE3_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "7")
75#define RC2_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "2")
76#define RC5_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "9") // Not (yet) implemented
77#define AES_OID "2.16.840.1.101.3.4.1."
78#define AES128_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "2")
79#define AES192_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "22") // Not (yet) implemented
80#define AES256_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "42") // Not (yet) implemented
81
82class QAsn1Element
83{
84public:
85 enum ElementType {
86 // universal
87 BooleanType = 0x01,
88 IntegerType = 0x02,
89 BitStringType = 0x03,
90 OctetStringType = 0x04,
91 NullType = 0x05,
92 ObjectIdentifierType = 0x06,
93 Utf8StringType = 0x0c,
94 PrintableStringType = 0x13,
95 TeletexStringType = 0x14,
96 UtcTimeType = 0x17,
97 GeneralizedTimeType = 0x18,
98 SequenceType = 0x30,
99 SetType = 0x31,
100
101 // GeneralNameTypes
102 Rfc822NameType = 0x81,
103 DnsNameType = 0x82,
104 UniformResourceIdentifierType = 0x86,
105 IpAddressType = 0x87,
106
107 // context specific
108 Context0Type = 0xA0,
109 Context1Type = 0xA1,
110 Context3Type = 0xA3
111 };
112
113 explicit QAsn1Element(quint8 type = 0, const QByteArray &value = QByteArray());
114 bool read(QDataStream &data);
115 bool read(const QByteArray &data);
116 void write(QDataStream &data) const;
117
118 static QAsn1Element fromBool(bool val);
119 static QAsn1Element fromInteger(unsigned int val);
120 static QAsn1Element fromVector(const QList<QAsn1Element> &items);
121 static QAsn1Element fromObjectId(const QByteArray &id);
122
123 bool toBool(bool *ok = nullptr) const;
124 QDateTime toDateTime() const;
125 QMultiMap<QByteArray, QString> toInfo() const;
126 qint64 toInteger(bool *ok = nullptr) const;
127 QList<QAsn1Element> toList() const;
128 QByteArray toObjectId() const;
129 QByteArray toObjectName() const;
130 QString toString() const;
131
132 quint8 type() const { return mType; }
133 QByteArray value() const { return mValue; }
134
135 friend inline bool operator==(const QAsn1Element &, const QAsn1Element &);
136 friend inline bool operator!=(const QAsn1Element &, const QAsn1Element &);
137
138private:
139 quint8 mType;
140 QByteArray mValue;
141};
142Q_DECLARE_TYPEINFO(QAsn1Element, Q_RELOCATABLE_TYPE);
143
144inline bool operator==(const QAsn1Element &e1, const QAsn1Element &e2)
145{ return e1.mType == e2.mType && e1.mValue == e2.mValue; }
146
147inline bool operator!=(const QAsn1Element &e1, const QAsn1Element &e2)
148{ return e1.mType != e2.mType || e1.mValue != e2.mValue; }
149
150QT_END_NAMESPACE
151
152#endif
153

source code of qtbase/src/plugins/tls/shared/qasn1element_p.h