1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Jeremy Lainé <jeremy.laine@m4x.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 
41 #ifndef QASN1ELEMENT_P_H
42 #define QASN1ELEMENT_P_H
43 
44 //
45 //  W A R N I N G
46 //  -------------
47 //
48 // This file is not part of the Qt API. It exists purely as an
49 // implementation detail. This header file may change from version to
50 // version without notice, or even be removed.
51 //
52 // We mean it.
53 //
54 
55 #include <QtNetwork/private/qtnetworkglobal_p.h>
56 #include <QtCore/qdatetime.h>
57 #include <QtCore/qmap.h>
58 
59 QT_BEGIN_NAMESPACE
60 
61 // General
62 #define RSADSI_OID "1.2.840.113549."
63 
64 #define RSA_ENCRYPTION_OID QByteArrayLiteral(RSADSI_OID "1.1.1")
65 #define DSA_ENCRYPTION_OID QByteArrayLiteral("1.2.840.10040.4.1")
66 #define EC_ENCRYPTION_OID QByteArrayLiteral("1.2.840.10045.2.1")
67 #define DH_ENCRYPTION_OID QByteArrayLiteral(RSADSI_OID "1.3.1")
68 
69 // These are mostly from the RFC for PKCS#5
70 // PKCS#5: https://tools.ietf.org/html/rfc8018#appendix-B
71 #define PKCS5_OID RSADSI_OID "1.5."
72 // PKCS#12: https://tools.ietf.org/html/rfc7292#appendix-D)
73 #define PKCS12_OID RSADSI_OID "1.12."
74 
75 // -PBES1
76 #define PKCS5_MD2_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "1") // Not (yet) implemented
77 #define PKCS5_MD2_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "4") // Not (yet) implemented
78 #define PKCS5_MD5_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "3")
79 #define PKCS5_MD5_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "6")
80 #define PKCS5_SHA1_DES_CBC_OID QByteArrayLiteral(PKCS5_OID "10")
81 #define PKCS5_SHA1_RC2_CBC_OID QByteArrayLiteral(PKCS5_OID "11")
82 #define PKCS12_SHA1_RC4_128_OID QByteArrayLiteral(PKCS12_OID "1.1") // Not (yet) implemented
83 #define PKCS12_SHA1_RC4_40_OID QByteArrayLiteral(PKCS12_OID "1.2") // Not (yet) implemented
84 #define PKCS12_SHA1_3KEY_3DES_CBC_OID QByteArrayLiteral(PKCS12_OID "1.3")
85 #define PKCS12_SHA1_2KEY_3DES_CBC_OID QByteArrayLiteral(PKCS12_OID "1.4")
86 #define PKCS12_SHA1_RC2_128_CBC_OID QByteArrayLiteral(PKCS12_OID "1.5")
87 #define PKCS12_SHA1_RC2_40_CBC_OID QByteArrayLiteral(PKCS12_OID "1.6")
88 
89 // -PBKDF2
90 #define PKCS5_PBKDF2_ENCRYPTION_OID QByteArrayLiteral(PKCS5_OID "12")
91 
92 // -PBES2
93 #define PKCS5_PBES2_ENCRYPTION_OID QByteArrayLiteral(PKCS5_OID "13")
94 
95 // Digest
96 #define DIGEST_ALGORITHM_OID RSADSI_OID "2."
97 // -HMAC-SHA-1
98 #define HMAC_WITH_SHA1 QByteArrayLiteral(DIGEST_ALGORITHM_OID "7")
99 // -HMAC-SHA-2
100 #define HMAC_WITH_SHA224 QByteArrayLiteral(DIGEST_ALGORITHM_OID "8")
101 #define HMAC_WITH_SHA256 QByteArrayLiteral(DIGEST_ALGORITHM_OID "9")
102 #define HMAC_WITH_SHA384 QByteArrayLiteral(DIGEST_ALGORITHM_OID "10")
103 #define HMAC_WITH_SHA512 QByteArrayLiteral(DIGEST_ALGORITHM_OID "11")
104 #define HMAC_WITH_SHA512_224 QByteArrayLiteral(DIGEST_ALGORITHM_OID "12")
105 #define HMAC_WITH_SHA512_256 QByteArrayLiteral(DIGEST_ALGORITHM_OID "13")
106 
107 // Encryption algorithms
108 #define ENCRYPTION_ALGORITHM_OID RSADSI_OID "3."
109 #define DES_CBC_ENCRYPTION_OID QByteArrayLiteral("1.3.14.3.2.7")
110 #define DES_EDE3_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "7")
111 #define RC2_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "2")
112 #define RC5_CBC_ENCRYPTION_OID QByteArrayLiteral(ENCRYPTION_ALGORITHM_OID "9") // Not (yet) implemented
113 #define AES_OID "2.16.840.1.101.3.4.1."
114 #define AES128_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "2")
115 #define AES192_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "22") // Not (yet) implemented
116 #define AES256_CBC_ENCRYPTION_OID QByteArrayLiteral(AES_OID "42") // Not (yet) implemented
117 
118 class Q_AUTOTEST_EXPORT QAsn1Element
119 {
120 public:
121     enum ElementType {
122         // universal
123         BooleanType = 0x01,
124         IntegerType  = 0x02,
125         BitStringType  = 0x03,
126         OctetStringType = 0x04,
127         NullType = 0x05,
128         ObjectIdentifierType = 0x06,
129         Utf8StringType = 0x0c,
130         PrintableStringType = 0x13,
131         TeletexStringType = 0x14,
132         UtcTimeType = 0x17,
133         GeneralizedTimeType = 0x18,
134         SequenceType = 0x30,
135         SetType = 0x31,
136 
137         // GeneralNameTypes
138         Rfc822NameType = 0x81,
139         DnsNameType = 0x82,
140         UniformResourceIdentifierType = 0x86,
141         IpAddressType = 0x87,
142 
143         // context specific
144         Context0Type = 0xA0,
145         Context1Type = 0xA1,
146         Context3Type = 0xA3
147     };
148 
149     explicit QAsn1Element(quint8 type = 0, const QByteArray &value = QByteArray());
150     bool read(QDataStream &data);
151     bool read(const QByteArray &data);
152     void write(QDataStream &data) const;
153 
154     static QAsn1Element fromBool(bool val);
155     static QAsn1Element fromInteger(unsigned int val);
156     static QAsn1Element fromVector(const QVector<QAsn1Element> &items);
157     static QAsn1Element fromObjectId(const QByteArray &id);
158 
159     bool toBool(bool *ok = nullptr) const;
160     QDateTime toDateTime() const;
161     QMultiMap<QByteArray, QString> toInfo() const;
162     qint64 toInteger(bool *ok = nullptr) const;
163     QVector<QAsn1Element> toVector() const;
164     QByteArray toObjectId() const;
165     QByteArray toObjectName() const;
166     QString toString() const;
167 
type()168     quint8 type() const { return mType; }
value()169     QByteArray value() const { return mValue; }
170 
171     friend inline bool operator==(const QAsn1Element &, const QAsn1Element &);
172     friend inline bool operator!=(const QAsn1Element &, const QAsn1Element &);
173 
174 private:
175     quint8 mType;
176     QByteArray mValue;
177 };
178 Q_DECLARE_TYPEINFO(QAsn1Element, Q_MOVABLE_TYPE);
179 
180 inline bool operator==(const QAsn1Element &e1, const QAsn1Element &e2)
181 { return e1.mType == e2.mType && e1.mValue == e2.mValue; }
182 
183 inline bool operator!=(const QAsn1Element &e1, const QAsn1Element &e2)
184 { return e1.mType != e2.mType || e1.mValue != e2.mValue; }
185 
186 QT_END_NAMESPACE
187 
188 #endif
189