spkigen.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* NOCW */
  2. /* demos/spkigen.c
  3. * 18-Mar-1997 - eay - A quick hack :-)
  4. * version 1.1, it would probably help to save or load the
  5. * private key :-)
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <openssl/err.h>
  10. #include <openssl/asn1.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/pem.h>
  15. /* The following two don't exist in SSLeay but they are in here as
  16. * examples */
  17. #define PEM_write_SPKI(fp,x) \
  18. PEM_ASN1_write((int (*)())i2d_NETSCAPE_SPKI,"SPKI",fp,\
  19. (char *)x,NULL,NULL,0,NULL)
  20. int SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey);
  21. /* These are defined in the next version of SSLeay */
  22. int EVP_PKEY_assign(EVP_PKEY *pkey, int type,char *key);
  23. #define RSA_F4 0x10001
  24. #define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
  25. (char *)(rsa))
  26. int main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30. RSA *rsa=NULL;
  31. NETSCAPE_SPKI *spki=NULL;
  32. EVP_PKEY *pkey=NULL;
  33. char buf[128];
  34. int ok=0,i;
  35. FILE *fp;
  36. pkey=EVP_PKEY_new();
  37. if (argc < 2)
  38. {
  39. /* Generate an RSA key, the random state should have been seeded
  40. * with lots of calls to RAND_seed(....) */
  41. fprintf(stderr,"generating RSA key, could take some time...\n");
  42. if ((rsa=RSA_generate_key(512,RSA_F4,NULL)) == NULL) goto err;
  43. }
  44. else
  45. {
  46. if ((fp=fopen(argv[1],"r")) == NULL)
  47. { perror(argv[1]); goto err; }
  48. if ((rsa=PEM_read_RSAPrivateKey(fp,NULL,NULL)) == NULL)
  49. goto err;
  50. fclose(fp);
  51. }
  52. if (!EVP_PKEY_assign_RSA(pkey,rsa)) goto err;
  53. rsa=NULL;
  54. /* lets make the spki and set the public key and challenge */
  55. if ((spki=NETSCAPE_SPKI_new()) == NULL) goto err;
  56. if (!SPKI_set_pubkey(spki,pkey)) goto err;
  57. fprintf(stderr,"please enter challenge string:");
  58. fflush(stderr);
  59. buf[0]='\0';
  60. fgets(buf,sizeof buf,stdin);
  61. i=strlen(buf);
  62. if (i > 0) buf[--i]='\0';
  63. if (!ASN1_STRING_set((ASN1_STRING *)spki->spkac->challenge,
  64. buf,i)) goto err;
  65. if (!NETSCAPE_SPKI_sign(spki,pkey,EVP_md5())) goto err;
  66. PEM_write_SPKI(stdout,spki);
  67. if (argc < 2)
  68. PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
  69. ok=1;
  70. err:
  71. if (!ok)
  72. {
  73. fprintf(stderr,"something bad happened....");
  74. ERR_print_errors_fp(stderr);
  75. }
  76. NETSCAPE_SPKI_free(spki);
  77. EVP_PKEY_free(pkey);
  78. exit(!ok);
  79. }
  80. /* This function is in the next version of SSLeay */
  81. int EVP_PKEY_assign(pkey,type,key)
  82. EVP_PKEY *pkey;
  83. int type;
  84. char *key;
  85. {
  86. if (pkey == NULL) return(0);
  87. if (pkey->pkey.ptr != NULL)
  88. {
  89. if (pkey->type == EVP_PKEY_RSA)
  90. RSA_free(pkey->pkey.rsa);
  91. /* else memory leak */
  92. }
  93. pkey->type=type;
  94. pkey->pkey.ptr=key;
  95. return(1);
  96. }
  97. /* While I have a
  98. * X509_set_pubkey() and X509_REQ_set_pubkey(), SPKI_set_pubkey() does
  99. * not currently exist so here is a version of it.
  100. * The next SSLeay release will probably have
  101. * X509_set_pubkey(),
  102. * X509_REQ_set_pubkey() and
  103. * NETSCAPE_SPKI_set_pubkey()
  104. * as macros calling the same function */
  105. int SPKI_set_pubkey(x,pkey)
  106. NETSCAPE_SPKI *x;
  107. EVP_PKEY *pkey;
  108. {
  109. int ok=0;
  110. X509_PUBKEY *pk;
  111. X509_ALGOR *a;
  112. ASN1_OBJECT *o;
  113. unsigned char *s,*p;
  114. int i;
  115. if (x == NULL) return(0);
  116. if ((pk=X509_PUBKEY_new()) == NULL) goto err;
  117. a=pk->algor;
  118. /* set the algorithm id */
  119. if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
  120. ASN1_OBJECT_free(a->algorithm);
  121. a->algorithm=o;
  122. /* Set the parameter list */
  123. if ((a->parameter == NULL) || (a->parameter->type != V_ASN1_NULL))
  124. {
  125. ASN1_TYPE_free(a->parameter);
  126. a->parameter=ASN1_TYPE_new();
  127. a->parameter->type=V_ASN1_NULL;
  128. }
  129. i=i2d_PublicKey(pkey,NULL);
  130. if ((s=(unsigned char *)malloc(i+1)) == NULL) goto err;
  131. p=s;
  132. i2d_PublicKey(pkey,&p);
  133. if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
  134. free(s);
  135. X509_PUBKEY_free(x->spkac->pubkey);
  136. x->spkac->pubkey=pk;
  137. pk=NULL;
  138. ok=1;
  139. err:
  140. if (pk != NULL) X509_PUBKEY_free(pk);
  141. return(ok);
  142. }