• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com). */
55 
56 #include <openssl/evp.h>
57 
58 #include <openssl/bn.h>
59 #include <openssl/bytestring.h>
60 #include <openssl/digest.h>
61 #include <openssl/err.h>
62 #include <openssl/mem.h>
63 #include <openssl/rsa.h>
64 
65 #include "../fipsmodule/rsa/internal.h"
66 #include "../internal.h"
67 #include "internal.h"
68 
69 
70 static struct CRYPTO_STATIC_MUTEX g_buggy_lock = CRYPTO_STATIC_MUTEX_INIT;
71 static int g_buggy = 1;
72 
EVP_set_buggy_rsa_parser(int buggy)73 void EVP_set_buggy_rsa_parser(int buggy) {
74   CRYPTO_STATIC_MUTEX_lock_write(&g_buggy_lock);
75   g_buggy = buggy;
76   CRYPTO_STATIC_MUTEX_unlock_write(&g_buggy_lock);
77 }
78 
rsa_pub_encode(CBB * out,const EVP_PKEY * key)79 static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) {
80   /* See RFC 3279, section 2.3.1. */
81   CBB spki, algorithm, oid, null, key_bitstring;
82   if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
83       !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
84       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
85       !CBB_add_bytes(&oid, rsa_asn1_meth.oid, rsa_asn1_meth.oid_len) ||
86       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
87       !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
88       !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
89       !RSA_marshal_public_key(&key_bitstring, key->pkey.rsa) ||
90       !CBB_flush(out)) {
91     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
92     return 0;
93   }
94 
95   return 1;
96 }
97 
rsa_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)98 static int rsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
99   int buggy;
100   CRYPTO_STATIC_MUTEX_lock_read(&g_buggy_lock);
101   buggy = g_buggy;
102   CRYPTO_STATIC_MUTEX_unlock_read(&g_buggy_lock);
103 
104   /* See RFC 3279, section 2.3.1. */
105 
106   /* The parameters must be NULL. */
107   CBS null;
108   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) ||
109       CBS_len(&null) != 0 ||
110       CBS_len(params) != 0) {
111     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
112     return 0;
113   }
114 
115   /* Estonian IDs issued between September 2014 to September 2015 are
116    * broken. See https://crbug.com/532048 and https://crbug.com/534766.
117    *
118    * TODO(davidben): Switch this to the strict version in March 2016 or when
119    * Chromium can force client certificates down a different codepath, whichever
120    * comes first. */
121   RSA *rsa = buggy ? RSA_parse_public_key_buggy(key) : RSA_parse_public_key(key);
122   if (rsa == NULL || CBS_len(key) != 0) {
123     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
124     RSA_free(rsa);
125     return 0;
126   }
127 
128   EVP_PKEY_assign_RSA(out, rsa);
129   return 1;
130 }
131 
rsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)132 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
133   return BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) == 0 &&
134          BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) == 0;
135 }
136 
rsa_priv_encode(CBB * out,const EVP_PKEY * key)137 static int rsa_priv_encode(CBB *out, const EVP_PKEY *key) {
138   CBB pkcs8, algorithm, oid, null, private_key;
139   if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
140       !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
141       !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
142       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
143       !CBB_add_bytes(&oid, rsa_asn1_meth.oid, rsa_asn1_meth.oid_len) ||
144       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
145       !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
146       !RSA_marshal_private_key(&private_key, key->pkey.rsa) ||
147       !CBB_flush(out)) {
148     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
149     return 0;
150   }
151 
152   return 1;
153 }
154 
rsa_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)155 static int rsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
156   /* Per RFC 3447, A.1, the parameters have type NULL. */
157   CBS null;
158   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) ||
159       CBS_len(&null) != 0 ||
160       CBS_len(params) != 0) {
161     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
162     return 0;
163   }
164 
165   RSA *rsa = RSA_parse_private_key(key);
166   if (rsa == NULL || CBS_len(key) != 0) {
167     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
168     RSA_free(rsa);
169     return 0;
170   }
171 
172   EVP_PKEY_assign_RSA(out, rsa);
173   return 1;
174 }
175 
rsa_opaque(const EVP_PKEY * pkey)176 static int rsa_opaque(const EVP_PKEY *pkey) {
177   return RSA_is_opaque(pkey->pkey.rsa);
178 }
179 
int_rsa_size(const EVP_PKEY * pkey)180 static int int_rsa_size(const EVP_PKEY *pkey) {
181   return RSA_size(pkey->pkey.rsa);
182 }
183 
rsa_bits(const EVP_PKEY * pkey)184 static int rsa_bits(const EVP_PKEY *pkey) {
185   return BN_num_bits(pkey->pkey.rsa->n);
186 }
187 
int_rsa_free(EVP_PKEY * pkey)188 static void int_rsa_free(EVP_PKEY *pkey) { RSA_free(pkey->pkey.rsa); }
189 
190 const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
191   EVP_PKEY_RSA,
192   /* 1.2.840.113549.1.1.1 */
193   {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01}, 9,
194 
195   rsa_pub_decode,
196   rsa_pub_encode,
197   rsa_pub_cmp,
198 
199   rsa_priv_decode,
200   rsa_priv_encode,
201 
202   rsa_opaque,
203 
204   int_rsa_size,
205   rsa_bits,
206 
207   0,0,0,
208 
209   int_rsa_free,
210 };
211