~ubuntu-branches/ubuntu/natty/xml-security-c/natty-security

« back to all changes in this revision

Viewing changes to src/enc/OpenSSL/OpenSSLCryptoKeyRSA.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Russ Allbery
  • Date: 2009-08-05 14:11:52 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090805141152-a7xyrgur2sq3exkr
Tags: 1.5.1-1
* New upstream release.
  - Rename library package for upstream SONAME bump.
* Upstream now ships an older version of libtool, so run libtoolize and
  aclocal before the build.  Add build dependencies on automake and
  libtool.
* Build against Xerces-C 3.0.
* Stop building against Xalan.  The Xalan packages for Debian have been
  orphaned, the current Xalan release does not support Xerces-C 3.0, and
  porting it is not trivial.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 *
22
22
 * Author(s): Berin Lautenbach
23
23
 *
24
 
 * $Id: OpenSSLCryptoKeyRSA.cpp 566299 2007-08-15 18:51:47Z scantor $
 
24
 * $Id: OpenSSLCryptoKeyRSA.cpp 697146 2008-09-19 16:36:31Z scantor $
25
25
 *
26
26
 */
27
27
 
43
43
#include <memory.h>
44
44
 
45
45
OpenSSLCryptoKeyRSA::OpenSSLCryptoKeyRSA() :
 
46
mp_rsaKey(NULL),
46
47
mp_oaepParams(NULL),
47
48
m_oaepParamsLen(0) {
48
 
 
49
 
        // Create a new key to be loaded as we go
50
 
 
51
 
        mp_rsaKey = RSA_new();
52
 
 
53
49
};
 
50
 
