1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtlskey_base_p.h"
5#include "qasn1element_p.h"
6
7QT_BEGIN_NAMESPACE
8
9namespace QTlsPrivate {
10
11QByteArray TlsKeyBase::pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const
12{
13 QByteArray pem(der.toBase64());
14
15 const int lineWidth = 64; // RFC 1421
16 const int newLines = pem.size() / lineWidth;
17 const bool rem = pem.size() % lineWidth;
18
19 for (int i = 0; i < newLines; ++i)
20 pem.insert(i: (i + 1) * lineWidth + i, c: '\n');
21 if (rem)
22 pem.append(c: '\n');
23
24 QByteArray extra;
25 if (!headers.isEmpty()) {
26 QMap<QByteArray, QByteArray>::const_iterator it = headers.constEnd();
27 do {
28 --it;
29 extra += it.key() + ": " + it.value() + '\n';
30 } while (it != headers.constBegin());
31 extra += '\n';
32 }
33
34 if (isEncryptedPkcs8(der)) {
35 pem.prepend(a: pkcs8Header(encrypted: true) + '\n' + extra);
36 pem.append(a: pkcs8Footer(encrypted: true) + '\n');
37 } else if (isPkcs8()) {
38 pem.prepend(a: pkcs8Header(encrypted: false) + '\n' + extra);
39 pem.append(a: pkcs8Footer(encrypted: false) + '\n');
40 } else {
41 pem.prepend(a: pemHeader() + '\n' + extra);
42 pem.append(a: pemFooter() + '\n');
43 }
44
45 return pem;
46}
47
48QByteArray TlsKeyBase::pkcs8Header(bool encrypted)
49{
50 return encrypted
51 ? QByteArrayLiteral("-----BEGIN ENCRYPTED PRIVATE KEY-----")
52 : QByteArrayLiteral("-----BEGIN PRIVATE KEY-----");
53}
54
55QByteArray TlsKeyBase::pkcs8Footer(bool encrypted)
56{
57 return encrypted
58 ? QByteArrayLiteral("-----END ENCRYPTED PRIVATE KEY-----")
59 : QByteArrayLiteral("-----END PRIVATE KEY-----");
60}
61
62bool TlsKeyBase::isEncryptedPkcs8(const QByteArray &der)
63{
64 static const QList<QByteArray> pbes1OIds {
65 // PKCS5
66 { PKCS5_MD2_DES_CBC_OID }, { PKCS5_MD2_RC2_CBC_OID }, { PKCS5_MD5_DES_CBC_OID },
67 { PKCS5_MD5_RC2_CBC_OID }, { PKCS5_SHA1_DES_CBC_OID }, { PKCS5_SHA1_RC2_CBC_OID },
68 };
69 QAsn1Element elem;
70 if (!elem.read(data: der) || elem.type() != QAsn1Element::SequenceType)
71 return false;
72
73 const auto items = elem.toList();
74 if (items.size() != 2
75 || items[0].type() != QAsn1Element::SequenceType
76 || items[1].type() != QAsn1Element::OctetStringType) {
77 return false;
78 }
79
80 const auto encryptionSchemeContainer = items[0].toList();
81 if (encryptionSchemeContainer.size() != 2
82 || encryptionSchemeContainer[0].type() != QAsn1Element::ObjectIdentifierType
83 || encryptionSchemeContainer[1].type() != QAsn1Element::SequenceType) {
84 return false;
85 }
86
87 const QByteArray encryptionScheme = encryptionSchemeContainer[0].toObjectId();
88 return encryptionScheme == PKCS5_PBES2_ENCRYPTION_OID
89 || pbes1OIds.contains(t: encryptionScheme)
90 || encryptionScheme.startsWith(PKCS12_OID);
91}
92
93} // namespace QTlsPrivate
94
95QT_END_NAMESPACE
96
97
98

source code of qtbase/src/plugins/tls/shared/qtlskey_base.cpp