- {
- RSA *rsa=NULL;
- NETSCAPE_SPKI *spki=NULL;
- EVP_PKEY *pkey=NULL;
- char buf[128];
- int ok=0,i;
- FILE *fp;
-
- pkey=EVP_PKEY_new();
-
- if (argc < 2)
- {
- /* Generate an RSA key, the random state should have been seeded
- * with lots of calls to RAND_seed(....) */
- fprintf(stderr,"generating RSA key, could take some time...\n");
- if ((rsa=RSA_generate_key(512,RSA_F4,NULL)) == NULL) goto err;
- }
- else
- {
- if ((fp=fopen(argv[1],"r")) == NULL)
- { perror(argv[1]); goto err; }
- if ((rsa=PEM_read_RSAPrivateKey(fp,NULL,NULL)) == NULL)
- goto err;
- fclose(fp);
- }
-
- if (!EVP_PKEY_assign_RSA(pkey,rsa)) goto err;
- rsa=NULL;
-
- /* lets make the spki and set the public key and challenge */
- if ((spki=NETSCAPE_SPKI_new()) == NULL) goto err;
-
- if (!SPKI_set_pubkey(spki,pkey)) goto err;
-
- fprintf(stderr,"please enter challenge string:");
- fflush(stderr);
- fgets(buf,120,stdin);
- i=strlen(buf);
- if (i > 0) buf[--i]='\0';
- if (!ASN1_STRING_set((ASN1_STRING *)spki->spkac->challenge,
- buf,i)) goto err;
-
- if (!NETSCAPE_SPKI_sign(spki,pkey,EVP_md5())) goto err;
- PEM_write_SPKI(stdout,spki);
- if (argc < 2)
- PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
-
- ok=1;
-err:
- if (!ok)
- {
- fprintf(stderr,"something bad happened....");
- ERR_print_errors_fp(stderr);
- }
- NETSCAPE_SPKI_free(spki);
- EVP_PKEY_free(pkey);
- exit(!ok);
- }
+{
+ RSA *rsa = NULL;
+ NETSCAPE_SPKI *spki = NULL;
+ EVP_PKEY *pkey = NULL;
+ char buf[128];
+ int ok = 0, i;
+ FILE *fp;
+
+ pkey = EVP_PKEY_new();
+
+ if (argc < 2) {
+ /*
+ * Generate an RSA key, the random state should have been seeded with
+ * lots of calls to RAND_seed(....)
+ */
+ fprintf(stderr, "generating RSA key, could take some time...\n");
+ if ((rsa = RSA_generate_key(512, RSA_F4, NULL)) == NULL)
+ goto err;
+ } else {
+ if ((fp = fopen(argv[1], "r")) == NULL) {
+ perror(argv[1]);
+ goto err;
+ }
+ if ((rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL)) == NULL)
+ goto err;
+ fclose(fp);
+ }
+
+ if (!EVP_PKEY_assign_RSA(pkey, rsa))
+ goto err;
+ rsa = NULL;
+
+ /* lets make the spki and set the public key and challenge */
+ if ((spki = NETSCAPE_SPKI_new()) == NULL)
+ goto err;
+
+ if (!SPKI_set_pubkey(spki, pkey))
+ goto err;
+
+ fprintf(stderr, "please enter challenge string:");
+ fflush(stderr);
+ buf[0] = '\0';
+ fgets(buf, sizeof buf, stdin);
+ i = strlen(buf);
+ if (i > 0)
+ buf[--i] = '\0';
+ if (!ASN1_STRING_set((ASN1_STRING *)spki->spkac->challenge, buf, i))
+ goto err;
+
+ if (!NETSCAPE_SPKI_sign(spki, pkey, EVP_md5()))
+ goto err;
+ PEM_write_SPKI(stdout, spki);
+ if (argc < 2)
+ PEM_write_RSAPrivateKey(stdout, pkey->pkey.rsa, NULL, NULL, 0, NULL);
+
+ ok = 1;
+ err:
+ if (!ok) {
+ fprintf(stderr, "something bad happened....");
+ ERR_print_errors_fp(stderr);
+ }
+ NETSCAPE_SPKI_free(spki);
+ EVP_PKEY_free(pkey);
+ exit(!ok);
+}