xref: /openbsd-src/lib/libcrypto/x509/x509_pku.c (revision 8b5faa7170599a10b7304ec693b84d6d6fd0697f)
1*8b5faa71Stb /* $OpenBSD: x509_pku.c,v 1.5 2024/07/13 15:08:58 tb Exp $ */
2e500e238Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e500e238Sjsing  * project 1999.
4e500e238Sjsing  */
5e500e238Sjsing /* ====================================================================
6e500e238Sjsing  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7e500e238Sjsing  *
8e500e238Sjsing  * Redistribution and use in source and binary forms, with or without
9e500e238Sjsing  * modification, are permitted provided that the following conditions
10e500e238Sjsing  * are met:
11e500e238Sjsing  *
12e500e238Sjsing  * 1. Redistributions of source code must retain the above copyright
13e500e238Sjsing  *    notice, this list of conditions and the following disclaimer.
14e500e238Sjsing  *
15e500e238Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
16e500e238Sjsing  *    notice, this list of conditions and the following disclaimer in
17e500e238Sjsing  *    the documentation and/or other materials provided with the
18e500e238Sjsing  *    distribution.
19e500e238Sjsing  *
20e500e238Sjsing  * 3. All advertising materials mentioning features or use of this
21e500e238Sjsing  *    software must display the following acknowledgment:
22e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
23e500e238Sjsing  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24e500e238Sjsing  *
25e500e238Sjsing  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26e500e238Sjsing  *    endorse or promote products derived from this software without
27e500e238Sjsing  *    prior written permission. For written permission, please contact
28e500e238Sjsing  *    licensing@OpenSSL.org.
29e500e238Sjsing  *
30e500e238Sjsing  * 5. Products derived from this software may not be called "OpenSSL"
31e500e238Sjsing  *    nor may "OpenSSL" appear in their names without prior written
32e500e238Sjsing  *    permission of the OpenSSL Project.
33e500e238Sjsing  *
34e500e238Sjsing  * 6. Redistributions of any form whatsoever must retain the following
35e500e238Sjsing  *    acknowledgment:
36e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
37e500e238Sjsing  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38e500e238Sjsing  *
39e500e238Sjsing  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40e500e238Sjsing  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41e500e238Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42e500e238Sjsing  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43e500e238Sjsing  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44e500e238Sjsing  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45e500e238Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46e500e238Sjsing  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47e500e238Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48e500e238Sjsing  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49e500e238Sjsing  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50e500e238Sjsing  * OF THE POSSIBILITY OF SUCH DAMAGE.
51e500e238Sjsing  * ====================================================================
52e500e238Sjsing  *
53e500e238Sjsing  * This product includes cryptographic software written by Eric Young
54e500e238Sjsing  * (eay@cryptsoft.com).  This product includes software written by Tim
55e500e238Sjsing  * Hudson (tjh@cryptsoft.com).
56e500e238Sjsing  *
57e500e238Sjsing  */
58e500e238Sjsing 
59e500e238Sjsing #include <stdio.h>
60e500e238Sjsing 
61e500e238Sjsing #include <openssl/asn1.h>
62e500e238Sjsing #include <openssl/asn1t.h>
63e500e238Sjsing #include <openssl/x509v3.h>
64e500e238Sjsing 
65e500e238Sjsing static int i2r_PKEY_USAGE_PERIOD(X509V3_EXT_METHOD *method,
66e500e238Sjsing     PKEY_USAGE_PERIOD *usage, BIO *out, int indent);
67e500e238Sjsing 
68*8b5faa71Stb static const X509V3_EXT_METHOD x509v3_ext_private_key_usage_period = {
69e500e238Sjsing 	.ext_nid = NID_private_key_usage_period,
70e500e238Sjsing 	.ext_flags = 0,
71e500e238Sjsing 	.it = &PKEY_USAGE_PERIOD_it,
72e500e238Sjsing 	.ext_new = NULL,
73e500e238Sjsing 	.ext_free = NULL,
74e500e238Sjsing 	.d2i = NULL,
75e500e238Sjsing 	.i2d = NULL,
76e500e238Sjsing 	.i2s = NULL,
77e500e238Sjsing 	.s2i = NULL,
78e500e238Sjsing 	.i2v = NULL,
79e500e238Sjsing 	.v2i = NULL,
80e500e238Sjsing 	.i2r = (X509V3_EXT_I2R)i2r_PKEY_USAGE_PERIOD,
81e500e238Sjsing 	.r2i = NULL,
82e500e238Sjsing 	.usr_data = NULL,
83e500e238Sjsing };
84e500e238Sjsing 
85*8b5faa71Stb const X509V3_EXT_METHOD *
x509v3_ext_method_private_key_usage_period(void)86*8b5faa71Stb x509v3_ext_method_private_key_usage_period(void)
87*8b5faa71Stb {
88*8b5faa71Stb 	return &x509v3_ext_private_key_usage_period;
89*8b5faa71Stb }
90*8b5faa71Stb 
91e500e238Sjsing static const ASN1_TEMPLATE PKEY_USAGE_PERIOD_seq_tt[] = {
92e500e238Sjsing 	{
93e500e238Sjsing 		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
94e500e238Sjsing 		.tag = 0,
95e500e238Sjsing 		.offset = offsetof(PKEY_USAGE_PERIOD, notBefore),
96e500e238Sjsing 		.field_name = "notBefore",
97e500e238Sjsing 		.item = &ASN1_GENERALIZEDTIME_it,
98e500e238Sjsing 	},
99e500e238Sjsing 	{
100e500e238Sjsing 		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
101e500e238Sjsing 		.tag = 1,
102e500e238Sjsing 		.offset = offsetof(PKEY_USAGE_PERIOD, notAfter),
103e500e238Sjsing 		.field_name = "notAfter",
104e500e238Sjsing 		.item = &ASN1_GENERALIZEDTIME_it,
105e500e238Sjsing 	},
106e500e238Sjsing };
107e500e238Sjsing 
108e500e238Sjsing const ASN1_ITEM PKEY_USAGE_PERIOD_it = {
109e500e238Sjsing 	.itype = ASN1_ITYPE_SEQUENCE,
110e500e238Sjsing 	.utype = V_ASN1_SEQUENCE,
111e500e238Sjsing 	.templates = PKEY_USAGE_PERIOD_seq_tt,
112e500e238Sjsing 	.tcount = sizeof(PKEY_USAGE_PERIOD_seq_tt) / sizeof(ASN1_TEMPLATE),
113e500e238Sjsing 	.funcs = NULL,
114e500e238Sjsing 	.size = sizeof(PKEY_USAGE_PERIOD),
115e500e238Sjsing 	.sname = "PKEY_USAGE_PERIOD",
116e500e238Sjsing };
117c0ebdaf2Sbeck LCRYPTO_ALIAS(PKEY_USAGE_PERIOD_it);
118e500e238Sjsing 
119e500e238Sjsing 
120e500e238Sjsing PKEY_USAGE_PERIOD *
d2i_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD ** a,const unsigned char ** in,long len)121e500e238Sjsing d2i_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD **a, const unsigned char **in, long len)
122e500e238Sjsing {
123e500e238Sjsing 	return (PKEY_USAGE_PERIOD *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
124e500e238Sjsing 	    &PKEY_USAGE_PERIOD_it);
125e500e238Sjsing }
126cedac418Stb LCRYPTO_ALIAS(d2i_PKEY_USAGE_PERIOD);
127e500e238Sjsing 
128e500e238Sjsing int
i2d_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD * a,unsigned char ** out)129e500e238Sjsing i2d_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD *a, unsigned char **out)
130e500e238Sjsing {
131e500e238Sjsing 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &PKEY_USAGE_PERIOD_it);
132e500e238Sjsing }
133cedac418Stb LCRYPTO_ALIAS(i2d_PKEY_USAGE_PERIOD);
134e500e238Sjsing 
135e500e238Sjsing PKEY_USAGE_PERIOD *
PKEY_USAGE_PERIOD_new(void)136e500e238Sjsing PKEY_USAGE_PERIOD_new(void)
137e500e238Sjsing {
138e500e238Sjsing 	return (PKEY_USAGE_PERIOD *)ASN1_item_new(&PKEY_USAGE_PERIOD_it);
139e500e238Sjsing }
140cedac418Stb LCRYPTO_ALIAS(PKEY_USAGE_PERIOD_new);
141e500e238Sjsing 
142e500e238Sjsing void
PKEY_USAGE_PERIOD_free(PKEY_USAGE_PERIOD * a)143e500e238Sjsing PKEY_USAGE_PERIOD_free(PKEY_USAGE_PERIOD *a)
144e500e238Sjsing {
145e500e238Sjsing 	ASN1_item_free((ASN1_VALUE *)a, &PKEY_USAGE_PERIOD_it);
146e500e238Sjsing }
147cedac418Stb LCRYPTO_ALIAS(PKEY_USAGE_PERIOD_free);
148e500e238Sjsing 
149e500e238Sjsing static int
i2r_PKEY_USAGE_PERIOD(X509V3_EXT_METHOD * method,PKEY_USAGE_PERIOD * usage,BIO * out,int indent)150e500e238Sjsing i2r_PKEY_USAGE_PERIOD(X509V3_EXT_METHOD *method, PKEY_USAGE_PERIOD *usage,
151e500e238Sjsing     BIO *out, int indent)
152e500e238Sjsing {
153e500e238Sjsing 	BIO_printf(out, "%*s", indent, "");
154e500e238Sjsing 	if (usage->notBefore) {
155e500e238Sjsing 		BIO_write(out, "Not Before: ", 12);
156e500e238Sjsing 		ASN1_GENERALIZEDTIME_print(out, usage->notBefore);
157e500e238Sjsing 		if (usage->notAfter)
158e500e238Sjsing 			BIO_write(out, ", ", 2);
159e500e238Sjsing 	}
160e500e238Sjsing 	if (usage->notAfter) {
161e500e238Sjsing 		BIO_write(out, "Not After: ", 11);
162e500e238Sjsing 		ASN1_GENERALIZEDTIME_print(out, usage->notAfter);
163e500e238Sjsing 	}
164e500e238Sjsing 	return 1;
165e500e238Sjsing }
166