Browse code

external_pkcs1_sign: Support non-RSA_SIG_RAW hash_ids

For TLSv1.2, we need to support various hashes. (GPL) code taken from
PolarSSL pkcs11_sign().

Signed-off-by: Joachim Schipper <joachim.schipper@fox-it.com>
Acked-by: Arne Schwabe <arne@rfc2549.org>
Message-Id: <1379587649-25506-4-git-send-email-steffan.karger@fox-it.com>
URL: http://article.gmane.org/gmane.network.openvpn.devel/7887
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Joachim Schipper authored on 2013/09/19 19:47:29
Showing 1 changed files
... ...
@@ -7,6 +7,7 @@
7 7
  *
8 8
  *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9 9
  *  Copyright (C) 2010 Fox Crypto B.V. <openvpn@fox-it.com>
10
+ *  Copyright (C) 2006-2010, Brainspark B.V.
10 11
  *
11 12
  *  This program is free software; you can redistribute it and/or modify
12 13
  *  it under the terms of the GNU General Public License version 2
... ...
@@ -345,6 +346,8 @@ static inline int external_pkcs1_sign( void *ctx_voidptr,
345 345
   char *in_b64 = NULL;
346 346
   char *out_b64 = NULL;
347 347
   int rv;
348
+  unsigned char * const p = sig;
349
+  size_t asn_len;
348 350
 
349 351
   ASSERT(NULL != ctx);
350 352
 
... ...
@@ -355,15 +358,74 @@ static inline int external_pkcs1_sign( void *ctx_voidptr,
355 355
     }
356 356
 
357 357
   /*
358
-   * Normally (i.e. rsa_pkcs1_sign()), the padding is set in the context, and
359
-   * we have padding-specific code to handle various hash_id's here. Since the
360
-   * management client will RSA-sign the bytes we present without further
361
-   * processing, we only support SIG_RSA_RAW (PolarSSL's equivalent of
362
-   * OpenSSL's NID_md5_sha1).
358
+   * Support a wide range of hashes. TLSv1.1 and before only need SIG_RSA_RAW,
359
+   * but TLSv1.2 needs the full suite of hashes.
360
+   *
361
+   * This code has been taken from PolarSSL pkcs11_sign(), under the GPLv2.0+.
363 362
    */
364
-  ASSERT(hash_id == SIG_RSA_RAW);
363
+  switch( hash_id )
364
+  {
365
+      case SIG_RSA_RAW:
366
+          asn_len = 0;
367
+          memcpy( p, hash, hashlen );
368
+          break;
369
+
370
+      case SIG_RSA_MD2:
371
+          asn_len = OID_SIZE(ASN1_HASH_MDX);
372
+          memcpy( p, ASN1_HASH_MDX, asn_len );
373
+          memcpy( p + asn_len, hash, hashlen );
374
+          p[13] = 2; break;
375
+
376
+      case SIG_RSA_MD4:
377
+          asn_len = OID_SIZE(ASN1_HASH_MDX);
378
+          memcpy( p, ASN1_HASH_MDX, asn_len );
379
+          memcpy( p + asn_len, hash, hashlen );
380
+          p[13] = 4; break;
381
+
382
+      case SIG_RSA_MD5:
383
+          asn_len = OID_SIZE(ASN1_HASH_MDX);
384
+          memcpy( p, ASN1_HASH_MDX, asn_len );
385
+          memcpy( p + asn_len, hash, hashlen );
386
+          p[13] = 5; break;
387
+
388
+      case SIG_RSA_SHA1:
389
+          asn_len = OID_SIZE(ASN1_HASH_SHA1);
390
+          memcpy( p, ASN1_HASH_SHA1, asn_len );
391
+          memcpy( p + 15, hash, hashlen );
392
+          break;
393
+
394
+      case SIG_RSA_SHA224:
395
+          asn_len = OID_SIZE(ASN1_HASH_SHA2X);
396
+          memcpy( p, ASN1_HASH_SHA2X, asn_len );
397
+          memcpy( p + asn_len, hash, hashlen );
398
+          p[1] += hashlen; p[14] = 4; p[18] += hashlen; break;
399
+
400
+      case SIG_RSA_SHA256:
401
+          asn_len = OID_SIZE(ASN1_HASH_SHA2X);
402
+          memcpy( p, ASN1_HASH_SHA2X, asn_len );
403
+          memcpy( p + asn_len, hash, hashlen );
404
+          p[1] += hashlen; p[14] = 1; p[18] += hashlen; break;
405
+
406
+      case SIG_RSA_SHA384:
407
+          asn_len = OID_SIZE(ASN1_HASH_SHA2X);
408
+          memcpy( p, ASN1_HASH_SHA2X, asn_len );
409
+          memcpy( p + asn_len, hash, hashlen );
410
+          p[1] += hashlen; p[14] = 2; p[18] += hashlen; break;
411
+
412
+      case SIG_RSA_SHA512:
413
+          asn_len = OID_SIZE(ASN1_HASH_SHA2X);
414
+          memcpy( p, ASN1_HASH_SHA2X, asn_len );
415
+          memcpy( p + asn_len, hash, hashlen );
416
+          p[1] += hashlen; p[14] = 3; p[18] += hashlen; break;
417
+
418
+  /* End of copy */
419
+      default:
420
+          rv = POLARSSL_ERR_RSA_BAD_INPUT_DATA;
421
+	  goto done;
422
+  }
423
+
365 424
   /* convert 'from' to base64 */
366
-  if (openvpn_base64_encode (hash, hashlen, &in_b64) <= 0)
425
+  if (openvpn_base64_encode (sig, asn_len + hashlen, &in_b64) <= 0)
367 426
     {
368 427
       rv = POLARSSL_ERR_RSA_BAD_INPUT_DATA;
369 428
       goto done;