54
51
OpenSSLCryptoKeyRSA::~OpenSSLCryptoKeyRSA() {
55
52
 
56
53
 
141
138
        m_oaepParamsLen = 0;
142
139
 
143
140
        mp_rsaKey = RSA_new();
144
 
        
 
141
 
145
142
        if (k == NULL || k->type != EVP_PKEY_RSA)
146
143
                return; // Nothing to do with us
147
144
 
175
172
//           Verify a signature encoded as a Base64 string
176
173
// --------------------------------------------------------------------------------
177
174
 
178
 
bool OpenSSLCryptoKeyRSA::verifySHA1PKCS1Base64Signature(const unsigned char * hashBuf, 
 
175
bool OpenSSLCryptoKeyRSA::verifySHA1PKCS1Base64Signature(const unsigned char * hashBuf,
179
176
                                                                 unsigned int hashLen,
180
177
                                                                 const char * base64Signature,
181
178
                                                                 unsigned int sigLen,
198
195
        char * cleanedBase64Signature;
199
196
        unsigned int cleanedBase64SignatureLen = 0;
200
197
 
201
 
        cleanedBase64Signature = 
 
198
        cleanedBase64Signature =
202
199
                XSECCryptoBase64::cleanBuffer(base64Signature, sigLen, cleanedBase64SignatureLen);
203
200
        ArrayJanitor<char> j_cleanedBase64Signature(cleanedBase64Signature);
204
201
 
205
202
        EVP_DecodeInit(&m_dctx);
206
 
        rc = EVP_DecodeUpdate(&m_dctx, 
207
 
                                                  sigVal, 
208
 
                                                  &sigValLen, 
209
 
                                                  (unsigned char *) cleanedBase64Signature, 
 
203
        rc = EVP_DecodeUpdate(&m_dctx,
 
204
                                                  sigVal,
 
205
                                                  &sigValLen,
 
206
                                                  (unsigned char *) cleanedBase64Signature,
210
207
                                                  cleanedBase64SignatureLen);
211
208
 
212
209
        if (rc < 0) {
216
213
        }
217
214
        int t = 0;
218
215
 
219
 
        EVP_DecodeFinal(&m_dctx, &sigVal[sigValLen], &t); 
 
216
        EVP_DecodeFinal(&m_dctx, &sigVal[sigValLen], &t);
220
217
 
221
218
        sigValLen += t;
222
219
 
235
232
        // padding should be and what the message digest OID should
236
233
        // be.
237
234
 
238
 
        int decryptSize = RSA_public_decrypt(sigValLen, 
239
 
                                                                                         sigVal, 
 
235
        int decryptSize = RSA_public_decrypt(sigValLen,
 
236
                                                                                         sigVal,
240
237
                                                                                         decryptBuf,
241
238
                                                                                         mp_rsaKey,
242
239
                                                                                         RSA_PKCS1_PADDING);
252
249
        /* Check the OID */
253
250
        int oidLen = 0;
254
251
        unsigned char * oid = getRSASigOID(hm, oidLen);
255
 
        
 
252
 
256
253
        if (oid == NULL) {
257
254
                throw XSECCryptoException(XSECCryptoException::RSAError,
258
255
                        "OpenSSL:RSA::verify() - Unsupported HASH algorithm for RSA");
261
258
        if (decryptSize != (int) (oidLen + hashLen) || hashLen != oid[oidLen-1]) {
262
259
 
263
260
                return false;
264
 
        
 
261
 
265
262
        }
266
263
 
267
264
        for (t = 0; t < oidLen; ++t) {
268
 
                
 
265
 
269
266
                if (oid[t] != decryptBuf[t]) {
270
267
 
271
268
                        return false;
316
313
        int oidLen;
317
314
        int encryptLen;
318
315
        int preEncryptLen;
319
 
        
 
316
 
320
317
        oid = getRSASigOID(hm, oidLen);
321
 
        
 
318
 
322
319
        if (oid == NULL) {
323
320
                throw XSECCryptoException(XSECCryptoException::RSAError,
324
321
                        "OpenSSL:RSA::sign() - Unsupported HASH algorithm for RSA");
386
383
// --------------------------------------------------------------------------------
387
384
 
388
385
unsigned int OpenSSLCryptoKeyRSA::privateDecrypt(const unsigned char * inBuf,
389
 
                                                                 unsigned char * plainBuf, 
 
386
                                                                 unsigned char * plainBuf,
390
387
                                                                 unsigned int inLength,
391
388
                                                                 unsigned int maxOutLength,
392
389
                                                                 PaddingType padding,
429
426
                                                        plainBuf,
430
427
                                                        mp_rsaKey,
431
428
                                                        RSA_PKCS1_PADDING);
432
 
                
 
429
 
433
430
                if (decryptSize < 0) {
434
431
 
435
432
                        throw XSECCryptoException(XSECCryptoException::RSAError,
516
513
// --------------------------------------------------------------------------------
517
514
 
518
515
unsigned int OpenSSLCryptoKeyRSA::publicEncrypt(const unsigned char * inBuf,
519
 
                                                                 unsigned char * cipherBuf, 
 
516
                                                                 unsigned char * cipherBuf,
520
517
                                                                 unsigned int inLength,
521
518
                                                                 unsigned int maxOutLength,
522
519
                                                                 PaddingType padding,
544
541
                                                        cipherBuf,
545
542
                                                        mp_rsaKey,
546
543
                                                        RSA_PKCS1_PADDING);
547
 
                
 
544
 
548
545
                if (encryptSize < 0) {
549
546
 
550
547
                        throw XSECCryptoException(XSECCryptoException::RSAError,
592
589
                                                                cipherBuf,
593
590
                                                                mp_rsaKey,
594
591
                                                                RSA_NO_PADDING);
595
 
                        
 
592
 
596
593
 
597
594
                        if (encryptSize < 0) {
598
595
 
650
647
                ret->m_oaepParamsLen = 0;
651
648
        }
652
649
 
653
 
        // Duplicate parameters 
 
650
        // Duplicate parameters
654
651
 
655
652
        if (mp_rsaKey->n)
656
653
                ret->mp_rsaKey->n = BN_dup(mp_rsaKey->n);