File: sign.c

package info (click to toggle)
oath-toolkit 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,824 kB
  • ctags: 9,237
  • sloc: ansic: 54,713; sh: 13,738; yacc: 1,254; xml: 756; makefile: 353
file content (223 lines) | stat: -rw-r--r-- 6,338 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
/*
 * sign.c - Sign PSKC data with X.509 key/cert.
 * Copyright (C) 2012-2015 Simon Josefsson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <config.h>

#include <pskc/pskc.h>

#define INTERNAL_NEED_PSKC_STRUCT
#include "internal.h"
#include <xmlsec/xmlsec.h>
#include <xmlsec/crypto.h>
#include <xmlsec/xmltree.h>
#include <xmlsec/xmldsig.h>
#include <xmlsec/templates.h>

/**
 * pskc_sign_x509:
 * @container: a #pskc_t handle, from pskc_init().
 * @key_file: filename of file containing private key.
 * @cert_file: filename of file containing corresponding X.509 certificate.
 *
 * Sign PSKC data using X.509 certificate and private key.
 *
 * Returns: On success, %PSKC_OK (zero) is returned, or an error code.
 */
int
pskc_sign_x509 (pskc_t * container,
		const char *key_file, const char *cert_file)
{
  xmlNodePtr signNode;
  xmlNodePtr keyInfoNode = NULL;
  xmlNodePtr refNode = NULL;
  xmlSecDSigCtxPtr dsigCtx = NULL;

  pskc_build_xml (container, NULL, NULL);

  /* create signature template for RSA-SHA1 enveloped signature */
  signNode = xmlSecTmplSignatureCreate (container->xmldoc,
					xmlSecTransformExclC14NId,
					xmlSecTransformRsaSha1Id, NULL);
  if (signNode == NULL)
    {
      _pskc_debug ("xmlSecTmplSignatureCreateNsPref failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* add <dsig:Signature/> node to the doc */
  xmlAddChild (xmlDocGetRootElement (container->xmldoc), signNode);

  /* add reference */
  refNode = xmlSecTmplSignatureAddReference (signNode, xmlSecTransformSha1Id,
					     NULL, NULL, NULL);
  if (refNode == NULL)
    {
      _pskc_debug ("xmlSecTmplSignatureAddReference failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* add enveloped transform */
  if (xmlSecTmplReferenceAddTransform (refNode,
				       xmlSecTransformEnvelopedId) == NULL)
    {
      _pskc_debug ("xmlSecTmplReferenceAddTransform failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* add <dsig:KeyInfo/> and <dsig:X509Data/> */
  keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo (signNode, NULL);
  if (keyInfoNode == NULL)
    {
      _pskc_debug ("xmlSecTmplSignatureEnsureKeyInfo failed");
      return PSKC_XMLSEC_ERROR;
    }

  if (xmlSecTmplKeyInfoAddX509Data (keyInfoNode) == NULL)
    {
      _pskc_debug ("xmlSecTmplKeyInfoAddX509Data failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* create signature context, we don't need keys manager in this example */
  dsigCtx = xmlSecDSigCtxCreate (NULL);
  if (dsigCtx == NULL)
    {
      _pskc_debug ("xmlSecDSigCtxCreate failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* load private key, assuming that there is not password */
  dsigCtx->signKey = xmlSecCryptoAppKeyLoad (key_file,
					     xmlSecKeyDataFormatPem,
					     NULL, NULL, NULL);
  if (dsigCtx->signKey == NULL)
    {
      _pskc_debug ("xmlSecCryptoAppKeyLoad failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* load certificate and add to the key */
  if (xmlSecCryptoAppKeyCertLoad
      (dsigCtx->signKey, cert_file, xmlSecKeyDataFormatCertPem) < 0)
    {
      _pskc_debug ("xmlSecCryptoAppKeyCertLoad failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* set key name to the file name, this is just an example! */
  if (xmlSecKeySetName (dsigCtx->signKey, BAD_CAST key_file) < 0)
    {
      _pskc_debug ("xmlSecKeySetName failed");
      return PSKC_XMLSEC_ERROR;
    }

  /* sign the template */
  if (xmlSecDSigCtxSign (dsigCtx, signNode) < 0)
    {
      _pskc_debug ("xmlSecDSigCtxSign failed");
      return PSKC_XMLSEC_ERROR;
    }

  return PSKC_OK;
}

static int
verify (pskc_t * container, xmlSecKeysMngrPtr mngr,
	xmlSecDSigCtxPtr dsigCtx, const char *cert_file, int *valid_signature)
{
  xmlNodePtr node = NULL;

  node = xmlSecFindNode (xmlDocGetRootElement (container->xmldoc),
			 xmlSecNodeSignature, xmlSecDSigNs);
  if (node == NULL)
    {
      _pskc_debug ("xmlSecFindNode failed");
      return PSKC_XMLSEC_ERROR;
    }

  if (xmlSecCryptoAppDefaultKeysMngrInit (mngr) < 0)
    {
      _pskc_debug ("xmlSecCryptoAppDefaultKeysMngrInit failed");
      return PSKC_XMLSEC_ERROR;
    }

  if (xmlSecCryptoAppKeysMngrCertLoad (mngr, cert_file,
				       xmlSecKeyDataFormatPem,
				       xmlSecKeyDataTypeTrusted) < 0)
    {
      _pskc_debug ("xmlSecCryptoAppKeysMngrCertLoad failed");
      return PSKC_XMLSEC_ERROR;
    }

  if (xmlSecDSigCtxVerify (dsigCtx, node) < 0)
    {
      _pskc_debug ("xmlSecDSigCtxVerify failed");
      return PSKC_XMLSEC_ERROR;
    }

  if (dsigCtx->status == xmlSecDSigStatusSucceeded)
    *valid_signature = 1;
  else
    *valid_signature = 0;

  return PSKC_OK;
}

/**
 * pskc_verify_x509crt:
 * @container: a #pskc_t handle, from pskc_init().
 * @cert_file: filename of file containing trusted X.509 certificate.
 * @valid_signature: output variable with result of verification.
 *
 * Verify signature in PSKC data against trusted X.509 certificate.
 *
 * Returns: On success, %PSKC_OK (zero) is returned, or an error code.
 */
int
pskc_verify_x509crt (pskc_t * container, const char *cert_file,
		     int *valid_signature)
{
  xmlSecKeysMngrPtr mngr = NULL;
  xmlSecDSigCtxPtr dsigCtx = NULL;
  int rc;

  mngr = xmlSecKeysMngrCreate ();
  if (mngr == NULL)
    {
      _pskc_debug ("xmlSecKeysMngrCreate failed");
      return PSKC_XMLSEC_ERROR;
    }

  dsigCtx = xmlSecDSigCtxCreate (mngr);
  if (dsigCtx == NULL)
    {
      _pskc_debug ("xmlSecDSigCtxCreate failed");
      xmlSecKeysMngrDestroy (mngr);
      return PSKC_XMLSEC_ERROR;
    }

  rc = verify (container, mngr, dsigCtx, cert_file, valid_signature);

  xmlSecDSigCtxDestroy (dsigCtx);
  xmlSecKeysMngrDestroy (mngr);

  return rc;
}