Nelson Bolyard wrote:
[EMAIL PROTECTED] wrote:

Hi,

I would like to create a RSA public key (SECKEYPublicKey) from the modulus and the exponent expressed in binary form. Is it possible with NSS? I only found to do this with DER format. How can I convert the modulus and exponent in DER format and vice-versa?

Thanks for help, Philippe.


See news://news.mozilla.org:119/[EMAIL PROTECTED]

Hi,

Thanks for the link. Indeed this is the simplest method. Here is the code source to create a public rsa key from modulus and exponent:

[File: RawPubKey.c]

#include <stdio.h>

#include "pk11func.h"
#include "secerr.h"


int main(void) {

  /* Example values */
  unsigned char Modulus[] = {4,5,7,5,1,5,8,4,2,5,1,5,8,4,5,8,7,4};
  unsigned char Exponent[] = {3};
  unsigned int sizeModulus=18, sizeExponent=1;

  SECKEYPublicKey *newKey;
  PRArenaPool *arena;
  SECStatus rv;
  SECItem *modulus, *exponent;

  /* Creation of an arena  */
  arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  if (arena == NULL)
  {
          PORT_SetError (SEC_ERROR_NO_MEMORY);
          return 1;
  }

  /* Creation of the modulus SECItem */
  modulus = SECITEM_AllocItem(arena, NULL, sizeModulus);
  if (PORT_Memcpy(modulus->data, Modulus, sizeModulus) == NULL)
  {
    printf("Memcpy error\n");
    return 1;
  }

  exponent = SECITEM_AllocItem(arena, NULL, sizeExponent);
  if(PORT_Memcpy(exponent->data, Exponent, sizeExponent) == NULL)
  {
    printf("Memcpy error\n");
    return 1;
  }

/* Creation of the public key */
newKey = (SECKEYPublicKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPublicKey));
if (newKey != NULL)
{

rv = SECSuccess;
newKey->arena = arena;
newKey->keyType = rsaKey;
newKey->pkcs11Slot = NULL;
newKey->pkcs11ID = CK_INVALID_HANDLE;


        }

  /* Update the parameters modulus and exponent of the public key */
        rv = SECITEM_CopyItem(arena, &(newKey->u.rsa.modulus), modulus);

        if (rv == SECSuccess) {

rv = SECITEM_CopyItem(arena, &(newKey->u.rsa.publicExponent), exponent);
if (rv == SECSuccess)
{


      printf("Public Key created\n");
      /* Normally free memory here before leaving... ;) */
      return 0;

    }

  }

  printf("Error while creating key.\n");

  return 0;

}



Compile with:
gcc -lnss3 -I/$MOZ_PATH/mozilla/dist/public/security
-I/$MOZ_PATH/mozilla/dist/include/nspr RawPubKey.c -o RawPubKey

where $MOZ_PATH is your mozilla path directory.

Philippe.
_______________________________________________
mozilla-crypto mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-crypto

Reply via email to