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