]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ocsp/ocsp_cid.c
The majority of the OCSP code from CertCo.
[thirdparty/openssl.git] / crypto / ocsp / ocsp_cid.c
1 /* ocsp_cid.c */
2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3 * project. */
4
5 /* History:
6 This file was originally part of ocsp.c and was transfered to Richard
7 Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included
8 in OpenSSL or released as a patch kit. */
9
10 /* ====================================================================
11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. All advertising materials mentioning features or use of this
26 * software must display the following acknowledgment:
27 * "This product includes software developed by the OpenSSL Project
28 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29 *
30 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31 * endorse or promote products derived from this software without
32 * prior written permission. For written permission, please contact
33 * openssl-core@openssl.org.
34 *
35 * 5. Products derived from this software may not be called "OpenSSL"
36 * nor may "OpenSSL" appear in their names without prior written
37 * permission of the OpenSSL Project.
38 *
39 * 6. Redistributions of any form whatsoever must retain the following
40 * acknowledgment:
41 * "This product includes software developed by the OpenSSL Project
42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
48 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55 * OF THE POSSIBILITY OF SUCH DAMAGE.
56 * ====================================================================
57 *
58 * This product includes cryptographic software written by Eric Young
59 * (eay@cryptsoft.com). This product includes software written by Tim
60 * Hudson (tjh@cryptsoft.com).
61 *
62 */
63
64 #include <openssl/bio.h>
65 #include <openssl/asn1_mac.h>
66 #include <openssl/err.h>
67 #include <openssl/ocsp.h>
68 #include <openssl/x509.h>
69
70 /* Make sure we work well with older variants of OpenSSL */
71 #ifndef OPENSSL_malloc
72 #define OPENSSL_malloc Malloc
73 #endif
74 #ifndef OPENSSL_realloc
75 #define OPENSSL_realloc Realloc
76 #endif
77 #ifndef OPENSSL_free
78 #define OPENSSL_free Free
79 #endif
80
81 OCSP_CERTID *OCSP_CERTID_new(void)
82 {
83 ASN1_CTX c;
84 OCSP_CERTID *ret=NULL;
85
86 M_ASN1_New_Malloc(ret, OCSP_CERTID);
87 M_ASN1_New(ret->hashAlgorithm, X509_ALGOR_new);
88 M_ASN1_New(ret->issuerNameHash, ASN1_OCTET_STRING_new);
89 M_ASN1_New(ret->issuerKeyHash, ASN1_OCTET_STRING_new);
90 M_ASN1_New(ret->serialNumber, ASN1_INTEGER_new);
91 return(ret);
92 M_ASN1_New_Error(ASN1_F_OCSP_CERTID_NEW);
93 }
94
95 void OCSP_CERTID_free(OCSP_CERTID *a)
96 {
97 if (a == NULL) return;
98 X509_ALGOR_free(a->hashAlgorithm);
99 ASN1_OCTET_STRING_free(a->issuerNameHash);
100 ASN1_OCTET_STRING_free(a->issuerKeyHash);
101 ASN1_INTEGER_free(a->serialNumber);
102 OPENSSL_free((char *)a);
103 }
104
105 int i2d_OCSP_CERTID(OCSP_CERTID *a,
106 unsigned char **pp)
107 {
108 M_ASN1_I2D_vars(a);
109
110 M_ASN1_I2D_len(a->hashAlgorithm, i2d_X509_ALGOR);
111 M_ASN1_I2D_len(a->issuerNameHash, i2d_ASN1_OCTET_STRING);
112 M_ASN1_I2D_len(a->issuerKeyHash, i2d_ASN1_OCTET_STRING);
113 M_ASN1_I2D_len(a->serialNumber, i2d_ASN1_INTEGER);
114 M_ASN1_I2D_seq_total();
115 M_ASN1_I2D_put(a->hashAlgorithm, i2d_X509_ALGOR);
116 M_ASN1_I2D_put(a->issuerNameHash, i2d_ASN1_OCTET_STRING);
117 M_ASN1_I2D_put(a->issuerKeyHash, i2d_ASN1_OCTET_STRING);
118 M_ASN1_I2D_put(a->serialNumber, i2d_ASN1_INTEGER);
119 M_ASN1_I2D_finish();
120 }
121
122 OCSP_CERTID *d2i_OCSP_CERTID(OCSP_CERTID **a,
123 unsigned char **pp,
124 long length)
125 {
126 M_ASN1_D2I_vars(a,OCSP_CERTID *,OCSP_CERTID_new);
127
128 M_ASN1_D2I_Init();
129 M_ASN1_D2I_start_sequence();
130 M_ASN1_D2I_get(ret->hashAlgorithm, d2i_X509_ALGOR);
131 M_ASN1_D2I_get(ret->issuerNameHash, d2i_ASN1_OCTET_STRING);
132 M_ASN1_D2I_get(ret->issuerKeyHash, d2i_ASN1_OCTET_STRING);
133 M_ASN1_D2I_get(ret->serialNumber, d2i_ASN1_INTEGER);
134
135 /* protect against malformed CERTID's */
136 if (ASN1_STRING_length(ret->issuerNameHash) == 0 ||
137 ASN1_STRING_length(ret->issuerKeyHash) == 0 ||
138 ASN1_STRING_length(ret->serialNumber) == 0)
139 goto err;
140
141 M_ASN1_D2I_Finish(a,OCSP_CERTID_free,ASN1_F_D2I_OCSP_CERTID);
142 }
143
144 int i2a_OCSP_CERTID(BIO *bp,
145 OCSP_CERTID* a)
146 {
147 #ifdef UNDEF
148 /* XXX this guy isn't implemented. */
149 i2a_X509_ALGOR(bp, a->hashAlgorithm);
150 #else /* instead, just show OID, not param */
151 i2a_ASN1_OBJECT(bp, a->hashAlgorithm->algorithm);
152 #endif
153 i2a_ASN1_STRING(bp, a->issuerNameHash, V_ASN1_OCTET_STRING);
154 i2a_ASN1_STRING(bp, a->issuerKeyHash, V_ASN1_OCTET_STRING);
155 i2a_ASN1_INTEGER(bp, a->serialNumber);
156 return 4;
157 }