4 * 18-Mar-1997 - eay - A quick hack :-)
5 * version 1.1, it would probably help to save or load the
10 #include <openssl/err.h>
11 #include <openssl/asn1.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
17 /* The following two don't exist in SSLeay but they are in here as
19 #define PEM_write_SPKI(fp,x) \
20 PEM_ASN1_write((int (*)())i2d_NETSCAPE_SPKI,"SPKI",fp,\
21 (char *)x,NULL,NULL,0,NULL)
22 int SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey);
24 /* These are defined in the next version of SSLeay */
25 int EVP_PKEY_assign(EVP_PKEY *pkey, int type,char *key);
26 #define RSA_F4 0x10001
27 #define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
35 NETSCAPE_SPKI *spki=NULL;
45 /* Generate an RSA key, the random state should have been seeded
46 * with lots of calls to RAND_seed(....) */
47 fprintf(stderr,"generating RSA key, could take some time...\n");
48 if ((rsa=RSA_generate_key(512,RSA_F4,NULL)) == NULL) goto err;
52 if ((fp=fopen(argv[1],"r")) == NULL)
53 { perror(argv[1]); goto err; }
54 if ((rsa=PEM_read_RSAPrivateKey(fp,NULL,NULL)) == NULL)
59 if (!EVP_PKEY_assign_RSA(pkey,rsa)) goto err;
62 /* lets make the spki and set the public key and challenge */
63 if ((spki=NETSCAPE_SPKI_new()) == NULL) goto err;
65 if (!SPKI_set_pubkey(spki,pkey)) goto err;
67 fprintf(stderr,"please enter challenge string:");
70 fgets(buf,sizeof buf,stdin);
72 if (i > 0) buf[--i]='\0';
73 if (!ASN1_STRING_set((ASN1_STRING *)spki->spkac->challenge,
76 if (!NETSCAPE_SPKI_sign(spki,pkey,EVP_md5())) goto err;
77 PEM_write_SPKI(stdout,spki);
79 PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
85 fprintf(stderr,"something bad happened....");
86 ERR_print_errors_fp(stderr);
88 NETSCAPE_SPKI_free(spki);
93 /* This function is in the next version of SSLeay */
94 int EVP_PKEY_assign(pkey,type,key)
99 if (pkey == NULL) return(0);
100 if (pkey->pkey.ptr != NULL)
102 if (pkey->type == EVP_PKEY_RSA)
103 RSA_free(pkey->pkey.rsa);
104 /* else memory leak */
112 * X509_set_pubkey() and X509_REQ_set_pubkey(), SPKI_set_pubkey() does
113 * not currently exist so here is a version of it.
114 * The next SSLeay release will probably have
116 * X509_REQ_set_pubkey() and
117 * NETSCAPE_SPKI_set_pubkey()
118 * as macros calling the same function */
119 int SPKI_set_pubkey(x,pkey)
130 if (x == NULL) return(0);
132 if ((pk=X509_PUBKEY_new()) == NULL) goto err;
135 /* set the algorithm id */
136 if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
137 ASN1_OBJECT_free(a->algorithm);
140 /* Set the parameter list */
141 if ((a->parameter == NULL) || (a->parameter->type != V_ASN1_NULL))
143 ASN1_TYPE_free(a->parameter);
144 a->parameter=ASN1_TYPE_new();
145 a->parameter->type=V_ASN1_NULL;
147 i=i2d_PublicKey(pkey,NULL);
148 if ((s=(unsigned char *)malloc(i+1)) == NULL) goto err;
150 i2d_PublicKey(pkey,&p);
151 if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
154 X509_PUBKEY_free(x->spkac->pubkey);
159 if (pk != NULL) X509_PUBKEY_free(pk);