3 * 18-Mar-1997 - eay - A quick hack :-)
4 * version 1.1, it would probably help to save or load the
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>
16 /* The following two don't exist in SSLeay but they are in here as
18 #define PEM_write_SPKI(fp,x) \
19 PEM_ASN1_write((int (*)())i2d_NETSCAPE_SPKI,"SPKI",fp,\
20 (char *)x,NULL,NULL,0,NULL)
21 int SPKI_set_pubkey(NETSCAPE_SPKI
*x
, EVP_PKEY
*pkey
);
23 /* These are defined in the next version of SSLeay */
24 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
,char *key
);
25 #define RSA_F4 0x10001
26 #define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
34 NETSCAPE_SPKI
*spki
=NULL
;
44 /* Generate an RSA key, the random state should have been seeded
45 * with lots of calls to RAND_seed(....) */
46 fprintf(stderr
,"generating RSA key, could take some time...\n");
47 if ((rsa
=RSA_generate_key(512,RSA_F4
,NULL
)) == NULL
) goto err
;
51 if ((fp
=fopen(argv
[1],"r")) == NULL
)
52 { perror(argv
[1]); goto err
; }
53 if ((rsa
=PEM_read_RSAPrivateKey(fp
,NULL
,NULL
)) == NULL
)
58 if (!EVP_PKEY_assign_RSA(pkey
,rsa
)) goto err
;
61 /* lets make the spki and set the public key and challenge */
62 if ((spki
=NETSCAPE_SPKI_new()) == NULL
) goto err
;
64 if (!SPKI_set_pubkey(spki
,pkey
)) goto err
;
66 fprintf(stderr
,"please enter challenge string:");
69 fgets(buf
,sizeof buf
,stdin
);
71 if (i
> 0) buf
[--i
]='\0';
72 if (!ASN1_STRING_set((ASN1_STRING
*)spki
->spkac
->challenge
,
75 if (!NETSCAPE_SPKI_sign(spki
,pkey
,EVP_md5())) goto err
;
76 PEM_write_SPKI(stdout
,spki
);
78 PEM_write_RSAPrivateKey(stdout
,pkey
->pkey
.rsa
,NULL
,NULL
,0,NULL
);
84 fprintf(stderr
,"something bad happened....");
85 ERR_print_errors_fp(stderr
);
87 NETSCAPE_SPKI_free(spki
);
92 /* This function is in the next version of SSLeay */
93 int EVP_PKEY_assign(pkey
,type
,key
)
98 if (pkey
== NULL
) return(0);
99 if (pkey
->pkey
.ptr
!= NULL
)
101 if (pkey
->type
== EVP_PKEY_RSA
)
102 RSA_free(pkey
->pkey
.rsa
);
103 /* else memory leak */
111 * X509_set_pubkey() and X509_REQ_set_pubkey(), SPKI_set_pubkey() does
112 * not currently exist so here is a version of it.
113 * The next SSLeay release will probably have
115 * X509_REQ_set_pubkey() and
116 * NETSCAPE_SPKI_set_pubkey()
117 * as macros calling the same function */
118 int SPKI_set_pubkey(x
,pkey
)
129 if (x
== NULL
) return(0);
131 if ((pk
=X509_PUBKEY_new()) == NULL
) goto err
;
134 /* set the algorithm id */
135 if ((o
=OBJ_nid2obj(pkey
->type
)) == NULL
) goto err
;
136 ASN1_OBJECT_free(a
->algorithm
);
139 /* Set the parameter list */
140 if ((a
->parameter
== NULL
) || (a
->parameter
->type
!= V_ASN1_NULL
))
142 ASN1_TYPE_free(a
->parameter
);
143 a
->parameter
=ASN1_TYPE_new();
144 a
->parameter
->type
=V_ASN1_NULL
;
146 i
=i2d_PublicKey(pkey
,NULL
);
147 if ((s
=(unsigned char *)malloc(i
+1)) == NULL
) goto err
;
149 i2d_PublicKey(pkey
,&p
);
150 if (!ASN1_BIT_STRING_set(pk
->public_key
,s
,i
)) goto err
;
153 X509_PUBKEY_free(x
->spkac
->pubkey
);
158 if (pk
!= NULL
) X509_PUBKEY_free(pk
);