summaryrefslogtreecommitdiffstats
path: root/libkpgp/kpgpblock.cpp
blob: ba1d8fec223458eae6e97dd8012d873e0888a242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    kpgpblock.cpp

    Copyright (C) 2001,2002 the KPGP authors
    See file AUTHORS.kpgp for details

    This file is part of KPGP, the KDE PGP/GnuPG support library.

    KPGP is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "kpgpblock.h"
#include "kpgp.h"

#include <string.h>

namespace Kpgp {

Block::Block( const TQCString& str )
  : mText(str), mProcessedText(), mError(),
    mSignatureUserId(), mSignatureKeyId(), mSignatureDate(),
    mRequiredKey(), mEncryptedFor(),
    mStatus(0), mHasBeenProcessed(false), mType(NoPgpBlock)
{
  mEncryptedFor.setAutoDelete( true );
}

Block::~Block()
{
}

void
Block::reset()
{
  mProcessedText = TQCString();
  mError = TQCString();
  mSignatureUserId = TQString();
  mSignatureKeyId = TQCString();
  mSignatureDate = TQCString();
  mRequiredKey = TQCString();
  mEncryptedFor.clear();
  mStatus = 0;
  mHasBeenProcessed = false;
}

void
Block::clear()
{
  reset();
  mText = TQCString();
  mType = NoPgpBlock;
}

BlockType
Block::determineType() const
{
  if( !strncmp( mText.data(), "-----BEGIN PGP ", 15 ) )
  {
    if( !strncmp( mText.data() + 15, "SIGNED", 6 ) )
      return ClearsignedBlock;
    else if( !strncmp( mText.data() + 15, "SIGNATURE", 9 ) )
      return SignatureBlock;
    else if( !strncmp( mText.data() + 15, "PUBLIC", 6 ) )
      return PublicKeyBlock;
    else if( !strncmp( mText.data() + 15, "PRIVATE", 7 ) ||
             !strncmp( mText.data() + 15, "SECRET", 6 ) )
      return PrivateKeyBlock;
    else if( !strncmp( mText.data() + 15, "MESSAGE", 7 ) )
    {
      if( !strncmp( mText.data() + 22, ", PART", 6 ) )
        return MultiPgpMessageBlock;
      else
        return PgpMessageBlock;
    }
    else if( !strncmp( mText.data() + 15, "ARMORED FILE", 12 ) )
      return PgpMessageBlock;
    else
      return UnknownBlock;
  }
  else
    return NoPgpBlock;
}

bool
Block::decrypt()
{
  Kpgp::Module *pgp = Kpgp::Module::getKpgp();

  if( pgp == 0 )
    return false;

  return pgp->decrypt( *this );
}

bool
Block::verify()
{
  Kpgp::Module *pgp = Kpgp::Module::getKpgp();

  if( pgp == 0 )
    return false;

  return pgp->verify( *this );
}

Kpgp::Result
Block::clearsign( const TQCString& keyId, const TQCString& charset )
{
  Kpgp::Module *pgp = Kpgp::Module::getKpgp();

  if( pgp == 0 )
    return Kpgp::Failure;

  return pgp->clearsign( *this, keyId, charset );
}

Kpgp::Result
Block::encrypt( const TQStringList& tqreceivers, const TQCString& keyId,
                const bool sign, const TQCString& charset )
{
  Kpgp::Module *pgp = Kpgp::Module::getKpgp();

  if( pgp == 0 )
    return Kpgp::Failure;

  return pgp->encrypt( *this, tqreceivers, keyId, sign, charset );
}

} // namespace Kpgp