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>
18 * The following two don't exist in SSLeay but they are in here as examples
20 #define PEM_write_SPKI(fp,x) \
21 PEM_ASN1_write((int (*)())i2d_NETSCAPE_SPKI,"SPKI",fp,\
22 (char *)x,NULL,NULL,0,NULL)
23 int SPKI_set_pubkey(NETSCAPE_SPKI
*x
, EVP_PKEY
*pkey
);
25 /* These are defined in the next version of SSLeay */
26 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, char *key
);
27 #define RSA_F4 0x10001
28 #define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
36 NETSCAPE_SPKI
*spki
= NULL
;
37 EVP_PKEY
*pkey
= NULL
;
42 pkey
= EVP_PKEY_new();
46 * Generate an RSA key, the random state should have been seeded with
47 * lots of calls to RAND_seed(....)
49 fprintf(stderr
, "generating RSA key, could take some time...\n");
50 if ((rsa
= RSA_generate_key(512, RSA_F4
, NULL
)) == NULL
)
53 if ((fp
= fopen(argv
[1], "r")) == NULL
) {
57 if ((rsa
= PEM_read_RSAPrivateKey(fp
, NULL
, NULL
)) == NULL
)
62 if (!EVP_PKEY_assign_RSA(pkey
, rsa
))
66 /* lets make the spki and set the public key and challenge */
67 if ((spki
= NETSCAPE_SPKI_new()) == NULL
)
70 if (!SPKI_set_pubkey(spki
, pkey
))
73 fprintf(stderr
, "please enter challenge string:");
76 fgets(buf
, sizeof buf
, stdin
);
80 if (!ASN1_STRING_set((ASN1_STRING
*)spki
->spkac
->challenge
, buf
, i
))
83 if (!NETSCAPE_SPKI_sign(spki
, pkey
, EVP_md5()))
85 PEM_write_SPKI(stdout
, spki
);
87 PEM_write_RSAPrivateKey(stdout
, pkey
->pkey
.rsa
, NULL
, NULL
, 0, NULL
);
92 fprintf(stderr
, "something bad happened....");
93 ERR_print_errors_fp(stderr
);
95 NETSCAPE_SPKI_free(spki
);
100 /* This function is in the next version of SSLeay */
101 int EVP_PKEY_assign(pkey
, type
, key
)
108 if (pkey
->pkey
.ptr
!= NULL
) {
109 if (pkey
->type
== EVP_PKEY_RSA
)
110 RSA_free(pkey
->pkey
.rsa
);
111 /* else memory leak */
114 pkey
->pkey
.ptr
= key
;
119 * While I have a X509_set_pubkey() and X509_REQ_set_pubkey(),
120 * SPKI_set_pubkey() does not currently exist so here is a version of it. The
121 * next SSLeay release will probably have X509_set_pubkey(),
122 * X509_REQ_set_pubkey() and NETSCAPE_SPKI_set_pubkey() as macros calling the
125 int SPKI_set_pubkey(x
, pkey
)
133 unsigned char *s
, *p
;
139 if ((pk
= X509_PUBKEY_new()) == NULL
)
143 /* set the algorithm id */
144 if ((o
= OBJ_nid2obj(pkey
->type
)) == NULL
)
146 ASN1_OBJECT_free(a
->algorithm
);
149 /* Set the parameter list */
150 if ((a
->parameter
== NULL
) || (a
->parameter
->type
!= V_ASN1_NULL
)) {
151 ASN1_TYPE_free(a
->parameter
);
152 a
->parameter
= ASN1_TYPE_new();
153 a
->parameter
->type
= V_ASN1_NULL
;
155 i
= i2d_PublicKey(pkey
, NULL
);
156 if ((s
= (unsigned char *)malloc(i
+ 1)) == NULL
)
159 i2d_PublicKey(pkey
, &p
);
160 if (!ASN1_BIT_STRING_set(pk
->public_key
, s
, i
))
164 X509_PUBKEY_free(x
->spkac
->pubkey
);
165 x
->spkac
->pubkey
= pk
;
170 X509_PUBKEY_free(pk
);