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

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