Messagelib

cryptohelper.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Sandro KnauƟ <[email protected]>
3  SPDX-FileCopyrightText: 2001,2002 the KPGP authors
4  See file AUTHORS.kpgp for details
5 
6  SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "cryptohelper.h"
10 
11 using namespace MimeTreeParser;
12 
13 PGPBlockType Block::determineType() const
14 {
15  const QByteArray data = text();
16  if (data.startsWith("-----BEGIN PGP SIGNED")) {
17  return ClearsignedBlock;
18  } else if (data.startsWith("-----BEGIN PGP SIGNATURE")) {
19  return SignatureBlock;
20  } else if (data.startsWith("-----BEGIN PGP PUBLIC")) {
21  return PublicKeyBlock;
22  } else if (data.startsWith("-----BEGIN PGP PRIVATE") || data.startsWith("-----BEGIN PGP SECRET")) {
23  return PrivateKeyBlock;
24  } else if (data.startsWith("-----BEGIN PGP MESSAGE")) {
25  if (data.startsWith("-----BEGIN PGP MESSAGE PART")) {
26  return MultiPgpMessageBlock;
27  } else {
28  return PgpMessageBlock;
29  }
30  } else if (data.startsWith("-----BEGIN PGP ARMORED FILE")) {
31  return PgpMessageBlock;
32  } else if (data.startsWith("-----BEGIN PGP ")) {
33  return UnknownBlock;
34  } else {
35  return NoPgpBlock;
36  }
37 }
38 
39 QVector<Block> MimeTreeParser::prepareMessageForDecryption(const QByteArray &msg)
40 {
41  PGPBlockType pgpBlock = NoPgpBlock;
42  QVector<Block> blocks;
43  int start = -1; // start of the current PGP block
44  int lastEnd = -1; // end of the last PGP block
45  const int length = msg.length();
46 
47  if (msg.isEmpty()) {
48  return blocks;
49  }
50 
51  if (msg.startsWith("-----BEGIN PGP ")) {
52  start = 0;
53  } else {
54  start = msg.indexOf("\n-----BEGIN PGP ") + 1;
55  if (start == 0) {
56  blocks.append(Block(msg, NoPgpBlock));
57  return blocks;
58  }
59  }
60 
61  while (start != -1) {
62  int nextEnd;
63  int nextStart;
64 
65  // is the PGP block a clearsigned block?
66  if (!strncmp(msg.constData() + start + 15, "SIGNED", 6)) {
67  pgpBlock = ClearsignedBlock;
68  } else {
69  pgpBlock = UnknownBlock;
70  }
71 
72  nextEnd = msg.indexOf("\n-----END PGP ", start + 15);
73  nextStart = msg.indexOf("\n-----BEGIN PGP ", start + 15);
74 
75  if (nextEnd == -1) { // Missing END PGP line
76  if (lastEnd != -1) {
77  blocks.append(Block(msg.mid(lastEnd + 1), UnknownBlock));
78  } else {
79  blocks.append(Block(msg.mid(start), UnknownBlock));
80  }
81  break;
82  }
83 
84  if ((nextStart == -1) || (nextEnd < nextStart) || (pgpBlock == ClearsignedBlock)) {
85  // most likely we found a PGP block (but we don't check if it's valid)
86 
87  // store the preceding non-PGP block
88  if (start - lastEnd - 1 > 0) {
89  blocks.append(Block(msg.mid(lastEnd + 1, start - lastEnd - 1), NoPgpBlock));
90  }
91 
92  lastEnd = msg.indexOf("\n", nextEnd + 14);
93  if (lastEnd == -1) {
94  if (start < length) {
95  blocks.append(Block(msg.mid(start)));
96  }
97  break;
98  } else {
99  blocks.append(Block(msg.mid(start, lastEnd + 1 - start)));
100  if ((nextStart != -1) && (nextEnd > nextStart)) {
101  nextStart = msg.indexOf("\n-----BEGIN PGP ", lastEnd + 1);
102  }
103  }
104  }
105 
106  start = nextStart;
107 
108  if (start == -1) {
109  if (lastEnd + 1 < length) {
110  // rest of mail is no PGP Block
111  blocks.append(Block(msg.mid(lastEnd + 1), NoPgpBlock));
112  }
113  break;
114  } else {
115  start++; // move start behind the '\n'
116  }
117  }
118 
119  return blocks;
120 }
121 
122 Block::Block() = default;
123 
124 Block::Block(const QByteArray &m)
125  : msg(m)
126 {
127  mType = determineType();
128 }
129 
130 Block::Block(const QByteArray &m, PGPBlockType t)
131  : msg(m)
132  , mType(t)
133 {
134 }
135 
136 QByteArray MimeTreeParser::Block::text() const
137 {
138  return msg;
139 }
140 
141 PGPBlockType Block::type() const
142 {
143  return mType;
144 }
void append(const T &value)
bool isEmpty() const const
bool startsWith(const QByteArray &ba) const const
int length() const const
int indexOf(char ch, int from) const const
const char * constData() const const
QByteArray mid(int pos, int len) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.