File: simaccon.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (255 lines) | stat: -rw-r--r-- 6,557 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 *
 *  Copyright (C) 1998-2023, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module: dcmsign
 *
 *  Author: Marco Eichelberg
 *
 *  Purpose:
 *    classes: SiMACConstructor
 *
 */

#include "dcmtk/config/osconfig.h"

#ifdef WITH_OPENSSL

#include "dcmtk/dcmsign/simaccon.h"
#include "dcmtk/dcmsign/simac.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcvrat.h"
#include "dcmtk/dcmdata/dcwcache.h"

// block size used for the memory buffer
#define SiMACConstructor_BlockSize 16384


SiMACConstructor::SiMACConstructor()
: buf(new unsigned char[SiMACConstructor_BlockSize])
, stream(buf, SiMACConstructor_BlockSize)
, dumpFile(NULL)
{
}


SiMACConstructor::~SiMACConstructor()
{
  delete[] buf;
}

void SiMACConstructor::setDumpFile(FILE *f)
{
  dumpFile = f;
}


OFCondition SiMACConstructor::flushBuffer(SiMAC& mac)
{
  OFCondition result = EC_Normal;
  void *bufptr = NULL;
  offile_off_t bufLen = 0;
  stream.flushBuffer(bufptr, bufLen);
  if (bufLen > 0)
  {
    if (dumpFile)
    {
      if (fwrite(bufptr, 1, OFstatic_cast(size_t, bufLen), dumpFile) != OFstatic_cast(size_t, bufLen))
      {
        // We are apparently unable to write the byte stream to a dump file.
        // This does not prevent us, however, from creating a valid digital signature.
        // Therefore, issue a warning but continue.
        DCMSIGN_WARN("Write error while dumping byte stream to file");
      }
    }
    result = mac.digest((unsigned char *)bufptr, (unsigned long)bufLen);
  }
  return result;
}


OFCondition SiMACConstructor::encodeElement(DcmElement *element, SiMAC& mac, E_TransferSyntax oxfer)
{
  if (element == NULL) return EC_IllegalCall;
  DcmWriteCache wcache;
  OFCondition result = EC_Normal;
  OFBool last = OFFalse;
  element->transferInit();
  while (!last)
  {
    result = element->writeSignatureFormat(stream, oxfer, EET_ExplicitLength, &wcache);
    if (result == EC_StreamNotifyClient) result = flushBuffer(mac);
    else
    {
      last=OFTrue;
    }
  }
  element->transferEnd();
  return result;
}


OFCondition SiMACConstructor::flush(SiMAC& mac)
{
  OFCondition result = EC_Normal;
  while (! stream.isFlushed() && result.good())
  {
    stream.flush();
    result = flushBuffer(mac);
  }
  return result;
}


OFBool SiMACConstructor::inTagList(const DcmElement *element, DcmAttributeTag *tagList)
{
  if (element == NULL) return OFFalse;
  if (tagList == NULL) return OFTrue; // tag list absent, use all tags

  DcmTagKey key;
  const DcmTag &elementTag = element->getTag();
  unsigned long vm = tagList->getVM();
  for (unsigned long i=0; i < vm; i++)
  {
    if ((tagList->getTagVal(key, i)).good() && (key == elementTag)) return OFTrue;
  }
  return OFFalse;
}

OFCondition SiMACConstructor::encodeDigitalSignatureItem(
  DcmItem& signatureItem,
  SiMAC& mac,
  E_TransferSyntax oxfer)
{
  if (! signatureItem.canWriteXfer(oxfer, EXS_Unknown)) return SI_EC_WrongTransferSyntax;
  OFCondition result = EC_Normal;
  signatureItem.transferInit();
  unsigned long numElements = signatureItem.card();
  DcmElement *element;
  DcmTagKey tagkey;
  for (unsigned long i=0; i < numElements; i++)
  {
    element = signatureItem.getElement(i);
    if (result.good())
    {
      if (element->isSignable())
      {
        tagkey = element->getTag().getTagKey();
        if ((tagkey != DCM_CertificateOfSigner) &&
            (tagkey != DCM_Signature) &&
            (tagkey != DCM_CertifiedTimestampType) &&
            (tagkey != DCM_CertifiedTimestamp))
        {
          result = encodeElement(element, mac, oxfer);
        }
      }
    }
  }

  /* done, flush stream buffer */
  result = flushBuffer(mac);
  signatureItem.transferEnd();
  return result;
}


OFCondition SiMACConstructor::encodeDataset(
  DcmItem& item,
  SiMAC& mac,
  E_TransferSyntax oxfer,
  DcmAttributeTag &tagListOut,
  DcmAttributeTag *tagListIn)
{
  tagListOut.clear();
  if (! item.canWriteXfer(oxfer, EXS_Unknown)) return SI_EC_WrongTransferSyntax;
  OFCondition result = EC_Normal;
  item.transferInit();
  unsigned long numElements = item.card();
  DcmElement *element;
  for (unsigned long i=0; i < numElements; i++)
  {
    element = item.getElement(i);
    if (result.good() && (inTagList(element, tagListIn)))
    {
      // if the element is signable, we should encode it
      if (element->isSignable())
      {
        result = encodeElement(element, mac, oxfer);
        if (result.good())
        {
          result = tagListOut.putTagVal(element->getTag(), tagListOut.getVM());
        }
      }
      else
      {
        // the element is unsignable, but either the user or the active
        // signature profile have requested its inclusion in the signature.
        if (tagListIn)
        {
          // print a warning
          DcmTag tag(element->getTag()); // we need to create a temporary copy of the tag
          DCMSIGN_INFO("List of attributes to be signed contains unsignable element " << tag << " " << tag.getTagName() );
          result = SI_EC_AttributeNotSignable;
        }
      }
    }
  }

  /* done, flush stream buffer */
  if (result.good()) result = flushBuffer(mac);
  item.transferEnd();
  return result;
}


OFCondition SiMACConstructor::encodeDatasetForVerification(
  DcmItem& item,
  SiMAC& mac,
  E_TransferSyntax oxfer,
  DcmAttributeTag *tagListIn)
{
  if (! item.canWriteXfer(oxfer, EXS_Unknown)) return SI_EC_WrongTransferSyntax;

  OFCondition result = EC_Normal;
  item.transferInit();
  unsigned long numElements = item.card();
  DcmElement *element;
  for (unsigned long i=0; i < numElements; i++)
  {
    element = item.getElement(i);
    if (result.good() && (inTagList(element, tagListIn)))
    {
      if (element->isSignable())
      {
        // element is signable, we should encode it
        result = encodeElement(element, mac, oxfer);
      }
      else
      {
        // print a warning
        DcmTag tag(element->getTag()); // we need to create a temporary copy of the tag
        DCMSIGN_INFO("  Signature contains unsignable element " << tag << " " << tag.getTagName() );
        result = SI_EC_VerificationFailed_AttributeNotSignable;
      }
    }
  }

  /* done, flush stream buffer */
  if (result.good()) result = flushBuffer(mac);
  item.transferEnd();
  return result;
}
#else /* WITH_OPENSSL */

int simaccon_cc_dummy_to_keep_linker_from_moaning = 0;

#endif